File size: 9,194 Bytes
76fc93a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import React, { useState, useEffect, useRef, useCallback, useMemo } from "react";
import { NodeViewWrapper } from "@tiptap/react";
import type { NodeViewProps } from "@tiptap/react";
import type { ComponentDef, ComponentField } from "../components/registry";
import { buildDoc, DEFAULT_EMBED_HEIGHT } from "./build-doc";
import type { EmbedStore } from "./embed-store";
import { useTheme } from "../../hooks/useTheme";

/**
 * Resolve embed store from the editor's storage.
 * The store is injected by Editor.tsx into editor.storage.htmlEmbed.embedStore.
 */
function useEmbedStore(editor: NodeViewProps["editor"]): EmbedStore | null {
  return (editor.storage.htmlEmbed as any)?.embedStore ?? null;
}

function FieldRow({
  field,
  value,
  onChange,
}: {
  field: ComponentField;
  value: unknown;
  onChange: (val: unknown) => void;
}) {
  if (field.type === "boolean") {
    return (
      <label className="embed-field-row embed-field-checkbox">
        <input
          type="checkbox"
          checked={!!value}
          onChange={(e) => onChange(e.target.checked)}
        />
        {field.label}
      </label>
    );
  }

  return (
    <div className="embed-field-row">
      <span className="embed-field-label">{field.label}</span>
      <input
        type="text"
        value={String(value ?? "")}
        placeholder={field.placeholder || field.label}
        onChange={(e) => onChange(e.target.value)}
        className="embed-field-input"
      />
    </div>
  );
}

/** Safely parse the stored height attribute (legacy: string; new: number). */
function parseStoredHeight(raw: unknown): number {
  if (typeof raw === "number" && raw > 0) return Math.round(raw);
  const n = parseInt(String(raw ?? ""), 10);
  return Number.isFinite(n) && n > 0 ? n : DEFAULT_EMBED_HEIGHT;
}

