mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-04 00:28:33 +00:00
feat: done create new doc
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 }
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user