mirror of
https://codeberg.org/uzu/strudel
synced 2026-07-30 08:23:18 -04:00
repeat charactar
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
keymap,
|
||||
lineNumbers,
|
||||
} from '@codemirror/view';
|
||||
import {repeatCharKeymap} from './repeatcharacter.mjs';
|
||||
import { persistentAtom } from '@nanostores/persistent';
|
||||
import { logger, registerControl, repl } from '@strudel/core';
|
||||
import { cleanupDraw, cleanupDrawContext, Drawer } from '@strudel/draw';
|
||||
@@ -111,6 +112,7 @@ export function initEditor({ initialCode = '', onChange, onEvaluate, onStop, roo
|
||||
syntaxHighlighting(defaultHighlightStyle),
|
||||
EditorView.updateListener.of((v) => onChange(v)),
|
||||
drawSelection({ cursorBlinkRate: 0 }),
|
||||
repeatCharKeymap,
|
||||
Prec.highest(
|
||||
keymap.of([
|
||||
{
|
||||
@@ -215,7 +217,6 @@ export function initEditor({ initialCode = '', onChange, onEvaluate, onStop, roo
|
||||
},
|
||||
};
|
||||
}),
|
||||
|
||||
/* {
|
||||
key: 'Ctrl-Shift-.',
|
||||
run: () => (onPanic ? onPanic() : onStop?.()),
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Compartment } from "@codemirror/state";
|
||||
import { keymap } from "@codemirror/view";
|
||||
|
||||
const repeatMode = new Compartment();
|
||||
|
||||
function repeatPreviousChar(times) {
|
||||
return ({ state, dispatch }) => {
|
||||
const { from, empty } = state.selection.main;
|
||||
if (!empty || from === 0) return false;
|
||||
|
||||
const prevChar = state.doc.sliceString(from - 1, from);
|
||||
const text = Array(times).fill(prevChar).join(" ");
|
||||
|
||||
dispatch(state.update({
|
||||
changes: {
|
||||
from,
|
||||
insert: " " + text
|
||||
}
|
||||
}));
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
function exitRepeatMode(view) {
|
||||
view.dispatch({
|
||||
effects: repeatMode.reconfigure([])
|
||||
});
|
||||
}
|
||||
|
||||
const digitBindings = Array.from({ length: 9 }, (_, i) => ({
|
||||
key: String(i + 1),
|
||||
run(view) {
|
||||
repeatPreviousChar(i + 1)(view);
|
||||
exitRepeatMode(view);
|
||||
return true;
|
||||
}
|
||||
}));
|
||||
|
||||
export const repeatCharKeymap = [
|
||||
|
||||
repeatMode.of([]),
|
||||
|
||||
keymap.of([
|
||||
{
|
||||
key: "Alt-r",
|
||||
run(view) {
|
||||
view.dispatch({
|
||||
effects: repeatMode.reconfigure(keymap.of(digitBindings))
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
])
|
||||
];
|
||||
Reference in New Issue
Block a user