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: Remove cancelled keys in favour of explicit keymaps #3417

Merged
merged 1 commit into from
Jul 2, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions ElectronClient/gui/NoteEditor/NoteBody/CodeMirror/CodeMirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,6 @@ function CodeMirror(props: NoteBodyEditorProps, ref: any) {

const { resetScroll, editor_scroll, setEditorPercentScroll, setViewerPercentScroll } = useScrollHandler(editorRef, webviewRef, props.onScroll);

const cancelledKeys: {mac: string[], default: string[]} = { mac: [], default: [] };
// Remove Joplin reserved key bindings from the editor
const letters = ['F', 'T', 'P', 'Q', 'L', ',', 'G', 'K'];
for (let i = 0; i < letters.length; i++) {
const l = letters[i];
cancelledKeys.default.push(`Ctrl-${l}`);
cancelledKeys.mac.push(`Cmd-${l}`);
}
cancelledKeys.default.push('Alt-E');
cancelledKeys.mac.push('Alt-E');

const codeMirror_change = useCallback((newBody: string) => {
props_onChangeRef.current({ changeId: null, content: newBody });
}, []);
Expand Down Expand Up @@ -381,7 +370,6 @@ function CodeMirror(props: NoteBodyEditorProps, ref: any) {
readOnly={props.visiblePanes.indexOf('editor') < 0}
autoMatchBraces={Setting.value('editor.autoMatchingBraces')}
keyMap={props.keyboardMode}
cancelledKeys={cancelledKeys}
onChange={codeMirror_change}
onScroll={editor_scroll}
onEditorContextMenu={onEditorContextMenu}
Expand Down
96 changes: 67 additions & 29 deletions ElectronClient/gui/NoteEditor/NoteBody/CodeMirror/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ import 'codemirror/mode/clike/clike';
import 'codemirror/mode/diff/diff';
import 'codemirror/mode/sql/sql';

export interface CancelledKeys {
mac: string[],
default: string[],
}

export interface EditorProps {
value: string,
mode: string,
Expand All @@ -41,7 +36,6 @@ export interface EditorProps {
readOnly: boolean,
autoMatchBraces: boolean,
keyMap: string,
cancelledKeys: CancelledKeys,
onChange: any,
onScroll: any,
onEditorContextMenu: any,
Expand All @@ -59,18 +53,73 @@ function Editor(props: EditorProps, ref: any) {
useCursorUtils(CodeMirror);
useLineSorting(CodeMirror);

useEffect(() => {
if (props.cancelledKeys) {
for (let i = 0; i < props.cancelledKeys.mac.length; i++) {
const k = props.cancelledKeys.mac[i];
CodeMirror.keyMap.macDefault[k] = null;
}
for (let i = 0; i < props.cancelledKeys.default.length; i++) {
const k = props.cancelledKeys.default[i];
CodeMirror.keyMap.default[k] = null;
}
}
}, [props.cancelledKeys]);
CodeMirror.keyMap.basic = {
'Left': 'goCharLeft',
'Right': 'goCharRight',
'Up': 'goLineUp',
'Down': 'goLineDown',
'End': 'goLineEnd',
'Home': 'goLineStartSmart',
'PageUp': 'goPageUp',
'PageDown': 'goPageDown',
'Delete': 'delCharAfter',
'Backspace': 'delCharBefore',
'Shift-Backspace': 'delCharBefore',
'Tab': 'smartListIndent',
'Shift-Tab': 'smartListUnindent',
'Enter': 'insertListElement',
'Insert': 'toggleOverwrite',
'Esc': 'singleSelection',
};
CodeMirror.keyMap.default = {
'Ctrl-A': 'selectAll',
'Ctrl-D': 'deleteLine',
'Ctrl-Z': 'undo',
'Shift-Ctrl-Z': 'redo',
'Ctrl-Y': 'redo',
'Ctrl-Home': 'goDocStart',
'Ctrl-End': 'goDocEnd',
'Ctrl-Up': 'goLineUp',
'Ctrl-Down': 'goLineDown',
'Ctrl-Left': 'goGroupLeft',
'Ctrl-Right': 'goGroupRight',
'Alt-Left': 'goLineStart',
'Alt-Right': 'goLineEnd',
'Ctrl-Backspace': 'delGroupBefore',
'Ctrl-Delete': 'delGroupAfter',
'Ctrl-[': 'indentLess',
'Ctrl-]': 'indentMore',
'Ctrl-/': 'toggleComment',
'Ctrl-Alt-S': 'sortSelectedLines',
'Alt-Up': 'swapLineUp',
'Alt-Down': 'swapLineDown',
'fallthrough': 'basic',
};
CodeMirror.keyMap.pcDefault = CodeMirror.keyMap.default;
CodeMirror.keyMap.macDefault = {
'Cmd-A': 'selectAll',
'Cmd-D': 'deleteLine',
'Cmd-Z': 'undo',
'Shift-Cmd-Z': 'redo',
'Cmd-Y': 'redo',
'Cmd-Home': 'goDocStart',
'Cmd-Up': 'goDocStart',
'Cmd-End': 'goDocEnd',
'Cmd-Down': 'goDocEnd',
'Alt-Left': 'goGroupLeft',
'Alt-Right': 'goGroupRight',
'Cmd-Left': 'goLineLeft',
'Cmd-Right': 'goLineRight',
'Alt-Backspace': 'delGroupBefore',
'Alt-Delete': 'delGroupAfter',
'Cmd-[': 'indentLess',
'Cmd-]': 'indentMore',
'Cmd-/': 'toggleComment',
'Cmd-Opt-S': 'sortSelectedLines',
'Opt-Up': 'swapLineUp',
'Opt-Down': 'swapLineDown',
'fallthrough': 'basic',
};

useImperativeHandle(ref, () => {
return editor;
Expand Down Expand Up @@ -133,17 +182,6 @@ function Editor(props: EditorProps, ref: any) {
spellcheck: true,
allowDropFileTypes: [''], // disable codemirror drop handling
keyMap: props.keyMap ? props.keyMap : 'default',
extraKeys: { 'Enter': 'insertListElement',
'Ctrl-/': 'toggleComment',
'Ctrl-Alt-S': 'sortSelectedLines',
'Alt-Up': 'swapLineUp',
'Alt-Down': 'swapLineDown',
'Cmd-/': 'toggleComment',
'Cmd-Opt-S': 'sortSelectedLines',
'Opt-Up': 'swapLineUp',
'Opt-Down': 'swapLineDown',
'Tab': 'smartListIndent',
'Shift-Tab': 'smartListUnindent' },
};
const cm = CodeMirror(editorParent.current, cmOptions);
setEditor(cm);
Expand Down