Add options to import documents into specific folders or compendium packs and sort them by tier.
This commit is contained in:
parent
3e9bc42ca4
commit
f5153485af
4 changed files with 133 additions and 10 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"id": "dh-importer",
|
"id": "dh-importer",
|
||||||
"title": "Daggerheart Statblock Importer",
|
"title": "Daggerheart Statblock Importer",
|
||||||
"version": "1.2.0",
|
"version": "1.2.1",
|
||||||
"compatibility": {
|
"compatibility": {
|
||||||
"minimum": "13",
|
"minimum": "13",
|
||||||
"verified": "13"
|
"verified": "13"
|
||||||
|
|
@ -34,5 +34,5 @@
|
||||||
"description": "Imports Adversaries and Environments from text blocks into the Daggerheart system.",
|
"description": "Imports Adversaries and Environments from text blocks into the Daggerheart system.",
|
||||||
"url": "https://github.com/cptn-cosmo/dh-importer",
|
"url": "https://github.com/cptn-cosmo/dh-importer",
|
||||||
"manifest": "https://git.geeks.gay/cosmo/dh-importer/raw/branch/main/module.json",
|
"manifest": "https://git.geeks.gay/cosmo/dh-importer/raw/branch/main/module.json",
|
||||||
"download": "https://git.geeks.gay/cosmo/dh-importer/releases/download/1.2.0/dh-importer.zip"
|
"download": "https://git.geeks.gay/cosmo/dh-importer/releases/download/1.2.1/dh-importer.zip"
|
||||||
}
|
}
|
||||||
|
|
@ -47,7 +47,9 @@ export class DHImporterApp extends HandlebarsApplicationMixin(ApplicationV2) {
|
||||||
adversary: "Adversary",
|
adversary: "Adversary",
|
||||||
environment: "Environment"
|
environment: "Environment"
|
||||||
},
|
},
|
||||||
inputText: this.inputText
|
inputText: this.inputText,
|
||||||
|
folders: game.folders.filter(f => f.type === "Actor").sort((a, b) => a.name.localeCompare(b.name)),
|
||||||
|
packs: game.packs.filter(p => p.documentName === "Actor" && !p.locked)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,7 +154,10 @@ export class DHImporterApp extends HandlebarsApplicationMixin(ApplicationV2) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await DHImporter.createDocuments(this.parsedData, this.parsedDataType);
|
const importTarget = formData.importTarget || "";
|
||||||
|
const sortTier = formData.sortTier || false;
|
||||||
|
|
||||||
|
await DHImporter.createDocuments(this.parsedData, this.parsedDataType, { target: importTarget, sortTier });
|
||||||
ui.notifications.info(`Successfully imported ${this.parsedData.length} documents.`);
|
ui.notifications.info(`Successfully imported ${this.parsedData.length} documents.`);
|
||||||
this.close();
|
this.close();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -544,19 +544,86 @@ export class DHImporter {
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async createDocuments(dataList, type) {
|
static async createDocuments(dataList, type, options = {}) {
|
||||||
if (!dataList || dataList.length === 0) return;
|
if (!dataList || dataList.length === 0) return;
|
||||||
|
|
||||||
|
const { target, sortTier } = options;
|
||||||
const Actor = getDocumentClass("Actor");
|
const Actor = getDocumentClass("Actor");
|
||||||
|
const Folder = getDocumentClass("Folder");
|
||||||
const actorsToCreate = [];
|
const actorsToCreate = [];
|
||||||
|
|
||||||
|
// Determine Target Context
|
||||||
|
let targetFolderId = null;
|
||||||
|
let targetPack = null;
|
||||||
|
|
||||||
|
if (target) {
|
||||||
|
if (game.packs.has(target)) {
|
||||||
|
targetPack = game.packs.get(target);
|
||||||
|
} else {
|
||||||
|
targetFolderId = target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Folder Cache for Tier Sorting
|
||||||
|
const folderCache = {};
|
||||||
|
|
||||||
|
const getTierFolder = async (tier) => {
|
||||||
|
const folderName = `Tier ${tier}`;
|
||||||
|
const cacheKey = `${targetPack ? targetPack.collection : "world"}-${targetFolderId || "root"}-${tier}`;
|
||||||
|
|
||||||
|
if (folderCache[cacheKey]) return folderCache[cacheKey];
|
||||||
|
|
||||||
|
// Parent ID (for creating subfolder)
|
||||||
|
const parentId = targetFolderId;
|
||||||
|
|
||||||
|
// Search for existing folder
|
||||||
|
let folder;
|
||||||
|
if (targetPack) {
|
||||||
|
// Search in compendium index (requires V11+ folders)
|
||||||
|
// We'll rely on pack.folders if available (V11)
|
||||||
|
if (targetPack.folders) {
|
||||||
|
folder = targetPack.folders.find(f => f.name === folderName && f.folder?.id === parentId);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Search in World
|
||||||
|
folder = game.folders.find(f => f.type === "Actor" && f.name === folderName && f.folder?.id === parentId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (folder) {
|
||||||
|
folderCache[cacheKey] = folder.id;
|
||||||
|
return folder.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Folder if not found
|
||||||
|
try {
|
||||||
|
const folderData = {
|
||||||
|
name: folderName,
|
||||||
|
type: "Actor",
|
||||||
|
folder: parentId,
|
||||||
|
sorting: "a"
|
||||||
|
};
|
||||||
|
|
||||||
|
if (targetPack) {
|
||||||
|
folderData.pack = targetPack.collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newFolder = await Folder.create(folderData, { pack: targetPack?.collection });
|
||||||
|
if (newFolder) {
|
||||||
|
folderCache[cacheKey] = newFolder.id;
|
||||||
|
return newFolder.id;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn("Could not create Tier folder:", e);
|
||||||
|
// Fallback to parent
|
||||||
|
return parentId;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for (const data of dataList) {
|
for (const data of dataList) {
|
||||||
// Process Items based on 'useCompendium' flags or similar set by the UI
|
// Process Items
|
||||||
// The UI should have modified data.items or we loop through and replace based on user choice
|
|
||||||
const finalItems = [];
|
const finalItems = [];
|
||||||
for (const item of data.items) {
|
for (const item of data.items) {
|
||||||
if (data.useFeatures && data.useFeatures[item.name]) {
|
if (data.useFeatures && data.useFeatures[item.name]) {
|
||||||
// User selected to use existing feature
|
|
||||||
const uuid = data.useFeatures[item.name];
|
const uuid = data.useFeatures[item.name];
|
||||||
const original = await fromUuid(uuid);
|
const original = await fromUuid(uuid);
|
||||||
if (original) {
|
if (original) {
|
||||||
|
|
@ -570,21 +637,43 @@ export class DHImporter {
|
||||||
}
|
}
|
||||||
data.items = finalItems;
|
data.items = finalItems;
|
||||||
|
|
||||||
|
// Handle Sorting / Folder Assignment
|
||||||
|
if (sortTier) {
|
||||||
|
const tier = data.system.tier !== undefined ? data.system.tier : 0;
|
||||||
|
data.folder = await getTierFolder(tier);
|
||||||
|
} else if (targetFolderId) {
|
||||||
|
data.folder = targetFolderId;
|
||||||
|
}
|
||||||
|
|
||||||
// Clean up temporary flags
|
// Clean up temporary flags
|
||||||
const shouldOpen = data.openSheet;
|
const shouldOpen = data.openSheet;
|
||||||
delete data.foundFeatures;
|
delete data.foundFeatures;
|
||||||
delete data.useFeatures;
|
delete data.useFeatures;
|
||||||
delete data.openSheet;
|
delete data.openSheet;
|
||||||
|
|
||||||
|
// If finding an existing actor to update could be a future feature, but for now we create new.
|
||||||
actorsToCreate.push({ data, shouldOpen });
|
actorsToCreate.push({ data, shouldOpen });
|
||||||
}
|
}
|
||||||
|
|
||||||
const createdActors = await Actor.createDocuments(actorsToCreate.map(a => a.data));
|
let createdActors = [];
|
||||||
|
if (targetPack) {
|
||||||
|
// Import to Compendium
|
||||||
|
// Note: createDocuments with pack option works in V10+
|
||||||
|
const docs = actorsToCreate.map(a => a.data);
|
||||||
|
createdActors = await Actor.createDocuments(docs, { pack: targetPack.collection });
|
||||||
|
} else {
|
||||||
|
// Import to World
|
||||||
|
createdActors = await Actor.createDocuments(actorsToCreate.map(a => a.data));
|
||||||
|
}
|
||||||
|
|
||||||
// Open sheets
|
// Open sheets
|
||||||
if (createdActors && createdActors.length > 0) {
|
if (createdActors && createdActors.length > 0) {
|
||||||
for (let i = 0; i < createdActors.length; i++) {
|
for (let i = 0; i < createdActors.length; i++) {
|
||||||
if (actorsToCreate[i].shouldOpen) {
|
// If we imported to compendium, we generally don't open sheets immediately as they are in the pack
|
||||||
|
// But if the user really wants to, we'd have to retrieve the sheet from the pack.
|
||||||
|
// Standard behavior for compendium import: don't open.
|
||||||
|
|
||||||
|
if (!targetPack && actorsToCreate[i].shouldOpen) {
|
||||||
createdActors[i].sheet.render(true);
|
createdActors[i].sheet.render(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,35 @@
|
||||||
{{/each}}
|
{{/each}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="import-options"
|
||||||
|
style="margin-top: 10px; padding: 10px; background: rgba(0,0,0,0.1); border-radius: 5px;">
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Import Target</label>
|
||||||
|
<select name="importTarget">
|
||||||
|
<option value="">World (Root)</option>
|
||||||
|
{{#if folders.length}}
|
||||||
|
<optgroup label="Folders">
|
||||||
|
{{#each folders}}
|
||||||
|
<option value="{{this.id}}">{{this.name}}</option>
|
||||||
|
{{/each}}
|
||||||
|
</optgroup>
|
||||||
|
{{/if}}
|
||||||
|
{{#if packs.length}}
|
||||||
|
<optgroup label="Compendiums">
|
||||||
|
{{#each packs}}
|
||||||
|
<option value="{{this.collection}}">{{this.metadata.label}}</option>
|
||||||
|
{{/each}}
|
||||||
|
</optgroup>
|
||||||
|
{{/if}}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" name="sortTier"> Sort into Sub-folders by Tier
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="action-footer">
|
<div class="action-footer">
|
||||||
<button type="button" data-action="back"><i class="fas fa-arrow-left"></i> Back</button>
|
<button type="button" data-action="back"><i class="fas fa-arrow-left"></i> Back</button>
|
||||||
<button type="button" data-action="import"><i class="fas fa-file-import"></i> Confirm Import</button>
|
<button type="button" data-action="import"><i class="fas fa-file-import"></i> Confirm Import</button>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue