Spaces:
Sleeping
Sleeping
File size: 3,816 Bytes
aeb61fc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import { PDFLibrary } from './PDFLibrary.js';
/**
* CommonPdfImport - Bridge between PDFLibrary and ColorRM apps
* Handles PDF selection/import for both main and split view apps
*/
export const CommonPdfImport = {
target: null, // 'main' | 'split'
app: null,
splitViewApp: null,
/**
* Show PDF library for target app
* @param {'main'|'split'} target - Which app to import into
*/
showLibrary(target = 'main') {
this.target = target;
PDFLibrary.show(async (pdf) => {
await this.importPdf(pdf);
});
},
/**
* Import selected PDF into target app
*/
async importPdf(pdf) {
if (!pdf || !pdf.blob) {
console.error('CommonPdfImport: Invalid PDF entry');
return;
}
// Create a File object from the blob for handleImport
const file = new File([pdf.blob], pdf.name + '.pdf', { type: 'application/pdf' });
if (this.target === 'split') {
// Import into split view
const splitApp = this.splitViewApp || window.SplitView?.app;
if (splitApp) {
await this.importIntoApp(splitApp, file, pdf.name);
} else {
console.warn('CommonPdfImport: Split view app not available');
if (window.UI?.showToast) window.UI.showToast('Split View is not ready');
}
} else {
// Import into main app
const mainApp = this.app || window.App;
if (mainApp && mainApp.handleImport) {
await mainApp.handleImport({ target: { files: [file] } }, false);
} else {
console.warn('CommonPdfImport: Main app not available');
if (window.UI?.showToast) window.UI.showToast('Main app is not ready');
}
}
},
/**
* Import PDF into a specific ColorRmApp instance
*/
async importIntoApp(app, file, name) {
if (!app) return;
try {
// Create a new session for this PDF
const projectId = `sv_${Date.now()}`;
// Create session in app's database
await app.dbPut('sessions', {
id: projectId,
name: name,
pageCount: 0,
lastMod: Date.now(),
ownerId: 'local',
idx: 0,
bookmarks: [],
clipboardBox: [],
state: null
});
// Set as current session
app.state.sessionId = projectId;
app.state.projectName = name;
// Import the PDF
await app.importBaseFile(file);
// Fix: Save metadata to SplitView's project list so it appears in "Local Projects"
if (window.SplitView && window.SplitView.app === app) {
await window.SplitView.saveProjectMeta(projectId, name);
console.log('CommonPdfImport: Saved to SplitView project list');
}
console.log('CommonPdfImport: Imported into app:', name);
} catch (error) {
console.error('CommonPdfImport: Import failed:', error);
if (window.UI?.showToast) window.UI.showToast('Import failed: ' + error.message);
}
},
/**
* Initialize with app references
*/
init(mainApp) {
if (mainApp) {
this.app = mainApp;
}
window.CommonPdfImport = this;
window.PDFLibrary = PDFLibrary;
},
/**
* Set split view app reference
*/
setSplitViewApp(app) {
this.splitViewApp = app;
},
// Legacy methods for backwards compatibility
show() {
this.showLibrary('main');
},
pick(target) {
this.showLibrary(target);
}
};
|