@@ -423,136 +423,3 @@ private final class GutenbergEditorController: NSObject, WKNavigationDelegate, W
423423 }
424424 }
425425}
426-
427- private class EditorAssetsProvider : NSObject , WKScriptMessageHandlerWithReply {
428- let library : EditorAssetsLibrary
429-
430- init ( library: EditorAssetsLibrary ) {
431- self . library = library
432- }
433-
434- func userContentController( _ userContentController: WKUserContentController , didReceive message: WKScriptMessage , replyHandler: @escaping @MainActor @Sendable ( Any ? , String ? ) -> Void ) {
435- guard let payload = message. body as? NSDictionary ,
436- let asset = payload. object ( forKey: " asset " ) as? String ,
437- asset == " manifest "
438- else {
439- replyHandler ( nil , " Unexpected message " )
440- return
441- }
442-
443- Task . detached { [ library] in
444- do {
445- let data = try await library. manifestContentForEditor ( )
446- let dict = try JSONSerialization . jsonObject ( with: data)
447- await replyHandler ( dict, nil )
448- } catch {
449- await replyHandler ( nil , error. localizedDescription)
450- }
451- }
452- }
453- }
454-
455- class CachedAssetSchemeHandler : NSObject , WKURLSchemeHandler {
456- nonisolated static let cachedURLSchemePrefix = " gbk-cache- "
457- nonisolated static let supportedURLSchemes = [ " gbk-cache-http " , " gbk-cache-https " ]
458-
459- nonisolated static func originalHTTPURL( from url: URL ) -> URL ? {
460- guard let scheme = url. scheme, supportedURLSchemes. contains ( scheme) else { return nil }
461-
462- guard var components = URLComponents ( url: url, resolvingAgainstBaseURL: true ) else {
463- return nil
464- }
465-
466- components. scheme = String ( scheme. suffix ( from: scheme. index ( scheme. startIndex, offsetBy: cachedURLSchemePrefix. count) ) )
467- return components. url
468- }
469-
470- nonisolated static func cachedURL( forWebLink link: String ) -> String ? {
471- if link. starts ( with: " http:// " ) || link. starts ( with: " https:// " ) {
472- return cachedURLSchemePrefix + link
473- }
474- return nil
475- }
476-
477- let worker : Worker
478-
479- init ( library: EditorAssetsLibrary ) {
480- self . worker = . init( library: library)
481- }
482-
483- func webView( _ webView: WKWebView , start urlSchemeTask: any WKURLSchemeTask ) {
484- Task {
485- await worker. start ( urlSchemeTask)
486- }
487- }
488-
489- func webView( _ webView: WKWebView , stop urlSchemeTask: any WKURLSchemeTask ) {
490- Task {
491- await worker. stop ( urlSchemeTask)
492- }
493- }
494-
495- actor Worker {
496- struct TaskInfo {
497- var webViewTask : WKURLSchemeTask
498- var fetchAssetTask : Task < Void , Never >
499-
500- func cancel( ) {
501- fetchAssetTask. cancel ( )
502- }
503- }
504-
505- let library : EditorAssetsLibrary
506- var tasks : [ ObjectIdentifier : TaskInfo ] = [ : ]
507-
508- init ( library: EditorAssetsLibrary ) {
509- self . library = library
510- }
511-
512- deinit {
513- for (_, task) in tasks {
514- task. cancel ( )
515- }
516- }
517-
518- func start( _ task: WKURLSchemeTask ) {
519- guard let url = task. request. url, let httpURL = CachedAssetSchemeHandler . originalHTTPURL ( from: url) else {
520- task. didFailWithError ( URLError ( . badURL) )
521- return
522- }
523-
524- let taskKey = ObjectIdentifier ( task)
525-
526- let fetchAssetTask = Task { [ library, weak self] in
527- do {
528- let ( response, content) = try await library. cacheAsset ( from: httpURL, webViewURL: url)
529-
530- await self ? . tasks [ taskKey] ? . webViewTask. didReceive ( response)
531- await self ? . tasks [ taskKey] ? . webViewTask. didReceive ( content)
532-
533- await self ? . finish ( with: nil , taskKey: taskKey)
534- } catch {
535- await self ? . finish ( with: error, taskKey: taskKey)
536- }
537- }
538- tasks [ taskKey] = . init( webViewTask: task, fetchAssetTask: fetchAssetTask)
539- }
540-
541- func stop( _ task: WKURLSchemeTask ) {
542- let taskKey = ObjectIdentifier ( task)
543- tasks [ taskKey] ? . cancel ( )
544- tasks [ taskKey] = nil
545- }
546-
547- private func finish( with error: Error ? , taskKey: ObjectIdentifier ) {
548- guard let task = tasks [ taskKey] else { return }
549-
550- if let error {
551- task. webViewTask. didFailWithError ( error)
552- } else {
553- task. webViewTask. didFinish ( )
554- }
555- tasks [ taskKey] = nil
556- }
557- }
558- }
0 commit comments