optimize formatting cleanup

This commit is contained in:
Kevin Jahns 2023-03-02 19:48:00 +01:00
parent bf338d8040
commit 035e350062

View File

@ -363,12 +363,18 @@ const formatText = (transaction, parent, currPos, length, attributes) => {
* @function * @function
*/ */
const cleanupFormattingGap = (transaction, start, curr, startAttributes, currAttributes) => { const cleanupFormattingGap = (transaction, start, curr, startAttributes, currAttributes) => {
let end = curr /**
const endAttributes = map.copy(currAttributes) * @type {Item|null}
*/
let end = start
/**
* @type {Map<string,ContentFormat>}
*/
const endFormats = map.create()
while (end && (!end.countable || end.deleted)) { while (end && (!end.countable || end.deleted)) {
if (!end.deleted && end.content.constructor === ContentFormat) { if (!end.deleted && end.content.constructor === ContentFormat) {
// @todo should set endAttributes[end.key] = end and then compare identities instead of values const cf = /** @type {ContentFormat} */ (end.content)
updateCurrentAttributes(endAttributes, /** @type {ContentFormat} */ (end.content)) endFormats.set(cf.key, cf)
} }
end = end.right end = end.right
} }
@ -384,7 +390,7 @@ const cleanupFormattingGap = (transaction, start, curr, startAttributes, currAtt
case ContentFormat: { case ContentFormat: {
const { key, value } = /** @type {ContentFormat} */ (content) const { key, value } = /** @type {ContentFormat} */ (content)
const startAttrValue = startAttributes.get(key) || null const startAttrValue = startAttributes.get(key) || null
if ((endAttributes.get(key) || null) !== value || startAttrValue === value) { if (endFormats.get(key) !== content || startAttrValue === value) {
// Either this format is overwritten or it is not necessary because the attribute already existed. // Either this format is overwritten or it is not necessary because the attribute already existed.
start.delete(transaction) start.delete(transaction)
cleanups++ cleanups++