Spaces:
Running
Running
File size: 7,997 Bytes
5f3e9f5 | 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | /**
* SettingsManager — Persistent settings via localStorage (#2) + input validation (#18)
*/
const SettingsManager = {
STORAGE_KEY: 'screenshotStudioSettings',
// Default settings for each tool
DEFAULTS: {
text: { zoom: 2.1, overlap: 20, viewportWidth: 1920, viewportHeight: 1080, maxScreenshots: 50 },
html: { zoom: 2.1, overlap: 20, viewportWidth: 1920, viewportHeight: 1080, maxScreenshots: 50 },
image: { zoom: 2.1, overlap: 20, viewportWidth: 1920, viewportHeight: 1080, maxScreenshots: 50 },
activeTool: 'text-to-image'
},
// Validation ranges (#18)
RANGES: {
zoom: { min: 0.5, max: 5, warn: 4 },
overlap: { min: 0, max: 200 },
viewportWidth: { min: 800, max: 3840, warn: 3000 },
viewportHeight: { min: 600, max: 2160 },
maxScreenshots: { min: 1, max: 100 }
},
/**
* Load all saved settings from localStorage.
*/
load() {
try {
const raw = localStorage.getItem(this.STORAGE_KEY);
if (raw) {
return { ...this.DEFAULTS, ...JSON.parse(raw) };
}
} catch (e) {
console.warn('Failed to load settings:', e);
}
return { ...this.DEFAULTS };
},
/**
* Save all settings to localStorage.
*/
save(settings) {
try {
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(settings));
} catch (e) {
console.warn('Failed to save settings:', e);
}
},
/**
* Save the active tool tab.
*/
saveActiveTool(toolId) {
const settings = this.load();
settings.activeTool = toolId;
this.save(settings);
},
/**
* Read settings from DOM inputs for a specific tool.
*/
readFromDOM(tool) {
const prefix = { text: 'text', html: 'html', image: 'image-' }[tool] || tool;
const isImage = tool === 'image';
const getId = (field) => {
if (isImage) return `image-${field.replace(/([A-Z])/g, '-$1').toLowerCase()}`;
return `${prefix}${field.charAt(0).toUpperCase() + field.slice(1)}`;
};
const idMap = {
text: { zoom: 'textZoom', overlap: 'textOverlap', viewportWidth: 'textViewportWidth', viewportHeight: 'textViewportHeight', maxScreenshots: 'textMaxScreenshots' },
html: { zoom: 'htmlZoom', overlap: 'htmlOverlap', viewportWidth: 'htmlViewportWidth', viewportHeight: 'htmlViewportHeight', maxScreenshots: 'htmlMaxScreenshots' },
image: { zoom: 'image-zoom', overlap: 'image-overlap', viewportWidth: 'image-viewport-width', viewportHeight: 'image-viewport-height', maxScreenshots: 'image-max-screenshots' }
};
const ids = idMap[tool];
if (!ids) return null;
const result = {};
for (const [key, id] of Object.entries(ids)) {
const el = document.getElementById(id);
if (el) result[key] = parseFloat(el.value);
}
return result;
},
/**
* Write settings to DOM inputs for a specific tool.
*/
writeToDOM(tool, values) {
const idMap = {
text: { zoom: 'textZoom', overlap: 'textOverlap', viewportWidth: 'textViewportWidth', viewportHeight: 'textViewportHeight', maxScreenshots: 'textMaxScreenshots' },
html: { zoom: 'htmlZoom', overlap: 'htmlOverlap', viewportWidth: 'htmlViewportWidth', viewportHeight: 'htmlViewportHeight', maxScreenshots: 'htmlMaxScreenshots' },
image: { zoom: 'image-zoom', overlap: 'image-overlap', viewportWidth: 'image-viewport-width', viewportHeight: 'image-viewport-height', maxScreenshots: 'image-max-screenshots' }
};
const ids = idMap[tool];
if (!ids || !values) return;
for (const [key, id] of Object.entries(ids)) {
const el = document.getElementById(id);
if (el && values[key] !== undefined) {
el.value = values[key];
}
}
},
/**
* Save current tool settings from DOM.
*/
saveToolSettings(tool) {
const settings = this.load();
const values = this.readFromDOM(tool);
if (values) {
settings[tool] = values;
this.save(settings);
}
},
/**
* Restore saved settings to DOM.
*/
restoreToolSettings(tool) {
const settings = this.load();
if (settings[tool]) {
this.writeToDOM(tool, settings[tool]);
}
},
/**
* Validate a single value and clamp to range (#18).
* Returns { value, warning } or null if ok.
*/
validate(field, value) {
const range = this.RANGES[field];
if (!range) return { value, warning: null };
const num = parseFloat(value);
if (isNaN(num)) return { value: range.min, warning: `Invalid value for ${field}` };
let warning = null;
let clamped = Math.max(range.min, Math.min(range.max, num));
if (clamped !== num) {
warning = `${field} clamped to ${clamped} (range: ${range.min}–${range.max})`;
} else if (range.warn && num > range.warn) {
warning = `High ${field} value (${num}) may slow down rendering`;
}
return { value: clamped, warning };
},
/**
* Validate all settings for a tool, clamp values, and show warnings (#18).
* Returns validated settings object.
*/
validateAll(tool) {
const values = this.readFromDOM(tool);
if (!values) return null;
const warnings = [];
const validated = {};
for (const [key, val] of Object.entries(values)) {
const result = this.validate(key, val);
validated[key] = result.value;
if (result.warning) warnings.push(result.warning);
}
// Write clamped values back to DOM
this.writeToDOM(tool, validated);
// Show warnings
if (warnings.length > 0 && typeof notificationManager !== 'undefined') {
notificationManager.warning('Settings Adjusted', warnings.join('. '));
}
return validated;
},
/**
* Initialize: restore all tool settings and set active tool.
*/
init() {
const settings = this.load();
// Restore settings for all tools
this.restoreToolSettings('text');
this.restoreToolSettings('html');
this.restoreToolSettings('image');
// Restore active tool
if (settings.activeTool && settings.activeTool !== 'text-to-image') {
const navItem = document.querySelector(`.nav-item[onclick*="${settings.activeTool}"]`);
if (navItem) {
navItem.click();
}
}
// Auto-save settings before generate
this._attachSaveListeners();
},
/**
* Listen for changes on settings inputs and auto-save.
*/
_attachSaveListeners() {
// Text settings
['textZoom', 'textOverlap', 'textViewportWidth', 'textViewportHeight', 'textMaxScreenshots'].forEach(id => {
const el = document.getElementById(id);
if (el) el.addEventListener('change', () => this.saveToolSettings('text'));
});
// HTML settings
['htmlZoom', 'htmlOverlap', 'htmlViewportWidth', 'htmlViewportHeight', 'htmlMaxScreenshots'].forEach(id => {
const el = document.getElementById(id);
if (el) el.addEventListener('change', () => this.saveToolSettings('html'));
});
// Image settings
['image-zoom', 'image-overlap', 'image-viewport-width', 'image-viewport-height', 'image-max-screenshots'].forEach(id => {
const el = document.getElementById(id);
if (el) el.addEventListener('change', () => this.saveToolSettings('image'));
});
}
};
// Initialize on DOM ready
document.addEventListener('DOMContentLoaded', () => {
SettingsManager.init();
});
|