export function makeHtmlEmbedView(def: ComponentDef) {
  function HtmlEmbedNodeView({ node, updateAttributes, editor }: NodeViewProps) {
    const src = (node.attrs.src as string) || "";
    const title = (node.attrs.title as string) || "";
    const storedHeight = parseStoredHeight(node.attrs.height);
    const { isDark, primaryColor } = useTheme();
    const embedStore = useEmbedStore(editor);

    const [html, setHtml] = useState("");
    const [iframeHeight, setIframeHeight] = useState(storedHeight);
    const [showSettings, setShowSettings] = useState(false);
    const iframeRef = useRef<HTMLIFrameElement>(null);

    // Sync from embed store
    useEffect(() => {
      if (!embedStore || !src) return;
      setHtml(embedStore.get(src));
      return embedStore.observeKey(src, setHtml);
    }, [embedStore, src]);

    // Build full document for srcdoc.
    // NOTE: only depends on `html` – theme changes are pushed via postMessage
    // below so we don't reload the iframe (which would lose chart state).
    const srcdoc = useMemo(() => {
      if (!html) return "";
      return buildDoc(html, { isDark, primaryColor });
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [html]);

    // Hot-swap theme inside the iframe without reload.
    useEffect(() => {
      const frame = iframeRef.current;
      if (!frame || !html) return;
      const win = frame.contentWindow;
      if (!win) return;
      const send = () => {
        try {
          win.postMessage(
            {
              type: "setTheme",
              theme: isDark ? "dark" : "light",
              primaryColor,
            },
            "*",
          );
        } catch {
          /* cross-origin / not yet loaded – will retry on load */
        }
      };
      send();
      const onLoad = () => send();
      frame.addEventListener("load", onLoad);
      return () => frame.removeEventListener("load", onLoad);
    }, [isDark, primaryColor, html]);

    // Listen for height reports from the iframe.
    // - Apply immediately to the visible iframe (React state) for smooth resize.
    // - Debounce the PM transaction (node attribute) so document history isn't
    //   polluted with noisy updates during animations.
    const lastPersistedRef = useRef<number>(storedHeight);
    const persistTimerRef = useRef(0);
    useEffect(() => {
      const handler = (e: MessageEvent) => {
        if (e.data?.type !== "embedResize") return;
        const frame = iframeRef.current;
        if (!frame || e.source !== frame.contentWindow) return;

        const h = Math.max(0, Math.ceil(e.data.height));
        if (!h) return;

        setIframeHeight((prev) => (prev === h ? prev : h));

        if (h !== lastPersistedRef.current) {
          clearTimeout(persistTimerRef.current);
          persistTimerRef.current = window.setTimeout(() => {
            if (h !== lastPersistedRef.current) {
              lastPersistedRef.current = h;
              updateAttributes({ height: h });
            }
          }, 800);
        }
      };
      window.addEventListener("message", handler);
      return () => {
        window.removeEventListener("message", handler);
        clearTimeout(persistTimerRef.current);
      };
    }, [updateAttributes]);

    // Reset persisted tracker when the underlying node attribute changes from
    // the outside (undo/redo, collab remote update).
    useEffect(() => {
      lastPersistedRef.current = storedHeight;
    }, [storedHeight]);

    const handleFieldChange = useCallback(
      (fieldName: string, value: unknown) => {
        updateAttributes({ [fieldName]: value });
      },
      [updateAttributes],
    );

    const hasContent = !!html;

    // Metadata fields (exclude height - managed automatically from iframe)
    const editableFields = def.fields.filter((f) => f.name !== "height");

    return (
      <NodeViewWrapper data-component="htmlEmbed">
        <div contentEditable={false} className="embed-view">
          {/* Header */}
          <div className="embed-header">
            <div className="embed-header-left">
              <span className="embed-header-icon">📊</span>
              <span className="embed-header-label">
                {title || src || "HTML Embed"}
              </span>
              {src && title && (
                <span className="embed-header-src">{src}</span>
              )}
            </div>
            <div className="embed-header-actions">
              <button
                className="embed-btn"
                onClick={() => setShowSettings(!showSettings)}
                title="Settings"
              >
                {showSettings ? "Close" : "Settings"}
              </button>
              {hasContent && (
                <button
                  className="embed-btn embed-btn-primary"
                  onClick={() => {
                    window.dispatchEvent(
                      new CustomEvent("open-embed-studio", { detail: { src } }),
                    );
                  }}
                  title="Open Embed Studio"
                >
                  Edit
                </button>
              )}
            </div>
          </div>

          {/* Settings panel */}
          {showSettings && (
            <div className="embed-settings">
              {editableFields.map((f) => (
                <FieldRow
                  key={f.name}
                  field={f}
                  value={node.attrs[f.name]}
                  onChange={(v) => handleFieldChange(f.name, v)}
                />
              ))}
            </div>
          )}

          {/* Preview */}
          {hasContent ? (
            <div className="embed-preview">
              <iframe
                ref={iframeRef}
                srcDoc={srcdoc}
                title={title || src || "Chart preview"}
                sandbox="allow-scripts allow-same-origin"
                className="embed-iframe"
                style={{
                  height: iframeHeight,
                  minHeight: Math.min(storedHeight, 120),
                }}
              />
            </div>
          ) : (
            <div className="embed-empty">
              {src ? (
                <>
                  <span className="embed-empty-icon">📊</span>
                  <span>
                    No content for <code>{src}</code>
                  </span>
                  <button
                    className="embed-btn embed-btn-primary"
                    onClick={() => {
                      window.dispatchEvent(
                        new CustomEvent("open-embed-studio", { detail: { src } }),
                      );
                    }}
                  >
                    Create Chart
                  </button>
                </>
              ) : (
                <>
                  <span className="embed-empty-icon">📊</span>
                  <span>Set a source filename in settings to link an embed</span>
                  <button
                    className="embed-btn"
                    onClick={() => setShowSettings(true)}
                  >
                    Open Settings
                  </button>
                </>
              )}
            </div>
          )}
        </div>
      </NodeViewWrapper>
    );
  }

  HtmlEmbedNodeView.displayName = "HtmlEmbedView";
  return HtmlEmbedNodeView;
}