Skip to content
This repository has been archived by the owner on Sep 16, 2019. It is now read-only.

Implement "Reopen with Encoding" command in File menu #53

Merged
merged 1 commit into from
Jul 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 135 additions & 2 deletions coffee/classes/mds_main_menu.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down Expand Up @@ -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' }
Expand All @@ -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 }
)
}
]
Expand Down Expand Up @@ -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()
34 changes: 23 additions & 11 deletions coffee/classes/mds_window.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down