From ad5be66206046038ca91580b352e262545fb3a3f Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Sat, 17 Aug 2019 00:17:22 +0800 Subject: [PATCH] Add status bar icon to toggle linter Signed-off-by: Yukai Huang --- public/css/index.css | 5 ++++ public/js/lib/editor/index.js | 40 +++++++++++++++++++++++++++-- public/js/lib/editor/statusbar.html | 3 +++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/public/css/index.css b/public/css/index.css index d9ebf6de..5663c1bb 100644 --- a/public/css/index.css +++ b/public/css/index.css @@ -513,6 +513,7 @@ div[contenteditable]:empty:not(:focus):before{ .status-bar .status-indicators .status-keymap > a, .status-bar .status-indicators .status-theme > a, .status-bar .status-indicators .status-spellcheck > a, +.status-bar .status-indicators .status-linter > a, .status-bar .status-indicators .status-preferences > a { color: inherit; text-decoration: none; @@ -520,6 +521,7 @@ div[contenteditable]:empty:not(:focus):before{ .status-bar .status-indicators .status-theme, .status-bar .status-indicators .status-spellcheck, +.status-bar .status-indicators .status-linter, .status-bar .status-indicators .status-preferences { padding: 0 4.3px; } @@ -540,17 +542,20 @@ div[contenteditable]:empty:not(:focus):before{ } .ui-theme-toggle, +.ui-linter-toggle, .ui-spellcheck-toggle { opacity: 0.2; cursor: pointer; } .ui-theme-toggle.active, +.ui-linter-toggle.active, .ui-spellcheck-toggle.active { opacity: 1; } .ui-theme-toggle:hover, +.ui-linter-toggle:hover, .ui-spellcheck-toggle:hover { opacity: 0.8; } diff --git a/public/js/lib/editor/index.js b/public/js/lib/editor/index.js index 6f7cb2be..5c94be69 100644 --- a/public/js/lib/editor/index.js +++ b/public/js/lib/editor/index.js @@ -232,6 +232,7 @@ export default class Editor { this.statusLength = this.statusBar.find('.status-length') this.statusTheme = this.statusBar.find('.status-theme') this.statusSpellcheck = this.statusBar.find('.status-spellcheck') + this.statusLinter = this.statusBar.find('.status-linter') this.statusPreferences = this.statusBar.find('.status-preferences') this.statusPanel = this.editor.addPanel(this.statusBar[0], { position: 'bottom' @@ -241,6 +242,7 @@ export default class Editor { this.setKeymap() this.setTheme() this.setSpellcheck() + this.setLinter() this.setPreferences() } @@ -499,6 +501,42 @@ export default class Editor { } } + toggleLinter (enable) { + const gutters = this.editor.getOption('gutters') + const lintGutter = 'CodeMirror-lint-markers' + + if (enable) { + if (!gutters.includes(lintGutter)) { + this.editor.setOption('gutters', [lintGutter, ...gutters]) + } + Cookies.set('linter', true, { + expires: 365 + }) + } else { + this.editor.setOption('gutters', gutters.filter(g => g !== lintGutter)) + Cookies.remove('linter') + } + this.editor.setOption('lint', enable) + } + + setLinter () { + const linterToggle = this.statusLinter.find('.ui-linter-toggle') + + const updateLinterStatus = (enable) => { + linterToggle.toggleClass('active', enable) + } + + linterToggle.click(() => { + const lintEnable = this.editor.getOption('lint') + this.toggleLinter.bind(this)(!lintEnable) + updateLinterStatus(!lintEnable) + }) + + const enable = !!Cookies.get('linter') + this.toggleLinter.bind(this)(enable) + updateLinterStatus(enable) + } + resetEditorKeymapToBrowserKeymap () { var keymap = this.editor.getOption('keyMap') if (!this.jumpToAddressBarKeymapValue) { @@ -553,7 +591,6 @@ export default class Editor { this.editor = CodeMirror.fromTextArea(textit, { mode: defaultEditorMode, backdrop: defaultEditorMode, - lint: true, keyMap: 'sublime', viewportMargin: viewportMargin, styleActiveLine: true, @@ -573,7 +610,6 @@ export default class Editor { autoCloseTags: true, foldGutter: true, gutters: [ - 'CodeMirror-lint-markers', 'CodeMirror-linenumbers', 'authorship-gutters', 'CodeMirror-foldgutter' diff --git a/public/js/lib/editor/statusbar.html b/public/js/lib/editor/statusbar.html index 24cbf6c2..c4c796bc 100644 --- a/public/js/lib/editor/statusbar.html +++ b/public/js/lib/editor/statusbar.html @@ -37,5 +37,8 @@
+
+ +