From cc52cf60dca1672704906f01ec2e9fe0f99db813 Mon Sep 17 00:00:00 2001 From: amedora Date: Thu, 28 Jun 2018 10:57:00 +0900 Subject: [PATCH 1/7] add reference to @susisu/mte-kernel --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 28f1239b..5741287b 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "dependencies": { "@rokt33r/markdown-it-math": "^4.0.1", "@rokt33r/season": "^5.3.0", + "@susisu/mte-kernel": "^2.0.0", "aws-sdk": "^2.48.0", "aws-sdk-mobile-analytics": "^0.9.2", "codemirror": "^5.37.0", From b44772441daf6a0560add966d35e05e64cbf50bf Mon Sep 17 00:00:00 2001 From: amedora Date: Thu, 28 Jun 2018 10:38:02 +0900 Subject: [PATCH 2/7] implement TextEditorInterface --- browser/lib/TextEditorInterface.js | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 browser/lib/TextEditorInterface.js diff --git a/browser/lib/TextEditorInterface.js b/browser/lib/TextEditorInterface.js new file mode 100644 index 00000000..53ae2337 --- /dev/null +++ b/browser/lib/TextEditorInterface.js @@ -0,0 +1,53 @@ +import { Point } from '@susisu/mte-kernel' + +export default class TextEditorInterface { + constructor (editor) { + this.editor = editor + } + + getCursorPosition () { + const pos = this.editor.getCursor() + return new Point(pos.line, pos.ch) + } + + setCursorPosition (pos) { + this.editor.setCursor({line: pos.row, ch: pos.column}) + } + + setSelectionRange (range) { + this.editor.setSelection({ + anchor: {line: range.start.row, ch: range.start.column}, + head: {line: range.end.row, ch: range.end.column} + }) + } + + getLastRow () { + return this.editor.lastLine() + } + + acceptsTableEdit (row) { + return true + } + + getLine (row) { + return this.editor.getLine(row) + } + + insertLine (row, line) { + this.editor.replaceRange(line, {line: row, ch: 0}) + } + + deleteLine (row) { + this.editor.replaceRange('', {line: row, ch: 0}, {line: row, ch: this.editor.getLine(row).length}) + } + + replaceLines (startRow, endRow, lines) { + endRow-- // because endRow is a first line after a table. + const endRowCh = this.editor.getLine(endRow).length + this.editor.replaceRange(lines, {line: startRow, ch: 0}, {line: endRow, ch: endRowCh}) + } + + transact (func) { + func() + } +} From 7ee12752ecdb7bac7cc9817c5d8d31e8ae51d012 Mon Sep 17 00:00:00 2001 From: amedora Date: Thu, 28 Jun 2018 10:53:56 +0900 Subject: [PATCH 3/7] CodeEditor use TextEditorInterface --- browser/components/CodeEditor.js | 1 + 1 file changed, 1 insertion(+) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 91e7683a..06171660 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -5,6 +5,7 @@ import CodeMirror from 'codemirror' import 'codemirror-mode-elixir' import attachmentManagement from 'browser/main/lib/dataApi/attachmentManagement' import convertModeName from 'browser/lib/convertModeName' +import TextEditorInterface from 'browser/lib/TextEditorInterface' import eventEmitter from 'browser/main/lib/eventEmitter' import iconv from 'iconv-lite' import crypto from 'crypto' From c9d05b11172a51147fbfa9c7e02a059520207980 Mon Sep 17 00:00:00 2001 From: amedora Date: Thu, 28 Jun 2018 13:25:37 +0900 Subject: [PATCH 4/7] CodeEditor use TableEditor --- browser/components/CodeEditor.js | 1 + 1 file changed, 1 insertion(+) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 06171660..92c04d1c 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -5,6 +5,7 @@ import CodeMirror from 'codemirror' import 'codemirror-mode-elixir' import attachmentManagement from 'browser/main/lib/dataApi/attachmentManagement' import convertModeName from 'browser/lib/convertModeName' +import { options, TableEditor } from '@susisu/mte-kernel' import TextEditorInterface from 'browser/lib/TextEditorInterface' import eventEmitter from 'browser/main/lib/eventEmitter' import iconv from 'iconv-lite' From f0941f47dd67b0a0c1fe70a77b4a0c3decd2ff04 Mon Sep 17 00:00:00 2001 From: amedora Date: Thu, 28 Jun 2018 13:29:24 +0900 Subject: [PATCH 5/7] create TableEditor when CodeEditor mounted --- browser/components/CodeEditor.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 92c04d1c..29d553f8 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -184,6 +184,8 @@ export default class CodeEditor extends React.Component { CodeMirror.Vim.defineEx('wq', 'wq', this.quitEditor) CodeMirror.Vim.defineEx('qw', 'qw', this.quitEditor) CodeMirror.Vim.map('ZZ', ':q', 'normal') + + this.tableEditor = new TableEditor(new TextEditorInterface(this.editor)) } expandSnippet (line, cursor, cm, snippets) { From 7bacd6f8f0a17d0417b6d71e9ccc71f61d02f31c Mon Sep 17 00:00:00 2001 From: amedora Date: Thu, 28 Jun 2018 13:33:48 +0900 Subject: [PATCH 6/7] CodeEditor can handle 'code:format-table' event --- browser/components/CodeEditor.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 29d553f8..5eda3877 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -50,6 +50,8 @@ export default class CodeEditor extends React.Component { } this.searchHandler = (e, msg) => this.handleSearch(msg) this.searchState = null + + this.formatTable = () => this.handleFormatTable() } handleSearch (msg) { @@ -83,6 +85,10 @@ export default class CodeEditor extends React.Component { }) } + handleFormatTable () { + this.tableEditor.formatAll(options({textWidthOptions: {}})) + } + componentDidMount () { const { rulers, enableRulers } = this.props const expandSnippet = this.expandSnippet.bind(this) @@ -186,6 +192,7 @@ export default class CodeEditor extends React.Component { CodeMirror.Vim.map('ZZ', ':q', 'normal') this.tableEditor = new TableEditor(new TextEditorInterface(this.editor)) + eventEmitter.on('code:format-table', this.formatTable) } expandSnippet (line, cursor, cm, snippets) { @@ -268,6 +275,8 @@ export default class CodeEditor extends React.Component { this.editor.off('scroll', this.scrollHandler) const editorTheme = document.getElementById('editorTheme') editorTheme.removeEventListener('load', this.loadStyleHandler) + + eventEmitter.off('code:format-table', this.formatTable) } componentDidUpdate (prevProps, prevState) { From 82db986bd7d5a7e2c3ec19c6fe393578f2f464d8 Mon Sep 17 00:00:00 2001 From: amedora Date: Tue, 26 Jun 2018 14:48:07 +0900 Subject: [PATCH 7/7] add 'Format Table' in the File menu --- lib/main-menu.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/main-menu.js b/lib/main-menu.js index 9345bd67..c639da0f 100644 --- a/lib/main-menu.js +++ b/lib/main-menu.js @@ -136,6 +136,15 @@ const file = { { type: 'separator' }, + { + label: 'Format Table', + click () { + mainWindow.webContents.send('code:format-table') + } + }, + { + type: 'separator' + }, { label: 'Print', accelerator: 'CommandOrControl+P',