diff --git a/.cursor/rules/40-archivist-api.mdc b/.cursor/rules/40-archivist-api.mdc index 6435748..6ecd2ba 100644 --- a/.cursor/rules/40-archivist-api.mdc +++ b/.cursor/rules/40-archivist-api.mdc @@ -170,6 +170,52 @@ DELETE /v1/items/{item_id} → 204 ⸻ +Quests + +GET /v1/quests?campaign_id=&page=&size=&search=&status=&quest_category= → paginated list +GET /v1/quests/{quest_id} +POST /v1/quests (worldId*/campaign_id*, questName*, questGiver?, questCategory?, status?, successDefinition?, failureConditions?, nextAction?, resolution?, objectives?, progressLog?, relatedCharacters?, relatedFactions?, relatedLocations?, relatedItems?, relatedEntityRefs?) → 201 +PATCH /v1/quests/{quest_id} — same fields as POST, all optional (partial update) +DELETE /v1/quests/{quest_id} → 204 + +IMPORTANT — casing differs from every other entity above: Quest's Pydantic schemas +declare fields in **camelCase** (`questName`, `successDefinition`, `relatedEntityRefs`, +…) as the primary name, with snake_case accepted only as an alias +(`populate_by_name=True` + an alias_generator). Every other entity in this doc uses +snake_case as canonical. Sending camelCase quest field names is correct and is what +`archivist-sync`'s `_normalizeQuestPayload`/`_normalizeQuestResponse` already assume. + +`objectives`: `[{ text, status }]` where status is one of +`pending | in-progress | completed | failed | blocked`. +`relatedEntityRefs`: `[{ entityType, entityId, entityNameSnapshot?, label? }]` where +entityType is one of `character | faction | location | item` — **Quest has no generic +`description` field**, and **Quest cannot appear in `/v1/campaigns/{id}/links`** (no +`ENTITY_CONFIG` entry on the backend for it, in either direction). Quest relationships +only ever persist via `relatedEntityRefs` on the quest itself — treat Quest as the +single source of truth for its own relationships, not the generic links system. + +⸻ + +Journals + +GET /v1/journals?campaign_id=&page=&size= +GET /v1/journals/{journal_id} +POST /v1/journals (id?, title*, content?, campaign_id*) → 201 +PATCH /v1/journals (id*, title?, content?) → 200 — note: PATCH takes `id` in the body, +not as a path param, unlike every other entity in this doc. +DELETE /v1/journals/{journal_id} → 204 + +⸻ + +Links — PATCH exists, use it instead of delete+recreate + +PATCH /v1/campaigns/{campaign_id}/links/{link_id} (alias?) → 200 +Prefer this over DELETE-then-POST when only a link's label/alias is changing and the +from/to endpoints stay the same — cheaper and avoids a window where the link doesn't +exist. Quest is not a valid `from_type`/`to_type` here (see above). + +⸻ + Client Helpers (use these in this project) Create a tiny wrapper so all calls are consistent and type-safe: diff --git a/lang/en.json b/lang/en.json index b742d46..b757611 100644 --- a/lang/en.json +++ b/lang/en.json @@ -171,7 +171,9 @@ "items": "Items", "recaps": "Recaps", "mappingOverride": "Mapping Override (JSON)", - "importProgress": "Import Progress" + "importProgress": "Import Progress", + "journals": "Journals", + "quests": "Quests" }, "status": { "connected": "Connected", @@ -229,7 +231,9 @@ "locationsPulled": "Locations pulled successfully", "charactersPulled": "Characters pulled successfully", "worldInitialized": "Foundry world has been initialized with Archivist for the first time", - "worldInitializedReset": "World initialization reset. Reloading to run setup again..." + "worldInitializedReset": "World initialization reset. Reloading to run setup again...", + "journalImportNotice": "Journal content syncs with Archivist. Folder structure is a point-in-time snapshot and not kept in sync.", + "questSynced": "Quest synced with Archivist." }, "errors": { "fetchFailed": "Failed to fetch worlds from Archivist API", @@ -319,6 +323,35 @@ "name": "Real‑Time Sync (auto‑update Archivist)", "hint": "When enabled, changes you make in Foundry (create, edit, or delete Actors, Items, and designated Faction/Location journal pages) are immediately mirrored to Archivist. Recaps are read‑only: creating a Recaps page will not create a Game Session, and deleting a Recaps page will not delete a Game Session in Archivist." } + }, + "quest": { + "status": { + "planned": "Planned", + "inProgress": "In Progress", + "blocked": "Blocked", + "failed": "Failed", + "done": "Done" + }, + "category": { + "main": "Main", + "side": "Side", + "faction": "Faction", + "personal": "Personal" + }, + "objectiveStatus": { + "pending": "Pending", + "inProgress": "In Progress", + "completed": "Completed", + "failed": "Failed", + "blocked": "Blocked" + }, + "fields": { + "questGiver": "Quest Giver", + "successDefinition": "Success Definition", + "failureConditions": "Failure Conditions", + "nextAction": "Next Action", + "resolution": "Resolution" + } } } } diff --git a/scripts/archivist-sync.js b/scripts/archivist-sync.js index e801cae..b75da9e 100644 --- a/scripts/archivist-sync.js +++ b/scripts/archivist-sync.js @@ -60,6 +60,8 @@ Hooks.once('init', async function () { LocationPageSheetV2, FactionPageSheetV2, RecapPageSheetV2, + JournalPageSheetV2, + QuestPageSheetV2, } = await import('./modules/sheets/page-sheet-v2.js'); const DSC = @@ -99,6 +101,16 @@ Hooks.once('init', async function () { types: ['base'], makeDefault: false, }); + DSC.registerSheet(JournalEntry, 'archivist-sync', JournalPageSheetV2, { + label: 'Archivist: Journal', + types: ['base'], + makeDefault: false, + }); + DSC.registerSheet(JournalEntry, 'archivist-sync', QuestPageSheetV2, { + label: 'Archivist: Quest', + types: ['base'], + makeDefault: false, + }); } catch (e) { console.error( '[Archivist Sync] Failed to register V2 DocumentSheet sheets', @@ -108,6 +120,11 @@ Hooks.once('init', async function () { // Register the Archivist Chat tab with the core Sidebar early so it renders its // nav button and panel using the Application V2 TabGroup. Availability will be // handled at runtime by showing/hiding the button and panel. + // NOTE: this Sidebar.TABS registration mechanism works on v13 but was found + // to not support dynamic tab addition on v14 — do not port this removal + // back here; v14 instead injects the chat button at runtime via + // updateArchivistChatAvailability() in the ready hook, which this branch + // also has as a fallback (see below). try { const Sidebar = foundry.applications.sidebar?.Sidebar; if (Sidebar) { @@ -266,9 +283,7 @@ Hooks.once('ready', async function () { try { const sidebar = document.getElementById('sidebar'); const tabsNav = sidebar?.querySelector?.('#sidebar-tabs, nav.tabs'); - if (!tabsNav) { - return; - } + if (tabsNav) { const onClick = (ev) => { // CRITICAL: Ignore clicks on dice roll cards or any elements inside them @@ -335,6 +350,8 @@ Hooks.once('ready', async function () { }, 0); }; tabsNav.addEventListener('click', onClick); + + } // end if (tabsNav found for delegated renderer) } catch (e) { console.error('[Archivist Sync] Failed to install delegated renderer', e); } @@ -343,9 +360,7 @@ Hooks.once('ready', async function () { try { const sidebar = document.getElementById('sidebar'); const tabsNav = sidebar?.querySelector?.('#sidebar-tabs, nav.tabs'); - if (!tabsNav) { - return; - } + if (tabsNav) { const onOtherTabClick = (ev) => { // CRITICAL: Ignore clicks on dice roll cards or any elements inside them @@ -410,6 +425,8 @@ Hooks.once('ready', async function () { }, 0); }; tabsNav.addEventListener('click', onOtherTabClick); + + } // end if (tabsNav found for delegated cleanup) } catch (e) { console.error('[Archivist Sync] Failed to install delegated cleanup', e); } @@ -885,7 +902,12 @@ function updateArchivistChatAvailability() { }); li.appendChild(btn); const menu = tabsNav.querySelector('menu.flexcol') || tabsNav; - menu.appendChild(li); + const beforeNode = menu.lastElementChild; + if (beforeNode) { + menu.insertBefore(li, beforeNode); + } else { + menu.appendChild(li); + } } catch (e) { console.warn( '[Archivist Sync] Failed to inject fallback Sidebar tab button', @@ -1100,6 +1122,17 @@ function installRealtimeSyncListeners() { ...(image ? { image } : {}), campaign_id: worldId, }); + } else if (sheetType === 'quest') { + res = await archivistApi.createQuest(apiKey, { + worldId, + questName: entry.name || 'Quest', + }); + } else if (sheetType === 'journal') { + res = await archivistApi.createJournal(apiKey, { + world_id: worldId, + title: entry.name || 'Journal', + content: description, + }); } if (res.success && res.data?.id) { @@ -1219,6 +1252,7 @@ function installRealtimeSyncListeners() { const flags = parent?.getFlag?.(CONFIG.MODULE_ID, 'archivist') || {}; const sheetType = String(flags?.sheetType || '').toLowerCase(); if (!sheetType || !flags.archivistId) return; + const html = Utils.extractPageHtml(page); const payload = { description: Utils.toMarkdownIfHtml?.(html) || html }; @@ -1252,6 +1286,19 @@ function installRealtimeSyncListeners() { summary: payload.description, }); return; + } else if (sheetType === 'quest') { + // Quest page content is not a synced API field (quests use structured + // fields like successDefinition, not a free description), and name + // changes are handled by the updateJournalEntry title-sync hook. So a + // page-content edit has nothing to push — skip the redundant PATCH. + return; + } else if (sheetType === 'journal') { + res = await archivistApi.updateJournal(apiKey, { + id: flags.archivistId, + title: parent?.name || page.name, + content: Utils.toMarkdownIfHtml?.(html) || html, + }); + return; } if (res && !res?.success && res?.isDescriptionTooLong) { @@ -1294,6 +1341,10 @@ function installRealtimeSyncListeners() { await archivistApi.updateLocation(apiKey, id, { name }); } else if (st === 'faction') { await archivistApi.updateFaction(apiKey, id, { name }); + } else if (st === 'quest') { + await archivistApi.updateQuest(apiKey, id, { questName: name }); + } else if (st === 'journal') { + await archivistApi.updateJournal(apiKey, { id, title: name }); } } catch (e) { console.warn('[RTS] updateJournalEntry (title sync) failed', e); @@ -1374,6 +1425,10 @@ function installRealtimeSyncListeners() { await archivistApi.deleteLocation(apiKey, id); } else if (st === 'faction' && archivistApi.deleteFaction) { await archivistApi.deleteFaction(apiKey, id); + } else if (st === 'quest') { + await archivistApi.deleteQuest(apiKey, id); + } else if (st === 'journal') { + await archivistApi.deleteJournal(apiKey, id); } } catch (e) { console.warn('[RTS] preDeleteJournalEntry failed', e); diff --git a/scripts/dialogs/sync-dialog.js b/scripts/dialogs/sync-dialog.js index 81fb955..0fcb207 100644 --- a/scripts/dialogs/sync-dialog.js +++ b/scripts/dialogs/sync-dialog.js @@ -14,9 +14,10 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi constructor(options = {}) { super(options); this.isLoading = false; + this.syncProgress = null; this.model = { - diffs: [], // { type, id, name, journalId, changes: { name?, description?, image?, links? }, deleted?:boolean, selected:boolean } - imports: [], // { type, id, name, image, description, selected:boolean, createCore:boolean, coreType:'actor'|'item'|'scene'|null } + diffs: [], + imports: [], stats: { diffs: 0, imports: 0 }, }; this._scrollPosition = 0; @@ -48,11 +49,88 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi async _onRender(context, options) { await super._onRender?.(context, options); - // Restore scroll position after render const content = this.element?.querySelector?.('.sync-dialog-content'); if (content && this._scrollPosition !== undefined) { content.scrollTop = this._scrollPosition; } + this._updateSyncButtonState(); + + // Shift+click multi-select for checkboxes + this._lastToggleClick = null; + const rows = this.element?.querySelectorAll?.('tr[data-id]') || []; + for (const row of rows) { + const cb = row.querySelector('input[data-action="toggleRow"]'); + if (!cb) continue; + row.addEventListener('click', (e) => { + if (e.target.closest('[data-action="toggleCore"]')) return; + if (!e.shiftKey || !this._lastToggleClick) { + this._lastToggleClick = row; + return; + } + // Range-select handles the clicked row itself; stop the event here so + // the root-level toggleRow action doesn't flip it back afterwards. + e.stopPropagation(); + const allRows = [...this.element.querySelectorAll('tr[data-id]')]; + const startIdx = allRows.indexOf(this._lastToggleClick); + const endIdx = allRows.indexOf(row); + if (startIdx < 0 || endIdx < 0) return; + const [lo, hi] = startIdx < endIdx ? [startIdx, endIdx] : [endIdx, startIdx]; + const targetState = cb.checked; + for (let i = lo; i <= hi; i++) { + const r = allRows[i]; + const kind = r.dataset.kind; + const id = String(r.dataset.id || ''); + let modelRow; + if (kind === 'diff') modelRow = this.model.diffs.find((x) => String(x.id) === id); + else if (kind === 'import') modelRow = this.model.imports.find((x) => String(x.id) === id); + if (modelRow) { + modelRow.selected = targetState; + const rCb = r.querySelector('input[data-action="toggleRow"]'); + if (rCb) rCb.checked = targetState; + r.classList.toggle('selected', targetState); + } + } + this._lastToggleClick = row; + this._updateSyncButtonState(); + }); + } + } + + _updateSyncButtonState() { + const btn = this.element?.querySelector?.('[data-action="sync"]'); + if (!btn) return; + const hasSelected = + this.model.diffs.some((d) => d.selected) || + this.model.imports.some((i) => i.selected); + btn.disabled = !hasSelected; + } + + /** + * Update the in-progress panel's text directly rather than re-rendering the + * whole dialog for every synced item. The panel is rendered once before the + * sync loop begins; a skipped update (panel absent) is corrected by the + * final render when syncProgress is cleared. + * @private + */ + _updateProgressUI() { + const panel = this.element?.querySelector?.('.loading-panel'); + if (!panel || !this.syncProgress) return; + const { processed, total, current } = this.syncProgress; + const textEl = panel.querySelector('.sync-progress-text'); + if (textEl) { + textEl.textContent = `Processing ${processed} / ${total}…`; + } + let detailEl = panel.querySelector('.sync-progress-detail'); + if (current) { + if (!detailEl) { + detailEl = document.createElement('span'); + detailEl.className = 'sync-progress-detail'; + panel.appendChild(detailEl); + } + detailEl.textContent = current; + } else if (detailEl) { + detailEl.remove(); + } } _captureScrollPosition() { @@ -63,30 +141,30 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi } async _prepareContext() { - // If not initialized, show loading and trigger background fetch if (!this._initialized) { - // Trigger data load in background (don't await here) this._loadModel().then(() => { this._initialized = true; this.render({ force: true }); }); - // Return loading state immediately return { isLoading: true, diffs: [], imports: [], stats: { diffs: 0, imports: 0 }, - isGM: game.user?.isGM, + syncProgress: null, }; } - const ctx = { + const hasSelected = + this.model.diffs.some((d) => d.selected) || + this.model.imports.some((i) => i.selected); + return { isLoading: this.isLoading, diffs: this.model.diffs, imports: this.model.imports, stats: this.model.stats, - isGM: game.user?.isGM, + syncProgress: this.syncProgress || null, + hasSelected, }; - return ctx; } async _onSelectAll(event) { @@ -117,15 +195,18 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi if (!row) return; const kind = row?.dataset?.kind; const id = String(row?.dataset?.id || ''); + let modelRow; if (kind === 'diff') { - const diffRow = this.model.diffs.find((x) => String(x.id) === id); - if (diffRow) diffRow.selected = !diffRow.selected; + modelRow = this.model.diffs.find((x) => String(x.id) === id); } else if (kind === 'import') { - const importRow = this.model.imports.find((x) => String(x.id) === id); - if (importRow) importRow.selected = !importRow.selected; + modelRow = this.model.imports.find((x) => String(x.id) === id); } - this._captureScrollPosition(); - await this.render(); + if (!modelRow) return; + modelRow.selected = !modelRow.selected; + const cb = row.querySelector('input[type="checkbox"]'); + if (cb) cb.checked = modelRow.selected; + row.classList.toggle('selected', modelRow.selected); + this._updateSyncButtonState(); } async _onToggleCreateCore(event) { @@ -135,19 +216,14 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi const row = this.model.imports.find((x) => String(x.id) === id); if (!row || !row.coreType) return; row.createCore = !row.createCore; - this._captureScrollPosition(); - await this.render(); } async _onRefresh(event) { event?.preventDefault?.(); - // Set loading state to show the spinner this.isLoading = true; - await this.render(); try { await this._loadModel(true); } finally { - // Loading state will be set to false by _loadModel await this.render(); } } @@ -193,28 +269,65 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi await this.render(); return; } - console.log('[SyncDialog] ✓ Real-time sync successfully suppressed'); + console.debug('[SyncDialog] Real-time sync successfully suppressed'); try { - // Apply diffs + // Confirm deletions before proceeding + const deleteDiffs = selectedDiffs.filter((d) => d.deleted); + if (deleteDiffs.length > 0) { + const names = deleteDiffs + .map((d) => foundry.utils.escapeHTML(String(d.name ?? ''))) + .join(', '); + const confirmed = await foundry.applications.api.DialogV2.confirm({ + window: { title: 'Confirm Deletion' }, + content: `
${deleteDiffs.length} journal${deleteDiffs.length > 1 ? 's' : ''} will be permanently deleted:
${names}
This cannot be undone. Continue?
`, + yes: { label: 'Delete', icon: 'fas fa-trash' }, + no: { label: 'Cancel' }, + }); + if (!confirmed) { + this.isLoading = false; + await this.render(); + return; + } + } + + const total = selectedDiffs.length + selectedImports.length; + let processed = 0; + let failCount = 0; + this.syncProgress = { total, processed, current: '' }; + await this.render(); + for (const d of selectedDiffs) { + this.syncProgress.current = `${d.type}: ${d.name}`; + this.syncProgress.processed = processed; + this._updateProgressUI(); try { await this._applyDiff(d); } catch (e) { - console.warn('[SyncDialog] applyDiff failed', e); + failCount++; + console.warn('[SyncDialog] applyDiff failed', d.name, e); } + processed++; } - // Apply imports for (const i of selectedImports) { + this.syncProgress.current = `${i.type}: ${i.name}`; + this.syncProgress.processed = processed; + this._updateProgressUI(); try { await this._applyImport(i, campaignId, apiKey); } catch (e) { - console.warn('[SyncDialog] applyImport failed', e); + failCount++; + console.warn('[SyncDialog] applyImport failed', i.name, e); } + processed++; } + // Reflect the final processed count before the panel is torn down. + this.syncProgress.processed = processed; + this._updateProgressUI(); + + this.syncProgress = null; - // Reorder all recaps after any sessionDate changes (from diffs or imports) const hasRecapDiffs = selectedDiffs.some( (d) => d.type === 'Session' && d.changes?.sessionDate ); @@ -237,7 +350,7 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi j.getFlag(CONFIG.MODULE_ID, 'sessionDate') || '' ).trim(); const t = iso ? new Date(iso).getTime() : NaN; - return Number.isFinite(t) ? t : Number.POSITIVE_INFINITY; // undated go to end + return Number.isFinite(t) ? t : Number.POSITIVE_INFINITY; })(), })); withDates.sort((a, b) => a.dateMs - b.dateMs); @@ -258,25 +371,27 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi } } - ui.notifications?.info?.('Archivist sync applied.'); - // Force-refresh core directories and any open Archivist windows so UI reflects new docs + const successCount = total - failCount; + if (failCount === 0) { + ui.notifications?.info?.(`Sync complete: ${successCount} item${successCount !== 1 ? 's' : ''} applied.`); + } else { + ui.notifications?.warn?.( + `Sync finished: ${successCount} succeeded, ${failCount} failed. See console for details.` + ); + } await this._refreshUIAfterSync?.(); - // Close the dialog after successful sync - this.close(); + await this._loadModel(true); } catch (error) { console.error('[SyncDialog] Sync failed:', error); ui.notifications?.error?.('Sync failed. See console for details.'); - // On error, reload model and stay open so user can retry await this._loadModel(true); - await this.render(); } finally { - // Resume real-time sync after apply + this.syncProgress = null; try { settingsManager.resumeRealtimeSync?.(); - console.log( - '[SyncDialog] ✓ Real-time sync resumed after sync operation' - ); + console.debug('[SyncDialog] Real-time sync resumed after sync operation'); } catch (_) {} + await this.render(); } } @@ -344,13 +459,15 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi return; } - const [chars, items, locs, facs, sessions, links] = await Promise.all([ + const [chars, items, locs, facs, sessions, links, journals, quests] = await Promise.all([ archivistApi.listCharacters(apiKey, campaignId), archivistApi.listItems(apiKey, campaignId), archivistApi.listLocations(apiKey, campaignId), archivistApi.listFactions(apiKey, campaignId), archivistApi.listSessions(apiKey, campaignId), archivistApi.listLinks(apiKey, campaignId), + archivistApi.listJournals(apiKey, campaignId), + archivistApi.listQuests(apiKey, campaignId), ]); const A = { characters: (chars?.success ? chars.data : []) || [], @@ -359,14 +476,18 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi factions: (facs?.success ? facs.data : []) || [], sessions: (sessions?.success ? sessions.data : []) || [], links: (links?.success ? links.data : []) || [], + journals: (journals?.success ? journals.data : []) || [], + quests: (quests?.success ? quests.data : []) || [], }; - console.log('[SyncDialog] Fetched Archivist data:', { + console.debug('[SyncDialog] Fetched Archivist data:', { characters: A.characters.length, items: A.items.length, locations: A.locations.length, factions: A.factions.length, sessions: A.sessions.length, links: A.links.length, + journals: A.journals.length, + quests: A.quests.length, }); const byId = { Character: new Map(A.characters.map((c) => [String(c.id), c])), @@ -374,6 +495,8 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi Location: new Map(A.locations.map((l) => [String(l.id), l])), Faction: new Map(A.factions.map((f) => [String(f.id), f])), Session: new Map(A.sessions.map((s) => [String(s.id), s])), + Journal: new Map(A.journals.map((j) => [String(j.id), j])), + Quest: new Map(A.quests.map((q) => [String(q.id), q])), }; // Compute outgoing links (from_id => [{ id: to_id, type: to_type }]) @@ -405,7 +528,11 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi ? 'Faction' : st === 'recap' || st === 'session' ? 'Session' - : null; + : st === 'journal' + ? 'Journal' + : st === 'quest' + ? 'Quest' + : null; if (!type) continue; const arch = byId[type].get(archId) || null; if (!arch) { @@ -425,9 +552,11 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi const archName = type === 'Character' ? arch.character_name || arch.name - : type === 'Session' + : type === 'Session' || type === 'Journal' ? arch.title || arch.name || '' - : arch.name || arch.title || ''; + : type === 'Quest' + ? arch.questName || arch.quest_name || arch.name || '' + : arch.name || arch.title || ''; if (String(j.name || '').trim() !== String(archName || '').trim()) { changes.name = { from: j.name, to: archName }; } @@ -437,7 +566,7 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi (j?.pages?.contents || []).find((p) => p.type === 'text') || null; const stored = Utils.extractPageHtml(textPage) || ''; const foundryPlain = Utils.toMarkdownIfHtml(stored); - const archMd = String((arch.description ?? arch.summary) || ''); + const archMd = String((arch.description ?? arch.summary ?? arch.content) || ''); const archHtml = Utils.markdownToStoredHtml(archMd); const archivistPlain = Utils.toMarkdownIfHtml(archHtml); @@ -545,7 +674,7 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi /* ignore */ } } - console.log('[SyncDialog] Linked Archivist IDs found in Foundry:', { + console.debug('[SyncDialog] Linked Archivist IDs found in Foundry:', { count: linkedIds.size, ids: Array.from(linkedIds).slice(0, 10), sample: Array.from(foundryJournalMap.entries()).slice(0, 5), @@ -584,7 +713,7 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi type, id, name: row.character_name || row.name || row.title || 'Untitled', - description: row.description || row.summary || '', + description: row.description || row.summary || row.content || '', image: row.image || '', selected: false, createCore: false, @@ -597,8 +726,14 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi for (const l of A.locations) pushImport('Location', l); for (const f of A.factions) pushImport('Faction', f); for (const s of A.sessions) pushImport('Session', s); + for (const j of A.journals) pushImport('Journal', { ...j, name: j.title || 'Untitled' }); + for (const q of A.quests) + pushImport('Quest', { + ...q, + name: q.questName || q.quest_name || 'Quest', + }); - console.log('[SyncDialog] Import candidates:', { + console.debug('[SyncDialog] Import candidates:', { imports: imports.length, skipped: skipped.length, importSample: imports @@ -756,10 +891,14 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi ? 'faction' : row.type === 'Session' ? 'recap' - : null; + : row.type === 'Journal' + ? 'journal' + : row.type === 'Quest' + ? 'quest' + : null; if (!sheetType) return; // Convert markdown from Archivist to HTML for Foundry storage (sessions use summary) - const markdownContent = String(row.description || row.summary || ''); + const markdownContent = String(row.description || row.summary || row.content || ''); const htmlContent = Utils.markdownToStoredHtml(markdownContent); // Determine folder ID based on sheet type using saved destinations @@ -779,6 +918,16 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi folderId = destinations.location; } else if (sheetType === 'faction' && destinations.faction) { folderId = destinations.faction; + } else if (sheetType === 'journal') { + // Worlds set up before journals existed have no saved destination — + // fall back to ensuring the default folder like recaps do. + folderId = + destinations.journal || + (await Utils.ensureJournalFolder('Archivist - Journals')); + } else if (sheetType === 'quest') { + folderId = + destinations.quest || + (await Utils.ensureJournalFolder('Archivist - Quests')); } else if (sheetType === 'recap') { // For sessions/recaps, use Recaps folder and preserve session_date ordering folderId = await Utils.ensureJournalFolder('Recaps'); @@ -789,7 +938,7 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi } } - console.log('[Sync Dialog] Importing to folder:', { + console.debug('[SyncDialog] Importing to folder:', { sheetType, folderId: folderId || 'root', name: row.name, @@ -809,6 +958,66 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi sort, }); if (!journal) return; + // For journals, set GM-only default permissions + if (sheetType === 'journal') { + try { + await journal.update( + { ownership: { default: CONST.DOCUMENT_OWNERSHIP_LEVELS.NONE } }, + { render: false } + ); + } catch (_) {} + } + // For quests, fetch full quest data and store in flags + if (sheetType === 'quest' && apiKey && row.id) { + try { + let fullQuest = row; + try { + const resp = await archivistApi.getQuest(apiKey, row.id); + if (resp.success && resp.data) fullQuest = resp.data; + } catch (_) {} + const flags = journal.getFlag(CONFIG.MODULE_ID, 'archivist') || {}; + flags.questData = { + questName: fullQuest.questName || fullQuest.quest_name || '', + questGiver: fullQuest.questGiver || fullQuest.quest_giver || '', + questCategory: + fullQuest.questCategory || fullQuest.quest_category || 'n/a', + status: fullQuest.status || 'planned', + successDefinition: + fullQuest.successDefinition || fullQuest.success_definition || '', + failureConditions: + fullQuest.failureConditions || fullQuest.failure_conditions || '', + nextAction: fullQuest.nextAction || fullQuest.next_action || '', + resolution: fullQuest.resolution || '', + objectives: Array.isArray(fullQuest.objectives) ? fullQuest.objectives : [], + progressLog: Array.isArray(fullQuest.progressLog) + ? fullQuest.progressLog + : Array.isArray(fullQuest.progress_log) + ? fullQuest.progress_log + : Array.isArray(fullQuest.progressLogEntries) + ? fullQuest.progressLogEntries.map((e) => + typeof e === 'string' ? e : e.text || '' + ) + : Array.isArray(fullQuest.progress_log_entries) + ? fullQuest.progress_log_entries.map((e) => + typeof e === 'string' ? e : e.text || '' + ) + : [], + relatedCharacters: + fullQuest.relatedCharacters || fullQuest.related_characters || [], + relatedFactions: + fullQuest.relatedFactions || fullQuest.related_factions || [], + relatedLocations: + fullQuest.relatedLocations || fullQuest.related_locations || [], + relatedItems: + fullQuest.relatedItems || fullQuest.related_items || [], + relatedEntityRefs: + fullQuest.relatedEntityRefs || fullQuest.related_entity_refs || [], + firstSession: fullQuest.firstSession || fullQuest.first_session || null, + lastSession: fullQuest.lastSession || fullQuest.last_session || null, + }; + await journal.setFlag(CONFIG.MODULE_ID, 'archivist', flags); + } catch (_) {} + } // For locations, set parent relation from Archivist's parent_id if (sheetType === 'location' && row.parent_id) { try { @@ -995,7 +1204,7 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi else if (itemId) targetDoc = game.items?.get?.(itemId) || null; else if (sceneId) targetDoc = game.scenes?.get?.(sceneId) || null; - const md = String(row.description || row.summary || ''); + const md = String(row.description || row.summary || row.content || ''); const html = Utils.markdownToStoredHtml(md); if (targetDoc) { @@ -1014,5 +1223,3 @@ export class SyncDialog extends foundry.applications.api.HandlebarsApplicationMi } catch (_) {} } } - -export const ArchivistSyncDialog = SyncDialog; diff --git a/scripts/dialogs/world-setup-dialog.js b/scripts/dialogs/world-setup-dialog.js index 6ef96a3..e1f7d4c 100644 --- a/scripts/dialogs/world-setup-dialog.js +++ b/scripts/dialogs/world-setup-dialog.js @@ -24,36 +24,17 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica selectedWorldId: '', selectedWorldName: '', setupComplete: false, - // Mapping removed - systemPreset: '', destinations: { pc: '', npc: '', item: '', location: '', faction: '' }, - // Step 4 selections - selections: { pcs: [], npcs: [], items: [], locations: [], factions: [] }, - // Step 5: Create Foundry core objects choices createFoundry: { actors: [], items: [], scenes: [] }, }; - // Mapping discovery caches removed - this.folderOptions = { - pc: [], - npc: [], - item: [], - location: [], - faction: [], - }; - // Mapping options removed (keep empty object to avoid legacy access) - this.mappingOptions = { actor: [], item: [] }; this.archivistCandidates = { characters: [], items: [], locations: [], factions: [], - }; - this.eligibleDocs = { - pcs: [], - npcs: [], - items: [], - locations: [], - factions: [], + journals: [], + journalFolders: [], + quests: [], }; this.syncPlan = { createInFoundry: [], createInArchivist: [], link: [] }; this.syncStatus = { @@ -193,25 +174,11 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica nextStep: WorldSetupDialog.prototype._onNextStep, prevStep: WorldSetupDialog.prototype._onPrevStep, validateApiKey: WorldSetupDialog.prototype._onValidateApiKey, - syncWorlds: WorldSetupDialog.prototype._onSyncWorlds, - selectWorld: WorldSetupDialog.prototype._onSelectWorld, openDocumentation: WorldSetupDialog.prototype._onOpenDocumentation, - // Step 3 actions loadCampaigns: WorldSetupDialog.prototype._onLoadCampaigns, createCampaign: WorldSetupDialog.prototype._onCreateCampaign, - campaignSelectChange: WorldSetupDialog.prototype._onCampaignSelectChange, - // Step 5 actions prepareSelections: WorldSetupDialog.prototype._onPrepareSelections, - toggleSelection: WorldSetupDialog.prototype._onToggleSelection, - changeMatch: WorldSetupDialog.prototype._onChangeMatch, - confirmSelections: WorldSetupDialog.prototype._onConfirmSelections, - selectAll: WorldSetupDialog.prototype._onSelectAll, - selectNone: WorldSetupDialog.prototype._onSelectNone, - // Step 6 actions beginSync: WorldSetupDialog.prototype._onBeginSync, - // Configuration file actions - downloadSampleConfig: WorldSetupDialog.prototype._onDownloadSampleConfig, - // Finalization completeSetup: WorldSetupDialog.prototype._onCompleteSetup, cancel: WorldSetupDialog.prototype._onCancel, }, @@ -227,366 +194,11 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica }, }; - /** - * Discover string properties in object model recursively - * @param {Object} obj - Object to traverse - * @param {string} basePath - Current path prefix - * @param {Array} results - Array to collect results - * @param {number} maxDepth - Maximum recursion depth - * @param {number} currentDepth - Current recursion depth - * @private - */ - _discoverStringProperties( - obj, - basePath = '', - results = [], - maxDepth = 6, - currentDepth = 0 - ) { - if (!obj || currentDepth >= maxDepth || typeof obj !== 'object') - return results; - - for (const [key, value] of Object.entries(obj)) { - const path = basePath ? `${basePath}.${key}` : key; - - if (typeof value === 'string' && value.trim().length > 0) { - // Only include non-empty string properties - results.push({ - path, - type: 'string', - sample: value.length > 50 ? value.substring(0, 47) + '...' : value, - }); - } else if (value && typeof value === 'object' && !Array.isArray(value)) { - // Recurse into nested objects - this._discoverStringProperties( - value, - path, - results, - maxDepth, - currentDepth + 1 - ); - } - } - - return results; - } - - /** - * Prepare mapping options by discovering string properties in Actor and Item models - * Uses temporary document creation to ensure complete property discovery - * @private - */ - async _prepareMappingOptions() { - try { - console.log('Archivist Sync | Discovering document properties...'); - - // Discover Actor properties by creating temporary actors - this.mappingOptions.actor = await this._discoverActorProperties(); - - // Discover Item properties by creating temporary items - this.mappingOptions.item = await this._discoverItemProperties(); - - // Sort options with priority for commonly used fields - const sortWithPriority = (options, priorityFields = []) => { - return options.sort((a, b) => { - const aPriority = priorityFields.indexOf(a.path); - const bPriority = priorityFields.indexOf(b.path); - - // If both are priority fields, sort by priority order - if (aPriority !== -1 && bPriority !== -1) { - return aPriority - bPriority; - } - - // Priority fields come first - if (aPriority !== -1) return -1; - if (bPriority !== -1) return 1; - - // Then sort by path depth (simpler first), then alphabetically - const depthDiff = a.path.split('.').length - b.path.split('.').length; - return depthDiff !== 0 ? depthDiff : a.path.localeCompare(b.path); - }); - }; - - const actorPriorityFields = [ - 'name', - 'img', - 'system.details.biography.value', - 'system.details.biography.public', - 'system.details.trait', - 'system.details.ideal', - 'system.details.bond', - 'system.details.flaw', - 'system.details.appearance', - 'system.description.value', - ]; - - const itemPriorityFields = [ - 'name', - 'img', - 'system.description.value', - 'system.description.short', - 'system.description.chat', - ]; - - this.mappingOptions.actor = sortWithPriority( - this.mappingOptions.actor, - actorPriorityFields - ); - this.mappingOptions.item = sortWithPriority( - this.mappingOptions.item, - itemPriorityFields - ); - - console.log( - `Archivist Sync | Property discovery complete: ${this.mappingOptions.actor.length} actor properties, ${this.mappingOptions.item.length} item properties` - ); - } catch (error) { - console.warn('Error preparing mapping options:', error); - // Ensure we have fallback options - this.mappingOptions.actor = [ - { path: 'name', type: 'string', sample: 'Actor name' }, - { path: 'img', type: 'string', sample: 'Actor image path' }, - ]; - this.mappingOptions.item = [ - { path: 'name', type: 'string', sample: 'Item name' }, - { path: 'img', type: 'string', sample: 'Item image path' }, - ]; - } - } - - /** - * Discover Actor properties by creating temporary actors of different types - * Uses the new Document() constructor (v13+) instead of deprecated { temporary: true } - * @private - */ - async _discoverActorProperties() { - const allProperties = new Map(); // Use Map to avoid duplicates by path - const actorTypes = ['character', 'npc']; // Common D&D 5e actor types - - // First, try to use existing actors - const existingActors = game.actors?.contents || []; - for (const actor of existingActors.slice(0, 2)) { - // Sample max 2 existing - try { - const actorData = actor.toObject(); - const properties = this._discoverStringProperties(actorData); - for (const prop of properties) { - allProperties.set(prop.path, prop); - } - console.log( - `Archivist Sync | Discovered ${properties.length} properties from existing ${actor.type} actor: ${actor.name}` - ); - } catch (error) { - console.warn( - `Error discovering properties from existing actor ${actor.name}:`, - error - ); - } - } - - // Try to discover properties from system data model templates instead of creating actors - for (const actorType of actorTypes) { - const hasExistingOfType = existingActors.some( - (a) => a.type === actorType - ); - - if (!hasExistingOfType) { - try { - console.log( - `Archivist Sync | Attempting system template discovery for ${actorType} actor` - ); - - // Try to access system data model template if available - let templateProperties = []; - - // Check if we can access the system's data model templates - if (game.system?.model?.Actor?.[actorType]) { - console.log( - `Archivist Sync | Found system template for ${actorType}` - ); - const template = game.system.model.Actor[actorType]; - templateProperties = this._discoverStringProperties(template); - - for (const prop of templateProperties) { - // Prefix with 'system.' since these are system model properties - const systemProp = { - ...prop, - path: prop.path.startsWith('system.') - ? prop.path - : `system.${prop.path}`, - sample: `${prop.sample} (from template)`, - }; - allProperties.set(systemProp.path, systemProp); - } - - console.log( - `Archivist Sync | Discovered ${templateProperties.length} properties from ${actorType} system template` - ); - } else { - // Fallback: use predefined property sets for this actor type - console.log( - `Archivist Sync | No system template found for ${actorType}, using fallback properties` - ); - const fallbackProperties = - this._getFallbackActorProperties(actorType); - for (const prop of fallbackProperties) { - allProperties.set(prop.path, prop); - } - console.log( - `Archivist Sync | Used ${fallbackProperties.length} fallback properties for ${actorType} actor` - ); - } - } catch (error) { - console.warn( - `Error discovering properties for ${actorType} actor:`, - error - ); - - // Final fallback: use predefined property sets - try { - const fallbackProperties = - this._getFallbackActorProperties(actorType); - for (const prop of fallbackProperties) { - allProperties.set(prop.path, prop); - } - console.log( - `Archivist Sync | Used fallback properties for ${actorType} actor after error` - ); - } catch (fallbackError) { - console.warn( - `Fallback also failed for ${actorType}:`, - fallbackError - ); - } - } - } - } - - // Add known D&D 5e paths that might not have been discovered - const knownPaths = [ - { path: 'name', sample: 'Actor name' }, - { path: 'img', sample: 'Actor image path' }, - { path: 'system.details.biography.value', sample: 'Full biography text' }, - { - path: 'system.details.biography.public', - sample: 'Public biography text', - }, - { path: 'system.description.value', sample: 'Description text' }, - { path: 'system.details.trait', sample: 'Personality traits' }, - { path: 'system.details.ideal', sample: 'Ideals' }, - { path: 'system.details.bond', sample: 'Bonds' }, - { path: 'system.details.flaw', sample: 'Flaws' }, - { path: 'system.details.appearance', sample: 'Physical appearance' }, - { path: 'system.details.background', sample: 'Character background' }, - { path: 'system.details.race', sample: 'Character race' }, - { path: 'system.details.class', sample: 'Character class' }, - { path: 'system.biography.value', sample: 'Biography (alt path)' }, - { - path: 'system.biography.public', - sample: 'Public biography (alt path)', - }, - ]; - - for (const pathInfo of knownPaths) { - if (!allProperties.has(pathInfo.path)) { - allProperties.set(pathInfo.path, { - path: pathInfo.path, - type: 'string', - sample: pathInfo.sample, - }); - } - } - - return Array.from(allProperties.values()); - } - - /** - * Get fallback properties for actor types when temporary document creation fails - * @private - */ - _getFallbackActorProperties(actorType) { - const baseProperties = [ - { path: 'name', type: 'string', sample: 'Actor name' }, - { path: 'img', type: 'string', sample: 'Actor image path' }, - ]; - - if (actorType === 'character') { - return [ - ...baseProperties, - { - path: 'system.details.biography.value', - type: 'string', - sample: 'Full biography text', - }, - { - path: 'system.details.trait', - type: 'string', - sample: 'Personality traits', - }, - { path: 'system.details.ideal', type: 'string', sample: 'Ideals' }, - { path: 'system.details.bond', type: 'string', sample: 'Bonds' }, - { path: 'system.details.flaw', type: 'string', sample: 'Flaws' }, - { - path: 'system.details.appearance', - type: 'string', - sample: 'Physical appearance', - }, - { - path: 'system.details.background', - type: 'string', - sample: 'Character background', - }, - ]; - } else if (actorType === 'npc') { - return [ - ...baseProperties, - { - path: 'system.details.biography.value', - type: 'string', - sample: 'Full biography text', - }, - { - path: 'system.details.biography.public', - type: 'string', - sample: 'Public biography text', - }, - { - path: 'system.description.value', - type: 'string', - sample: 'Description text', - }, - ]; - } - - return baseProperties; - } - /** * Prepare context data for template rendering * @returns {Object} Template data */ async _prepareContext() { - // Prepare folder options when needed - try { - const folders = game.folders?.contents || []; - const pick = (type) => - folders - .filter((f) => f.type === type) - .map((f) => ({ id: f.id, name: f.name, depth: f.depth || 0 })) - .sort((a, b) => a.depth - b.depth || a.name.localeCompare(b.name)); - this.folderOptions = { - pc: pick('Actor'), - npc: pick('Actor'), - item: pick('Item'), - location: pick('JournalEntry'), - faction: pick('JournalEntry'), - }; - } catch (_) { - /* no-op */ - } - - // Mapping step removed; skip legacy mapping preparation entirely - const contextData = { currentStep: this.currentStep, totalSteps: this.totalSteps, @@ -596,8 +208,6 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica foundryWorldDescription: game.world.description || '', worlds: this.worlds, setupData: this.setupData, - folderOptions: this.folderOptions, - mappingOptions: this.mappingOptions, archivistCandidates: this.archivistCandidates, syncPlan: this.syncPlan, syncStatus: this.syncStatus, @@ -607,7 +217,14 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica (this.syncStatus.processed / this.syncStatus.total) * 100 ) : 0, - steps: Array.from({ length: this.totalSteps }, (_, i) => i + 1), + steps: [ + { num: 1, label: 'Welcome' }, + { num: 2, label: 'API Key' }, + { num: 3, label: 'Campaign' }, + { num: 4, label: 'Reconcile' }, + { num: 5, label: 'Create' }, + { num: 6, label: 'Sync' }, + ], // Step-specific data isStep1: this.currentStep === 1, isStep2: this.currentStep === 2, @@ -689,6 +306,8 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica // Import-only buckets count.imp.factions = this.archivistCandidates?.factions?.length || 0; count.imp.recaps = this.archivistCandidates?.recaps?.length || 0; + count.imp.journals = this.archivistCandidates?.journals?.length || 0; + count.imp.quests = this.archivistCandidates?.quests?.length || 0; imp = count.imp; exp = count.exp; @@ -752,6 +371,14 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica linked: 0, noteNoExport: true, }, + { + key: 'Quests', + fromArchivist: Number(imp.quests || 0), + createFoundry: 0, + toArchivist: 0, + linked: 0, + noteNoExport: true, + }, ]; // Set warning flag if any of Characters, Items, or Locations have > 100 exports @@ -795,7 +422,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica } catch (_) {} if (this.currentStep === 4) { - console.log('[World Setup] _prepareContext for Step 4:', { + console.debug('[World Setup] _prepareContext for Step 4:', { hasReconcileData: !!contextData.setupData?.reconcile, hasAnyCandidates: contextData.setupData?.hasAnyCandidates, isLoading: contextData.isLoading, @@ -853,14 +480,14 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica */ async _onNextStep(event) { event.preventDefault(); - console.log( + console.debug( '[World Setup] _onNextStep called, currentStep before increment:', this.currentStep ); // Special handling: if leaving step 4, must build sync plan first if (this.currentStep === 4 && this._canProceedFromCurrentStep()) { - console.log( + console.debug( '[World Setup] _onNextStep from step 4: building sync plan via _onConfirmSelections' ); await this._onConfirmSelections(event); @@ -873,13 +500,13 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica this._canProceedFromCurrentStep() ) { this.currentStep++; - console.log( + console.debug( '[World Setup] _onNextStep, currentStep after increment:', this.currentStep ); // Auto-prepare reconciliation data when entering Step 4 if (this.currentStep === 4) { - console.log( + console.debug( '[World Setup] _onNextStep triggering _onPrepareSelections for Step 4' ); await this._onPrepareSelections(event); @@ -887,7 +514,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica } // Prepare create choices when entering Step 5 if (this.currentStep === 5) { - console.log( + console.debug( '[World Setup] _onNextStep preparing create choices for Step 5' ); await this._prepareCreateFoundryChoices(); @@ -896,7 +523,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica } await this.render(); } else { - console.log('[World Setup] _onNextStep cannot proceed:', { + console.debug('[World Setup] _onNextStep cannot proceed:', { currentStep: this.currentStep, totalSteps: this.totalSteps, canProceed: this._canProceedFromCurrentStep(), @@ -999,16 +626,6 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica } } - /** - * Handle sync worlds button click - * @param {Event} event - Click event - * @private - */ - async _onSyncWorlds(event) { - // Delegate to load campaigns (legacy compatibility) - return await this._onLoadCampaigns(event); - } - /** * Step 3: Load user's campaigns using validated API key */ @@ -1016,19 +633,13 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica event?.preventDefault?.(); try { this.isLoading = true; - try { - await this.render(); - } catch (_) { - /* ignore after close */ - } + await this.render(); const apiKey = this.setupData.apiKey || settingsManager.getApiKey(); if (!apiKey) throw new Error('Missing API key'); const resp = await archivistApi.fetchCampaignsList(apiKey); if (resp.success) { this.worlds = resp.data || []; - // Move to step 3 if not already there if (this.currentStep < 3) this.currentStep = 3; - await this.render(); } else { ui.notifications.error(resp.message || 'Failed to load campaigns'); } @@ -1102,14 +713,14 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica async _onPrepareSelections(event) { event?.preventDefault?.(); try { - console.log('[World Setup] Starting _onPrepareSelections'); + console.debug('[World Setup] Starting _onPrepareSelections'); this.isLoading = true; await this.render(); const apiKey = this.setupData.apiKey || settingsManager.getApiKey(); // Always prefer the explicit selection from Step 3; fallback to saved setting only if necessary const campaignId = this.setupData.selectedWorldId || settingsManager.getSelectedWorldId(); - console.log('[World Setup] Using campaignId:', campaignId); + console.debug('[World Setup] Using campaignId:', campaignId); if (!campaignId) { ui.notifications.warn( 'Please select a campaign in Step 3 before continuing.' @@ -1123,27 +734,33 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica const foundryActors = getAll(game.actors); const foundryItems = getAll(game.items); const foundryScenes = getAll(game.scenes); - console.log('[World Setup] Foundry docs:', { + console.debug('[World Setup] Foundry docs:', { actors: foundryActors.length, items: foundryItems.length, scenes: foundryScenes.length, }); // Archivist side - console.log('[World Setup] Fetching Archivist data...'); - const [chars, its, locs, facs, sessions] = await Promise.all([ + console.debug('[World Setup] Fetching Archivist data...'); + const [chars, its, locs, facs, sessions, journals, journalFolders, quests] = await Promise.all([ archivistApi.listCharacters(apiKey, campaignId), archivistApi.listItems(apiKey, campaignId), archivistApi.listLocations(apiKey, campaignId), archivistApi.listFactions(apiKey, campaignId), archivistApi.listSessions(apiKey, campaignId), + archivistApi.listJournals(apiKey, campaignId), + archivistApi.listJournalFolders(apiKey, campaignId), + archivistApi.listQuests(apiKey, campaignId), ]); - console.log('[World Setup] Archivist API responses:', { + console.debug('[World Setup] Archivist API responses:', { chars, its, locs, facs, sessions, + journals, + journalFolders, + quests, }); this.archivistCandidates.characters = chars.success ? chars.data || [] @@ -1154,16 +771,28 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica this.archivistCandidates.recaps = sessions.success ? sessions.data || [] : []; - console.log('[World Setup] Archivist docs:', { + this.archivistCandidates.journals = journals.success + ? journals.data || [] + : []; + this.archivistCandidates.journalFolders = journalFolders.success + ? journalFolders.data || [] + : []; + this.archivistCandidates.quests = quests.success + ? quests.data || [] + : []; + console.debug('[World Setup] Archivist docs:', { characters: this.archivistCandidates.characters.length, items: this.archivistCandidates.items.length, locations: this.archivistCandidates.locations.length, factions: this.archivistCandidates.factions.length, recaps: this.archivistCandidates.recaps.length, + journals: this.archivistCandidates.journals.length, + journalFolders: this.archivistCandidates.journalFolders.length, + quests: this.archivistCandidates.quests.length, }); // Build reconciliation model with initial matches and selections - console.log('[World Setup] Building reconciliation model...'); + console.debug('[World Setup] Building reconciliation model...'); const reconcile = this._buildReconciliationModel({ archivist: { characters: this.archivistCandidates.characters, @@ -1177,8 +806,8 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica scenes: foundryScenes, }, }); - console.log('[World Setup] Reconciliation model built:', reconcile); - console.log('[World Setup] Detailed reconciliation counts:', { + console.debug('[World Setup] Reconciliation model built:', reconcile); + console.debug('[World Setup] Detailed reconciliation counts:', { charactersArchivist: reconcile?.characters?.archivist?.length || 0, charactersFoundry: reconcile?.characters?.foundry?.length || 0, itemsArchivist: reconcile?.items?.archivist?.length || 0, @@ -1200,30 +829,24 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica (reconcile?.factions?.archivist?.length || 0) + (reconcile?.factions?.foundry?.length || 0) ); - console.log( + console.debug( '[World Setup] Has candidates:', this.setupData.hasAnyCandidates ); - console.log( + console.debug( '[World Setup] setupData.reconcile assigned:', this.setupData.reconcile ); - console.log( + console.debug( '[World Setup] Sample archivist character (first item):', reconcile?.characters?.archivist?.[0] ); - console.log( + console.debug( '[World Setup] Sample archivist location (first item):', reconcile?.locations?.archivist?.[0] ); - // stay on Selection step after refresh this.currentStep = 4; - try { - await this.render(); - } catch (_) { - /* ignore after close */ - } } catch (e) { console.error('Prepare selections error', e); ui.notifications.error('Failed to prepare selections'); @@ -1237,46 +860,6 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica } } - _updateSelection(kind, id, updates) { - const arr = this.setupData.selections[kind] || []; - const idx = arr.findIndex((x) => x.id === id); - if (idx >= 0) Object.assign(arr[idx], updates); - } - - async _onToggleSelection(event) { - const el = event?.target; - const kind = el?.dataset?.kind; - const id = el?.dataset?.id; - if (!kind || !id) return; - this._updateSelection(kind, id, { selected: el.checked }); - } - - async _onChangeMatch(event) { - const el = event?.target; - const kind = el?.dataset?.kind; - const id = el?.dataset?.id; - if (!kind || !id) return; - this._updateSelection(kind, id, { - match: { id: el.value, label: el.options[el.selectedIndex]?.text || '' }, - }); - } - - async _onSelectAll(event) { - const kind = event?.target?.dataset?.kind; - if (!kind) return; - (this.setupData.selections[kind] || []).forEach((s) => (s.selected = true)); - await this.render(); - } - - async _onSelectNone(event) { - const kind = event?.target?.dataset?.kind; - if (!kind) return; - (this.setupData.selections[kind] || []).forEach( - (s) => (s.selected = false) - ); - await this.render(); - } - async _onConfirmSelections(event) { event?.preventDefault?.(); // Build plan from reconciliation model @@ -1455,6 +1038,14 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica plan.importFromArchivist.recaps = this.archivistCandidates.recaps?.length || 0; + // Journals (Archivist-only, import all with folder structure) + plan.importFromArchivist.journals = + this.archivistCandidates.journals?.length || 0; + + // Quests (Archivist-only, import all) + plan.importFromArchivist.quests = + this.archivistCandidates.quests?.length || 0; + this.syncPlan = plan; this.currentStep = 5; await this._prepareCreateFoundryChoices(); @@ -1503,7 +1094,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica }); } catch (_) { // User cancelled or closed the dialog - do not proceed with sync - console.log('[World Setup] Sync cancelled by user'); + console.debug('[World Setup] Sync cancelled by user'); return; } @@ -1542,7 +1133,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica const campaignId = this.setupData.selectedWorldId || settingsManager.getSelectedWorldId(); - console.log('Archivist Sync | Beginning sync with:', { + console.debug('Archivist Sync | Beginning sync with:', { apiKey: apiKey ? `***${apiKey.slice(-4)}` : 'none', campaignId, }); @@ -1567,11 +1158,11 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica await this.render(); return; } - console.log('[World Setup] ✓ Real-time sync successfully suppressed'); + console.debug('[World Setup] ✓ Real-time sync successfully suppressed'); // Ensure Journal folders exist for each sheet type BEFORE importing try { - console.log('[World Setup] Creating organized folders...'); + console.debug('[World Setup] Creating organized folders...'); const ensureFolder = async (name) => { try { return await Utils.ensureJournalFolder(name); @@ -1586,15 +1177,19 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica location: await ensureFolder('Archivist - Locations'), faction: await ensureFolder('Archivist - Factions'), recap: await ensureFolder('Recaps'), + journal: await ensureFolder('Archivist - Journals'), + quest: await ensureFolder('Archivist - Quests'), }; - console.log('[World Setup] Folders created:', { + console.debug('[World Setup] Folders created:', { pc: folders.pc || 'failed', npc: folders.npc || 'failed', item: folders.item || 'failed', location: folders.location || 'failed', faction: folders.faction || 'failed', recap: folders.recap || 'failed', + journal: folders.journal || 'failed', + quest: folders.quest || 'failed', }); this.setupData.destinations = { @@ -1603,14 +1198,18 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica item: folders.item, location: folders.location, faction: folders.faction, + journal: folders.journal, + quest: folders.quest, + recap: folders.recap, }; - console.log('[World Setup] Destinations set:', { + console.debug('[World Setup] Destinations set:', { pc: this.setupData.destinations.pc || 'none', npc: this.setupData.destinations.npc || 'none', item: this.setupData.destinations.item || 'none', location: this.setupData.destinations.location || 'none', faction: this.setupData.destinations.faction || 'none', + recap: this.setupData.destinations.recap || 'none', }); } catch (e) { console.error('[World Setup] Folder creation failed:', e); @@ -1621,7 +1220,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica ...(this.syncPlan.createInArchivist || []), ...(this.syncPlan.link || []), ]; - console.log(`Archivist Sync | Processing ${work.length} sync jobs:`, { + console.debug(`Archivist Sync | Processing ${work.length} sync jobs:`, { createInArchivist: this.syncPlan.createInArchivist?.length || 0, link: this.syncPlan.link?.length || 0, }); @@ -1971,7 +1570,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica type: job.kind, campaign_id: campaignId, }; - console.log(`Archivist Sync | Creating ${job.kind} character:`, { + console.debug(`Archivist Sync | Creating ${job.kind} character:`, { name: payload.character_name, campaignId, }); @@ -1981,7 +1580,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica ...getMappedFields('Item', doc), campaign_id: campaignId, }; - console.log(`Archivist Sync | Creating item:`, { + console.debug(`Archivist Sync | Creating item:`, { name: payload.name, campaignId, }); @@ -2000,7 +1599,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica ...(image ? { image } : {}), campaign_id: campaignId, }; - console.log(`Archivist Sync | Creating location from Scene:`, { + console.debug(`Archivist Sync | Creating location from Scene:`, { name: payload.name, campaignId, }); @@ -2047,7 +1646,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica html = Utils.toMarkdownIfHtml(String(desc || '')); imageUrl = doc?.img || undefined; - console.log( + console.debug( `[World Setup] Creating journal for exported ${job.kind}:`, { name: doc.name, @@ -2088,7 +1687,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica html = Utils.toMarkdownIfHtml(String(desc || '')); imageUrl = doc?.img || undefined; - console.log(`[World Setup] Creating journal for exported Item:`, { + console.debug(`[World Setup] Creating journal for exported Item:`, { name: doc.name, archivistId: newId, targetFolderId: targetFolderId || 'none', @@ -2123,7 +1722,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica html = ''; imageUrl = doc?.thumb || doc?.background?.src || undefined; - console.log( + console.debug( `[World Setup] Creating journal for exported Location:`, { name: doc.name, @@ -2177,7 +1776,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica await settingsManager.setJournalDestinations( this.setupData.destinations ); - console.log( + console.debug( '[World Setup] Journal destinations saved to settings:', this.setupData.destinations ); @@ -2194,7 +1793,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica // Resume realtime sync now that batch operations are complete try { settingsManager.resumeRealtimeSync?.(); - console.log( + console.debug( '[World Setup] ✓ Real-time sync resumed after successful setup' ); } catch (_) {} @@ -2214,16 +1813,12 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica : `Failed to process ${count} entities: ${errorNames}. Their descriptions exceed the maximum length of 10,000 characters. Please shorten the descriptions and try again.`; ui.notifications.error(message, { permanent: true }); } else { - ui.notifications.info('Sync completed'); + ui.notifications.info('Sync completed successfully. Click "Complete Setup" to finish.'); } - - try { - await this.close(); - } catch (_) {} } catch (e) { try { settingsManager.resumeRealtimeSync?.(); - console.log('[World Setup] Real-time sync resumed after error'); + console.debug('[World Setup] Real-time sync resumed after error'); } catch (_) {} console.error('[World Setup] ❌ Begin sync failed', e); ui.notifications.error('Sync failed'); @@ -2241,24 +1836,35 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica // Removed Archivist journal folder setup; journals are created only in user destinations - async _importArchivistMissing(apiKey, campaignId, jf) { - try { - console.log('Archivist Sync | Importing existing data from Archivist...'); - - // Always import existing Archivist data to Foundry during world setup - // This ensures that existing campaign data is available in the new Foundry world + /** + * Update the Step 6 progress bar/text in place instead of re-rendering the + * whole wizard step for every imported entity. The panel is rendered once + * before the import loops begin; the final render after the loops reflects + * the completed state. + * @private + */ + _updateSyncStatusUI() { + const root = this.element; + if (!root || !this.syncStatus?.total) return; + const { processed, total, current } = this.syncStatus; + const pct = total > 0 ? Math.round((processed / total) * 100) : 0; + const fill = root.querySelector('.ws-sync-progress-fill'); + if (fill) fill.style.width = `${pct}%`; + const text = root.querySelector('.ws-sync-progress-text'); + if (text) { + text.textContent = `${processed} / ${total} — ${current || ''}`; + } + } - // Pull candidates from Archivist - const [chars, its, locs, facs] = await Promise.all([ - archivistApi.listCharacters(apiKey, campaignId), - archivistApi.listItems(apiKey, campaignId), - archivistApi.listLocations(apiKey, campaignId), - archivistApi.listFactions(apiKey, campaignId), - ]); - const allCharacters = chars.success ? chars.data || [] : []; - const allItems = its.success ? its.data || [] : []; - const allLocations = locs.success ? locs.data || [] : []; - const factions = facs.success ? facs.data || [] : []; + async _importArchivistMissing(apiKey, campaignId) { + try { + const allCharacters = this.archivistCandidates?.characters || []; + const allItems = this.archivistCandidates?.items || []; + const allLocations = this.archivistCandidates?.locations || []; + const factions = this.archivistCandidates?.factions || []; + const archivistJournals = this.archivistCandidates?.journals || []; + const archivistJournalFolders = this.archivistCandidates?.journalFolders || []; + const archivistQuests = this.archivistCandidates?.quests || []; // Build reconciliation lookup maps to avoid duplicating existing Foundry docs when user mapped them const reconcile = this.setupData?.reconcile || {}; @@ -2315,13 +1921,14 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica ); const total = - characters.length + items.length + locations.length + factions.length; - console.log( - `Archivist Sync | Found ${characters.length} characters, ${items.length} items, ${locations.length} locations, ${factions.length} factions in Archivist` + characters.length + items.length + locations.length + factions.length + + archivistJournals.length + archivistQuests.length; + console.debug( + `Archivist Sync | Found ${characters.length} characters, ${items.length} items, ${locations.length} locations, ${factions.length} factions, ${archivistJournals.length} journals, ${archivistQuests.length} quests in Archivist` ); if (!total) { - console.log( + console.debug( 'Archivist Sync | No existing data found in Archivist to import' ); return; @@ -2522,7 +2129,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica }; } - console.log(`Archivist Sync | Creating ${archivistType} actor:`, { + console.debug(`Archivist Sync | Creating ${archivistType} actor:`, { name: actorData.name, type: foundryType, folderId, @@ -2549,7 +2156,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica ? this.setupData.destinations.npc : this.setupData.destinations.pc; - console.log(`[World Setup] Creating journal for ${archivistType}:`, { + console.debug(`[World Setup] Creating journal for ${archivistType}:`, { name, archivistId: c.id, archivistType, @@ -2654,7 +2261,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica }; // Description no longer written into item system during setup - console.log(`Archivist Sync | Creating item:`, { + console.debug(`Archivist Sync | Creating item:`, { name: itemData.name, folderId, hasDescription: !!i.description, @@ -2674,7 +2281,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica const imageUrl = i.image || undefined; const targetFolderId = this.setupData.destinations.item; - console.log(`[World Setup] Creating journal for Item:`, { + console.debug(`[World Setup] Creating journal for Item:`, { name, archivistId: i.id, targetFolderId: targetFolderId || 'none', @@ -2736,7 +2343,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica const sheetType = kind.toLowerCase(); - console.log(`[World Setup] Creating journal for ${kind}:`, { + console.debug(`[World Setup] Creating journal for ${kind}:`, { name, archivistId: e.id, sheetType, @@ -2804,40 +2411,202 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica for (const c of characters) { this.syncStatus.current = `Import ${c.type || c.character_type || 'PC'}: ${c.character_name || c.name}`; - await this.render(); await createActor(c); this.syncStatus.processed++; - await this.render(); + this._updateSyncStatusUI(); } for (const it of items) { this.syncStatus.current = `Import Item: ${it.name}`; - await this.render(); await createItem(it); this.syncStatus.processed++; - await this.render(); + this._updateSyncStatusUI(); } - // Insert Locations alphabetically locations.sort((a, b) => String(a.name || '').localeCompare(String(b.name || '')) ); for (const l of locations) { this.syncStatus.current = `Import Location: ${l.name || l.title}`; - await this.render(); await upsertIntoContainer(l, 'Location'); this.syncStatus.processed++; - await this.render(); + this._updateSyncStatusUI(); } - // Insert Factions alphabetically factions.sort((a, b) => String(a.name || '').localeCompare(String(b.name || '')) ); for (const f of factions) { this.syncStatus.current = `Import Faction: ${f.name || f.title}`; - await this.render(); await upsertIntoContainer(f, 'Faction'); this.syncStatus.processed++; - await this.render(); + this._updateSyncStatusUI(); } + + // Import Journals with folder structure (GM-only, point-in-time snapshot) + if (archivistJournals.length) { + const journalRootFolderId = this.setupData.destinations.journal || null; + const folderIdMap = new Map(); + + // Build Foundry sub-folders mirroring Archivist journal folder paths + const sortedFolders = [...archivistJournalFolders].sort((a, b) => + (a.path || '').localeCompare(b.path || '') + ); + for (const af of sortedFolders) { + try { + const segments = (af.path || af.name || '').split('/').filter(Boolean); + let parentId = journalRootFolderId; + let currentPath = ''; + for (const seg of segments) { + currentPath = currentPath ? `${currentPath}/${seg}` : seg; + if (folderIdMap.has(currentPath)) { + parentId = folderIdMap.get(currentPath); + continue; + } + const existing = (game.folders?.contents || []).find( + (f) => + f.type === 'JournalEntry' && + f.name === seg && + (f.folder?.id || null) === (parentId || null) + ); + if (existing) { + folderIdMap.set(currentPath, existing.id); + parentId = existing.id; + } else { + const created = await Folder.create( + { name: seg, type: 'JournalEntry', folder: parentId || null }, + { render: false } + ); + folderIdMap.set(currentPath, created.id); + parentId = created.id; + } + } + folderIdMap.set(String(af.id), parentId); + } catch (e) { + console.warn('[World Setup] Failed to create journal folder', af, e); + } + } + + for (const j of archivistJournals) { + this.syncStatus.current = `Import Journal: ${j.title || 'Untitled'}`; + try { + const folderId = j.folder_id + ? folderIdMap.get(String(j.folder_id)) || journalRootFolderId + : journalRootFolderId; + const html = Utils.toMarkdownIfHtml(String(j.content || j.summary || '')); + const imageUrl = + typeof j.cover_image === 'string' && j.cover_image.trim() + ? j.cover_image.trim() + : null; + const journal = await Utils.createCustomJournalForImport({ + name: j.title || 'Untitled', + html, + imageUrl, + sheetType: 'journal', + archivistId: j.id, + worldId: campaignId, + folderId: folderId || null, + }); + // GM-only: set default ownership to NONE for all players + if (journal) { + try { + await journal.update( + { ownership: { default: CONST.DOCUMENT_OWNERSHIP_LEVELS.NONE } }, + { render: false } + ); + } catch (_) {} + // Store folder_id in flags for reference (not kept in sync) + if (j.folder_id) { + try { + const flags = journal.getFlag(CONFIG.MODULE_ID, 'archivist') || {}; + flags.archivistFolderId = j.folder_id; + await journal.setFlag(CONFIG.MODULE_ID, 'archivist', flags); + } catch (_) {} + } + } + } catch (e) { + console.warn('[World Setup] Failed to import journal', j, e); + } + this.syncStatus.processed++; + this._updateSyncStatusUI(); + } + ui.notifications?.info?.( + 'Journals imported as GM-only. Folder structure reflects the import snapshot and will not auto-sync.' + ); + } + + // Import Quests + for (const q of archivistQuests) { + this.syncStatus.current = `Import Quest: ${q.questName || q.quest_name || 'Quest'}`; + try { + const questFolderId = this.setupData.destinations.quest || null; + + // Fetch full quest data if we only have summary + let fullQuest = q; + if (!q.objectives && q.id) { + try { + const resp = await archivistApi.getQuest(apiKey, q.id); + if (resp.success && resp.data) fullQuest = resp.data; + } catch (_) {} + } + + // Quest has no generic description field in the API; its Info tab is a + // structured metadata form (see quest.hbs), not free-text page content. + const journal = await Utils.createCustomJournalForImport({ + name: fullQuest.questName || fullQuest.quest_name || 'Quest', + html: '', + sheetType: 'quest', + archivistId: fullQuest.id, + worldId: campaignId, + folderId: questFolderId || null, + }); + if (journal) { + const flags = journal.getFlag(CONFIG.MODULE_ID, 'archivist') || {}; + flags.questData = { + questName: fullQuest.questName || fullQuest.quest_name || '', + questGiver: fullQuest.questGiver || fullQuest.quest_giver || '', + questCategory: + fullQuest.questCategory || fullQuest.quest_category || 'n/a', + status: fullQuest.status || 'planned', + successDefinition: + fullQuest.successDefinition || fullQuest.success_definition || '', + failureConditions: + fullQuest.failureConditions || fullQuest.failure_conditions || '', + nextAction: fullQuest.nextAction || fullQuest.next_action || '', + resolution: fullQuest.resolution || '', + objectives: Array.isArray(fullQuest.objectives) ? fullQuest.objectives : [], + progressLog: Array.isArray(fullQuest.progressLog) + ? fullQuest.progressLog + : Array.isArray(fullQuest.progress_log) + ? fullQuest.progress_log + : Array.isArray(fullQuest.progressLogEntries) + ? fullQuest.progressLogEntries.map((e) => + typeof e === 'string' ? e : e.text || '' + ) + : Array.isArray(fullQuest.progress_log_entries) + ? fullQuest.progress_log_entries.map((e) => + typeof e === 'string' ? e : e.text || '' + ) + : [], + relatedCharacters: + fullQuest.relatedCharacters || fullQuest.related_characters || [], + relatedFactions: + fullQuest.relatedFactions || fullQuest.related_factions || [], + relatedLocations: + fullQuest.relatedLocations || fullQuest.related_locations || [], + relatedItems: + fullQuest.relatedItems || fullQuest.related_items || [], + relatedEntityRefs: + fullQuest.relatedEntityRefs || fullQuest.related_entity_refs || [], + firstSession: fullQuest.firstSession || fullQuest.first_session || null, + lastSession: fullQuest.lastSession || fullQuest.last_session || null, + }; + await journal.setFlag(CONFIG.MODULE_ID, 'archivist', flags); + } + } catch (e) { + console.warn('[World Setup] Failed to import quest', q, e); + } + this.syncStatus.processed++; + this._updateSyncStatusUI(); + } + // After creating journals and pages, hydrate link graph from Archivist Links try { // Ensure parent/child relationships for Locations using helper before indexing @@ -2962,66 +2731,6 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica } } - /** - * Handle world selection - * @param {Event} event - Change event - * @private - */ - async _onSelectWorld(event) { - event.preventDefault(); - - const worldId = event.target.value; - if (!worldId) { - this.setupData.selectedWorldId = ''; - this.setupData.selectedWorldName = ''; - return; - } - - const selectedWorld = this.worlds.find((w) => w.id === worldId); - if (selectedWorld) { - const displayName = - selectedWorld?.name || selectedWorld?.title || 'World'; - this.setupData.selectedWorldId = worldId; - this.setupData.selectedWorldName = displayName; - - // Save to settings immediately - try { - await settingsManager.setSelectedWorld(worldId, displayName); - ui.notifications.info( - game.i18n.localize('ARCHIVIST_SYNC.messages.worldSaved') - ); - } catch (error) { - console.error('Error saving world selection during setup:', error); - ui.notifications.error( - game.i18n.localize('ARCHIVIST_SYNC.errors.saveFailed') - ); - } - } - - await this.render(); - } - - /** - * Convert markdown to HTML with proper paragraph and line break handling - */ - _mdToHtml(md) { - const s = String(md || '').trim(); - if (!s) return ''; - return s - .replace(/\r\n/g, '\n') - .split('\n\n') // Split on double newlines to create paragraphs - .map((para) => { - // Within each paragraph, convert single newlines to${content}
`; - }) - .join(''); - } - /** * Create a Recaps folder and a journal per session, ordered by date */ @@ -3029,15 +2738,21 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica try { const sessionsResp = await archivistApi.listSessions(apiKey, campaignId); if (!sessionsResp.success) return; - const sessions = (sessionsResp.data || []).filter( - (s) => !!s.session_date - ); + const sessions = Array.isArray(sessionsResp.data) ? sessionsResp.data.slice() : []; - // Sort by ascending date (oldest first) + // Sort by ascending date (oldest first). Undated sessions go last. sessions.sort( - (a, b) => - new Date(a.session_date).getTime() - - new Date(b.session_date).getTime() + (a, b) => { + const aTime = a?.session_date + ? new Date(a.session_date).getTime() + : Number.POSITIVE_INFINITY; + const bTime = b?.session_date + ? new Date(b.session_date).getTime() + : Number.POSITIVE_INFINITY; + const safeATime = Number.isFinite(aTime) ? aTime : Number.POSITIVE_INFINITY; + const safeBTime = Number.isFinite(bTime) ? bTime : Number.POSITIVE_INFINITY; + return safeATime - safeBTime; + } ); // Create individual Recap journals, one per session, inside Recaps folder @@ -3064,8 +2779,12 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica for (const s of sessions) { const title = s.title || 'Session'; const html = String(s.summary || ''); - // Use session_date timestamp as sort value for proper ordering in Foundry sidebar - const sortValue = new Date(s.session_date).getTime(); + // Use session_date timestamp as sort value for proper ordering in Foundry + // sidebar; leave undated sessions unsorted and normalize them later. + const sessionDateMs = s.session_date + ? new Date(s.session_date).getTime() + : NaN; + const sortValue = Number.isFinite(sessionDateMs) ? sessionDateMs : undefined; const journal = await Utils.createCustomJournalForImport({ name: title, html, @@ -3077,13 +2796,15 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica sort: sortValue, }); if (journal) { - try { - await journal.setFlag( - CONFIG.MODULE_ID, - 'sessionDate', - String(s.session_date) - ); - } catch (_) {} + if (s.session_date) { + try { + await journal.setFlag( + CONFIG.MODULE_ID, + 'sessionDate', + String(s.session_date) + ); + } catch (_) {} + } } } @@ -3125,6 +2846,14 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica } catch (_) { /* ignore ordering failures */ } + + // Recaps are created late in the wizard flow, so force the Journal + // directory to refresh here rather than waiting for a later global render. + try { + await ui?.journal?.render?.({ force: true }); + } catch (_) { + /* ignore refresh failures */ + } } catch (e) { console.warn('Recaps sync skipped/failed:', e); } @@ -3159,7 +2888,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica try { if (window.ARCHIVIST_SYNC?.installRealtimeSyncListeners) { window.ARCHIVIST_SYNC.installRealtimeSyncListeners(); - console.log( + console.debug( '[Archivist Sync] Real-Time Sync listeners installed after setup' ); } @@ -3170,7 +2899,7 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica setTimeout(async () => { try { await ui?.journal?.render?.({ force: true }); - console.log('[Archivist Sync] Journal Directory re-rendered after setup'); + console.debug('[Archivist Sync] Journal Directory re-rendered after setup'); } catch (e) { console.warn('[Archivist Sync] Failed to re-render Journal Directory after setup', e); } @@ -3183,89 +2912,6 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica } } - /** - * Handle cancel button click - * @param {Event} event - Click event - * @private - */ - /** - * Generate and download a sample configuration file - */ - async _onDownloadSampleConfig(event) { - event?.preventDefault?.(); - - // Generate sample configuration with schema and examples - const sampleConfig = { - _schema_version: '1.0', - _description: - "Archivist Sync Configuration File - Edit the paths below to match your game system's data structure", - _instructions: { - actorMappings: - 'Configure how PC and NPC data is mapped from Foundry actors', - itemMappings: 'Configure how Item data is mapped from Foundry items', - destinations: - 'Configure where different entity types are synced to in Archivist', - }, - actorMappings: { - pc: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.details.biography.value', - _examples: { - namePath: 'name (actor name field)', - imagePath: 'img (actor image field)', - descriptionPath: - 'system.details.biography.value (D&D 5e), system.biography (PF2e), system.description (other systems)', - }, - }, - npc: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.details.biography.value', - _examples: { - namePath: 'name (actor name field)', - imagePath: 'img (actor image field)', - descriptionPath: - 'system.details.biography.value (D&D 5e), system.biography (PF2e), system.description (other systems)', - }, - }, - }, - itemMappings: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.description.value', - _examples: { - namePath: 'name (item name field)', - imagePath: 'img (item image field)', - descriptionPath: - 'system.description.value (D&D 5e), system.description (PF2e), system.description.value (other systems)', - }, - }, - destinations: { - pc: 'pc', - npc: 'npc', - item: 'item', - location: 'location', - faction: 'faction', - _options: { - pc: ['pc', 'npc'], - npc: ['npc', 'pc'], - item: ['item', 'note'], - location: ['location', 'note'], - faction: ['faction', 'note'], - }, - }, - }; - - // Open the sample config in a new tab - window.open( - 'https://raw.githubusercontent.com/camrun91/archivist-sync/main/archivist-sync-sample-config.json', - '_blank' - ); - - ui.notifications.info('Sample configuration opened in new tab.'); - } - async _onCancel(event) { event.preventDefault(); await this.close(); @@ -3285,20 +2931,19 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica progressBar.style.width = `${context.progressPercentage}%`; } - // Add keyboard shortcuts for Step 2 if (context.isStep2) { const apiKeyInput = this.element.querySelector('#api-key-input'); if (apiKeyInput) { - // Focus the input field apiKeyInput.focus(); - - // Add Enter key listener for validation - apiKeyInput.addEventListener('keypress', (event) => { - if (event.key === 'Enter') { - event.preventDefault(); - this._onValidateApiKey(event); - } - }); + if (!apiKeyInput.dataset.bound) { + apiKeyInput.addEventListener('keypress', (event) => { + if (event.key === 'Enter') { + event.preventDefault(); + this._onValidateApiKey(event); + } + }); + apiKeyInput.dataset.bound = 'true'; + } } } @@ -3435,43 +3080,73 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica } }); - // Delegate checkbox toggle + // Delegate checkbox toggle with shift+click range-select if (!root.dataset.boundReconToggle) { - root.addEventListener('change', async (ev) => { - const cb = ev.target?.closest?.( - 'input[type="checkbox"][data-action="recon-toggle-select"]' - ); - if (!cb) return; - const tab = cb.dataset.tab; - const side = cb.dataset.side; - const id = cb.dataset.id; - const checked = cb.checked; + this._lastReconClick = {}; + + const syncRow = (tab, side, id, checked) => { const r = this.setupData.reconcile?.[tab]; if (!r) return; const list = r[side] || []; const row = list.find((x) => x.id === id); if (!row) return; row.selected = checked; - // Sync with matched counterpart const otherSide = side === 'archivist' ? 'foundry' : 'archivist'; const mid = row.match || null; if (mid) { const otherRow = (r[otherSide] || []).find((x) => x.id === mid); if (otherRow) { otherRow.selected = checked; - // Update the matched checkbox in the DOM const matchedCheckbox = root.querySelector( `input[data-action="recon-toggle-select"][data-tab="${tab}"][data-side="${otherSide}"][data-id="${mid}"]` ); if (matchedCheckbox) matchedCheckbox.checked = checked; } } - // Update header "select all" checkbox state + }; + + const updateHeaderCheckbox = (tab, side) => { + const r = this.setupData.reconcile?.[tab]; + if (!r) return; + const list = r[side] || []; const allSelected = list.length > 0 && list.every((x) => x.selected); - const headerCheckbox = root.querySelector( + const headerCb = root.querySelector( `input[data-action="recon-select-all-toggle"][data-tab="${tab}"][data-side="${side}"]` ); - if (headerCheckbox) headerCheckbox.checked = allSelected; + if (headerCb) headerCb.checked = allSelected; + }; + + root.addEventListener('click', (ev) => { + const cb = ev.target?.closest?.( + 'input[type="checkbox"][data-action="recon-toggle-select"]' + ); + if (!cb) return; + const tab = cb.dataset.tab; + const side = cb.dataset.side; + const key = `${tab}:${side}`; + const allCbs = Array.from( + root.querySelectorAll( + `input[data-action="recon-toggle-select"][data-tab="${tab}"][data-side="${side}"]` + ) + ); + const idx = allCbs.indexOf(cb); + + if (ev.shiftKey && this._lastReconClick[key] != null) { + const lastIdx = this._lastReconClick[key]; + const from = Math.min(lastIdx, idx); + const to = Math.max(lastIdx, idx); + const checked = cb.checked; + for (let i = from; i <= to; i++) { + const target = allCbs[i]; + target.checked = checked; + syncRow(tab, side, target.dataset.id, checked); + } + } else { + syncRow(tab, side, cb.dataset.id, cb.checked); + } + + this._lastReconClick[key] = idx; + updateHeaderCheckbox(tab, side); }); root.dataset.boundReconToggle = 'true'; } @@ -3641,553 +3316,201 @@ export class WorldSetupDialog extends foundry.applications.api.HandlebarsApplica } } - // Legacy Step 4 (Mapping) removed – skip binding for legacy controls - if (false && context.isStep4) { - const val = (sel) => this.element.querySelector(sel)?.value?.trim() || ''; - const updateMappingData = () => { - this.setupData.mapping.pc.namePath = val('#map-pc-name'); - this.setupData.mapping.pc.imagePath = val('#map-pc-image'); - this.setupData.mapping.pc.descPath = val('#map-pc-desc'); - this.setupData.mapping.npc.namePath = val('#map-npc-name'); - this.setupData.mapping.npc.imagePath = val('#map-npc-image'); - this.setupData.mapping.npc.descPath = val('#map-npc-desc'); - this.setupData.mapping.item.namePath = val('#map-item-name'); - this.setupData.mapping.item.imagePath = val('#map-item-image'); - this.setupData.mapping.item.descPath = val('#map-item-desc'); - this.setupData.destinations.pc = val('#dest-pc'); - this.setupData.destinations.npc = val('#dest-npc'); - this.setupData.destinations.item = val('#dest-item'); - }; - - // Attach change listeners to all mapping and destination selects - const selectors = [ - '#map-pc-name', - '#map-pc-image', - '#map-pc-desc', - '#map-npc-name', - '#map-npc-image', - '#map-npc-desc', - '#map-item-name', - '#map-item-image', - '#map-item-desc', - '#dest-pc', - '#dest-npc', - '#dest-item', - ]; - - selectors.forEach((selector) => { - const element = this.element.querySelector(selector); - if (element && !element.dataset.boundSetup) { - element.addEventListener('change', updateMappingData); - element.dataset.boundSetup = 'true'; - } - }); - - // Add file input handler for config loading - const configFileInput = this.element.querySelector('#config-file-input'); - if (configFileInput && !configFileInput.dataset.boundSetup) { - configFileInput.addEventListener('change', async (event) => { - const file = event.target.files[0]; - if (!file) return; - - try { - const text = await file.text(); - const config = JSON.parse(text); - - // Apply configuration to form fields - if (config.actorMappings?.pc) { - if (config.actorMappings.pc.namePath) { - const pcNameSelect = this.element.querySelector('#map-pc-name'); - if (pcNameSelect) - pcNameSelect.value = config.actorMappings.pc.namePath; - } - if (config.actorMappings.pc.imagePath) { - const pcImageSelect = - this.element.querySelector('#map-pc-image'); - if (pcImageSelect) - pcImageSelect.value = config.actorMappings.pc.imagePath; - } - if (config.actorMappings.pc.descriptionPath) { - const pcDescSelect = this.element.querySelector('#map-pc-desc'); - if (pcDescSelect) - pcDescSelect.value = config.actorMappings.pc.descriptionPath; - } - } - - if (config.actorMappings?.npc) { - if (config.actorMappings.npc.namePath) { - const npcNameSelect = - this.element.querySelector('#map-npc-name'); - if (npcNameSelect) - npcNameSelect.value = config.actorMappings.npc.namePath; - } - if (config.actorMappings.npc.imagePath) { - const npcImageSelect = - this.element.querySelector('#map-npc-image'); - if (npcImageSelect) - npcImageSelect.value = config.actorMappings.npc.imagePath; - } - if (config.actorMappings.npc.descriptionPath) { - const npcDescSelect = - this.element.querySelector('#map-npc-desc'); - if (npcDescSelect) - npcDescSelect.value = - config.actorMappings.npc.descriptionPath; - } - } - - if (config.itemMappings) { - if (config.itemMappings.namePath) { - const itemNameSelect = - this.element.querySelector('#map-item-name'); - if (itemNameSelect) - itemNameSelect.value = config.itemMappings.namePath; - } - if (config.itemMappings.imagePath) { - const itemImageSelect = - this.element.querySelector('#map-item-image'); - if (itemImageSelect) - itemImageSelect.value = config.itemMappings.imagePath; - } - if (config.itemMappings.descriptionPath) { - const itemDescSelect = - this.element.querySelector('#map-item-desc'); - if (itemDescSelect) - itemDescSelect.value = config.itemMappings.descriptionPath; - } - } - - // Update internal data - updateMappingData(); - - ui.notifications.info('Configuration loaded successfully.'); - } catch (error) { - console.error('Failed to load configuration file:', error); - ui.notifications.error( - 'Failed to load configuration file. Please check the file format.' - ); - } - - // Clear the file input - event.target.value = ''; - }); - configFileInput.dataset.boundSetup = 'true'; - } - // Bind preset change validation in Step 4 (if the dropdown exists here) - const presetSelect = this.element.querySelector('#ws-system-preset'); - if (presetSelect && !presetSelect.dataset.boundSetup) { - presetSelect.addEventListener('change', async (e) => { - const key = e?.target?.value || ''; - if (!key) { - this.setupData.systemPreset = ''; - return; - } - try { - await this._validateOrRejectPreset(key); - this.setupData.systemPreset = key; - this._applyPresetToSetupData(key); - await this.render(); - ui.notifications.info( - `Applied ${presetSelect.options[presetSelect.selectedIndex].text} preset` - ); - } catch (err) { - ui.notifications.error( - String( - err?.message || err || 'Preset unavailable for this system.' - ) - ); - // revert selection - presetSelect.value = this.setupData.systemPreset || ''; - } - }); - presetSelect.dataset.boundSetup = 'true'; - } - } - } - - /** - * Get shared system presets (mirrors sync options dialog) - */ - _getSystemPresets() { - return { - dnd5e: { - name: 'D&D 5e', - actorMappings: { - pc: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.details.biography.value', - }, - npc: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.details.biography.public', - }, - }, - itemMappings: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.description.value', - }, - }, - pf2e: { - name: 'Pathfinder 2e', - actorMappings: { - pc: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.details.biography.value', - }, - npc: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.details.publicNotes', - }, - }, - itemMappings: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.description.value', - }, - }, - coc7: { - name: 'Call of Cthulhu 7e', - actorMappings: { - pc: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.biography.personal.description', - }, - npc: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.biography.personal.description', - }, - }, - itemMappings: { - namePath: 'name', - imagePath: 'img', - descriptionPath: 'system.description.value', - }, - }, - }; } - /** - * Try auto-detect preset based on whether all preset paths exist in system model - * Order: dnd5e -> pf2e -> coc7 - */ - async _autoDetectPreset() { - const order = ['dnd5e', 'pf2e', 'coc7']; - for (const key of order) { - try { - await this._validateOrRejectPreset(key); - return key; - } catch (_) { - /* try next */ + _buildReconciliationModel(input) { + const A = input.archivist || {}; + const F = input.foundry || {}; + const toName = (v) => String(v || '').trim(); + const normName = (v) => toName(v).toLowerCase(); + + const actors = Array.isArray(F.actors) ? F.actors : []; + const actorsBase = actors.map((a) => ({ + id: a.id, + name: toName(a.name), + img: a.img || '', + type: String(a.type || ''), + doc: a, + })); + let pcActors = actorsBase.filter( + (a) => a.type.toLowerCase() === 'character' + ); + let npcActors = actorsBase.filter( + (a) => + a.type.toLowerCase() === 'npc' || a.type.toLowerCase() === 'monster' + ); + if ( + pcActors.length === 0 && + npcActors.length === 0 && + actorsBase.length > 0 + ) { + const acChars = Array.isArray(A.characters) ? A.characters : []; + const acByName = new Map(); + for (const c of acChars) { + const n = normName(c.character_name || c.name); + if (n) + acByName.set(n, (c.type || c.character_type || '').toUpperCase()); + } + for (const a of actorsBase) { + const kind = acByName.get(normName(a.name)) || 'NPC'; + if (kind === 'PC') pcActors.push(a); + else npcActors.push(a); } } - return ''; - } - /** - * Validate preset against current system model; throws if invalid - */ - async _validateOrRejectPreset(key) { - const presets = this._getSystemPresets(); - const preset = presets[key]; - if (!preset) throw new Error('Unknown preset'); - - // Validate that each mapping path exists in the system model/property graph - const testPaths = [ - preset.actorMappings?.pc?.namePath, - preset.actorMappings?.pc?.imagePath, - preset.actorMappings?.pc?.descriptionPath, - preset.actorMappings?.npc?.namePath, - preset.actorMappings?.npc?.imagePath, - preset.actorMappings?.npc?.descriptionPath, - preset.itemMappings?.namePath, - preset.itemMappings?.imagePath, - preset.itemMappings?.descriptionPath, - ].filter(Boolean); - - // Use our discovered mapping options to validate existence - const availableActor = new Set( - (this.mappingOptions.actor || []).map((o) => o.path) + const fItems = (Array.isArray(F.items) ? F.items : []).map((i) => ({ + id: i.id, + name: toName(i.name), + img: i.img || '', + doc: i, + })); + const fScenes = (Array.isArray(F.scenes) ? F.scenes : []).map((s) => ({ + id: s.id, + name: toName(s.name), + img: s.thumb || s.background?.src || '', + doc: s, + })); + + const aChars = (Array.isArray(A.characters) ? A.characters : []).map( + (c) => ({ + id: String(c.id), + name: toName(c.character_name || c.name), + img: toName(c.image || ''), + type: String(c.type || c.character_type || 'PC').toUpperCase(), + }) ); - const availableItem = new Set( - (this.mappingOptions.item || []).map((o) => o.path) + const aItems = (Array.isArray(A.items) ? A.items : []).map((i) => ({ + id: String(i.id), + name: toName(i.name), + img: toName(i.image || ''), + })); + const aLocs = (Array.isArray(A.locations) ? A.locations : []).map((l) => ({ + id: String(l.id), + name: toName(l.name || l.title), + img: toName(l.image || ''), + })); + const aFactions = (Array.isArray(A.factions) ? A.factions : []).map( + (f) => ({ + id: String(f.id), + name: toName(f.name || f.title), + img: toName(f.image || ''), + }) ); - const exists = (p) => { - if (!p) return false; - if (p === 'name' || p === 'img') return true; // safe defaults always exist - if (p.startsWith('system.')) { - // Try actor first, then item - return availableActor.has(p) || availableItem.has(p); + const matchByName = ( + left, + right, + getLeftName, + getRightName, + constraint + ) => { + const usedRight = new Set(); + const leftOut = []; + for (const L of left) { + let hit = null; + for (const R of right) { + if (usedRight.has(R.id)) continue; + if (constraint && !constraint(L, R)) continue; + if (normName(getLeftName(L)) === normName(getRightName(R))) { + hit = R; + break; + } + } + if (hit) { + usedRight.add(hit.id); + leftOut.push({ ...L, match: hit.id, selected: true }); + } else { + leftOut.push({ ...L, match: null, selected: false }); + } } - return availableActor.has(p) || availableItem.has(p); + const rightOut = right.map((R) => { + const L = leftOut.find((x) => x.match === R.id) || null; + return { ...R, match: L ? L.id : null, selected: !!L }; + }); + return { leftOut, rightOut }; }; - const missing = testPaths.filter((p) => !exists(p)); - if (missing.length) { - throw new Error( - `Preset unavailable: missing properties in this system → ${missing.join(', ')}` - ); - } - return true; - } - - /** - * Apply preset mappings to setupData (does not touch destinations) - */ - _applyPresetToSetupData(key) { - const presets = this._getSystemPresets(); - const preset = presets[key]; - if (!preset) return; - const gp = (o, p, fb = '') => { - try { - return foundry.utils.getProperty(o, p) ?? fb; - } catch (_) { - return fb; - } + const allActors = [...pcActors, ...npcActors]; + const charConstraint = (ac, fa) => { + if (!fa.type || fa.type === '' || fa.type === 'base') return true; + return ac.type === 'PC' + ? fa.type.toLowerCase() === 'character' + : fa.type.toLowerCase() === 'npc' || + fa.type.toLowerCase() === 'monster'; }; - this.setupData.mapping.pc.namePath = gp( - preset, - 'actorMappings.pc.namePath' - ); - this.setupData.mapping.pc.imagePath = gp( - preset, - 'actorMappings.pc.imagePath' + const { leftOut: aCharsOut, rightOut: fActorsOut } = matchByName( + aChars, + allActors, + (x) => x.name, + (x) => x.name, + charConstraint ); - this.setupData.mapping.pc.descPath = gp( - preset, - 'actorMappings.pc.descriptionPath' - ); - this.setupData.mapping.npc.namePath = gp( - preset, - 'actorMappings.npc.namePath' - ); - this.setupData.mapping.npc.imagePath = gp( - preset, - 'actorMappings.npc.imagePath' - ); - this.setupData.mapping.npc.descPath = gp( - preset, - 'actorMappings.npc.descriptionPath' - ); - this.setupData.mapping.item.namePath = gp(preset, 'itemMappings.namePath'); - this.setupData.mapping.item.imagePath = gp( - preset, - 'itemMappings.imagePath' - ); - this.setupData.mapping.item.descPath = gp( - preset, - 'itemMappings.descriptionPath' - ); - } -} -// Build reconciliation model for Step 4 -WorldSetupDialog.prototype._buildReconciliationModel = function (input) { - console.log('[World Setup] _buildReconciliationModel called with:', input); - const A = input.archivist || {}; - const F = input.foundry || {}; - const toName = (v) => String(v || '').trim(); - const normName = (v) => toName(v).toLowerCase(); - - // Foundry actors classification with fallback - const actors = Array.isArray(F.actors) ? F.actors : []; - const actorsBase = actors.map((a) => ({ - id: a.id, - name: toName(a.name), - img: a.img || '', - type: String(a.type || ''), - doc: a, - })); - let pcActors = actorsBase.filter((a) => a.type.toLowerCase() === 'character'); - let npcActors = actorsBase.filter( - (a) => a.type.toLowerCase() === 'npc' || a.type.toLowerCase() === 'monster' - ); - if ( - pcActors.length === 0 && - npcActors.length === 0 && - actorsBase.length > 0 - ) { - const acChars = Array.isArray(A.characters) ? A.characters : []; - const acByName = new Map(); - for (const c of acChars) { - const n = normName(c.character_name || c.name); - if (n) acByName.set(n, (c.type || c.character_type || '').toUpperCase()); - } - for (const a of actorsBase) { - const kind = acByName.get(normName(a.name)) || 'NPC'; - if (kind === 'PC') pcActors.push(a); - else npcActors.push(a); - } - } + const { leftOut: aItemsOut, rightOut: fItemsOut } = matchByName( + aItems, + fItems, + (x) => x.name, + (x) => x.name + ); - // Foundry items and scenes - const fItems = (Array.isArray(F.items) ? F.items : []).map((i) => ({ - id: i.id, - name: toName(i.name), - img: i.img || '', - doc: i, - })); - const fScenes = (Array.isArray(F.scenes) ? F.scenes : []).map((s) => ({ - id: s.id, - name: toName(s.name), - img: s.thumb || s.background?.src || '', - doc: s, - })); - - // Archivist lists normalized - const aChars = (Array.isArray(A.characters) ? A.characters : []).map((c) => ({ - id: String(c.id), - name: toName(c.character_name || c.name), - img: toName(c.image || ''), - type: String(c.type || c.character_type || 'PC').toUpperCase(), - })); - const aItems = (Array.isArray(A.items) ? A.items : []).map((i) => ({ - id: String(i.id), - name: toName(i.name), - img: toName(i.image || ''), - })); - const aLocs = (Array.isArray(A.locations) ? A.locations : []).map((l) => ({ - id: String(l.id), - name: toName(l.name || l.title), - img: toName(l.image || ''), - })); - const aFactions = (Array.isArray(A.factions) ? A.factions : []).map((f) => ({ - id: String(f.id), - name: toName(f.name || f.title), - img: toName(f.image || ''), - })); - - // Matching helpers (one-to-one by name and type/bucket) - const matchByName = (left, right, getLeftName, getRightName, constraint) => { - const usedRight = new Set(); - const leftOut = []; - for (const L of left) { - let hit = null; - for (const R of right) { - if (usedRight.has(R.id)) continue; - if (constraint && !constraint(L, R)) continue; - if (normName(getLeftName(L)) === normName(getRightName(R))) { - hit = R; - break; - } - } - if (hit) { - usedRight.add(hit.id); - leftOut.push({ ...L, match: hit.id, selected: false }); - } else { - leftOut.push({ ...L, match: null, selected: false }); - } - } - // Right side mirror - const rightOut = right.map((R) => { - const L = leftOut.find((x) => x.match === R.id) || null; - return { ...R, match: L ? L.id : null, selected: false }; - }); - return { leftOut, rightOut }; - }; + const { leftOut: aLocsOut, rightOut: fScenesOut } = matchByName( + aLocs, + fScenes, + (x) => x.name, + (x) => x.name + ); - // Characters: allow PC->character and NPC->npc by constraint - const allActors = [...pcActors, ...npcActors]; - // More lenient constraint: if types exist, respect them; otherwise allow any match - const charConstraint = (ac, fa) => { - // If Foundry actor has no meaningful type, allow match - if (!fa.type || fa.type === '' || fa.type === 'base') return true; - // Otherwise respect type matching - return ac.type === 'PC' - ? fa.type.toLowerCase() === 'character' - : fa.type.toLowerCase() === 'npc' || fa.type.toLowerCase() === 'monster'; - }; - const { leftOut: aCharsOut, rightOut: fActorsOut } = matchByName( - aChars, - allActors, - (x) => x.name, - (x) => x.name, - charConstraint - ); - - // Items - const { leftOut: aItemsOut, rightOut: fItemsOut } = matchByName( - aItems, - fItems, - (x) => x.name, - (x) => x.name - ); - - // Locations vs Scenes - const { leftOut: aLocsOut, rightOut: fScenesOut } = matchByName( - aLocs, - fScenes, - (x) => x.name, - (x) => x.name - ); - - // Factions — Foundry side empty by default - const aFactionsOut = aFactions.map((f) => ({ - ...f, - match: null, - selected: false, - })); - const fFactionsOut = []; - - const result = { - characters: { archivist: aCharsOut, foundry: fActorsOut }, - items: { archivist: aItemsOut, foundry: fItemsOut }, - locations: { archivist: aLocsOut, foundry: fScenesOut }, - factions: { archivist: aFactionsOut, foundry: fFactionsOut }, - }; + const aFactionsOut = aFactions.map((f) => ({ + ...f, + match: null, + selected: false, + })); + const fFactionsOut = []; + + const result = { + characters: { archivist: aCharsOut, foundry: fActorsOut }, + items: { archivist: aItemsOut, foundry: fItemsOut }, + locations: { archivist: aLocsOut, foundry: fScenesOut }, + factions: { archivist: aFactionsOut, foundry: fFactionsOut }, + }; - // Post-pass: ensure exact-name matches are linked if still unmatched (helps systems without types) - try { - const ensureNameMatch = (leftArr, rightArr) => { - const rightByName = new Map(rightArr.map((r) => [normName(r.name), r])); - for (const l of leftArr) { - if (!l.match) { - const r = rightByName.get(normName(l.name)); - if (r && !r.match) { - l.match = r.id; - r.match = l.id; + try { + const ensureNameMatch = (leftArr, rightArr) => { + const rightByName = new Map( + rightArr.map((r) => [normName(r.name), r]) + ); + for (const l of leftArr) { + if (!l.match) { + const r = rightByName.get(normName(l.name)); + if (r && !r.match) { + l.match = r.id; + r.match = l.id; + } } } - } - }; + }; - ensureNameMatch(result.characters.archivist, result.characters.foundry); - ensureNameMatch(result.items.archivist, result.items.foundry); - ensureNameMatch(result.locations.archivist, result.locations.foundry); - } catch (_) { - /* ignore */ - } + ensureNameMatch(result.characters.archivist, result.characters.foundry); + ensureNameMatch(result.items.archivist, result.items.foundry); + ensureNameMatch(result.locations.archivist, result.locations.foundry); + } catch (_) { + /* ignore */ + } - // Sort all lists alphabetically by name for display in Steps 4 and 5 - const sortByName = (arr) => - arr.sort((a, b) => toName(a.name).localeCompare(toName(b.name))); - try { - sortByName(result.characters.archivist); - sortByName(result.characters.foundry); - sortByName(result.items.archivist); - sortByName(result.items.foundry); - sortByName(result.locations.archivist); - sortByName(result.locations.foundry); - sortByName(result.factions.archivist); - sortByName(result.factions.foundry); - } catch (_) { - /* ignore */ + const sortByName = (arr) => + arr.sort((a, b) => toName(a.name).localeCompare(toName(b.name))); + try { + sortByName(result.characters.archivist); + sortByName(result.characters.foundry); + sortByName(result.items.archivist); + sortByName(result.items.foundry); + sortByName(result.locations.archivist); + sortByName(result.locations.foundry); + sortByName(result.factions.archivist); + sortByName(result.factions.foundry); + } catch (_) { + /* ignore */ + } + return result; } - console.log('[World Setup] Reconciliation result:', { - charactersArchivist: aCharsOut.length, - charactersFoundry: fActorsOut.length, - itemsArchivist: aItemsOut.length, - itemsFoundry: fItemsOut.length, - locationsArchivist: aLocsOut.length, - locationsFoundry: fScenesOut.length, - factionsArchivist: aFactionsOut.length, - }); - return result; -}; +} diff --git a/scripts/modules/config.js b/scripts/modules/config.js index d2ec55e..6a1fdb1 100644 --- a/scripts/modules/config.js +++ b/scripts/modules/config.js @@ -85,7 +85,7 @@ export const SETTINGS = { scope: 'world', config: false, type: Object, - default: { pc: '', npc: '', item: '', location: '', faction: '' }, + default: { pc: '', npc: '', item: '', location: '', faction: '', journal: '', quest: '' }, }, CHAT_HISTORY: { diff --git a/scripts/modules/journal-manager.js b/scripts/modules/journal-manager.js index 819ff83..30609f2 100644 --- a/scripts/modules/journal-manager.js +++ b/scripts/modules/journal-manager.js @@ -14,6 +14,8 @@ export class JournalManager { location: 'Archivist - Locations', faction: 'Archivist - Factions', recap: 'Recaps', + quest: 'Archivist - Quests', + journal: 'Archivist - Journals', entry: null, }; } diff --git a/scripts/modules/projection/merge.js b/scripts/modules/projection/merge.js index b693b27..7726f46 100644 --- a/scripts/modules/projection/merge.js +++ b/scripts/modules/projection/merge.js @@ -1,7 +1,9 @@ -// Merge helper: inject Archivist HTML into an existing HTML field non-destructively +// Merge helper: inject Archivist content into an existing field non-destructively const WRAP_START = '$1')
+ .replace(/\n/g, '${foundry.utils.escapeHTML(p.trim())}
`) + .map((p) => p.trim()) + .filter(Boolean) + .map((p) => `${renderInline(p)}
`) .join(''); } return foundry?.utils?.TextEditor?.cleanHTML @@ -1013,6 +1037,8 @@ export class Utils { item: 'Archivist - Items', location: 'Archivist - Locations', faction: 'Archivist - Factions', + journal: 'Archivist - Journals', + quest: 'Archivist - Quests', }; console.log( @@ -1040,6 +1066,8 @@ export class Utils { item: 'Archivist - Items', location: 'Archivist - Locations', faction: 'Archivist - Factions', + journal: 'Archivist - Journals', + quest: 'Archivist - Quests', }; const name = map[String(type || '').toLowerCase()]; @@ -1110,6 +1138,8 @@ export class Utils { location: 'archivist-sync.LocationPageSheetV2', faction: 'archivist-sync.FactionPageSheetV2', recap: 'archivist-sync.RecapPageSheetV2', + journal: 'archivist-sync.JournalPageSheetV2', + quest: 'archivist-sync.QuestPageSheetV2', }; const normalizedType = String(sheetType || '').toLowerCase(); const sheetClass = sheetClassMap[normalizedType] || ''; diff --git a/scripts/modules/utils.js.bak b/scripts/modules/utils.js.bak deleted file mode 100644 index 69fa90f..0000000 --- a/scripts/modules/utils.js.bak +++ /dev/null @@ -1,1033 +0,0 @@ - /** - * Return the current system id in lowercase (e.g., "dnd5e", "pf2e"). - */ - static getSystemId() { - return String(game?.system?.id || '').toLowerCase(); -} - - /** - * Safely get the first non-empty string value from an object using a list of dot-paths. - * @param {object} obj - * @param {string[]} paths - * @returns {string} - */ - static pickFirstProperty(obj, paths = []) { - const get = (o, path) => { - try { - if (foundry?.utils?.getProperty) return foundry.utils.getProperty(o, path); - } catch (_) { } - return String(path).split('.').reduce((acc, k) => (acc && k in acc ? acc[k] : undefined), o); - }; - for (const p of paths) { - const v = get(obj, p); - if (typeof v === 'string' && v.trim()) return v; - } - return ''; -} - - /** - * Compute the preferred READ paths for an Actor description based on system and actor type. - * @param {Actor} actor - * @returns {string[]} - */ - static getActorDescriptionReadPaths(actor) { - const sysId = this.getSystemId(); - const isPC = String(actor?.type || '').toLowerCase() === 'character'; - const isNPC = String(actor?.type || '').toLowerCase() === 'npc'; - - if (sysId === 'dnd5e') return ['system.details.biography.value', 'system.details.biography.public', 'system.description.value']; - - if (sysId === 'pf2e') { - if (isPC) return ['system.details.biography.backstory', 'system.details.publicNotes', 'system.description.value']; - if (isNPC) return ['system.details.notes.description', 'system.details.publicNotes', 'system.description.value']; - return ['system.details.biography.backstory', 'system.details.notes.description', 'system.details.publicNotes', 'system.description.value']; - } - - // Generic fallbacks - return [ - 'system.details.biography.value', - 'system.details.biography.public', - 'system.description.value', - 'system.details.description', - 'system.details.publicNotes', - 'system.description', - ]; -} - - /** - * Compute the preferred WRITE path for projecting an Actor description back into the system. - * @param {Actor} actor - * @returns {string} A dot-path suitable for Actor.update({ [path]: html }) - */ - static getActorDescriptionWritePath(actor) { - const sysId = this.getSystemId(); - const isPC = String(actor?.type || '').toLowerCase() === 'character'; - const isNPC = String(actor?.type || '').toLowerCase() === 'npc'; - - if (sysId === 'dnd5e') return 'system.details.biography.value'; - - if (sysId === 'pf2e') { - if (isPC) return 'system.details.biography.backstory'; - if (isNPC) return 'system.details.notes.description'; - return 'system.details.publicNotes'; - } - - // Generic destination - return 'system.description.value'; -} - - /** - * Read an Actor description as plain text (Markdown-like) for ingest. - * @param {Actor} actor - * @returns {string} - */ - static readActorDescription(actor) { - const paths = this.getActorDescriptionReadPaths(actor); - // Strip to text/markdown for ingest using our existing HTML->MD helper - const htmlOrText = this.pickFirstProperty(actor, paths); - return this.toMarkdownIfHtml(htmlOrText); -} - - /** - * Project a description onto an Actor at the proper system path. - * Accepts Markdown and converts to sanitized HTML for storage. - * @param {Actor} actor - * @param {string} markdown - */ - static async projectActorDescription(actor, markdown) { - const destPath = this.getActorDescriptionWritePath(actor); - const html = this.markdownToStoredHtml(String(markdown ?? '')); - await actor.update({ [destPath]: html }); - return destPath; -} -import { CONFIG } from './config.js'; - -/** - * Utility functions for Archivist Sync Module - */ -export class Utils { - /** - * Resolve a valid Item type for the current system. - * - Prefer a cached value after first resolution - * - Fallback to 'loot' if available; otherwise first defined item type - * @returns {string} valid item type id for Item.create - */ - static getDefaultItemType() { - try { - if (this.__defaultItemType && typeof this.__defaultItemType === 'string') { - console.log('[Utils.getDefaultItemType] Using cached default type:', this.__defaultItemType); - return this.__defaultItemType; - } - console.log('[Utils.getDefaultItemType] Discovering system Item types...'); - const types = (() => { - try { - const meta = CONFIG?.Item?.documentClass?.metadata?.types; - console.log('[Utils.getDefaultItemType] CONFIG.Item.documentClass.metadata.types:', meta); - if (Array.isArray(meta) && meta.length) return meta.map(t => String(t).toLowerCase()); - } catch (e) { - console.warn('[Utils.getDefaultItemType] Failed to read CONFIG metadata:', e); - } - try { - const model = game?.system?.model?.Item; - const keys = model ? Object.keys(model) : []; - console.log('[Utils.getDefaultItemType] game.system.model.Item keys:', keys); - if (keys.length) return keys.map(t => String(t).toLowerCase()); - } catch (e) { - console.warn('[Utils.getDefaultItemType] Failed to read system.model.Item:', e); - } - try { - const docTypes = game?.system?.documentTypes?.Item; - console.log('[Utils.getDefaultItemType] game.system.documentTypes.Item:', docTypes); - if (Array.isArray(docTypes) && docTypes.length) return docTypes.map(t => String(t).toLowerCase()); - if (docTypes && typeof docTypes === 'object') { - const keys = Object.keys(docTypes); - if (keys.length) return keys.map(t => String(t).toLowerCase()); - } - } catch (e) { - console.warn('[Utils.getDefaultItemType] Failed to read system.documentTypes.Item:', e); - } - console.warn('[Utils.getDefaultItemType] No system types found, returning empty array'); - return []; - })(); - console.log('[Utils.getDefaultItemType] Resolved types array:', types); - const picked = types[0] || 'loot'; - console.log('[Utils.getDefaultItemType] Picked default type:', picked, '(will fallback to "loot" if types empty)'); - this.__defaultItemType = picked; - return picked; - } catch (e) { - console.error('[Utils.getDefaultItemType] Outer catch, returning "loot":', e); - return 'loot'; - } - } - - /** - * Resolve a safe Item type from a source descriptor with fallback to system default. - * @param {any} source - object that may include type/item_type/category - * @returns {string} valid item type id - */ - static resolveItemType(source) { - try { - console.log('[Utils.resolveItemType] Called with source:', { type: source?.type, item_type: source?.item_type, category: source?.category }); - const types = (() => { - try { - const meta = CONFIG?.Item?.documentClass?.metadata?.types; - console.log('[Utils.resolveItemType] CONFIG.Item.documentClass.metadata.types:', meta); - if (Array.isArray(meta) && meta.length) return meta.map(t => String(t).toLowerCase()); - } catch (_) { } - try { - const model = game?.system?.model?.Item; - const keys = model ? Object.keys(model) : []; - console.log('[Utils.resolveItemType] game.system.model.Item keys:', keys); - if (keys.length) return keys.map(t => String(t).toLowerCase()); - } catch (_) { } - try { - const docTypes = game?.system?.documentTypes?.Item; - console.log('[Utils.resolveItemType] game.system.documentTypes.Item:', docTypes); - if (Array.isArray(docTypes) && docTypes.length) return docTypes.map(t => String(t).toLowerCase()); - if (docTypes && typeof docTypes === 'object') { - const keys = Object.keys(docTypes); - if (keys.length) return keys.map(t => String(t).toLowerCase()); - } - } catch (_) { } - return []; - })(); - console.log('[Utils.resolveItemType] Available system types:', types); - const typeSet = new Set(types); - const raw = String(source?.type ?? source?.item_type ?? source?.category ?? '') - .trim() - .toLowerCase(); - console.log('[Utils.resolveItemType] Raw type from source:', raw); - if (raw && typeSet.has(raw)) { - console.log('[Utils.resolveItemType] Raw type is valid, returning:', raw); - return raw; - } - - // Helper: pick the first that exists in current system types - const pick = (candidates) => { - for (const c of candidates) { - const t = String(c).toLowerCase(); - if (typeSet.has(t)) return t; - } - return null; - }; - - // Common normalizations and aliases across systems - // Prefer system-specific types when available (e.g., PF2e uses 'treasure' instead of 'loot') - const alias = ( - pick([ - // Loot-like - raw.match(/loot|treasure|generic/) ? 'treasure' : null, - // Equipment/armor/weapons - raw.match(/^armor|^equipment/) ? 'equipment' : null, - raw.match(/weapon/) ? 'weapon' : null, - // Consumables - raw.match(/consum/) ? 'consumable' : null, - // Containers - raw.match(/pack|bag|backpack/) ? 'backpack' : null, - // Others - raw.match(/feat|ability/) ? 'feat' : null, - raw.match(/tool/) ? 'tool' : null, - raw.match(/spell/) ? 'spell' : null, - ].filter(Boolean)) - ); - if (alias) { - console.log('[Utils.resolveItemType] Found alias match:', alias); - return alias; - } - - // As a final attempt, map generic buckets to something safe - console.log('[Utils.resolveItemType] No alias match, trying generic fallbacks...'); - const generic = pick(['equipment', 'treasure', 'weapon', 'consumable']); - if (generic) { - console.log('[Utils.resolveItemType] Found generic fallback:', generic); - return generic; - } - - console.log('[Utils.resolveItemType] No generic fallback, getting default type...'); - const deflt = this.getDefaultItemType(); - console.log('[Utils.resolveItemType] Default type:', deflt, 'typeSet size:', typeSet.size); - if (!typeSet.size || typeSet.has(deflt)) { - console.log('[Utils.resolveItemType] Returning default:', deflt); - return deflt; - } - const final = types[0] || deflt || 'equipment'; - console.log('[Utils.resolveItemType] Final fallback:', final); - return final; - } catch (e) { - console.error('[Utils.resolveItemType] Exception, returning default:', e); - return this.getDefaultItemType(); - } - } - /** - * Convert HTML to Markdown (naive conversion: strip HTML tags, keep text) - * @param {any} value - The value to convert - * @returns {string} Plain text - */ - static toMarkdownIfHtml(value) { - const s = String(value ?? ''); - if (!s) return ''; - try { - // Insert explicit newlines for common block elements before stripping tags - let pre = s - .replace(/\r\n/g, '\n') - .replace(/${foundry.utils.escapeHTML(p.trim())}
`) - .join(''); - } - return foundry?.utils?.TextEditor?.cleanHTML - ? foundry.utils.TextEditor.cleanHTML(rawHtml) - : rawHtml; - } catch (_) { - return String(markdown || ''); - } - } - - /** - * Get current Foundry world information - * @returns {object} World information object - */ - static getFoundryWorldInfo() { - return { - id: game.world.id, - title: game.world.title, - description: game.world.description || 'No description', - }; - } - - /** - * Get character actors from the current world - * @returns {Array} Array of character and NPC actors - */ - static getCharacterActors() { - return game.actors.contents.filter(actor => actor.type === 'character' || actor.type === 'npc'); - } - - /** - * Get journal entries that likely represent Factions (by folder name or flag) - * @returns {Array.
- * @param {string} text
- * @returns {string}
- */
- static stripLeadingImage(text) {
- const s = String(text || '');
- if (!s) return '';
- // Common patterns: Markdown image at start, possibly followed by blank line
- const mdImg = /^(?:\s*)!\[[^\]]*\]\([^\)]+\)\s*(?:\n+)?/;
- if (mdImg.test(s)) return s.replace(mdImg, '').trimStart();
- // HTML possibly wrapped in
at the very start - const htmlImgP = /^(?:\s*)
]*>\s* Quests are linked from the Quest sheet itself; this list is read-only here. Quests are linked from the Quest sheet itself; this list is read-only here. Quests are linked from the Quest sheet itself; this list is read-only here. Quests are linked from the Quest sheet itself; this list is read-only here. No objectives Drag Characters, Factions, Locations, or Items onto this sheet to link them. Unlink from
+ here — related entities show this quest under their own "Quests" tab. GM-only notes are not visible to players. These Foundry journals have changes in Archivist. Check the ones you want to update. These Archivist entities don't have a Foundry journal yet. Check the ones you want to import.]*>\s*<\/p>\s*/i;
- if (htmlImgP.test(s)) return s.replace(htmlImgP, '').trimStart();
- const htmlImg = /^(?:\s*)
]*>\s*/i;
- if (htmlImg.test(s)) return s.replace(htmlImg, '').trimStart();
- return s;
- }
-
- /**
- * Ensure a journal has a single primary text page with provided content.
- * Works across Foundry versions (v10+ with pages collection).
- * @param {JournalEntry} journal
- * @param {string} content
- */
- static async ensureJournalTextPage(journal, content) {
- // v10+ API: JournalEntryPage documents under journal.pages
- const pagesCollection = journal.pages;
- const safeContent = String(content ?? '');
-
- console.log(`[Utils] ensureJournalTextPage:`, {
- journalId: journal?.id,
- journalName: journal?.name,
- contentLength: safeContent.length,
- contentPreview: safeContent.substring(0, 100),
- });
-
- if (pagesCollection) {
- const pages =
- pagesCollection.contents ?? (Array.isArray(pagesCollection) ? pagesCollection : []);
- const textPage = pages.find(p => p.type === 'text');
- if (textPage) {
- await textPage.update({ text: { content: safeContent, markdown: safeContent, format: 2 } });
- } else {
- await journal.createEmbeddedDocuments('JournalEntryPage', [
- {
- name: 'Description',
- type: 'text',
- text: { content: safeContent, markdown: safeContent, format: 2 },
- },
- ]);
- }
- return;
- }
- // Fallback (older Foundry versions) — use JournalEntry content
- await journal.update({ content: safeContent });
- }
-
- /**
- * Set a journal's thumbnail image (img property) to the provided URL.
- * Does not modify journal content or pages.
- * @param {JournalEntry} journal
- * @param {string} imageUrl
- */
- static async ensureJournalLeadImage(journal, imageUrl) {
- try {
- const url = String(imageUrl || '').trim();
- if (!url) return;
- console.debug('[Archivist Sync] ensureJournalLeadImage()', { journalId: journal?.id, url });
- // Set the journal thumbnail so it shows in lists
- try {
- await journal.update({ img: url });
- } catch (e) {
- console.debug('[Archivist Sync] journal img update failed', e);
- }
- } catch (e) {
- console.warn('[Archivist Sync] Failed to set journal lead image:', e);
- }
- }
-
- /**
- * Flags helpers for mapping Archivist IDs
- */
- static getActorArchivistId(actor) {
- return actor.getFlag(CONFIG.MODULE_ID, 'archivistId');
- }
-
- static async setActorArchivistId(actor, id, worldId) {
- await actor.setFlag(CONFIG.MODULE_ID, 'archivistId', id);
- if (worldId) await actor.setFlag(CONFIG.MODULE_ID, 'archivistWorldId', worldId);
- return true;
- }
-
- static getJournalArchivistMeta(journal) {
- return {
- id: journal.getFlag(CONFIG.MODULE_ID, 'archivistId') || null,
- type: journal.getFlag(CONFIG.MODULE_ID, 'archivistType') || null,
- worldId: journal.getFlag(CONFIG.MODULE_ID, 'archivistWorldId') || null,
- };
- }
-
- static async setJournalArchivistMeta(journal, id, type, worldId) {
- await journal.setFlag(CONFIG.MODULE_ID, 'archivistId', id);
- if (type) await journal.setFlag(CONFIG.MODULE_ID, 'archivistType', type);
- if (worldId) await journal.setFlag(CONFIG.MODULE_ID, 'archivistWorldId', worldId);
- }
-
- /**
- * Get Archivist metadata from a JournalEntryPage
- * @param {JournalEntryPage} page
- */
- static getPageArchivistMeta(page) {
- return {
- id: page?.getFlag?.(CONFIG.MODULE_ID, 'archivistId') || null,
- type: page?.getFlag?.(CONFIG.MODULE_ID, 'archivistType') || null,
- worldId: page?.getFlag?.(CONFIG.MODULE_ID, 'archivistWorldId') || null,
- };
- }
-
- /**
- * Set Archivist metadata on a JournalEntryPage
- * @param {JournalEntryPage} page
- * @param {string} id
- * @param {string} type
- * @param {string} worldId
- */
- static async setPageArchivistMeta(page, id, type, worldId) {
- if (!page) return;
- if (id) await page.setFlag(CONFIG.MODULE_ID, 'archivistId', id);
- if (type) await page.setFlag(CONFIG.MODULE_ID, 'archivistType', type);
- if (worldId) await page.setFlag(CONFIG.MODULE_ID, 'archivistWorldId', worldId);
- }
-
- /**
- * Ensure a single root-level JournalEntry exists as a container
- * @param {string} name
- * @returns {Promise
{{setup.name}}
+ {{/if}}
+ Objectives
+ {{#if setup.editingInfo}}
+
+ {{/if}}
+
+ {{#each quest.objectives}}
+
+ {{#unless quest.objectives.length}}
+ {{#unless setup.editingInfo}}
+ Progress Log
+
+ {{#each quest.progressLog}}
+
+ Related Entities
+ {{#if setup.isGM}}
+ Review Diffs ({{stats.diffs}})
+ Review Changes ({{stats.diffs}})
+
-
{{#each diffs as |d|}}
- Type
+
+ Type
Name
- Title
- Description
- Image
- Date
- Links
+ Name
+ Description
+ Image
+ Date
+ Links
+ class="{{#if d.selected}}selected{{/if}} {{#if d.deleted}}sync-deleted-row{{/if}}"
+ {{#if d.deleted}}title="This entity was deleted in Archivist. Syncing will permanently remove the Foundry journal."{{/if}}>
{{/each}}
{{d.type}}
{{d.name}}
- {{#if d.deleted}}Deleted{{/if}}
+ {{#if d.deleted}}
+ Deleted
+ {{/if}}
-
- {{#if d.changes.name}}{{/if}}
+
+ {{#if d.changes.name}}{{/if}}
-
- {{#if d.changes.description}}{{/if}}
+
+ {{#if d.changes.description}}{{/if}}
-
- {{#if d.changes.image}}{{/if}}
+
+ {{#if d.changes.image}}{{/if}}
-
- {{#if d.changes.sessionDate}}{{/if}}
+
+ {{#if d.changes.sessionDate}}{{/if}}
-
- {{#if d.changes.links}}{{/if}}
+
+ {{#if d.changes.links}}{{/if}}
Import New ({{stats.imports}})
+ Import New ({{stats.imports}})
+
-
@@ -94,11 +116,11 @@
- Type
+
+ Type
Name
- Create New
+ Create New
{{r.type}}
{{r.name}}
-
+
{{#if r.coreType}}
-
@@ -107,11 +129,19 @@