一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
40 行
1.1 KiB
TypeScript
40 行
1.1 KiB
TypeScript
import { RefObject, useMemo, useRef } from 'react';
|
|
import { PostMessage } from '../types';
|
|
import useMessageHandler from './useMessageHandler';
|
|
|
|
type FormDataRecord = Record<string, unknown>;
|
|
type FormDataListener = (formData: FormDataRecord)=> void;
|
|
|
|
const useFormData = (viewRef: RefObject<HTMLIFrameElement>, postMessage: PostMessage) => {
|
|
const formDataListenersRef = useRef<FormDataListener[]>([]);
|
|
useMessageHandler(viewRef, (event) => {
|
|
if (event.data.message === 'serializedForms') {
|
|
const formData = event.data.formData;
|
|
if (typeof formData !== 'object') {
|
|
throw new Error('Invalid formData result.');
|
|
}
|
|
|
|
const listeners = [...formDataListenersRef.current];
|
|
formDataListenersRef.current = [];
|
|
for (const listener of listeners) {
|
|
listener(formData);
|
|
}
|
|
}
|
|
});
|
|
|
|
return useMemo(() => {
|
|
return {
|
|
getFormData: () => {
|
|
return new Promise<FormDataRecord>(resolve => {
|
|
postMessage('serializeForms', null);
|
|
formDataListenersRef.current.push((data) => {
|
|
resolve(data);
|
|
});
|
|
});
|
|
},
|
|
};
|
|
}, [postMessage]);
|
|
};
|
|
|
|
export default useFormData;
|