From 486e047b6d9a2d64c6b5f60fc5de97058b7b78f8 Mon Sep 17 00:00:00 2001 From: Lakr Date: Fri, 4 Apr 2025 16:22:50 +0800 Subject: [PATCH] feat: done create new doc --- .../App/AffineViewController+AIButton.swift | 22 +++++++++++++ .../ios/App/App/AffineViewController.swift | 2 ++ .../apps/ios/App/App/AppDelegate.swift | 1 - .../App/ApplicationBridgedWindowScript.swift | 31 ++++++++++++++----- .../Backend/Constant/Delegates.swift | 16 ++++++++++ ...ntelligentsEphemeralActionController.swift | 10 ++++++ 6 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 packages/frontend/apps/ios/App/Packages/Intelligents/Sources/Intelligents/Backend/Constant/Delegates.swift diff --git a/packages/frontend/apps/ios/App/App/AffineViewController+AIButton.swift b/packages/frontend/apps/ios/App/App/AffineViewController+AIButton.swift index e34d2da57b..206c8a9b4d 100644 --- a/packages/frontend/apps/ios/App/App/AffineViewController+AIButton.swift +++ b/packages/frontend/apps/ios/App/App/AffineViewController+AIButton.swift @@ -17,6 +17,27 @@ extension AFFiNEViewController: IntelligentsButtonDelegate, IntelligentsFocusApe } button.beginProgress() + + // associate current view controller to callbacks + Intelligents.Delegates.createNewDocument = { title, content in + print("[*] creating new document \(title) \(content)") + webView.evaluateScript( + .createNewDocument, + arguments: [content, title] + ) { _ in } + } + Intelligents.Delegates.dismissAll = { [weak self] in + guard let self else { return } + if self.presentedViewController != nil { + self.dismiss(animated: true) + } + if let focusView = self.focusView { + focusView.executeAnimationDismiss() { + focusView.removeFromSuperview() + } + } + self.focusView = nil + } let group = DispatchGroup() @@ -67,6 +88,7 @@ extension AFFiNEViewController: IntelligentsButtonDelegate, IntelligentsFocusApe webView?.scrollView.contentOffset = contentOffset } let focus = IntelligentsFocusApertureView() + self.focusView = focus focus.prepareAnimationWith( capturingTargetContentView: webView ?? .init(), coveringRootViewController: self diff --git a/packages/frontend/apps/ios/App/App/AffineViewController.swift b/packages/frontend/apps/ios/App/App/AffineViewController.swift index b17db0815b..ecc32c98f7 100644 --- a/packages/frontend/apps/ios/App/App/AffineViewController.swift +++ b/packages/frontend/apps/ios/App/App/AffineViewController.swift @@ -2,6 +2,7 @@ import Capacitor import Intelligents import UIKit +@objc(AFFiNEViewController) class AFFiNEViewController: CAPBridgeViewController { var baseUrl: String? { didSet { Intelligents.setUpstreamEndpoint(baseUrl ?? "") } @@ -9,6 +10,7 @@ class AFFiNEViewController: CAPBridgeViewController { var documentID: String? var workspaceID: String? var documentContent: String? + var focusView: IntelligentsFocusApertureView? override func viewDidLoad() { super.viewDidLoad() diff --git a/packages/frontend/apps/ios/App/App/AppDelegate.swift b/packages/frontend/apps/ios/App/App/AppDelegate.swift index fae3240822..8bbed9f9a5 100644 --- a/packages/frontend/apps/ios/App/App/AppDelegate.swift +++ b/packages/frontend/apps/ios/App/App/AppDelegate.swift @@ -6,7 +6,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { - // Override point for customization after application launch. true } diff --git a/packages/frontend/apps/ios/App/App/ApplicationBridgedWindowScript.swift b/packages/frontend/apps/ios/App/App/ApplicationBridgedWindowScript.swift index 06f275a71c..bc84261792 100644 --- a/packages/frontend/apps/ios/App/App/ApplicationBridgedWindowScript.swift +++ b/packages/frontend/apps/ios/App/App/ApplicationBridgedWindowScript.swift @@ -9,10 +9,11 @@ import Foundation import WebKit enum ApplicationBridgedWindowScript: String { - case getCurrentDocContentInMarkdown = "return await window.getCurrentDocContentInMarkdown();" - case getCurrentServerBaseUrl = "window.getCurrentServerBaseUrl()" - case getCurrentWorkspaceId = "window.getCurrentWorkspaceId();" - case getCurrentDocId = "window.getCurrentDocId();" + case getCurrentDocContentInMarkdown = ###"return await window.getCurrentDocContentInMarkdown();"### + case getCurrentServerBaseUrl = ###"window.getCurrentServerBaseUrl()"### + case getCurrentWorkspaceId = ###"window.getCurrentWorkspaceId();"### + case getCurrentDocId = ###"window.getCurrentDocId();"### + case createNewDocument = ###"window.createNewDocByMarkdownInCurrentWorkspace($ARG_1$, $ARG_2$)"### var requiresAsyncContext: Bool { switch self { @@ -23,10 +24,24 @@ enum ApplicationBridgedWindowScript: String { } extension WKWebView { - func evaluateScript(_ script: ApplicationBridgedWindowScript, callback: @escaping (Any?) -> ()) { + func evaluateScript(_ script: ApplicationBridgedWindowScript, arguments: [String] = [], callback: @escaping (Any?) -> ()) { + let escapedArguments = arguments.map { arg in + let encoded = arg.data(using: .utf8)?.base64EncodedString() ?? "" + return "atob('\(encoded)')" + } + var sourceCode = script.rawValue + for (idx, argument) in escapedArguments.enumerated() { + sourceCode = sourceCode.replacingOccurrences( + of: "$ARG_\(idx + 1)$", + with: argument + ) + } +#if DEBUG + print("[*] evaluating script: \(sourceCode)") +#endif if script.requiresAsyncContext { callAsyncJavaScript( - script.rawValue, + sourceCode, arguments: [:], in: nil, in: .page @@ -39,7 +54,9 @@ extension WKWebView { } } } else { - evaluateJavaScript(script.rawValue) { output, _ in callback(output) } + evaluateJavaScript(sourceCode) { output, _ in + callback(output) + } } } } diff --git a/packages/frontend/apps/ios/App/Packages/Intelligents/Sources/Intelligents/Backend/Constant/Delegates.swift b/packages/frontend/apps/ios/App/Packages/Intelligents/Sources/Intelligents/Backend/Constant/Delegates.swift new file mode 100644 index 0000000000..4c8e90db10 --- /dev/null +++ b/packages/frontend/apps/ios/App/Packages/Intelligents/Sources/Intelligents/Backend/Constant/Delegates.swift @@ -0,0 +1,16 @@ +// +// File.swift +// Intelligents +// +// Created by 秋星桥 on 4/4/25. +// + +import Foundation + + +public extension Intelligents { + enum Delegates { + public static var createNewDocument: ((_ title: String, _ content: String) -> Void) = { _, _ in } + public static var dismissAll: (() -> Void) = { } + } +} diff --git a/packages/frontend/apps/ios/App/Packages/Intelligents/Sources/Intelligents/Interface/IntelligentsEphemeralActionController/IntelligentsEphemeralActionController.swift b/packages/frontend/apps/ios/App/Packages/Intelligents/Sources/Intelligents/Interface/IntelligentsEphemeralActionController/IntelligentsEphemeralActionController.swift index 6794ffe43f..20dbba8236 100644 --- a/packages/frontend/apps/ios/App/Packages/Intelligents/Sources/Intelligents/Interface/IntelligentsEphemeralActionController/IntelligentsEphemeralActionController.swift +++ b/packages/frontend/apps/ios/App/Packages/Intelligents/Sources/Intelligents/Interface/IntelligentsEphemeralActionController/IntelligentsEphemeralActionController.swift @@ -131,6 +131,10 @@ public class IntelligentsEphemeralActionController: UIViewController { guard let self else { return } continueToChat() } + actionBar.createNewDoc.action = { [weak self] in + guard let self else { return } + createNewDoc() + } } func setupContentViews() { @@ -294,4 +298,10 @@ extension IntelligentsEphemeralActionController { chatController.metadata[.content] = documentContent navigationController?.pushViewController(chatController, animated: true) } + + func createNewDoc() { + let content = copilotDocumentStorage + Intelligents.Delegates.createNewDocument("New Document", content) + Intelligents.Delegates.dismissAll() + } }