From cb4617f9ad7f9b3e9820125764c5524d0ecb7fc6 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 24 Jul 2016 00:41:28 +0900 Subject: [PATCH] Implement Reopen with Encoding command in File menu --- coffee/classes/mds_main_menu.coffee | 137 +++++++++++++++++++++++++++- coffee/classes/mds_window.coffee | 34 ++++--- 2 files changed, 158 insertions(+), 13 deletions(-) diff --git a/coffee/classes/mds_main_menu.coffee b/coffee/classes/mds_main_menu.coffee index 6944ff3..f16aa5a 100644 --- a/coffee/classes/mds_main_menu.coffee +++ b/coffee/classes/mds_main_menu.coffee @@ -98,6 +98,11 @@ module.exports = class MdsMainMenu label: 'Open &Recent' submenu: [{ replacement: 'fileHistory' }] } + { + label: 'Reopen with Encoding' + enabled: !!@window?.mdsWindow?.path + submenu: [{ replacement: 'encodings' }] + } { label: '&Save', enabled: @window?, accelerator: 'CmdOrCtrl+S', click: => @window.mdsWindow.trigger 'save' } { label: 'Save &As...', enabled: @window?, click: => @window.mdsWindow.trigger 'saveAs' } { type: 'separator' } @@ -181,7 +186,7 @@ module.exports = class MdsMainMenu click: (item, w) -> MdsWindow.loadFromFile( path.join(__dirname, '../../example.md'), - w?.mdsWindow, true + w?.mdsWindow, { ignoreRecent: true } ) } { type: 'separator' } @@ -190,7 +195,7 @@ module.exports = class MdsMainMenu click: (item, w) -> MdsWindow.loadFromFile( path.join(__dirname, '../../examples/gaia.md'), - w?.mdsWindow, true + w?.mdsWindow, { ignoreRecent: true } ) } ] @@ -263,4 +268,132 @@ module.exports = class MdsMainMenu } ] + encodings: do => + injectAll = (items) => + inject = (item) => + item.enabled = !!@window?.mdsWindow?.path + + if item.encoding? + item.click = => @window.mdsWindow.trigger 'reopen', { encoding: item.encoding } + + injectAll(item.submenu) if item.submenu? + item + + inject(i) for i in items + + injectAll [ + { label: 'UTF-8 (Default)', encoding: 'utf8' } + { label: 'UTF-16LE', encoding: 'utf16le' } + { label: 'UTF-16BE', encoding: 'utf16be' } + { type: 'separator' } + { + label: 'Western' + submenu: [ + { label: 'Windows 1252', encoding: 'windows1252' } + { label: 'ISO 8859-1', encoding: 'iso88591' } + { label: 'ISO 8859-3', encoding: 'iso88593' } + { label: 'ISO 8859-15', encoding: 'iso885915' } + { label: 'Mac Roman', encoding: 'macroman' } + ] + } + { + label: 'Arabic' + submenu: [ + { label: 'Windows 1256', encoding: 'windows1256' } + { label: 'ISO 8859-6', encoding: 'iso88596' } + ] + } + { + label: 'Baltic' + submenu: [ + { label: 'Windows 1257', encoding: 'windows1257' } + { label: 'ISO 8859-4', encoding: 'iso88594' } + ] + } + { + label: 'Celtic' + submenu: [{ label: 'ISO 8859-14', encoding: 'iso885914' }] + } + { + label: 'Central European' + submenu: [ + { label: 'Windows 1250', encoding: 'windows1250' } + { label: 'ISO 8859-2', encoding: 'iso88592' } + ] + } + { + label: 'Cyrillic' + submenu: [ + { label: 'Windows 1251', encoding: 'windows1251' } + { label: 'CP 866', encoding: 'cp866' } + { label: 'ISO 8859-5', encoding: 'iso88595' } + { label: 'KOI8-R', encoding: 'koi8r' } + { label: 'KOI8-U', encoding: 'koi8u' } + ] + } + { + label: 'Estonian' + submenu: [{ label: 'ISO 8859-13', encoding: 'iso885913' }] + } + { + label: 'Greek' + submenu: [ + { label: 'Windows 1253', encoding: 'windows1253' } + { label: 'ISO 8859-7', encoding: 'iso88597' } + ] + } + { + label: 'Hebrew' + submenu: [ + { label: 'Windows 1255', encoding: 'windows1255' } + { label: 'ISO 8859-8', encoding: 'iso88598' } + ] + } + { + label: 'Nordic' + submenu: [{ label: 'ISO 8859-10', encoding: 'iso885910' }] + } + { + label: 'Romanian' + submenu: [{ label: 'ISO 8859-16', encoding: 'iso885916' }] + } + { + label: 'Turkish' + submenu: [ + { label: 'Windows 1254', encoding: 'windows1254' } + { label: 'ISO 8859-9', encoding: 'iso88599' } + ] + } + { + label: 'Vietnamese' + submenu: [{ label: 'Windows 1254', encoding: 'windows1258' }] + } + { + label: 'Chinese' + submenu: [ + { label: 'GBK', encoding: 'gbk' } + { label: 'GB18030', encoding: 'gb18030' } + { type: 'separator' } + { label: 'Big5', encoding: 'cp950' } + { label: 'Big5-HKSCS', encoding: 'big5hkscs' } + ] + } + { + label: 'Japanese' + submenu: [ + { label: 'Shift JIS', encoding: 'shiftjis' } + { label: 'EUC-JP', encoding: 'eucjp' } + { label: 'CP 932', encoding: 'cp932' } + ] + } + { + label: 'Korean' + submenu: [{ label: 'EUC-KR', encoding: 'euckr' }] + } + { + label: 'Other' + submenu: [{ label: 'CP 437 (DOS)', encoding: 'cp437' }] + } + ] + @applyMenu() diff --git a/coffee/classes/mds_window.coffee b/coffee/classes/mds_window.coffee index 4489cb5..b15a626 100644 --- a/coffee/classes/mds_window.coffee +++ b/coffee/classes/mds_window.coffee @@ -109,26 +109,26 @@ module.exports = class MdsWindow @_setIsOpen true - @loadFromFile: (fname, mdsWindow, ignoreRecent = false) -> + @loadFromFile: (fname, mdsWindow, options = {}) -> fs.readFile fname, (err, txt) => return if err - {encoding} = jschardet.detect(txt) ? {} - if encoding isnt 'UTF-8' && encoding isnt 'ascii' && iconv_lite.encodingExists(encoding) - buf = iconv_lite.decode(txt, encoding) + encoding = options?.encoding || jschardet.detect(txt)?.encoding + buf = if encoding isnt 'UTF-8' and encoding isnt 'ascii' and iconv_lite.encodingExists(encoding) + iconv_lite.decode(txt, encoding) else - buf = txt.toString() + txt.toString() - unless ignoreRecent + unless options?.ignoreRecent MdsFileHistory.push fname MdsMainMenu.updateMenuToAll() - if mdsWindow? and mdsWindow.isBufferEmpty() + if mdsWindow? and (options?.override or mdsWindow.isBufferEmpty()) mdsWindow.trigger 'load', buf, fname else new MdsWindow { path: fname, buffer: buf } - loadFromFile: (fname, ignoreRecent = false) => MdsWindow.loadFromFile fname, @, ignoreRecent + loadFromFile: (fname, options = {}) => MdsWindow.loadFromFile fname, @, options trigger: (evt, args...) => @events[evt]?.apply(@, args) @@ -145,7 +145,18 @@ module.exports = class MdsWindow @trigger 'initializeState', path @send 'loadText', buffer - loadFromFile: (fname) -> @loadFromFile fname + loadFromFile: (fname, options = {}) -> @loadFromFile fname, options + + reopen: (options = {}) -> + return if @freeze or !@path + return if @changed and dialog.showMessageBox(@browserWindow, + type: 'question' + buttons: ['OK', 'Cancel'] + title: 'Marp' + message: 'Are you sure?' + detail: 'You will lose your changes on Marp. Reopen anyway?') + + @loadFromFile @path, extend({ override: true }, options) save: (triggerOnSucceeded = null) -> if @path then @send('save', @path, triggerOnSucceeded) else @trigger('saveAs', triggerOnSucceeded) @@ -182,12 +193,13 @@ module.exports = class MdsWindow initializeState: (filePath = null, changed = false) -> @path = filePath - @changed = !!changed - @refreshTitle() + @trigger 'setChangedStatus', changed dir = if filePath then "#{Path.dirname(filePath)}#{Path.sep}" else null @send 'setImageDirectory', dir + @menu.updateMenu() + setChangedStatus: (changed) -> @changed = !!changed @refreshTitle()