1
0
silverbullet/web/cm_plugins/preface_ps.ts

76 lines
2.0 KiB
TypeScript
Raw Normal View History

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;
constructor(
readonly editor: Client,
readonly panel: PanelConfig,
2023-11-25 12:40:56 +00:00
readonly className: string,
) {
super();
2023-11-25 12:40:56 +00:00
this.widgetHeightCacheKey = `${this.editor.currentPage!}#${this.className}`;
}
toDOM(): HTMLElement {
2023-11-25 12:40:56 +00:00
const iframe = createWidgetSandboxIFrame(
this.editor,
this.widgetHeightCacheKey,
this.panel,
);
iframe.classList.add(this.className);
return iframe;
}
2023-11-25 12:40:56 +00:00
get estimatedHeight(): number {
const height = this.editor.space.getCachedWidgetHeight(
this.widgetHeightCacheKey,
);
console.log("GOt height", height);
return height;
}
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) {
return decoratorStateField((state: EditorState) => {
const widgets: any[] = [];
2023-11-25 12:40:56 +00:00
if (editor.ui.viewState.panels.preface.html) {
widgets.push(
Decoration.widget({
widget: new IFrameWidget(
editor,
editor.ui.viewState.panels.preface,
"sb-preface-iframe",
),
side: -1,
block: true,
}).range(0),
);
}
if (editor.ui.viewState.panels.ps.html) {
widgets.push(
Decoration.widget({
widget: new IFrameWidget(
editor,
editor.ui.viewState.panels.ps,
2023-11-25 12:40:56 +00:00
"sb-ps-iframe",
),
side: 1,
block: true,
}).range(state.doc.length),
);
}
return Decoration.set(widgets);
});
}