From 88118cf02ac88d08481ddbd2cc02b991c79e8d7e Mon Sep 17 00:00:00 2001 From: Amir Angel <36531255+17Amir17@users.noreply.github.com> Date: Sun, 15 Dec 2024 14:27:48 +0200 Subject: [PATCH 1/3] fix: dynamic height jitter android --- src/RichText/utils.ts | 2 ++ src/webEditorUtils/useTenTap.tsx | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/RichText/utils.ts b/src/RichText/utils.ts index aef8206..652c018 100644 --- a/src/RichText/utils.ts +++ b/src/RichText/utils.ts @@ -1,3 +1,4 @@ +import { Platform } from 'react-native'; import type BridgeExtension from '../bridges/base'; import type { EditorBridge } from '../types'; @@ -70,6 +71,7 @@ export const getInjectedJSBeforeContentLoad = (editor: EditorBridge) => { window.disableColorHighlight = ${!!editor.disableColorHighlight}; window.dynamicHeight = ${editor.dynamicHeight}; window.contentInjected = true; + window.platform = "${Platform.OS}"; `); }; diff --git a/src/webEditorUtils/useTenTap.tsx b/src/webEditorUtils/useTenTap.tsx index 5ffd0e9..28f21cf 100644 --- a/src/webEditorUtils/useTenTap.tsx +++ b/src/webEditorUtils/useTenTap.tsx @@ -16,6 +16,7 @@ declare global { whiteListBridgeExtensions: string[]; dynamicHeight?: boolean; disableColorHighlight?: boolean; + platform?: 'ios' | 'android' | 'web'; ReactNativeWebView: { postMessage: (message: string) => void }; } } @@ -135,17 +136,33 @@ export const useTenTap = (options?: useTenTapArgs) => { }, [editor, bridges]); useEffect(() => { - if (editor && !contentHeightListener.connected && window.dynamicHeight) + if (editor && !contentHeightListener.connected && window.dynamicHeight) { + const paragraphHeight = getParagraphHeight(); contentHeightListener.connect( document.querySelector('.ProseMirror')!, (height) => { sendMessage({ type: CoreEditorActionType.DocumentHeight, - payload: height, + payload: height + paragraphHeight, }); } ); + } }, [editor]); return editor; }; + +// This is a utility to get the height of a paragraph element +// This is needed on android to avoid an issue where text jumps https://github.com/10play/10tap-editor/issues/236 +const getParagraphHeight = () => { + if (window.platform !== 'android') return 0; + const tempParagraph = document.createElement('p'); + tempParagraph.style.visibility = 'hidden'; // Ensure it's not visible + tempParagraph.textContent = 'tmp'; + document.body.appendChild(tempParagraph); + + const pHeight = tempParagraph.getBoundingClientRect().height; + document.body.removeChild(tempParagraph); + return pHeight; +}; From 6eeaf12bd252dc4d61cb7fc1e48a9dddbef50ff4 Mon Sep 17 00:00:00 2001 From: Amir Angel <36531255+17Amir17@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:08:22 +0200 Subject: [PATCH 2/3] fix: reset to regular height --- src/webEditorUtils/useTenTap.tsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/webEditorUtils/useTenTap.tsx b/src/webEditorUtils/useTenTap.tsx index 28f21cf..fa58b70 100644 --- a/src/webEditorUtils/useTenTap.tsx +++ b/src/webEditorUtils/useTenTap.tsx @@ -141,9 +141,18 @@ export const useTenTap = (options?: useTenTapArgs) => { contentHeightListener.connect( document.querySelector('.ProseMirror')!, (height) => { + // On android we first need to send the height of the document + paragraph height + // to avoid an issue where text jumps https://github.com/10play/10tap-editor/issues/236 + if (window.platform === 'android') { + sendMessage({ + type: CoreEditorActionType.DocumentHeight, + payload: height + paragraphHeight, + }); + } + // document.body.style.overflow = 'hidden'; sendMessage({ type: CoreEditorActionType.DocumentHeight, - payload: height + paragraphHeight, + payload: height, }); } ); @@ -153,8 +162,6 @@ export const useTenTap = (options?: useTenTapArgs) => { return editor; }; -// This is a utility to get the height of a paragraph element -// This is needed on android to avoid an issue where text jumps https://github.com/10play/10tap-editor/issues/236 const getParagraphHeight = () => { if (window.platform !== 'android') return 0; const tempParagraph = document.createElement('p'); From 040f89c4f914a0a1b254bdb629d9cadc568c3960 Mon Sep 17 00:00:00 2001 From: Amir Angel <36531255+17Amir17@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:25:58 +0200 Subject: [PATCH 3/3] fix: cleanup --- src/webEditorUtils/useTenTap.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/webEditorUtils/useTenTap.tsx b/src/webEditorUtils/useTenTap.tsx index fa58b70..bc1983c 100644 --- a/src/webEditorUtils/useTenTap.tsx +++ b/src/webEditorUtils/useTenTap.tsx @@ -149,7 +149,6 @@ export const useTenTap = (options?: useTenTapArgs) => { payload: height + paragraphHeight, }); } - // document.body.style.overflow = 'hidden'; sendMessage({ type: CoreEditorActionType.DocumentHeight, payload: height,