perf: add identity checking, avoiding decoration facet recomputation

This commit is contained in:
kotaindah55 2025-03-17 10:28:21 +02:00
parent 076ed7d952
commit 47bf42e0fe

View file

@ -5,23 +5,32 @@ import { EditorView } from "@codemirror/view";
/** Stores only height-altering decorations. */
export const decoSetField = StateField.define({
create(state) {
let builder = state.field(builderField);
let holder = state.field(builderField).holder;
return {
lineBreaksSet: builder.holder.lineBreaksSet,
blockOmittedSet: builder.holder.blockOmittedSet
lineBreaksSet: holder.lineBreaksSet,
blockOmittedSet: holder.blockOmittedSet
};
},
update(decoSet, transaction) {
let builder = transaction.state.field(builderField);
let holder = transaction.state.field(builderField).holder;
// Avoid `EditorView.decorations` recomputation while the sets haven't
// changed/updated yet. It's done by returning same object rather than
// creating the new one, though the same holder.
if (
holder.lineBreaksSet === decoSet.lineBreaksSet &&
holder.blockOmittedSet === decoSet.blockOmittedSet
) { return decoSet }
return {
lineBreaksSet: builder.holder.lineBreaksSet,
blockOmittedSet: builder.holder.blockOmittedSet
lineBreaksSet: holder.lineBreaksSet,
blockOmittedSet: holder.blockOmittedSet
};
},
provide(field) {
// The both sets replace line breaks with "<br>" or hide it. Hence, we
// need providing it directly, i.e. not in the view update.
return [
EditorView.decorations.from(field, set => set.blockOmittedSet),
EditorView.decorations.from(field, set => set.lineBreaksSet)
]
},
}
});