Skip to content

Commit

Permalink
Merge pull request #1045 from 3DStreet/fix-duplicate-id-upon-load
Browse files Browse the repository at this point in the history
autofix scenes with dupe ids
  • Loading branch information
kfarr authored Jan 27, 2025
2 parents d4e6c44 + c111098 commit 4d6eee1
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/editor/lib/SceneUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
updateScene,
uploadThumbnailImage
} from '@/editor/api/scene';
import { createUniqueId } from '@/editor/lib/entity.js';

export function createBlankScene() {
STREET.utils.newScene();
Expand Down Expand Up @@ -52,15 +53,49 @@ export function createElementsForScenesFromJSON(streetData) {
return;
}

STREET.utils.createEntities(streetData, streetContainerEl);
const processStreetDataForDuplicateIds = (data) => {
// Keep track of IDs we've seen during processing
const seenIds = new Set();
let changeCounter = 0;

// Main recursive function to process IDs and children
const processItem = (obj) => {
if (obj.id) {
if (seenIds.has(obj.id)) {
// If we've seen this ID before, generate a new one
obj.id = createUniqueId();
changeCounter++;
} else {
// First time seeing this ID, add it to seen set
seenIds.add(obj.id);
}
}

if (obj.children) {
obj.children = obj.children.map(processItem);
}

return obj;
};
const output = data.map(processItem);
if (changeCounter > 0) {
console.log(`Duplicate IDs fixed: ${changeCounter} instances`);
}
return output;
};

const correctedStreetData = processStreetDataForDuplicateIds(streetData);

STREET.utils.createEntities(correctedStreetData, streetContainerEl);
AFRAME.scenes[0].emit('newScene');
}

export function fileJSON(event) {
let reader = new FileReader();

reader.onload = function () {
STREET.utils.createElementsFromJSON(reader.result, true);
const data = JSON.parse(reader.result);
createElementsForScenesFromJSON(data.data);
};

reader.readAsText(event.target.files[0]);
Expand Down

0 comments on commit 4d6eee1

Please sign in to comment.