From e3a43d00622d307f07eb6323840cac7b288af88b Mon Sep 17 00:00:00 2001 From: Coki <92775570+HashCookie@users.noreply.github.com> Date: Sun, 15 Dec 2024 02:07:02 +0800 Subject: [PATCH] feat: improve the advanced editor of model edit page (#3427) --- web/src/IframeEditor.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/web/src/IframeEditor.js b/web/src/IframeEditor.js index 82f47b33..60a6760e 100644 --- a/web/src/IframeEditor.js +++ b/web/src/IframeEditor.js @@ -26,10 +26,12 @@ const IframeEditor = forwardRef(({initialModelText, onModelTextChange}, ref) => onModelTextChange(event.data.modelText); } else if (event.data.type === "iframeReady") { setIframeReady(true); - iframeRef.current?.contentWindow.postMessage({ - type: "initializeModel", - modelText: initialModelText, - }, "*"); + if (initialModelText && iframeRef.current?.contentWindow) { + iframeRef.current.contentWindow.postMessage({ + type: "initializeModel", + modelText: initialModelText, + }, "*"); + } } }; @@ -39,11 +41,15 @@ const IframeEditor = forwardRef(({initialModelText, onModelTextChange}, ref) => useImperativeHandle(ref, () => ({ getModelText: () => { - iframeRef.current?.contentWindow.postMessage({type: "getModelText"}, "*"); + if (iframeRef.current?.contentWindow) { + iframeRef.current.contentWindow.postMessage({ + type: "getModelText", + }, "*"); + } }, updateModelText: (newModelText) => { - if (iframeReady) { - iframeRef.current?.contentWindow.postMessage({ + if (iframeReady && iframeRef.current?.contentWindow) { + iframeRef.current.contentWindow.postMessage({ type: "updateModelText", modelText: newModelText, }, "*");