Skip to content

Commit

Permalink
Split shortcuts by platform and name them
Browse files Browse the repository at this point in the history
  • Loading branch information
hpohlmeyer authored and lukemelia committed Sep 18, 2023
1 parent 942bc72 commit 069865e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 87 deletions.
105 changes: 57 additions & 48 deletions src/js/editor/key-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,121 +46,130 @@ function deleteToEndOfSection(editor: Editor) {
})
}

export const DEFAULT_KEY_COMMANDS: KeyCommand[] = [
const MAC_KEY_COMMANDS: KeyCommand[] = [
{
str: 'META+B',
name: 'default-bold',
run(editor) {
editor.toggleMarkup('strong')
},
},
{
str: 'CTRL+B',
str: 'META+I',
name: 'default-italic',
run(editor) {
editor.toggleMarkup('strong')
editor.toggleMarkup('em')
},
},
{
str: 'META+I',
str: 'META+U',
name: 'default-underline',
run(editor) {
editor.toggleMarkup('em')
editor.toggleMarkup('u')
},
},
{
str: 'CTRL+I',
str: 'META+K',
name: 'default-link',
run(editor) {
editor.toggleMarkup('em')
return toggleLink(editor)
},
},
{
str: 'META+U',
str: 'META+A',
name: 'default-select-all',
run(editor) {
editor.toggleMarkup('u')
selectAll(editor)
},
},
{
str: 'CTRL+U',
str: 'CTRL+A',
name: 'default-goto-line-start',
run(editor) {
editor.toggleMarkup('u')
gotoStartOfLine(editor)
},
},
{
str: 'CTRL+K',
str: 'CTRL+E',
name: 'default-goto-line-end',
run(editor) {
if (Browser.isMac()) {
return deleteToEndOfSection(editor)
} else if (Browser.isWin()) {
return toggleLink(editor)
}
gotoEndOfLine(editor)
},
},
{
str: 'CTRL+A',
str: 'META+Z',
name: 'default-undo',
run(editor) {
if (Browser.isMac()) {
gotoStartOfLine(editor)
} else {
selectAll(editor)
}
editor.run(postEditor => postEditor.undoLastChange())
},
},
{
str: 'META+A',
str: 'META+SHIFT+Z',
name: 'default-redo',
run(editor) {
if (Browser.isMac()) {
selectAll(editor)
}
editor.run(postEditor => postEditor.redoLastChange())
},
},
{
str: 'CTRL+E',
str: 'CTRL+K',
name: 'default-delete-line',
run(editor) {
if (Browser.isMac()) {
gotoEndOfLine(editor)
}
return deleteToEndOfSection(editor)
},
},
]
const WINDOWS_ETC_KEY_COMMANDS: KeyCommand[] = [
{
str: 'META+K',
str: 'CTRL+B',
name: 'default-bold',
run(editor) {
return toggleLink(editor)
editor.toggleMarkup('strong')
},
},
{
str: 'META+Z',
str: 'CTRL+I',
name: 'default-italic',
run(editor) {
editor.run(postEditor => {
postEditor.undoLastChange()
})
editor.toggleMarkup('em')
},
},
{
str: 'META+SHIFT+Z',
str: 'CTRL+U',
name: 'default-underline',
run(editor) {
editor.run(postEditor => {
postEditor.redoLastChange()
})
editor.toggleMarkup('u')
},
},
{
str: 'CTRL+K',
name: 'default-link',
run(editor) {
return toggleLink(editor)
},
},
{
str: 'CTRL+A',
name: 'default-select-all',
run(editor) {
selectAll(editor)
},
},
{
str: 'CTRL+Z',
name: 'default-undo',
run(editor) {
if (Browser.isMac()) {
return false
}
editor.run(postEditor => postEditor.undoLastChange())
},
},
{
str: 'CTRL+SHIFT+Z',
name: 'default-redo',
run(editor) {
if (Browser.isMac()) {
return false
}
editor.run(postEditor => postEditor.redoLastChange())
},
},
]

export const DEFAULT_KEY_COMMANDS = (Browser.isMac() && MAC_KEY_COMMANDS) || WINDOWS_ETC_KEY_COMMANDS
function modifierNamesToMask(modiferNames: string[]) {
let defaultVal = 0
return reduce(
Expand Down
84 changes: 45 additions & 39 deletions tests/acceptance/editor-key-commands-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,47 +122,53 @@ function testStatefulCommand({modifierName, key, command, markupName}) {
});
}

testStatefulCommand({
modifierName: 'META',
key: 'B',
command: 'command-B',
markupName: 'strong'
});

testStatefulCommand({
modifierName: 'CTRL',
key: 'B',
command: 'ctrl-B',
markupName: 'strong'
});

testStatefulCommand({
modifierName: 'META',
key: 'I',
command: 'command-I',
markupName: 'em'
});

testStatefulCommand({
modifierName: 'CTRL',
key: 'I',
command: 'ctrl-I',
markupName: 'em'
});
if (Browser.isMac()) {
testStatefulCommand({
modifierName: 'META',
key: 'B',
command: 'command-B',
markupName: 'strong'
});
} else {
testStatefulCommand({
modifierName: 'CTRL',
key: 'B',
command: 'ctrl-B',
markupName: 'strong'
});
}

testStatefulCommand({
modifierName: 'META',
key: 'U',
command: 'command-U',
markupName: 'u'
});
if (Browser.isMac()) {
testStatefulCommand({
modifierName: 'META',
key: 'I',
command: 'command-I',
markupName: 'em'
});
} else {
testStatefulCommand({
modifierName: 'CTRL',
key: 'I',
command: 'ctrl-I',
markupName: 'em'
});
}

testStatefulCommand({
modifierName: 'CTRL',
key: 'U',
command: 'ctrl-U',
markupName: 'u'
});
if (Browser.isMac()) {
testStatefulCommand({
modifierName: 'META',
key: 'U',
command: 'command-U',
markupName: 'u'
});
} else {
testStatefulCommand({
modifierName: 'CTRL',
key: 'U',
command: 'ctrl-U',
markupName: 'u'
});
}

if (Browser.isMac()) {
test(`[Mac] ctrl-k clears to the end of a line`, (assert) => {
Expand Down

0 comments on commit 069865e

Please sign in to comment.