Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename markedit-api related modules #830

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CoreEditor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { WebModuleSelectionImpl } from './src/bridge/web/selection';
import { WebModuleFormatImpl } from './src/bridge/web/format';
import { WebModuleSearchImpl } from './src/bridge/web/search';
import { WebModuleTableOfContentsImpl } from './src/bridge/web/toc';
import { WebModuleUIImpl } from './src/bridge/web/ui';
import { WebModuleAPIImpl } from './src/bridge/web/api';
import { WebModuleWritingToolsImpl } from './src/bridge/web/writingTools';

import { pseudoDocument } from './test/utils/mock';
Expand All @@ -20,7 +20,7 @@ import { NativeModuleCore } from './src/bridge/native/core';
import { NativeModuleCompletion } from './src/bridge/native/completion';
import { NativeModulePreview } from './src/bridge/native/preview';
import { NativeModuleTokenizer } from './src/bridge/native/tokenizer';
import { NativeModuleUI } from './src/bridge/native/ui';
import { NativeModuleAPI } from './src/bridge/native/api';

import { resetEditor } from './src/core';
import { initMarkEditModules } from './src/api/modules';
Expand Down Expand Up @@ -66,7 +66,7 @@ window.webModules = {
format: new WebModuleFormatImpl(),
search: new WebModuleSearchImpl(),
toc: new WebModuleTableOfContentsImpl(),
ui: new WebModuleUIImpl(),
api: new WebModuleAPIImpl(),
writingTools: new WebModuleWritingToolsImpl(),
};

Expand All @@ -75,7 +75,7 @@ window.nativeModules = {
completion: createNativeModule<NativeModuleCompletion>('completion'),
preview: createNativeModule<NativeModulePreview>('preview'),
tokenizer: createNativeModule<NativeModuleTokenizer>('tokenizer'),
ui: createNativeModule<NativeModuleUI>('ui'),
api: createNativeModule<NativeModuleAPI>('api'),
};

window.onload = () => {
Expand Down
4 changes: 2 additions & 2 deletions CoreEditor/src/@types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NativeModuleCore } from '../bridge/native/core';
import { NativeModuleCompletion } from '../bridge/native/completion';
import { NativeModulePreview } from '../bridge/native/preview';
import { NativeModuleTokenizer } from '../bridge/native/tokenizer';
import { NativeModuleUI } from '../bridge/native/ui';
import { NativeModuleAPI } from '../bridge/native/api';

import type { MarkEdit } from 'markedit-api';

Expand Down Expand Up @@ -41,7 +41,7 @@ declare global {
completion: NativeModuleCompletion;
preview: NativeModulePreview;
tokenizer: NativeModuleTokenizer;
ui: NativeModuleUI;
api: NativeModuleAPI;
};
}

Expand Down
8 changes: 4 additions & 4 deletions CoreEditor/src/api/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { getRect, scrollToSelection, isPositionVisible } from '../modules/select

