Browse Source

add cos base url costomize

zhuzhuyule 7 years ago
parent
commit
57d44ef5c8

+ 6 - 16
app/moe-app.js

@@ -202,14 +202,14 @@ class MoeditorApplication {
             MoeditorAction.saveAs();
         });
 
-        // shortcut.register('CmdOrCtrl + R', () => {
-        //     let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
-        //     if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'read');
-        // });
+        shortcut.register('CmdOrCtrl + R', () => {
+            let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
+            if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'read');
+        });
 
         shortcut.register('CmdOrCtrl + W', () => {
             let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
-            if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'change');
+            if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'write');
         });
 
         shortcut.register('CmdOrCtrl + P', () => {
@@ -217,18 +217,16 @@ class MoeditorApplication {
             if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'preview');
         });
 
-        shortcut.register('CmdOrCtrl + Shift + P', () => {
+        shortcut.register('Alt + C', () => {
             let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
             if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'changepreview');
         });
 
         shortcut.register('CmdOrCtrl + Alt + Shift + R', () => {
-            let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
             hexoWindow.reload();
         });
 
         shortcut.register('CmdOrCtrl + Alt + S', () => {
-            let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
             MoeditorSettings();
         });
 
@@ -236,14 +234,6 @@ class MoeditorApplication {
             let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
             hexoWindow.webContents.openDevTools();
         });
