Skip to content

Commit abcb3bf

Browse files
committed
fix: fix renaming across all pages
1 parent 2502f81 commit abcb3bf

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

src/model/GrapesJsSelectors.ts

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,45 @@ function renameCssClass(editor: Editor, oldClassName: string, newClassName: stri
233233
}
234234

235235
// TODO: handle the related selectors
236+
// IMPORTANT: Find components BEFORE renaming the selector
237+
// Because oldSelector.set('name') will automatically update component.getClasses()
238+
239+
// Helper function to recursively find all components with a class in their selector
240+
const findComponentsWithClass = (component: Component, className: string): Component[] => {
241+
const results: Component[] = []
242+
243+
// Check if this component has the class in its selector
244+
const selector = component.get('selector')
245+
if (selector?.mainSelector?.selectors) {
246+
const hasClass = selector.mainSelector.selectors.some(
247+
(sel: any) => sel.type === SimpleSelectorType.CLASS && sel.value === className
248+
)
249+
if (hasClass) {
250+
results.push(component)
251+
}
252+
}
253+
254+
// Recursively check children
255+
component.components().forEach((child: Component) => {
256+
results.push(...findComponentsWithClass(child, className))
257+
})
258+
259+
return results
260+
}
261+
236262
const toUpdate = editor.Pages.getAll()
237-
.flatMap(page => page.getMainComponent().find(`.${oldClassName}`))
263+
.flatMap(page => findComponentsWithClass(page.getMainComponent(), oldClassName))
238264

239-
// This has to be before we update components
265+
// This has to be before we update component selectors (but after finding them!)
240266
// Otherwise it messes up the undo
241267
oldSelector.set('name', newClassName)
242268

243269
// Rename the class in the components
244270
// TODO: handle the related selectors
271+
// NOTE: No need to manually remove the old class from component.getClasses()
272+
// because oldSelector.set('name', newClassName) already updated it automatically
245273
toUpdate.forEach(component => {
274+
// Update the selector stored in component attributes
246275
const selector = getComponentSelector(component)
247276
const updatedSelector = {
248277
...selector,
@@ -285,17 +314,16 @@ export function setComponentSelector(component: Component, selector: ComplexSele
285314
}
286315
})
287316
// FIXME: Add back the protected classes
288-
// Add the missing classes
317+
// Replace the classes with the ones from the selector
289318
const existing = component.getClasses()
290-
const toAdd = existing
291-
.filter((c: string) => !classes.includes(c))
292-
.concat(
293-
classes
294-
.filter((c: string) => !existing.includes(c))
295-
)
296-
if (toAdd.length > 0) {
297-
component.setClass(component.getClasses().concat(toAdd))
298-
}
319+
320+
// Keep existing classes that are not in the selector + add all classes from the selector
321+
const finalClasses = [
322+
...existing.filter((c: string) => !classes.includes(c)),
323+
...classes
324+
]
325+
326+
component.setClass(finalClasses)
299327
}
300328

301329
export function getComponentSelector(component: Component): ComplexSelector {

0 commit comments

Comments
 (0)