Skip to content

Commit

Permalink
refactor: table operation
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Nov 16, 2023
1 parent b72c502 commit 4b8e697
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
1 change: 0 additions & 1 deletion console/src/lib/codemirror-kit/keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import browser from "./browser";

export function priorRunHandlers(bindings: readonly KeyBinding[], view: EditorView, event: KeyboardEvent) {
const keyMap = getKeymap(bindings, view);
console.log(keyMap)
return runHandlers(keyMap, event, view, "editor")
}

Expand Down
34 changes: 28 additions & 6 deletions console/src/plugins/better-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { insertNewlineAndIndent } from "@codemirror/commands";
import { syntaxTree } from "@codemirror/language";
import { TableRangeDetector } from "@/lib/mte-kernel/table-helper";
import { renderMarkdownFregment } from "@/lib/remark";
import { priorRunHandlers, buildKeymap } from "@/lib/codemirror-kit/keymap";
import { priorRunHandlers } from "@/lib/codemirror-kit/keymap";

// options for the table editor
const opts = options({
Expand All @@ -23,12 +23,26 @@ const keyMapOf = (
tableEditor: TableEditor
): ReadonlyArray<KeyBinding> => {
const tableKeyMap: ReadonlyArray<KeyBinding> = [
{ key: "Tab", run: () => tableEditor.nextCell(opts) },
{ key: "Shift-Tab", run: () => tableEditor.previousCell(opts) },
{
key: "Tab",
run: () => {
tableEditor.nextCell(opts);
return true;
},
},
{
key: "Shift-Tab",
run: () => {
tableEditor.previousCell(opts);
return true;
},
},
{
key: "Enter",
run: () => {
if (tableEditor._textEditor.getCursorPosition().column !== 0) {
const {row, column} = tableEditor._textEditor.getCursorPosition();
const text = tableEditor._textEditor.getLine(row);
if (column !== 0 && column < text.length) {
tableEditor.nextRow(opts);
return true;
}
Expand Down Expand Up @@ -105,8 +119,16 @@ const viewPlugin = ViewPlugin.define(() => ({}), {
tableEditor.resetSmartCursor();
return false;
}
event.preventDefault();
return priorRunHandlers(keyMapOf(view, tableEditor), view, event);

const handled = priorRunHandlers(
keyMapOf(view, tableEditor),
view,
event
);
if (handled == undefined || handled) {
event.preventDefault();
}
return handled;
},
},
});
Expand Down

0 comments on commit 4b8e697

Please sign in to comment.