BUG: Custom Dynamic List freezes after binding array and expanding rows to match length
Question
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
140.0.7339.214(正式版本) (x86_64)
Reproducible demo link
https://codesandbox.io/p/devbox/d27l6n
Describe the bug
I want to create a custom dynamic list component based on GrapesJS (with 3 rows by default). It can bind to an array variable and dynamically change the number of rows in the container according to the array length. Now I'm encountering a freeze issue. The reproduction steps are as follows:
-
Click the Variables tab on the left and create a new array variable with a length of 4.
[![enter image description here][1]][1]
-
Go back to the Components tab and drag a Dynamic List container component onto the canvas.
-
Select the Dynamic List component you just dragged in. On the right, the Styles tab will show a dropdown to bind an array variable. Choose the array variable created in step 1. After selection, the dynamic list on the canvas changes from 3 rows to 4 rows, which is expected.
[![enter image description here][2]][2]
4. Move the mouse over the dynamic list component (now 4 rows) on the canvas. You'll notice the page freezes.
After my investigation, the key issue lies in the logic that adjusts the number of container rows based on the array length, but I couldn't pinpoint the cause.
editor.on('component:update:attributes', (comp: any, attrs?: any, opts?: any) => {
console.log('component:update:attributes')
if (isSyncing(editor)) return;
if (comp.get && comp.get('type') === DL_TYPE) {
const dlAttrs = comp.getAttributes?.() || {};
const varName = dlAttrs[DL_ATTR_ARRAY_VAR];
const arrData = varName ? getDLArrayData(editor, comp) : undefined;
if (!varName || !Array.isArray(arrData)) {
// clearBindingsAndResetDefaults(editor, comp);
} else {
ensureRowCountByArray(editor, comp);
}
return;
}
});
function cloneIntoTarget(sourceRow: any, targetRow: any) {
console.log('cloneIntoTarget')
sourceRow.components().each((child: any) => {
const cloned = child.clone();
targetRow.append(cloned);
});
}
function ensureRowCountByArray(editor: any, dl: any) {
console.log('ensureRowCountByArray')
if (isSyncing(editor)) return;
const arr = getDLArrayData(editor, dl);
const targetCount = Array.isArray(arr) ? (arr.length > 0 ? arr.length : 3) : 3;
markSyncing(editor, true);
try {
let rows = getRows(dl);
const curCount = rows.length;
if (curCount !== targetCount) {
if (rows.length === 0) {
dl.append({ type: ROW_TYPE });
rows = getRows(dl);
}
const baseRow = rows[0];
if (curCount < targetCount) {
const need = targetCount - curCount;
for (let i = 0; i < need; i++) {
const newRow = dl.append({ type: ROW_TYPE })[0];
cloneIntoTarget(baseRow, newRow);
}
} else {
for (let i = curCount - 1; i >= targetCount; i--) {
rows[i].remove();
}
}
}
} finally {
markSyncing(editor, false);
}
}
What's going on here?
[1]: https://i.sstatic.net/7AlxKreK.png
[2]: https://i.sstatic.net/fe2lo06t.png
### Code of Conduct
- [x] I agree to follow this project's Code of ConductAnswers (0)
No answers yet.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #6685
BUG: Custom component styles are not applied after deletion and re-addition
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v143.0.7499.193 Reproducible demo l...
Issue #3939
BUG: Disabled drag and drop in preview
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? v95Reproducible demo link https://codesandbo...
Issue #4573
BUG: parseStyle is not a function
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Firefox 104.0.1Reproducible demo link https:...
Issue #6152
BUG: CSS added via custom code persists after custom code component is removed
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? any Reproducible demo link https://grapesj...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.