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

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

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}, []);

Expand Down Expand Up @@ -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);
}
}, []);

Expand Down
6 changes: 3 additions & 3 deletions 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';
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion packages/editor/types.ts
Expand Up @@ -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
Expand All @@ -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;
Expand Down