Skip to content

Commit ad5be66

Browse files
committed
Add status bar icon to toggle linter
Signed-off-by: Yukai Huang <yukaihuangtw@gmail.com>
1 parent 968e042 commit ad5be66

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

public/css/index.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,15 @@ div[contenteditable]:empty:not(:focus):before{
513513
.status-bar .status-indicators .status-keymap > a,
514514
.status-bar .status-indicators .status-theme > a,
515515
.status-bar .status-indicators .status-spellcheck > a,
516+
.status-bar .status-indicators .status-linter > a,
516517
.status-bar .status-indicators .status-preferences > a {
517518
color: inherit;
518519
text-decoration: none;
519520
}
520521

521522
.status-bar .status-indicators .status-theme,
522523
.status-bar .status-indicators .status-spellcheck,
524+
.status-bar .status-indicators .status-linter,
523525
.status-bar .status-indicators .status-preferences {
524526
padding: 0 4.3px;
525527
}
@@ -540,17 +542,20 @@ div[contenteditable]:empty:not(:focus):before{
540542
}
541543

542544
.ui-theme-toggle,
545+
.ui-linter-toggle,
543546
.ui-spellcheck-toggle {
544547
opacity: 0.2;
545548
cursor: pointer;
546549
}
547550

548551
.ui-theme-toggle.active,
552+
.ui-linter-toggle.active,
549553
.ui-spellcheck-toggle.active {
550554
opacity: 1;
551555
}
552556

553557
.ui-theme-toggle:hover,
558+
.ui-linter-toggle:hover,
554559
.ui-spellcheck-toggle:hover {
555560
opacity: 0.8;
556561
}

public/js/lib/editor/index.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export default class Editor {
232232
this.statusLength = this.statusBar.find('.status-length')
233233
this.statusTheme = this.statusBar.find('.status-theme')
234234
this.statusSpellcheck = this.statusBar.find('.status-spellcheck')
235+
this.statusLinter = this.statusBar.find('.status-linter')
235236
this.statusPreferences = this.statusBar.find('.status-preferences')
236237
this.statusPanel = this.editor.addPanel(this.statusBar[0], {
237238
position: 'bottom'
@@ -241,6 +242,7 @@ export default class Editor {
241242
this.setKeymap()
242243
this.setTheme()
243244
this.setSpellcheck()
245+
this.setLinter()
244246
this.setPreferences()
245247
}
246248

@@ -499,6 +501,42 @@ export default class Editor {
499501
}
500502
}
501503

504+
toggleLinter (enable) {
505+
const gutters = this.editor.getOption('gutters')
506+
const lintGutter = 'CodeMirror-lint-markers'
507+
508+
if (enable) {
509+
if (!gutters.includes(lintGutter)) {
510+
this.editor.setOption('gutters', [lintGutter, ...gutters])
511+
}
512+
Cookies.set('linter', true, {
513+
expires: 365
514+
})
515+
} else {
516+
this.editor.setOption('gutters', gutters.filter(g => g !== lintGutter))
517+
Cookies.remove('linter')
518+
}
519+
this.editor.setOption('lint', enable)
520+
}
521+
522+
setLinter () {
523+
const linterToggle = this.statusLinter.find('.ui-linter-toggle')
524+
525+
const updateLinterStatus = (enable) => {
526+
linterToggle.toggleClass('active', enable)
527+
}
528+
529+
linterToggle.click(() => {
530+
const lintEnable = this.editor.getOption('lint')
531+
this.toggleLinter.bind(this)(!lintEnable)
532+
updateLinterStatus(!lintEnable)
533+
})
534+
535+
const enable = !!Cookies.get('linter')
536+
this.toggleLinter.bind(this)(enable)
537+
updateLinterStatus(enable)
538+
}
539+
502540
resetEditorKeymapToBrowserKeymap () {
503541
var keymap = this.editor.getOption('keyMap')
504542
if (!this.jumpToAddressBarKeymapValue) {
@@ -553,7 +591,6 @@ export default class Editor {
553591
this.editor = CodeMirror.fromTextArea(textit, {
554592
mode: defaultEditorMode,
555593
backdrop: defaultEditorMode,
556-
lint: true,
557594
keyMap: 'sublime',
558595
viewportMargin: viewportMargin,
559596
styleActiveLine: true,
@@ -573,7 +610,6 @@ export default class Editor {
573610
autoCloseTags: true,
574611
foldGutter: true,
575612
gutters: [
576-
'CodeMirror-lint-markers',
577613
'CodeMirror-linenumbers',
578614
'authorship-gutters',
579615
'CodeMirror-foldgutter'

public/js/lib/editor/statusbar.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
<div class="status-spellcheck">
3838
<a class="ui-spellcheck-toggle" title="Toggle spellcheck"><i class="fa fa-check fa-fw"></i></a>
3939
</div>
40+
<div class="status-linter">
41+
<a class="ui-linter-toggle" title="Toggle linter"><i class="fa fa-lightbulb-o fa-fw"></i></a>
42+
</div>
4043
</div>
4144
</div>

0 commit comments

Comments
 (0)