Skip to content

Commit

Permalink
kedyou: modify/improve delete hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
manstie committed Feb 26, 2024
1 parent 2402bb2 commit e30d6bf
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 41 deletions.
60 changes: 34 additions & 26 deletions src/editor-model/commands-delete.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { register } from '../editor/commands';
import type { _Model } from './model-private';
import { deleteForward, deleteBackward, deleteRange } from './delete';
import { wordBoundaryOffset } from './commands';
import { skip } from './commands';

register(
{
Expand All @@ -11,33 +11,41 @@ register(
deleteForward: (model: _Model): boolean => deleteForward(model),
deleteBackward: (model: _Model): boolean => deleteBackward(model),
deleteNextWord: (model: _Model): boolean =>
model.contentWillChange({ inputType: 'deleteWordForward' }) &&
deleteRange(
model,
[model.anchor, wordBoundaryOffset(model, model.position, 'forward')],
'deleteWordForward'
),
skip(model, 'forward', { delete: true }),
deletePreviousWord: (model: _Model): boolean =>
model.contentWillChange({ inputType: 'deleteWordBackward' }) &&
deleteRange(
model,
[model.anchor, wordBoundaryOffset(model, model.position, 'backward')],
'deleteWordBackward'
),
deleteToGroupStart: (model: _Model): boolean =>
model.contentWillChange({ inputType: 'deleteSoftLineBackward' }) &&
deleteRange(
model,
[model.anchor, model.offsetOf(model.at(model.position).firstSibling)],
'deleteSoftLineBackward'
),
deleteToGroupEnd: (model: _Model): boolean =>
model.contentWillChange({ inputType: 'deleteSoftLineForward' }) &&
deleteRange(
model,
[model.anchor, model.offsetOf(model.at(model.position).lastSibling)],
'deleteSoftLineForward'
),
skip(model, 'backward', { delete: true }),
deleteToGroupStart: (model: _Model): boolean => {
if (!model.contentWillChange({ inputType: 'deleteSoftLineBackward' }))
return false;
const pos = model.offsetOf(model.at(model.position).firstSibling);
if (pos === model.position) {
model.announce('plonk');
return false;
}

model.deferNotifications(
{ content: true, selection: true, type: 'deleteSoftLineBackward' },
() => model.deleteAtoms([model.anchor, pos])
);
model.position = pos;
return true;
},
deleteToGroupEnd: (model: _Model): boolean => {
if (!model.contentWillChange({ inputType: 'deleteSoftLineForward' }))
return false;
const pos = model.offsetOf(model.at(model.position).lastSibling);
if (pos === model.position) {
model.announce('plonk');
return false;
}

model.deferNotifications(
{ content: true, selection: true, type: 'deleteSoftLineForward' },
() => model.deleteAtoms([model.anchor, pos])
);
return true;
},
deleteToMathFieldStart: (model: _Model): boolean =>
model.contentWillChange({ inputType: 'deleteHardLineBackward' }) &&
deleteRange(model, [model.anchor, 0], 'deleteHardLineBackward'),
Expand Down
19 changes: 16 additions & 3 deletions src/editor-model/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { PromptAtom } from '../atoms/prompt';
import { getLocalDOMRect } from 'editor-mathfield/utils';
import { _Mathfield } from 'editor-mathfield/mathfield-private';
import { alignedDelimiters } from './array';
import { deleteRange } from './delete';

/*
* Calculates the offset of the "next word".
Expand Down Expand Up @@ -118,12 +119,13 @@ export function wordBoundaryOffset(
* than the current focus.
* If `extend` is true, the selection will be extended. Otherwise, it is
* collapsed, then moved.
* If `delete` is true, the skipped range is removed.
* @todo array
*/
export function skip(
model: _Model,
direction: 'forward' | 'backward',
options?: { extend: boolean }
options?: { extend?: boolean; delete?: boolean }
): boolean {
const previousPosition = model.position;

Expand Down Expand Up @@ -276,13 +278,24 @@ export function skip(
model.announce('plonk');
return false;
}
model.announce('move', previousPosition);
} else {
if (offset === model.position) {
model.announce('plonk');
return false;
}

model.position = offset;
if (options?.delete ?? false) {
if (direction === 'forward')
deleteRange(model, [previousPosition, offset], 'deleteWordForward');
else {
deleteRange(model, [previousPosition, offset], 'deleteWordBackward');
model.position = offset;
}
} else {
model.position = offset;
model.announce('move', previousPosition);
}
}

model.announce('move', previousPosition);
Expand Down Expand Up @@ -336,7 +349,7 @@ export function move(
//
// Kedyou: Customize cursor movement in aligned environment
//
let atom = model.at(pos);
const atom = model.at(pos);

if (
atom.parent instanceof ArrayAtom &&
Expand Down
31 changes: 19 additions & 12 deletions src/editor/keybindings-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ export const DEFAULT_KEYBINDINGS: Keybinding[] = [
{ key: 'shift+[ArrowDown]', command: 'extendSelectionDownward' },

{ key: '[Backspace]', command: 'deleteBackward' },
{ key: 'alt+[Delete]', command: 'deleteBackward' },

{ key: '[Delete]', command: 'deleteForward' },
{ key: 'alt+[Backspace]', command: 'deleteForward' },
{ key: 'shift+[Backspace]', command: 'deleteForward' },

{ key: 'alt+[Backspace]', command: 'deletePreviousWord' },
{ key: 'alt+[Delete]', command: 'deleteNextWord' },

{ key: 'ctrl+[Backspace]', command: 'deleteToGroupStart' },

{ key: 'ctrl+[Delete]', command: 'deleteToGroupEnd' },
{ key: 'ctrl+shift+[Backspace]', command: 'deleteToGroupEnd' },

{ key: 'alt+[ArrowLeft]', command: 'moveToPreviousWord' },
{ key: 'alt+[ArrowRight]', command: 'moveToNextWord' },
Expand Down Expand Up @@ -332,16 +339,6 @@ export const DEFAULT_KEYBINDINGS: Keybinding[] = [
ifMode: 'math',
command: 'addRowBefore',
},
{
key: 'ctrl+[Backspace]',
ifMode: 'math',
command: 'removeRow',
},
{
key: 'cmd+[Backspace]',
ifMode: 'math',
command: 'removeRow',
},

// {
// key: 'ctrl+[Comma]',
Expand Down Expand Up @@ -380,6 +377,16 @@ export const DEFAULT_KEYBINDINGS: Keybinding[] = [
ifMode: 'math',
command: 'removeColumn',
},
{
key: 'shift+[Delete]',
ifMode: 'math',
command: 'removeRow',
},
{
key: 'shift+alt+[Backspace]',
ifMode: 'math',
command: 'removeRow',
},

{
key: 'alt+[Digit5]',
Expand Down

0 comments on commit e30d6bf

Please sign in to comment.