Skip to content

Commit b3211d3

Browse files
authored
Implement MarkEdit.openFile() (#1218)
1 parent 205a8ad commit b3211d3

File tree

10 files changed

+53
-9
lines changed

10 files changed

+53
-9
lines changed

CoreEditor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"glob": "^12.0.0",
4343
"jest": "^30.2.0",
4444
"jest-environment-jsdom": "^30.2.0",
45-
"markedit-api": "https://github.com/MarkEdit-app/MarkEdit-api#v0.20.0",
45+
"markedit-api": "https://github.com/MarkEdit-app/MarkEdit-api#v0.21.0",
4646
"rollup": "^4.0.0",
4747
"ts-gyb": "^0.12.0",
4848
"ts-jest": "^29.4.5",

CoreEditor/src/api/files.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { CreateFileOptions, DirectoryType, FileInfo, FileObject } from 'markedit-api';
22

3+
export async function openFile(path: string): Promise<boolean> {
4+
return window.nativeModules.api.openFile({ path });
5+
}
6+
37
export async function createFile(options: CreateFileOptions): Promise<boolean> {
48
return window.nativeModules.api.createFile({ options });
59
}

CoreEditor/src/api/modules.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { languageModel } from './languageModel';
1919

2020
import { onEditorReady, addExtension, addMarkdownConfig, addCodeLanguage } from './methods';
2121
import { addMainMenuItem, showContextMenu, showAlert, showTextBox, showSavePanel, runService } from './ui';
22-
import { createFile, deleteFile, listFiles, getFileContent, getFileObject, getFileInfo, getDirectoryPath } from './files';
22+
import { openFile, createFile, deleteFile, listFiles, getFileContent, getFileObject, getFileInfo, getDirectoryPath } from './files';
2323
import { getPasteboardItems, getPasteboardString } from './pasteboard';
2424

2525
export function initMarkEditModules() {
@@ -50,6 +50,7 @@ export function initMarkEditModules() {
5050
MarkEdit.lezer = lezer;
5151

5252
MarkEdit.onEditorReady = onEditorReady;
53+
MarkEdit.openFile = openFile;
5354
MarkEdit.createFile = createFile;
5455
MarkEdit.deleteFile = deleteFile;
5556
MarkEdit.listFiles = listFiles;

CoreEditor/src/bridge/native/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { WebPoint } from '../../@types/WebPoint';
99
* @bridgeName NativeBridgeAPI
1010
*/
1111
export interface NativeModuleAPI extends NativeModule {
12+
openFile({ path }: { path: string }): Promise<boolean>;
1213
createFile({ options }: { options: CreateFileOptions }): Promise<boolean>;
1314
deleteFile({ path }: { path: string }): Promise<boolean>;
1415
listFiles({ path }: { path: string }): Promise<string[] | undefined>;

CoreEditor/yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5059,9 +5059,9 @@ __metadata:
50595059
languageName: node
50605060
linkType: hard
50615061

5062-
"markedit-api@https://github.com/MarkEdit-app/MarkEdit-api#v0.20.0":
5063-
version: 0.20.0
5064-
resolution: "markedit-api@https://github.com/MarkEdit-app/MarkEdit-api.git#commit=9cba195b6b08b5373ba62eaa4a1f911da1ab14e2"
5062+
"markedit-api@https://github.com/MarkEdit-app/MarkEdit-api#v0.21.0":
5063+
version: 0.21.0
5064+
resolution: "markedit-api@https://github.com/MarkEdit-app/MarkEdit-api.git#commit=080e8fbad8cd6de91b958c74e793c0dc34ef6fb2"
50655065
peerDependencies:
50665066
"@codemirror/autocomplete": ^6.0.0
50675067
"@codemirror/commands": ^6.0.0
@@ -5074,7 +5074,7 @@ __metadata:
50745074
"@lezer/highlight": ^1.0.0
50755075
"@lezer/lr": ^1.0.0
50765076
"@lezer/markdown": ^1.0.0
5077-
checksum: 10c0/95e7cced2c51c39b16b00595bd8ace6fc87f9545316467c8a8af0986d892cc29635f40c0f4ad81284c5af8f6445707996e3d49b17092b655fc95faa9bf6f46b2
5077+
checksum: 10c0/9e504c5d0d5b0f3ab35771582a0f6a9da756cfd6dc599cf4d67991f3b9764532daf1bd09f9f01c04a2393a0b953d0e23e1bbc364d8be40ea5350422c09daa0bc
50785078
languageName: node
50795079
linkType: hard
50805080

@@ -5108,7 +5108,7 @@ __metadata:
51085108
jest: "npm:^30.2.0"
51095109
jest-environment-jsdom: "npm:^30.2.0"
51105110
js-yaml: "npm:^4.1.1"
5111-
markedit-api: "https://github.com/MarkEdit-app/MarkEdit-api#v0.20.0"
5111+
markedit-api: "https://github.com/MarkEdit-app/MarkEdit-api#v0.21.0"
51125112
rollup: "npm:^4.0.0"
51135113
ts-gyb: "npm:^0.12.0"
51145114
ts-jest: "npm:^29.4.5"

MarkEditKit/Sources/Bridge/Native/Generated/NativeModuleAPI.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import MarkEditCore
1212

1313
@MainActor
1414
public protocol NativeModuleAPI: NativeModule {
15+
func openFile(path: String) async -> Bool
1516
func createFile(options: CreateFileOptions) async -> Bool
1617
func deleteFile(path: String) async -> Bool
1718
func listFiles(path: String) async -> [String]?
@@ -36,6 +37,9 @@ public extension NativeModuleAPI {
3637
final class NativeBridgeAPI: NativeBridge {
3738
static let name = "api"
3839
lazy var methods: [String: NativeMethod] = [
40+
"openFile": { [weak self] in
41+
await self?.openFile(parameters: $0)
42+
},
3943
"createFile": { [weak self] in
4044
await self?.createFile(parameters: $0)
4145
},
@@ -87,6 +91,23 @@ final class NativeBridgeAPI: NativeBridge {
8791
self.module = module
8892
}
8993

94+
private func openFile(parameters: Data) async -> Result<Any?, Error>? {
95+
struct Message: Decodable {
96+
var path: String
97+
}
98+
99+
let message: Message
100+
do {
101+
message = try decoder.decode(Message.self, from: parameters)
102+
} catch {
103+
Logger.assertFail("Failed to decode parameters: \(parameters)")
104+
return .failure(error)
105+
}
106+
107+
let result = await module.openFile(path: message.path)
108+
return .success(result)
109+
}
110+
90111
private func createFile(parameters: Data) async -> Result<Any?, Error>? {
91112
struct Message: Decodable {
92113
var options: CreateFileOptions

MarkEditKit/Sources/Bridge/Native/Modules/EditorModuleAPI.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import UniformTypeIdentifiers
1313

1414
@MainActor
1515
public protocol EditorModuleAPIDelegate: AnyObject {
16+
func editorAPIOpenFile(_ sender: EditorModuleAPI, fileURL: URL) -> Bool
1617
func editorAPIGetFileURL(_ sender: EditorModuleAPI, path: String?) -> URL?
1718
func editorAPI(_ sender: EditorModuleAPI, addMainMenuItems items: [(String, WebMenuItem)])
1819
func editorAPI(_ sender: EditorModuleAPI, showContextMenu items: [WebMenuItem], location: WebPoint)
@@ -39,6 +40,10 @@ public final class EditorModuleAPI: NativeModuleAPI {
3940
self.delegate = delegate
4041
}
4142

43+
public func openFile(path: String) async -> Bool {
44+
delegate?.editorAPIOpenFile(self, fileURL: URL(filePath: path)) == true
45+
}
46+
4247
public func createFile(options: CreateFileOptions) async -> Bool {
4348
guard let fileURL = delegate?.editorAPIGetFileURL(self, path: options.path) else {
4449
return false

MarkEditMac/Sources/Editor/Controllers/EditorViewController+Delegate.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ extension EditorViewController: EditorModulePreviewDelegate {
288288
// MARK: - EditorModuleAPIDelegate
289289

290290
extension EditorViewController: EditorModuleAPIDelegate {
291+
func editorAPIOpenFile(_ sender: EditorModuleAPI, fileURL: URL) -> Bool {
292+
NSWorkspace.shared.openOrReveal(url: fileURL)
293+
}
294+
291295
func editorAPIGetFileURL(_ sender: EditorModuleAPI, path: String?) -> URL? {
292296
guard let path else {
293297
return document?.fileURL

MarkEditMac/Sources/Main/Application/AppDelegate+Document.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ extension AppDelegate {
6161
}
6262
}
6363

64+
func openFile(queryDict: [String: String]?) {
65+
if let filePath = queryDict?["path"] {
66+
NSWorkspace.shared.openOrReveal(url: URL(filePath: filePath))
67+
} else {
68+
NSApp.showOpenPanel()
69+
}
70+
}
71+
6472
func createNewFile(queryDict: [String: String]?) {
6573
let fileName = queryDict?["filename"]
6674
let initialContent = queryDict?["initial-content"]

MarkEditMac/Sources/Main/Application/AppDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ extension AppDelegate {
152152
// markedit://new-file?filename=Untitled&initial-content=Hello
153153
createNewFile(queryDict: components?.queryDict)
154154
case "open":
155-
// markedit://open
156-
application.showOpenPanel()
155+
// markedit://open or markedit://open?path=Untitled.md
156+
openFile(queryDict: components?.queryDict)
157157
default:
158158
break
159159
}

0 commit comments

Comments
 (0)