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 { CodeWidgetCallback } from "../hooks/code_widget.ts";
|
|
|
|
import {
|
|
|
|
decoratorStateField,
|
|
|
|
invisibleDecoration,
|
|
|
|
isCursorInRange,
|
|
|
|
} from "./util.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { createWidgetSandboxIFrame } from "../components/widget_sandbox_iframe.ts";
|
2022-12-22 15:20:05 +00:00
|
|
|
|
|
|
|
class IFrameWidget extends WidgetType {
|
2023-10-03 12:16:33 +00:00
|
|
|
iframe?: HTMLIFrameElement;
|
|
|
|
|
2022-12-22 15:20:05 +00:00
|
|
|
constructor(
|
|
|
|
readonly from: number,
|
|
|
|
readonly to: number,
|
2023-07-14 14:56:20 +00:00
|
|
|
readonly editor: Client,
|
2022-12-22 15:20:05 +00:00
|
|
|
readonly bodyText: string,
|
|
|
|
readonly codeWidgetCallback: CodeWidgetCallback,
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
toDOM(): HTMLElement {
|
2023-10-03 12:16:33 +00:00
|
|
|
const iframe = createWidgetSandboxIFrame(
|
|
|
|
this.editor,
|
|
|
|
this.bodyText,
|
|
|
|
this.codeWidgetCallback(this.bodyText),
|
|
|
|
(message) => {
|
|
|
|
switch (message.type) {
|
|
|
|
case "blur":
|
|
|
|
this.editor.editorView.dispatch({
|
|
|
|
selection: { anchor: this.from },
|
|
|
|
});
|
|
|
|
this.editor.focus();
|
2022-12-22 15:20:05 +00:00
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
break;
|
|
|
|
case "reload":
|
|
|
|
this.codeWidgetCallback(this.bodyText).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-10-03 12:34:07 +00:00
|
|
|
iframe.style.height = `${this.estimatedHeight}px`;
|
|
|
|
|
2022-12-22 15:20:05 +00:00
|
|
|
return iframe;
|
|
|
|
}
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
get estimatedHeight(): number {
|
|
|
|
const cachedHeight = this.editor.space.getCachedWidgetHeight(this.bodyText);
|
2023-10-03 12:34:07 +00:00
|
|
|
console.log("Calling estimated height", cachedHeight);
|
|
|
|
return cachedHeight > 0 ? cachedHeight : 150;
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|