export function addMainMenuItem(spec: MenuItem | MenuItem[]): void {
const items = Array.isArray(spec) ? spec : [spec];
window.nativeModules.ui.addMainMenuItems({
window.nativeModules.api.addMainMenuItems({
items: items.map(item => createMenuItem(item, mainActions)),
});
}

export function showContextMenu(items: MenuItem[], location?: WebPoint) {
const caretPos = window.editor.state.selection.main.head;
const invokeNative = () => {
window.nativeModules.ui.showContextMenu({
window.nativeModules.api.showContextMenu({
items: items.map(item => createMenuItem(item, contextActions)),
location: location ?? (() => {
const rect = getRect(caretPos);
Expand All @@ -41,12 +41,12 @@ export function showContextMenu(items: MenuItem[], location?: WebPoint) {

export function showAlert(spec: Alert): Promise<number> {
const alert = typeof spec === 'string' ? { title: spec } : spec;
return window.nativeModules.ui.showAlert(alert);
return window.nativeModules.api.showAlert(alert);
}

export function showTextBox(spec?: TextBox): Promise<string | undefined> {
const textBox = typeof spec === 'string' ? { title: spec } : spec;
return window.nativeModules.ui.showTextBox(textBox ?? {});
return window.nativeModules.api.showTextBox(textBox ?? {});
}

export function handleMainMenuAction(id: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { WebPoint } from '../../@types/WebPoint';

/**
* @shouldExport true
* @invokePath ui
* @bridgeName NativeBridgeUI
* @invokePath api
* @bridgeName NativeBridgeAPI
*/
export interface NativeModuleUI extends NativeModule {
export interface NativeModuleAPI extends NativeModule {
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { handleMainMenuAction, handleContextMenuAction } from '../../api/ui';

/**
* @shouldExport true
* @invokePath ui
* @overrideModuleName WebBridgeUI
* @invokePath api
* @overrideModuleName WebBridgeAPI
*/
export interface WebModuleUI extends WebModule {
export interface WebModuleAPI extends WebModule {
handleMainMenuAction({ id }: { id: string }): void;
handleContextMenuAction({ id }: { id: string }): void;
}

export class WebModuleUIImpl implements WebModuleUI {
export class WebModuleAPIImpl implements WebModuleAPI {
handleMainMenuAction({ id }: { id: string }): void {
handleMainMenuAction(id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// NativeModuleUI.swift
// NativeModuleAPI.swift
//
// Generated using https://github.com/microsoft/ts-gyb
//
Expand All @@ -11,20 +11,20 @@ import Foundation
import MarkEditCore

@MainActor
public protocol NativeModuleUI: NativeModule {
public protocol NativeModuleAPI: NativeModule {
func addMainMenuItems(items: [WebMenuItem])
func showContextMenu(items: [WebMenuItem], location: WebPoint)
func showAlert(title: String?, message: String?, buttons: [String]?) -> Int
func showTextBox(title: String?, placeholder: String?, defaultValue: String?) -> String?
}

public extension NativeModuleUI {
var bridge: NativeBridge { NativeBridgeUI(self) }
public extension NativeModuleAPI {
var bridge: NativeBridge { NativeBridgeAPI(self) }
}

@MainActor
final class NativeBridgeUI: NativeBridge {
static let name = "ui"
final class NativeBridgeAPI: NativeBridge {
static let name = "api"
lazy var methods: [String: NativeMethod] = [
"addMainMenuItems": { [weak self] in
self?.addMainMenuItems(parameters: $0)
Expand All @@ -40,10 +40,10 @@ final class NativeBridgeUI: NativeBridge {
},
]

private let module: NativeModuleUI
private let module: NativeModuleAPI
private lazy var decoder = JSONDecoder()

init(_ module: NativeModuleUI) {
init(_ module: NativeModuleAPI) {
self.module = module
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// EditorModuleUI.swift
// EditorModuleAPI.swift
//
// Created by cyan on 10/4/24.
//
Expand All @@ -8,48 +8,48 @@ import Foundation
import CryptoKit

@MainActor
public protocol EditorModuleUIDelegate: AnyObject {
func editorUI(_ sender: EditorModuleUI, addMainMenuItems items: [(String, WebMenuItem)])
func editorUI(_ sender: EditorModuleUI, showContextMenu items: [WebMenuItem], location: WebPoint)
func editorUI(
_ sender: EditorModuleUI,
public protocol EditorModuleAPIDelegate: AnyObject {
func editorAPI(_ sender: EditorModuleAPI, addMainMenuItems items: [(String, WebMenuItem)])
func editorAPI(_ sender: EditorModuleAPI, showContextMenu items: [WebMenuItem], location: WebPoint)
func editorAPI(
_ sender: EditorModuleAPI,
alertWith title: String?,
message: String?,
buttons: [String]?
) -> Int
func editorUI(
_ sender: EditorModuleUI,
func editorAPI(
_ sender: EditorModuleAPI,
showTextBox title: String?,
placeholder: String?,
defaultValue: String?
) -> String?
}

public final class EditorModuleUI: NativeModuleUI {
private weak var delegate: EditorModuleUIDelegate?
public final class EditorModuleAPI: NativeModuleAPI {
private weak var delegate: EditorModuleAPIDelegate?

public init(delegate: EditorModuleUIDelegate) {
public init(delegate: EditorModuleAPIDelegate) {
self.delegate = delegate
}

public func addMainMenuItems(items: [WebMenuItem]) {
delegate?.editorUI(self, addMainMenuItems: items.map { item in
delegate?.editorAPI(self, addMainMenuItems: items.map { item in
let hash = SHA256.hash(data: Data(item.uniqueID.utf8))
let id = hash.map { String(format: "%02x", $0) }.joined()
return (id, item)
})
}

public func showContextMenu(items: [WebMenuItem], location: WebPoint) {
delegate?.editorUI(self, showContextMenu: items, location: location)
delegate?.editorAPI(self, showContextMenu: items, location: location)
}

public func showAlert(title: String?, message: String?, buttons: [String]?) -> Int {
delegate?.editorUI(self, alertWith: title, message: message, buttons: buttons) ?? 0
delegate?.editorAPI(self, alertWith: title, message: message, buttons: buttons) ?? 0
}

public func showTextBox(title: String?, placeholder: String?, defaultValue: String?) -> String? {
delegate?.editorUI(self, showTextBox: title, placeholder: placeholder, defaultValue: defaultValue)
delegate?.editorAPI(self, showTextBox: title, placeholder: placeholder, defaultValue: defaultValue)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// WebBridgeUI.swift
// WebBridgeAPI.swift
//
// Generated using https://github.com/microsoft/ts-gyb
//
Expand All @@ -11,7 +11,7 @@ import WebKit
import MarkEditCore

@MainActor
public final class WebBridgeUI {
public final class WebBridgeAPI {
private weak var webView: WKWebView?

init(webView: WKWebView) {
Expand All @@ -27,7 +27,7 @@ public final class WebBridgeUI {
id: id
)

webView?.invoke(path: "webModules.ui.handleMainMenuAction", message: message, completion: completion)
webView?.invoke(path: "webModules.api.handleMainMenuAction", message: message, completion: completion)
}

public func handleContextMenuAction(id: String, completion: ((Result<Void, WKWebView.InvokeError>) -> Void)? = nil) {
Expand All @@ -39,6 +39,6 @@ public final class WebBridgeUI {
id: id
)

webView?.invoke(path: "webModules.ui.handleContextMenuAction", message: message, completion: completion)
webView?.invoke(path: "webModules.api.handleContextMenuAction", message: message, completion: completion)
}
}
4 changes: 2 additions & 2 deletions MarkEditKit/Sources/Bridge/Web/WebModuleBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct WebModuleBridge {
public let format: WebBridgeFormat
public let search: WebBridgeSearch
public let toc: WebBridgeTableOfContents
public let ui: WebBridgeUI
public let api: WebBridgeAPI
public let writingTools: WebBridgeWritingTools

public init(webView: WKWebView) {
Expand All @@ -35,7 +35,7 @@ public struct WebModuleBridge {
self.format = WebBridgeFormat(webView: webView)
self.search = WebBridgeSearch(webView: webView)
self.toc = WebBridgeTableOfContents(webView: webView)
self.ui = WebBridgeUI(webView: webView)
self.api = WebBridgeAPI(webView: webView)
self.writingTools = WebBridgeWritingTools(webView: webView)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,19 @@ extension EditorViewController: EditorModulePreviewDelegate {
}
}

// MARK: - EditorModuleUIDelegate
// MARK: - EditorModuleAPIDelegate

extension EditorViewController: EditorModuleUIDelegate {
func editorUI(_ sender: EditorModuleUI, addMainMenuItems items: [(String, WebMenuItem)]) {
extension EditorViewController: EditorModuleAPIDelegate {
func editorAPI(_ sender: EditorModuleAPI, addMainMenuItems items: [(String, WebMenuItem)]) {
addMainMenuItems(items: items)
}

func editorUI(_ sender: EditorModuleUI, showContextMenu items: [WebMenuItem], location: WebPoint) {
func editorAPI(_ sender: EditorModuleAPI, showContextMenu items: [WebMenuItem], location: WebPoint) {
showContextMenu(items: items, location: location.cgPoint)
}

func editorUI(
_ sender: EditorModuleUI,
func editorAPI(
_ sender: EditorModuleAPI,
alertWith title: String?,
message: String?,
buttons: [String]?
Expand All @@ -269,7 +269,7 @@ extension EditorViewController: EditorModuleUIDelegate {
return response.rawValue - NSApplication.ModalResponse.alertFirstButtonReturn.rawValue
}

func editorUI(_ sender: EditorModuleUI, showTextBox title: String?, placeholder: String?, defaultValue: String?) -> String? {
func editorAPI(_ sender: EditorModuleAPI, showTextBox title: String?, placeholder: String?, defaultValue: String?) -> String? {
showTextBox(title: title, placeholder: placeholder, defaultValue: defaultValue)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ extension EditorViewController {
}

for spec in userDefinedMenuItems {
let item = createMenuItem(spec: spec.item, handler: bridge.ui.handleMainMenuAction)
let item = createMenuItem(spec: spec.item, handler: bridge.api.handleMainMenuAction)
item.identifier = NSUserInterfaceItemIdentifier("\(EditorMenuItem.uniquePrefix).\(spec.id)")

// Preferably, make it the last one before the special divider
Expand All @@ -280,7 +280,7 @@ extension EditorViewController {
}

func showContextMenu(items: [WebMenuItem], location: CGPoint) {
let menu = createMenu(items: items, handler: bridge.ui.handleContextMenuAction)
let menu = createMenu(items: items, handler: bridge.api.handleContextMenuAction)
menu.identifier = EditorWebView.userDefinedContextMenuID

NSCursor.arrow.push()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ final class EditorViewController: NSViewController {
EditorModuleCompletion(delegate: self),
EditorModulePreview(delegate: self),
EditorModuleTokenizer(),
EditorModuleUI(delegate: self),
EditorModuleAPI(delegate: self),
])

let handler = EditorMessageHandler(modules: modules)
Expand Down