From 788033cb5ffed93e5da0904137850d4601def758 Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Wed, 6 Aug 2025 02:23:07 -0700 Subject: [PATCH] Mobile: Fixes #12891: Fix error logged when opening the Markdown editor (#12892) --- .../markdownEditorBundle/useWebViewSetup.ts | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/packages/app-mobile/contentScripts/markdownEditorBundle/useWebViewSetup.ts b/packages/app-mobile/contentScripts/markdownEditorBundle/useWebViewSetup.ts index 24b0566e01..0d53e2701e 100644 --- a/packages/app-mobile/contentScripts/markdownEditorBundle/useWebViewSetup.ts +++ b/packages/app-mobile/contentScripts/markdownEditorBundle/useWebViewSetup.ts @@ -49,22 +49,32 @@ const useWebViewSetup = ({ const injectedJavaScript = useMemo(() => ` if (!window.cm) { - ${shim.injectedJs('markdownEditorBundle')}; - markdownEditorBundle.setUpLogger(); + const parentClassName = ${JSON.stringify(editorOptions.parentElementClassName)}; + const foundParent = document.getElementsByClassName(parentClassName).length > 0; - window.cm = markdownEditorBundle.initializeEditor( - ${JSON.stringify(editorOptions)} - ); + // On Android, injectedJavaScript can be run multiple times, including once before the + // document has loaded. To avoid logging an error each time the editor starts, don't throw + // if the parent element can't be found: + if (foundParent) { + ${shim.injectedJs('markdownEditorBundle')}; + markdownEditorBundle.setUpLogger(); - ${jumpToHashJs} - // Set the initial selection after jumping to the header -- the initial selection, - // if specified, should take precedence. - ${setInitialSelectionJs} - ${setInitialSearchJs} + window.cm = markdownEditorBundle.initializeEditor( + ${JSON.stringify(editorOptions)} + ); - window.onresize = () => { - cm.execCommand('scrollSelectionIntoView'); - }; + ${jumpToHashJs} + // Set the initial selection after jumping to the header -- the initial selection, + // if specified, should take precedence. + ${setInitialSelectionJs} + ${setInitialSearchJs} + + window.onresize = () => { + cm.execCommand('scrollSelectionIntoView'); + }; + } else { + console.log('No parent element found with class name ', parentClassName); + } } `, [jumpToHashJs, setInitialSearchJs, setInitialSelectionJs, editorOptions]);