Skip to content

Commit

Permalink
Integrate markedit-api v0.8.0 for pasteboard APIs (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong authored Jan 23, 2025
1 parent 3c1a99b commit 37f0c0f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CoreEditor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"eslint-plugin-promise": "^6.6.0",
"jest": "^29.6.4",
"jest-environment-jsdom": "^29.6.4",
"markedit-api": "https://github.com/MarkEdit-app/MarkEdit-api#v0.7.0",
"markedit-api": "https://github.com/MarkEdit-app/MarkEdit-api#v0.8.0",
"rollup": "^4.0.0",
"ts-gyb": "^0.12.0",
"ts-jest": "^29.1.1",
Expand Down
11 changes: 10 additions & 1 deletion CoreEditor/src/api/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EditorView } from '@codemirror/view';
import { Extension } from '@codemirror/state';
import { LanguageDescription } from '@codemirror/language';
import { MarkdownConfig } from '@lezer/markdown';
import { FileInfo } from 'markedit-api';
import { FileInfo, PasteboardItem } from 'markedit-api';
import { markdownConfigurations } from '../extensions';

export function onEditorReady(listener: (editorView: EditorView) => void) {
Expand Down Expand Up @@ -36,6 +36,15 @@ export async function getFileInfo(): Promise<FileInfo | undefined> {
});
}

export async function getPasteboardItems(): Promise<PasteboardItem[]> {
const items = await window.nativeModules.api.getPasteboardItems();
return items === undefined ? [] : JSON.parse(items);
}

export function getPasteboardString(): Promise<string | undefined> {
return window.nativeModules.api.getPasteboardString();
}

