Fix flicker on apply

This commit is contained in:
Reece 2025-09-26 19:03:24 +01:00
parent 80faf0bc1e
commit b486d1270e
4 changed files with 32 additions and 16 deletions

View File

@ -124,6 +124,11 @@ function FileContextInner({
baseActions.unpinFile(file.fileId);
}, [baseActions]);
// Refresh file context - triggers re-render of file-dependent components
const refreshFileContext = useCallback(() => {
dispatch({ type: 'REFRESH_CONTEXT' });
}, []);
// Complete actions object
const actions = useMemo<FileContextActions>(() => ({
...baseActions,
@ -170,6 +175,7 @@ function FileContextInner({
}
}
},
refreshFileContext,
// Pinned files functionality with File object wrappers
pinFile: pinFileWrapper,
unpinFile: unpinFileWrapper,
@ -191,7 +197,8 @@ function FileContextInner({
pinFileWrapper,
unpinFileWrapper,
indexedDB,
enablePersistence
enablePersistence,
refreshFileContext
]);
// Split context values to minimize re-renders

View File

@ -282,6 +282,17 @@ export function fileContextReducer(state: FileContextState, action: FileContextA
return { ...initialFileContextState };
}
case 'REFRESH_CONTEXT': {
// Trigger re-render by returning new state object while preserving data
// This forces components to reload without losing file data
return {
...state,
ui: {
...state.ui
}
};
}
default:
return state;
}

View File

@ -16,7 +16,7 @@ const Sign = (props: BaseToolProps) => {
const { t } = useTranslation();
const { setWorkbench } = useNavigation();
const { setSignatureConfig, activateDrawMode, activateSignaturePlacementMode, deactivateDrawMode, updateDrawSettings, undo, redo, signatureApiRef, getImageData, setSignaturesApplied } = useSignature();
const { consumeFiles, selectors } = useFileContext();
const { consumeFiles, selectors, actions } = useFileContext();
const { exportActions, getScrollState } = useViewer();
// Track which signature mode was active for reactivation after save
@ -97,20 +97,16 @@ const Sign = (props: BaseToolProps) => {
// Mark signatures as applied
setSignaturesApplied(true);
// Force refresh the viewer to show the flattened PDF
// Refresh the file context to reload the flattened PDF in viewer
setTimeout(() => {
// Navigate away from viewer and back to force reload
setWorkbench('fileEditor');
setTimeout(() => {
setWorkbench('viewer');
actions.refreshFileContext();
// Reactivate the signature mode that was active before save
if (activeModeRef.current === 'draw') {
activateDrawMode();
} else if (activeModeRef.current === 'placement') {
handleSignaturePlacement();
}
}, 100);
// Reactivate the signature mode that was active before save
if (activeModeRef.current === 'draw') {
activateDrawMode();
} else if (activeModeRef.current === 'placement') {
handleSignaturePlacement();
}
}, 200);
} else {
console.error('Signature flattening failed');
@ -118,7 +114,7 @@ const Sign = (props: BaseToolProps) => {
} catch (error) {
console.error('Error saving signed document:', error);
}
}, [exportActions, base.selectedFiles, selectors, consumeFiles, signatureApiRef, getImageData, setWorkbench, activateDrawMode]);
}, [exportActions, base.selectedFiles, selectors, consumeFiles, signatureApiRef, getImageData, actions, activateDrawMode, handleSignaturePlacement, setSignaturesApplied]);
const getSteps = () => {
const steps = [];

View File

@ -250,7 +250,8 @@ export type FileContextAction =
| { type: 'SET_UNSAVED_CHANGES'; payload: { hasChanges: boolean } }
// Context management
| { type: 'RESET_CONTEXT' };
| { type: 'RESET_CONTEXT' }
| { type: 'REFRESH_CONTEXT' };
export interface FileContextActions {
// File management - lightweight actions only
@ -285,6 +286,7 @@ export interface FileContextActions {
// Context management
resetContext: () => void;
refreshFileContext: () => void;
// Resource management
trackBlobUrl: (url: string) => void;