mirror of
https://github.com/yjs/yjs.git
synced 2026-02-23 19:49:59 +01:00
getContent => toDelta
This commit is contained in:
@@ -55,7 +55,7 @@ const attributedDeletions = createIdMapFromIdSet(deleteSetDiff, [new Y.Attributi
|
||||
// implementations is the TwosetAttributionManager
|
||||
const attributionManager = new TwosetAttributionManager(attributedInsertions, attributedDeletions)
|
||||
// we render the attributed content with the attributionManager
|
||||
let attributedContent = ytext.getContent(attributionManager)
|
||||
let attributedContent = ytext.toDelta(attributionManager)
|
||||
console.log(JSON.stringify(attributedContent.toJSON().ops, null, 2))
|
||||
let expectedContent = delta.create().insert('Hell', { italic: true }, { attributes: { italic: ['Bob'] } }).insert('o ').insert('World', {}, { delete: ['Bob'] }).insert('attributions', {}, { insert: ['Bob'] }).insert('!')
|
||||
t.assert(attributedContent.equals(expectedContent))
|
||||
|
||||
@@ -79,19 +79,19 @@ const attributedDelta = ytext.getDelta(attributionManager)
|
||||
// ]
|
||||
```
|
||||
|
||||
#### `getContent([attributionManager])`
|
||||
#### `toDelta([attributionManager])`
|
||||
|
||||
Returns the content representation with optional attribution information.
|
||||
|
||||
**Parameters:**
|
||||
- `attributionManager` (optional): The attribution manager instance
|
||||
- `toDelta` (optional): The attribution manager instance
|
||||
|
||||
**Returns:**
|
||||
- Content representation with attribution metadata if `attributionManager` is provided
|
||||
|
||||
### YArray
|
||||
|
||||
#### `getContent([attributionManager])`
|
||||
#### `toDelta([attributionManager])`
|
||||
|
||||
Returns the array content with optional attribution information for each element.
|
||||
|
||||
@@ -103,7 +103,7 @@ Returns the array content with optional attribution information for each element
|
||||
|
||||
### YMap
|
||||
|
||||
#### `getContent([attributionManager])`
|
||||
#### `toDelta([attributionManager])`
|
||||
|
||||
Returns the map content with optional attribution information for each key-value pair.
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ export class YEvent {
|
||||
}
|
||||
modified = dchanged
|
||||
}
|
||||
return /** @type {any} */ (this.target.getContent(am, { itemsToRender, retainDeletes: true, deletedItems: this.transaction.deleteSet, deep: !!deep, modified }))
|
||||
return /** @type {any} */ (this.target.toDelta(am, { itemsToRender, retainDeletes: true, deletedItems: this.transaction.deleteSet, deep: !!deep, modified }))
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -43,7 +43,7 @@ export const diffDocsToDelta = (v1, v2, { am = createAttributionManagerFromDiff(
|
||||
const typeConf = changedTypes.get(type)
|
||||
if (typeConf) {
|
||||
// @ts-ignore
|
||||
const shareDelta = type.getContent(am, {
|
||||
const shareDelta = type.toDelta(am, {
|
||||
itemsToRender, retainDeletes: true, deletedItems: deletesOnly, modified: changedTypes, deep: true
|
||||
})
|
||||
d.modifyAttr(typename, shareDelta)
|
||||
|
||||
20
src/ytype.js
20
src/ytype.js
@@ -792,7 +792,7 @@ export class YType {
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
getContent (am = noAttributionsManager, opts = {}) {
|
||||
toDelta (am = noAttributionsManager, opts = {}) {
|
||||
const { itemsToRender = null, retainInserts = false, retainDeletes = false, deletedItems = null, modified = null, deep = false } = opts
|
||||
const renderAttrs = modified?.get(this) || null
|
||||
const renderChildren = !!(modified == null || modified.get(this)?.has(null))
|
||||
@@ -893,7 +893,7 @@ export class YType {
|
||||
if (c.deleted ? retainDeletes : retainInserts) {
|
||||
d.retain(c.content.getLength(), null, attribution ?? {})
|
||||
} else if (deep && c.content.constructor === ContentType) {
|
||||
d.insert([/** @type {any} */(c.content).type.getContent(am, optsAll)], null, attribution)
|
||||
d.insert([/** @type {any} */(c.content).type.toDelta(am, optsAll)], null, attribution)
|
||||
} else {
|
||||
d.insert(c.content.getContent(), null, attribution)
|
||||
}
|
||||
@@ -902,7 +902,7 @@ export class YType {
|
||||
} else if (retainContent) {
|
||||
if (c.content.constructor === ContentType && modified?.has(/** @type {ContentType} */ (c.content).type)) {
|
||||
// @todo use current transaction instead
|
||||
d.modify(/** @type {any} */ (c.content).type.getContent(am, opts))
|
||||
d.modify(/** @type {any} */ (c.content).type.toDelta(am, opts))
|
||||
} else {
|
||||
d.usedAttributes = changedAttributes
|
||||
usingChangedAttributes = true
|
||||
@@ -1016,8 +1016,8 @@ export class YType {
|
||||
* @param {AbstractAttributionManager} am
|
||||
* @return {delta.Delta<DConf>}
|
||||
*/
|
||||
getContentDeep (am = noAttributionsManager) {
|
||||
return /** @type {any} */ (this.getContent(am, { deep: true }))
|
||||
toDeltaDeep (am = noAttributionsManager) {
|
||||
return /** @type {any} */ (this.toDelta(am, { deep: true }))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1082,7 +1082,7 @@ export class YType {
|
||||
*/
|
||||
clone () {
|
||||
const cpy = this._copy()
|
||||
cpy.applyDelta(this.getContentDeep())
|
||||
cpy.applyDelta(this.toDeltaDeep())
|
||||
return cpy
|
||||
}
|
||||
|
||||
@@ -1264,7 +1264,7 @@ export class YType {
|
||||
* @return {Array<delta.DeltaConfGetChildren<DConf> | delta.DeltaConfGetText<DConf>>}
|
||||
*/
|
||||
toArray () {
|
||||
const dcontent = this.getContent()
|
||||
const dcontent = this.toDelta()
|
||||
/**
|
||||
* @type {Array<any>}
|
||||
*/
|
||||
@@ -1410,7 +1410,7 @@ export class YType {
|
||||
* @param {this} other
|
||||
*/
|
||||
[traits.EqualityTraitSymbol] (other) {
|
||||
return this.getContent().equals(other.getContent())
|
||||
return this.toDelta().equals(other.toDelta())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1868,7 +1868,7 @@ export const typeMapGetDelta = (d, parent, attrsToRender, am, deep, modified, de
|
||||
d.deleteAttr(key, attribution, c)
|
||||
}
|
||||
} else if (deep && c instanceof YType && modified?.has(c)) {
|
||||
d.modifyAttr(key, c.getContent(am, opts))
|
||||
d.modifyAttr(key, c.toDelta(am, opts))
|
||||
} else {
|
||||
// find prev content
|
||||
let prevContentItem = item
|
||||
@@ -1878,7 +1878,7 @@ export const typeMapGetDelta = (d, parent, attrsToRender, am, deep, modified, de
|
||||
}
|
||||
const prevValue = (prevContentItem !== item && itemsToRender?.hasId(prevContentItem.lastId)) ? array.last(prevContentItem.content.getContent()) : undefined
|
||||
if (deep && c instanceof YType) {
|
||||
c = /** @type {any} */(c).getContent(am, optsAll)
|
||||
c = /** @type {any} */(c).toDelta(am, optsAll)
|
||||
}
|
||||
d.setAttr(key, c, attribution, prevValue)
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ export const testAttributedEvents = _tc => {
|
||||
ytext.delete(6, 5)
|
||||
})
|
||||
const am = Y.createAttributionManagerFromDiff(v1, ydoc)
|
||||
const c1 = ytext.getContent(am)
|
||||
const c1 = ytext.toDelta(am)
|
||||
t.compare(c1, delta.create().insert('hello ').insert('world', null, { delete: [] }))
|
||||
let calledObserver = false
|
||||
ytext.observe(event => {
|
||||
@@ -63,7 +63,7 @@ export const testInsertionsMindingAttributedContent = _tc => {
|
||||
ytext.delete(6, 5)
|
||||
})
|
||||
const am = Y.createAttributionManagerFromDiff(v1, ydoc)
|
||||
const c1 = ytext.getContent(am)
|
||||
const c1 = ytext.toDelta(am)
|
||||
t.compare(c1, delta.create().insert('hello ').insert('world', null, { delete: [] }))
|
||||
ytext.applyDelta(delta.create().retain(11).insert('content'), am)
|
||||
t.assert(ytext.toString() === 'hello content')
|
||||
@@ -81,7 +81,7 @@ export const testInsertionsIntoAttributedContent = _tc => {
|
||||
ytext.insert(6, 'word')
|
||||
})
|
||||
const am = Y.createAttributionManagerFromDiff(v1, ydoc)
|
||||
const c1 = ytext.getContent(am)
|
||||
const c1 = ytext.toDelta(am)
|
||||
t.compare(c1, delta.create().insert('hello ').insert('word', null, { insert: [] }))
|
||||
ytext.applyDelta(delta.create().retain(9).insert('l'), am)
|
||||
t.assert(ytext.toString() === 'hello world')
|
||||
|
||||
@@ -65,5 +65,5 @@ export const testTextDecodingCompatibilityV1 = _tc => {
|
||||
}
|
||||
return d.done()
|
||||
}
|
||||
t.compare(doc.get('text').getContent(), quillDeltaToDelta(oldVal))
|
||||
t.compare(doc.get('text').toDelta(), quillDeltaToDelta(oldVal))
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ export const testBasics = _tc => {
|
||||
// the observed change should equal the applied change
|
||||
t.assert(observedDelta.equals(mergedChanges))
|
||||
// read the current state of the yjs types as a delta
|
||||
const currState = ytype.getContentDeep()
|
||||
const currState = ytype.toDeltaDeep()
|
||||
t.assert(currState.equals(mergedChanges)) // equal to the changes that we applied
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ export const testAttributions = _tc => {
|
||||
ytype.applyDelta(delta.create().retain(5).delete(6).insert('!'))
|
||||
const am = Y.createAttributionManagerFromDiff(ydocV1, ydoc)
|
||||
// get the attributed differences
|
||||
const attributedContent = ytype.getContent(am)
|
||||
const attributedContent = ytype.toDelta(am)
|
||||
console.log('attributed content', attributedContent.toJSON())
|
||||
t.assert(attributedContent.equals(delta.create().insert('hello').insert(' world', null, { delete: [] }).insert('!', null, { insert: [] })))
|
||||
// for editor bindings, it is also necessary to observe changes and get the attributed changes
|
||||
|
||||
@@ -12,7 +12,7 @@ export const testBasic = _tc => {
|
||||
const snapshot = Y.snapshot(ydoc)
|
||||
ydoc.get().insert(0, 'hello ')
|
||||
const restored = Y.createDocFromSnapshot(ydoc, snapshot)
|
||||
t.assert(restored.get().getContent().equals(delta.create().insert('world!')))
|
||||
t.assert(restored.get().toDelta().equals(delta.create().insert('world!')))
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -463,7 +463,7 @@ export const compare = users => {
|
||||
// @todo fix type error here
|
||||
// @ts-ignore
|
||||
const userXmlValues = users.map(u => /** @type {Y.XmlElement} */ (u.get('xml', Y.XmlElement)).toString())
|
||||
const userTextValues = users.map(u => u.get('text').getContentDeep())
|
||||
const userTextValues = users.map(u => u.get('text').toDeltaDeep())
|
||||
for (const u of users) {
|
||||
t.assert(u.store.pendingDs === null)
|
||||
t.assert(u.store.pendingStructs === null)
|
||||
|
||||
@@ -11,7 +11,7 @@ export const testInconsistentFormat = () => {
|
||||
const content = ydoc.get('text')
|
||||
content.format(0, 6, { bold: null })
|
||||
content.format(6, 4, { type: 'text' })
|
||||
t.compare(content.getContent(), delta.create().insert('Merge Test', { type: 'text' }).insert(' After', { type: 'text', italic: true }).done())
|
||||
t.compare(content.toDelta(), delta.create().insert('Merge Test', { type: 'text' }).insert(' After', { type: 'text', italic: true }).done())
|
||||
}
|
||||
const initializeYDoc = () => {
|
||||
const yDoc = new Y.Doc({ gc: false })
|
||||
@@ -85,11 +85,11 @@ export const testUndoText = tc => {
|
||||
t.assert(text0.toString() === 'bcxyz')
|
||||
// test marks
|
||||
text0.format(1, 3, { bold: true })
|
||||
t.compare(text0.getContent(), delta.create().insert('b').insert('cxy', { bold: true }).insert('z'))
|
||||
t.compare(text0.toDelta(), delta.create().insert('b').insert('cxy', { bold: true }).insert('z'))
|
||||
undoManager.undo()
|
||||
t.compare(text0.getContent(), delta.create().insert('bcxyz'))
|
||||
t.compare(text0.toDelta(), delta.create().insert('bcxyz'))
|
||||
undoManager.redo()
|
||||
t.compare(text0.getContent(), delta.create().insert('b').insert('cxy', { bold: true }).insert('z'))
|
||||
t.compare(text0.toDelta(), delta.create().insert('b').insert('cxy', { bold: true }).insert('z'))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -300,15 +300,15 @@ export const testUndoXml = tc => {
|
||||
textchild.format(3, 4, { bold: true })
|
||||
const v1 = delta.create().insert([delta.create('p').insert([delta.create().insert('con').insert('tent', { bold: true }).done()]).done()]).done()
|
||||
const v2 = delta.create().insert([delta.create('p').insert([delta.create().insert('content').done()]).done()]).done()
|
||||
t.compare(xml0.getContentDeep(), v1)
|
||||
t.compare(xml0.toDeltaDeep(), v1)
|
||||
undoManager.undo()
|
||||
t.compare(xml0.getContentDeep(), v2)
|
||||
t.compare(xml0.toDeltaDeep(), v2)
|
||||
undoManager.redo()
|
||||
t.compare(xml0.getContentDeep(), v1)
|
||||
t.compare(xml0.toDeltaDeep(), v1)
|
||||
xml0.delete(0, 1)
|
||||
t.compare(xml0.getContentDeep(), delta.create().done())
|
||||
t.compare(xml0.toDeltaDeep(), delta.create().done())
|
||||
undoManager.undo()
|
||||
t.compare(xml0.getContentDeep(), v1)
|
||||
t.compare(xml0.toDeltaDeep(), v1)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -475,19 +475,19 @@ export const testUndoNestedUndoIssue = _tc => {
|
||||
blocks3.push([blocks3block])
|
||||
text.setAttr('blocks', blocks3block)
|
||||
})
|
||||
t.compare(design.getContentDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Something Else' }) }) }))
|
||||
t.compare(design.toDeltaDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Something Else' }) }) }))
|
||||
undoManager.undo()
|
||||
t.compare(design.getContentDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Something' }) }) }))
|
||||
t.compare(design.toDeltaDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Something' }) }) }))
|
||||
undoManager.undo()
|
||||
t.compare(design.getContentDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Type Something' }) }) }))
|
||||
t.compare(design.toDeltaDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Type Something' }) }) }))
|
||||
undoManager.undo()
|
||||
t.compare(design.getContentDeep(), delta.from({}))
|
||||
t.compare(design.toDeltaDeep(), delta.from({}))
|
||||
undoManager.redo()
|
||||
t.compare(design.getContentDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Type Something' }) }) }))
|
||||
t.compare(design.toDeltaDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Type Something' }) }) }))
|
||||
undoManager.redo()
|
||||
t.compare(design.getContentDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Something' }) }) }))
|
||||
t.compare(design.toDeltaDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Something' }) }) }))
|
||||
undoManager.redo()
|
||||
t.compare(design.getContentDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Something Else' }) }) }))
|
||||
t.compare(design.toDeltaDeep(), delta.from({ text: delta.from({ blocks: delta.from({ text: 'Something Else' }) }) }))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -675,8 +675,8 @@ export const testUndoDeleteTextFormat = _tc => {
|
||||
.insert('Attack ships ')
|
||||
.insert('on fire', { bold: true })
|
||||
.insert(' off the shoulder of Orion.')
|
||||
t.compare(text.getContent(), expect)
|
||||
t.compare(text2.getContent(), expect)
|
||||
t.compare(text.toDelta(), expect)
|
||||
t.compare(text2.toDelta(), expect)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -128,7 +128,7 @@ export const testKeyEncoding = tc => {
|
||||
const update = Y.encodeStateAsUpdateV2(users[0])
|
||||
Y.applyUpdateV2(users[1], update)
|
||||
|
||||
const c = text1.getContent()
|
||||
const c = text1.toDelta()
|
||||
t.compare(
|
||||
c,
|
||||
delta.create()
|
||||
@@ -335,7 +335,7 @@ export const testObfuscateUpdates = _tc => {
|
||||
const omap = odoc.get('map')
|
||||
const oarray = odoc.get('array')
|
||||
// test ytext
|
||||
const d = /** @type {any} */ (otext.getContent().toJSON().children)
|
||||
const d = /** @type {any} */ (otext.toDelta().toJSON().children)
|
||||
t.assert(d.length === 2)
|
||||
t.assert(d[0].insert !== 'text' && d[0].insert.length === 4)
|
||||
t.assert(object.length(d[0].format) === 1)
|
||||
|
||||
@@ -162,9 +162,9 @@ export const testDeleteInsert = tc => {
|
||||
export const testInsertThreeElementsTryRegetProperty = tc => {
|
||||
const { testConnector, users, array0, array1 } = init(tc, { users: 2 })
|
||||
array0.insert(0, [1, true, false])
|
||||
t.compare(array0.getContent(), delta.create().insert([1, true, false]), 'content works')
|
||||
t.compare(array0.toDelta(), delta.create().insert([1, true, false]), 'content works')
|
||||
testConnector.flushAllMessages()
|
||||
t.compare(array1.getContent(), delta.create().insert([1, true, false]), 'comparison works after sync')
|
||||
t.compare(array1.toDelta(), delta.create().insert([1, true, false]), 'comparison works after sync')
|
||||
compare(users)
|
||||
}
|
||||
|
||||
@@ -479,7 +479,7 @@ export const testAttributedContent = _tc => {
|
||||
yarray.insert(1, [42])
|
||||
})
|
||||
const expectedContent = delta.create().insert([1], null, { delete: [] }).insert([2]).insert([42], null, { insert: [] })
|
||||
const attributedContent = yarray.getContent(attributionManager)
|
||||
const attributedContent = yarray.toDelta(attributionManager)
|
||||
console.log(attributedContent.toJSON())
|
||||
t.assert(attributedContent.equals(expectedContent))
|
||||
})
|
||||
|
||||
@@ -101,7 +101,7 @@ export const testMapHavingIterableAsConstructorParamTests = tc => {
|
||||
map0.setAttr('m2', m2)
|
||||
t.assert(m2.getAttr('object')?.x === 1)
|
||||
t.assert(m2.getAttr('boolean') === true)
|
||||
const m3 = new Y.Type().applyDelta(m1.getContent()).applyDelta(m2.getContent())
|
||||
const m3 = new Y.Type().applyDelta(m1.toDelta()).applyDelta(m2.toDelta())
|
||||
map0.setAttr('m3', m3)
|
||||
t.assert(m3.getAttr('number') === 1)
|
||||
t.assert(m3.getAttr('string') === 'hello')
|
||||
@@ -562,21 +562,21 @@ export const testAttributedContent = _tc => {
|
||||
t.group('initial value', () => {
|
||||
ymap.setAttr('test', 42)
|
||||
const expectedContent = { test: delta.$deltaMapChangeJson.expect({ type: 'insert', value: 42, attribution: { insert: [] } }) }
|
||||
const attributedContent = ymap.getContent(attributionManager)
|
||||
const attributedContent = ymap.toDelta(attributionManager)
|
||||
console.log(attributedContent.toJSON())
|
||||
t.compare(expectedContent, attributedContent.toJSON().attrs)
|
||||
})
|
||||
t.group('overwrite value', () => {
|
||||
ymap.setAttr('test', 'fourtytwo')
|
||||
const expectedContent = { test: delta.$deltaMapChangeJson.expect({ type: 'insert', value: 'fourtytwo', attribution: { insert: [] } }) }
|
||||
const attributedContent = ymap.getContent(attributionManager)
|
||||
const attributedContent = ymap.toDelta(attributionManager)
|
||||
console.log(attributedContent)
|
||||
t.compare(expectedContent, attributedContent.toJSON().attrs)
|
||||
})
|
||||
t.group('delete value', () => {
|
||||
ymap.deleteAttr('test')
|
||||
const expectedContent = { test: delta.$deltaMapChangeJson.expect({ type: 'delete', prevValue: 'fourtytwo', attribution: { delete: [] } }) }
|
||||
const attributedContent = ymap.getContent(attributionManager)
|
||||
const attributedContent = ymap.toDelta(attributionManager)
|
||||
console.log(attributedContent.toJSON())
|
||||
t.compare(expectedContent, attributedContent.toJSON().attrs)
|
||||
})
|
||||
|
||||
@@ -166,7 +166,7 @@ export const testDeltaBug = _tc => {
|
||||
}
|
||||
})
|
||||
ytext.applyDelta(addingList)
|
||||
const result = ytext.getContent()
|
||||
const result = ytext.toDelta()
|
||||
const expectedResult = delta.create()
|
||||
.insert('\n', { 'block-id': 'block-28eea923-9cbb-4b6f-a950-cf7fd82bc087' })
|
||||
.insert('\n\n\n', { 'table-col': { width: '150' } })
|
||||
@@ -1213,7 +1213,7 @@ export const testDeltaBug2 = _tc => {
|
||||
'block-id': 'block-9d6566a1-be55-4e20-999a-b990bc15e143'
|
||||
})
|
||||
ytext.applyDelta(changeEvent)
|
||||
const d = ytext.getContent()
|
||||
const d = ytext.toDelta()
|
||||
t.compare(d.toJSON().children?.[40], {
|
||||
type: 'insert',
|
||||
insert: '\n',
|
||||
@@ -1296,29 +1296,29 @@ export const testBasicFormat = tc => {
|
||||
})
|
||||
text0.insert(0, 'abc', { bold: true })
|
||||
t.assert(text0.toString() === 'abc', 'Basic insert with attributes works')
|
||||
t.compare(text0.getContent(), delta.create().insert('abc', { bold: true }).done())
|
||||
t.compare(text0.toDelta(), delta.create().insert('abc', { bold: true }).done())
|
||||
t.compare(eventDelta, delta.create().insert('abc', { bold: true }))
|
||||
text0.delete(0, 1)
|
||||
t.assert(text0.toString() === 'bc', 'Basic delete on formatted works (position 0)')
|
||||
t.compare(text0.getContent(), delta.create().insert('bc', { bold: true }).done())
|
||||
t.compare(text0.toDelta(), delta.create().insert('bc', { bold: true }).done())
|
||||
t.compare(eventDelta, delta.create().delete(1))
|
||||
text0.delete(1, 1)
|
||||
t.assert(text0.toString() === 'b', 'Basic delete works (position 1)')
|
||||
t.compare(text0.getContent(), delta.create().insert('b', { bold: true }).done())
|
||||
t.compare(text0.toDelta(), delta.create().insert('b', { bold: true }).done())
|
||||
t.compare(eventDelta, delta.create().retain(1).delete(1))
|
||||
text0.insert(0, 'z', { bold: true })
|
||||
t.assert(text0.toString() === 'zb')
|
||||
t.compare(text0.getContent(), delta.create().insert('zb', { bold: true }).done())
|
||||
t.compare(text0.toDelta(), delta.create().insert('zb', { bold: true }).done())
|
||||
t.compare(eventDelta, delta.create().insert('z', { bold: true }))
|
||||
// @ts-ignore
|
||||
t.assert(text0._start.right.right.right.content.str === 'b', 'Does not insert duplicate attribute marker')
|
||||
text0.insert(0, 'y')
|
||||
t.assert(text0.toString() === 'yzb')
|
||||
t.compare(text0.getContent(), delta.create().insert('y').insert('zb', { bold: true }).done())
|
||||
t.compare(text0.toDelta(), delta.create().insert('y').insert('zb', { bold: true }).done())
|
||||
t.compare(eventDelta, delta.create().insert('y'))
|
||||
text0.format(0, 2, { bold: null })
|
||||
t.assert(text0.toString() === 'yzb')
|
||||
t.compare(text0.getContent(), delta.create().insert('yz').insert('b', { bold: true }).done())
|
||||
t.compare(text0.toDelta(), delta.create().insert('yz').insert('b', { bold: true }).done())
|
||||
t.compare(eventDelta, delta.create().retain(1).retain(1, { bold: null }))
|
||||
compare(users)
|
||||
}
|
||||
@@ -1328,18 +1328,18 @@ export const testBasicFormat = tc => {
|
||||
*/
|
||||
export const testFalsyFormats = tc => {
|
||||
const { users, text0 } = init(tc, { users: 2 })
|
||||
let delta = text0.getContent().toJSON().children
|
||||
let delta = text0.toDelta().toJSON().children
|
||||
text0.observe(event => {
|
||||
delta = event.delta.toJSON().children
|
||||
})
|
||||
text0.insert(0, 'abcde', { falsy: false })
|
||||
t.compare(text0.getContent().toJSON().children, [{ type: 'insert', insert: 'abcde', format: { falsy: false } }])
|
||||
t.compare(text0.toDelta().toJSON().children, [{ type: 'insert', insert: 'abcde', format: { falsy: false } }])
|
||||
t.compare(delta, [{ type: 'insert', insert: 'abcde', format: { falsy: false } }])
|
||||
text0.format(1, 3, { falsy: true })
|
||||
t.compare(text0.getContent().toJSON().children, [{ type: 'insert', insert: 'a', format: { falsy: false } }, { type: 'insert', insert: 'bcd', format: { falsy: true } }, { type: 'insert', insert: 'e', format: { falsy: false } }])
|
||||
t.compare(text0.toDelta().toJSON().children, [{ type: 'insert', insert: 'a', format: { falsy: false } }, { type: 'insert', insert: 'bcd', format: { falsy: true } }, { type: 'insert', insert: 'e', format: { falsy: false } }])
|
||||
t.compare(delta, [{ type: 'retain', retain: 1 }, { type: 'retain', retain: 3, format: { falsy: true } }])
|
||||
text0.format(2, 1, { falsy: false })
|
||||
t.compare(text0.getContent().toJSON().children, [{ type: 'insert', insert: 'a', format: { falsy: false } }, { type: 'insert', insert: 'b', format: { falsy: true } }, { type: 'insert', insert: 'c', format: { falsy: false } }, { type: 'insert', insert: 'd', format: { falsy: true } }, { type: 'insert', insert: 'e', format: { falsy: false } }])
|
||||
t.compare(text0.toDelta().toJSON().children, [{ type: 'insert', insert: 'a', format: { falsy: false } }, { type: 'insert', insert: 'b', format: { falsy: true } }, { type: 'insert', insert: 'c', format: { falsy: false } }, { type: 'insert', insert: 'd', format: { falsy: true } }, { type: 'insert', insert: 'e', format: { falsy: false } }])
|
||||
t.compare(delta, [{ type: 'retain', retain: 2 }, { type: 'retain', retain: 1, format: { falsy: false } }])
|
||||
compare(users)
|
||||
}
|
||||
@@ -1359,7 +1359,7 @@ export const testMultilineFormat = _tc => {
|
||||
.retain(10, { bold: true })
|
||||
testText.applyDelta(tt)
|
||||
t.compare(
|
||||
testText.getContent(),
|
||||
testText.toDelta(),
|
||||
delta.create()
|
||||
.insert('Test', { bold: true })
|
||||
.insert('\n')
|
||||
@@ -1383,7 +1383,7 @@ export const testNotMergeEmptyLinesFormat = _tc => {
|
||||
.insert('\n', { title: true })
|
||||
)
|
||||
t.compare(
|
||||
testText.getContent(),
|
||||
testText.toDelta(),
|
||||
delta.create()
|
||||
.insert('Text')
|
||||
.insert('\n', { title: true })
|
||||
@@ -1409,7 +1409,7 @@ export const testPreserveAttributesThroughDelete = _tc => {
|
||||
.delete(1)
|
||||
.retain(1, { title: true })
|
||||
)
|
||||
t.compare(testText.getContent(),
|
||||
t.compare(testText.toDelta(),
|
||||
delta.create()
|
||||
.insert('Text')
|
||||
.insert('\n', { title: true })
|
||||
@@ -1423,7 +1423,7 @@ export const testPreserveAttributesThroughDelete = _tc => {
|
||||
export const testGetDeltaWithEmbeds = tc => {
|
||||
const { text0 } = init(tc, { users: 1 })
|
||||
text0.applyDelta(delta.create().insert([{ linebreak: 's' }]))
|
||||
t.compare(text0.getContent(),
|
||||
t.compare(text0.toDelta(),
|
||||
delta.create()
|
||||
.insert([{ linebreak: 's' }])
|
||||
.done()
|
||||
@@ -1438,7 +1438,7 @@ export const testTypesAsEmbed = tc => {
|
||||
text0.applyDelta(delta.create()
|
||||
.insert([Y.Type.from(delta.create().setAttr('key', 'val'))])
|
||||
)
|
||||
t.compare(/** @type {any} */ (text0).getContentDeep().toJSON().children, [{ type: 'insert', insert: [{ type: 'delta', attrs: { key: { type: 'insert', value: 'val' } } }] }])
|
||||
t.compare(/** @type {any} */ (text0).toDeltaDeep().toJSON().children, [{ type: 'insert', insert: [{ type: 'delta', attrs: { key: { type: 'insert', value: 'val' } } }] }])
|
||||
let firedEvent = false
|
||||
text1.observe(event => {
|
||||
const d = event.deltaDeep
|
||||
@@ -1447,7 +1447,7 @@ export const testTypesAsEmbed = tc => {
|
||||
firedEvent = true
|
||||
})
|
||||
testConnector.flushAllMessages()
|
||||
const dd = text1.getContentDeep().toJSON().children
|
||||
const dd = text1.toDeltaDeep().toJSON().children
|
||||
t.assert(dd?.length === 1)
|
||||
t.compare(/** @type {any} */ (dd?.[0]).insert[0], { type: 'delta', attrs: { key: { type: 'insert', value: 'val' } } })
|
||||
t.assert(firedEvent, 'fired the event observer containing a Type-Embed')
|
||||
@@ -1473,11 +1473,11 @@ export const testSnapshot = tc => {
|
||||
.insert('x')
|
||||
.delete(1)
|
||||
)
|
||||
const state1 = text0.getContent(createAttributionManagerFromSnapshots(snapshot1))
|
||||
const state1 = text0.toDelta(createAttributionManagerFromSnapshots(snapshot1))
|
||||
t.compare(state1, delta.create().insert('abcd').done())
|
||||
const state2 = text0.getContent(createAttributionManagerFromSnapshots(snapshot2))
|
||||
const state2 = text0.toDelta(createAttributionManagerFromSnapshots(snapshot2))
|
||||
t.compare(state2, delta.create().insert('axcd').done())
|
||||
const state2Diff = text0.getContent(createAttributionManagerFromSnapshots(snapshot1, snapshot2))
|
||||
const state2Diff = text0.toDelta(createAttributionManagerFromSnapshots(snapshot1, snapshot2))
|
||||
t.compare(
|
||||
state2Diff,
|
||||
delta.create()
|
||||
@@ -1504,7 +1504,7 @@ export const testSnapshotDeleteAfter = tc => {
|
||||
.retain(4)
|
||||
.insert('e')
|
||||
)
|
||||
const state1 = text0.getContent(createAttributionManagerFromSnapshots(snapshot1))
|
||||
const state1 = text0.toDelta(createAttributionManagerFromSnapshots(snapshot1))
|
||||
t.compare(state1, delta.create().insert('abcd'))
|
||||
}
|
||||
|
||||
@@ -1514,7 +1514,7 @@ export const testSnapshotDeleteAfter = tc => {
|
||||
export const testDeltaCompare = tc => {
|
||||
const { text0 } = init(tc, { users: 1 })
|
||||
text0.insert(0, 'abc', { bold: true })
|
||||
t.compare(text0.getContent(), delta.create().insert('abc', { bold: true }).done())
|
||||
t.compare(text0.toDelta(), delta.create().insert('abc', { bold: true }).done())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1524,7 +1524,7 @@ export const testToDeltaEmbedAttributes = tc => {
|
||||
const { text0 } = init(tc, { users: 1 })
|
||||
text0.insert(0, 'ab', { bold: true })
|
||||
text0.insert(1, [{ image: 'imageSrc.png' }], { width: 100 })
|
||||
const delta0 = text0.getContent()
|
||||
const delta0 = text0.toDelta()
|
||||
t.compare(
|
||||
delta0,
|
||||
delta.create()
|
||||
@@ -1542,7 +1542,7 @@ export const testToDeltaEmbedNoAttributes = tc => {
|
||||
const { text0 } = init(tc, { users: 1 })
|
||||
text0.insert(0, 'ab', { bold: true })
|
||||
text0.insert(1, [{ image: 'imageSrc.png' }])
|
||||
const delta0 = text0.getContent()
|
||||
const delta0 = text0.toDelta()
|
||||
t.compare(
|
||||
delta0,
|
||||
delta.create()
|
||||
@@ -1854,9 +1854,9 @@ export const testFormattingBug = async _tc => {
|
||||
.insert('\n', { url: 'http://docs.yjs.dev' })
|
||||
.insert('\n', { url: 'http://example.com' })
|
||||
.done()
|
||||
t.compare(text1.getContent(), expectedResult)
|
||||
t.compare(text1.getContent().toJSON(), text2.getContent().toJSON())
|
||||
console.log(text1.getContent().toJSON())
|
||||
t.compare(text1.toDelta(), expectedResult)
|
||||
t.compare(text1.toDelta().toJSON(), text2.toDelta().toJSON())
|
||||
console.log(text1.toDelta().toJSON())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1884,8 +1884,8 @@ export const testDeleteFormatting = _tc => {
|
||||
.insert('on ', { bold: true })
|
||||
.insert('fire off the shoulder of Orion.')
|
||||
.done()
|
||||
t.compare(text.getContent(), expected)
|
||||
t.compare(text2.getContent(), expected)
|
||||
t.compare(text.toDelta(), expected)
|
||||
t.compare(text2.toDelta(), expected)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1904,14 +1904,14 @@ export const testAttributedContent = _tc => {
|
||||
t.group('insert / delete / format', () => {
|
||||
ytext.applyDelta(delta.create().retain(4, { italic: true }).retain(2).delete(5).insert('attributions'))
|
||||
const expectedContent = delta.create().insert('Hell', { italic: true }, { format: { italic: [] } }).insert('o ').insert('World', {}, { delete: [] }).insert('attributions', {}, { insert: [] }).insert('!')
|
||||
const attributedContent = ytext.getContent(attributionManager)
|
||||
const attributedContent = ytext.toDelta(attributionManager)
|
||||
console.log(attributedContent.toJSON())
|
||||
t.assert(attributedContent.equals(expectedContent))
|
||||
})
|
||||
t.group('unformat', () => {
|
||||
ytext.applyDelta(delta.create().retain(5, { italic: null }))
|
||||
const expectedContent = delta.create().insert('Hell', null, { format: { italic: [] } }).insert('o attributions!')
|
||||
const attributedContent = ytext.getContent(attributionManager)
|
||||
const attributedContent = ytext.toDelta(attributionManager)
|
||||
console.log(attributedContent.toJSON())
|
||||
t.assert(attributedContent.equals(expectedContent))
|
||||
})
|
||||
@@ -1942,7 +1942,7 @@ export const testAttributedDiffing = _tc => {
|
||||
// implementations is the TwosetAttributionManager
|
||||
const attributionManager = new TwosetAttributionManager(attributedInsertions, attributedDeletions)
|
||||
// we render the attributed content with the attributionManager
|
||||
const attributedContent = ytext.getContent(attributionManager)
|
||||
const attributedContent = ytext.toDelta(attributionManager)
|
||||
console.log(JSON.stringify(attributedContent.toJSON(), null, 2))
|
||||
const expectedContent = delta.create().insert('Hell', { italic: true }, { format: { italic: ['Bob'] } }).insert('o ').insert('World', {}, { delete: ['Bob'] }).insert('attributions', {}, { insert: ['Bob'] }).insert('!')
|
||||
t.assert(attributedContent.equals(expectedContent))
|
||||
@@ -2165,8 +2165,8 @@ const qChanges = [
|
||||
const checkResult = result => {
|
||||
for (let i = 1; i < result.testObjects.length; i++) {
|
||||
t.info('length of text = ' + result.users[i - 1].get('text').length)
|
||||
const p1 = result.users[i - 1].get('text').getContentDeep().children
|
||||
const p2 = result.users[i].get('text').getContentDeep().children
|
||||
const p1 = result.users[i - 1].get('text').toDeltaDeep().children
|
||||
const p2 = result.users[i].get('text').toDeltaDeep().children
|
||||
t.compare(p1, p2)
|
||||
}
|
||||
// Uncomment this to find formatting-cleanup issues
|
||||
@@ -2177,7 +2177,7 @@ const checkResult = result => {
|
||||
|
||||
/**
|
||||
* Generally, the content reader with attributed content works better if there is more deleted
|
||||
* content. Increasing the number of deletions improves performance of getContent (even to the point
|
||||
* content. Increasing the number of deletions improves performance of toDelta (even to the point
|
||||
* that is beats toString(), but that might be because of internal js optimization steps).
|
||||
*
|
||||
* @param {t.TestCase} tc
|
||||
@@ -2207,9 +2207,9 @@ export const testAttributionManagerDefaultPerformance = tc => {
|
||||
ytext.toString()
|
||||
}
|
||||
})
|
||||
t.measureTime(`getContent(attributionManager) performance <executed ${M} times>`, () => {
|
||||
t.measureTime(`toDelta(attributionManager) performance <executed ${M} times>`, () => {
|
||||
for (let i = 0; i < M; i++) {
|
||||
ytext.getContent()
|
||||
ytext.toDelta()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ export const testFormattingBug = _tc => {
|
||||
.insert('B', { em: {} })
|
||||
.insert('C', { em: {}, strong: {} })
|
||||
yxml.applyDelta(q)
|
||||
t.compare(yxml.getContent(), q)
|
||||
t.compare(yxml.toDelta(), q)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,10 +150,10 @@ export const testFragmentAttributedContent = _tc => {
|
||||
yfragment.insert(1, [elem3])
|
||||
})
|
||||
const expectedContent = delta.create().insert([elem1], null, { delete: [] }).insert([elem2]).insert([elem3], null, { insert: [] })
|
||||
const attributedContent = yfragment.getContent(attributionManager)
|
||||
const attributedContent = yfragment.toDelta(attributionManager)
|
||||
console.log(attributedContent.toJSON())
|
||||
t.assert(attributedContent.equals(expectedContent))
|
||||
t.compare(elem1.getContent(attributionManager).toJSON(), delta.create().insert('hello', null, { delete: [] }).toJSON())
|
||||
t.compare(elem1.toDelta(attributionManager).toJSON(), delta.create().insert('hello', null, { delete: [] }).toJSON())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -183,7 +183,7 @@ export const testElementAttributedContent = _tc => {
|
||||
.insert([elem2])
|
||||
.insert([delta.create().insert('world', null, { insert: [] })], null, { insert: [] })
|
||||
.setAttr('key', '42', { insert: [] })
|
||||
const attributedContent = yelement.getContentDeep(attributionManager)
|
||||
const attributedContent = yelement.toDeltaDeep(attributionManager)
|
||||
console.log('retrieved content', attributedContent.toJSON())
|
||||
t.assert(attributedContent.equals(expectedContent))
|
||||
t.compare(attributedContent.toJSON().attrs, { key: { type: 'insert', value: '42', attribution: { insert: [] } } })
|
||||
@@ -207,8 +207,8 @@ export const testElementAttributedContentViaDiffer = _tc => {
|
||||
yelement.setAttr('key', '42')
|
||||
})
|
||||
const attributionManager = Y.createAttributionManagerFromDiff(ydocV1, ydoc)
|
||||
const expectedContent = delta.create().insert([delta.create().insert('hello')], null, { delete: [] }).insert([elem2.getContentDeep()]).insert([delta.create().insert('world', null, { insert: [] })], null, { insert: [] }).setAttr('key', '42', { insert: [] })
|
||||
const attributedContent = yelement.getContentDeep(attributionManager)
|
||||
const expectedContent = delta.create().insert([delta.create().insert('hello')], null, { delete: [] }).insert([elem2.toDeltaDeep()]).insert([delta.create().insert('world', null, { insert: [] })], null, { insert: [] }).setAttr('key', '42', { insert: [] })
|
||||
const attributedContent = yelement.toDeltaDeep(attributionManager)
|
||||
console.log('children', attributedContent.toJSON().children)
|
||||
console.log('attributes', attributedContent.toJSON().attrs)
|
||||
t.compare(attributedContent.toJSON(), expectedContent.toJSON())
|
||||
@@ -226,7 +226,7 @@ export const testElementAttributedContentViaDiffer = _tc => {
|
||||
delta.create().insert('world', null, { insert: [] })
|
||||
], null, { insert: [] })
|
||||
.setAttr('key', '42', { insert: [] })
|
||||
const attributedContent = yelement.getContentDeep(attributionManager)
|
||||
const attributedContent = yelement.toDeltaDeep(attributionManager)
|
||||
console.log('children', JSON.stringify(attributedContent.toJSON().children, null, 2))
|
||||
console.log('cs expec', JSON.stringify(expectedContent.toJSON(), null, 2))
|
||||
console.log('attributes', attributedContent.toJSON().attrs)
|
||||
@@ -250,7 +250,7 @@ export const testElementAttributedContentViaDiffer = _tc => {
|
||||
delta.create().insert('bigworld', null, { insert: [] })
|
||||
], null, { insert: [] })
|
||||
.setAttr('key', '42', { insert: [] })
|
||||
const attributedContent = yelement.getContentDeep(attributionManager)
|
||||
const attributedContent = yelement.toDeltaDeep(attributionManager)
|
||||
console.log('children', JSON.stringify(attributedContent.toJSON().children, null, 2))
|
||||
console.log('cs expec', JSON.stringify(expectedContent.toJSON(), null, 2))
|
||||
console.log('attributes', attributedContent.toJSON().attrs)
|
||||
@@ -264,7 +264,7 @@ export const testElementAttributedContentViaDiffer = _tc => {
|
||||
const expectedContent = delta.create().insert([delta.create('span')]).insert([
|
||||
delta.create().insert('bigworld')
|
||||
]).setAttr('key', '42')
|
||||
const attributedContent = yelement.getContentDeep(attributionManager)
|
||||
const attributedContent = yelement.toDeltaDeep(attributionManager)
|
||||
console.log('children', JSON.stringify(attributedContent.toJSON().children, null, 2))
|
||||
console.log('cs expec', JSON.stringify(expectedContent.toJSON(), null, 2))
|
||||
console.log('attributes', attributedContent.toJSON().attrs)
|
||||
@@ -295,7 +295,7 @@ export const testAttributionManagerSimpleExample = _tc => {
|
||||
ytext.delete(11, 8)
|
||||
ytext.insert(11, '!')
|
||||
// highlight the changes
|
||||
console.log(JSON.stringify(ydocFork.get().getContentDeep(Y.createAttributionManagerFromDiff(ydoc, ydocFork)), null, 2))
|
||||
console.log(JSON.stringify(ydocFork.get().toDeltaDeep(Y.createAttributionManagerFromDiff(ydoc, ydocFork)), null, 2))
|
||||
/* =>
|
||||
{
|
||||
"children": {
|
||||
|
||||
Reference in New Issue
Block a user