export function addExtension(extension: Extension) {
storage.extensions.push(extension);

Expand Down
4 changes: 4 additions & 0 deletions CoreEditor/src/api/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { TextEditor } from './editor';
import {
onEditorReady,
getFileInfo,
getPasteboardItems,
getPasteboardString,
addExtension,
addMarkdownConfig,
addCodeLanguage,
Expand Down Expand Up @@ -49,6 +51,8 @@ export function initMarkEditModules() {

MarkEdit.onEditorReady = onEditorReady;
MarkEdit.getFileInfo = getFileInfo;
MarkEdit.getPasteboardItems = getPasteboardItems;
MarkEdit.getPasteboardString = getPasteboardString;
MarkEdit.addExtension = addExtension;
MarkEdit.addMarkdownConfig = addMarkdownConfig;
MarkEdit.addCodeLanguage = addCodeLanguage;
Expand Down
2 changes: 2 additions & 0 deletions CoreEditor/src/bridge/native/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { WebPoint } from '../../@types/WebPoint';
*/
export interface NativeModuleAPI extends NativeModule {
getFileInfo(): Promise<string | undefined>;
getPasteboardItems(): Promise<string | undefined>;
getPasteboardString(): 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
10 changes: 5 additions & 5 deletions CoreEditor/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4461,9 +4461,9 @@ __metadata:
languageName: node
linkType: hard

"markedit-api@https://github.com/MarkEdit-app/MarkEdit-api#v0.7.0":
version: 0.7.0
resolution: "markedit-api@https://github.com/MarkEdit-app/MarkEdit-api.git#commit=a1c40532d25a8a74a086db478fe86e6361b64568"
"markedit-api@https://github.com/MarkEdit-app/MarkEdit-api#v0.8.0":
version: 0.8.0
resolution: "markedit-api@https://github.com/MarkEdit-app/MarkEdit-api.git#commit=97769b3bd88e185a1de853b9f15bb81802fcaf45"
peerDependencies:
"@codemirror/commands": ^6.0.0
"@codemirror/language": ^6.0.0
Expand All @@ -4474,7 +4474,7 @@ __metadata:
"@lezer/highlight": ^1.0.0
"@lezer/lr": ^1.0.0
"@lezer/markdown": ^1.0.0
checksum: 10c0/4a795bfb1d7f7ced30d888d69ffc202cd2a739bfe430bfa8024673566c2d428e4d5c1fc0bc77f12cb7e16788f9f9456e3aae021721ca9b826e1bb14652064427
checksum: 10c0/cd88a5f28df29926fb831b60918cf065a1fe14e8a2deb953e604333c784dbcbba7e5128334608aeec8499f55ff149463ed8b705e1ba273dbc956bca6c2b80181
languageName: node
linkType: hard

Expand Down Expand Up @@ -4504,7 +4504,7 @@ __metadata:
jest: "npm:^29.6.4"
jest-environment-jsdom: "npm:^29.6.4"
js-yaml: "npm:^4.1.0"
markedit-api: "https://github.com/MarkEdit-app/MarkEdit-api#v0.7.0"
markedit-api: "https://github.com/MarkEdit-app/MarkEdit-api#v0.8.0"
rollup: "npm:^4.0.0"
ts-gyb: "npm:^0.12.0"
ts-jest: "npm:^29.1.1"
Expand Down
18 changes: 18 additions & 0 deletions MarkEditKit/Sources/Bridge/Native/Generated/NativeModuleAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import MarkEditCore
@MainActor
public protocol NativeModuleAPI: NativeModule {
func getFileInfo() -> String?
func getPasteboardItems() -> String?
func getPasteboardString() -> String?
func addMainMenuItems(items: [WebMenuItem])
func showContextMenu(items: [WebMenuItem], location: WebPoint)
func showAlert(title: String?, message: String?, buttons: [String]?) -> Int
Expand All @@ -30,6 +32,12 @@ final class NativeBridgeAPI: NativeBridge {
"getFileInfo": { [weak self] in
self?.getFileInfo(parameters: $0)
},
"getPasteboardItems": { [weak self] in
self?.getPasteboardItems(parameters: $0)
},
"getPasteboardString": { [weak self] in
self?.getPasteboardString(parameters: $0)
},
"addMainMenuItems": { [weak self] in
self?.addMainMenuItems(parameters: $0)
},
Expand All @@ -56,6 +64,16 @@ final class NativeBridgeAPI: NativeBridge {
return .success(result)
}

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

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

private func addMainMenuItems(parameters: Data) -> Result<Any?, Error>? {
struct Message: Decodable {
var items: [WebMenuItem]
Expand Down
39 changes: 39 additions & 0 deletions MarkEditKit/Sources/Bridge/Native/Modules/EditorModuleAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import Foundation
import CryptoKit

#if os(macOS)
import AppKit
#endif

@MainActor
public protocol EditorModuleAPIDelegate: AnyObject {
func editorAPIGetFileURL(_ sender: EditorModuleAPI) -> URL?
Expand Down Expand Up @@ -51,6 +55,41 @@ public final class EditorModuleAPI: NativeModuleAPI {
return try? JSONSerialization.data(withJSONObject: json).toString()
}

public func getPasteboardItems() -> String? {
#if os(macOS)
let pasteboard = NSPasteboard.general
let types = pasteboard.types ?? []

let json: [[String: String]] = types.compactMap { type in
guard let data = pasteboard.data(forType: type) else {
return nil
}

var dict = [
"type": type.rawValue,
"data": data.base64EncodedString(),
]

dict["string"] = data.toString()
return dict
}

return try? JSONSerialization.data(withJSONObject: json).toString()
#else
Logger.assertFail("Missing implementation, consider using web api directly")
return []
#endif
}

public func getPasteboardString() -> String? {
#if os(macOS)
return NSPasteboard.general.string(forType: .string)
#else
Logger.assertFail("Missing implementation, consider using web api directly")
return nil
#endif
}

public func addMainMenuItems(items: [WebMenuItem]) {
delegate?.editorAPI(self, addMainMenuItems: items.map { item in
let hash = SHA256.hash(data: Data(item.uniqueID.utf8))
Expand Down

0 comments on commit 37f0c0f

Please sign in to comment.