Closed BrianHung closed 3 years ago
Use ProseMirror method findDiffStart to compare / diff documents instead of converting to JSON and then using lib0's equalityDeep:
findDiffStart
equalityDeep
- !fun.equalityDeep(view.state.doc.type.createAndFill().content.toJSON(), view.state.doc.content.toJSON()) + view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) != null
Reasoning is equalityDeep fails on two equal JSON objects created by ProseMirror's toJSON(), because ProseMirror uses Object.empty(null) to initialize node attrs, which results in equalityDeep returning false because Object.create(null).constructor is undefined (instead of Object as expected). See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#custom_and_null_objects.
toJSON()
Object.empty(null)
Object.create(null).constructor
Another work around would be to use
!fun.equalityDeep( JSON.parse(JSON.stringify(view.state.doc.type.createAndFill().content.toJSON())), JSON.parse(JSON.stringify(view.state.doc.content.toJSON())), );
which works correctly by converting Object.create(null) and forcing it to inherit the Object.constructor method. But
Object.create(null)
Object.constructor
JSON.parse(JSON.stringify(
Thanks! @BrianHung Merged and published the fix in v1.0.9
v1.0.9
Use ProseMirror method
findDiffStart
to compare / diff documents instead of converting to JSON and then using lib0'sequalityDeep
:Reasoning is
equalityDeep
fails on two equal JSON objects created by ProseMirror'stoJSON()
, because ProseMirror usesObject.empty(null)
to initialize node attrs, which results inequalityDeep
returning false becauseObject.create(null).constructor
is undefined (instead of Object as expected). See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#custom_and_null_objects.Another work around would be to use
which works correctly by converting
Object.create(null)
and forcing it to inherit theObject.constructor
method. ButJSON.parse(JSON.stringify(
.