Use JavaScript standard style

This commit is contained in:
Yukai Huang 2017-03-09 15:39:42 +08:00
parent 6556c284e5
commit 9b513f619f
3 changed files with 577 additions and 550 deletions

View file

@ -1,426 +1,445 @@
import * as utils from './utils'; import * as utils from './utils'
/* config section */ /* config section */
const isMac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault; const isMac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault
const defaultEditorMode = 'gfm'; const defaultEditorMode = 'gfm'
const viewportMargin = 20; const viewportMargin = 20
const jumpToAddressBarKeymapName = isMac ? "Cmd-L" : "Ctrl-L"; const jumpToAddressBarKeymapName = isMac ? 'Cmd-L' : 'Ctrl-L'
export default class Editor { export default class Editor {
constructor() { constructor () {
this.editor = null; this.editor = null
this.defaultExtraKeys = { this.defaultExtraKeys = {
"F10": function (cm) { F10: function (cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen")); cm.setOption('fullScreen', !cm.getOption('fullScreen'))
}, },
"Esc": function (cm) { Esc: function (cm) {
if (cm.getOption('keyMap').substr(0, 3) === 'vim') return CodeMirror.Pass; if (cm.getOption('keyMap').substr(0, 3) === 'vim') {
else if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); return CodeMirror.Pass
} else if (cm.getOption('fullScreen')) {
cm.setOption('fullScreen', false)
}
}, },
"Cmd-S": function () { 'Cmd-S': function () {
return false; return false
}, },
"Ctrl-S": function () { 'Ctrl-S': function () {
return false; return false
}, },
"Enter": "newlineAndIndentContinueMarkdownList", Enter: 'newlineAndIndentContinueMarkdownList',
"Tab": function (cm) { Tab: function (cm) {
var tab = '\t'; var tab = '\t'
// contruct x length spaces // contruct x length spaces
var spaces = Array(parseInt(cm.getOption("indentUnit")) + 1).join(" "); var spaces = Array(parseInt(cm.getOption('indentUnit')) + 1).join(' ')
//auto indent whole line when in list or blockquote // auto indent whole line when in list or blockquote
var cursor = cm.getCursor(); var cursor = cm.getCursor()
var line = cm.getLine(cursor.line); var line = cm.getLine(cursor.line)
// this regex match the following patterns // this regex match the following patterns
// 1. blockquote starts with "> " or ">>" // 1. blockquote starts with "> " or ">>"
// 2. unorder list starts with *+- // 2. unorder list starts with *+-
// 3. order list starts with "1." or "1)" // 3. order list starts with "1." or "1)"
var regex = /^(\s*)(>[> ]*|[*+-]\s|(\d+)([.)]))/; var regex = /^(\s*)(>[> ]*|[*+-]\s|(\d+)([.)]))/
var match; var match
var multiple = cm.getSelection().split('\n').length > 1 || cm.getSelections().length > 1; var multiple = cm.getSelection().split('\n').length > 1 ||
cm.getSelections().length > 1
if (multiple) { if (multiple) {
cm.execCommand('defaultTab'); cm.execCommand('defaultTab')
} else if ((match = regex.exec(line)) !== null) { } else if ((match = regex.exec(line)) !== null) {
var ch = match[1].length; var ch = match[1].length
var pos = { var pos = {
line: cursor.line, line: cursor.line,
ch: ch ch: ch
}; }
if (cm.getOption('indentWithTabs')) if (cm.getOption('indentWithTabs')) {
cm.replaceRange(tab, pos, pos, '+input'); cm.replaceRange(tab, pos, pos, '+input')
else
cm.replaceRange(spaces, pos, pos, '+input');
} else { } else {
if (cm.getOption('indentWithTabs')) cm.replaceRange(spaces, pos, pos, '+input')
cm.execCommand('defaultTab'); }
else { } else {
cm.replaceSelection(spaces); if (cm.getOption('indentWithTabs')) {
cm.execCommand('defaultTab')
} else {
cm.replaceSelection(spaces)
} }
} }
}, },
"Cmd-Left": "goLineLeftSmart", 'Cmd-Left': 'goLineLeftSmart',
"Cmd-Right": "goLineRight", 'Cmd-Right': 'goLineRight',
"Ctrl-C": function (cm) { 'Ctrl-C': function (cm) {
if (!isMac && cm.getOption('keyMap').substr(0, 3) === 'vim') { if (!isMac && cm.getOption('keyMap').substr(0, 3) === 'vim') {
document.execCommand("copy"); document.execCommand('copy')
} else { } else {
return CodeMirror.Pass; return CodeMirror.Pass
} }
}, },
"Ctrl-*": (cm) => { 'Ctrl-*': cm => {
utils.wrapTextWith(this.editor, cm, '*'); utils.wrapTextWith(this.editor, cm, '*')
}, },
"Shift-Ctrl-8": (cm) => { 'Shift-Ctrl-8': cm => {
utils.wrapTextWith(this.editor, cm, '*'); utils.wrapTextWith(this.editor, cm, '*')
}, },
"Ctrl-_": (cm) => { 'Ctrl-_': cm => {
utils.wrapTextWith(this.editor, cm, '_'); utils.wrapTextWith(this.editor, cm, '_')
}, },
"Shift-Ctrl--": (cm) => { 'Shift-Ctrl--': cm => {
utils.wrapTextWith(this.editor, cm, '_'); utils.wrapTextWith(this.editor, cm, '_')
}, },
"Ctrl-~": (cm) => { 'Ctrl-~': cm => {
utils.wrapTextWith(this.editor, cm, '~'); utils.wrapTextWith(this.editor, cm, '~')
}, },
"Shift-Ctrl-`": (cm) => { 'Shift-Ctrl-`': cm => {
utils.wrapTextWith(this.editor, cm, '~'); utils.wrapTextWith(this.editor, cm, '~')
}, },
"Ctrl-^": (cm) => { 'Ctrl-^': cm => {
utils.wrapTextWith(this.editor, cm, '^'); utils.wrapTextWith(this.editor, cm, '^')
}, },
"Shift-Ctrl-6": (cm) => { 'Shift-Ctrl-6': cm => {
utils.wrapTextWith(this.editor, cm, '^'); utils.wrapTextWith(this.editor, cm, '^')
}, },
"Ctrl-+": (cm) => { 'Ctrl-+': cm => {
utils.wrapTextWith(this.editor, cm, '+'); utils.wrapTextWith(this.editor, cm, '+')
}, },
"Shift-Ctrl-=": (cm) => { 'Shift-Ctrl-=': cm => {
utils.wrapTextWith(this.editor, cm, '+'); utils.wrapTextWith(this.editor, cm, '+')
}, },
"Ctrl-=": (cm) => { 'Ctrl-=': cm => {
utils.wrapTextWith(this.editor, cm, '='); utils.wrapTextWith(this.editor, cm, '=')
}, },
"Shift-Ctrl-Backspace": (cm) => { 'Shift-Ctrl-Backspace': cm => {
utils.wrapTextWith(this.editor, cm, 'Backspace'); utils.wrapTextWith(this.editor, cm, 'Backspace')
} }
};
this.jumpToAddressBarKeymapValue = null;
} }
getStatusBarTemplate(callback) { this.jumpToAddressBarKeymapValue = null
$.get(window.serverurl + '/views/statusbar.html', (template) => {
this.statusBarTemplate = template;
if (callback) callback();
});
} }
addStatusBar() { getStatusBarTemplate (callback) {
$.get(window.serverurl + '/views/statusbar.html', template => {
this.statusBarTemplate = template
if (callback) callback()
})
}
addStatusBar () {
if (!this.statusBarTemplate) { if (!this.statusBarTemplate) {
this.getStatusBarTemplate(this.addStatusBar); this.getStatusBarTemplate(this.addStatusBar)
return; return
} }
this.statusBar = $(this.statusBarTemplate); this.statusBar = $(this.statusBarTemplate)
this.statusCursor = this.statusBar.find('.status-cursor'); this.statusCursor = this.statusBar.find('.status-cursor')
this.statusFile = this.statusBar.find('.status-file'); this.statusFile = this.statusBar.find('.status-file')
this.statusIndicators = this.statusBar.find('.status-indicators'); this.statusIndicators = this.statusBar.find('.status-indicators')
this.statusIndent = this.statusBar.find('.status-indent'); this.statusIndent = this.statusBar.find('.status-indent')
this.statusKeymap = this.statusBar.find('.status-keymap'); this.statusKeymap = this.statusBar.find('.status-keymap')
this.statusLength = this.statusBar.find('.status-length'); this.statusLength = this.statusBar.find('.status-length')
this.statusTheme = this.statusBar.find('.status-theme'); this.statusTheme = this.statusBar.find('.status-theme')
this.statusSpellcheck = this.statusBar.find('.status-spellcheck'); this.statusSpellcheck = this.statusBar.find('.status-spellcheck')
this.statusPreferences = this.statusBar.find('.status-preferences'); this.statusPreferences = this.statusBar.find('.status-preferences')
this.statusPanel = this.editor.addPanel(this.statusBar[0], { this.statusPanel = this.editor.addPanel(this.statusBar[0], {
position: "bottom" position: 'bottom'
}); })
this.setIndent(); this.setIndent()
this.setKeymap(); this.setKeymap()
this.setTheme(); this.setTheme()
this.setSpellcheck(); this.setSpellcheck()
this.setPreferences(); this.setPreferences()
} }
setIndent() { setIndent () {
var cookieIndentType = Cookies.get('indent_type'); var cookieIndentType = Cookies.get('indent_type')
var cookieTabSize = parseInt(Cookies.get('tab_size')); var cookieTabSize = parseInt(Cookies.get('tab_size'))
var cookieSpaceUnits = parseInt(Cookies.get('space_units')); var cookieSpaceUnits = parseInt(Cookies.get('space_units'))
if (cookieIndentType) { if (cookieIndentType) {
if (cookieIndentType == 'tab') { if (cookieIndentType === 'tab') {
this.editor.setOption('indentWithTabs', true); this.editor.setOption('indentWithTabs', true)
if (cookieTabSize) if (cookieTabSize) {
this.editor.setOption('indentUnit', cookieTabSize); this.editor.setOption('indentUnit', cookieTabSize)
} else if (cookieIndentType == 'space') { }
this.editor.setOption('indentWithTabs', false); } else if (cookieIndentType === 'space') {
if (cookieSpaceUnits) this.editor.setOption('indentWithTabs', false)
this.editor.setOption('indentUnit', cookieSpaceUnits); if (cookieSpaceUnits) {
this.editor.setOption('indentUnit', cookieSpaceUnits)
} }
} }
if (cookieTabSize) }
this.editor.setOption('tabSize', cookieTabSize); if (cookieTabSize) {
this.editor.setOption('tabSize', cookieTabSize)
}
var type = this.statusIndicators.find('.indent-type'); var type = this.statusIndicators.find('.indent-type')
var widthLabel = this.statusIndicators.find('.indent-width-label'); var widthLabel = this.statusIndicators.find('.indent-width-label')
var widthInput = this.statusIndicators.find('.indent-width-input'); var widthInput = this.statusIndicators.find('.indent-width-input')
const setType = () => { const setType = () => {
if (this.editor.getOption('indentWithTabs')) { if (this.editor.getOption('indentWithTabs')) {
Cookies.set('indent_type', 'tab', { Cookies.set('indent_type', 'tab', {
expires: 365 expires: 365
}); })
type.text('Tab Size:'); type.text('Tab Size:')
} else { } else {
Cookies.set('indent_type', 'space', { Cookies.set('indent_type', 'space', {
expires: 365 expires: 365
}); })
type.text('Spaces:'); type.text('Spaces:')
} }
} }
setType(); setType()
const setUnit = () => { const setUnit = () => {
var unit = this.editor.getOption('indentUnit'); var unit = this.editor.getOption('indentUnit')
if (this.editor.getOption('indentWithTabs')) { if (this.editor.getOption('indentWithTabs')) {
Cookies.set('tab_size', unit, { Cookies.set('tab_size', unit, {
expires: 365 expires: 365
}); })
} else { } else {
Cookies.set('space_units', unit, { Cookies.set('space_units', unit, {
expires: 365 expires: 365
}); })
} }
widthLabel.text(unit); widthLabel.text(unit)
} }
setUnit(); setUnit()
type.click(() => { type.click(() => {
if (this.editor.getOption('indentWithTabs')) { if (this.editor.getOption('indentWithTabs')) {
this.editor.setOption('indentWithTabs', false); this.editor.setOption('indentWithTabs', false)
cookieSpaceUnits = parseInt(Cookies.get('space_units')); cookieSpaceUnits = parseInt(Cookies.get('space_units'))
if (cookieSpaceUnits) if (cookieSpaceUnits) {
this.editor.setOption('indentUnit', cookieSpaceUnits) this.editor.setOption('indentUnit', cookieSpaceUnits)
}
} else { } else {
this.editor.setOption('indentWithTabs', true); this.editor.setOption('indentWithTabs', true)
cookieTabSize = parseInt(Cookies.get('tab_size')); cookieTabSize = parseInt(Cookies.get('tab_size'))
if (cookieTabSize) { if (cookieTabSize) {
this.editor.setOption('indentUnit', cookieTabSize); this.editor.setOption('indentUnit', cookieTabSize)
this.editor.setOption('tabSize', cookieTabSize); this.editor.setOption('tabSize', cookieTabSize)
} }
} }
setType(); setType()
setUnit(); setUnit()
}); })
widthLabel.click(() => { widthLabel.click(() => {
if (widthLabel.is(':visible')) { if (widthLabel.is(':visible')) {
widthLabel.addClass('hidden'); widthLabel.addClass('hidden')
widthInput.removeClass('hidden'); widthInput.removeClass('hidden')
widthInput.val(this.editor.getOption('indentUnit')); widthInput.val(this.editor.getOption('indentUnit'))
widthInput.select(); widthInput.select()
} else { } else {
widthLabel.removeClass('hidden'); widthLabel.removeClass('hidden')
widthInput.addClass('hidden'); widthInput.addClass('hidden')
} }
}); })
widthInput.on('change', () => { widthInput.on('change', () => {
var val = parseInt(widthInput.val()); var val = parseInt(widthInput.val())
if (!val) val = this.editor.getOption('indentUnit'); if (!val) val = this.editor.getOption('indentUnit')
if (val < 1) val = 1; if (val < 1) val = 1
else if (val > 10) val = 10; else if (val > 10) val = 10
if (this.editor.getOption('indentWithTabs')) { if (this.editor.getOption('indentWithTabs')) {
this.editor.setOption('tabSize', val); this.editor.setOption('tabSize', val)
} }
this.editor.setOption('indentUnit', val); this.editor.setOption('indentUnit', val)
setUnit(); setUnit()
}); })
widthInput.on('blur', function () { widthInput.on('blur', function () {
widthLabel.removeClass('hidden'); widthLabel.removeClass('hidden')
widthInput.addClass('hidden'); widthInput.addClass('hidden')
}); })
} }
setKeymap() { setKeymap () {
var cookieKeymap = Cookies.get('keymap'); var cookieKeymap = Cookies.get('keymap')
if (cookieKeymap) if (cookieKeymap) {
this.editor.setOption('keyMap', cookieKeymap); this.editor.setOption('keyMap', cookieKeymap)
}
var label = this.statusIndicators.find('.ui-keymap-label'); var label = this.statusIndicators.find('.ui-keymap-label')
var sublime = this.statusIndicators.find('.ui-keymap-sublime'); var sublime = this.statusIndicators.find('.ui-keymap-sublime')
var emacs = this.statusIndicators.find('.ui-keymap-emacs'); var emacs = this.statusIndicators.find('.ui-keymap-emacs')
var vim = this.statusIndicators.find('.ui-keymap-vim'); var vim = this.statusIndicators.find('.ui-keymap-vim')
const setKeymapLabel = () => { const setKeymapLabel = () => {
var keymap = this.editor.getOption('keyMap'); var keymap = this.editor.getOption('keyMap')
Cookies.set('keymap', keymap, { Cookies.set('keymap', keymap, {
expires: 365 expires: 365
}); })
label.text(keymap); label.text(keymap)
this.restoreOverrideEditorKeymap(); this.restoreOverrideEditorKeymap()
this.setOverrideBrowserKeymap(); this.setOverrideBrowserKeymap()
} }
setKeymapLabel(); setKeymapLabel()
sublime.click(() => { sublime.click(() => {
this.editor.setOption('keyMap', 'sublime'); this.editor.setOption('keyMap', 'sublime')
setKeymapLabel(); setKeymapLabel()
}); })
emacs.click(() => { emacs.click(() => {
this.editor.setOption('keyMap', 'emacs'); this.editor.setOption('keyMap', 'emacs')
setKeymapLabel(); setKeymapLabel()
}); })
vim.click(() => { vim.click(() => {
this.editor.setOption('keyMap', 'vim'); this.editor.setOption('keyMap', 'vim')
setKeymapLabel(); setKeymapLabel()
}); })
} }
setTheme() { setTheme () {
var cookieTheme = Cookies.get('theme'); var cookieTheme = Cookies.get('theme')
if (cookieTheme) { if (cookieTheme) {
this.editor.setOption('theme', cookieTheme); this.editor.setOption('theme', cookieTheme)
} }
var themeToggle = this.statusTheme.find('.ui-theme-toggle'); var themeToggle = this.statusTheme.find('.ui-theme-toggle')
const checkTheme = () => { const checkTheme = () => {
var theme = this.editor.getOption('theme'); var theme = this.editor.getOption('theme')
if (theme == "one-dark") { if (theme === 'one-dark') {
themeToggle.removeClass('active'); themeToggle.removeClass('active')
} else { } else {
themeToggle.addClass('active'); themeToggle.addClass('active')
} }
} }
themeToggle.click(() => { themeToggle.click(() => {
var theme = this.editor.getOption('theme'); var theme = this.editor.getOption('theme')
if (theme == "one-dark") { if (theme === 'one-dark') {
theme = "default"; theme = 'default'
} else { } else {
theme = "one-dark"; theme = 'one-dark'
} }
this.editor.setOption('theme', theme); this.editor.setOption('theme', theme)
Cookies.set('theme', theme, { Cookies.set('theme', theme, {
expires: 365 expires: 365
}); })
checkTheme(); checkTheme()
}); })
checkTheme(); checkTheme()
} }
setSpellcheck() { setSpellcheck () {
var cookieSpellcheck = Cookies.get('spellcheck'); var cookieSpellcheck = Cookies.get('spellcheck')
if (cookieSpellcheck) { if (cookieSpellcheck) {
var mode = null; var mode = null
if (cookieSpellcheck === 'true' || cookieSpellcheck === true) { if (cookieSpellcheck === 'true' || cookieSpellcheck === true) {
mode = 'spell-checker'; mode = 'spell-checker'
} else { } else {
mode = defaultEditorMode; mode = defaultEditorMode
} }
if (mode && mode !== this.editor.getOption('mode')) { if (mode && mode !== this.editor.getOption('mode')) {
this.editor.setOption('mode', mode); this.editor.setOption('mode', mode)
} }
} }
var spellcheckToggle = this.statusSpellcheck.find('.ui-spellcheck-toggle'); var spellcheckToggle = this.statusSpellcheck.find('.ui-spellcheck-toggle')
const checkSpellcheck = () => { const checkSpellcheck = () => {
var mode = this.editor.getOption('mode'); var mode = this.editor.getOption('mode')
if (mode == defaultEditorMode) { if (mode === defaultEditorMode) {
spellcheckToggle.removeClass('active'); spellcheckToggle.removeClass('active')
} else { } else {
spellcheckToggle.addClass('active'); spellcheckToggle.addClass('active')
} }
} }
spellcheckToggle.click(() => { spellcheckToggle.click(() => {
var mode = this.editor.getOption('mode'); var mode = this.editor.getOption('mode')
if (mode == defaultEditorMode) { if (mode === defaultEditorMode) {
mode = "spell-checker"; mode = 'spell-checker'
} else { } else {
mode = defaultEditorMode; mode = defaultEditorMode
} }
if (mode && mode !== this.editor.getOption('mode')) { if (mode && mode !== this.editor.getOption('mode')) {
this.editor.setOption('mode', mode); this.editor.setOption('mode', mode)
} }
Cookies.set('spellcheck', (mode == "spell-checker"), { Cookies.set('spellcheck', mode === 'spell-checker', {
expires: 365 expires: 365
}); })
checkSpellcheck(); checkSpellcheck()
}); })
checkSpellcheck(); checkSpellcheck()
//workaround spellcheck might not activate beacuse the ajax loading // workaround spellcheck might not activate beacuse the ajax loading
if (window.num_loaded < 2) { if (window.num_loaded < 2) {
var spellcheckTimer = setInterval(() => { var spellcheckTimer = setInterval(
() => {
if (window.num_loaded >= 2) { if (window.num_loaded >= 2) {
if (this.editor.getOption('mode') == "spell-checker") { if (this.editor.getOption('mode') === 'spell-checker') {
this.editor.setOption('mode', "spell-checker"); this.editor.setOption('mode', 'spell-checker')
} }
clearInterval(spellcheckTimer); clearInterval(spellcheckTimer)
} }
}, 100); },
100,
)
} }
} }
resetEditorKeymapToBrowserKeymap() { resetEditorKeymapToBrowserKeymap () {
var keymap = this.editor.getOption('keyMap'); var keymap = this.editor.getOption('keyMap')
if (!this.jumpToAddressBarKeymapValue) { if (!this.jumpToAddressBarKeymapValue) {
this.jumpToAddressBarKeymapValue = CodeMirror.keyMap[keymap][jumpToAddressBarKeymapName]; this.jumpToAddressBarKeymapValue = CodeMirror.keyMap[keymap][jumpToAddressBarKeymapName]
delete CodeMirror.keyMap[keymap][jumpToAddressBarKeymapName]; delete CodeMirror.keyMap[keymap][jumpToAddressBarKeymapName]
} }
} }
restoreOverrideEditorKeymap() { restoreOverrideEditorKeymap () {
var keymap = this.editor.getOption('keyMap'); var keymap = this.editor.getOption('keyMap')
if (this.jumpToAddressBarKeymapValue) { if (this.jumpToAddressBarKeymapValue) {
CodeMirror.keyMap[keymap][jumpToAddressBarKeymapName] = this.jumpToAddressBarKeymapValue; CodeMirror.keyMap[keymap][jumpToAddressBarKeymapName] = this.jumpToAddressBarKeymapValue
this.jumpToAddressBarKeymapValue = null; this.jumpToAddressBarKeymapValue = null
} }
} }
setOverrideBrowserKeymap() { setOverrideBrowserKeymap () {
var overrideBrowserKeymap = $('.ui-preferences-override-browser-keymap label > input[type="checkbox"]'); var overrideBrowserKeymap = $(
if (overrideBrowserKeymap.is(":checked")) { '.ui-preferences-override-browser-keymap label > input[type="checkbox"]',
)
if (overrideBrowserKeymap.is(':checked')) {
Cookies.set('preferences-override-browser-keymap', true, { Cookies.set('preferences-override-browser-keymap', true, {
expires: 365 expires: 365
}); })
this.restoreOverrideEditorKeymap(); this.restoreOverrideEditorKeymap()
} else { } else {
Cookies.remove('preferences-override-browser-keymap'); Cookies.remove('preferences-override-browser-keymap')
this.resetEditorKeymapToBrowserKeymap(); this.resetEditorKeymapToBrowserKeymap()
} }
} }
setPreferences() { setPreferences () {
var overrideBrowserKeymap = $('.ui-preferences-override-browser-keymap label > input[type="checkbox"]'); var overrideBrowserKeymap = $(
var cookieOverrideBrowserKeymap = Cookies.get('preferences-override-browser-keymap'); '.ui-preferences-override-browser-keymap label > input[type="checkbox"]',
if (cookieOverrideBrowserKeymap && cookieOverrideBrowserKeymap === "true") { )
overrideBrowserKeymap.prop('checked', true); var cookieOverrideBrowserKeymap = Cookies.get(
'preferences-override-browser-keymap',
)
if (cookieOverrideBrowserKeymap && cookieOverrideBrowserKeymap === 'true') {
overrideBrowserKeymap.prop('checked', true)
} else { } else {
overrideBrowserKeymap.prop('checked', false); overrideBrowserKeymap.prop('checked', false)
} }
this.setOverrideBrowserKeymap(); this.setOverrideBrowserKeymap()
overrideBrowserKeymap.change(() => { overrideBrowserKeymap.change(() => {
this.setOverrideBrowserKeymap(); this.setOverrideBrowserKeymap()
}); })
} }
init(textit) { init (textit) {
this.editor = CodeMirror.fromTextArea(textit, { this.editor = CodeMirror.fromTextArea(textit, {
mode: defaultEditorMode, mode: defaultEditorMode,
backdrop: defaultEditorMode, backdrop: defaultEditorMode,
keyMap: "sublime", keyMap: 'sublime',
viewportMargin: viewportMargin, viewportMargin: viewportMargin,
styleActiveLine: true, styleActiveLine: true,
lineNumbers: true, lineNumbers: true,
@ -428,9 +447,9 @@ export default class Editor {
showCursorWhenSelecting: true, showCursorWhenSelecting: true,
highlightSelectionMatches: true, highlightSelectionMatches: true,
indentUnit: 4, indentUnit: 4,
continueComments: "Enter", continueComments: 'Enter',
theme: "one-dark", theme: 'one-dark',
inputStyle: "textarea", inputStyle: 'textarea',
matchBrackets: true, matchBrackets: true,
autoCloseBrackets: true, autoCloseBrackets: true,
matchTags: { matchTags: {
@ -438,7 +457,11 @@ export default class Editor {
}, },
autoCloseTags: true, autoCloseTags: true,
foldGutter: true, foldGutter: true,
gutters: ["CodeMirror-linenumbers", "authorship-gutters", "CodeMirror-foldgutter"], gutters: [
'CodeMirror-linenumbers',
'authorship-gutters',
'CodeMirror-foldgutter'
],
extraKeys: this.defaultExtraKeys, extraKeys: this.defaultExtraKeys,
flattenSpans: true, flattenSpans: true,
addModeClass: true, addModeClass: true,
@ -446,14 +469,14 @@ export default class Editor {
autoRefresh: true, autoRefresh: true,
otherCursors: true, otherCursors: true,
placeholder: "← Start by entering a title here\n===\nVisit /features if you don't know what to do.\nHappy hacking :)" placeholder: "← Start by entering a title here\n===\nVisit /features if you don't know what to do.\nHappy hacking :)"
}); })
this.getStatusBarTemplate(); this.getStatusBarTemplate()
return this.editor; return this.editor
} }
getEditor() { getEditor () {
return this.editor; return this.editor
} }
} }

View file

@ -3,57 +3,57 @@
*/ */
export const getUIElements = () => ({ export const getUIElements = () => ({
spinner: $(".ui-spinner"), spinner: $('.ui-spinner'),
content: $(".ui-content"), content: $('.ui-content'),
toolbar: { toolbar: {
shortStatus: $(".ui-short-status"), shortStatus: $('.ui-short-status'),
status: $(".ui-status"), status: $('.ui-status'),
new: $(".ui-new"), new: $('.ui-new'),
publish: $(".ui-publish"), publish: $('.ui-publish'),
extra: { extra: {
revision: $(".ui-extra-revision"), revision: $('.ui-extra-revision'),
slide: $(".ui-extra-slide") slide: $('.ui-extra-slide')
}, },
download: { download: {
markdown: $(".ui-download-markdown"), markdown: $('.ui-download-markdown'),
html: $(".ui-download-html"), html: $('.ui-download-html'),
rawhtml: $(".ui-download-raw-html"), rawhtml: $('.ui-download-raw-html'),
pdf: $(".ui-download-pdf-beta"), pdf: $('.ui-download-pdf-beta')
}, },
export: { export: {
dropbox: $(".ui-save-dropbox"), dropbox: $('.ui-save-dropbox'),
googleDrive: $(".ui-save-google-drive"), googleDrive: $('.ui-save-google-drive'),
gist: $(".ui-save-gist"), gist: $('.ui-save-gist'),
snippet: $(".ui-save-snippet") snippet: $('.ui-save-snippet')
}, },
import: { import: {
dropbox: $(".ui-import-dropbox"), dropbox: $('.ui-import-dropbox'),
googleDrive: $(".ui-import-google-drive"), googleDrive: $('.ui-import-google-drive'),
gist: $(".ui-import-gist"), gist: $('.ui-import-gist'),
snippet: $(".ui-import-snippet"), snippet: $('.ui-import-snippet'),
clipboard: $(".ui-import-clipboard") clipboard: $('.ui-import-clipboard')
}, },
mode: $(".ui-mode"), mode: $('.ui-mode'),
edit: $(".ui-edit"), edit: $('.ui-edit'),
view: $(".ui-view"), view: $('.ui-view'),
both: $(".ui-both"), both: $('.ui-both'),
uploadImage: $(".ui-upload-image") uploadImage: $('.ui-upload-image')
}, },
infobar: { infobar: {
lastchange: $(".ui-lastchange"), lastchange: $('.ui-lastchange'),
lastchangeuser: $(".ui-lastchangeuser"), lastchangeuser: $('.ui-lastchangeuser'),
nolastchangeuser: $(".ui-no-lastchangeuser"), nolastchangeuser: $('.ui-no-lastchangeuser'),
permission: { permission: {
permission: $(".ui-permission"), permission: $('.ui-permission'),
label: $(".ui-permission-label"), label: $('.ui-permission-label'),
freely: $(".ui-permission-freely"), freely: $('.ui-permission-freely'),
editable: $(".ui-permission-editable"), editable: $('.ui-permission-editable'),
locked: $(".ui-permission-locked"), locked: $('.ui-permission-locked'),
private: $(".ui-permission-private"), private: $('.ui-permission-private'),
limited: $(".ui-permission-limited"), limited: $('.ui-permission-limited'),
protected: $(".ui-permission-protected") protected: $('.ui-permission-protected')
}, },
delete: $(".ui-delete-note") delete: $('.ui-delete-note')
}, },
toc: { toc: {
toc: $('.ui-toc'), toc: $('.ui-toc'),
@ -62,23 +62,25 @@ export const getUIElements = () => ({
dropdown: $('.ui-toc-dropdown') dropdown: $('.ui-toc-dropdown')
}, },
area: { area: {
edit: $(".ui-edit-area"), edit: $('.ui-edit-area'),
view: $(".ui-view-area"), view: $('.ui-view-area'),
codemirror: $(".ui-edit-area .CodeMirror"), codemirror: $('.ui-edit-area .CodeMirror'),
codemirrorScroll: $(".ui-edit-area .CodeMirror .CodeMirror-scroll"), codemirrorScroll: $('.ui-edit-area .CodeMirror .CodeMirror-scroll'),
codemirrorSizer: $(".ui-edit-area .CodeMirror .CodeMirror-sizer"), codemirrorSizer: $('.ui-edit-area .CodeMirror .CodeMirror-sizer'),
codemirrorSizerInner: $(".ui-edit-area .CodeMirror .CodeMirror-sizer > div"), codemirrorSizerInner: $(
markdown: $(".ui-view-area .markdown-body"), '.ui-edit-area .CodeMirror .CodeMirror-sizer > div',
),
markdown: $('.ui-view-area .markdown-body'),
resize: { resize: {
handle: $('.ui-resizable-handle'), handle: $('.ui-resizable-handle'),
syncToggle: $('.ui-sync-toggle') syncToggle: $('.ui-sync-toggle')
} }
}, },
modal: { modal: {
snippetImportProjects: $("#snippetImportModalProjects"), snippetImportProjects: $('#snippetImportModalProjects'),
snippetImportSnippets: $("#snippetImportModalSnippets"), snippetImportSnippets: $('#snippetImportModalSnippets'),
revision: $("#revisionModal") revision: $('#revisionModal')
} }
}) })
export default getUIElements; export default getUIElements

View file

@ -1,43 +1,45 @@
const wrapSymbols = ['*', '_', '~', '^', '+', '=']; const wrapSymbols = ['*', '_', '~', '^', '+', '=']
export function wrapTextWith(editor, cm, symbol) { export function wrapTextWith (editor, cm, symbol) {
if (!cm.getSelection()) { if (!cm.getSelection()) {
return CodeMirror.Pass; return CodeMirror.Pass
} else { } else {
var ranges = cm.listSelections(); var ranges = cm.listSelections()
for (var i = 0; i < ranges.length; i++) { for (var i = 0; i < ranges.length; i++) {
var range = ranges[i]; var range = ranges[i]
if (!range.empty()) { if (!range.empty()) {
var from = range.from(), to = range.to(); const from = range.from()
const to = range.to()
if (symbol !== 'Backspace') { if (symbol !== 'Backspace') {
cm.replaceRange(symbol, to, to, '+input'); cm.replaceRange(symbol, to, to, '+input')
cm.replaceRange(symbol, from, from, '+input'); cm.replaceRange(symbol, from, from, '+input')
// workaround selection range not correct after add symbol // workaround selection range not correct after add symbol
var _ranges = cm.listSelections(); var _ranges = cm.listSelections()
var anchorIndex = editor.indexFromPos(_ranges[i].anchor); var anchorIndex = editor.indexFromPos(_ranges[i].anchor)
var headIndex = editor.indexFromPos(_ranges[i].head); var headIndex = editor.indexFromPos(_ranges[i].head)
if (anchorIndex > headIndex) { if (anchorIndex > headIndex) {
_ranges[i].anchor.ch--; _ranges[i].anchor.ch--
} else { } else {
_ranges[i].head.ch--; _ranges[i].head.ch--
} }
cm.setSelections(_ranges); cm.setSelections(_ranges)
} else { } else {
var preEndPos = { var preEndPos = {
line: to.line, line: to.line,
ch: to.ch + 1 ch: to.ch + 1
}; }
var preText = cm.getRange(to, preEndPos); var preText = cm.getRange(to, preEndPos)
var preIndex = wrapSymbols.indexOf(preText); var preIndex = wrapSymbols.indexOf(preText)
var postEndPos = { var postEndPos = {
line: from.line, line: from.line,
ch: from.ch - 1 ch: from.ch - 1
}; }
var postText = cm.getRange(postEndPos, from); var postText = cm.getRange(postEndPos, from)
var postIndex = wrapSymbols.indexOf(postText); var postIndex = wrapSymbols.indexOf(postText)
// check if surround symbol are list in array and matched // check if surround symbol are list in array and matched
if (preIndex > -1 && postIndex > -1 && preIndex === postIndex) { if (preIndex > -1 && postIndex > -1 && preIndex === postIndex) {
cm.replaceRange("", to, preEndPos, '+delete'); cm.replaceRange('', to, preEndPos, '+delete')
cm.replaceRange("", postEndPos, from, '+delete'); cm.replaceRange('', postEndPos, from, '+delete')
} }
} }
} }