Skip to content

Commit

Permalink
Merge pull request #540 from yjs/ytext-cleanup-538-refactor
Browse files Browse the repository at this point in the history
Ytext cleanup 538 refactor
  • Loading branch information
dmonad committed Jun 15, 2023
2 parents 00ef472 + ce098d0 commit 9563612
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 46 deletions.
97 changes: 51 additions & 46 deletions src/types/YText.js
Expand Up @@ -476,6 +476,56 @@ export const cleanupYTextFormatting = type => {
return res
}

/**
* This will be called by the transction once the event handlers are called to potentially cleanup
* formatting attributes.
*
* @param {Transaction} transaction
*/
export const cleanupYTextAfterTransaction = transaction => {
/**
* @type {Set<YText>}
*/
const needFullCleanup = new Set()
// check if another formatting item was inserted
const doc = transaction.doc
for (const [client, afterClock] of transaction.afterState.entries()) {
const clock = transaction.beforeState.get(client) || 0
if (afterClock === clock) {
continue
}
iterateStructs(transaction, /** @type {Array<Item|GC>} */ (doc.store.clients.get(client)), clock, afterClock, item => {
if (
!item.deleted && /** @type {Item} */ (item).content.constructor === ContentFormat && item.constructor !== GC
) {
needFullCleanup.add(/** @type {any} */ (item).parent)
}
})
}
// cleanup in a new transaction
transact(doc, (t) => {
iterateDeletedStructs(transaction, transaction.deleteSet, item => {
if (item instanceof GC || needFullCleanup.has(/** @type {YText} */ (item.parent))) {
return
}
const parent = /** @type {YText} */ (item.parent)
if (item.content.constructor === ContentFormat) {
needFullCleanup.add(parent)
} else {
// If no formatting attribute was inserted or deleted, we can make due with contextless
// formatting cleanups.
// Contextless: it is not necessary to compute currentAttributes for the affected position.
cleanupContextlessFormattingGap(t, item)
}
})
// If a formatting item was inserted, we simply clean the whole type.
// We need to compute currentAttributes for the current position anyway.
for (const yText of needFullCleanup) {
cleanupYTextFormatting(yText)
}
})
}

/**
* @param {Transaction} transaction
* @param {ItemTextListPosition} currPos
Expand Down Expand Up @@ -859,55 +909,10 @@ export class YText extends AbstractType {
_callObserver (transaction, parentSubs) {
super._callObserver(transaction, parentSubs)
const event = new YTextEvent(this, transaction, parentSubs)
const doc = transaction.doc
callTypeObservers(this, transaction, event)
// If a remote change happened, we try to cleanup potential formatting duplicates.
if (!transaction.local) {
// check if another formatting item was inserted
let foundFormattingItem = false
for (const [client, afterClock] of transaction.afterState.entries()) {
const clock = transaction.beforeState.get(client) || 0
if (afterClock === clock) {
continue
}
iterateStructs(transaction, /** @type {Array<Item|GC>} */ (doc.store.clients.get(client)), clock, afterClock, item => {
if (!item.deleted && /** @type {Item} */ (item).content.constructor === ContentFormat) {
foundFormattingItem = true
}
})
if (foundFormattingItem) {
break
}
}
if (!foundFormattingItem) {
iterateDeletedStructs(transaction, transaction.deleteSet, item => {
if (item instanceof GC || foundFormattingItem) {
return
}
if (item.parent === this && item.content.constructor === ContentFormat) {
foundFormattingItem = true
}
})
}
transact(doc, (t) => {
if (foundFormattingItem) {
// If a formatting item was inserted, we simply clean the whole type.
// We need to compute currentAttributes for the current position anyway.
cleanupYTextFormatting(this)
} else {
// If no formatting attribute was inserted, we can make due with contextless
// formatting cleanups.
// Contextless: it is not necessary to compute currentAttributes for the affected position.
iterateDeletedStructs(t, t.deleteSet, item => {
if (item instanceof GC) {
return
}
if (item.parent === this) {
cleanupContextlessFormattingGap(t, item)
}
})
}
})
transaction._needFormattingCleanup = true
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/Transaction.js
Expand Up @@ -11,6 +11,7 @@ import {
Item,
generateNewClientId,
createID,
cleanupYTextAfterTransaction,
UpdateEncoderV1, UpdateEncoderV2, GC, StructStore, AbstractType, AbstractStruct, YEvent, Doc // eslint-disable-line
} from '../internals.js'

Expand Down Expand Up @@ -114,6 +115,10 @@ export class Transaction {
* @type {Set<Doc>}
*/
this.subdocsLoaded = new Set()
/**
* @type {boolean}
*/
this._needFormattingCleanup = false
}
}

Expand Down Expand Up @@ -295,6 +300,9 @@ const cleanupTransactions = (transactionCleanups, i) => {
fs.push(() => doc.emit('afterTransaction', [transaction, doc]))
})
callAll(fs, [])
if (transaction._needFormattingCleanup) {
cleanupYTextAfterTransaction(transaction)
}
} finally {
// Replace deleted items with ItemDeleted / GC.
// This is where content is actually remove from the Yjs Doc.
Expand Down

0 comments on commit 9563612

Please sign in to comment.