-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add keyboard shortcuts via editor#registerKeyCommand
fixes #112
- Loading branch information
Showing
6 changed files
with
217 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Key from '../utils/key'; | ||
import { MODIFIERS } from '../utils/key'; | ||
import { detect } from '../utils/array-utils'; | ||
import LinkCommand from '../commands/link'; | ||
import BoldCommand from '../commands/bold'; | ||
import ItalicCommand from '../commands/italic'; | ||
|
||
function runSelectionCommand(editor, CommandKlass) { | ||
if (editor.cursor.hasSelection()) { | ||
const cmd = new CommandKlass(editor); | ||
if (cmd.isActive()) { | ||
cmd.unexec(); | ||
} else { | ||
cmd.exec(); | ||
} | ||
} | ||
} | ||
|
||
export const DEFAULT_KEY_COMMANDS = [{ | ||
modifier: MODIFIERS.META, | ||
str: 'B', | ||
run(editor) { | ||
runSelectionCommand(editor, BoldCommand); | ||
} | ||
}, { | ||
modifier: MODIFIERS.CTRL, | ||
str: 'B', | ||
run(editor) { | ||
runSelectionCommand(editor, BoldCommand); | ||
} | ||
}, { | ||
modifier: MODIFIERS.META, | ||
str: 'I', | ||
run(editor) { | ||
runSelectionCommand(editor, ItalicCommand); | ||
} | ||
}, { | ||
modifier: MODIFIERS.CTRL, | ||
str: 'I', | ||
run(editor) { | ||
runSelectionCommand(editor, ItalicCommand); | ||
} | ||
}, { | ||
modifier: MODIFIERS.META, | ||
str: 'K', | ||
run(editor) { | ||
if (!editor.cursor.hasSelection()) { return; } | ||
|
||
let selectedText = editor.cursor.selectedText(); | ||
let defaultUrl = ''; | ||
if (selectedText.indexOf('http') !== -1) { defaultUrl = selectedText; } | ||
|
||
editor.showPrompt('Enter a URL', defaultUrl, url => { | ||
if (!url) { return; } | ||
|
||
const linkCommand = new LinkCommand(editor); | ||
linkCommand.exec(url); | ||
}); | ||
} | ||
}]; | ||
|
||
export function validateKeyCommand(keyCommand) { | ||
return !!keyCommand.modifier && !!keyCommand.str && !!keyCommand.run; | ||
} | ||
|
||
export function findKeyCommand(keyCommands, keyEvent) { | ||
const key = Key.fromEvent(keyEvent); | ||
|
||
return detect(keyCommands, ({modifier, str}) => { | ||
return key.hasModifier(modifier) && key.isChar(str); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Editor } from 'content-kit-editor'; | ||
import { MODIFIERS } from 'content-kit-editor/utils/key'; | ||
import Helpers from '../test-helpers'; | ||
|
||
const { module, test } = Helpers; | ||
|
||
let editor, editorElement; | ||
|
||
module('Acceptance: Editor: Key Commands', { | ||
beforeEach() { | ||
editorElement = document.createElement('div'); | ||
editorElement.setAttribute('id', 'editor'); | ||
$('#qunit-fixture').append(editorElement); | ||
}, | ||
afterEach() { | ||
if (editor) { editor.destroy(); } | ||
} | ||
}); | ||
|
||
test('typing command-B bolds highlighted text', (assert) => { | ||
const mobiledoc = Helpers.mobiledoc.build( | ||
({post, markupSection, marker}) => post([ | ||
markupSection('p', [marker('something')]) | ||
])); | ||
|
||
editor = new Editor({mobiledoc}); | ||
editor.render(editorElement); | ||
|
||
assert.hasNoElement('#editor strong', 'precond - no strong text'); | ||
Helpers.dom.selectText('something', editorElement); | ||
Helpers.dom.triggerKeyCommand(editor, 'B', MODIFIERS.META); | ||
|
||
assert.hasElement('#editor strong:contains(something)', 'text is strengthened'); | ||
}); | ||
|
||
test('typing command-I italicizes highlighted text', (assert) => { | ||
const mobiledoc = Helpers.mobiledoc.build( | ||
({post, markupSection, marker}) => post([ | ||
markupSection('p', [marker('something')]) | ||
])); | ||
|
||
editor = new Editor({mobiledoc}); | ||
editor.render(editorElement); | ||
|
||
assert.hasNoElement('#editor em', 'precond - no strong text'); | ||
Helpers.dom.selectText('something', editorElement); | ||
Helpers.dom.triggerKeyCommand(editor, 'I', MODIFIERS.META); | ||
|
||
assert.hasElement('#editor em:contains(something)', 'text is emphasized'); | ||
}); | ||
|
||
test('new key commands can be registered', (assert) => { | ||
const mobiledoc = Helpers.mobiledoc.build( | ||
({post, markupSection, marker}) => post([ | ||
markupSection('p', [marker('something')]) | ||
])); | ||
|
||
let passedEditor; | ||
editor = new Editor({mobiledoc}); | ||
editor.registerKeyCommand({ | ||
modifier: MODIFIERS.CTRL, | ||
str: 'X', | ||
run(editor) { passedEditor = editor; } | ||
}); | ||
editor.render(editorElement); | ||
|
||
Helpers.dom.triggerKeyCommand(editor, 'Y', MODIFIERS.CTRL); | ||
|
||
assert.ok(!passedEditor, 'incorrect key combo does not trigger key command'); | ||
|
||
Helpers.dom.triggerKeyCommand(editor, 'X', MODIFIERS.CTRL); | ||
|
||
assert.ok(!!passedEditor && passedEditor === editor, 'run method is called'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters