From 4db8a9ea9a3e7fc335319071b1b3ac26249ec66d Mon Sep 17 00:00:00 2001 From: eight Date: Wed, 7 Nov 2018 19:33:14 +0800 Subject: [PATCH] Fix: output codemirror themes to a separated file (#538) --- edit.html | 1 + edit/codemirror-themes.js | 61 +++++++++++++++++++++++++++++++ edit/edit.js | 60 +----------------------------- package.json | 3 +- tools/update-codemirror-themes.js | 23 +++++------- 5 files changed, 75 insertions(+), 73 deletions(-) create mode 100644 edit/codemirror-themes.js diff --git a/edit.html b/edit.html index 7b7a10dd..c064d89f 100644 --- a/edit.html +++ b/edit.html @@ -92,6 +92,7 @@ + diff --git a/edit/codemirror-themes.js b/edit/codemirror-themes.js new file mode 100644 index 00000000..f0501a4d --- /dev/null +++ b/edit/codemirror-themes.js @@ -0,0 +1,61 @@ +/* exported CODEMIRROR_THEMES */ +// this file is generated by update-codemirror-themes.js +'use strict'; + +const CODEMIRROR_THEMES = [ + '3024-day', + '3024-night', + 'abcdef', + 'ambiance', + 'ambiance-mobile', + 'base16-dark', + 'base16-light', + 'bespin', + 'blackboard', + 'cobalt', + 'colorforth', + 'darcula', + 'dracula', + 'duotone-dark', + 'duotone-light', + 'eclipse', + 'elegant', + 'erlang-dark', + 'gruvbox-dark', + 'hopscotch', + 'icecoder', + 'idea', + 'isotope', + 'lesser-dark', + 'liquibyte', + 'lucario', + 'material', + 'mbo', + 'mdn-like', + 'midnight', + 'monokai', + 'neat', + 'neo', + 'night', + 'oceanic-next', + 'panda-syntax', + 'paraiso-dark', + 'paraiso-light', + 'pastel-on-dark', + 'railscasts', + 'rubyblue', + 'seti', + 'shadowfox', + 'solarized', + 'ssms', + 'the-matrix', + 'tomorrow-night-bright', + 'tomorrow-night-eighties', + 'ttcn', + 'twilight', + 'vibrant-ink', + 'xq-dark', + 'xq-light', + 'yeti', + 'zenburn' +]; diff --git a/edit/edit.js b/edit/edit.js index f28ab59d..8551381a 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -2,7 +2,7 @@ createSourceEditor queryTabs sessionStorageHash getOwnTab FIREFOX API tryCatch closeCurrentTab messageBox debounce workerUtil beautify ignoreChromeError - moveFocus msg createSectionsEditor rerouteHotkeys */ + moveFocus msg createSectionsEditor rerouteHotkeys CODEMIRROR_THEMES */ /* exported showCodeMirrorPopup editorWorker toggleContextMenuDelete */ 'use strict'; @@ -46,63 +46,7 @@ preinit(); if (!chrome.runtime.getPackageDirectoryEntry) { const themes = [ chrome.i18n.getMessage('defaultTheme'), - /* populate-theme-start */ - '3024-day', - '3024-night', - 'abcdef', - 'ambiance', - 'ambiance-mobile', - 'base16-dark', - 'base16-light', - 'bespin', - 'blackboard', - 'cobalt', - 'colorforth', - 'darcula', - 'dracula', - 'duotone-dark', - 'duotone-light', - 'eclipse', - 'elegant', - 'erlang-dark', - 'gruvbox-dark', - 'hopscotch', - 'icecoder', - 'idea', - 'isotope', - 'lesser-dark', - 'liquibyte', - 'lucario', - 'material', - 'mbo', - 'mdn-like', - 'midnight', - 'monokai', - 'neat', - 'neo', - 'night', - 'oceanic-next', - 'panda-syntax', - 'paraiso-dark', - 'paraiso-light', - 'pastel-on-dark', - 'railscasts', - 'rubyblue', - 'seti', - 'shadowfox', - 'solarized', - 'ssms', - 'the-matrix', - 'tomorrow-night-bright', - 'tomorrow-night-eighties', - 'ttcn', - 'twilight', - 'vibrant-ink', - 'xq-dark', - 'xq-light', - 'yeti', - 'zenburn', - /* populate-theme-end */ + ...CODEMIRROR_THEMES ]; localStorage.codeMirrorThemes = themes.join(' '); return Promise.resolve(themes); diff --git a/package.json b/package.json index c1939f03..c1f6e2d7 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "devDependencies": { "archiver": "^3.0.0", "codemirror": "^5.41.0", + "endent": "^1.2.0", "eslint": "^5.8.0", "fs-extra": "^7.0.0", "jsonlint": "^1.6.3", @@ -18,8 +19,8 @@ "stylelint-bundle": "^8.0.0", "stylus-lang-bundle": "^0.54.5", "updates": "^5.1.2", - "web-ext": "^2.9.1", "usercss-meta": "^0.8.1", + "web-ext": "^2.9.1", "webext-tx-fix": "^0.3.1" }, "scripts": { diff --git a/tools/update-codemirror-themes.js b/tools/update-codemirror-themes.js index a9679153..94f1b4e4 100644 --- a/tools/update-codemirror-themes.js +++ b/tools/update-codemirror-themes.js @@ -3,6 +3,7 @@ const fs = require('fs-extra'); const path = require('path'); +const endent = require('endent'); // Update theme names list in codemirror-editing-hook.js async function getThemes() { @@ -14,21 +15,15 @@ async function getThemes() { .sort(); } -function replaceThemes(content, themes) { - const lineFeed = content.includes('\r\n') ? '\r\n' : '\n'; - return content.replace( - /(\x20+)(\/\*\s*populate-theme-start\s*\*\/)[\s\S]+?(\/\*\s*populate-theme-end\s*\*\/)/, - (_, indent, intro, outro) => - indent + intro + lineFeed + - themes.map(_ => `${indent}'${_}',`).join(lineFeed) + lineFeed + - indent + outro - ); -} - async function updateHook(themes) { - const fileName = path.join(__dirname, '..', 'edit/codemirror-editing-hooks.js'); - const content = await fs.readFile(fileName, 'utf-8'); - fs.writeFile(fileName, replaceThemes(content, themes)); + const fileName = path.join(__dirname, '..', 'edit/codemirror-themes.js'); + fs.writeFile(fileName, endent` + /* exported CODEMIRROR_THEMES */ + // this file is generated by update-codemirror-themes.js + 'use strict'; + + const CODEMIRROR_THEMES = ${JSON.stringify(themes, null, 2)}; + `.replace(/"/g, "'") + '\n'); } function exit(err) {