-
-
-        /*
-        shortcut.register('CmdOrCtrl + Shift + P', () => {
-            let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
-            if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'preview');
-        });
-        */
     }
 
     listenSettingChanges() {

+ 2 - 0
app/moe-l10n.js

@@ -225,6 +225,7 @@ const strings = {
         "SecretKey": "Secret Key",
         "Bucket": "Bucket",
         "BaseWeb": "Base Web",
+        "Customize": "Customize",
 
 
         "Undo": "Undo",
@@ -401,6 +402,7 @@ const strings = {
         "SecretKey": "Secret Key",
         "Bucket": "存储空间",
         "BaseWeb": "域名",
+        "Customize": "自定义",
 
         "Undo": "撤销",
         "Redo": "重做",

+ 12 - 4
app/tool/hexo-cos.js

@@ -14,7 +14,8 @@ var cosServer = (function () {
         SecretKey: '',
         Bucket: '',
         Region: '',
-        Protocol: 'http://'
+        Protocol: 'http://',
+        Customize: ''
     };
     let TaskId;
 
@@ -42,7 +43,7 @@ var cosServer = (function () {
          * @param bucket
          * @param region
          */
-        update(acessKey, secretKey, bucket, region, protocol) {
+        update(acessKey, secretKey, bucket, region, protocol,customize) {
             this.cos.options.SecretId = acessKey || config.SecretId || '';
             this.cos.options.SecretKey = secretKey || config.SecretKey || '';
             config.SecretId = acessKey || config.SecretId || '';
@@ -50,6 +51,7 @@ var cosServer = (function () {
             config.Bucket = bucket || config.Bucket || '';
             config.Region = region || config.Region || '';
             config.Protocol = protocol || config.Protocol || 'http://';
+            config.Customize = customize || '';
             log.debug('update options.')
         }
 
@@ -229,13 +231,16 @@ var cosServer = (function () {
                         result.statusCode = err.statusCode;
                         result.msg = err.error.message;
                     } else {
+                        let url =  config.Protocol + data.Location.replace(/https?:\/\//, '');
+                        if(config.Customize)
+                            url = url.replace(/^https?:\/\/[^\/]+/,config.Customize.replace(/\/$/,''));
                         result.type = 200;
                         result.statusCode = data.statusCode;
                         result.data = {
                             localname: path.basename(localFile),
                             storename: path.basename(serverFile),
                             path: serverFile,
-                            url: config.Protocol + data.Location.replace(/https?:\/\//, ''),
+                            url: url
                         }
                     }
                     cb(result)
@@ -282,13 +287,16 @@ var cosServer = (function () {
                         result.statusCode = err.statusCode;
                         result.msg = err.error.message;
                     } else {
+                        let url = config.Protocol + data.Location.replace(/https?:\/\//, '');
+                        if(config.Customize)
+                            url = url.replace(/^https?:\/\/[^\/]+/,config.Customize.replace(/\/$/,''));
                         result.type = 200;
                         result.statusCode = data.statusCode;
                         result.data = {
                             localname: path.basename(localFile),
                             storename: path.basename(serverFile),
                             path: data.Key,
-                            url: config.Protocol + data.Location
+                            url: url
                         }
                     }
                     cb(result)

+ 4 - 3
app/tool/hexo-uploadServer.js

@@ -56,7 +56,8 @@ module.exports = (function () {
                 moeApp.config.get('image-cos-secretKey'),
                 bucketObj[0],
                 bucketObj[1],
-                moeApp.config.get('image-cos-url-protocol')
+                moeApp.config.get('image-cos-url-protocol'),
+                moeApp.config.get('image-cos-customize-enable') ? moeApp.config.get('image-cos-customize') : ''
             );
         }
         return cosServer;
@@ -308,8 +309,8 @@ module.exports = (function () {
             getQiNiuServer().deleteFile(fileanme, cb)
         }
         //Cos
-        updateCos(acessKey, secretKey, bucket, region, protocol) {
-            getCOSServer().update(acessKey, secretKey, bucket, region, protocol)
+        updateCos(acessKey, secretKey, bucket, region, protocol,customize) {
+            getCOSServer().update(acessKey, secretKey, bucket, region, protocol,customize||'')
         }
 
         getCosService(cb) {

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

@@ -76,15 +76,15 @@ document.addEventListener('DOMContentLoaded', () => {
                     enabled: inEditor && (require('electron').clipboard.readText().length !== 0 ||
                         !clipboard.readImage().isEmpty()),
                     click(item, w) {
-                        pasteData();
+                        window.editor.execCommand('pasteContent')
                     }
                 },
                 {
                     label: __('Delete'),
                     enabled: inEditor && window.editor.doc.somethingSelected(),
                     click(item, w) {
-                        hexoWindow.webContents.sendInputEvent({type: 'keyDown', modifiers: [], keyCode: 'Delete'});
-                        hexoWindow.webContents.sendInputEvent({type: 'keyUp', modifiers: [], keyCode: 'Delete'});
+                        hexoWindow.window.webContents.sendInputEvent({type: 'keyDown', modifiers: [], keyCode: 'Delete'});
+                        hexoWindow.window.webContents.sendInputEvent({type: 'keyUp', modifiers: [], keyCode: 'Delete'});
                     }
                 },
                 {

+ 34 - 21
views/main/moe-settings.js

@@ -203,71 +203,82 @@ function setSourceCenter(val) {
 }
 
 function setImageWebType(val) {
+    if (val === 'smms'){
+        if (oldURL){
+            let content = editor.getValue();
+            content = content.replace(new RegExp(oldURL,'g'),'https://i.loli.net');
+            editor.setValue(content);
+            hexoWindow.changed = true;
+            hexoWindow.content = content;
+        }
+        moeApp.config.set('image-base-url', 'https://i.loli.net');
+    }
 }
 
 function SetQiNiuAccessKey(val) {
-    if(imgManager && imgManager.qiniuServer){
-        imgManager.qiniuServer.update(val)
+    if (imgManager && imgManager.uploadServer) {
+        imgManager.uploadServer.updateQiniu(val)
     }
 }
 
 function SetQiNiuSecretKey(val) {
-    if(imgManager && imgManager.qiniuServer){
-        imgManager.qiniuServer.update('',val)
+    if (imgManager && imgManager.uploadServer) {
+        imgManager.qiniuServer.update('', val)
     }
 }
 
 
 function SetQiNiuBucket(val) {
-    if(imgManager && imgManager.qiniuServer){
-        imgManager.qiniuServer.update('','',val)
+    if (imgManager && imgManager.uploadServer) {
+        imgManager.uploadServer.updateQiniu('', '', val)
     }
 }
 
 function SetQiNiuWeb(val) {
-    if (val && val.oldURL && val.newURL){
+    if (val && val.oldURL && val.newURL) {
         let content = editor.getValue();
-        content = content.replace(new RegExp(val.oldURL,'g'),val.newURL);
+        content = content.replace(new RegExp(val.oldURL, 'g'), val.newURL);
         editor.setValue(content);
         hexoWindow.changed = true;
         hexoWindow.content = content;
     }
-    if(imgManager && imgManager.qiniuServer){
-        imgManager.qiniuServer.update('','','',val.newURL)
+    if (imgManager && imgManager.uploadServer) {
+        imgManager.uploadServer.updateQiniu('', '', '', val.newURL)
     }
 }
 
 
 function SetCosAccessKey(val) {
-    if(imgManager && imgManager.cosServer){
-        imgManager.cosServer.update(val)
+    if (imgManager && imgManager.uploadServer) {
+        imgManager.uploadServer.updateCos(val)
     }
 }
 
 function SetCosSecretKey(val) {
-    if(imgManager && imgManager.cosServer){
-        imgManager.cosServer.update('',val)
+    if (imgManager && imgManager.uploadServer) {
+        imgManager.uploadServer.updateCos('', val)
     }
 }
 
 
 function SetCosBucket(val) {
-    if(imgManager && imgManager.cosServer){
-        val = (val||"|").split('|');
-        imgManager.cosServer.update('','',val[0],val[1])
+    if (imgManager && imgManager.uploadServer) {
+        val = (val || "|").split('|');
+        imgManager.uploadServer.updateCos('', '', val[0], val[1])
     }
 }
 
 function SetCosWeb(val) {
-    if (val && val.oldURL && val.newURL){
+    if (val && val.oldURL && val.newURL) {
         let content = editor.getValue();
-        content = content.replace(new RegExp(val.oldURL,'g'),val.newURL);
+        content = content.replace(new RegExp(val.oldURL, 'g'), val.newURL);
         editor.setValue(content);
         hexoWindow.changed = true;
         hexoWindow.content = content;
     }
-    if(imgManager && imgManager.cosServer){
-        imgManager.cosServer.update('','','','',moeApp.config.get('image-cos-url-protocol'))
+    if (imgManager && imgManager.uploadServer) {
+        let customize = (val && val.customize) ? val.customize : '';
+        imgManager.uploadServer.updateCos('', '', '', '', moeApp.config.get('image-cos-url-protocol'), customize)
     }
 }
 
@@ -341,5 +352,7 @@ ipcRenderer.on('setting-changed', (e, arg) => {
         tryRun(SetCosBucket, arg.val);
     } else if (arg.key === 'image-cos-url') {
         tryRun(SetCosWeb, arg.val);
+    } else if (arg.key === 'image-cos-customize') {
+        tryRun(SetCosWeb, arg.val);
     }
 });

+ 48 - 11
views/settings/moe-settings.js

@@ -526,6 +526,8 @@ document.addEventListener('DOMContentLoaded', () => {
     let imageCosBucket = imageTabContents.querySelector('#image-cos-bucket');
     let imageCosBaseWebProtocol = imageTabContents.querySelector('#image-cos-url-protocol');
     let imageCosBaseWeb = imageTabContents.querySelector('#image-cos-url');
+    let imageCosCustomizeCheck = imageTabContents.querySelector('#image-cos-customize-enable');
+    let imageCosCustomize = imageTabContents.querySelector('#image-cos-customize');
 
     imageType.addEventListener('change', function (e) {
         moeApp.config.set(imageType.id, imageType.value);
@@ -768,12 +770,12 @@ document.addEventListener('DOMContentLoaded', () => {
     })
 
     imageBaseWeb.addEventListener('change', function (e) {
-        let protocol = moeApp.config.get(imageBaseWebProtocol.id)
-        let oldURL = moeApp.config.get(imageBaseWeb.id);
+        let oldURL = moeApp.config.get('image-base-url');
         let value = {
-            oldURL: (oldURL ? protocol + oldURL : ''),
+            oldURL: (oldURL),
             newURL: (imageBaseWeb.value ? imageBaseWebProtocol.value + imageBaseWeb.value : '')
         };
+        moeApp.config.set('image-base-url', value.newURL);
         moeApp.config.set(imageBaseWebProtocol.id, imageBaseWebProtocol.value);
         moeApp.config.set(imageBaseWeb.id, imageBaseWeb.value);
         ipcRenderer.send('setting-changed', {key: imageBaseWeb.id, val: value});
@@ -876,16 +878,51 @@ document.addEventListener('DOMContentLoaded', () => {
         imageCosBaseWeb.dispatchEvent(new Event('change'));
     })
 
-    imageCosBaseWeb.addEventListener('change', function (e) {
-        let protocol = moeApp.config.get(imageCosBaseWebProtocol.id)
-        let oldURL = moeApp.config.get(imageCosBaseWeb.id);
+    imageCosBaseWeb.addEventListener('change', (e)=> {
+        if(imageCosCustomizeCheck.checked){
+            imageCosCustomize.dispatchEvent(new Event('blur'));
+        } else {
+            let oldURL = moeApp.config.get('image-base-url');
+            let value = {
+                oldURL: (oldURL),
+                newURL: (imageCosBaseWeb.value ? imageCosBaseWebProtocol.value + imageCosBaseWeb.value : '')
+            };
+            moeApp.config.set('image-base-url', value.newURL);
+            moeApp.config.set(imageCosBaseWebProtocol.id, imageCosBaseWebProtocol.value);
+            moeApp.config.set(imageCosBaseWeb.id, imageCosBaseWeb.value);
+            ipcRenderer.send('setting-changed', {key: imageCosBaseWeb.id, val: value});
+        }
+    })
+
+
+    imageCosCustomizeCheck.checked = moeApp.config.get(imageCosCustomizeCheck.id);
+    if (imageCosCustomizeCheck.checked ){
+        imageCosCustomize.removeAttribute('disabled');
+    }  else {
+        imageCosCustomize.setAttribute('disabled',true);
+    }
+    imageCosCustomizeCheck.addEventListener('click',(e)=>{
+        moeApp.config.set(imageCosCustomizeCheck.id, imageCosCustomizeCheck.checked);
+        if (imageCosCustomizeCheck.checked ){
+            imageCosCustomize.removeAttribute('disabled');
+            imageCosCustomize.dispatchEvent(new Event('blur'));
+        }  else {
+            imageCosCustomize.setAttribute('disabled',true);
+            imageCosBaseWeb.dispatchEvent(new Event('change'));
+        }
+    })
+
+    imageCosCustomize.value = moeApp.config.get(imageCosCustomize.id);
+    imageCosCustomize.addEventListener('blur',(e)=>{
+        let oldURL = moeApp.config.get('image-base-url');
         let value = {
-            oldURL: (oldURL ? protocol + oldURL : ''),
-            newURL: (imageCosBaseWeb.value ? imageCosBaseWebProtocol.value + imageCosBaseWeb.value : '')
+            oldURL: (oldURL),
+            newURL: (imageCosCustomize.value||''),
+            customize: (imageCosCustomize.value||'')
         };
-        moeApp.config.set(imageCosBaseWebProtocol.id, imageCosBaseWebProtocol.value);
-        moeApp.config.set(imageCosBaseWeb.id, imageCosBaseWeb.value);
-        ipcRenderer.send('setting-changed', {key: imageCosBaseWeb.id, val: value});
+        moeApp.config.set('image-base-url', value.newURL);
+        moeApp.config.set(imageCosCustomize.id, imageCosCustomize.value);
+        ipcRenderer.send('setting-changed', {key: imageCosCustomize.id, val: value});
     })
 
     let type = moeApp.config.get(imageType.id);

+ 8 - 1
views/settings/settings.html

@@ -361,7 +361,14 @@
                                 <td width="30%" class="l10n">BaseWeb</td>
                                 <td width="70%">
                                     <input class="settings-item-image" id="image-cos-url-protocol" readonly style="transition: width .5s;  width:53px" />
-                                    <input class="settings-item-image" id="image-cos-url" readonly style="transition: width .5s;width: calc(100% - 54px);" />
+                                    <input class="settings-item-image" id="image-cos-url" style="transition: width .5s;width: calc(100% - 54px);" />
+                                </td>
+                            </tr>
+                            <tr data-type="image-cos" style="display:none">
+                                <td width="30%" class="l10n">Customize</td>
+                                <td width="70%">
+                                    <input class="settings-item-image"  type="checkbox" id="image-cos-customize-enable" >
+                                    <input class="settings-item-image" id="image-cos-customize" style="width: calc(100% - 23px);" />
                                 </td>
                             </tr>
                         </table>