1
0
silverbullet/web/cm_plugins/directive.ts

114 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-12-09 16:13:26 +00:00
import {
directiveEndRegex,
directiveStartRegex,
} from "../../plug-api/lib/query.ts";
import { Decoration, EditorState, syntaxTree } from "../deps.ts";
import { decoratorStateField, HtmlWidget, isCursorInRange } from "./util.ts";
2022-12-09 16:13:26 +00:00
// Does a few things: hides the directives when the cursor is not placed inside
// Adds a class to the start and end of the directive when the cursor is placed inside
2022-12-19 11:35:58 +00:00
export function directivePlugin() {
2022-12-09 16:13:26 +00:00
return decoratorStateField((state: EditorState) => {
const widgets: any[] = [];
2022-12-09 16:13:26 +00:00
syntaxTree(state).iterate({
enter: ({ type, from, to, node }) => {
const parent = node.parent;
if (!parent) {
2022-12-09 16:13:26 +00:00
return;
}
2023-09-09 19:06:15 +00:00
const cursorInRange = isCursorInRange(state, [from, to]);
if (type.name === "DirectiveStart") {
if (cursorInRange) {
// Cursor outside this directive
2022-12-09 16:13:26 +00:00
widgets.push(
Decoration.line({ class: "sb-directive-start" }).range(from),
2022-12-09 16:13:26 +00:00
);
} else {
const text = state.sliceDoc(from, to);
const match = directiveStartRegex.exec(text);
if (!match) {
console.error("Something went wrong with this directive");
return;
}
2022-12-19 11:35:58 +00:00
const [_fullMatch, directiveName] = match;
widgets.push(
Decoration.widget({
widget: new HtmlWidget(
`#${directiveName}`,
"sb-directive-placeholder",
),
}).range(from),
);
2022-12-09 16:13:26 +00:00
widgets.push(
Decoration.line({
class: "sb-directive-start sb-directive-start-outside",
attributes: {
spellcheck: "false",
},
}).range(
from,
),
2022-12-09 16:13:26 +00:00
);
}
return true;
}
if (type.name === "DirectiveEnd") {
// Cursor outside this directive
if (cursorInRange) {
2022-12-09 16:13:26 +00:00
widgets.push(
Decoration.line({ class: "sb-directive-end" }).range(from),
2022-12-09 16:13:26 +00:00
);
} else {
const text = state.sliceDoc(from, to);
const match = directiveEndRegex.exec(text);
if (!match) {
console.error("Something went wrong with this directive");
return;
}
2022-12-19 11:35:58 +00:00
const [_fullMatch, directiveName] = match;
widgets.push(
Decoration.widget({
widget: new HtmlWidget(
`/${directiveName}`,
"sb-directive-placeholder",
),
}).range(from),
);
2022-12-09 16:13:26 +00:00
widgets.push(
Decoration.line({
class: "sb-directive-end sb-directive-end-outside",
}).range(
from,
),
2022-12-09 16:13:26 +00:00
);
}
return true;
}
if (type.name === "DirectiveBody") {
const lines = state.sliceDoc(from, to).split("\n");
let pos = from;
for (const line of lines) {
if (pos !== to) {
widgets.push(
Decoration.line({
class: "sb-directive-body",
}).range(pos),
);
}
pos += line.length + 1;
}
return true;
2022-12-09 16:13:26 +00:00
}
},
});
2022-12-09 16:13:26 +00:00
return Decoration.set(widgets, true);
});
}