1
0
silverbullet/web/cm_plugins/fenced_code.ts

170 lines
5.1 KiB
TypeScript
Raw Normal View History

2023-01-21 12:37:55 +00:00
import { WidgetContent } from "../../plug-api/app_event.ts";
2022-12-22 15:20:05 +00:00
import { Decoration, EditorState, syntaxTree, WidgetType } from "../deps.ts";
2023-07-14 14:56:20 +00:00
import type { Client } from "../client.ts";
2022-12-22 15:20:05 +00:00
import {
decoratorStateField,
invisibleDecoration,
isCursorInRange,
} from "./util.ts";
import { createWidgetSandboxIFrame } from "../components/widget_sandbox_iframe.ts";
2023-10-29 09:02:50 +00:00
import type { CodeWidgetCallback } from "$sb/types.ts";
2022-12-22 15:20:05 +00:00
class IFrameWidget extends WidgetType {
iframe?: HTMLIFrameElement;
2022-12-22 15:20:05 +00:00
constructor(
readonly from: number,
readonly to: number,
2023-11-03 11:01:33 +00:00
readonly client: Client,
2022-12-22 15:20:05 +00:00
readonly bodyText: string,
readonly codeWidgetCallback: CodeWidgetCallback,
) {
super();
}
toDOM(): HTMLElement {
2023-10-04 14:21:18 +00:00
const from = this.from;
const iframe = createWidgetSandboxIFrame(
2023-11-03 11:01:33 +00:00
this.client,
this.bodyText,
2023-11-03 11:01:33 +00:00
this.codeWidgetCallback(this.bodyText, this.client.currentPage!),
(message) => {
switch (message.type) {
case "blur":
2023-11-03 11:01:33 +00:00
this.client.editorView.dispatch({
2023-10-04 14:21:18 +00:00
selection: { anchor: from },
});
2023-11-03 11:01:33 +00:00
this.client.focus();
2022-12-22 15:20:05 +00:00
break;
case "reload":
2023-11-03 11:01:33 +00:00
this.codeWidgetCallback(this.bodyText, this.client.currentPage!)
2023-10-31 09:33:38 +00:00
.then(
(widgetContent: WidgetContent) => {
iframe.contentWindow!.postMessage({
type: "html",
html: widgetContent.html,
script: widgetContent.script,
theme:
document.getElementsByTagName("html")[0].dataset.theme,
});
},
);
break;
}
},
);
2022-12-22 15:20:05 +00:00
2023-11-16 08:59:37 +00:00
const estimatedHeight = this.estimatedHeight;
iframe.height = `${estimatedHeight}px`;
2023-10-03 12:34:07 +00:00
2022-12-22 15:20:05 +00:00
return iframe;
}
get estimatedHeight(): number {
2023-11-03 11:01:33 +00:00
const cachedHeight = this.client.space.getCachedWidgetHeight(this.bodyText);
2023-10-03 12:34:21 +00:00
// console.log("Calling estimated height", cachedHeight);
2023-10-03 12:34:07 +00:00
return cachedHeight > 0 ? cachedHeight : 150;
}
2022-12-22 15:20:05 +00:00
eq(other: WidgetType): boolean {
return (
other instanceof IFrameWidget &&
other.bodyText === this.bodyText
);
}
}
2023-07-14 14:56:20 +00:00
export function fencedCodePlugin(editor: Client) {
2022-12-22 15:20:05 +00:00
return decoratorStateField((state: EditorState) => {
const widgets: any[] = [];
syntaxTree(state).iterate({
enter({ from, to, name, node }) {
if (name === "FencedCode") {
if (isCursorInRange(state, [from, to])) return;
const text = state.sliceDoc(from, to);
const [_, lang] = text.match(/^```(\w+)?/)!;
2023-07-14 11:44:30 +00:00
const codeWidgetCallback = editor.system.codeWidgetHook
.codeWidgetCallbacks
2022-12-22 15:20:05 +00:00
.get(lang);
if (codeWidgetCallback) {
// We got a custom renderer!
const lineStrings = text.split("\n");
const lines: { from: number; to: number }[] = [];
let fromIt = from;
for (const line of lineStrings) {
lines.push({
from: fromIt,
to: fromIt + line.length,
});
fromIt += line.length + 1;
}
const firstLine = lines[0], lastLine = lines[lines.length - 1];
// In case of doubt, back out
if (!firstLine || !lastLine) return;
widgets.push(
invisibleDecoration.range(firstLine.from, firstLine.to),
);
widgets.push(
invisibleDecoration.range(lastLine.from, lastLine.to),
);
widgets.push(
Decoration.line({
class: "sb-fenced-code-iframe",
}).range(firstLine.from),
);
widgets.push(
Decoration.line({
class: "sb-fenced-code-hide",
}).range(lastLine.from),
);
lines.slice(1, lines.length - 1).forEach((line) => {
widgets.push(
Decoration.line({ class: "sb-line-table-outside" }).range(
line.from,
),
);
});
widgets.push(
Decoration.widget({
widget: new IFrameWidget(
from + lineStrings[0].length + 1,
to - lineStrings[lineStrings.length - 1].length - 1,
editor,
lineStrings.slice(1, lineStrings.length - 1).join("\n"),
codeWidgetCallback,
),
}).range(from),
);
return false;
}
return true;
}
if (
name === "CodeMark"
) {
const parent = node.parent!;
// Hide ONLY if CodeMark is not insine backticks (InlineCode) and the cursor is placed outside
if (
parent.node.name !== "InlineCode" &&
!isCursorInRange(state, [parent.from, parent.to])
) {
widgets.push(
Decoration.line({
class: "sb-line-code-outside",
2022-12-29 11:45:53 +00:00
}).range(state.doc.lineAt(from).from),
2022-12-22 15:20:05 +00:00
);
}
}
},
});
return Decoration.set(widgets, true);
});
}