feat: global block library tree + grouping tool — 19 files, 588 tests green
This commit is contained in:
@@ -10,6 +10,7 @@ import type {
|
||||
DatabaseInterface, DBProject, DBDrawing, DBLayer,
|
||||
DBElement, DBBlock, DBSetting, DBUser, DBSession,
|
||||
DBNotification, DBProjectShare,
|
||||
DBGlobalBlockFolder, DBGlobalBlock,
|
||||
} from './DatabaseInterface.js';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
@@ -319,4 +320,81 @@ export class SqliteAdapter implements DatabaseInterface {
|
||||
deleteProjectShare(id: string): boolean {
|
||||
return this.db.prepare('DELETE FROM project_shares WHERE id = ?').run(id).changes > 0;
|
||||
}
|
||||
|
||||
// ─── Global Block Folders ──────────────────────────────
|
||||
listGlobalFolders(parentId?: string | null): DBGlobalBlockFolder[] {
|
||||
if (parentId === undefined) {
|
||||
return this.db.prepare('SELECT * FROM global_block_folders ORDER BY name').all() as DBGlobalBlockFolder[];
|
||||
}
|
||||
if (parentId === null) {
|
||||
return this.db.prepare('SELECT * FROM global_block_folders WHERE parent_id IS NULL ORDER BY name').all() as DBGlobalBlockFolder[];
|
||||
}
|
||||
return this.db.prepare('SELECT * FROM global_block_folders WHERE parent_id = ? ORDER BY name').all(parentId) as DBGlobalBlockFolder[];
|
||||
}
|
||||
|
||||
getGlobalFolder(id: string): DBGlobalBlockFolder | null {
|
||||
return (this.db.prepare('SELECT * FROM global_block_folders WHERE id = ?').get(id) as DBGlobalBlockFolder) ?? null;
|
||||
}
|
||||
|
||||
createGlobalFolder(data: Partial<DBGlobalBlockFolder>): DBGlobalBlockFolder {
|
||||
const id = data.id ?? `gfolder-${Date.now()}`;
|
||||
this.db.prepare(
|
||||
'INSERT INTO global_block_folders (id, name, parent_id) VALUES (?, ?, ?)',
|
||||
).run(id, data.name ?? 'Neuer Ordner', data.parent_id ?? null);
|
||||
return this.getGlobalFolder(id)!;
|
||||
}
|
||||
|
||||
updateGlobalFolder(id: string, data: Partial<DBGlobalBlockFolder>): DBGlobalBlockFolder | null {
|
||||
const sets: string[] = [];
|
||||
const vals: unknown[] = [];
|
||||
if (data.name !== undefined) { sets.push('name = ?'); vals.push(data.name); }
|
||||
if (data.parent_id !== undefined) { sets.push('parent_id = ?'); vals.push(data.parent_id); }
|
||||
if (sets.length > 0) {
|
||||
this.db.prepare(`UPDATE global_block_folders SET ${sets.join(', ')} WHERE id = ?`).run(...vals, id);
|
||||
}
|
||||
return this.getGlobalFolder(id);
|
||||
}
|
||||
|
||||
deleteGlobalFolder(id: string): boolean {
|
||||
return this.db.prepare('DELETE FROM global_block_folders WHERE id = ?').run(id).changes > 0;
|
||||
}
|
||||
|
||||
// ─── Global Blocks ─────────────────────────────────────
|
||||
listGlobalBlocks(folderId?: string | null): DBGlobalBlock[] {
|
||||
if (folderId === undefined) {
|
||||
return this.db.prepare('SELECT * FROM global_blocks ORDER BY updated_at DESC').all() as DBGlobalBlock[];
|
||||
}
|
||||
if (folderId === null) {
|
||||
return this.db.prepare('SELECT * FROM global_blocks WHERE folder_id IS NULL ORDER BY updated_at DESC').all() as DBGlobalBlock[];
|
||||
}
|
||||
return this.db.prepare('SELECT * FROM global_blocks WHERE folder_id = ? ORDER BY updated_at DESC').all(folderId) as DBGlobalBlock[];
|
||||
}
|
||||
|
||||
getGlobalBlock(id: string): DBGlobalBlock | null {
|
||||
return (this.db.prepare('SELECT * FROM global_blocks WHERE id = ?').get(id) as DBGlobalBlock) ?? null;
|
||||
}
|
||||
|
||||
createGlobalBlock(data: Partial<DBGlobalBlock>): DBGlobalBlock {
|
||||
const id = data.id ?? `gblock-${Date.now()}`;
|
||||
this.db.prepare(
|
||||
'INSERT INTO global_blocks (id, folder_id, name, block_data, svg_data) VALUES (?, ?, ?, ?, ?)',
|
||||
).run(id, data.folder_id ?? null, data.name ?? 'Global Block', data.block_data ?? '{}', data.svg_data ?? null);
|
||||
return this.getGlobalBlock(id)!;
|
||||
}
|
||||
|
||||
updateGlobalBlock(id: string, data: Partial<DBGlobalBlock>): DBGlobalBlock | null {
|
||||
const sets: string[] = [];
|
||||
const vals: unknown[] = [];
|
||||
if (data.name !== undefined) { sets.push('name = ?'); vals.push(data.name); }
|
||||
if (data.folder_id !== undefined) { sets.push('folder_id = ?'); vals.push(data.folder_id); }
|
||||
if (data.block_data !== undefined) { sets.push('block_data = ?'); vals.push(data.block_data); }
|
||||
if (data.svg_data !== undefined) { sets.push('svg_data = ?'); vals.push(data.svg_data); }
|
||||
sets.push("updated_at = datetime('now')");
|
||||
this.db.prepare(`UPDATE global_blocks SET ${sets.join(', ')} WHERE id = ?`).run(...vals, id);
|
||||
return this.getGlobalBlock(id);
|
||||
}
|
||||
|
||||
deleteGlobalBlock(id: string): boolean {
|
||||
return this.db.prepare('DELETE FROM global_blocks WHERE id = ?').run(id).changes > 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user