File size: 27,395 Bytes
70ceb8b | 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 | // CustomPresetsManager.js - Manages custom user presets with persistence
import { createModuleLogger } from "./LoggerUtils.js";
const log = createModuleLogger('CustomPresetsManager');
export class CustomPresetsManager {
constructor(resolutionMasterInstance) {
this.rm = resolutionMasterInstance;
this.customPresets = {};
this.hiddenBuiltInPresets = {};
this.loadCustomPresets();
}
/**
* Loads custom presets from node properties
*/
loadCustomPresets() {
try {
const props = this.rm.node.properties;
if (props.customPresetsJSON && typeof props.customPresetsJSON === 'string') {
const data = JSON.parse(props.customPresetsJSON);
// Support both old and new format
if (data.customPresets) {
// New format with hiddenBuiltInPresets
this.customPresets = data.customPresets || {};
this.hiddenBuiltInPresets = data.hiddenBuiltInPresets || {};
} else {
// Old format - just custom presets
this.customPresets = data;
this.hiddenBuiltInPresets = {};
}
// Clean up empty categories
this.cleanEmptyCategories();
log.debug('Loaded custom presets:', this.customPresets);
log.debug('Loaded hidden built-in presets:', this.hiddenBuiltInPresets);
} else {
this.customPresets = {};
this.hiddenBuiltInPresets = {};
log.debug('No custom presets found, initialized empty');
}
} catch (error) {
log.error('Error loading custom presets:', error);
this.customPresets = {};
this.hiddenBuiltInPresets = {};
}
}
/**
* Saves custom presets to node properties
*/
saveCustomPresets() {
try {
// Clean up empty categories before saving
this.cleanEmptyCategories();
const data = {
customPresets: this.customPresets,
hiddenBuiltInPresets: this.hiddenBuiltInPresets
};
this.rm.node.properties.customPresetsJSON = JSON.stringify(data);
log.debug('Saved custom presets and hidden built-in presets to node properties');
} catch (error) {
log.error('Error saving custom presets:', error);
}
}
/**
* Removes empty categories from customPresets
*/
cleanEmptyCategories() {
Object.keys(this.customPresets).forEach(category => {
if (!this.customPresets[category] ||
typeof this.customPresets[category] !== 'object' ||
Object.keys(this.customPresets[category]).length === 0) {
delete this.customPresets[category];
log.debug(`Removed empty category: ${category}`);
}
});
}
/**
* Gets all custom presets
* @returns {Object} Custom presets object
*/
getCustomPresets() {
return this.customPresets;
}
/**
* Gets merged presets (built-in + custom)
* Custom presets OVERRIDE built-in presets with the same name
* Hidden built-in presets are FILTERED OUT
* @param {Object} builtInPresets - Built-in preset categories
* @returns {Object} Merged presets with custom indicator and hidden flag
*/
getMergedPresets(builtInPresets) {
// Start with deep copy of built-in presets
const merged = {};
// First, copy all built-in presets (including hidden ones, but mark them)
Object.entries(builtInPresets).forEach(([categoryName, presets]) => {
merged[categoryName] = {};
Object.entries(presets).forEach(([presetName, dimensions]) => {
// Mark as hidden if in hiddenBuiltInPresets list
const isHidden = this.isBuiltInPresetHidden(categoryName, presetName);
merged[categoryName][presetName] = {
width: dimensions.width,
height: dimensions.height,
isCustom: false,
isHidden: isHidden
};
});
});
// Then, add/override with custom presets
Object.entries(this.customPresets).forEach(([categoryName, presets]) => {
if (!merged[categoryName]) {
// New custom category
merged[categoryName] = {};
}
Object.entries(presets).forEach(([presetName, dimensions]) => {
// This will override built-in preset if it exists
merged[categoryName][presetName] = {
width: dimensions.width,
height: dimensions.height,
isCustom: true,
isHidden: false,
originalCategory: categoryName
};
});
});
return merged;
}
/**
* Adds a new preset or updates existing one
* @param {string} category - Category name
* @param {string} name - Preset name
* @param {number} width - Width value
* @param {number} height - Height value
* @returns {boolean} Success status
*/
addPreset(category, name, width, height) {
try {
if (!this.customPresets[category]) {
this.customPresets[category] = {};
}
this.customPresets[category][name] = {
width: parseInt(width),
height: parseInt(height)
};
this.saveCustomPresets();
log.debug(`Added/Updated preset: ${category}/${name} (${width}x${height})`);
return true;
} catch (error) {
log.error('Error adding preset:', error);
return false;
}
}
/**
* Deletes a preset
* @param {string} category - Category name
* @param {string} name - Preset name
* @returns {boolean} Success status
*/
deletePreset(category, name) {
try {
if (this.customPresets[category] && this.customPresets[category][name]) {
delete this.customPresets[category][name];
// Remove category if empty
if (Object.keys(this.customPresets[category]).length === 0) {
delete this.customPresets[category];
}
this.saveCustomPresets();
log.debug(`Deleted preset: ${category}/${name}`);
return true;
}
return false;
} catch (error) {
log.error('Error deleting preset:', error);
return false;
}
}
/**
* Renames a preset
* @param {string} category - Category name
* @param {string} oldName - Old preset name
* @param {string} newName - New preset name
* @returns {boolean} Success status
*/
renamePreset(category, oldName, newName) {
try {
if (this.customPresets[category] && this.customPresets[category][oldName]) {
const preset = this.customPresets[category][oldName];
this.customPresets[category][newName] = preset;
delete this.customPresets[category][oldName];
this.saveCustomPresets();
log.debug(`Renamed preset: ${category}/${oldName} -> ${newName}`);
return true;
}
return false;
} catch (error) {
log.error('Error renaming preset:', error);
return false;
}
}
/**
* Updates a preset (name and/or dimensions) while preserving its position in the list
* @param {string} category - Category name
* @param {string} oldName - Current preset name
* @param {string} newName - New preset name
* @param {number} width - New width value
* @param {number} height - New height value
* @returns {boolean} Success status
*/
updatePreset(category, oldName, newName, width, height) {
try {
if (!this.customPresets[category] || !this.customPresets[category][oldName]) {
log.warn(`Preset ${category}/${oldName} does not exist`);
return false;
}
// If both name and dimensions are the same, no update needed
const oldPreset = this.customPresets[category][oldName];
if (oldName === newName &&
oldPreset.width === parseInt(width) &&
oldPreset.height === parseInt(height)) {
return true;
}
// Check if new name already exists (and it's not the same preset)
if (oldName !== newName && this.customPresets[category][newName]) {
log.warn(`Preset ${category}/${newName} already exists`);
return false;
}
// Convert category presets to array of entries to preserve order
const entries = Object.entries(this.customPresets[category]);
// Find the index of the old preset
const index = entries.findIndex(([name]) => name === oldName);
if (index === -1) {
log.warn(`Preset ${category}/${oldName} not found in entries`);
return false;
}
// Replace the entry with updated preset
entries[index] = [newName, {
width: parseInt(width),
height: parseInt(height)
}];
// Rebuild the category object from entries (preserves order)
this.customPresets[category] = Object.fromEntries(entries);
this.saveCustomPresets();
log.debug(`Updated preset: ${category}/${oldName} -> ${newName} (${width}x${height})`);
return true;
} catch (error) {
log.error('Error updating preset:', error);
return false;
}
}
/**
* Renames a category while preserving its position in the list
* @param {string} oldName - Current category name
* @param {string} newName - New category name
* @returns {boolean} Success status
*/
renameCategory(oldName, newName) {
// Validate input
const trimmedNewName = newName.trim();
if (!trimmedNewName) {
log.warn('New category name cannot be empty');
return false;
}
// Check if old category exists
if (!this.customPresets[oldName]) {
log.warn(`Category ${oldName} does not exist`);
return false;
}
// Check if new name already exists (and it's not the same category)
if (oldName !== trimmedNewName && this.customPresets[trimmedNewName]) {
log.warn(`Category ${trimmedNewName} already exists`);
return false;
}
// If names are the same (after trim), no change needed
if (oldName === trimmedNewName) {
return true;
}
// Convert customPresets to array of entries to preserve order
const entries = Object.entries(this.customPresets);
// Find the index of the old category
const index = entries.findIndex(([categoryName]) => categoryName === oldName);
if (index === -1) {
log.warn(`Category ${oldName} not found in entries`);
return false;
}
// Replace the entry with new category name (keeping the same presets)
entries[index] = [trimmedNewName, entries[index][1]];
// Rebuild the customPresets object from entries (preserves order)
this.customPresets = Object.fromEntries(entries);
this.saveCustomPresets();
log.debug(`Renamed category: ${oldName} -> ${trimmedNewName}`);
return true;
}
/**
* Duplicates a preset (works with both built-in and custom)
* @param {string} sourceCategory - Source category name
* @param {string} sourceName - Source preset name
* @param {string} targetCategory - Target category name
* @param {string} targetName - Target preset name
* @param {Object} builtInPresets - Built-in presets to check
* @returns {boolean} Success status
*/
duplicatePreset(sourceCategory, sourceName, targetCategory, targetName, builtInPresets) {
try {
let sourcePreset = null;
// Check custom presets first
if (this.customPresets[sourceCategory] && this.customPresets[sourceCategory][sourceName]) {
sourcePreset = this.customPresets[sourceCategory][sourceName];
}
// Check built-in presets
else if (builtInPresets[sourceCategory] && builtInPresets[sourceCategory][sourceName]) {
sourcePreset = builtInPresets[sourceCategory][sourceName];
}
if (sourcePreset) {
return this.addPreset(targetCategory, targetName, sourcePreset.width, sourcePreset.height);
}
return false;
} catch (error) {
log.error('Error duplicating preset:', error);
return false;
}
}
/**
* Exports custom presets and hidden built-in presets to JSON string
* @returns {string} JSON string of custom presets and hidden built-in presets
*/
exportToJSON() {
try {
const data = {
customPresets: this.customPresets,
hiddenBuiltInPresets: this.hiddenBuiltInPresets
};
return JSON.stringify(data, null, 2);
} catch (error) {
log.error('Error exporting presets:', error);
return null;
}
}
/**
* Imports presets from JSON string
* Supports both old format (direct presets) and new format (with customPresets and hiddenBuiltInPresets)
* @param {string} jsonString - JSON string to import
* @param {boolean} merge - If true, merge with existing. If false, replace.
* @returns {boolean} Success status
*/
importFromJSON(jsonString, merge = true) {
try {
const imported = JSON.parse(jsonString);
// Validate structure
if (typeof imported !== 'object' || imported === null) {
throw new Error('Invalid preset format');
}
// Detect format: new format has 'customPresets' field, old format doesn't
let presetsToImport;
let hiddenToImport = {};
if (imported.customPresets) {
// New format with customPresets and hiddenBuiltInPresets
presetsToImport = imported.customPresets;
hiddenToImport = imported.hiddenBuiltInPresets || {};
log.debug('Importing new format (with hiddenBuiltInPresets)');
} else {
// Old format - direct presets object
presetsToImport = imported;
log.debug('Importing old format (without hiddenBuiltInPresets)');
}
// Validate each preset
for (const [category, presets] of Object.entries(presetsToImport)) {
if (typeof presets !== 'object') {
throw new Error(`Invalid category format: ${category}`);
}
for (const [name, dimensions] of Object.entries(presets)) {
if (!dimensions.width || !dimensions.height ||
typeof dimensions.width !== 'number' ||
typeof dimensions.height !== 'number') {
throw new Error(`Invalid preset dimensions: ${category}/${name}`);
}
}
}
if (merge) {
// Merge with existing presets
Object.entries(presetsToImport).forEach(([category, presets]) => {
if (!this.customPresets[category]) {
this.customPresets[category] = {};
}
Object.assign(this.customPresets[category], presets);
});
// Merge hidden built-in presets
Object.entries(hiddenToImport).forEach(([category, hiddenPresets]) => {
if (!this.hiddenBuiltInPresets[category]) {
this.hiddenBuiltInPresets[category] = [];
}
// Merge arrays, avoiding duplicates
hiddenPresets.forEach(presetName => {
if (!this.hiddenBuiltInPresets[category].includes(presetName)) {
this.hiddenBuiltInPresets[category].push(presetName);
}
});
});
} else {
// Replace all presets
this.customPresets = presetsToImport;
this.hiddenBuiltInPresets = hiddenToImport;
}
this.saveCustomPresets();
log.debug('Imported presets successfully', merge ? '(merged)' : '(replaced)');
return true;
} catch (error) {
log.error('Error importing presets:', error);
return false;
}
}
/**
* Clears all custom presets
* @returns {boolean} Success status
*/
clearAllPresets() {
try {
this.customPresets = {};
this.saveCustomPresets();
log.debug('Cleared all custom presets');
return true;
} catch (error) {
log.error('Error clearing presets:', error);
return false;
}
}
/**
* Gets preset count statistics
* @returns {Object} Statistics object
*/
getStatistics() {
let totalCategories = 0;
let totalPresets = 0;
Object.entries(this.customPresets).forEach(([category, presets]) => {
totalCategories++;
totalPresets += Object.keys(presets).length;
});
return {
categories: totalCategories,
presets: totalPresets
};
}
/**
* Checks if a preset name exists in a category
* @param {string} category - Category name
* @param {string} name - Preset name
* @returns {boolean} True if exists
*/
presetExists(category, name) {
return !!(this.customPresets[category] && this.customPresets[category][name]);
}
/**
* Checks if a category exists
* @param {string} category - Category name
* @returns {boolean} True if exists
*/
categoryExists(category) {
return !!this.customPresets[category];
}
/**
* Checks if a preset is a custom preset (vs built-in)
* @param {string} category - Category name
* @param {string} name - Preset name
* @returns {boolean} True if custom preset
*/
isCustomPreset(category, name) {
return this.presetExists(category, name);
}
/**
* Reorders categories by moving one category to a new position
* @param {string} categoryName - Category to move
* @param {number} newIndex - New position index
* @returns {boolean} Success status
*/
reorderCategories(categoryName, newIndex) {
try {
// Convert to array of entries
const entries = Object.entries(this.customPresets);
// Find current index
const currentIndex = entries.findIndex(([name]) => name === categoryName);
if (currentIndex === -1) {
log.warn(`Category ${categoryName} not found`);
return false;
}
// Remove from current position
const [movedEntry] = entries.splice(currentIndex, 1);
// Insert at new position
entries.splice(newIndex, 0, movedEntry);
// Rebuild object with new order
this.customPresets = Object.fromEntries(entries);
this.saveCustomPresets();
log.debug(`Reordered category ${categoryName}: ${currentIndex} -> ${newIndex}`);
return true;
} catch (error) {
log.error('Error reordering categories:', error);
return false;
}
}
/**
* Reorders presets within a category
* @param {string} category - Category name
* @param {string} presetName - Preset to move
* @param {number} newIndex - New position index
* @returns {boolean} Success status
*/
reorderPresets(category, presetName, newIndex) {
try {
if (!this.customPresets[category]) {
log.warn(`Category ${category} not found`);
return false;
}
// Convert to array of entries
const entries = Object.entries(this.customPresets[category]);
// Find current index
const currentIndex = entries.findIndex(([name]) => name === presetName);
if (currentIndex === -1) {
log.warn(`Preset ${presetName} not found in category ${category}`);
return false;
}
// Remove from current position
const [movedEntry] = entries.splice(currentIndex, 1);
// Insert at new position
entries.splice(newIndex, 0, movedEntry);
// Rebuild category with new order
this.customPresets[category] = Object.fromEntries(entries);
this.saveCustomPresets();
log.debug(`Reordered preset ${category}/${presetName}: ${currentIndex} -> ${newIndex}`);
return true;
} catch (error) {
log.error('Error reordering presets:', error);
return false;
}
}
/**
* Moves a preset from one category to another
* @param {string} sourceCategory - Source category name
* @param {string} presetName - Preset name to move
* @param {string} targetCategory - Target category name
* @param {number} targetIndex - Index in target category (optional, defaults to end)
* @returns {boolean} Success status
*/
movePreset(sourceCategory, presetName, targetCategory, targetIndex = -1) {
try {
// Check if source preset exists
if (!this.customPresets[sourceCategory] || !this.customPresets[sourceCategory][presetName]) {
log.warn(`Source preset ${sourceCategory}/${presetName} not found`);
return false;
}
// Get the preset data
const presetData = this.customPresets[sourceCategory][presetName];
// Create target category if it doesn't exist
if (!this.customPresets[targetCategory]) {
this.customPresets[targetCategory] = {};
}
// Check if preset with same name already exists in target
if (this.customPresets[targetCategory][presetName]) {
log.warn(`Preset ${presetName} already exists in category ${targetCategory}`);
return false;
}
// Convert target category to array for insertion at specific index
const targetEntries = Object.entries(this.customPresets[targetCategory]);
// Insert at specified index (or end if -1)
if (targetIndex === -1 || targetIndex >= targetEntries.length) {
targetEntries.push([presetName, presetData]);
} else {
targetEntries.splice(targetIndex, 0, [presetName, presetData]);
}
// Rebuild target category
this.customPresets[targetCategory] = Object.fromEntries(targetEntries);
// Remove from source category
delete this.customPresets[sourceCategory][presetName];
// Remove source category if empty
if (Object.keys(this.customPresets[sourceCategory]).length === 0) {
delete this.customPresets[sourceCategory];
}
this.saveCustomPresets();
log.debug(`Moved preset ${presetName} from ${sourceCategory} to ${targetCategory} at index ${targetIndex}`);
return true;
} catch (error) {
log.error('Error moving preset:', error);
return false;
}
}
/**
* Gets all hidden built-in presets
* @returns {Object} Hidden built-in presets object
*/
getHiddenBuiltInPresets() {
return this.hiddenBuiltInPresets;
}
/**
* Checks if a built-in preset is hidden
* @param {string} category - Category name
* @param {string} name - Preset name
* @returns {boolean} True if hidden
*/
isBuiltInPresetHidden(category, name) {
return !!(this.hiddenBuiltInPresets[category] &&
this.hiddenBuiltInPresets[category].includes(name));
}
/**
* Toggles visibility of a built-in preset (hide/unhide)
* @param {string} category - Category name
* @param {string} name - Preset name
* @returns {boolean} New visibility state (true = now hidden, false = now visible)
*/
toggleBuiltInPresetVisibility(category, name) {
try {
if (!this.hiddenBuiltInPresets[category]) {
this.hiddenBuiltInPresets[category] = [];
}
const index = this.hiddenBuiltInPresets[category].indexOf(name);
if (index === -1) {
// Not hidden yet - hide it
this.hiddenBuiltInPresets[category].push(name);
log.debug(`Hidden built-in preset: ${category}/${name}`);
this.saveCustomPresets();
return true; // Now hidden
} else {
// Already hidden - unhide it
this.hiddenBuiltInPresets[category].splice(index, 1);
// Remove category if empty
if (this.hiddenBuiltInPresets[category].length === 0) {
delete this.hiddenBuiltInPresets[category];
}
log.debug(`Unhidden built-in preset: ${category}/${name}`);
this.saveCustomPresets();
return false; // Now visible
}
} catch (error) {
log.error('Error toggling built-in preset visibility:', error);
return this.isBuiltInPresetHidden(category, name);
}
}
}
|