1
0
silverbullet/web/cm_plugins/top_bottom_panels.ts

74 lines
1.9 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 {
2023-11-25 13:31:39 +00:00
return this.editor.space.getCachedWidgetHeight(
2023-11-25 12:40:56 +00:00
this.widgetHeightCacheKey,
);
}
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[] = [];
if (editor.ui.viewState.panels.top.html) {
2023-11-25 12:40:56 +00:00
widgets.push(
Decoration.widget({
widget: new IFrameWidget(
editor,
editor.ui.viewState.panels.top,
"sb-top-iframe",
2023-11-25 12:40:56 +00:00
),
side: -1,
block: true,
}).range(0),
);
}
if (editor.ui.viewState.panels.bottom.html) {
widgets.push(
Decoration.widget({
widget: new IFrameWidget(
editor,
editor.ui.viewState.panels.bottom,
"sb-bottom-iframe",
),
side: 1,
block: true,
}).range(state.doc.length),
);
}
return Decoration.set(widgets);
});
}