silverbullet/web/cm_plugins/escapes.ts

29 lines
724 B
TypeScript
Raw Permalink Normal View History

2024-07-30 23:33:33 +08:00
import type { EditorState } from "@codemirror/state";
import { syntaxTree } from "@codemirror/language";
import { Decoration } from "@codemirror/view";
import {
decoratorStateField,
invisibleDecoration,
isCursorInRange,
} from "./util.ts";
export function cleanEscapePlugin() {
return decoratorStateField(
(state: EditorState) => {
const widgets: any[] = [];
syntaxTree(state).iterate({
enter({ type, from, to }) {
if (
type.name === "Escape" &&
!isCursorInRange(state, [from, to])
) {
widgets.push(invisibleDecoration.range(from, from + 1));
}
},
});
return Decoration.set(widgets, true);
},
);
}