File size: 3,594 Bytes
dd87944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useCallback, useEffect, useRef } from "react";
import { FileCode2, Globe } from "lucide-react";
import { basename } from "../lib/filePreview";
import { IFRAME_ALLOW, IFRAME_SANDBOX } from "../lib/iframePreview";

/** Mini-navigateur pour aperçu HTML live — chrome pro, iframe sandbox, mises à jour sans reload. */
export default function LiveHtmlPreview({ path = "", title, content = "", compact = false }) {
  const iframeRef = useRef(null);
  const lastWrittenRef = useRef("");
  const displayTitle = title || (path ? basename(path) : "Aperçu HTML");

  const writeContent = useCallback((html) => {
    const iframe = iframeRef.current;
    if (!iframe || html == null) return;
    if (html === lastWrittenRef.current) return;
    lastWrittenRef.current = html;
    try {
      const doc = iframe.contentDocument || iframe.contentWindow?.document;
      if (!doc) return;
      doc.open();
      doc.write(html);
      doc.close();
    } catch {
      /* sandbox / cross-origin — ignore */
    }
  }, []);

  useEffect(() => {
    writeContent(content || "");
  }, [content, writeContent]);

  const handleIframeLoad = () => {
    if (content && content !== lastWrittenRef.current) {
      writeContent(content);
    }
  };

  return (
    <div
      className="rounded-xl overflow-hidden"
      data-testid="live-html-preview"
      style={{
        background: "var(--emo-surface)",
        border: "1px solid var(--emo-border)",
        boxShadow: "0 2px 12px rgba(0,0,0,0.06)",
      }}
    >
      <div
        className="flex items-center gap-1.5 px-2 py-1.5"
        style={{
          borderBottom: "1px solid var(--emo-border)",
          background: "var(--emo-subtle-bg, var(--emo-surface))",
        }}
      >
        <FileCode2 size={12} className="flex-shrink-0" style={{ color: "var(--emo-text-muted)" }} />
        <div
          className="flex-1 min-w-0 flex items-center gap-1.5 px-2 py-0.5 rounded-full"
          style={{
            background: "var(--emo-surface)",
            border: "1px solid var(--emo-border)",
          }}
        >
          <Globe size={10} className="flex-shrink-0" style={{ color: "var(--emo-text-muted)" }} />
          <span
            className="flex-1 min-w-0 truncate text-[10px] font-code"
            style={{ color: "var(--emo-text)" }}
            title={path || displayTitle}
          >
            {displayTitle}
          </span>
        </div>
      </div>

      <div
        className="relative"
        style={{
          background: "var(--emo-preview-bg)",
          boxShadow: "inset 0 2px 8px rgba(0,0,0,0.08)",
        }}
      >
        <iframe
          ref={iframeRef}
          title={path || displayTitle}
          src="about:blank"
          onLoad={handleIframeLoad}
          className="w-full border-0 block"
          style={{
            height: compact ? 220 : 360,
            background: "#fff",
          }}
          sandbox={IFRAME_SANDBOX}
          allow={IFRAME_ALLOW}
          allowFullScreen
        />
      </div>

      <p
        className="text-[9px] px-2 py-1 flex items-center gap-1"
        style={{ color: "var(--emo-text-muted)", borderTop: "1px solid var(--emo-border)" }}
      >
        <span
          className="w-1.5 h-1.5 rounded-full flex-shrink-0 animate-pulse"
          style={{ background: "var(--mode-color)" }}
          aria-hidden
        />
        Aperçu HTML en direct
        {path && (
          <span className="truncate opacity-70 font-code" title={path}>
            · {path}
          </span>
        )}
      </p>
    </div>
  );
}