Skip to content

Commit

Permalink
Add bindings for CM6 editor
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrosinger committed Dec 11, 2021
1 parent ea2855e commit 9d99988
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 15 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"author": "Tony Grosinger",
"license": "GPL-3.0",
"devDependencies": {
"@codemirror/state": "0.19.0",
"@codemirror/view": "0.19.29",
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-typescript": "^6.0.0",
Expand Down
30 changes: 23 additions & 7 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typescript from '@rollup/plugin-typescript';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default {
Expand All @@ -8,12 +8,28 @@ export default {
dir: '.',
sourcemap: 'inline',
format: 'cjs',
exports: 'default'
exports: 'default',
},
external: ['obsidian'],
plugins: [
typescript(),
nodeResolve({browser: true}),
commonjs(),
]
};
nodeResolve({ browser: true }),
commonjs()
],
external: [
'obsidian',
'@codemirror/autocomplete',
'@codemirror/closebrackets',
'@codemirror/commands',
'@codemirror/fold',
'@codemirror/gutter',
'@codemirror/history',
'@codemirror/language',
'@codemirror/rangeset',
'@codemirror/rectangular-selection',
'@codemirror/search',
'@codemirror/state',
'@codemirror/stream-parser',
'@codemirror/text',
'@codemirror/view',
],
};
44 changes: 44 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
TableControlsViewType,
} from './table-controls-view';
import { TableEditor } from './table-editor';
import { Extension, Prec } from '@codemirror/state';
import { keymap } from '@codemirror/view';
import { FormatType } from '@tgrosinger/md-advanced-tables';
import {
App,
Expand Down Expand Up @@ -49,6 +51,9 @@ export default class TableEditorPlugin extends Plugin {
cm.on('keydown', this.handleKeyDown);
});

// CM6 editor extension for remapping keys
this.registerEditorExtension(this.makeEditorExtension());

this.addCommand({
id: 'next-row',
name: 'Go to next row',
Expand Down Expand Up @@ -276,6 +281,44 @@ export default class TableEditorPlugin extends Plugin {
});
}

// makeEditorExtension is used to bind Tab and Enter in the new CM6 Live Preview editor.
private readonly makeEditorExtension = (): Extension =>
Prec.high(
keymap.of([
{
key: 'Tab',
run: (): boolean =>
this.newPerformTableActionCM6((te: TableEditor) => te.nextCell())(),
shift: (): boolean =>
this.newPerformTableActionCM6((te: TableEditor) =>
te.previousCell(),
)(),
preventDefault: true,
},
{
key: 'Enter',
run: (): boolean =>
this.newPerformTableActionCM6((te: TableEditor) => te.nextRow())(),
preventDefault: true,
},
]),
);

private readonly newPerformTableActionCM6 =
(fn: (te: TableEditor) => void): (() => boolean) =>
(): boolean => {
const leaf = this.app.workspace.activeLeaf;
if (leaf.view instanceof MarkdownView) {
const te = new TableEditor(this.app, leaf.view.editor, this.settings);

if (te.cursorIsInTable()) {
fn(te);
return true;
}
}
return false;
};

private readonly newPerformTableAction =
(fn: (te: TableEditor) => void, alertOnNoTable = true) =>
(checking: boolean, editor: Editor, view: MarkdownView): boolean | void => {
Expand All @@ -288,6 +331,7 @@ export default class TableEditorPlugin extends Plugin {
fn(te);
};

// handleKeyDown is used to bind the tab and enter keys in the legacy CM5 editor.
private readonly handleKeyDown = (
cm: CodeMirror.Editor,
event: KeyboardEvent,
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
dependencies:
"@codemirror/state" "^0.19.0"

"@codemirror/state@0.19.0":
version "0.19.0"
resolved "https://registry.yarnpkg.com/@codemirror/state/-/state-0.19.0.tgz#db3b111fbbab1bd4d7bb74b4c85f131b7004925c"
integrity sha512-yS4oUzaYggUCsDA5cVqMGFNe2CjA2x1YQp68A8/Nnczxr5PctIPuzSTUfBQCi/IKNRouJxMr+gDPweEErJi7fw==
dependencies:
"@codemirror/text" "^0.19.0"

"@codemirror/state@^0.19.0", "@codemirror/state@^0.19.3":
version "0.19.6"
resolved "https://registry.yarnpkg.com/@codemirror/state/-/state-0.19.6.tgz#d631f041d39ce41b7891b099fca26cb1fdb9763e"
Expand All @@ -42,7 +49,7 @@
resolved "https://registry.yarnpkg.com/@codemirror/text/-/text-0.19.5.tgz#75033af2476214e79eae22b81ada618815441c18"
integrity sha512-Syu5Xc7tZzeUAM/y4fETkT0zgGr48rDG+w4U38bPwSIUr+L9S/7w2wDE1WGNzjaZPz12F6gb1gxWiSTg9ocLow==

"@codemirror/view@^0.19.0":
"@codemirror/view@0.19.29", "@codemirror/view@^0.19.0":
version "0.19.29"
resolved "https://registry.yarnpkg.com/@codemirror/view/-/view-0.19.29.tgz#0e2063c107fa0704b212d5f339f18831a6135738"
integrity sha512-rET2Ogs0M8NsH29t5R+bC/zevfHrjs1aaMV23mdbOtrebxwvANic1s4PtNofAU2dFiuK3x2vdTHPVtVg4dokBA==
Expand Down Expand Up @@ -1212,13 +1219,6 @@ object.values@^1.1.1:
function-bind "^1.1.1"
has "^1.0.3"

"obsidian@https://github.com/obsidianmd/obsidian-api/archive/5687a75ecfdb78b6a832127cf6f9229e27e196a0.tar.gz":
version "0.12.17"
resolved "https://github.com/obsidianmd/obsidian-api/archive/5687a75ecfdb78b6a832127cf6f9229e27e196a0.tar.gz#6fb726b645507fbe9ee25edd765e45c42cf8836a"
dependencies:
"@types/codemirror" "0.0.108"
moment "2.29.1"

"obsidian@https://github.com/obsidianmd/obsidian-api/archive/fac5e67f5d83829a4e0126905494c8cbca27765b.tar.gz":
version "0.13.8"
resolved "https://github.com/obsidianmd/obsidian-api/archive/fac5e67f5d83829a4e0126905494c8cbca27765b.tar.gz#122949f19dca4c4f18e9ec47af03c39fa1899a47"
Expand Down

0 comments on commit 9d99988

Please sign in to comment.