|
1 | | -import { useEffect, useRef } from "react"; |
| 1 | +import { useCallback, useEffect, useRef } from "react"; |
2 | 2 | import { embedInSandcastleTemplate } from "./Helpers"; |
3 | 3 | import "./Bucket.css"; |
4 | 4 | import { ConsoleMessageType } from "./ConsoleMirror"; |
@@ -35,103 +35,106 @@ function Bucket({ |
35 | 35 | const bucket = useRef<HTMLIFrameElement>(null); |
36 | 36 | const lastRunNumber = useRef<number>(Number.NEGATIVE_INFINITY); |
37 | 37 |
|
38 | | - function activateBucketScripts( |
39 | | - bucketFrame: HTMLIFrameElement, |
40 | | - code: string, |
41 | | - html: string, |
42 | | - ) { |
43 | | - const bucketDoc = bucketFrame.contentDocument; |
44 | | - if (!bucketDoc) { |
45 | | - return; |
46 | | - } |
47 | | - |
48 | | - const headNodes = bucketDoc.head.childNodes; |
49 | | - let node; |
50 | | - const nodes: HTMLScriptElement[] = []; |
51 | | - let i, len; |
52 | | - for (i = 0, len = headNodes.length; i < len; ++i) { |
53 | | - node = headNodes[i]; |
54 | | - // header is included in blank frame. |
55 | | - if ( |
56 | | - node instanceof HTMLScriptElement && |
57 | | - node.src.indexOf("Sandcastle-header.js") < 0 && |
58 | | - node.src.indexOf("Cesium.js") < 0 |
59 | | - ) { |
60 | | - nodes.push(node); |
| 38 | + const activateBucketScripts = useCallback( |
| 39 | + function activateBucketScripts( |
| 40 | + bucketFrame: HTMLIFrameElement, |
| 41 | + code: string, |
| 42 | + html: string, |
| 43 | + ) { |
| 44 | + const bucketDoc = bucketFrame.contentDocument; |
| 45 | + if (!bucketDoc) { |
| 46 | + return; |
61 | 47 | } |
62 | | - } |
63 | | - |
64 | | - for (i = 0, len = nodes.length; i < len; ++i) { |
65 | | - bucketDoc.head.removeChild(nodes[i]); |
66 | | - } |
67 | 48 |
|
68 | | - // Apply user HTML to bucket. |
69 | | - const htmlElement = bucketDoc.createElement("div"); |
70 | | - htmlElement.innerHTML = html; |
71 | | - bucketDoc.body.appendChild(htmlElement); |
72 | | - |
73 | | - const onScriptTagError = function () { |
74 | | - if (bucketFrame.contentDocument === bucketDoc) { |
75 | | - // @ts-expect-error this has type any because it's from anywhere inside the bucket |
76 | | - appendConsole("error", `Error loading ${this.src}`); |
77 | | - appendConsole( |
78 | | - "error", |
79 | | - "Make sure Cesium is built, see the Contributor's Guide for details.", |
80 | | - ); |
| 49 | + const headNodes = bucketDoc.head.childNodes; |
| 50 | + let node; |
| 51 | + const nodes: HTMLScriptElement[] = []; |
| 52 | + let i, len; |
| 53 | + for (i = 0, len = headNodes.length; i < len; ++i) { |
| 54 | + node = headNodes[i]; |
| 55 | + // header is included in blank frame. |
| 56 | + if ( |
| 57 | + node instanceof HTMLScriptElement && |
| 58 | + node.src.indexOf("Sandcastle-header.js") < 0 && |
| 59 | + node.src.indexOf("Cesium.js") < 0 |
| 60 | + ) { |
| 61 | + nodes.push(node); |
| 62 | + } |
81 | 63 | } |
82 | | - }; |
83 | 64 |
|
84 | | - // Load each script after the previous one has loaded. |
85 | | - const loadScript = function () { |
86 | | - if (bucketFrame.contentDocument !== bucketDoc) { |
87 | | - // A newer reload has happened, abort this. |
88 | | - return; |
| 65 | + for (i = 0, len = nodes.length; i < len; ++i) { |
| 66 | + bucketDoc.head.removeChild(nodes[i]); |
89 | 67 | } |
90 | | - if (nodes.length > 0) { |
91 | | - while (nodes.length > 0) { |
92 | | - node = nodes.shift(); |
93 | | - if (!node) { |
94 | | - continue; |
95 | | - } |
96 | | - const scriptElement = bucketDoc.createElement("script"); |
97 | | - let hasSrc = false; |
98 | | - for ( |
99 | | - let j = 0, numAttrs = node.attributes.length; |
100 | | - j < numAttrs; |
101 | | - ++j |
102 | | - ) { |
103 | | - const name = node.attributes[j].name; |
104 | | - const val = node.attributes[j].value; |
105 | | - scriptElement.setAttribute(name, val); |
106 | | - if (name === "src" && val) { |
107 | | - hasSrc = true; |
| 68 | + |
| 69 | + // Apply user HTML to bucket. |
| 70 | + const htmlElement = bucketDoc.createElement("div"); |
| 71 | + htmlElement.innerHTML = html; |
| 72 | + bucketDoc.body.appendChild(htmlElement); |
| 73 | + |
| 74 | + const onScriptTagError = function () { |
| 75 | + if (bucketFrame.contentDocument === bucketDoc) { |
| 76 | + // @ts-expect-error this has type any because it's from anywhere inside the bucket |
| 77 | + appendConsole("error", `Error loading ${this.src}`); |
| 78 | + appendConsole( |
| 79 | + "error", |
| 80 | + "Make sure Cesium is built, see the Contributor's Guide for details.", |
| 81 | + ); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + // Load each script after the previous one has loaded. |
| 86 | + const loadScript = function () { |
| 87 | + if (bucketFrame.contentDocument !== bucketDoc) { |
| 88 | + // A newer reload has happened, abort this. |
| 89 | + return; |
| 90 | + } |
| 91 | + if (nodes.length > 0) { |
| 92 | + while (nodes.length > 0) { |
| 93 | + node = nodes.shift(); |
| 94 | + if (!node) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + const scriptElement = bucketDoc.createElement("script"); |
| 98 | + let hasSrc = false; |
| 99 | + for ( |
| 100 | + let j = 0, numAttrs = node.attributes.length; |
| 101 | + j < numAttrs; |
| 102 | + ++j |
| 103 | + ) { |
| 104 | + const name = node.attributes[j].name; |
| 105 | + const val = node.attributes[j].value; |
| 106 | + scriptElement.setAttribute(name, val); |
| 107 | + if (name === "src" && val) { |
| 108 | + hasSrc = true; |
| 109 | + } |
| 110 | + } |
| 111 | + scriptElement.innerHTML = node.innerHTML; |
| 112 | + if (hasSrc) { |
| 113 | + scriptElement.onload = loadScript; |
| 114 | + scriptElement.onerror = onScriptTagError; |
| 115 | + bucketDoc.head.appendChild(scriptElement); |
| 116 | + } else { |
| 117 | + bucketDoc.head.appendChild(scriptElement); |
| 118 | + loadScript(); |
108 | 119 | } |
109 | 120 | } |
110 | | - scriptElement.innerHTML = node.innerHTML; |
111 | | - if (hasSrc) { |
112 | | - scriptElement.onload = loadScript; |
113 | | - scriptElement.onerror = onScriptTagError; |
114 | | - bucketDoc.head.appendChild(scriptElement); |
115 | | - } else { |
116 | | - bucketDoc.head.appendChild(scriptElement); |
117 | | - loadScript(); |
118 | | - } |
119 | | - } |
120 | | - } else { |
121 | | - // Apply user JS to bucket |
122 | | - const element = bucketDoc.createElement("script"); |
123 | | - element.type = "module"; |
| 121 | + } else { |
| 122 | + // Apply user JS to bucket |
| 123 | + const element = bucketDoc.createElement("script"); |
| 124 | + element.type = "module"; |
124 | 125 |
|
125 | | - // Firefox line numbers are zero-based, not one-based. |
126 | | - const isFirefox = navigator.userAgent.indexOf("Firefox/") >= 0; |
| 126 | + // Firefox line numbers are zero-based, not one-based. |
| 127 | + const isFirefox = navigator.userAgent.indexOf("Firefox/") >= 0; |
127 | 128 |
|
128 | | - element.textContent = embedInSandcastleTemplate(code, isFirefox); |
129 | | - bucketDoc.body.appendChild(element); |
130 | | - } |
131 | | - }; |
| 129 | + element.textContent = embedInSandcastleTemplate(code, isFirefox); |
| 130 | + bucketDoc.body.appendChild(element); |
| 131 | + } |
| 132 | + }; |
132 | 133 |
|
133 | | - loadScript(); |
134 | | - } |
| 134 | + loadScript(); |
| 135 | + }, |
| 136 | + [appendConsole], |
| 137 | + ); |
135 | 138 |
|
136 | 139 | useEffect(() => { |
137 | 140 | if ( |
@@ -197,7 +200,14 @@ function Bucket({ |
197 | 200 | }; |
198 | 201 | window.addEventListener("message", messageHandler); |
199 | 202 | return () => window.removeEventListener("message", messageHandler); |
200 | | - }, [code, html, highlightLine, resetConsole, appendConsole]); |
| 203 | + }, [ |
| 204 | + code, |
| 205 | + html, |
| 206 | + highlightLine, |
| 207 | + resetConsole, |
| 208 | + appendConsole, |
| 209 | + activateBucketScripts, |
| 210 | + ]); |
201 | 211 |
|
202 | 212 | return ( |
203 | 213 | <div className="bucket-container"> |
|
0 commit comments