2022-12-09 15:09:53 +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-12-19 11:35:58 +00:00
|
|
|
HtmlWidget,
|
2022-11-18 15:04:37 +00:00
|
|
|
invisibleDecoration,
|
|
|
|
isCursorInRange,
|
|
|
|
} from "./util.ts";
|
|
|
|
|
2022-12-09 15:09:53 +00:00
|
|
|
function hideNodes(state: EditorState) {
|
2022-11-18 15:04:37 +00:00
|
|
|
const widgets: any[] = [];
|
2022-12-09 15:09:53 +00:00
|
|
|
syntaxTree(state).iterate({
|
2022-11-18 15:04:37 +00:00
|
|
|
enter(node) {
|
|
|
|
if (
|
|
|
|
node.name === "HorizontalRule" &&
|
2022-12-09 15:09:53 +00:00
|
|
|
!isCursorInRange(state, [node.from, node.to])
|
2022-11-18 15:04:37 +00:00
|
|
|
) {
|
|
|
|
widgets.push(invisibleDecoration.range(node.from, node.to));
|
|
|
|
widgets.push(
|
|
|
|
Decoration.line({
|
|
|
|
class: "sb-line-hr",
|
|
|
|
}).range(node.from),
|
|
|
|
);
|
|
|
|
}
|
2022-11-18 15:20:00 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
node.name === "Image" &&
|
2022-12-09 15:09:53 +00:00
|
|
|
!isCursorInRange(state, [node.from, node.to])
|
2022-11-18 15:20:00 +00:00
|
|
|
) {
|
|
|
|
widgets.push(invisibleDecoration.range(node.from, node.to));
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:04:37 +00:00
|
|
|
if (
|
|
|
|
node.name === "FrontMatterMarker"
|
|
|
|
) {
|
|
|
|
const parent = node.node.parent!;
|
2022-12-09 15:09:53 +00:00
|
|
|
if (!isCursorInRange(state, [parent.from, parent.to])) {
|
2022-11-18 15:04:37 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.line({
|
|
|
|
class: "sb-line-frontmatter-outside",
|
|
|
|
}).range(node.from),
|
|
|
|
);
|
2022-12-19 11:35:58 +00:00
|
|
|
if (parent.from === node.from) {
|
|
|
|
// Only put this on the first line of the frontmatter
|
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
|
|
|
widget: new HtmlWidget(
|
|
|
|
`frontmatter`,
|
|
|
|
"sb-frontmatter-marker",
|
|
|
|
),
|
|
|
|
}).range(node.from),
|
|
|
|
);
|
|
|
|
}
|
2022-11-18 15:04:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
node.name === "CodeMark"
|
|
|
|
) {
|
|
|
|
const parent = node.node.parent!;
|
2022-11-29 07:35:46 +00:00
|
|
|
// Hide ONLY if CodeMark is not insine backticks (InlineCode) and the cursor is placed outside
|
|
|
|
if (
|
|
|
|
parent.node.name !== "InlineCode" &&
|
2022-12-09 15:09:53 +00:00
|
|
|
!isCursorInRange(state, [parent.from, parent.to])
|
2022-11-29 07:35:46 +00:00
|
|
|
) {
|
2022-11-18 15:04:37 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.line({
|
|
|
|
class: "sb-line-code-outside",
|
|
|
|
}).range(node.from),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return Decoration.set(widgets, true);
|
|
|
|
}
|
|
|
|
|
2022-12-09 15:09:53 +00:00
|
|
|
export function cleanBlockPlugin() {
|
|
|
|
return decoratorStateField(hideNodes);
|
|
|
|
}
|