Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split shortcuts by platform and name them #789

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 3 additions & 2 deletions tests/acceptance/editor-undo-redo-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MODIFIERS } from 'mobiledoc-kit/utils/key';
import Helpers from '../test-helpers';
import Browser from 'mobiledoc-kit/utils/browser';

const { module, test } = Helpers;

Expand All @@ -8,11 +9,11 @@ const undoBlockTimeout = 2000;
let editor, editorElement, oldDateNow;

function undo(editor) {
Helpers.dom.triggerKeyCommand(editor, 'Z', [MODIFIERS.META]);
Helpers.dom.triggerKeyCommand(editor, 'Z', [Browser.isMac() ? MODIFIERS.META : MODIFIERS.CTRL]);
}

function redo(editor) {
Helpers.dom.triggerKeyCommand(editor, 'Z', [MODIFIERS.META, MODIFIERS.SHIFT]);
Helpers.dom.triggerKeyCommand(editor, 'Z', [Browser.isMac() ? MODIFIERS.META : MODIFIERS.CTRL, MODIFIERS.SHIFT]);
}

module('Acceptance: Editor: Undo/Redo', {
Expand Down
Loading