2022-12-09 16:13:26 +00:00
|
|
|
import {
|
2022-12-19 10:50:48 +00:00
|
|
|
directiveEndRegex,
|
|
|
|
directiveStartRegex,
|
|
|
|
} from "../../plug-api/lib/query.ts";
|
|
|
|
import { Decoration, EditorState, syntaxTree } from "../deps.ts";
|
|
|
|
import { decoratorStateField, HtmlWidget, isCursorInRange } from "./util.ts";
|
2022-11-18 15:04:37 +00:00
|
|
|
|
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-11-18 15:04:37 +00:00
|
|
|
|
2022-12-09 16:13:26 +00:00
|
|
|
syntaxTree(state).iterate({
|
2022-12-14 19:04:20 +00:00
|
|
|
enter: ({ type, from, to, node }) => {
|
|
|
|
const parent = node.parent;
|
|
|
|
|
|
|
|
if (!parent) {
|
2022-12-09 16:13:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-14 19:04:20 +00:00
|
|
|
|
|
|
|
const cursorInRange = isCursorInRange(state, [parent.from, parent.to]);
|
|
|
|
|
|
|
|
if (type.name === "DirectiveStart") {
|
|
|
|
if (cursorInRange) {
|
|
|
|
// Cursor outside this directive
|
2022-12-09 16:13:26 +00:00
|
|
|
widgets.push(
|
2022-12-14 19:04:20 +00:00
|
|
|
Decoration.line({ class: "sb-directive-start" }).range(from),
|
2022-12-09 16:13:26 +00:00
|
|
|
);
|
2022-12-14 19:04:20 +00:00
|
|
|
} else {
|
2022-12-19 10:50:48 +00:00
|
|
|
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;
|
2022-12-19 10:50:48 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
|
|
|
widget: new HtmlWidget(
|
|
|
|
`#${directiveName}`,
|
|
|
|
"sb-directive-placeholder",
|
|
|
|
),
|
|
|
|
}).range(from),
|
|
|
|
);
|
2022-12-09 16:13:26 +00:00
|
|
|
widgets.push(
|
2022-12-19 10:50:48 +00:00
|
|
|
Decoration.line({
|
|
|
|
class: "sb-directive-start sb-directive-start-outside",
|
|
|
|
}).range(
|
|
|
|
from,
|
2022-12-14 19:04:20 +00:00
|
|
|
),
|
2022-12-09 16:13:26 +00:00
|
|
|
);
|
2022-12-14 19:04:20 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type.name === "DirectiveEnd") {
|
|
|
|
// Cursor outside this directive
|
|
|
|
if (cursorInRange) {
|
2022-12-09 16:13:26 +00:00
|
|
|
widgets.push(
|
2022-12-14 19:04:20 +00:00
|
|
|
Decoration.line({ class: "sb-directive-end" }).range(from),
|
2022-12-09 16:13:26 +00:00
|
|
|
);
|
2022-12-14 19:04:20 +00:00
|
|
|
} else {
|
2022-12-19 10:50:48 +00:00
|
|
|
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;
|
2022-12-19 10:50:48 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
|
|
|
widget: new HtmlWidget(
|
|
|
|
`/${directiveName}`,
|
|
|
|
"sb-directive-placeholder",
|
|
|
|
),
|
|
|
|
}).range(from),
|
|
|
|
);
|
2022-12-09 16:13:26 +00:00
|
|
|
widgets.push(
|
2022-12-19 10:50:48 +00:00
|
|
|
Decoration.line({
|
|
|
|
class: "sb-directive-end sb-directive-end-outside",
|
|
|
|
}).range(
|
|
|
|
from,
|
2022-12-14 19:04:20 +00:00
|
|
|
),
|
2022-12-09 16:13:26 +00:00
|
|
|
);
|
|
|
|
}
|
2022-12-14 19:04:20 +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;
|
|
|
|
}
|
2022-12-19 10:50:48 +00:00
|
|
|
return true;
|
2022-12-09 16:13:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2022-11-18 15:04:37 +00:00
|
|
|
|
2022-12-09 16:13:26 +00:00
|
|
|
return Decoration.set(widgets, true);
|
|
|
|
});
|
2022-12-09 15:09:53 +00:00
|
|
|
}
|