mirror of
https://github.com/yjs/yjs.git
synced 2025-12-16 11:47:46 +01:00
add an simple attributions example
This commit is contained in:
@@ -23,6 +23,7 @@ import {
|
||||
typeListGetContent
|
||||
} from '../internals.js'
|
||||
|
||||
import * as delta from '../utils/Delta.js'
|
||||
import * as error from 'lib0/error'
|
||||
import * as array from 'lib0/array'
|
||||
|
||||
@@ -397,11 +398,15 @@ export class YXmlFragment extends AbstractType {
|
||||
* @return {{ children: import('../utils/Delta.js').ArrayDelta<Array<import('./AbstractType.js').YXmlDeepContent>> }}
|
||||
*/
|
||||
getContentDeep (am) {
|
||||
const { children: origChildren } = this.getContent()
|
||||
const { children: origChildren } = this.getContent(am)
|
||||
/**
|
||||
* @type {import('../utils/Delta.js').ArrayDelta<Array<import('./AbstractType.js').YXmlDeepContent>>}
|
||||
*/
|
||||
const children = origChildren.map(d => /** @type {any} */ (d instanceof AbstractType ? d.getContentDeep(am) : d))
|
||||
const children = origChildren.map(d => /** @type {any} */ (
|
||||
d instanceof delta.InsertOp && d.insert instanceof Array
|
||||
? new delta.InsertOp(d.insert.map(e => e instanceof AbstractType ? e.getContentDeep(am) : e), d.attributes, d.attribution)
|
||||
: d
|
||||
))
|
||||
return { children }
|
||||
}
|
||||
|
||||
|
||||
@@ -342,3 +342,74 @@ export const testElementAttributedContentViaDiffer = _tc => {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {t.TestCase} _tc
|
||||
*/
|
||||
export const testAttributionManagerSimpleExample = _tc => {
|
||||
const ydoc = new Y.Doc()
|
||||
// create some initial content
|
||||
ydoc.getXmlFragment().insert(0, [new Y.XmlText('hello world')])
|
||||
const ydocFork = new Y.Doc()
|
||||
Y.applyUpdate(ydocFork, Y.encodeStateAsUpdate(ydoc))
|
||||
// modify the fork
|
||||
// append a span element
|
||||
ydocFork.getXmlFragment().insert(1, [new Y.XmlElement('span')])
|
||||
const ytext = /** @type {Y.XmlText} */ (ydocFork.getXmlFragment().get(0))
|
||||
// make "hello" italic
|
||||
ytext.format(0, 5, { italic: true })
|
||||
ytext.insert(11, '!')
|
||||
// highlight the changes
|
||||
console.log(JSON.stringify(ydocFork.getXmlFragment().getContentDeep(Y.createAttributionManagerFromDiff(ydoc, ydocFork)), null, 2))
|
||||
/* =>
|
||||
{
|
||||
"children": {
|
||||
"ops": [
|
||||
{
|
||||
"insert": [
|
||||
{
|
||||
"ops": [
|
||||
{
|
||||
"insert": "hello",
|
||||
"attributes": {
|
||||
"italic": true
|
||||
},
|
||||
"attribution": {
|
||||
"attributes": {
|
||||
"italic": [] -- the attribute "italic" was changed
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"insert": " world" -- "world" remains unchanged
|
||||
},
|
||||
{
|
||||
"insert": "!",
|
||||
"attribution": {
|
||||
"insert": [] -- "!" was inserted
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"insert": [
|
||||
{
|
||||
"nodeName": "span",
|
||||
"children": {
|
||||
"ops": []
|
||||
},
|
||||
"attributes": {}
|
||||
}
|
||||
],
|
||||
"attribution": {
|
||||
"insert": [] -- A <span/> tag was inserted
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user