1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-16 00:14:34 +02:00

Chore: Refactor mobile plugin logic into locations more consistent with other parts of the app (#10636)

This commit is contained in:
Henry Heino
2024-06-25 05:59:59 -07:00
committed by GitHub
parent 801d36c41f
commit c7116b135f
34 changed files with 155 additions and 91 deletions

View File

@ -0,0 +1,52 @@
import useAsyncEffect from '@joplin/lib/hooks/useAsyncEffect';
import { useRef, useState } from 'react';
import { DialogContentSize, DialogWebViewApi } from '../../types';
import { PixelRatio } from 'react-native';
interface Props {
dialogControl: DialogWebViewApi;
webViewLoadCount: number;
watchForSizeChanges: boolean;
}
const useDialogSize = (props: Props) => {
const { dialogControl, webViewLoadCount } = props;
const [dialogSize, setDialogSize] = useState<DialogContentSize|null>(null);
const lastSizeRef = useRef(dialogSize);
lastSizeRef.current = dialogSize;
useAsyncEffect(async event => {
if (!dialogControl) {
// May happen if the webview is still loading.
return;
}
while (!event.cancelled) {
const contentSize = await dialogControl.getContentSize();
if (event.cancelled) return;
const lastSize = lastSizeRef.current;
if (contentSize.width !== lastSize?.width || contentSize.height !== lastSize?.height) {
// We use 1000 here because getPixelSizeForLayoutSize is guaranteed to return
// an integer.
const pixelToDpRatio = 1000 / PixelRatio.getPixelSizeForLayoutSize(1000);
setDialogSize({
width: contentSize.width * pixelToDpRatio,
height: contentSize.height * pixelToDpRatio,
});
}
if (!props.watchForSizeChanges) return;
await new Promise<void>(resolve => {
setTimeout(() => {
resolve();
}, 500);
});
}
}, [dialogControl, setDialogSize, webViewLoadCount]);
return dialogSize;
};
export default useDialogSize;