mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-18 01:19:24 +00:00
Fixes page editor loading
This commit is contained in:
parent
dc9acdd7cc
commit
aaa0eb4e0f
@ -2,6 +2,7 @@ import React, { createContext, useContext, useState, useCallback, useMemo } from
|
|||||||
import { useFileHandler } from '../hooks/useFileHandler';
|
import { useFileHandler } from '../hooks/useFileHandler';
|
||||||
import { useFileActions } from './FileContext';
|
import { useFileActions } from './FileContext';
|
||||||
import { StirlingFileStub } from '../types/fileContext';
|
import { StirlingFileStub } from '../types/fileContext';
|
||||||
|
import { fileStorage } from '../services/fileStorage';
|
||||||
|
|
||||||
interface FilesModalContextType {
|
interface FilesModalContextType {
|
||||||
isFilesModalOpen: boolean;
|
isFilesModalOpen: boolean;
|
||||||
@ -47,23 +48,34 @@ export const FilesModalProvider: React.FC<{ children: React.ReactNode }> = ({ ch
|
|||||||
closeFilesModal();
|
closeFilesModal();
|
||||||
}, [addFiles, closeFilesModal, insertAfterPage, customHandler]);
|
}, [addFiles, closeFilesModal, insertAfterPage, customHandler]);
|
||||||
|
|
||||||
const handleRecentFileSelect = useCallback((stirlingFileStubs: StirlingFileStub[]) => {
|
const handleRecentFileSelect = useCallback(async (stirlingFileStubs: StirlingFileStub[]) => {
|
||||||
if (customHandler) {
|
if (customHandler) {
|
||||||
// For custom handlers, we need to load the actual files first
|
// Load the actual files from storage for custom handler
|
||||||
// This is a bit complex - for now, let's not support custom handlers with stubs
|
try {
|
||||||
console.warn('Custom handlers not yet supported for StirlingFileStub selection');
|
const loadedFiles: File[] = [];
|
||||||
closeFilesModal();
|
for (const stub of stirlingFileStubs) {
|
||||||
return;
|
const stirlingFile = await fileStorage.getStirlingFile(stub.id);
|
||||||
|
if (stirlingFile) {
|
||||||
|
loadedFiles.push(stirlingFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the new addStirlingFileStubs action to preserve metadata
|
if (loadedFiles.length > 0) {
|
||||||
|
customHandler(loadedFiles, insertAfterPage);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load files for custom handler:', error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Normal case - use addStirlingFileStubs to preserve metadata
|
||||||
if (actions.addStirlingFileStubs) {
|
if (actions.addStirlingFileStubs) {
|
||||||
actions.addStirlingFileStubs(stirlingFileStubs, { selectFiles: true });
|
actions.addStirlingFileStubs(stirlingFileStubs, { selectFiles: true });
|
||||||
} else {
|
} else {
|
||||||
console.error('addStirlingFileStubs action not available');
|
console.error('addStirlingFileStubs action not available');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
closeFilesModal();
|
closeFilesModal();
|
||||||
}, [actions.addStirlingFileStubs, closeFilesModal, customHandler]);
|
}, [actions.addStirlingFileStubs, closeFilesModal, customHandler, insertAfterPage]);
|
||||||
|
|
||||||
const setModalCloseCallback = useCallback((callback: () => void) => {
|
const setModalCloseCallback = useCallback((callback: () => void) => {
|
||||||
setOnModalClose(() => callback);
|
setOnModalClose(() => callback);
|
||||||
|
@ -545,6 +545,31 @@ export async function addStirlingFileStubs(
|
|||||||
record.insertAfterPageId = options.insertAfterPageId;
|
record.insertAfterPageId = options.insertAfterPageId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if processedFile data needs regeneration for proper Page Editor support
|
||||||
|
if (stirlingFile.type.startsWith('application/pdf')) {
|
||||||
|
const needsProcessing = !record.processedFile ||
|
||||||
|
!record.processedFile.pages ||
|
||||||
|
record.processedFile.pages.length === 0 ||
|
||||||
|
record.processedFile.totalPages !== record.processedFile.pages.length;
|
||||||
|
|
||||||
|
if (needsProcessing) {
|
||||||
|
if (DEBUG) console.log(`📄 addStirlingFileStubs: Regenerating processedFile for ${record.name}`);
|
||||||
|
try {
|
||||||
|
// Generate basic processedFile structure with page count
|
||||||
|
const result = await generateThumbnailWithMetadata(stirlingFile);
|
||||||
|
record.processedFile = createProcessedFile(result.pageCount, result.thumbnail);
|
||||||
|
record.thumbnailUrl = result.thumbnail; // Update thumbnail if needed
|
||||||
|
if (DEBUG) console.log(`📄 addStirlingFileStubs: Regenerated processedFile for ${record.name} with ${result.pageCount} pages`);
|
||||||
|
} catch (error) {
|
||||||
|
if (DEBUG) console.warn(`📄 addStirlingFileStubs: Failed to regenerate processedFile for ${record.name}:`, error);
|
||||||
|
// Ensure we have at least basic structure
|
||||||
|
if (!record.processedFile) {
|
||||||
|
record.processedFile = createProcessedFile(1); // Fallback to 1 page
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
existingQuickKeys.add(stub.quickKey || '');
|
existingQuickKeys.add(stub.quickKey || '');
|
||||||
validStubs.push(record);
|
validStubs.push(record);
|
||||||
loadedFiles.push(stirlingFile);
|
loadedFiles.push(stirlingFile);
|
||||||
|
@ -4,9 +4,11 @@ import { useFileActions } from '../contexts/FileContext';
|
|||||||
export const useFileHandler = () => {
|
export const useFileHandler = () => {
|
||||||
const { actions } = useFileActions();
|
const { actions } = useFileActions();
|
||||||
|
|
||||||
const addFiles = useCallback(async (files: File[]) => {
|
const addFiles = useCallback(async (files: File[], options: { insertAfterPageId?: string; selectFiles?: boolean } = {}) => {
|
||||||
|
// Merge default options with passed options - passed options take precedence
|
||||||
|
const mergedOptions = { selectFiles: true, ...options };
|
||||||
// Let FileContext handle deduplication with quickKey logic
|
// Let FileContext handle deduplication with quickKey logic
|
||||||
await actions.addFiles(files, { selectFiles: true });
|
await actions.addFiles(files, mergedOptions);
|
||||||
}, [actions.addFiles]);
|
}, [actions.addFiles]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user