MCPcopy
hub / github.com/yjs/yjs / testAttributions

Function testAttributions

tests/delta.tests.js:159–195  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

157 * @param {t.TestCase} _tc
158 */
159export const testAttributions = _tc => {
160 const ydocV1 = new Y.Doc()
161 const ytypeV1 = ydocV1.get('txt')
162 ytypeV1.applyDelta(delta.create().insert('hello world'))
163 // create a new version with updated content
164 const ydoc = new Y.Doc()
165 Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(ydocV1))
166 const ytype = ydoc.get('txt')
167 // delete " world" and insert exclamation mark "!".
168 ytype.applyDelta(delta.create().retain(5).delete(6).insert('!'))
169 const am = Y.createAttributionManagerFromDiff(ydocV1, ydoc)
170 // get the attributed differences
171 const attributedContent = ytype.toDelta(am)
172 console.log('attributed content', attributedContent.toJSON())
173 t.assert(attributedContent.equals(delta.create().insert('hello').insert(' world', null, { delete: [] }).insert('!', null, { insert: [] })))
174 // for editor bindings, it is also necessary to observe changes and get the attributed changes
175 ytype.observe(event => {
176 const attributedChange = event.getDelta(am)
177 console.log('the attributed change', attributedChange.toJSON())
178 t.assert(attributedChange.done().equals(delta.create().retain(11).insert('!', null, { insert: [] })))
179 const unattributedChange = event.delta
180 console.log('the UNattributed change', unattributedChange.toJSON())
181 t.assert(unattributedChange.equals(delta.create().retain(5).insert('!')))
182 })
183 /**
184 * Content now has different representations.
185 * - The UNattributed representation renders the latest state, without history.
186 * - The attributed representation renders the differences.
187 *
188 * Attributed: 'hello<delete> world</delete><insert>!</insert>'
189 * UNattributed: 'world!'
190 */
191 // Apply a change to the attributed content
192 ytype.applyDelta(delta.create().retain(11).insert('!'), am)
193 // // Equivalent to applying a change to the UNattributed content:
194 // ytype.applyDelta(delta.create().retain(5).insert('!'))
195}

Callers

nothing calls this directly

Calls 8

getMethod · 0.95
applyDeltaMethod · 0.80
insertMethod · 0.80
toDeltaMethod · 0.80
observeMethod · 0.80
getDeltaMethod · 0.80
deleteMethod · 0.45
toJSONMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…