From 5b30a51adfb41f629ac7225dd7a218f75862e07d Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Thu, 31 Jul 2014 12:21:48 -0400 Subject: [PATCH 1/2] Enable cycling header levels with ctrl+h. closes #3019 * Remove preexisting `ctrl+h` keymap. * Add logic to cycle the header level. * Loops around after h3. * Sets cursor position to end of heading * Set initial level to h2. * Remove `ctrl+alt+${NUM}` keymaps (as they do not work internationally). --- core/client/utils/codemirror-shortcuts.js | 55 ++++++++++------------- core/client/utils/editor-shortcuts.js | 9 +--- 2 files changed, 24 insertions(+), 40 deletions(-) diff --git a/core/client/utils/codemirror-shortcuts.js b/core/client/utils/codemirror-shortcuts.js index bb3ee63644..a67b33136f 100644 --- a/core/client/utils/codemirror-shortcuts.js +++ b/core/client/utils/codemirror-shortcuts.js @@ -6,6 +6,9 @@ import titleize from 'ghost/utils/titleize'; function init() { + // remove predefined `ctrl+h` shortcut + delete CodeMirror.keyMap.emacsy['Ctrl-H']; + //Used for simple, noncomputational replace-and-go! shortcuts. // See default case in shortcut function below. CodeMirror.prototype.simpleShortcutSyntax = { @@ -24,39 +27,27 @@ function init() { fromLineStart = {line: cursor.line, ch: 0}, toLineEnd = {line: cursor.line, ch: line.length}, md, letterCount, textIndex, position, converter, - generatedHTML; + generatedHTML, match, currentHeaderLevel, hashPrefix, + replacementLine; switch (type) { - case 'h1': - line = line.replace(/^#* /, ''); - this.replaceRange('# ' + line, fromLineStart, toLineEnd); - this.setCursor(cursor.line, cursor.ch + 2); - return; - case 'h2': - line = line.replace(/^#* /, ''); - this.replaceRange('## ' + line, fromLineStart, toLineEnd); - this.setCursor(cursor.line, cursor.ch + 3); - return; - case 'h3': - line = line.replace(/^#* /, ''); - this.replaceRange('### ' + line, fromLineStart, toLineEnd); - this.setCursor(cursor.line, cursor.ch + 4); - return; - case 'h4': - line = line.replace(/^#* /, ''); - this.replaceRange('#### ' + line, fromLineStart, toLineEnd); - this.setCursor(cursor.line, cursor.ch + 5); - return; - case 'h5': - line = line.replace(/^#* /, ''); - this.replaceRange('##### ' + line, fromLineStart, toLineEnd); - this.setCursor(cursor.line, cursor.ch + 6); - return; - case 'h6': - line = line.replace(/^#* /, ''); - this.replaceRange('###### ' + line, fromLineStart, toLineEnd); - this.setCursor(cursor.line, cursor.ch + 7); - return; + case 'cycleHeaderLevel': + match = line.match(/^#+/); + + if (!match) { + currentHeaderLevel = 1; + } else { + currentHeaderLevel = match[0].length; + } + + if (currentHeaderLevel > 2) { currentHeaderLevel = 1; } + + hashPrefix = new Array(currentHeaderLevel + 2).join('#'); + replacementLine = hashPrefix + ' ' + line.replace(/^#* /, ''); + + this.replaceRange(replacementLine, fromLineStart, toLineEnd); + this.setCursor(cursor.line, cursor.ch + replacementLine.length); + break; case 'link': md = this.simpleShortcutSyntax.link.replace('$1', text); this.replaceSelection(md, 'end'); @@ -111,7 +102,7 @@ function init() { // Talk to Ember this.component.sendAction('openModal', 'copy-html', { generatedHTML: generatedHTML }); - + break; default: if (this.simpleShortcutSyntax[type]) { diff --git a/core/client/utils/editor-shortcuts.js b/core/client/utils/editor-shortcuts.js index 7d7b3d1986..ed29222f48 100644 --- a/core/client/utils/editor-shortcuts.js +++ b/core/client/utils/editor-shortcuts.js @@ -21,14 +21,7 @@ shortcuts['ctrl+U'] = {action: 'codeMirrorShortcut', options: {type: 'uppercase' shortcuts['ctrl+shift+U'] = {action: 'codeMirrorShortcut', options: {type: 'lowercase'}}; shortcuts['ctrl+alt+shift+U'] = {action: 'codeMirrorShortcut', options: {type: 'titlecase'}}; shortcuts[ctrlOrCmd + '+shift+c'] = {action: 'codeMirrorShortcut', options: {type: 'copyHTML'}}; - -//Headings -shortcuts['ctrl+alt+1'] = {action: 'codeMirrorShortcut', options: {type: 'h1'}}; -shortcuts['ctrl+alt+2'] = {action: 'codeMirrorShortcut', options: {type: 'h2'}}; -shortcuts['ctrl+alt+3'] = {action: 'codeMirrorShortcut', options: {type: 'h3'}}; -shortcuts['ctrl+alt+4'] = {action: 'codeMirrorShortcut', options: {type: 'h4'}}; -shortcuts['ctrl+alt+5'] = {action: 'codeMirrorShortcut', options: {type: 'h5'}}; -shortcuts['ctrl+alt+6'] = {action: 'codeMirrorShortcut', options: {type: 'h6'}}; +shortcuts[ctrlOrCmd + '+h'] = {action: 'codeMirrorShortcut', options: {type: 'cycleHeaderLevel'}}; //Formatting shortcuts['ctrl+q'] = {action: 'codeMirrorShortcut', options: {type: 'blockquote'}}; From 686de626448ee1b35dbe3494a1284abdf7169561 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Wed, 24 Sep 2014 18:12:43 +0100 Subject: [PATCH 2/2] Updating markdown help modal with new shortcuts issue #3019 --- core/client/templates/modals/markdown.hbs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/client/templates/modals/markdown.hbs b/core/client/templates/modals/markdown.hbs index 445fd1166f..3fff7634d8 100644 --- a/core/client/templates/modals/markdown.hbs +++ b/core/client/templates/modals/markdown.hbs @@ -53,17 +53,17 @@ H1 # Heading - Ctrl + Alt + 1 + H2 ## Heading - Ctrl + Alt + 2 + Ctrl/⌘ + H H3 ### Heading - Ctrl + Alt + 3 + Ctrl/⌘ + H (x2)