Skip to content

Commit

Permalink
fix: remove element stats in pages (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjuanes authored Oct 28, 2024
1 parent dfad930 commit 82e8575
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 30 deletions.
4 changes: 2 additions & 2 deletions app/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ export const getElementSnappingPoints = (element, snapEdge) => {
};

// @public generate display name for the provided element
export const getElementDisplayName = (element, index = 0) => {
return [getElementConfig(element).displayName, index + 1].join(" ");
export const getElementDisplayName = element => {
return getElementConfig(element).displayName;
};

// @public get the bounds of the provided elements
Expand Down
32 changes: 4 additions & 28 deletions app/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,9 @@ const fixPagesIndex = pages => {
});
};

// @private fix elements names in provided page
const fixElementsNamesInPage = page => {
// Check if we have to initialize page counts
if (page.elements.length > 0 && Object.keys(page.stats?.elementCounts || {}).length === 0) {
page.stats.elementCounts = {};
page.elements.forEach(element => {
const type = element[FIELDS.TYPE];
const index = page.stats.elementCounts[type] ?? 0;
element[FIELDS.NAME] = element[FIELDS.NAME] || getElementDisplayName(element, index);
page.stats.elementCounts[type] = index + 1;
});
}
return page;
};

// @private create a new page
const createPage = (page, index = 0) => {
return fixElementsNamesInPage({
return {
id: page?.id || generateRandomId(),
title: page?.title || `Page ${index + 1}`,
elements: (page?.elements || []).map(element => ({
Expand All @@ -216,18 +201,15 @@ const createPage = (page, index = 0) => {
[FIELDS.EDITING]: false,
[FIELDS.CREATING]: false,
[FIELDS.VERSION]: element[FIELDS.VERSION] ?? 0,
[FIELDS.NAME]: element[FIELDS.NAME] || getElementDisplayName(element),
})),
history: page?.history || [],
historyIndex: page?.historyIndex ?? 0,
translateX: page?.translateX ?? 0,
translateY: page?.translateY ?? 0,
zoom: page?.zoom ?? ZOOM_DEFAULT,
activeGroup: null,
stats: {
elementCounts: {},
...(page?.stats || {}),
},
});
};
};

// Default values for new elements
Expand Down Expand Up @@ -296,7 +278,6 @@ export const createScene = initialData => {
id: page.id,
title: page.title,
elements: page.elements,
stats: page?.stats || {},
})),
assets: scene.assets,
background: scene.background,
Expand Down Expand Up @@ -360,7 +341,6 @@ export const createScene = initialData => {
scene.addPage({
title:"Copy of " + (page?.title || "-"),
elements: page?.elements || [],
stats: page?.stats || {},
}, setAsActive);
},

Expand Down Expand Up @@ -446,11 +426,8 @@ export const createScene = initialData => {
const numElements = scene.page.elements.length;
// 1. Fix elements values
elements.forEach((element, index) => {
const type = element[FIELDS.TYPE];
const count = scene.page.stats.elementCounts[type] ?? 0;
element[FIELDS.NAME] = element[FIELDS.NAME] || getElementDisplayName(element, count);
element[FIELDS.NAME] = element[FIELDS.NAME] || getElementDisplayName(element);
element[FIELDS.ORDER] = numElements + index;
scene.page.stats.elementCounts[type] = count + 1;
});
// 2. Register element create in the history
scene.addHistory({
Expand Down Expand Up @@ -652,7 +629,6 @@ export const createScene = initialData => {
// 3. insert new elements
newElements.forEach(element => {
scene.page.elements.push(element);
scene.page.stats.elementCounts[element.type] = (scene.page.stats.elementCounts[element.type] ?? 0) + 1;
});
// 4.1 Register CREATE change
changes.push({
Expand Down

0 comments on commit 82e8575

Please sign in to comment.