2024-03-09 12:49:28 +02:00
|
|
|
import { EditorView, KeyBinding, keymap } from '@codemirror/view';
|
2024-04-15 19:14:38 +02:00
|
|
|
import { EditorCommandType, EditorControl, EditorSettings, LogMessageCallback, ContentScriptData, SearchState, UserEventSource } from '../types';
|
2023-09-21 10:12:40 +02:00
|
|
|
import CodeMirror5Emulation from './CodeMirror5Emulation/CodeMirror5Emulation';
|
2024-02-22 23:30:48 +02:00
|
|
|
import editorCommands from './editorCommands/editorCommands';
|
2024-03-06 11:53:07 +02:00
|
|
|
import { Compartment, EditorSelection, Extension, StateEffect } from '@codemirror/state';
|
2023-09-21 10:12:40 +02:00
|
|
|
import { updateLink } from './markdown/markdownCommands';
|
|
|
|
import { SearchQuery, setSearchQuery } from '@codemirror/search';
|
2023-12-13 21:45:02 +02:00
|
|
|
import PluginLoader from './pluginApi/PluginLoader';
|
2024-02-22 17:36:27 +02:00
|
|
|
import customEditorCompletion, { editorCompletionSource, enableLanguageDataAutocomplete } from './pluginApi/customEditorCompletion';
|
|
|
|
import { CompletionSource } from '@codemirror/autocomplete';
|
2024-06-11 08:41:41 +02:00
|
|
|
import { RegionSpec, toggleInlineSelectionFormat } from './util/editorStateUtils';
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
interface Callbacks {
|
|
|
|
onUndoRedo(): void;
|
|
|
|
onSettingsChange(newSettings: EditorSettings): void;
|
|
|
|
onClearHistory(): void;
|
|
|
|
onRemove(): void;
|
|
|
|
onLogMessage: LogMessageCallback;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-02-22 23:30:48 +02:00
|
|
|
type EditorUserCommand = (...args: any[])=> any;
|
|
|
|
|
2023-09-21 10:12:40 +02:00
|
|
|
export default class CodeMirrorControl extends CodeMirror5Emulation implements EditorControl {
|
|
|
|
private _pluginControl: PluginLoader;
|
2024-02-22 23:30:48 +02:00
|
|
|
private _userCommands: Map<string, EditorUserCommand> = new Map();
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
public constructor(
|
|
|
|
editor: EditorView,
|
|
|
|
private _callbacks: Callbacks,
|
|
|
|
) {
|
|
|
|
super(editor, _callbacks.onLogMessage);
|
|
|
|
|
|
|
|
this._pluginControl = new PluginLoader(this, _callbacks.onLogMessage);
|
2024-02-22 17:36:27 +02:00
|
|
|
|
|
|
|
this.addExtension(customEditorCompletion());
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public supportsCommand(name: string) {
|
2024-02-19 12:04:20 +02:00
|
|
|
return name in editorCommands || this._userCommands.has(name) || super.commandExists(name);
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-02-22 23:30:48 +02:00
|
|
|
public override execCommand(name: string, ...args: any[]) {
|
2024-02-19 12:04:20 +02:00
|
|
|
let commandOutput;
|
|
|
|
if (this._userCommands.has(name)) {
|
2024-02-22 23:30:48 +02:00
|
|
|
commandOutput = this._userCommands.get(name)(...args);
|
2024-02-19 12:04:20 +02:00
|
|
|
} else if (name in editorCommands) {
|
2024-03-11 17:02:15 +02:00
|
|
|
commandOutput = editorCommands[name as EditorCommandType](this.editor, ...args);
|
2023-09-21 10:12:40 +02:00
|
|
|
} else if (super.commandExists(name)) {
|
2024-03-11 17:02:15 +02:00
|
|
|
commandOutput = super.execCommand(name, ...args);
|
|
|
|
} else if (super.supportsJoplinCommand(name)) {
|
|
|
|
commandOutput = super.execJoplinCommand(name);
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (name === EditorCommandType.Undo || name === EditorCommandType.Redo) {
|
|
|
|
this._callbacks.onUndoRedo();
|
|
|
|
}
|
2024-02-19 12:04:20 +02:00
|
|
|
|
|
|
|
return commandOutput;
|
|
|
|
}
|
|
|
|
|
2024-02-22 23:30:48 +02:00
|
|
|
public registerCommand(name: string, command: EditorUserCommand) {
|
2024-02-19 12:04:20 +02:00
|
|
|
this._userCommands.set(name, command);
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public undo() {
|
|
|
|
this.execCommand(EditorCommandType.Undo);
|
|
|
|
this._callbacks.onUndoRedo();
|
|
|
|
}
|
|
|
|
|
|
|
|
public redo() {
|
|
|
|
this.execCommand(EditorCommandType.Redo);
|
|
|
|
this._callbacks.onUndoRedo();
|
|
|
|
}
|
|
|
|
|
|
|
|
public select(anchor: number, head: number) {
|
|
|
|
this.editor.dispatch(this.editor.state.update({
|
|
|
|
selection: { anchor, head },
|
|
|
|
scrollIntoView: true,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
public clearHistory() {
|
|
|
|
this._callbacks.onClearHistory();
|
|
|
|
}
|
|
|
|
|
|
|
|
public setScrollPercent(fraction: number) {
|
|
|
|
const maxScroll = this.editor.scrollDOM.scrollHeight - this.editor.scrollDOM.clientHeight;
|
|
|
|
this.editor.scrollDOM.scrollTop = fraction * maxScroll;
|
|
|
|
}
|
|
|
|
|
2024-04-15 19:14:38 +02:00
|
|
|
public insertText(text: string, userEvent?: UserEventSource) {
|
|
|
|
this.editor.dispatch(this.editor.state.replaceSelection(text), { userEvent });
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
2024-06-11 08:41:41 +02:00
|
|
|
public wrapSelections(start: string, end: string) {
|
|
|
|
const regionSpec = RegionSpec.of({ template: { start, end } });
|
|
|
|
|
|
|
|
this.editor.dispatch(
|
|
|
|
this.editor.state.changeByRange(range => {
|
|
|
|
const update = toggleInlineSelectionFormat(this.editor.state, regionSpec, range);
|
|
|
|
if (!update.range.empty) {
|
|
|
|
// Deselect the start and end characters (roughly preserve the original
|
|
|
|
// selection).
|
|
|
|
update.range = EditorSelection.range(
|
|
|
|
update.range.from + start.length,
|
|
|
|
update.range.to - end.length,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return update;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-21 10:12:40 +02:00
|
|
|
public updateBody(newBody: string) {
|
|
|
|
// TODO: doc.toString() can be slow for large documents.
|
|
|
|
const currentBody = this.editor.state.doc.toString();
|
|
|
|
|
|
|
|
if (newBody !== currentBody) {
|
|
|
|
// For now, collapse the selection to a single cursor
|
|
|
|
// to ensure that the selection stays within the document
|
|
|
|
// (and thus avoids an exception).
|
|
|
|
const mainCursorPosition = this.editor.state.selection.main.anchor;
|
2024-06-04 10:52:09 +02:00
|
|
|
|
|
|
|
// The maximum cursor position needs to be calculated using the EditorState,
|
|
|
|
// to correctly account for line endings.
|
|
|
|
const maxCursorPosition = this.editor.state.toText(newBody).length;
|
|
|
|
const newCursorPosition = Math.min(mainCursorPosition, maxCursorPosition);
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
this.editor.dispatch(this.editor.state.update({
|
|
|
|
changes: {
|
|
|
|
from: 0,
|
|
|
|
to: this.editor.state.doc.length,
|
|
|
|
insert: newBody,
|
|
|
|
},
|
|
|
|
selection: EditorSelection.cursor(newCursorPosition),
|
|
|
|
scrollIntoView: true,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateLink(newLabel: string, newUrl: string) {
|
|
|
|
updateLink(newLabel, newUrl)(this.editor);
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateSettings(newSettings: EditorSettings) {
|
|
|
|
this._callbacks.onSettingsChange(newSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public setSearchState(newState: SearchState) {
|
|
|
|
const query = new SearchQuery({
|
|
|
|
search: newState.searchText,
|
|
|
|
caseSensitive: newState.caseSensitive,
|
|
|
|
regexp: newState.useRegex,
|
|
|
|
replace: newState.replaceText,
|
|
|
|
});
|
|
|
|
this.editor.dispatch({
|
|
|
|
effects: setSearchQuery.of(query),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public addStyles(...styles: Parameters<typeof EditorView.theme>) {
|
2024-03-06 11:53:07 +02:00
|
|
|
const compartment = new Compartment();
|
2023-09-21 10:12:40 +02:00
|
|
|
this.editor.dispatch({
|
2024-03-06 11:53:07 +02:00
|
|
|
effects: StateEffect.appendConfig.of(
|
|
|
|
compartment.of(EditorView.theme(...styles)),
|
|
|
|
),
|
2023-09-21 10:12:40 +02:00
|
|
|
});
|
2024-03-06 11:53:07 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
remove: () => {
|
|
|
|
this.editor.dispatch({
|
|
|
|
effects: compartment.reconfigure([]),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
2024-02-19 12:29:37 +02:00
|
|
|
public setContentScripts(plugins: ContentScriptData[]) {
|
2023-09-21 10:12:40 +02:00
|
|
|
return this._pluginControl.setPlugins(plugins);
|
|
|
|
}
|
|
|
|
|
|
|
|
public remove() {
|
|
|
|
this._pluginControl.remove();
|
|
|
|
this._callbacks.onRemove();
|
|
|
|
}
|
2023-12-13 21:45:02 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// CodeMirror-specific methods
|
|
|
|
//
|
|
|
|
|
2024-03-09 12:49:28 +02:00
|
|
|
public prependKeymap(bindings: readonly KeyBinding[]) {
|
|
|
|
const compartment = new Compartment();
|
|
|
|
this.editor.dispatch({
|
|
|
|
effects: StateEffect.appendConfig.of([
|
|
|
|
compartment.of(keymap.of(bindings)),
|
|
|
|
]),
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
remove: () => {
|
|
|
|
this.editor.dispatch({
|
|
|
|
effects: compartment.reconfigure([]),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-02-22 17:36:27 +02:00
|
|
|
public joplinExtensions = {
|
|
|
|
// Some plugins want to enable autocompletion from *just* that plugin, without also
|
|
|
|
// enabling autocompletion for text within code blocks (and other built-in completion
|
|
|
|
// sources).
|
|
|
|
// To support this, we need to provide extensions that wrap the built-in autocomplete.
|
|
|
|
// See https://discuss.codemirror.net/t/autocompletion-merging-override-in-config/7853
|
|
|
|
completionSource: (completionSource: CompletionSource) => editorCompletionSource.of(completionSource),
|
|
|
|
enableLanguageDataAutocomplete: enableLanguageDataAutocomplete,
|
|
|
|
};
|
|
|
|
|
2023-12-13 21:45:02 +02:00
|
|
|
public addExtension(extension: Extension) {
|
|
|
|
this.editor.dispatch({
|
|
|
|
effects: StateEffect.appendConfig.of([extension]),
|
|
|
|
});
|
|
|
|
}
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|