Skip to content

Commit

Permalink
Merge pull request #240 from 10play/fix-dynamic-jitter
Browse files Browse the repository at this point in the history
fix: dynamic height jitter android
  • Loading branch information
17Amir17 authored Dec 15, 2024
2 parents 49f7ca3 + 040f89c commit 85fe5b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/RichText/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Platform } from 'react-native';
import type BridgeExtension from '../bridges/base';
import type { EditorBridge } from '../types';

Expand Down Expand Up @@ -70,6 +71,7 @@ export const getInjectedJSBeforeContentLoad = (editor: EditorBridge) => {
window.disableColorHighlight = ${!!editor.disableColorHighlight};
window.dynamicHeight = ${editor.dynamicHeight};
window.contentInjected = true;
window.platform = "${Platform.OS}";
`);
};

Expand Down
25 changes: 24 additions & 1 deletion src/webEditorUtils/useTenTap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare global {
whiteListBridgeExtensions: string[];
dynamicHeight?: boolean;
disableColorHighlight?: boolean;
platform?: 'ios' | 'android' | 'web';
ReactNativeWebView: { postMessage: (message: string) => void };
}
}
Expand Down Expand Up @@ -135,17 +136,39 @@ 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) => {
// 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,
});
}
sendMessage({
type: CoreEditorActionType.DocumentHeight,
payload: height,
});
}
);
}
}, [editor]);

return editor;
};

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;
};

0 comments on commit 85fe5b9

Please sign in to comment.