@mujian/js-sdk 0.0.6-beta.12 → 0.0.6-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/react.js +38 -5
- package/package.json +1 -1
package/dist/react.js
CHANGED
|
@@ -319,10 +319,46 @@ ${content}
|
|
|
319
319
|
</html>
|
|
320
320
|
`;
|
|
321
321
|
}
|
|
322
|
+
function adjustIframeHeight(iframe) {
|
|
323
|
+
if (!iframe.contentWindow) return;
|
|
324
|
+
const document1 = iframe.contentWindow.document;
|
|
325
|
+
const height = Math.max(document1.body.offsetHeight, document1.documentElement.offsetHeight);
|
|
326
|
+
if (!Number.isFinite(height) || height <= 0) return;
|
|
327
|
+
iframe.style.height = `${height}px`;
|
|
328
|
+
iframe.contentWindow.postMessage({
|
|
329
|
+
type: "MJ_UPDATE_VIEWPORT_HEIGHT"
|
|
330
|
+
}, "*");
|
|
331
|
+
}
|
|
332
|
+
const observed_elements = new Map();
|
|
333
|
+
const observer = new ResizeObserver((entries)=>{
|
|
334
|
+
for (const entry of entries){
|
|
335
|
+
const element = entry.target;
|
|
336
|
+
const iframe = observed_elements.get(element);
|
|
337
|
+
if (iframe) adjustIframeHeight(iframe);
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
function useHeightObserver() {
|
|
341
|
+
return {
|
|
342
|
+
observe: (iframe)=>{
|
|
343
|
+
if (!iframe?.contentWindow?.document?.body) return;
|
|
344
|
+
const body = iframe.contentWindow.document.body;
|
|
345
|
+
observed_elements.set(body, iframe);
|
|
346
|
+
observer.observe(body);
|
|
347
|
+
adjustIframeHeight(iframe);
|
|
348
|
+
},
|
|
349
|
+
unobserve: (iframe)=>{
|
|
350
|
+
if (!iframe.contentWindow?.document?.body) return;
|
|
351
|
+
const body = iframe.contentWindow.document.body;
|
|
352
|
+
observed_elements.delete(body);
|
|
353
|
+
observer.unobserve(body);
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
}
|
|
322
357
|
const MdRendererBase = ({ content })=>{
|
|
323
358
|
const containerRef = useRef(null);
|
|
324
359
|
useRef([]);
|
|
325
360
|
const iframeIdList = useRef([]);
|
|
361
|
+
const { observe, unobserve } = useHeightObserver();
|
|
326
362
|
useEffect(()=>{
|
|
327
363
|
let mes = messageFormatting(content);
|
|
328
364
|
mes = mes.replace(/<code(.*)>[\s\S]*?<\/code>/g, (match)=>{
|
|
@@ -350,19 +386,16 @@ const MdRendererBase = ({ content })=>{
|
|
|
350
386
|
const iframe = document.getElementById("MJ-iframe-" + id);
|
|
351
387
|
if (!iframe) return;
|
|
352
388
|
iframe.addEventListener("load", ()=>{
|
|
353
|
-
|
|
389
|
+
observe(iframe);
|
|
354
390
|
const spinElement = iframe.parentElement?.querySelector("#MJ-spin-" + id);
|
|
355
391
|
if (spinElement) spinElement.remove();
|
|
356
|
-
var iframeDoc = iframe.contentDocument || iframe.contentWindow?.document;
|
|
357
|
-
var height = iframeDoc?.body?.scrollHeight || 0;
|
|
358
|
-
iframe.style.height = height + "px";
|
|
359
392
|
});
|
|
360
393
|
});
|
|
361
394
|
return ()=>{
|
|
362
395
|
iframeIdList.current.forEach((id)=>{
|
|
363
396
|
const iframe = document.getElementById("MJ-iframe-" + id);
|
|
364
397
|
if (!iframe) return;
|
|
365
|
-
|
|
398
|
+
unobserve(iframe);
|
|
366
399
|
const spinElement = iframe.parentElement?.querySelector("#MJ-spin-" + iframe.id);
|
|
367
400
|
if (spinElement) spinElement.remove();
|
|
368
401
|
iframe.removeEventListener("load", ()=>{
|