0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

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).
This commit is contained in:
Robert Jackson 2014-07-31 12:21:48 -04:00 committed by Hannah Wolfe
parent 7a626d54a5
commit 5b30a51adf
2 changed files with 24 additions and 40 deletions

View file

@ -6,6 +6,9 @@
import titleize from 'ghost/utils/titleize'; import titleize from 'ghost/utils/titleize';
function init() { function init() {
// remove predefined `ctrl+h` shortcut
delete CodeMirror.keyMap.emacsy['Ctrl-H'];
//Used for simple, noncomputational replace-and-go! shortcuts. //Used for simple, noncomputational replace-and-go! shortcuts.
// See default case in shortcut function below. // See default case in shortcut function below.
CodeMirror.prototype.simpleShortcutSyntax = { CodeMirror.prototype.simpleShortcutSyntax = {
@ -24,39 +27,27 @@ function init() {
fromLineStart = {line: cursor.line, ch: 0}, fromLineStart = {line: cursor.line, ch: 0},
toLineEnd = {line: cursor.line, ch: line.length}, toLineEnd = {line: cursor.line, ch: line.length},
md, letterCount, textIndex, position, converter, md, letterCount, textIndex, position, converter,
generatedHTML; generatedHTML, match, currentHeaderLevel, hashPrefix,
replacementLine;
switch (type) { switch (type) {
case 'h1': case 'cycleHeaderLevel':
line = line.replace(/^#* /, ''); match = line.match(/^#+/);
this.replaceRange('# ' + line, fromLineStart, toLineEnd);
this.setCursor(cursor.line, cursor.ch + 2); if (!match) {
return; currentHeaderLevel = 1;
case 'h2': } else {
line = line.replace(/^#* /, ''); currentHeaderLevel = match[0].length;
this.replaceRange('## ' + line, fromLineStart, toLineEnd); }
this.setCursor(cursor.line, cursor.ch + 3);
return; if (currentHeaderLevel > 2) { currentHeaderLevel = 1; }
case 'h3':
line = line.replace(/^#* /, ''); hashPrefix = new Array(currentHeaderLevel + 2).join('#');
this.replaceRange('### ' + line, fromLineStart, toLineEnd); replacementLine = hashPrefix + ' ' + line.replace(/^#* /, '');
this.setCursor(cursor.line, cursor.ch + 4);
return; this.replaceRange(replacementLine, fromLineStart, toLineEnd);
case 'h4': this.setCursor(cursor.line, cursor.ch + replacementLine.length);
line = line.replace(/^#* /, ''); break;
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 'link': case 'link':
md = this.simpleShortcutSyntax.link.replace('$1', text); md = this.simpleShortcutSyntax.link.replace('$1', text);
this.replaceSelection(md, 'end'); this.replaceSelection(md, 'end');
@ -111,7 +102,7 @@ function init() {
// Talk to Ember // Talk to Ember
this.component.sendAction('openModal', 'copy-html', { generatedHTML: generatedHTML }); this.component.sendAction('openModal', 'copy-html', { generatedHTML: generatedHTML });
break; break;
default: default:
if (this.simpleShortcutSyntax[type]) { if (this.simpleShortcutSyntax[type]) {

View file

@ -21,14 +21,7 @@ shortcuts['ctrl+U'] = {action: 'codeMirrorShortcut', options: {type: 'uppercase'
shortcuts['ctrl+shift+U'] = {action: 'codeMirrorShortcut', options: {type: 'lowercase'}}; shortcuts['ctrl+shift+U'] = {action: 'codeMirrorShortcut', options: {type: 'lowercase'}};
shortcuts['ctrl+alt+shift+U'] = {action: 'codeMirrorShortcut', options: {type: 'titlecase'}}; shortcuts['ctrl+alt+shift+U'] = {action: 'codeMirrorShortcut', options: {type: 'titlecase'}};
shortcuts[ctrlOrCmd + '+shift+c'] = {action: 'codeMirrorShortcut', options: {type: 'copyHTML'}}; shortcuts[ctrlOrCmd + '+shift+c'] = {action: 'codeMirrorShortcut', options: {type: 'copyHTML'}};
shortcuts[ctrlOrCmd + '+h'] = {action: 'codeMirrorShortcut', options: {type: 'cycleHeaderLevel'}};
//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'}};
//Formatting //Formatting
shortcuts['ctrl+q'] = {action: 'codeMirrorShortcut', options: {type: 'blockquote'}}; shortcuts['ctrl+q'] = {action: 'codeMirrorShortcut', options: {type: 'blockquote'}};