From fb2ca04c34d6c5c2bc936ffbea83bb6dce085f9a Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 29 Jun 2016 11:51:53 +0200 Subject: [PATCH] add a theme selector --- www/code/main.js | 61 ++++++++++++++++++++++++++++++++++++++++++---- www/code/themes.js | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 www/code/themes.js diff --git a/www/code/main.js b/www/code/main.js index f0354292c..42b34567a 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -10,10 +10,11 @@ define([ '/bower_components/chainpad-json-validator/json-ot.js', '/common/cryptpad-common.js', '/code/modes.js', + '/code/themes.js', '/bower_components/file-saver/FileSaver.min.js', '/bower_components/jquery/dist/jquery.min.js', '/customize/pad.js' -], function (Config, /*RTCode,*/ Messages, Crypto, Realtime, TextPatcher, Toolbar, JSONSortify, JsonOT, Cryptpad, Modes) { +], function (Config, /*RTCode,*/ Messages, Crypto, Realtime, TextPatcher, Toolbar, JSONSortify, JsonOT, Cryptpad, Modes, Themes) { var $ = window.jQuery; var saveAs = window.saveAs; var module = window.APP = {}; @@ -51,15 +52,46 @@ define([ readOnly: true }); - var setMode = module.setMode = function (mode) { + var setMode = module.setMode = function (mode, $select) { if (mode === 'text') { editor.setOption('mode', 'text'); return; } CodeMirror.autoLoadMode(editor, mode); editor.setOption('mode', mode); + if ($select && select.val) { $select.val(mode); } }; + + var setTheme = module.setTheme = (function () { + var path = './theme/'; + + var $head = $(ifrw.document.head); + + var themeLoaded = module.themeLoaded = function (theme) { + return $head.find('link[href*="'+theme+'"]').length; + }; + + var loadTheme = module.loadTheme = function (theme) { + $head.append($('', { + rel: 'stylesheet', + href: path + theme + '.css', + })); + }; + + return function (theme, $select) { + if (!theme) { + editor.setOption('theme', 'default'); + } else { + if (!themeLoaded(theme)) { + loadTheme(theme); + } + editor.setOption('theme', theme); + } + if ($select && select.val) { $select.val(theme || 'default'); } + }; + }()); + var setEditable = module.setEditable = function (bool) { editor.setOption('readOnly', !bool); }; @@ -177,21 +209,40 @@ define([ onLocal(); })); - var dropdown = '\n' + Modes.map(function (o) { var selected = o.mode === 'javascript'? ' selected="selected"' : ''; return ''; }).join('\n') + ''; + var themeDropdown = ''; - $bar.find('.rtwysiwyg-toolbar-rightside').append(dropdown); + $bar.find('.rtwysiwyg-toolbar-rightside') + .append(syntaxDropdown); - var $language = $bar.find('#language-mode').on('change', function () { + var $language = module.$language = $bar.find('#language-mode').on('change', function () { var mode = $language.val(); setMode(mode); }); + $bar.find('.rtwysiwyg-toolbar-rightside') + .append(themeDropdown); + + var $theme = $bar.find('select#display-theme'); + console.log($theme); + + $theme.on('change', function () { + var theme = $theme.val(); + console.log("Setting theme to %s", theme); + setTheme(theme); + }); + window.location.hash = info.channel + secret.key; Cryptpad.rememberPad(); }; diff --git a/www/code/themes.js b/www/code/themes.js new file mode 100644 index 000000000..237459ff5 --- /dev/null +++ b/www/code/themes.js @@ -0,0 +1,56 @@ +define(function () { + return [ + "3024-day 3024-day.css", + "3024-night 3024-night.css", + "abcdef abcdef.css", + "ambiance-mobile ambiance-mobile.css", + "ambiance ambiance.css", + "base16-dark base16-dark.css", + "base16-light base16-light.css", + "bespin bespin.css", + "blackboard blackboard.css", + "cobalt cobalt.css", + "colorforth colorforth.css", + "default default", + "dracula dracula.css", + "eclipse eclipse.css", + "elegant elegant.css", + "erlang-dark erlang-dark.css", + "hopscotch hopscotch.css", + "icecoder icecoder.css", + "isotope isotope.css", + "lesser-dark lesser-dark.css", + "liquibyte liquibyte.css", + "material material.css", + "mbo mbo.css", + "mdn-like mdn-like.css", + "midnight midnight.css", + "monokai monokai.css", + "neat neat.css", + "neo neo.css", + "night night.css", + "paraiso-dark paraiso-dark.css", + "paraiso-light paraiso-light.css", + "pastel-on-dark pastel-on-dark.css", + "railscasts railscasts.css", + "rubyblue rubyblue.css", + "seti seti.css", + "solarized solarized.css", + "the-matrix the-matrix.css", + "tomorrow-night-bright tomorrow-night-bright.css", + "tomorrow-night-eighties tomorrow-night-eighties.css", + "ttcn ttcn.css", + "twilight twilight.css", + "vibrant-ink vibrant-ink.css", + "xq-dark xq-dark.css", + "xq-light xq-light.css", + "yeti yeti.css", + "zenburn zenburn.css" + ].map(function (line) { + var kv = line.split(/\s/); + return { + name: kv[0].replace(/_/g, ' '), + theme: kv[1], + }; + }); +});