file-operations.js (1.1 KB)


 1 import { saveToStorage } from './storage.js';
 2 import { setCurrentSpread, updateSpreadIndicator } from './spread-navigation.js';
 3 
 4 // File operations
 5 function downloadHtmlFile(content, filename = 'zine.html') {
 6 	const blob = new Blob([content], { type: 'text/html' });
 7 	const url = URL.createObjectURL(blob);
 8 	const a = document.createElement('a');
 9 	a.href = url;
10 	a.download = filename;
11 	a.click();
12 	URL.revokeObjectURL(url);
13 }
14 
15 export function saveFile(getEditorContent) {
16 	downloadHtmlFile(getEditorContent());
17 }
18 
19 export function loadFile(editorView, updatePreviewCallback) {
20 	const input = document.createElement('input');
21 	input.type = 'file';
22 	input.accept = '.html,.htm';
23 	input.onchange = function(event) {
24 		const file = event.target.files[0];
25 		if (!file) return;
26 
27 		const reader = new FileReader();
28 		reader.onload = function(e) {
29 			editorView.dispatch({
30 				changes: { from: 0, to: editorView.state.doc.length, insert: e.target.result }
31 			});
32 			saveToStorage(e.target.result);
33 			setCurrentSpread(0);
34 			updateSpreadIndicator();
35 			updatePreviewCallback();
36 		};
37 		reader.readAsText(file);
38 	};
39 	input.click();
40 }