attribution-manager.md
The Attribution feature extends Yjs types to provide rich metadata about content changes, including information about who created, deleted, or formatted content. This enables powerful collaborative editing features such as authorship tracking and change visualization. The information about who performed which changes can be handled by a separate CRDT (which is part of the attribution manager).
The attributionManager is the central component that tracks and manages
attribution data. It must be passed to methods that support attribution to
enable the feature.
Different implementations of AttributionManager are available for different use cases:
DiffingAttributionManager: Highlights the differences between two Yjs documentsSnapshotAttributionManager: Highlights the differences between two snapshotsAttributed content includes standard Yjs operations enhanced with attribution metadata:
// Standard content
[{ insert: 'hello world' }]
// Attributed content
[
{ insert: 'hello', attribution: { insert: ['kevin'] } },
{ insert: ' world', attribution: { insert: ['alice'] } }
]
Deleted content is represented in attributed results to maintain authorship information and proper position tracking:
// Shows deleted content with attribution
[
{ insert: 'hello ', attribution: { delete: ['kevin'] } },
{ insert: 'world' }
]
getDelta([attributionManager])Returns the delta representation of the YText content, optionally with attribution information.
Parameters:
attributionManager (optional): The attribution manager instanceReturns:
attributionManager is providedExamples:
const ytext = new Y.Text()
// Content is inserted during collaborative editing
// Attribution is handled automatically by the server
// Without attribution
const delta = ytext.getDelta()
// [{ insert: 'hello world' }]
// With attribution
const attributedDelta = ytext.getDelta(attributionManager)
// [
// { insert: 'hello', attribution: { insert: ['kevin'] } },
// { insert: ' world', attribution: { insert: ['alice'] } }
// ]
toDelta([attributionManager])Returns the content representation with optional attribution information.
Parameters:
toDelta (optional): The attribution manager instanceReturns:
attributionManager is providedtoDelta([attributionManager])Returns the array content with optional attribution information for each element.
Parameters:
attributionManager (optional): The attribution manager instanceReturns:
attributionManager is providedtoDelta([attributionManager])Returns the map content with optional attribution information for each key-value pair.
Parameters:
attributionManager (optional): The attribution manager instanceReturns:
attributionManager is providedWhen working with attributed content, position calculations must account for deleted content that appears in the attributed representation but not in the standard representation.
// Standard content (length: 5)
ytext.toString() // "world"
// Attributed content (includes deleted content)
ytext.getDelta(attributionManager)
// [
// { insert: 'hello ', attribution: { delete: ['kevin'] } }, // positions 0-5
// { insert: 'world' } // positions 6-10
// ]
// To insert after "world":
// - Standard position: 5 (after "world")
// - Attributed position: 11 (after "world" accounting for deleted "hello ")
Events in Yjs are enhanced to work with attributed content, automatically adjusting positions when attribution is considered.
When an attributionManager is used, event positions are automatically adjusted to account for deleted content.
Example:
// Initial content: "hello world"
// User deletes "hello " (positions 0-6)
// Current visible content: "world"
ytext.observe((event, transaction) => {
// User wants to insert "!" after "world"
// Standard event (without attribution)
const standardDelta = event.getDelta()
// Shows insertion at position 5 (after "world" in visible content)
// Attributed event (with attribution manager)
const attributedDelta = event.getDelta(attributionManager)
// Shows insertion at position 11 (accounting for deleted "hello ")
// [
// { insert: 'hello ', attribution: { delete: ['kevin'] } },
// { insert: 'world' },
// { insert: '!' } // inserted at attributed position 11
// ]
})
Display content with visual indicators of who created each part:
function renderWithAuthorship(ytext, attributionManager) {
const attributedDelta = ytext.getDelta(attributionManager)
return attributedDelta.map(op => {
const author = op.attribution?.insert?.[0] || 'unknown'
const isDeleted = op.attribution?.delete
return {
content: op.insert,
author,
isDeleted,
className: `author-${author} ${isDeleted ? 'deleted' : ''}`
}
})
}
Track who made specific changes to content:
function trackChanges(ytext, attributionManager) {
ytext.observe((event, transaction) => {
const changes = event.changes.getAttributedDelta?.(attributionManager) || event.changes.delta
changes.forEach(change => {
if (change.attribution) {
console.log(`Change by ${change.attribution.insert?.[0] || change.attribution.delete?.[0]}:`, change)
}
})
})
}
To add attribution support to existing Yjs applications:
The Attribution feature is fully backward compatible: