|
| 1 | +/* global CodeMirror, $ */ |
| 2 | +import { TableEditor, Point, options, Alignment, FormatType } from '@susisu/mte-kernel' |
| 3 | + |
| 4 | +// port of the code from: https://github.com/susisu/mte-demo/blob/master/src/main.js |
| 5 | + |
| 6 | +// text editor interface |
| 7 | +// see https://doc.esdoc.org/github.com/susisu/mte-kernel/class/lib/text-editor.js~ITextEditor.html |
| 8 | +class TextEditorInterface { |
| 9 | + constructor (editor) { |
| 10 | + this.editor = editor |
| 11 | + this.doc = editor.getDoc() |
| 12 | + this.transaction = false |
| 13 | + this.onDidFinishTransaction = null |
| 14 | + } |
| 15 | + |
| 16 | + getCursorPosition () { |
| 17 | + const { line, ch } = this.doc.getCursor() |
| 18 | + return new Point(line, ch) |
| 19 | + } |
| 20 | + |
| 21 | + setCursorPosition (pos) { |
| 22 | + this.doc.setCursor({ line: pos.row, ch: pos.column }) |
| 23 | + } |
| 24 | + |
| 25 | + setSelectionRange (range) { |
| 26 | + this.doc.setSelection( |
| 27 | + { line: range.start.row, ch: range.start.column }, |
| 28 | + { line: range.end.row, ch: range.end.column } |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + getLastRow () { |
| 33 | + return this.doc.lineCount() - 1 |
| 34 | + } |
| 35 | + |
| 36 | + acceptsTableEdit () { |
| 37 | + return true |
| 38 | + } |
| 39 | + |
| 40 | + getLine (row) { |
| 41 | + return this.doc.getLine(row) |
| 42 | + } |
| 43 | + |
| 44 | + insertLine (row, line) { |
| 45 | + const lastRow = this.getLastRow() |
| 46 | + if (row > lastRow) { |
| 47 | + const lastLine = this.getLine(lastRow) |
| 48 | + this.doc.replaceRange( |
| 49 | + '\n' + line, |
| 50 | + { line: lastRow, ch: lastLine.length }, |
| 51 | + { line: lastRow, ch: lastLine.length } |
| 52 | + ) |
| 53 | + } else { |
| 54 | + this.doc.replaceRange( |
| 55 | + line + '\n', |
| 56 | + { line: row, ch: 0 }, |
| 57 | + { line: row, ch: 0 } |
| 58 | + ) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + deleteLine (row) { |
| 63 | + const lastRow = this.getLastRow() |
| 64 | + if (row >= lastRow) { |
| 65 | + if (lastRow > 0) { |
| 66 | + const preLastLine = this.getLine(lastRow - 1) |
| 67 | + const lastLine = this.getLine(lastRow) |
| 68 | + this.doc.replaceRange( |
| 69 | + '', |
| 70 | + { line: lastRow - 1, ch: preLastLine.length }, |
| 71 | + { line: lastRow, ch: lastLine.length } |
| 72 | + ) |
| 73 | + } else { |
| 74 | + const lastLine = this.getLine(lastRow) |
| 75 | + this.doc.replaceRange( |
| 76 | + '', |
| 77 | + { line: lastRow, ch: 0 }, |
| 78 | + { line: lastRow, ch: lastLine.length } |
| 79 | + ) |
| 80 | + } |
| 81 | + } else { |
| 82 | + this.doc.replaceRange( |
| 83 | + '', |
| 84 | + { line: row, ch: 0 }, |
| 85 | + { line: row + 1, ch: 0 } |
| 86 | + ) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + replaceLines (startRow, endRow, lines) { |
| 91 | + const lastRow = this.getLastRow() |
| 92 | + if (endRow > lastRow) { |
| 93 | + const lastLine = this.getLine(lastRow) |
| 94 | + this.doc.replaceRange( |
| 95 | + lines.join('\n'), |
| 96 | + { line: startRow, ch: 0 }, |
| 97 | + { line: lastRow, ch: lastLine.length } |
| 98 | + ) |
| 99 | + } else { |
| 100 | + this.doc.replaceRange( |
| 101 | + lines.join('\n') + '\n', |
| 102 | + { line: startRow, ch: 0 }, |
| 103 | + { line: endRow, ch: 0 } |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + transact (func) { |
| 109 | + this.transaction = true |
| 110 | + func() |
| 111 | + this.transaction = false |
| 112 | + if (this.onDidFinishTransaction) { |
| 113 | + this.onDidFinishTransaction.call(undefined) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +export function initTableEditor (editor) { |
| 119 | + // create an interface to the text editor |
| 120 | + const editorIntf = new TextEditorInterface(editor) |
| 121 | + // create a table editor object |
| 122 | + const tableEditor = new TableEditor(editorIntf) |
| 123 | + // options for the table editor |
| 124 | + const opts = options({ |
| 125 | + smartCursor: true, |
| 126 | + formatType: FormatType.NORMAL |
| 127 | + }) |
| 128 | + // keymap of the commands |
| 129 | + // from https://github.com/susisu/mte-demo/blob/master/src/main.js |
| 130 | + const keyMap = CodeMirror.normalizeKeyMap({ |
| 131 | + Tab: () => { tableEditor.nextCell(opts) }, |
| 132 | + 'Shift-Tab': () => { tableEditor.previousCell(opts) }, |
| 133 | + Enter: () => { tableEditor.nextRow(opts) }, |
| 134 | + 'Ctrl-Enter': () => { tableEditor.escape(opts) }, |
| 135 | + 'Cmd-Enter': () => { tableEditor.escape(opts) }, |
| 136 | + 'Shift-Ctrl-Left': () => { tableEditor.alignColumn(Alignment.LEFT, opts) }, |
| 137 | + 'Shift-Cmd-Left': () => { tableEditor.alignColumn(Alignment.LEFT, opts) }, |
| 138 | + 'Shift-Ctrl-Right': () => { tableEditor.alignColumn(Alignment.RIGHT, opts) }, |
| 139 | + 'Shift-Cmd-Right': () => { tableEditor.alignColumn(Alignment.RIGHT, opts) }, |
| 140 | + 'Shift-Ctrl-Up': () => { tableEditor.alignColumn(Alignment.CENTER, opts) }, |
| 141 | + 'Shift-Cmd-Up': () => { tableEditor.alignColumn(Alignment.CENTER, opts) }, |
| 142 | + 'Shift-Ctrl-Down': () => { tableEditor.alignColumn(Alignment.NONE, opts) }, |
| 143 | + 'Shift-Cmd-Down': () => { tableEditor.alignColumn(Alignment.NONE, opts) }, |
| 144 | + 'Ctrl-Left': () => { tableEditor.moveFocus(0, -1, opts) }, |
| 145 | + 'Cmd-Left': () => { tableEditor.moveFocus(0, -1, opts) }, |
| 146 | + 'Ctrl-Right': () => { tableEditor.moveFocus(0, 1, opts) }, |
| 147 | + 'Cmd-Right': () => { tableEditor.moveFocus(0, 1, opts) }, |
| 148 | + 'Ctrl-Up': () => { tableEditor.moveFocus(-1, 0, opts) }, |
| 149 | + 'Cmd-Up': () => { tableEditor.moveFocus(-1, 0, opts) }, |
| 150 | + 'Ctrl-Down': () => { tableEditor.moveFocus(1, 0, opts) }, |
| 151 | + 'Cmd-Down': () => { tableEditor.moveFocus(1, 0, opts) }, |
| 152 | + 'Ctrl-K Ctrl-I': () => { tableEditor.insertRow(opts) }, |
| 153 | + 'Cmd-K Cmd-I': () => { tableEditor.insertRow(opts) }, |
| 154 | + 'Ctrl-L Ctrl-I': () => { tableEditor.deleteRow(opts) }, |
| 155 | + 'Cmd-L Cmd-I': () => { tableEditor.deleteRow(opts) }, |
| 156 | + 'Ctrl-K Ctrl-J': () => { tableEditor.insertColumn(opts) }, |
| 157 | + 'Cmd-K Cmd-J': () => { tableEditor.insertColumn(opts) }, |
| 158 | + 'Ctrl-L Ctrl-J': () => { tableEditor.deleteColumn(opts) }, |
| 159 | + 'Cmd-L Cmd-J': () => { tableEditor.deleteColumn(opts) }, |
| 160 | + 'Alt-Shift-Ctrl-Left': () => { tableEditor.moveColumn(-1, opts) }, |
| 161 | + 'Alt-Shift-Cmd-Left': () => { tableEditor.moveColumn(-1, opts) }, |
| 162 | + 'Alt-Shift-Ctrl-Right': () => { tableEditor.moveColumn(1, opts) }, |
| 163 | + 'Alt-Shift-Cmd-Right': () => { tableEditor.moveColumn(1, opts) }, |
| 164 | + 'Alt-Shift-Ctrl-Up': () => { tableEditor.moveRow(-1, opts) }, |
| 165 | + 'Alt-Shift-Cmd-Up': () => { tableEditor.moveRow(-1, opts) }, |
| 166 | + 'Alt-Shift-Ctrl-Down': () => { tableEditor.moveRow(1, opts) }, |
| 167 | + 'Alt-Shift-Cmd-Down': () => { tableEditor.moveRow(1, opts) } |
| 168 | + }) |
| 169 | + // enable keymap if the cursor is in a table |
| 170 | + function updateActiveState () { |
| 171 | + const tableTools = $('.toolbar .table-tools') |
| 172 | + const active = tableEditor.cursorIsInTable(opts) |
| 173 | + if (active) { |
| 174 | + tableTools.show() |
| 175 | + tableTools.parent().scrollLeft(tableTools.parent()[0].scrollWidth) |
| 176 | + editor.setOption('extraKeys', keyMap) |
| 177 | + } else { |
| 178 | + tableTools.hide() |
| 179 | + editor.setOption('extraKeys', null) |
| 180 | + tableEditor.resetSmartCursor() |
| 181 | + } |
| 182 | + } |
| 183 | + // event subscriptions |
| 184 | + editor.on('cursorActivity', () => { |
| 185 | + if (!editorIntf.transaction) { |
| 186 | + updateActiveState() |
| 187 | + } |
| 188 | + }) |
| 189 | + editor.on('changes', () => { |
| 190 | + if (!editorIntf.transaction) { |
| 191 | + updateActiveState() |
| 192 | + } |
| 193 | + }) |
| 194 | + editorIntf.onDidFinishTransaction = () => { |
| 195 | + updateActiveState() |
| 196 | + } |
| 197 | + |
| 198 | + return tableEditor |
| 199 | +} |
0 commit comments