From 035557de9fc2f6c251feb991de5114cc7d17f9d4 Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Mon, 15 Apr 2024 10:14:38 -0700 Subject: [PATCH] Desktop: Allow creating plugins that process pasted text in the beta editor (#10310) --- .../gui/NoteEditor/NoteBody/CodeMirror/v6/CodeMirror.tsx | 9 ++++----- packages/editor/CodeMirror/CodeMirrorControl.ts | 6 +++--- packages/editor/types.ts | 7 ++++++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/CodeMirror.tsx b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/CodeMirror.tsx index c820771bd..6bf1702fa 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/CodeMirror.tsx +++ b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/CodeMirror.tsx @@ -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 { + 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 { if (editorRef.current) { const modifiedMd = await Note.replaceResourceExternalToInternalLinks(clipboard.readText(), { useAbsolutePaths: true }); - editorRef.current.insertText(modifiedMd); + editorRef.current.insertText(modifiedMd, UserEventSource.Paste); } }, []); diff --git a/packages/editor/CodeMirror/CodeMirrorControl.ts b/packages/editor/CodeMirror/CodeMirrorControl.ts index 4ff54dfc4..ec177aec7 100644 --- a/packages/editor/CodeMirror/CodeMirrorControl.ts +++ b/packages/editor/CodeMirror/CodeMirrorControl.ts @@ -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) { diff --git a/packages/editor/types.ts b/packages/editor/types.ts index befc7cee7..86b46212a 100644 --- a/packages/editor/types.ts +++ b/packages/editor/types.ts @@ -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; // 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;