codex-team / codex-team/editor.js

[Bug] Editing the class of a node always triggers 'didMutated'

Open
#1,963 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
31.9k
Forks
2.2k
Avg merge
1d 1h
Merged PRs (30d)
1

Description

When changing the class name of an HTML element inside a block, this always triggers the `didMutated` event.

I've tried to disable this behavior by adding the attribute `data-mutation-free="true"` to the element, but the code does not respect this because the function `shouldFireUpdate` only considers added nodes and removed nodes with this attribute, but not the changed node itself.

How about changing [this function](https://github.com/codex-team/editor.js/blob/27aa9164a11aa61dee58c3ebb459180eac920404/src/components/block/index.ts#L205)

const shouldFireUpdate = mutationsOrInputEvent instanceof InputEvent ||
!mutationsOrInputEvent.some(({
addedNodes = [],
removedNodes,
}) => {
return [...Array.from(addedNodes), ...Array.from(removedNodes)]
.some(node => $.isElement(node) && (node as HTMLElement).dataset.mutationFree === 'true');
});

to this:

const shouldFireUpdate = mutationsOrInputEvent instanceof InputEvent ||
!mutationsOrInputEvent.some((mutationEvent) => {
return [
mutationEvent.target,
...Array.from(mutationEvent.addedNodes),
...Array.from(mutationEvent.removedNodes)
]
.some(node => $.isElement(node) && (node as HTMLElement).dataset.mutationFree === 'true');
});

This would include the target of the mutation record in the check for the attribute.

Steps to reproduce:
1. Change the class name of an HTML element with the attribute `data-mutation-free="true"`

Expected behavior:
The `didMutated` event should not fire.

Device, Browser, OS: all

Editor.js version: 2.23.0

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.