文件
mynode/packages/editor/ProseMirror/plugins/joplinEditorApiPlugin.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

68 行
1.9 KiB
TypeScript

import { EditorState, Plugin, Transaction } from 'prosemirror-state';
import { OnEventCallback, OnLocalize } from '../../types';
import { OnCreateCodeEditor, RendererControl } from '../types';
import { focus } from '@joplin/lib/utils/focusHandler';
import createTextArea from '../utils/dom/createTextArea';
export interface EditorApi {
renderer: RendererControl;
onEvent: OnEventCallback;
createCodeEditor: OnCreateCodeEditor;
localize: OnLocalize;
}
export const getEditorApi = (state: EditorState) => {
return joplinEditorApiPlugin.getState(state);
};
export const setEditorApi = (transaction: Transaction, api: EditorApi) => {
return transaction.setMeta(joplinEditorApiPlugin, api);
};
// Stores the editor event handler callback in the editor state.
const joplinEditorApiPlugin = new Plugin<EditorApi>({
state: {
init: () => ({
onEvent: ()=>{},
renderer: {
renderHtmlToMarkup: () => {
throw new Error('Not initialized');
},
renderMarkupToHtml: () => {
throw new Error('Not initialized');
},
},
localize: input => input,
// A default implementation for testing environments
createCodeEditor: (parent, _language, onChange) => {
const editor = createTextArea({ label: 'Editor', initialContent: '', onChange });
parent.appendChild(editor.textArea);
return {
focus: () => focus('joplinEditorApiPlugin', editor.textArea),
remove: () => {
editor.textArea.remove();
},
updateBody: (newValue) => {
editor.textArea.value = newValue;
},
select: (anchor, head) => {
editor.textArea.setSelectionRange(Math.min(anchor, head), Math.max(anchor, head));
},
};
},
}),
apply: (tr, value) => {
const proposedValue = tr.getMeta(joplinEditorApiPlugin);
if (proposedValue) {
return proposedValue;
}
return value;
},
},
});
export default joplinEditorApiPlugin;