2024-01-04 19:08:12 +00:00
|
|
|
import { Decoration, EditorState, syntaxTree } from "../deps.ts";
|
2022-11-18 15:04:37 +00:00
|
|
|
import {
|
2022-12-09 15:09:53 +00:00
|
|
|
decoratorStateField,
|
2022-11-18 15:04:37 +00:00
|
|
|
invisibleDecoration,
|
|
|
|
isCursorInRange,
|
|
|
|
} from "./util.ts";
|
|
|
|
|
2024-01-04 19:08:12 +00:00
|
|
|
export function cleanBlockPlugin() {
|
|
|
|
return decoratorStateField(
|
|
|
|
(state: EditorState) => {
|
|
|
|
const widgets: any[] = [];
|
2022-11-18 15:20:00 +00:00
|
|
|
|
2024-01-04 19:08:12 +00: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),
|
|
|
|
);
|
|
|
|
}
|
2023-12-21 18:04:54 +00:00
|
|
|
|
2024-01-04 19:08:12 +00:00
|
|
|
if (
|
|
|
|
node.name === "Image" &&
|
|
|
|
!isCursorInRange(state, [node.from, node.to])
|
|
|
|
) {
|
|
|
|
widgets.push(invisibleDecoration.range(node.from, node.to));
|
2023-12-21 18:04:54 +00:00
|
|
|
}
|
2024-01-04 19:08:12 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
return Decoration.set(widgets, true);
|
2022-11-18 15:04:37 +00:00
|
|
|
},
|
2024-01-04 19:08:12 +00:00
|
|
|
);
|
2022-12-09 15:09:53 +00:00
|
|
|
}
|