Skip to content

Migrate from WebView to WKWebView #111

Description

@schuyler

Overview

MacDown currently uses the deprecated WebView class for rendering the Markdown preview. This should be migrated to the modern WKWebView API.

Why This Is Important

1. WebView is Deprecated

  • WebView was deprecated in macOS 10.14 (Mojave)
  • Apple recommends migrating to WKWebView for all new development
  • Future macOS versions may remove WebView support entirely

2. Technical Benefits

  • Better performance: WKWebView uses a separate process for rendering
  • Modern web standards: Better JavaScript, CSS, and HTML5 support
  • Improved security: Better sandboxing and security features
  • Proper cache control: No need for private API hacks
  • Better rendering: More accurate rendering of modern web content

3. Fixes Multiple Issues

Migrating to WKWebView could potentially resolve:

Current WebView Usage

Locations in the codebase:

Migration Challenges

API Differences

WebView WKWebView Notes
loadHTMLString:baseURL: loadHTMLString:baseURL: Similar API
stringByEvaluatingJavaScriptFromString: evaluateJavaScript:completionHandler: Async in WKWebView
WebFrameLoadDelegate WKNavigationDelegate Different delegate pattern
mainFrame.DOMDocument No direct DOM access Need JavaScript bridge
Synchronous JS execution Asynchronous JS execution Requires code changes

Key Changes Required

  1. Asynchronous JavaScript Execution

    • All stringByEvaluatingJavaScriptFromString: calls need to be converted to async
    • May require significant refactoring of code that depends on JS results
  2. No Direct DOM Access

    • The commented-out DOM update code (lines 1072-1112 in MPDocument.m) uses DOMDocument
    • Would need to use JavaScript instead: document.documentElement.innerHTML = newHTML
  3. Delegate Method Migration

    • Convert WebFrameLoadDelegate methods to WKNavigationDelegate
    • Update method signatures and behavior
  4. Cache Control

    • Replace private WebCache API with WKWebsiteDataStore
    • Proper cache management for images
  5. Testing

    • Thorough testing of all Markdown features
    • Verify JavaScript libraries work correctly (MathJax, Prism, Mermaid, etc.)
    • Test scroll position preservation
    • Verify all delegate callbacks work correctly

Implementation Plan

Phase 1: Research and Planning

  • Audit all WebView usage in the codebase
  • Document all WebView delegate methods used
  • Identify all JavaScript execution points
  • Research WKWebView cache management APIs
  • Create migration design document

Phase 2: Core Migration

  • Replace WebView with WKWebView in MPDocument
  • Migrate delegate methods to WKNavigationDelegate
  • Convert synchronous JS calls to async
  • Implement proper cache control using WKWebsiteDataStore

Phase 3: JavaScript Library Integration

  • Verify MathJax works correctly
  • Verify Prism syntax highlighting works
  • Verify Mermaid diagram rendering works
  • Verify Graphviz/Viz rendering works
  • Test task list functionality

Phase 4: Testing and Polish

  • Test all Markdown features
  • Test with large documents
  • Verify scroll position preservation
  • Test image loading and caching
  • Performance testing
  • Edge case testing

Phase 5: Image Cache Management

Success Criteria

Priority

Medium-High - Not urgent since WebView still works, but important for:

Estimated Effort

Large - This is a significant refactoring:

  • Multiple files affected
  • Async/await pattern changes required
  • Extensive testing needed
  • Potential for subtle bugs

Estimate: 1-2 weeks of focused development + 1 week of testing

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthighImportant for release, significant impact

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions