Эх сурвалжийг харах

fix add theme or set hexo config, same name cover.

zhuzhuyule 8 жил өмнө
parent
commit
66741df3bf

+ 5 - 2
app/moe-app.js

@@ -213,8 +213,11 @@ class MoeditorApplication {
     getHighlightThemesDir(){
         const currTheme = this.config.get('render-theme')
         let themedir = 'github'
-        if (!(currTheme == 'GitHub' || currTheme == 'No Theme')){
-            themedir = currTheme.toLowerCase();
+        if (!(currTheme == '*GitHub' || currTheme == '*No Theme')){
+            if (currTheme.startsWith('*'))
+                themedir = currTheme.slice(1).toLowerCase();
+            else
+                themedir = currTheme.toLowerCase();
         }
         themedir = path.join(moeApp.Const.path+'/views/highlightThemes/',themedir);
 	    return (fs.existsSync(themedir)? themedir : '');

+ 1 - 1
app/moe-config-default.js

@@ -37,7 +37,7 @@ module.exports = {
     'auto-reload': 'auto',
     'auto-save': 'disabled',
     'highlight-theme': 'github',
-    'render-theme': 'GitHub',
+    'render-theme': '*GitHub',
     'custom-render-themes': {},
     'custom-csss': {},
     'editor-ShowLineNumber': false,

+ 9 - 0
app/moe-l10n.js

@@ -176,6 +176,11 @@ const strings = {
         "Hexo Config": "Hexo Config",
         "Hexo tags": "Tag Templates",
 
+        "Waring": "Waring",
+        "WaringNoFile": "No CSS files found !",
+        "WaringNoFileDetail1": 'Please check the directory: \n"',
+        "WaringNoFileDetail2": '"\n\nIf you were used Hexo,\nyou can try open Hexo\'s directory and execute command \n"hexo -g" to solve.',
+
         "Default": "Default",
         "System Default": "System Default",
 
@@ -289,6 +294,10 @@ const strings = {
         "Hexo Config": "Hexo配置文件",
         "Hexo tags": "Tag模板目录",
 
+        "Waring": "警告",
+        "WaringNoFile": "没有发现CSS文件!",
+        "WaringNoFileDetail1": '请检查如下路径: \n"',
+        "WaringNoFileDetail2": '"\n\n如果你使用Hexo框架,\n可尝试在Hexo项目目录下运行命令"hexo -g"来解决。',
 
         "Default": "默认",
         "System Default": "系统默认",

+ 7 - 5
views/main/moe-rendertheme.js

@@ -19,19 +19,21 @@
 
 'use strict'
 
-const fs = require('fs');
 const path = require('path');
 const url = require('url');
 
-const builtin = fs.readdirSync(path.resolve(app.getAppPath(), 'themes'));
 
 module.exports = {
     getCSS(forURL) {
         const theme = moeApp.config.get('render-theme');
-        moeApp.defTheme = ['GitHub','No Theme'].indexOf(theme) > -1;
+        moeApp.defTheme = ['*GitHub','*No Theme'].indexOf(theme) > -1;
+
         let res;
-        if (builtin.includes(theme)) res = path.resolve(app.getAppPath(), 'themes', theme, 'main.css');
-        else res = path.resolve(moeApp.config.get('custom-render-themes')[theme], 'main.css');
+        if (theme.startsWith('*')) {
+            res = path.resolve(app.getAppPath(), 'themes', theme.slice(1), 'main.css');
+        }
+        else
+            res = path.resolve(moeApp.config.get('custom-render-themes')[theme], 'main.css');
         if (forURL) res = url.resolve('file://', res);
         return res;
     }

+ 6 - 1
views/main/moe-settings.js

@@ -125,7 +125,7 @@ function setHighlightTheme(val) {
 
 function setRenderTheme(val) {
     const container = document.getElementById('container');
-    if (['GitHub','No Theme'].indexOf(val) > -1){
+    if (['*GitHub','*No Theme'].indexOf(val) > -1){
         $(container).addClass('_def');
         $(container).removeClass('post-body');
         moeApp.defTheme = true;
@@ -185,6 +185,11 @@ function setCustomCSSs(val) {
     }
 }
 
+// just use to v1.1.8 ---> v1.1.10
+let tmpTheme = moeApp.config.get('render-theme');
+if (['GitHub','No Theme','next'].indexOf(tmpTheme) > -1){
+    moeApp.config.set('render-theme', '*'+tmpTheme)
+}
 
 tryRun(setEditorFont, moeApp.config.get('editor-font'));
 tryRun(setShowLineNumber, !!moeApp.config.get('editor-ShowLineNumber'));

+ 8 - 0
views/settings/moe-settings.css

@@ -127,3 +127,11 @@ html.darwin input {
     padding-left: 7px;
     border-radius: 5px;
 }
+
+option.builtin{
+    background-color: #eee;
+}
+
+option.invalidFile {
+    color: red;
+}

+ 47 - 43
views/settings/moe-settings.js

@@ -77,18 +77,31 @@ document.addEventListener('DOMContentLoaded', () => {
 
     // Custom render themes
     let renderThemeSelect = document.querySelector('select[data-key="render-theme"]');
+
+    function setCurrentTheme(currTheme, changeConfig){
+        if (!currTheme){
+            currTheme = moeApp.config.get('render-theme');
+        } else {
+            if (changeConfig)
+                moeApp.config.set("render-theme", currTheme);
+        }
+        renderThemeSelect.value = currTheme;
+        ipcRenderer.send('setting-changed', { key: "render-theme", val: currTheme });
+    }
+
     function reloadRenderThemeSelect() {
         renderThemeSelect.querySelectorAll('option:not(.builtin)').forEach((a) => renderThemeSelect.removeChild(a));
         const custom = moeApp.config.get('custom-render-themes');
-        for (const x in custom) {
+        for (const item in custom) {
             const option = document.createElement('option');
-            option.value = option.text = x;
-            if (fs.existsSync(path.resolve( custom[x],'main.css')))
+            option.value = option.text = item;
+            if (!fs.existsSync(path.resolve( custom[item],'main.css')))
                 option.classList.add('invalidFile');
             renderThemeSelect.appendChild(option);
         }
-        renderThemeSelect.value = moeApp.config.get('render-theme');
+        setCurrentTheme();
     }
+
     let renderThemeButtonAdd = document.querySelector('select[data-key="render-theme"] ~ button.button-add');
     let renderThemeButtonRemove = document.querySelector('select[data-key="render-theme"] ~ button.button-remove');
     function setRenderThemeButtons() {
@@ -132,10 +145,10 @@ document.addEventListener('DOMContentLoaded', () => {
             for (const s of a) themes[path.basename(s)] = s;
             moeApp.config.set('custom-render-themes', themes);
             console.log(themes);
-            reloadRenderThemeSelect();
             if (a.length > 0){
-                renderThemeSelect.value = path.basename(s);
+                moeApp.config.set('render-theme', path.basename(a[0]));
             }
+            reloadRenderThemeSelect();
         });
     });
     renderThemeButtonRemove.addEventListener('click', () => {
@@ -144,15 +157,8 @@ document.addEventListener('DOMContentLoaded', () => {
         let themes = JSON.parse(JSON.stringify(moeApp.config.get('custom-render-themes')));
         themes[option.value] = undefined;
         moeApp.config.set('custom-render-themes', themes);
+        moeApp.config.set('render-theme', '*GitHub');
         reloadRenderThemeSelect();
-
-        // Reset to default
-        moeApp.config.set('render-theme', 'GitHub');
-        renderThemeSelect.value = 'GitHub';
-
-        let e = document.createEvent('HTMLEvents');
-        e.initEvent('change', false, true);
-        renderThemeSelect.dispatchEvent(e);
     });
 
     // Highlight Theme
@@ -367,7 +373,11 @@ document.addEventListener('DOMContentLoaded', () => {
 
     function newCustomeTheme(dir){
         try {
+            if ( !fs.existsSync(dir ))
+                return false;
             let fileNames = fs.readdirSync(dir);
+            if (fileNames.includes('main.css') )
+                return true;
             let stylecss = [];
             fileNames.filter(function (file) {
                 if (file.endsWith('.css')) {
@@ -377,9 +387,6 @@ document.addEventListener('DOMContentLoaded', () => {
             });
             if (stylecss.length > 0) {
                 fs.writeFileSync(path.resolve(dir, 'main.css'), stylecss.toString());
-
-                themes[hexoTheme] = dir;
-                moeApp.config.set('custom-render-themes', themes);
                 return true;
             }
         } catch (e) {
@@ -400,49 +407,46 @@ document.addEventListener('DOMContentLoaded', () => {
             let breaksCheck = document.querySelector('input[data-key="breaks"]');
             breaksCheck.checked || breaksCheck.click();
 
-
             let hexoConfig = moeApp.getHexo().config;
             let themes = JSON.parse(JSON.stringify(moeApp.config.get('custom-render-themes')));
             let hexoTheme = hexoConfig.theme;
             let isFindStyle = false;
+
             if (themes[hexoTheme]){
-                if (fs.existsSync(path.resolve( themes[hexoTheme],'main.css'))) {
-                    renderThemeSelect.value = hexoTheme;
-                    reloadHighlightSelect(hexoTheme);
-                    isFindStyle = true;
-                }else {
-                    if (newCustomeTheme(themes[hexoTheme])) {
-                        isFindStyle = true;
-                    }
-                }
+                    isFindStyle = newCustomeTheme(themes[hexoTheme])
             }
             //未配置主题,自动配置
             let classDir = path.join(hexoConfig.__basedir,hexoConfig.public_dir,'css');
             if (!isFindStyle){
                 if ( fs.existsSync(classDir) && fs.lstatSync(classDir).isDirectory()){
-                    try {
-                        let fileNames = fs.readdirSync(classDir);
-                        if (fileNames.includes('main.css')){
-                            isFindStyle = true;
-                        } else {
-                            isFindStyle = newCustomeTheme(classDir);
-                        }
-                    } catch (err) {
-                    }
+                    isFindStyle = newCustomeTheme(classDir);
                 }
             }
             if (isFindStyle) {
+                for (let item in themes){
+                    if (themes.hasOwnProperty(item) && themes[item] == classDir) {
+                            delete themes[item];
+                    }
+                }
+
                 themes[hexoTheme] = classDir;
                 moeApp.config.set('custom-render-themes', themes);
-                reloadRenderThemeSelect();
-
-                renderThemeSelect.value = hexoTheme;
-                const key = renderThemeSelect.getAttribute('data-key');
-                moeApp.config.set(key, hexoTheme);
-                if (!renderThemeSelect.classList.contains('dont-notify'))
-                    ipcRenderer.send('setting-changed', { key: key, val: hexoTheme });
+                moeApp.config.set("render-theme", hexoTheme);
 
+                reloadRenderThemeSelect(hexoTheme);
                 reloadHighlightSelect(hexoTheme);
+            } else {
+                dialog.showMessageBox(
+                    window.w,
+                    {
+                        type: 'warning',
+                        title: __('Waring'),
+                        message: __("WaringNoFile"),
+                        detail: __("WaringNoFileDetail1") + classDir +  __("WaringNoFileDetail2")
+                    } ,
+                    function (res) {
+                    }
+                )
             }
     })
 });

+ 1 - 1
views/settings/settings.html

@@ -233,7 +233,7 @@
                                         <script>
                                             fs.readdirSync(`${app.getAppPath()}/themes`)
                                                 .forEach(s => {
-                                                    document.write(`<option value="${s}" class="builtin">${s}</option>`);
+                                                    document.write(`<option value="\*${s}" class="builtin">*${s}</option>`);
                                                 });
 
                                             const custom = moeApp.config.get('custom-render-themes');