Skip to content

Commit 0354cd1

Browse files
authored
Update item compendium migration (#791)
* Retain specified item fields during migrations * Update the rendering on item selection when comparison is present
1 parent 3c559c5 commit 0354cd1

File tree

8 files changed

+57
-2
lines changed

8 files changed

+57
-2
lines changed

lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,7 @@
17651765
"PressurePoint": "Pressure Point",
17661766
"Stagger": "Stagger",
17671767
"CompendiumBrowser": "Compendium Browser",
1768+
"CompendiumItem": "Compendium Item",
17681769
"CompendiumMigrateItem": "Migrate Item",
17691770
"CompendiumMigrateItemMessage": "Would you like to migrate the data in the item <strong>{name}</strong> to that of the compendium?",
17701771
"CompendiumMigrateActorItems": "Migrate Actor Items",

module/documents/items/item-data-model.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ export class FUItemDataModel extends foundry.abstract.TypeDataModel {
1919
return [];
2020
}
2121

22+
/**
23+
* @returns {String[]} The path to fields in this data model that should be retained during migrations.
24+
*/
25+
get retainedFieldPaths() {
26+
return [];
27+
}
28+
2229
static migrateData(source) {
2330
source = super.migrateData(source);
2431
if (source.source?.value) {

module/documents/items/skill/skill-data-model.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ export class SkillDataModel extends BaseSkillDataModel {
243243
return [...this.commonPartials, ItemPartialTemplates.classField, ItemPartialTemplates.skillAttributes];
244244
}
245245

246+
get retainedFieldPaths() {
247+
return ['level.value'];
248+
}
249+
246250
/**
247251
* Action definition, invoked by sheets when 'data-action' equals the method name and no action defined on the sheet matches that name.
248252
* @param {PointerEvent} event

module/helpers/foundry-utils.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { StringUtils } from './string-utils.mjs';
22
import { systemTemplatePath } from './system-utils.mjs';
33
import { TextEditor } from './text-editor.mjs';
44
import { CompendiumIndex } from '../ui/compendium/compendium-index.mjs';
5+
import { ObjectUtils } from './object-utils.mjs';
56

67
const { api, fields, handlebars } = foundry.applications;
78

@@ -405,6 +406,16 @@ export default class FoundryUtils {
405406
* @async
406407
*/
407408
static async migrateItem(sourceItem, targetItem) {
409+
// Gather retained data model properties
410+
const retainedFields = {};
411+
for (const fieldPath of targetItem.system.retainedFieldPaths) {
412+
const systemPath = `system.${fieldPath}`;
413+
const fieldValue = ObjectUtils.getProperty(targetItem, systemPath);
414+
if (fieldValue !== undefined) {
415+
retainedFields[systemPath] = fieldValue;
416+
}
417+
}
418+
408419
// Properties
409420
await targetItem.update(
410421
{
@@ -416,6 +427,9 @@ export default class FoundryUtils {
416427
{ diff: false },
417428
);
418429

430+
// After the deep clone, apply them again.
431+
await targetItem.update(retainedFields);
432+
419433
// Effects
420434
const updates = [];
421435
const creates = [];

module/helpers/object-utils.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ function getProperty(obj, path) {
3434
return foundry.utils.getProperty(obj, path);
3535
}
3636

37+
/**
38+
* @param {Object} obj The object to set the property on
39+
* @param {String} path The path to the property, in dot notation
40+
* @param {*} value The value to set on the property.
41+
*/
42+
function setProperty(obj, path, value) {
43+
return foundry.utils.setProperty(obj, path, value);
44+
}
45+
3746
/**
3847
*
3948
* @param {Object} obj
@@ -56,6 +65,7 @@ function pick(record, keys) {
5665
export const ObjectUtils = Object.freeze({
5766
mergeRecursive,
5867
getProperty,
68+
setProperty,
5969
cleanObject,
6070
pick,
6171
});

module/sheets/actor-sheet.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,17 @@ export class FUActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
165165
});
166166

167167
items = updates.map((upd) => upd.item);
168+
const compendiumItems = updates.map((upd) => upd.compendiumItem);
168169

170+
// TODO: Custom table?
169171
const title = 'FU.CompendiumMigrateActorItems';
170172
/** @type ItemSelectionData **/
171173
const data = {
172174
title: title,
173175
message,
174176
style: 'list',
175177
items: items,
178+
compendiumItems: compendiumItems,
176179
initial: items,
177180
getDescription: async (item) => {
178181
const text = item.system?.description ?? '';

module/ui/features/item-selection-dialog.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import FoundryUtils from '../../helpers/foundry-utils.mjs';
55
* @property {String} title
66
* @property {String} message
77
* @property {FUItem[]|FUActiveEffect[]} items
8+
* @property {FUItem[]} compendiumItems If assigned, will be used to compare to the original items.
89
* @property {Object[]} initial
910
* @property {'grid'|'list'} style
1011
* @property {Number} max

templates/dialog/dialog-item-selection.hbs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
</span>
4343
</legend>
4444
<table class="table-compact fu-scrollable" style="max-height: 400px;">
45+
<thead>
46+
<tr>
47+
<th></th>
48+
<th>{{localize 'FU.Item'}}</th>
49+
{{#if compendiumItems}}
50+
<th>{{localize 'FU.CompendiumItem'}}</th>
51+
{{/if}}
52+
</tr>
53+
</thead>
4554
{{#each items as |item|}}
4655
<tr>
4756
<td>
@@ -56,10 +65,16 @@
5665
</td>
5766
<td>
5867
{{pfuItemAnchor item}}
59-
</td>
60-
<td>
6168
<span data-tooltip="{{lookup ../descriptions @index}}">{{item.name}}</span>
6269
</td>
70+
{{#if @root.compendiumItems}}
71+
<td>
72+
{{#with (lookup ../compendiumItems @index) as |item|}}
73+
{{pfuItemAnchor item}}
74+
<span>{{item.name}}</span>
75+
{{/with}}
76+
</td>
77+
{{/if}}
6378
</tr>
6479
{{/each}}
6580
</table>

0 commit comments

Comments
 (0)