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

View File

@@ -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

View File

@@ -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()

View File

@@ -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
}

View File

@@ -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)
}
}
}
}

View File

@@ -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) = { }
}
}

View File

@@ -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()
}
}