Repository navigation
Expand file tree
/
Copy pathwikiStorage.js
More file actions
496 lines (414 loc) · 15.8 KB
/
Copy pathwikiStorage.js
File metadata and controls
496 lines (414 loc) · 15.8 KB
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
import path from 'path';
import fs from 'fs/promises';
import { existsSync } from 'fs';
import { fileURLToPath } from 'url';
// ─────────────────────────────────────────────────────────────────────────
// File-based wiki store. Lives in a git-tracked folder so the documentation
// ships with the app (unlike data/, which is per-user and gitignored).
// wiki/manifest.json — flat list of pages (the tree is derived from parentId)
// wiki/content/<id>.md — one Markdown file per page
// wiki/media/ — screenshots and videos referenced by the pages
//
// Resolve the folder relative to THIS module — NOT process.cwd(). The desktop
// app runs the backend with cwd set to the per-user data dir (so the writable
// data/ folder lands there), but the wiki is shipped, read-only documentation
// that sits beside this file: the repo root in dev, resources/app/ in a
// packaged build. Keying off cwd made the packaged app read (and seed a stray
// default) wiki under the user data dir instead of the shipped one.
// ─────────────────────────────────────────────────────────────────────────
const MODULE_DIR = path.dirname(fileURLToPath(import.meta.url));
export const WIKI_DIR = path.join(MODULE_DIR, 'wiki');
export const WIKI_CONTENT_DIR = path.join(WIKI_DIR, 'content');
export const WIKI_MEDIA_DIR = path.join(WIKI_DIR, 'media');
const WIKI_MANIFEST = path.join(WIKI_DIR, 'manifest.json');
const WIKI_SEED_TREE = [
{
title: 'Getting Started',
icon: 'rocket_launch',
content: `# Welcome to 3D Gen Studio
This is your living documentation hub. Use it to capture workflows, tips, and reference notes — with **text, screenshots, and videos** all in one place.
## How to use this Wiki
- Browse pages from the tree on the left.
- Click **Edit** to change a page. The editor uses simple Markdown.
- Use the toolbar to **insert a screenshot or video** — files upload automatically and the embed is dropped in for you.
- Use **+ New Page** to add your own topics, nested anywhere you like.
> 💡 **Tip:** Paste an image directly into the editor, or drag a file onto it — it uploads and embeds instantly.
### Adding a screenshot
\`\`\`

\`\`\`
### Adding a video
\`\`\`
<video src="upload-a-video" controls></video>
\`\`\`
Happy documenting!`
},
{
title: 'Projects',
icon: 'folder_special',
content: `# Projects
A **Project** is a self-contained workspace that bundles everything for one creation: its Kanban board, its node Graph, and its linked assets.
Each project gives you two complementary views of the same work:
- **[Kanban board]** — a stage-by-stage pipeline of cards.
- **[Graph page]** — a node-based view of how assets flow into one another.
> 📸 _Add a screenshot of the Projects gallery here._`,
children: [
{
title: 'Kanban Board',
icon: 'view_kanban',
content: `# Kanban Board
The Kanban board organizes a project's work into five pipeline columns. Drag cards between columns as they progress.
## Columns
| Column | Purpose |
| --- | --- |
| **Images** | Source and generated images |
| **Image Edit** | Edited / refined images |
| **Mesh Gen** | Meshes generated from images |
| **Mesh Edit** | Meshes being modeled or refined |
| **Texturing** | Meshes in texturing |
> 📸 _Screenshot of the board with cards in each column._
> 🎬 _Short clip of dragging a card from Images → Mesh Gen._`
},
{
title: 'Graph Page',
icon: 'account_tree',
content: `# Graph Page
The Graph page is a node-based canvas where you wire assets and values together to describe how a result is produced.
## Node Types
- **Image** — an image asset.
- **Mesh** — a 3D mesh asset.
- **Image Compare** — side-by-side comparison of two images.
- **Number** — a numeric value.
- **Text** — a text value (e.g. a prompt).
- **Boolean** — a true/false toggle.
> 📸 _Screenshot of a small example graph._`
}
]
},
{
title: 'Assets',
icon: 'inventory_2',
content: `# Assets
The Assets Library is your central store of reusable files. Import once, reuse across every project.
Sections:
- **Images** — bitmaps you generate, edit, or import.
- **Meshes** — 3D models and their versions.
- **Brushes** — paint brushes, including imported \`.abr\` sets.
- **Workflows** — saved ComfyUI graphs with exposed inputs/outputs.`,
children: [
{
title: 'Images',
icon: 'image',
content: `# Images
Import bitmap images, browse them, and open them in the **Image Editor**. Each image can keep a history of edits.
> 📸 _Screenshot of the Images grid._`
},
{
title: 'Meshes',
icon: 'deployed_code',
content: `# Meshes
3D meshes (\`.glb\`, \`.gltf\`, \`.obj\`, \`.fbx\`, \`.stl\`, \`.ply\`) with thumbnail previews and versioning. Open any mesh in the **Mesh Editor**.
> 📸 _Screenshot of a mesh thumbnail and its versions._`
},
{
title: 'Brushes',
icon: 'brush',
content: `# Brushes
Reusable paint brushes used by the editors. Import Photoshop \`.abr\` brush sets — each sample becomes a brush variant.
> 📸 _Screenshot of the brush gallery._`
},
{
title: 'Workflows',
icon: 'schema',
content: `# Workflows
Import a **ComfyUI** workflow JSON, then choose which nodes become editable **parameters** and which nodes are saved as **outputs**. Saved workflows can be run from the editors and the Kanban pipeline.
> 🎬 _Clip of importing a workflow and selecting inputs/outputs._`
}
]
},
{
title: 'Editors',
icon: 'edit_square',
content: `# Editors
Two dedicated editors let you refine assets directly inside the studio:
- **[Image Editor]** — crop, adjust, paint, and AI-edit images.
- **[Mesh Editor]** — model, texture, paint, project, displace, and sculpt meshes.`,
children: [
{
title: 'Image Editor',
icon: 'photo_filter',
content: `# Image Editor
A full raster editor for refining images.
## Features
- **Crop** — trim to a region or aspect ratio.
- **Resize** — change pixel dimensions.
- **Levels / Contrast / Saturation** — tonal and color adjustments.
- **Blur / Sharpen** — soften or enhance detail.
- **Shadow Remover** — GPU-accelerated shadow removal.
- **Paint** — freehand painting with a **Brush** or **Image Brush**.
- **AI** — paint a **Mask** and run a **ComfyUI** workflow on the selection.
> 📸 _Screenshot of the editor with the tool panel._
> 🎬 _Clip of the Shadow Remover before/after._`
},
{
title: 'Mesh Editor',
icon: 'view_in_ar',
content: `# Mesh Editor
Work on 3D meshes with a stack of complementary tools.
## Tools
- **Modeling** — edit geometry.
- **Texturing** — generate textures with **ComfyUI**.
- **Painting** — paint directly on the surface with a **Brush**.
- **Projection** — project imagery onto the mesh with **ComfyUI**.
- **Displace** — displace geometry using a **Brush**.
- **Sculpting** — sculpt the surface with a **Brush**.
> 🎬 _Clip of projecting a texture onto a mesh._`
}
]
},
{
title: 'Settings',
icon: 'settings',
content: `# Settings
Configure the services that power generation and editing.
- **[APIs]** — keys and endpoints for image/mesh generation providers.
- **[ComfyUI]** — connection to your local or remote ComfyUI server.`,
children: [
{
title: 'APIs',
icon: 'vpn_key',
content: `# APIs
Store API keys and endpoints for the providers you use (image generation, mesh generation, and more). Keys are saved locally with your app data.
> 📸 _Screenshot of the APIs settings tab._`
},
{
title: 'ComfyUI',
icon: 'lan',
content: `# ComfyUI
Point the studio at your **ComfyUI** server. Once connected, imported workflows can run from the editors and the Kanban pipeline.
> 📸 _Screenshot of the ComfyUI connection settings._`
}
]
}
];
async function ensureDirs() {
await fs.mkdir(WIKI_CONTENT_DIR, { recursive: true });
await fs.mkdir(WIKI_MEDIA_DIR, { recursive: true });
}
export function wikiManifestExists() {
return existsSync(WIKI_MANIFEST);
}
async function readManifest() {
try {
const raw = await fs.readFile(WIKI_MANIFEST, 'utf8');
const data = JSON.parse(raw);
return Array.isArray(data?.pages) ? data : { pages: [] };
} catch {
return { pages: [] };
}
}
async function writeManifest(manifest) {
await ensureDirs();
await fs.writeFile(WIKI_MANIFEST, `${JSON.stringify(manifest, null, 2)}\n`, 'utf8');
}
function contentPath(id) {
return path.join(WIKI_CONTENT_DIR, `${id}.md`);
}
async function readContent(id) {
try {
return await fs.readFile(contentPath(id), 'utf8');
} catch {
return '';
}
}
async function writeContent(id, content) {
await ensureDirs();
await fs.writeFile(contentPath(id), content ?? '', 'utf8');
}
async function removeContent(id) {
try {
await fs.unlink(contentPath(id));
} catch {
// already gone — ignore
}
}
function nextId(pages) {
return pages.reduce((max, page) => Math.max(max, Number(page.id) || 0), 0) + 1;
}
function normalizeParentId(parentId) {
if (parentId === null || parentId === undefined || parentId === '') return null;
return Number(parentId);
}
export async function listWikiPages() {
const { pages } = await readManifest();
return pages
.slice()
.sort((a, b) => (a.position - b.position) || (a.id - b.id))
.map(page => ({
id: page.id,
parentId: page.parentId ?? null,
title: page.title,
icon: page.icon || null,
position: page.position ?? 0,
updatedAt: page.updatedAt
}));
}
export async function getWikiPage(id) {
const { pages } = await readManifest();
const page = pages.find(item => Number(item.id) === Number(id));
if (!page) return null;
return {
id: page.id,
parentId: page.parentId ?? null,
title: page.title,
icon: page.icon || null,
position: page.position ?? 0,
createdAt: page.createdAt,
updatedAt: page.updatedAt,
content: await readContent(page.id)
};
}
export async function createWikiPage({ parentId = null, title, icon = null, content = '' } = {}) {
const manifest = await readManifest();
const targetParentId = normalizeParentId(parentId);
const id = nextId(manifest.pages);
const position = manifest.pages
.filter(page => (page.parentId ?? null) === targetParentId)
.reduce((max, page) => Math.max(max, page.position ?? 0), -1) + 1;
const timestamp = Date.now();
const page = {
id,
parentId: targetParentId,
title: String(title || '').trim() || 'Untitled',
icon: icon || null,
position,
createdAt: timestamp,
updatedAt: timestamp
};
manifest.pages.push(page);
await writeManifest(manifest);
await writeContent(id, content || '');
return { ...page, content: content || '' };
}
export async function updateWikiPage(id, fields = {}) {
const manifest = await readManifest();
const page = manifest.pages.find(item => Number(item.id) === Number(id));
if (!page) return null;
if (fields.title !== undefined) page.title = String(fields.title).trim() || 'Untitled';
if (fields.icon !== undefined) page.icon = fields.icon || null;
page.updatedAt = Date.now();
await writeManifest(manifest);
if (fields.content !== undefined) await writeContent(id, String(fields.content));
return await getWikiPage(id);
}
export async function deleteWikiPage(id) {
const manifest = await readManifest();
if (!manifest.pages.some(page => Number(page.id) === Number(id))) {
return { status: 'not-found' };
}
// Collect the page and all of its descendants.
const toDelete = new Set([Number(id)]);
let changed = true;
while (changed) {
changed = false;
for (const page of manifest.pages) {
if (page.parentId != null && toDelete.has(Number(page.parentId)) && !toDelete.has(Number(page.id))) {
toDelete.add(Number(page.id));
changed = true;
}
}
}
manifest.pages = manifest.pages.filter(page => !toDelete.has(Number(page.id)));
await writeManifest(manifest);
for (const deletedId of toDelete) {
await removeContent(deletedId);
}
return { status: 'deleted' };
}
export async function moveWikiPage(id, { parentId, position } = {}) {
const manifest = await readManifest();
const page = manifest.pages.find(item => Number(item.id) === Number(id));
if (!page) return null;
const targetParentId = parentId === undefined ? (page.parentId ?? null) : normalizeParentId(parentId);
// Guard against moving a page into itself or one of its descendants.
if (targetParentId !== null) {
const byId = new Map(manifest.pages.map(item => [Number(item.id), item]));
let cursor = targetParentId;
while (cursor !== null && cursor !== undefined) {
if (Number(cursor) === Number(id)) {
throw new Error('Cannot move a page into itself or its descendants');
}
const parent = byId.get(Number(cursor));
cursor = parent ? (parent.parentId ?? null) : null;
}
}
const siblingIds = manifest.pages
.filter(item => (item.parentId ?? null) === targetParentId && Number(item.id) !== Number(id))
.sort((a, b) => (a.position - b.position) || (a.id - b.id))
.map(item => Number(item.id));
const requestedIndex = Number.isFinite(Number(position)) ? Number(position) : siblingIds.length;
const insertIndex = Math.max(0, Math.min(requestedIndex, siblingIds.length));
siblingIds.splice(insertIndex, 0, Number(id));
const timestamp = Date.now();
siblingIds.forEach((siblingId, index) => {
const sibling = manifest.pages.find(item => Number(item.id) === siblingId);
if (!sibling) return;
if (siblingId === Number(id)) {
sibling.parentId = targetParentId;
sibling.position = index;
sibling.updatedAt = timestamp;
} else {
sibling.position = index;
}
});
await writeManifest(manifest);
return await getWikiPage(id);
}
async function writeSeedNode(manifest, node, parentId, position, timestamp, idRef) {
const id = idRef.next;
idRef.next += 1;
manifest.pages.push({
id,
parentId,
title: node.title,
icon: node.icon || null,
position,
createdAt: timestamp,
updatedAt: timestamp
});
await writeContent(id, node.content || '');
const children = node.children || [];
for (let index = 0; index < children.length; index += 1) {
await writeSeedNode(manifest, children[index], id, index, timestamp, idRef);
}
}
export async function seedWikiFiles() {
await ensureDirs();
const manifest = { pages: [] };
const timestamp = Date.now();
const idRef = { next: 1 };
for (let index = 0; index < WIKI_SEED_TREE.length; index += 1) {
await writeSeedNode(manifest, WIKI_SEED_TREE[index], null, index, timestamp, idRef);
}
await writeManifest(manifest);
}
// One-time import used when migrating an existing database-backed wiki into
// the new folder. `fullPages` is the array returned by the old DB getters,
// each entry carrying { id, parentId, title, icon, position, content, ... }.
export async function importWikiPages(fullPages) {
await ensureDirs();
const manifest = { pages: [] };
for (const page of fullPages) {
manifest.pages.push({
id: Number(page.id),
parentId: page.parentId == null ? null : Number(page.parentId),
title: page.title,
icon: page.icon || null,
position: page.position ?? 0,
createdAt: page.createdAt || Date.now(),
updatedAt: page.updatedAt || Date.now()
});
await writeContent(Number(page.id), page.content || '');
}
await writeManifest(manifest);
}