diff --git a/app/imports/api/creature/creatureProperties/methods/insertPropertyFromLibraryNode.js b/app/imports/api/creature/creatureProperties/methods/insertPropertyFromLibraryNode.js index 6628fb7d..e25f633c 100644 --- a/app/imports/api/creature/creatureProperties/methods/insertPropertyFromLibraryNode.js +++ b/app/imports/api/creature/creatureProperties/methods/insertPropertyFromLibraryNode.js @@ -59,8 +59,10 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({ 'ancestors.id': nodeId, removed: {$ne: true}, }).fetch(); - // The root node is last in the array of nodes - nodes.push(node); + + // The root node is first in the array of nodes + // It must get the first generated ID to prevent flickering + nodes = [node, ...nodes]; // re-map all the ancestors setLineageOfDocs({ @@ -82,10 +84,10 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({ }); // Insert the creature properties - let insertedDocIds = CreatureProperties.batchInsert(nodes); + CreatureProperties.batchInsert(nodes); // get the root inserted doc - let rootId = insertedDocIds[insertedDocIds.length - 1]; + let rootId = node._id; // Tree structure changed by inserts, reorder the tree reorderDocs({ @@ -97,7 +99,6 @@ const insertPropertyFromLibraryNode = new ValidatedMethod({ recomputeInactiveProperties(rootId); // Inserting a creature property invalidates dependencies: full recompute recomputeCreatureByDoc(rootCreature); - // Return the docId of the last property, the inserted root property return rootId; }, diff --git a/app/imports/api/parenting/parenting.js b/app/imports/api/parenting/parenting.js index 52a83d3d..b7135356 100644 --- a/app/imports/api/parenting/parenting.js +++ b/app/imports/api/parenting/parenting.js @@ -120,13 +120,19 @@ export function setLineageOfDocs({docArray, oldParent, newAncestry}){ export function renewDocIds({docArray, collectionMap}){ // map of {oldId: newId} let idMap = {}; - // Give new ids and map the changes + + // Get a random generator that's consistent on client and server + let randomSrc = DDP.randomStream('renewDocIds'); + + // Give new ids and map the changes as {oldId: newId} docArray.forEach(doc => { let oldId = doc._id; - let newId = Random.id(); + let newId = randomSrc.id(); doc._id = newId; idMap[oldId] = newId; }); + + // Remap all references using the new IDs const remapReference = ref => { if (idMap[ref.id]){ ref.id = idMap[ref.id];