feat: done create new doc

This commit is contained in:
Lakr
2025-04-04 16:22:50 +08:00
committed by LongYinan
parent a7db5c5027
commit 486e047b6d
6 changed files with 74 additions and 8 deletions
@@ -18,6 +18,27 @@ extension AFFiNEViewController: IntelligentsButtonDelegate, IntelligentsFocusApe
button.beginProgress() 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() let group = DispatchGroup()
group.enter() group.enter()
@@ -67,6 +88,7 @@ extension AFFiNEViewController: IntelligentsButtonDelegate, IntelligentsFocusApe
webView?.scrollView.contentOffset = contentOffset webView?.scrollView.contentOffset = contentOffset
} }
let focus = IntelligentsFocusApertureView() let focus = IntelligentsFocusApertureView()
self.focusView = focus
focus.prepareAnimationWith( focus.prepareAnimationWith(
capturingTargetContentView: webView ?? .init(), capturingTargetContentView: webView ?? .init(),
coveringRootViewController: self coveringRootViewController: self
@@ -2,6 +2,7 @@ import Capacitor
import Intelligents import Intelligents
import UIKit import UIKit
@objc(AFFiNEViewController)
class AFFiNEViewController: CAPBridgeViewController { class AFFiNEViewController: CAPBridgeViewController {
var baseUrl: String? { var baseUrl: String? {
didSet { Intelligents.setUpstreamEndpoint(baseUrl ?? "") } didSet { Intelligents.setUpstreamEndpoint(baseUrl ?? "") }
@@ -9,6 +10,7 @@ class AFFiNEViewController: CAPBridgeViewController {
var documentID: String? var documentID: String?
var workspaceID: String? var workspaceID: String?
var documentContent: String? var documentContent: String?
var focusView: IntelligentsFocusApertureView?
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@@ -6,7 +6,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? var window: UIWindow?
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
true true
} }
@@ -9,10 +9,11 @@ import Foundation
import WebKit import WebKit
enum ApplicationBridgedWindowScript: String { enum ApplicationBridgedWindowScript: String {
case getCurrentDocContentInMarkdown = "return await window.getCurrentDocContentInMarkdown();" case getCurrentDocContentInMarkdown = ###"return await window.getCurrentDocContentInMarkdown();"###
case getCurrentServerBaseUrl = "window.getCurrentServerBaseUrl()" case getCurrentServerBaseUrl = ###"window.getCurrentServerBaseUrl()"###
case getCurrentWorkspaceId = "window.getCurrentWorkspaceId();" case getCurrentWorkspaceId = ###"window.getCurrentWorkspaceId();"###
case getCurrentDocId = "window.getCurrentDocId();" case getCurrentDocId = ###"window.getCurrentDocId();"###
case createNewDocument = ###"window.createNewDocByMarkdownInCurrentWorkspace($ARG_1$, $ARG_2$)"###
var requiresAsyncContext: Bool { var requiresAsyncContext: Bool {
switch self { switch self {
@@ -23,10 +24,24 @@ enum ApplicationBridgedWindowScript: String {
} }
extension WKWebView { 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 { if script.requiresAsyncContext {
callAsyncJavaScript( callAsyncJavaScript(
script.rawValue, sourceCode,
arguments: [:], arguments: [:],
in: nil, in: nil,
in: .page in: .page
@@ -39,7 +54,9 @@ extension WKWebView {
} }
} }
} else { } else {
evaluateJavaScript(script.rawValue) { output, _ in callback(output) } evaluateJavaScript(sourceCode) { output, _ in
callback(output)
}
} }
} }
} }
@@ -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) = { }
}
}
@@ -131,6 +131,10 @@ public class IntelligentsEphemeralActionController: UIViewController {
guard let self else { return } guard let self else { return }
continueToChat() continueToChat()
} }
actionBar.createNewDoc.action = { [weak self] in
guard let self else { return }
createNewDoc()
}
} }
func setupContentViews() { func setupContentViews() {
@@ -294,4 +298,10 @@ extension IntelligentsEphemeralActionController {
chatController.metadata[.content] = documentContent chatController.metadata[.content] = documentContent
navigationController?.pushViewController(chatController, animated: true) navigationController?.pushViewController(chatController, animated: true)
} }
func createNewDoc() {
let content = copilotDocumentStorage
Intelligents.Delegates.createNewDocument("New Document", content)
Intelligents.Delegates.dismissAll()
}
} }