From 09fb1209678b2acc18ee13ee0c407278e67d2c06 Mon Sep 17 00:00:00 2001 From: Cheng-Han, Wu Date: Tue, 21 Jun 2016 21:45:45 +0800 Subject: Update to support toggle editor theme and spellcheck in status bar --- public/js/index.js | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) (limited to 'public/js/index.js') diff --git a/public/js/index.js b/public/js/index.js index 43296af4..910ffa16 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -307,6 +307,8 @@ var statusIndicators = null; var statusLength = null; var statusKeymap = null; var statusIndent = null; +var statusTheme = null; +var statusSpellcheck = null; function getStatusBarTemplate(callback) { $.get(serverurl + '/views/statusbar.html', function (template) { @@ -328,12 +330,16 @@ function addStatusBar() { statusIndent = statusBar.find('.status-indent'); statusKeymap = statusBar.find('.status-keymap'); statusLength = statusBar.find('.status-length'); + statusTheme = statusBar.find('.status-theme'); + statusSpellcheck = statusBar.find('.status-spellcheck'); statusPanel = editor.addPanel(statusBar[0], { position: "bottom" }); setIndent(); setKeymap(); + setTheme(); + setSpellcheck(); } function setIndent() { @@ -467,6 +473,89 @@ function setKeymap() { }); } +function setTheme() { + var cookieTheme = Cookies.get('theme'); + if (cookieTheme) { + editor.setOption('theme', cookieTheme); + } + + var themeToggle = statusTheme.find('.ui-theme-toggle'); + themeToggle.click(function () { + var theme = editor.getOption('theme'); + if (theme == "one-dark") { + theme = "default"; + } else { + theme = "one-dark"; + } + editor.setOption('theme', theme); + Cookies.set('theme', theme, { + expires: 365 + }); + checkTheme(); + }); + function checkTheme() { + var theme = editor.getOption('theme'); + if (theme == "one-dark") { + themeToggle.removeClass('active'); + } else { + themeToggle.addClass('active'); + } + } + checkTheme(); +} + +function setSpellcheck() { + var cookieSpellcheck = Cookies.get('spellcheck'); + if (cookieSpellcheck) { + var mode = null; + if (cookieSpellcheck === 'true') { + mode = 'spell-checker'; + } else { + mode = 'gfm'; + } + if (mode && mode !== editor.getOption('mode')) { + editor.setOption('mode', mode); + } + } + + var spellcheckToggle = statusSpellcheck.find('.ui-spellcheck-toggle'); + spellcheckToggle.click(function () { + var mode = editor.getOption('mode'); + if (mode == "gfm") { + mode = "spell-checker"; + } else { + mode = "gfm"; + } + if (mode && mode !== editor.getOption('mode')) { + editor.setOption('mode', mode); + } + Cookies.set('spellcheck', (mode == "spell-checker"), { + expires: 365 + }); + checkSpellcheck(); + }); + function checkSpellcheck() { + var mode = editor.getOption('mode'); + if (mode == "gfm") { + spellcheckToggle.removeClass('active'); + } else { + spellcheckToggle.addClass('active'); + } + } + checkSpellcheck(); + + //workaround spellcheck might not activate beacuse the ajax loading + if (num_loaded < 2) { + var spellcheckTimer = setInterval(function () { + if (num_loaded >= 2) { + if (editor.getOption('mode') == "spell-checker") + editor.setOption('mode', "spell-checker"); + clearInterval(spellcheckTimer); + } + }, 100); + } +} + var selection = null; function updateStatusBar() { -- cgit v1.2.3