1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Allow creating plugins that process pasted text in the beta editor (#10310)

This commit is contained in:
Henry Heino 2024-04-15 10:14:38 -07:00 committed by GitHub
parent 97ff2b51f1
commit 035557de9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 9 deletions

View File

@ -16,7 +16,7 @@ import { MarkupToHtml } from '@joplin/renderer';
const { clipboard } = require('electron');
import { reg } from '@joplin/lib/registry';
import ErrorBoundary from '../../../../ErrorBoundary';
import { EditorKeymap, EditorLanguageType, EditorSettings } from '@joplin/editor/types';
import { EditorKeymap, EditorLanguageType, EditorSettings, UserEventSource } from '@joplin/editor/types';
import useStyles from '../utils/useStyles';
import { EditorEvent, EditorEventType } from '@joplin/editor/events';
import useScrollHandler from '../utils/useScrollHandler';
@ -78,12 +78,11 @@ const CodeMirror = (props: NoteBodyEditorProps, ref: ForwardedRef<NoteBodyEditor
}
}, [props.content]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
const onEditorPaste = useCallback(async (event: any = null) => {
const onEditorPaste = useCallback(async (event: Event|null = null) => {
const resourceMds = await getResourcesFromPasteEvent(event);
if (!resourceMds.length) return;
if (editorRef.current) {
editorRef.current.insertText(resourceMds.join('\n'));
editorRef.current.insertText(resourceMds.join('\n'), UserEventSource.Paste);
}
}, []);
@ -130,7 +129,7 @@ const CodeMirror = (props: NoteBodyEditorProps, ref: ForwardedRef<NoteBodyEditor
const editorPasteText = useCallback(async () => {
if (editorRef.current) {
const modifiedMd = await Note.replaceResourceExternalToInternalLinks(clipboard.readText(), { useAbsolutePaths: true });
editorRef.current.insertText(modifiedMd);
editorRef.current.insertText(modifiedMd, UserEventSource.Paste);
}
}, []);

View File

@ -1,5 +1,5 @@
import { EditorView, KeyBinding, keymap } from '@codemirror/view';
import { EditorCommandType, EditorControl, EditorSettings, LogMessageCallback, ContentScriptData, SearchState } from '../types';
import { EditorCommandType, EditorControl, EditorSettings, LogMessageCallback, ContentScriptData, SearchState, UserEventSource } from '../types';
import CodeMirror5Emulation from './CodeMirror5Emulation/CodeMirror5Emulation';
import editorCommands from './editorCommands/editorCommands';
import { Compartment, EditorSelection, Extension, StateEffect } from '@codemirror/state';
@ -89,8 +89,8 @@ export default class CodeMirrorControl extends CodeMirror5Emulation implements E
this.editor.scrollDOM.scrollTop = fraction * maxScroll;
}
public insertText(text: string) {
this.editor.dispatch(this.editor.state.replaceSelection(text));
public insertText(text: string, userEvent?: UserEventSource) {
this.editor.dispatch(this.editor.state.replaceSelection(text), { userEvent });
}
public updateBody(newBody: string) {

View File

@ -85,6 +85,11 @@ export interface ContentScriptData {
postMessageHandler: (message: any)=> any;
}
// Intended to correspond with https://codemirror.net/docs/ref/#state.Transaction%5EuserEvent
export enum UserEventSource {
Paste = 'input.paste',
}
export interface EditorControl {
supportsCommand(name: EditorCommandType|string): boolean|Promise<boolean>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
@ -98,7 +103,7 @@ export interface EditorControl {
// 0 corresponds to the top, 1 corresponds to the bottom.
setScrollPercent(fraction: number): void;
insertText(text: string): void;
insertText(text: string, source?: UserEventSource): void;
updateBody(newBody: string): void;
updateSettings(newSettings: EditorSettings): void;