mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-29 08:09:52 +08:00
feat: done create new doc
This commit is contained in:
@@ -17,6 +17,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()
|
||||||
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
@@ -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) = { }
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
@@ -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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user