2023-10-03 12:16:33 +00:00
|
|
|
import { Decoration, EditorState, WidgetType } from "../deps.ts";
|
|
|
|
import type { Client } from "../client.ts";
|
|
|
|
import { decoratorStateField } from "./util.ts";
|
|
|
|
import { PanelConfig } from "../types.ts";
|
|
|
|
import { createWidgetSandboxIFrame } from "../components/widget_sandbox_iframe.ts";
|
|
|
|
|
|
|
|
class IFrameWidget extends WidgetType {
|
2023-11-25 12:40:56 +00:00
|
|
|
widgetHeightCacheKey: string;
|
2023-10-03 12:16:33 +00:00
|
|
|
constructor(
|
|
|
|
readonly editor: Client,
|
|
|
|
readonly panel: PanelConfig,
|
2023-11-25 12:40:56 +00:00
|
|
|
readonly className: string,
|
2023-10-03 12:16:33 +00:00
|
|
|
) {
|
|
|
|
super();
|
2023-11-25 12:40:56 +00:00
|
|
|
this.widgetHeightCacheKey = `${this.editor.currentPage!}#${this.className}`;
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toDOM(): HTMLElement {
|
2023-11-25 12:40:56 +00:00
|
|
|
const iframe = createWidgetSandboxIFrame(
|
|
|
|
this.editor,
|
|
|
|
this.widgetHeightCacheKey,
|
|
|
|
this.panel,
|
|
|
|
);
|
|
|
|
iframe.classList.add(this.className);
|
2023-10-03 12:16:33 +00:00
|
|
|
return iframe;
|
|
|
|
}
|
|
|
|
|
2023-11-25 12:40:56 +00:00
|
|
|
get estimatedHeight(): number {
|
2023-11-25 13:31:39 +00:00
|
|
|
return this.editor.space.getCachedWidgetHeight(
|
2023-11-25 12:40:56 +00:00
|
|
|
this.widgetHeightCacheKey,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
eq(other: WidgetType): boolean {
|
|
|
|
return this.panel.html ===
|
|
|
|
(other as IFrameWidget).panel.html &&
|
|
|
|
this.panel.script ===
|
|
|
|
(other as IFrameWidget).panel.script;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 12:40:56 +00:00
|
|
|
export function postScriptPrefacePlugin(editor: Client) {
|
2023-10-03 12:16:33 +00:00
|
|
|
return decoratorStateField((state: EditorState) => {
|
|
|
|
const widgets: any[] = [];
|
2023-11-27 15:29:19 +00:00
|
|
|
if (editor.ui.viewState.panels.top.html) {
|
2023-11-25 12:40:56 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
|
|
|
widget: new IFrameWidget(
|
|
|
|
editor,
|
2023-11-27 15:29:19 +00:00
|
|
|
editor.ui.viewState.panels.top,
|
|
|
|
"sb-top-iframe",
|
2023-11-25 12:40:56 +00:00
|
|
|
),
|
|
|
|
side: -1,
|
|
|
|
block: true,
|
|
|
|
}).range(0),
|
|
|
|
);
|
|
|
|
}
|
2023-11-27 15:29:19 +00:00
|
|
|
if (editor.ui.viewState.panels.bottom.html) {
|
2023-10-03 12:16:33 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
|
|
|
widget: new IFrameWidget(
|
|
|
|
editor,
|
2023-11-27 15:29:19 +00:00
|
|
|
editor.ui.viewState.panels.bottom,
|
|
|
|
"sb-bottom-iframe",
|
2023-10-03 12:16:33 +00:00
|
|
|
),
|
|
|
|
side: 1,
|
|
|
|
block: true,
|
|
|
|
}).range(state.doc.length),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Decoration.set(widgets);
|
|
|
|
});
|
|
|
|
}
|