Skip to content

Commit

Permalink
Move GetFileURL to a better place (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong authored Jan 23, 2025
1 parent d2b90c6 commit 3c1a99b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CoreEditor/src/api/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function onEditorReady(listener: (editorView: EditorView) => void) {
}

export async function getFileInfo(): Promise<FileInfo | undefined> {
const info = await window.nativeModules.core.getFileInfo();
const info = await window.nativeModules.api.getFileInfo();

// eslint-disable-next-line compat/compat
return new Promise(resolve => {
Expand Down
1 change: 1 addition & 0 deletions CoreEditor/src/bridge/native/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { WebPoint } from '../../@types/WebPoint';
* @bridgeName NativeBridgeAPI
*/
export interface NativeModuleAPI extends NativeModule {
getFileInfo(): Promise<string | undefined>;
addMainMenuItems({ items }: { items: WebMenuItem[] }): void;
showContextMenu(args: { items: WebMenuItem[]; location: WebPoint }): void;
showAlert(args: { title?: string; message?: string; buttons?: string[] }): Promise<CodeGen_Int>;
Expand Down
1 change: 0 additions & 1 deletion CoreEditor/src/bridge/native/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { LineColumnInfo } from '../../modules/selection/types';
* @bridgeName NativeBridgeCore
*/
export interface NativeModuleCore extends NativeModule {
getFileInfo(): Promise<string | undefined>;
notifyWindowDidLoad(): void;
notifyBackgroundColorDidChange({ color }: { color: CodeGen_Int }): void;
notifyViewportScaleDidChange(): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import MarkEditCore

@MainActor
public protocol NativeModuleAPI: NativeModule {
func getFileInfo() -> String?
func addMainMenuItems(items: [WebMenuItem])
func showContextMenu(items: [WebMenuItem], location: WebPoint)
func showAlert(title: String?, message: String?, buttons: [String]?) -> Int
Expand All @@ -26,6 +27,9 @@ public extension NativeModuleAPI {
final class NativeBridgeAPI: NativeBridge {
static let name = "api"
lazy var methods: [String: NativeMethod] = [
"getFileInfo": { [weak self] in
self?.getFileInfo(parameters: $0)
},
"addMainMenuItems": { [weak self] in
self?.addMainMenuItems(parameters: $0)
},
Expand All @@ -47,6 +51,11 @@ final class NativeBridgeAPI: NativeBridge {
self.module = module
}

private func getFileInfo(parameters: Data) -> Result<Any?, Error>? {
let result = module.getFileInfo()
return .success(result)
}

private func addMainMenuItems(parameters: Data) -> Result<Any?, Error>? {
struct Message: Decodable {
var items: [WebMenuItem]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import MarkEditCore

@MainActor
public protocol NativeModuleCore: NativeModule {
func getFileInfo() -> String?
func notifyWindowDidLoad()
func notifyBackgroundColorDidChange(color: Int)
func notifyViewportScaleDidChange()
Expand All @@ -31,9 +30,6 @@ public extension NativeModuleCore {
final class NativeBridgeCore: NativeBridge {
static let name = "core"
lazy var methods: [String: NativeMethod] = [
"getFileInfo": { [weak self] in
self?.getFileInfo(parameters: $0)
},
"notifyWindowDidLoad": { [weak self] in
self?.notifyWindowDidLoad(parameters: $0)
},
Expand Down Expand Up @@ -67,11 +63,6 @@ final class NativeBridgeCore: NativeBridge {
self.module = module
}

private func getFileInfo(parameters: Data) -> Result<Any?, Error>? {
let result = module.getFileInfo()
return .success(result)
}

private func notifyWindowDidLoad(parameters: Data) -> Result<Any?, Error>? {
module.notifyWindowDidLoad()
return .success(nil)
Expand Down
19 changes: 19 additions & 0 deletions MarkEditKit/Sources/Bridge/Native/Modules/EditorModuleAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CryptoKit

@MainActor
public protocol EditorModuleAPIDelegate: AnyObject {
func editorAPIGetFileURL(_ sender: EditorModuleAPI) -> URL?
func editorAPI(_ sender: EditorModuleAPI, addMainMenuItems items: [(String, WebMenuItem)])
func editorAPI(_ sender: EditorModuleAPI, showContextMenu items: [WebMenuItem], location: WebPoint)
func editorAPI(
Expand All @@ -32,6 +33,24 @@ public final class EditorModuleAPI: NativeModuleAPI {
self.delegate = delegate
}

public func getFileInfo() -> String? {
guard let fileURL = delegate?.editorAPIGetFileURL(self) else {
return nil
}

let attributes = try? FileManager.default.attributesOfItem(atPath: fileURL.path)
Logger.assert(attributes != nil, "Cannot get file attributes of: \(fileURL)")

let json: [String: Any] = [
"filePath": fileURL.path,
"fileSize": Double(attributes?[.size] as? Int64 ?? 0),
"creationDate": (attributes?[.creationDate] as? Date ?? .distantPast).timeIntervalSince1970,
"modificationDate": (attributes?[.modificationDate] as? Date ?? .distantPast).timeIntervalSince1970,
]

return try? JSONSerialization.data(withJSONObject: json).toString()
}

public func addMainMenuItems(items: [WebMenuItem]) {
delegate?.editorAPI(self, addMainMenuItems: items.map { item in
let hash = SHA256.hash(data: Data(item.uniqueID.utf8))
Expand Down
19 changes: 0 additions & 19 deletions MarkEditKit/Sources/Bridge/Native/Modules/EditorModuleCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Foundation

@MainActor
public protocol EditorModuleCoreDelegate: AnyObject {
func editorCoreGetFileURL(_ sender: EditorModuleCore) -> URL?
func editorCoreWindowDidLoad(_ sender: EditorModuleCore)
func editorCoreBackgroundColorDidChange(_ sender: EditorModuleCore, color: UInt32)
func editorCoreViewportScaleDidChange(_ sender: EditorModuleCore)
Expand All @@ -32,24 +31,6 @@ public final class EditorModuleCore: NativeModuleCore {
self.delegate = delegate
}

public func getFileInfo() -> String? {
guard let fileURL = delegate?.editorCoreGetFileURL(self) else {
return nil
}

let attributes = try? FileManager.default.attributesOfItem(atPath: fileURL.path)
Logger.assert(attributes != nil, "Cannot get file attributes of: \(fileURL)")

let json: [String: Any] = [
"filePath": fileURL.path,
"fileSize": Double(attributes?[.size] as? Int64 ?? 0),
"creationDate": (attributes?[.creationDate] as? Date ?? .distantPast).timeIntervalSince1970,
"modificationDate": (attributes?[.modificationDate] as? Date ?? .distantPast).timeIntervalSince1970,
]

return try? JSONSerialization.data(withJSONObject: json).toString()
}

public func notifyWindowDidLoad() {
delegate?.editorCoreWindowDidLoad(self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ extension EditorViewController: EditorWebViewActionDelegate {
// MARK: - EditorModuleCoreDelegate

extension EditorViewController: EditorModuleCoreDelegate {
func editorCoreGetFileURL(_ sender: EditorModuleCore) -> URL? {
document?.fileURL
}

func editorCoreWindowDidLoad(_ sender: EditorModuleCore) {
hasFinishedLoading = true
resetEditor()
Expand Down Expand Up @@ -251,6 +247,10 @@ extension EditorViewController: EditorModulePreviewDelegate {
// MARK: - EditorModuleAPIDelegate

extension EditorViewController: EditorModuleAPIDelegate {
func editorAPIGetFileURL(_ sender: EditorModuleAPI) -> URL? {
document?.fileURL
}

func editorAPI(_ sender: EditorModuleAPI, addMainMenuItems items: [(String, WebMenuItem)]) {
addMainMenuItems(items: items)
}
Expand Down

0 comments on commit 3c1a99b

Please sign in to comment.