Browse Source

Optimize logic
Package image processing object

zhuzhuyule 8 years ago
parent
commit
4590a257e3

+ 0 - 1
app/moe-window.js

@@ -71,7 +71,6 @@ class MoeditorWindow {
 
     registerEvents() {
         this.window.on('close', (e) => {
-            console.log(moeApp.windows.length,this.changed)
             if (moeApp.windows.length && this.changed) {
                 const choice = dialog.showMessageBox(
                     this.window,

+ 173 - 0
views/main/hexo-image.js

@@ -0,0 +1,173 @@
+/*
+ *  This file is part of Moeditor.
+ *
+ *  Copyright (c) 2016 Menci <huanghaorui301@gmail.com>
+ *
+ *  Moeditor is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  Moeditor is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Moeditor. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+window.mkdirsSync = (dirpath, mode) => {
+    if (!fs.existsSync(dirpath)) {
+        var pathtmp;
+        dirpath.split(path.sep).forEach(function (dirname) {
+            if (pathtmp) {
+                pathtmp = path.join(pathtmp, dirname);
+            }
+            else {
+                pathtmp = dirname;
+            }
+            if (!fs.existsSync(pathtmp)) {
+                if (!fs.mkdirSync(pathtmp, mode)) {
+                    return false;
+                }
+            }
+        });
+    }
+    return true;
+}
+
+window.md5 = (text) => {
+    return require('crypto').createHash('md5').update(text).digest('hex');
+};
+
+class ImgManager {
+    constructor() {
+        this.fileName = path.basename(hexoWindow.fileName, path.extname(hexoWindow.fileName)) || hexoWindow.ID || "";
+        this.imgMD5IDList = {};
+        this.imgPathIDList = {};
+        this.imgBasePath = '';
+        this.type = {
+            png: '.png',
+            jpg: '.jpg',
+            jpeg: '.jpg',
+            bmp: '.bmp'
+        }
+        this.updateBase();
+    }
+
+    updateBase() {
+        let rootPaht = '';
+        if (moeApp.config.get('image-path')) {
+            rootPaht = moeApp.config.get('image-path');
+        } else if (moeApp.useHexo && hexo.config.__basedir) {
+            rootPaht = path.join(hexo.config.__basedir, 'source', 'images');
+        } else {
+            rootPaht = hexoWindow.directory;
+        }
+        rootPaht = rootPaht.replace(/\\/g, '/');
+        if (this.imgBasePath && this.imgBasePath !== rootPaht) {
+            try {
+                fs.renameSync(path.join(this.imgBasePath, this.fileName), path.join(rootPaht, this.fileName));
+            } catch (e) {
+                fs.renameSync(path.join(this.imgBasePath, this.fileName), path.join(rootPaht, this.fileName));
+            }
+            this.updateDictionary(this.imgBasePath, rootPaht)
+        }
+        this.imgBasePath = rootPaht;
+    }
+
+    relativePath(p) {
+        if (hexoWindow.useHexo)
+            return '/images/' + path.relative(this.imgBasePath, p).replace(/\\/g, '/');
+        return '/' + path.relative(this.imgBasePath, p).replace(/\\/g, '/');
+    }
+
+    resolvePath(p) {
+        if (path.isAbsolute(p))
+            p = '.' + p;
+        if (hexoWindow.useHexo) {
+            if ([0, 1].includes(p.indexOf('images/')))
+                return path.resolve(this.imgBasePath, '..', p).replace(/\\/g, '/');
+        }
+        return path.resolve(this.imgBasePath, p).replace(/\\/g, '/');
+    }
+
+    getImage(img) {
+        if (typeof img === "string") {
+            return this.getImageOfPath(img)
+        }
+        return this.getImageOfObj(img)
+    }
+
+    getImageOfPath(imgPath, md5ID) {
+        if (fs.existsSync(imgPath)) {
+            imgPath = imgPath.replace(/\\/g, '/');
+            let relativePath = '';
+            if (this.imgPathIDList[imgPath]) {
+                relativePath = this.imgPathIDList[imgPath];
+            } else {
+                relativePath = '/' + path.relative(this.imgBasePath, imgPath).replace(/\\/g, '/');
+                this.imgPathIDList[imgPath] = relativePath;
+                if (md5ID) {
+                    this.imgMD5IDList[md5ID] = imgPath;
+                }
+            }
+            return relativePath
+        }
+    }
+
+    getImageOfObj(imgObject, imgName, ext) {
+        if (!ext || ext == '.png') {
+            imgObject = imgObject.toPNG();
+        } else if (ext == '.bmp') {
+            imgObject = imgObject.toBitmap();
+        } else if (ext == '.jpg') {
+            imgObject = imgObject.toJPEG(100);
+        } else {
+            imgObject = imgObject.toPNG();
+        }
+
+        let md5ID = md5(imgObject);
+        if (this.imgMD5IDList[md5ID]) {
+            return this.imgPathIDList[this.imgMD5IDList[md5ID]]
+        }
+
+        ext = (ext && this.type[ext.toLowerCase().replace(/^\./, '')]) || '.png';
+
+        let imageName = imgName || require('moment')().format('YYYYMMDDhhmmssSSS');
+        let count = 0;
+        let imageAbsolutePath = path.join(this.imgBasePath, this.fileName, imageName + ext);
+        do {
+            if (count > 0)
+                imageAbsolutePath = path.join(this.imgBasePath, this.fileName, imageName + count + ext);
+            count += 1;
+            if (count > 50) {
+                imageAbsolutePath = path.join(this.imgBasePath, this.fileName, imageName + require('moment')().format('YYYYMMDDhhmmssSSS') + ext);
+                break;
+            }
+        } while (fs.existsSync(imageAbsolutePath));
+        mkdirsSync(path.dirname(imageAbsolutePath));
+        fs.writeFileSync(imageAbsolutePath, imgObject);
+        return this.getImageOfPath(imageAbsolutePath, md5ID)
+    }
+
+    updateImgage(imgName, newImgName) {
+        this.updateDictionary(imgName + '$', newImgName)
+    }
+
+    updateFile(fileName) {
+        this.updateDictionary('/' + this.filename + '/', '/' + fileName + '/')
+        this.filename = fileName;
+    }
+
+    updateDictionary(oldStr, newStr) {
+        if (this.imgPathIDList.hasOwnProperty()) {
+            this.imgPathIDList = JSON.parse(JSON.stringify(this.imgPathIDList).replace(new RegExp(oldStr, 'g'), newStr));
+            if (this.imgMD5IDList.hasOwnProperty())
+                this.imgMD5IDList = JSON.parse(JSON.stringify(this.imgMD5IDList).replace(new RegExp(oldStr, 'g'), newStr));
+        }
+    }
+}
+
+module.exports = ImgManager;

+ 12 - 6
views/main/hexo/hexo.js

@@ -78,8 +78,12 @@ Hexo.prototype.loadTags = function () {
     }
 }
 
-Hexo.prototype.changeConfig = function () {
-    hexo.enable = moeApp.config.get('hexo-config-enable');
+Hexo.prototype.changeConfig = function (enable) {
+    this.enable = enable;
+    let file = moeApp.config.get('hexo-config');
+    if (typeof file === 'object')
+        file = file.toString();
+    this.config.__basedir = path.dirname(file|| '');
     // var sep = path.sep;
     // this.base_dir = base + sep;
     // this.public_dir = pathFn.join(base, 'public') + sep;
@@ -90,10 +94,12 @@ Hexo.prototype.changeConfig = function () {
     // this.theme_dir = pathFn.join(base, 'themes', defaultConfig.theme) + sep;
     // this.theme_script_dir = pathFn.join(this.theme_dir, 'scripts') + sep;
 
-    // Load Hexo config
-    this.loadConfig();
-    // Load tags file
-    this.loadTags();
+    if (enable){
+        // Load Hexo config
+        this.loadConfig();
+        // Load tags file
+        this.loadTags();
+    }
 }
 
 module.exports = Hexo;

+ 13 - 12
views/main/hexo/previewer.js

@@ -5,7 +5,6 @@ const Hexo = require('./hexo');
 const __ = require('lodash');
 const Promise = require('bluebird');
 const url = require('url');
-const fs = require('fs');
 
 var rEscapeContent = /<escape(?:[^>]*)>([\s\S]*?)<\/escape>/g;
 var rSwigVar = /\{\{[\s\S]*?\}\}/g;
@@ -73,7 +72,6 @@ Previewer.prototype.render = function (content, MoeMark, options, callback) {
 
     function after_post_render() {
         hexo.execFilter('after_post_render', data, {context: hexo});
-        checkRes();
     }
 
     function checkRes() {
@@ -85,17 +83,20 @@ Previewer.prototype.render = function (content, MoeMark, options, callback) {
             let srcLocal = '';
             if (src && (url.parse(src).protocol === null)) {
                 if (!fs.existsSync(src)) {
-                    srcLocal = (imgRelativePathToID[src] ? imgRelativeToAbsolute[src] : '');
-                    if (!srcLocal && !moeApp.useHexo && hexo.config.__basedir) {
-                        srcLocal = path.join(hexo.config.__basedir, 'source', src);
+                    srcLocal = imgManager.resolvePath(src)|| '';
+                    //首先查询用户设置目录
+                    if (!srcLocal && moeApp.config.get('image-path')) {
+                        srcLocal = path.join(moeApp.config.get('image-path'), src);
                         if (!fs.existsSync(srcLocal))
                             srcLocal = '';
                     }
-                    if (!srcLocal && moeApp.config.get('image-path')) {
-                        srcLocal = path.join(moeApp.config.get('image-path'), src);
+                    //再查询Hexo资源目录
+                    if (!srcLocal && moeApp.useHexo && hexo.config.__basedir) {
+                        srcLocal = path.join(hexo.config.__basedir, 'source', src);
                         if (!fs.existsSync(srcLocal))
                             srcLocal = '';
                     }
+                    //最后查询文档所在目录
                     if (!srcLocal)
                         srcLocal = path.join(hexoWindow.directory, src);
                     img.id = src;
@@ -110,20 +111,20 @@ Previewer.prototype.render = function (content, MoeMark, options, callback) {
 
     if (moeApp.useHexo) {
         Promise.resolve()
+            .then(tryFilterHeader).catch(console.log)
+            .then(before_post_render).catch(console.log)
             .then(escapeTag).catch(console.log)
             .then(markdownContent, markdownContent).catch(console.log)
             .then(backTag).catch(console.log)
+            .then(after_post_render).catch(console.log)
+            .then(checkRes).catch(console.log)
             .then(function () {
                 callback(data.content)
             })
     } else {
         Promise.resolve()
-            .then(tryFilterHeader).catch(console.log)
-            .then(before_post_render).catch(console.log)
-            .then(escapeTag).catch(console.log)
             .then(markdownContent, markdownContent).catch(console.log)
-            .then(backTag).catch(console.log)
-            .then(after_post_render).catch(console.log)
+            .then(checkRes).catch(console.log)
             .then(function () {
                 callback(data.content)
             })

+ 3 - 3
views/main/moe-contextmenu.js

@@ -112,12 +112,12 @@ document.addEventListener('DOMContentLoaded', () => {
                     }
                 },
                 {
-                    type: moeApp.useHexo ? 'normal' : 'separator',
-                    visible: !moeApp.useHexo,
+                    type: moeApp.useHexo ?  'separator' :'normal',
+                    visible: moeApp.useHexo,
                 },
                 {
                     label: "HEXO",
-                    visible: !moeApp.useHexo,
+                    visible: moeApp.useHexo,
                     enabled: !shellServer.processRunning(),
                     click(item, hexoWindow) {
                         const shell = require('electron').shell

+ 156 - 272
views/main/moe-document.js

@@ -23,16 +23,11 @@ require('electron-titlebar');
 window.app = require('electron').remote.app;
 window.moeApp = app.moeApp;
 window.hexoWindow = moeApp.hexoWindow;
-window.imgRelativePathToID = {};
-window.imgIDToRelativePath = {};
-window.imgRelativeToAbsolute = {};
 window.clipboard = require('electron').clipboard;
-
-hexoWindow.savedContent;
+window.fs = require('fs');
+window.path = require('path');
 
 $(() => {
-    const fs = require('fs');
-    const path = require('path');
     const dialog = require('electron').remote.dialog;
     const YMAL = require('yamljs');
 
@@ -73,116 +68,6 @@ $(() => {
     });
 
     editor.focus();
-    // editor.on('keypress', ()=>{
-    //     editor.showHint();
-    // });
-
-
-    window.mkdirsSync = (dirpath, mode) => {
-        if (!fs.existsSync(dirpath)) {
-            var pathtmp;
-            dirpath.split(path.sep).forEach(function (dirname) {
-                if (pathtmp) {
-                    pathtmp = path.join(pathtmp, dirname);
-                }
-                else {
-                    pathtmp = dirname;
-                }
-                if (!fs.existsSync(pathtmp)) {
-                    if (!fs.mkdirSync(pathtmp, mode)) {
-                        return false;
-                    }
-                }
-            });
-        }
-        return true;
-    }
-
-    window.md5 = (text) => {
-        return require('crypto').createHash('md5').update(text).digest('hex');
-    };
-    window.imgRootPath = (extendPath) => {
-        let rootPaht = '';
-        if (!moeApp.useHexo && hexo.config.__basedir) {
-            rootPaht = path.join(hexo.config.__basedir, 'source', extendPath || '');
-        } else if (moeApp.config.get('image-path')) {
-            rootPaht = moeApp.config.get('image-path');
-        } else {
-            rootPaht = hexoWindow.directory;
-        }
-        return rootPaht;
-    }
-
-    function replaceImgSelection(codeMirror, title, relativePath, imageID, absolutePath) {
-        if (!relativePath) {
-            if (!moeApp.useHexo && hexo.config.__basedir)
-                relativePath = '/' + path.relative(imgRootPath(), absolutePath).replace(/\\+/g, '/')
-            else
-                relativePath = '/' + path.relative(imgRootPath(), absolutePath).replace(/\\+/g, '/')
-            if (!imageID) {
-                imageID = imgRelativePathToID[absolutePath] || md5(absolutePath);
-            }
-            imgIDToRelativePath[imageID] = relativePath;
-            imgRelativeToAbsolute[imageID] = absolutePath;
-            imgRelativePathToID[relativePath] = imageID;
-        }
-        codeMirror.replaceSelection(`![${title}](${relativePath})`);
-    }
-
-    window.pasteData = (codeMirror) => {
-        if (!codeMirror)
-            codeMirror = editor;
-        let image = clipboard.readImage();
-        if (!image.isEmpty()) {
-            image = image.toPNG();
-            let imageTitle = codeMirror.getSelection();
-            let imageID = md5(image);
-            let relativePath = imgIDToRelativePath[imageID];
-            if (relativePath) {
-                replaceImgSelection(codeMirror, imageTitle, relativePath);
-                return;
-            }
-            let rootPaht = imgRootPath('images');
-            let imageName = imageTitle || require('moment')().format('YYYYMMDDhhmmssSSS');
-
-            let count = 0;
-            let currFileName = path.basename(hexoWindow.fileName, path.extname(hexoWindow.fileName)) || hexoWindow.ID;
-            let imageAbsolutePath = path.join(rootPaht, currFileName, imageName + '.png');
-            do {
-                if (count > 0)
-                    imageAbsolutePath = path.join(rootPaht, currFileName, imageName + count + '.png');
-                count += 1;
-                if (count > 50) {
-                    imageAbsolutePath = path.join(rootPaht, currFileName, imageName + require('moment')().format('YYYYMMDDhhmmssSSS') + '.png');
-                    break;
-                }
-            } while (fs.existsSync(imageAbsolutePath));
-            mkdirsSync(path.dirname(imageAbsolutePath));
-            fs.writeFileSync(imageAbsolutePath, image);
-            replaceImgSelection(codeMirror, imageTitle, '', imageID, imageAbsolutePath);
-        } else {
-            codeMirror.replaceSelection(clipboard.readText())
-        }
-    };
-
-    var prastDataKey = (process.platform === 'darwin' ? "Cmd" : "Ctrl") + "-V";
-    editor.options.extraKeys[prastDataKey] = pasteData;
-
-    const holder = document
-    holder.ondragover = () => {
-        return false;
-    }
-    holder.ondragleave = holder.ondragend = () => {
-        return false;
-    }
-    holder.ondrop = (e) => {
-        e.stopPropagation()
-        e.preventDefault()
-        for (let f of e.dataTransfer.files) {
-            replaceImgSelection(editor, (editor.getSelection() || ''), '', '', f.path);
-        }
-        return false;
-    }
 
     const scroll = require('./moe-scroll');
     window.updatePreview = (force) => {
@@ -191,143 +76,28 @@ $(() => {
         });
     };
 
-    window.changeFileName = (force) => {
-        if (!force && hexoWindow.defName !== hexoWindow.fileName) return;
-
-        let title, fileNameNew;
-        let filename = path.basename(hexoWindow.fileName, path.extname(hexoWindow.fileName));
-        let oldName = filename;
-
-        hexoWindow.content.replace(/^---+([\w\W]+?)---+/, function () {
-            title = YMAL.parse(arguments[1]).title;
-            return '';
-        });
-
-        if (filename === title) {
-            if (force)
-                hexoWindow.isSaved = true;
-            return
-        }
-
-        try {
-            filename = title.toString().replace(/[ \\\/:\*\?"<>\|]+/g, '-');
-            let dir = path.dirname(hexoWindow.fileName);
-            let ext = path.extname(hexoWindow.fileName);
-            let count = -1;
-            do {
-                count++;
-                fileNameNew = filename + (count > 0 ? count : '');
-                fileNameNew = path.resolve(dir, fileNameNew + ext);
-                if (hexoWindow.fileName == fileNameNew || count > 50) {
-                    return;
-                }
-            } while (fs.existsSync(fileNameNew))
-
-            fs.renameSync(hexoWindow.fileName, fileNameNew);
-            hexoWindow.fileName = fileNameNew;
-
-            hexoWindow.window.setRepresentedFilename(fileNameNew);
-            document.getElementsByTagName('title')[0].innerText = 'HexoEditor - ' + path.basename(fileNameNew);
-
-            let irp = imgRootPath('images');
-            let imgFilePathOld = path.join(irp, oldName);
-            if (fs.existsSync(imgFilePathOld)) {
-                let imgFilePathNew = path.join(irp, path.basename(fileNameNew, ext));
-                fs.rename(imgFilePathOld, imgFilePathNew, err => {
-                    if (err) console.error(err);
-                    fs.stat(imgFilePathNew, (err, stats) => {
-                        if (err) console.error(err);
-                        console.log('stats: ' + JSON.stringify(stats));
-                        let relativePathOld = path.relative(imgRootPath(),imgFilePathOld).replace(/[\/\\]/g,'/');
-                        let relativePathNew = path.relative(imgRootPath(),imgFilePathNew).replace(/[\/\\]/g,'/');
-                        editor.setValue(editor.getValue().replace(new RegExp('\\]\\(/'+relativePathOld,'g'),'](/'+relativePathNew))
-                        renameFinished(relativePathOld,relativePathNew,)
-
-                        window.imgIDToRelativePath = JSON.parse(JSON.stringify(imgIDToRelativePath).replace(new RegExp(relativePathOld,'g'),relativePathNew));
-                        window.imgRelativePathToID = JSON.parse(JSON.stringify(imgRelativePathToID).replace(new RegExp(relativePathOld,'g'),relativePathNew));
-                        window.imgRelativeToAbsolute = JSON.parse(JSON.stringify(imgRelativeToAbsolute).replace(new RegExp(relativePathOld,'g'),relativePathNew));
-                    })
-                })
-            }
-
-
-            if (force) {
-                fs.writeFile(hexoWindow.fileName, hexoWindow.content, (err) => {
-                    if (err) {
-                        hexoWindow.changed = true;
-                        hexoWindow.window.setDocumentEdited(true);
-                        return;
-                    }
-                    hexoWindow.isSaved = true;
-                    hexoWindow.changed = false;
-                    hexoWindow.window.setDocumentEdited(false);
-                    app.addRecentDocument(fileNameNew);
-                    hexoWindow.savedContent = hexoWindow.content;
-                });
-            }
-        } catch (e) {
-            console.log(e);
-        }
-    }
-
-    window.autoSave = () => {
-        const option = moeApp.config.get('auto-save');
-        if (option === 'auto' && hexoWindow.content !== hexoWindow.savedContent) {
-            fs.writeFile(hexoWindow.fileName, hexoWindow.content, (err) => {
-                if (err) {
-                    hexoWindow.changed = true;
-                    hexoWindow.window.setDocumentEdited(true);
-                    return;
-                }
-                hexoWindow.isSaved = true;
-                hexoWindow.changed = false;
-                hexoWindow.window.setDocumentEdited(false);
-            });
-        }
-    }
-
-    window.throttle = (func,wait,must)=>{
-       let timeout = 0;
-       return function(){
-           var context = this,
-               args = arguments,
-               curTime = new Date();
-
-           clearTimeout(timeout);
-           // 如果达到了规定的触发时间间隔,触发 handler
-           if(curTime - startTime >= mustRun){
-               func.apply(context,args);
-               startTime = curTime;
-               // 没达到触发间隔,重新设定定时器
-           }else{
-               timeout = setTimeout(func, wait);
-           }
-       }
-    }
-
     let debounceTimeout = 0;
-    let debounceStartTime =0;
-    window.debounce = (func, wait, mustRun)=> {
+    let debounceStartTime = 0;
+    window.debounce = (func, wait, mustRun) => {
         clearTimeout(debounceTimeout);
         let curTime = new Date();
-        if(curTime - debounceStartTime >= mustRun){
+        if (curTime - debounceStartTime >= mustRun) {
             func();
             debounceTimeout = 0;
             debounceStartTime = curTime;
-        }else{
-            debounceTimeout = setTimeout(()=>{
+        } else {
+            debounceTimeout = setTimeout(() => {
                 func();
                 debounceTimeout = 0;
                 debounceStartTime = curTime
             }, wait);
         }
-};
-
+    };
 
     editor.on('change', () => {
-        debounce(()=> {
+        debounce(() => {
             window.updatePreview(false)
-        }, 150,500);
+        }, 150, 500);
     });
 
     editor.on('blur', () => {
@@ -385,7 +155,7 @@ $(() => {
     });
 
     if (moeApp.config.get('focus-mode') === true) document.getElementById('editor').classList.add('focus');
-    document.getElementById('button-bottom-focus').addEventListener('click', function() {
+    document.getElementById('button-bottom-focus').addEventListener('click', function () {
         document.getElementById('editor').classList.toggle('focus');
         moeApp.config.set('focus-mode', document.getElementById('editor').classList.contains('focus'));
     });
@@ -394,11 +164,15 @@ $(() => {
         document.getElementsByTagName('title')[0].innerText = 'HexoEditor - ' + path.basename(fileName);
     });
 
+    //加载填图功能
+    const ImgManager = require('./hexo-image');
+    window.imgManager = new ImgManager(hexo.filename)
+
+    //加载设置
     require('./moe-settings');
 
     hexoWindow.window.show();
 
-
     $("#main-container div").mousemove(function (e) {
         // $('.scrolling').removeClass('scrolling');
         if (e.clientX + 100 > this.offsetWidth + this.offsetLeft)
@@ -412,7 +186,6 @@ $(() => {
         function (e) {
         },
         function (e) {
-            // $('.scrolling').removeClass('scrolling');
             $(this).find('.CodeMirror-vscrollbar').removeClass('hoverScroll');
         }
     )
@@ -420,9 +193,7 @@ $(() => {
     document.querySelectorAll('.CodeMirror-vscrollbar').forEach(
         function (item) {
             item.addEventListener("transitionend", function (e) {
-                // if (e.propertyName == 'font-size'){
                 $('.scrolling').removeClass('scrolling');
-                // }
             });
 
             item.addEventListener("scroll", function (e) {
@@ -435,6 +206,127 @@ $(() => {
         }
     );
 
+
+
+    function replaceImgSelection(codeMirror, title, relativePath) {
+        codeMirror.replaceSelection(`![${title}](${relativePath})`);
+    }
+
+    window.pasteData = (codeMirror) => {
+        if (!codeMirror)
+            codeMirror = editor;
+        let image = clipboard.readImage();
+        if (!image.isEmpty()) {
+            let imageTitle = codeMirror.getSelection();
+            replaceImgSelection(codeMirror, imageTitle, imgManager.getImageOfObj(image,imageTitle));
+        } else {
+            codeMirror.replaceSelection(clipboard.readText())
+        }
+    };
+
+    var prastDataKey = (process.platform === 'darwin' ? "Cmd" : "Ctrl") + "-V";
+    editor.options.extraKeys[prastDataKey] = pasteData;
+
+    const holder = document
+    holder.ondragover = () => {
+        return false;
+    }
+    holder.ondragleave = holder.ondragend = () => {
+        return false;
+    }
+    holder.ondrop = (e) => {
+        e.stopPropagation()
+        e.preventDefault()
+        for (let f of e.dataTransfer.files) {
+            replaceImgSelection(editor, (editor.getSelection() || ''),imgManager.getImageOfPath(f.path));
+        }
+        return false;
+    }
+
+    window.changeFileName = (force) => {
+        if (!force && hexoWindow.defName !== hexoWindow.fileName)
+            return;
+
+        let title, nameNew;
+        let nameOld = path.basename(hexoWindow.fileName, path.extname(hexoWindow.fileName));
+
+        hexoWindow.content.replace(/^---+([\w\W]+?)---+/, function () {
+            title = YMAL.parse(arguments[1]).title;
+            return '';
+        });
+
+        try {
+            nameNew = title.toString().replace(/[ \\\/:\*\?"<>\|]+/g, '-');
+            if (nameNew !== nameOld){
+                let dir = path.dirname(hexoWindow.fileName);
+                let ext = path.extname(hexoWindow.fileName);
+                let count = -1,fileNameNew;
+                do {
+                    count++;
+                    fileNameNew = nameOld + (count > 0 ? count : '');
+                    fileNameNew = path.resolve(dir, nameNew + ext);
+                    if (count > 50) {
+                        return;
+                    }
+                } while (fs.existsSync(fileNameNew))
+
+                fs.renameSync(hexoWindow.fileName, fileNameNew);
+                hexoWindow.fileName = fileNameNew;
+                hexoWindow.changed = ture;
+                hexoWindow.window.setRepresentedFilename(fileNameNew);
+                document.getElementsByTagName('title')[0].innerText = 'HexoEditor - ' + nameNew + ext;
+
+                //修改文章中的Image链接
+                let imgPathOld = path.join(imgManager.imgBasePath,nameOld).replace(/\\/g,'/');
+                if (fs.existsSync(imgPathOld)) {
+                    let imgPathNew = path.join(imgManager.imgBasePath,nameNew).replace(/\\/g,'/');
+                    fs.rename(imgPathOld, imgPathNew, err => {
+                        if (err) console.error(err);
+                        fs.stat(imgFilePathNew, (err, stats) => {
+                            if (err) console.error(err);
+                            let relativePathOld = imgManager.relativePath(imgPathOld);
+                            let relativePathNew = imgManager.relativePath(imgPathNew);
+                            editor.setValue(editor.getValue().replace(new RegExp('\\]\\(/' + relativePathOld, 'g'), '](/' + relativePathNew))
+                            imgManager.updateFile(nameNew);
+                        })
+                    })
+                }
+            }
+            if (force) {
+                fs.writeFile(hexoWindow.fileName, hexoWindow.content, (err) => {
+                    if (err) {
+                        hexoWindow.window.setDocumentEdited(true);
+                        return;
+                    }
+                    hexoWindow.isSaved = true;
+                    hexoWindow.changed = false;
+                    hexoWindow.window.setDocumentEdited(false);
+                    hexoWindow.fileContent = hexoWindow.content;
+                    app.addRecentDocument(nameNew);
+                });
+            }
+        } catch (e) {
+            console.log(e);
+        }
+    }
+
+    window.autoSave = () => {
+        const option = moeApp.config.get('auto-save');
+        if (option === 'auto' && hexoWindow.content !== hexoWindow.fileContent) {
+            fs.writeFile(hexoWindow.fileName, hexoWindow.content, (err) => {
+                if (err) {
+                    hexoWindow.changed = true;
+                    hexoWindow.window.setDocumentEdited(true);
+                    return;
+                }
+                hexoWindow.isSaved = true;
+                hexoWindow.changed = false;
+                hexoWindow.fileContent = hexoWindow.content;
+                hexoWindow.window.setDocumentEdited(false);
+            });
+        }
+    }
+
     let renameForm = document.querySelector('#renameForm');
     window.renameImage = (relativePath) => {
         renameForm.setAttribute('path', relativePath);
@@ -442,22 +334,10 @@ $(() => {
         renameForm.querySelector('input').value = '';
         renameForm.classList.add('show');
     }
-    window.renameFinished = (oldRelative,newRelative,oldAbsolute,newAbsolute)=>{
-        if (!newAbsolute)
-            newAbsolute = path.join(imgRootPath(), newRelative).replace(/\\/g, '/');
-        let oldImgID = imgRelativePathToID[oldRelative];
-        imgIDToRelativePath[oldImgID] = newRelative;
-        imgRelativePathToID[newRelative] = oldImgID;
-        imgRelativeToAbsolute[newRelative] = newAbsolute;
-        delete imgRelativePathToID[oldRelative];
-        delete imgRelativeToAbsolute[oldRelative];
-        delete imgIDToRelativePath[oldImgID];
-    }
 
     document.querySelector('#renameForm .button-check').addEventListener("click", e => {
-        let rootPaht = window.imgRootPath();
         let relativePath = renameForm.getAttribute('path');
-        let absolutePath = path.join(rootPaht, relativePath);
+        let absolutePath = imgManager.resolvePath(relativePath);
         if (fs.existsSync(absolutePath)) {
             let newName = renameForm.querySelector('input').value;
             const ext = path.extname(relativePath);
@@ -465,7 +345,7 @@ $(() => {
                 newName += ext;
 
             const newAbsolutePath = path.join(path.dirname(absolutePath), newName).replace(/\\/g, '/');
-            const newRelativePath = path.normalize('/' + path.relative(rootPaht, newAbsolutePath)).replace(/\\/g, '/');
+            const newRelativePath = imgManager.relativePath(newAbsolutePath);
 
             if (fs.existsSync(newAbsolutePath)) {
                 window.popMessageShell(e, {
@@ -475,19 +355,23 @@ $(() => {
                 })
                 renameForm.querySelector('input').select();
             } else {
+                //重名文件名
                 fs.renameSync(absolutePath, newAbsolutePath);
-
-                renameFinished(relativePath,newRelativePath,newAbsolutePath)
-
-                let reg = new RegExp('(!\\[[^\\[\\]]*\\]\\()' + relativePath.replace(/\\/g, '\\\\') + '\\)', 'g')
-                editor.setValue(editor.getValue().replace(reg, '$1' + newRelativePath + ')'));
+                //替换原内容
+                let reg = new RegExp('(!\\[[^\\[\\]]*\\]\\()' + relativePath.replace(/\\/g, '/') + '\\)', 'g')
+                hexoWindow.content = editor.getValue().replace(reg, '$1' + newRelativePath + ')');
+                editor.setValue(hexoWindow.content);
+                hexoWindow.changed = false;
+                //更新字典
+                imgManager.updateImgage(path.basename(relativePath),newName);
+                //更新结果提示
                 renameForm.classList.remove('show');
                 window.popMessageShell(e, {
                     content: __('Operation Finished'),
                     type: 'success',
                     btnTip: 'check',
                     autoHide: true
-                })
+                });
             }
         }
     })
@@ -553,17 +437,18 @@ $(() => {
     })
 
     window.onfocus = (e) => {
-        if (hexoWindow.fileName === '') return;
+        if (hexoWindow.fileName === '' || !fs.existsSync(hexoWindow.fileName))
+            return;
         fs.readFile(hexoWindow.fileName, (err, res) => {
             if (err) {
                 hexoWindow.changed = true;
                 hexoWindow.window.setDocumentEdited(true);
                 return;
             }
-            let s = res.toString();
-            if (s !== hexoWindow.fileContent) {
-                const option = moeApp.config.get('auto-reload');
+            let content = res.toString();
+            if (content !== hexoWindow.fileContent) {
                 let flag = false;
+                const option = moeApp.config.get('auto-reload');
                 if (option === 'auto') flag = true;
                 else if (option === 'never') flag = false;
                 else {
@@ -579,18 +464,17 @@ $(() => {
                     ) === 0;
                 }
 
-                hexoWindow.fileContent = hexoWindow.content = s;
-
                 if (!flag) {
                     hexoWindow.changed = true;
                     hexoWindow.window.setDocumentEdited(true);
                     return;
                 }
+                hexoWindow.fileContent = hexoWindow.content = content;
 
                 let pos = window.editor.getCursor();
                 let editorScroll = document.querySelector('.CodeMirror-vscrollbar');
                 let scrollpos = editorScroll.scrollTop;
-                window.editor.setValue(s);
+                window.editor.setValue(content);
                 window.editor.setCursor(pos);
 
                 if (scrollpos > 0)

+ 4 - 4
views/main/moe-preview.js

@@ -28,15 +28,15 @@ let gNeedUpdate = false;
 let gUpdateRunning = false;
 
 module.exports = (cm, force, cb) => {
-    const content = cm.getValue();
-    if (hexoWindow.content === content) {
+    hexoWindow.content = cm.getValue();
+    if (hexoWindow.fileContent !== hexoWindow.content) {
         hexoWindow.changed = true;
         hexoWindow.window.setDocumentEdited(true);
     } else {
         hexoWindow.changed = false;
     }
 
-    if (gNeedUpdate || (!force && gLastContent == content))
+    if (gNeedUpdate || (!force && gLastContent == hexoWindow.content))
         return;
 
     if (gUpdateRunning ){
@@ -77,9 +77,9 @@ module.exports = (cm, force, cb) => {
 
                 gUpdateRunning = false;
                 if (gNeedUpdate){
+                    gNeedUpdate = false;
                     setTimeout(updateAsync,0);
                 }
-                gNeedUpdate = false;
                 if(typeof cb === 'function')
                     cb();
             });

+ 0 - 1
views/main/moe-rendertheme.js

@@ -26,7 +26,6 @@ const url = require('url');
 module.exports = {
     getCSS(forURL) {
         const theme = moeApp.config.get('render-theme');
-        moeApp.useHexo = ['*GitHub','*No Theme'].indexOf(theme) > -1;
 
         let res;
         if (theme.startsWith('*')) {

+ 2 - 10
views/main/moe-settings.js

@@ -19,8 +19,6 @@
 
 'use strict';
 
-const path = require('path');
-const fs = require('fs');
 const body = document.body;
 const codemirror = document.querySelector('#editor > .CodeMirror');
 
@@ -90,7 +88,7 @@ function setEditorLineHeight(val) {
 }
 
 function setImagePath(val) {
-    console.log(val)
+    imgManager.updateBase();
     window.editor.refresh();
 }
 
@@ -154,7 +152,7 @@ function setHexoAutoSetting(val) {
 
 function setHexoConfigEnable(val) {
     moeApp.useHexo = val;
-    hexo.enable = val;
+    imgManager.updateBase();
     hexo.changeConfig();
     window.updatePreview(true);
 };
@@ -187,12 +185,6 @@ 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'));
 tryRun(setScrollTogether, moeApp.config.get('scroll-Together'));