Skip to content

Commit

Permalink
feat: mac meta vs ctrl key
Browse files Browse the repository at this point in the history
  • Loading branch information
dstoc committed Nov 4, 2024
1 parent aaf4e94 commit 1f09398
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {Focus} from './markdown/view-model-ops.js';
import {findOpenCreateBundle} from './commands/find-open-create.js';
import {CommandContext} from './commands/context.js';
import {sigprop} from './signal-utils.js';
import {platformModifier} from './keyboard.js';

export interface EditorNavigation {
kind: 'navigate' | 'replace';
Expand Down Expand Up @@ -394,7 +395,7 @@ export class Editor extends LitElement {
const alter = keyboardEvent.shiftKey ? 'extend' : 'move';
const granularity = ['ArrowUp', 'ArrowDown'].includes(keyboardEvent.key)
? 'line'
: keyboardEvent.ctrlKey
: platformModifier(keyboardEvent)
? 'word'
: 'character';
const result = hostContext.hasSelection
Expand Down Expand Up @@ -464,7 +465,7 @@ export class Editor extends LitElement {
} else {
this.runEditAction(inline, editInlineIndent, mode);
}
} else if (keyboardEvent.key === 'z' && keyboardEvent.ctrlKey) {
} else if (keyboardEvent.key === 'z' && platformModifier(keyboardEvent)) {
if (!hostContext.root) return;
event.preventDefault();
const focus = hostContext.root[viewModel].tree.undo(hostContext.root);
Expand All @@ -477,7 +478,7 @@ export class Editor extends LitElement {
hostContext.focusOffset = focus.offset;
focus.node[viewModel].renderSignal.value++;
}
} else if (keyboardEvent.key === 'y' && keyboardEvent.ctrlKey) {
} else if (keyboardEvent.key === 'y' && platformModifier(keyboardEvent)) {
if (!hostContext.root) return;
event.preventDefault();
const focus = hostContext.root[viewModel].tree.redo(hostContext.root);
Expand All @@ -490,7 +491,7 @@ export class Editor extends LitElement {
hostContext.focusOffset = focus.offset;
focus.node[viewModel].renderSignal.value++;
}
} else if (keyboardEvent.key === 'a' && keyboardEvent.ctrlKey) {
} else if (keyboardEvent.key === 'a' && platformModifier(keyboardEvent)) {
this.autocomplete.abort();
const {hostContext: selectedHostContext} =
getBlockSelectionTarget(inline) ?? {};
Expand All @@ -509,12 +510,12 @@ export class Editor extends LitElement {
hostContext.setSelection(node, node);
}
}
} else if (keyboardEvent.key === 'c' && keyboardEvent.ctrlKey) {
} else if (keyboardEvent.key === 'c' && platformModifier(keyboardEvent)) {
const {hostContext} = getBlockSelectionTarget(inline) ?? {};
if (!hostContext) return;
keyboardEvent.preventDefault();
noAwait(copyMarkdownToClipboard(serializeSelection(hostContext)));
} else if (keyboardEvent.key === 'x' && keyboardEvent.ctrlKey) {
} else if (keyboardEvent.key === 'x' && platformModifier(keyboardEvent)) {
const selectionTarget = getBlockSelectionTarget(inline);
if (!selectionTarget?.hostContext) return;
keyboardEvent.preventDefault();
Expand Down
4 changes: 4 additions & 0 deletions src/keyboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const isMac = /Mac|iPhone/i.test(navigator.platform);
export function platformModifier(e: KeyboardEvent) {
return isMac ? e.metaKey : e.ctrlKey;
}
3 changes: 2 additions & 1 deletion src/pkmapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {Components, ComponentsBuilder} from './components.js';
import {Backup} from './backup.js';
import {ConfigStore} from './config-store.js';
import {BackLinks} from './backlinks.js';
import {platformModifier} from './keyboard.js';

export function injectStyles() {
document.adoptedStyleSheets = [...styles];
Expand Down Expand Up @@ -96,7 +97,7 @@ export abstract class PkmAppBase extends LitElement {
constructor() {
super();
document.addEventListener('keydown', (e) => {
if (e.key === 'p' && e.ctrlKey) {
if (e.key === 'p' && platformModifier(e)) {
e.preventDefault();
this.onCommands({detail: undefined});
}
Expand Down

0 comments on commit 1f09398

Please sign in to comment.