Files
KGStudio/src/util/projectNameUtil.ts
T
Xiaohan-Tian 2d3ea33ce6 refactor: migrate project storage from IndexedDB to OPFS with folder-based structure
- Replace monolithic KGStorage with KGProjectStorage (OPFS) and KGConfigStorage (IndexedDB)
- Each project stored as folder: project.json + meta.json + media/
- Add KGConfigUpgrader with V1 upgrader to auto-migrate existing IndexedDB projects to OPFS
- Request persistent storage via navigator.storage.persist() to prevent browser eviction
- Show loading spinner during one-time migration
- Enforce safe project name characters (letters, numbers, space, hyphen, underscore, period, parens)
- Export projects as .kgstudio zip bundles instead of raw JSON
- Import supports .kgstudio bundles (with meta.json validation) and legacy JSON files
- Auto-deduplicate project names on import with (1), (2), etc. suffix
- Add OPFS shell debugger (pwd, ls, cd, cat, dl) accessible via KGDebugger.opfs()
- Add ESLint semi rule for consistent semicolons
- Delete KGStorage.ts — all logic absorbed by new storage classes
- Add jszip dependency for zip export/import
2026-04-09 19:25:08 -07:00

45 lines
1.5 KiB
TypeScript

/**
* Allowed characters for project names: letters, numbers, space, hyphen, underscore, period, parentheses.
* These are safe across Windows, macOS, and Linux as directory names.
*/
const VALID_PROJECT_NAME_REGEX = /^[a-zA-Z0-9 \-_.()\u00C0-\u024F]+$/;
/**
* Characters that are NOT allowed in project names — replaced during sanitization.
*/
const DISALLOWED_CHARS_REGEX = /[^a-zA-Z0-9 \-_.()\u00C0-\u024F]/g;
/**
* Validate whether a project name contains only allowed characters.
* Does NOT check for empty string — caller should check that separately.
*/
export function isValidProjectName(name: string): boolean {
if (!name || name.trim().length === 0) return false;
if (name.startsWith('.')) return false; // hidden files on Unix
return VALID_PROJECT_NAME_REGEX.test(name);
}
/**
* Sanitize a project name by replacing disallowed characters with underscores,
* collapsing consecutive underscores/spaces, and trimming.
*/
export function sanitizeProjectName(name: string): string {
let sanitized = name.replace(DISALLOWED_CHARS_REGEX, '_');
// Collapse consecutive underscores
sanitized = sanitized.replace(/_{2,}/g, '_');
// Collapse consecutive spaces
sanitized = sanitized.replace(/ {2,}/g, ' ');
// Trim leading/trailing whitespace, underscores, and dots
sanitized = sanitized.replace(/^[.\s_]+|[.\s_]+$/g, '').trim();
// If everything was stripped, provide a fallback
if (sanitized.length === 0) {
sanitized = 'Untitled Project';
}
return sanitized;
}