silverbullet/web/cm_plugins/block.ts

34 lines
876 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 cleanBlockPlugin() {
return decoratorStateField(
(state: EditorState) => {
const widgets: any[] = [];
2022-11-18 23:20:00 +08:00
syntaxTree(state).iterate({
enter(node) {
if (
node.name === "HorizontalRule" &&
!isCursorInRange(state, [node.from, node.to])
) {
widgets.push(invisibleDecoration.range(node.from, node.to));
widgets.push(
Decoration.line({
class: "sb-line-hr",
}).range(node.from),
);
}
},
});
return Decoration.set(widgets, true);
},
);
}