@textbus/collaborate 2.0.0-alpha.37 → 2.0.0-alpha.40
Sign up to get free protection for your applications and to get access to all the features.
- package/bundles/collab/_api.d.ts +1 -1
- package/bundles/collab/_api.js +1 -1
- package/bundles/collab/_api.js.map +1 -1
- package/bundles/collab/collaborate-cursor.d.ts +37 -0
- package/bundles/collab/collaborate-cursor.js +254 -0
- package/bundles/collab/collaborate-cursor.js.map +1 -0
- package/bundles/collab/local-to-remote.d.ts +10 -2
- package/bundles/collab/local-to-remote.js +139 -157
- package/bundles/collab/local-to-remote.js.map +1 -1
- package/bundles/collab/remote-to-local.d.ts +10 -1
- package/bundles/collab/remote-to-local.js +136 -112
- package/bundles/collab/remote-to-local.js.map +1 -1
- package/bundles/collaborate.d.ts +29 -8
- package/bundles/collaborate.js +191 -33
- package/bundles/collaborate.js.map +1 -1
- package/bundles/public-api.js.map +1 -1
- package/package.json +5 -4
- package/src/collab/_api.ts +1 -1
- package/src/collab/collaborate-cursor.ts +233 -0
- package/src/collab/local-to-remote.ts +136 -159
- package/src/collab/remote-to-local.ts +127 -108
- package/src/collaborate.ts +78 -28
- package/tsconfig-build.json +2 -0
- package/bundles/collab/collaborate-history.d.ts +0 -22
- package/bundles/collab/collaborate-history.js +0 -53
- package/bundles/collab/collaborate-history.js.map +0 -1
- package/src/collab/collaborate-history.ts +0 -55
@@ -1,184 +1,161 @@
|
|
1
|
-
import { Action, Operation, SlotLiteral, ComponentLiteral,
|
2
|
-
import { Array as YArray, Map as YMap } from 'yjs'
|
1
|
+
import { Action, Operation, SlotLiteral, ComponentLiteral, makeError } from '@textbus/core'
|
2
|
+
import { Array as YArray, Map as YMap, Text as YText } from 'yjs'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
const
|
9
|
-
|
10
|
-
|
4
|
+
const collaborateErrorFn = makeError('Collaborate')
|
5
|
+
|
6
|
+
export class LocalToRemote {
|
7
|
+
transform(operation: Operation, root: YText) {
|
8
|
+
const path = [...operation.path]
|
9
|
+
path.shift()
|
10
|
+
if (path.length) {
|
11
|
+
const componentIndex = path.shift()!
|
12
|
+
const sharedComponent = this.getSharedComponentByIndex(root, componentIndex)
|
13
|
+
if (sharedComponent) {
|
14
|
+
this.applyComponentOperationToSharedComponent(path, operation.apply, sharedComponent)
|
15
|
+
}
|
16
|
+
return
|
17
|
+
}
|
18
|
+
this.mergeActionsToSharedSlot(root, operation.apply)
|
11
19
|
}
|
12
|
-
insertContent(root, operation.apply)
|
13
|
-
}
|
14
20
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
private applyComponentOperationToSharedComponent(path: number[], actions: Action[], componentYMap: YMap<any>) {
|
22
|
+
const sharedSlots = componentYMap.get('slots') as YArray<any>
|
23
|
+
if (path.length) {
|
24
|
+
const slotIndex = path.shift()!
|
25
|
+
const sharedSlot = sharedSlots.get(slotIndex)
|
26
|
+
this.applySlotOperationToSharedSlot(path, actions, sharedSlot)
|
27
|
+
return
|
28
|
+
}
|
29
|
+
let index: number
|
30
|
+
actions.forEach(action => {
|
31
|
+
switch (action.type) {
|
32
|
+
case 'retain':
|
33
|
+
index = action.offset
|
34
|
+
break
|
35
|
+
case 'insertSlot':
|
36
|
+
sharedSlots.insert(index, [this.makeSharedSlotBySlotLiteral(action.slot)])
|
37
|
+
index++
|
38
|
+
break
|
39
|
+
case 'apply':
|
40
|
+
componentYMap.set('state', action.value)
|
41
|
+
break
|
42
|
+
case 'delete':
|
43
|
+
sharedSlots.delete(index, action.count)
|
44
|
+
break
|
45
|
+
}
|
46
|
+
})
|
22
47
|
}
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
break
|
32
|
-
case 'apply':
|
33
|
-
componentYMap.set('state', action.value)
|
34
|
-
break
|
35
|
-
case 'delete':
|
36
|
-
sharedSlots.delete(index, action.count)
|
37
|
-
break
|
48
|
+
|
49
|
+
private applySlotOperationToSharedSlot(path: number[], actions: Action[], slotYMap: YMap<any>) {
|
50
|
+
if (path.length) {
|
51
|
+
const componentIndex = path.shift()!
|
52
|
+
const sharedContent = slotYMap.get('content') as YText
|
53
|
+
const sharedComponent = this.getSharedComponentByIndex(sharedContent, componentIndex)!
|
54
|
+
this.applyComponentOperationToSharedComponent(path, actions, sharedComponent)
|
55
|
+
return
|
38
56
|
}
|
39
|
-
|
40
|
-
}
|
57
|
+
const content = slotYMap.get('content') as YText
|
41
58
|
|
42
|
-
|
43
|
-
if (path.length) {
|
44
|
-
const componentIndex = path.shift()!
|
45
|
-
const sharedContent = slotYMap.get('content') as YArray<any>
|
46
|
-
const sharedComponent = sharedContent.get(componentIndex)
|
47
|
-
applyComponentOperationToSharedComponent(path, actions, sharedComponent)
|
48
|
-
return
|
59
|
+
this.mergeActionsToSharedSlot(content, actions, slotYMap)
|
49
60
|
}
|
50
|
-
const content = slotYMap.get('content') as YArray<any>
|
51
61
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
62
|
+
private mergeActionsToSharedSlot(content: YText, actions: Action[], slotYMap?: YMap<any>) {
|
63
|
+
let index: number
|
64
|
+
let length: number
|
65
|
+
|
66
|
+
actions.forEach(action => {
|
67
|
+
if (action.type === 'retain') {
|
57
68
|
if (action.formats) {
|
58
|
-
|
69
|
+
content.format(index, action.offset, action.formats)
|
70
|
+
} else {
|
71
|
+
index = action.offset
|
59
72
|
}
|
60
|
-
|
61
|
-
|
62
|
-
|
73
|
+
} else if (action.type === 'insert') {
|
74
|
+
const delta = content.toDelta()
|
75
|
+
const isEmpty = delta.length === 1 && delta[0].insert === '\n'
|
76
|
+
|
63
77
|
if (typeof action.content === 'string') {
|
64
|
-
|
65
|
-
content.insert(index, action.content
|
78
|
+
length = action.content.length
|
79
|
+
content.insert(index, action.content)
|
66
80
|
} else {
|
67
|
-
|
68
|
-
content.
|
81
|
+
length = 1
|
82
|
+
content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(action.content))
|
69
83
|
}
|
70
84
|
if (action.formats) {
|
71
|
-
|
85
|
+
content.format(index, length, action.formats)
|
72
86
|
}
|
73
|
-
|
74
|
-
|
75
|
-
if (content.length === 0) {
|
76
|
-
// 当内容为空时,slot 实例内容为 ['\n'],触发删除会导致长度溢出
|
77
|
-
return
|
87
|
+
if (isEmpty && index === 0) {
|
88
|
+
content.delete(content.length - 1, 1)
|
78
89
|
}
|
90
|
+
index += length
|
91
|
+
} else if (action.type === 'delete') {
|
92
|
+
const delta = content.toDelta()
|
79
93
|
content.delete(index, action.count)
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
}
|
94
|
+
if (content.length === 0) {
|
95
|
+
content.insert(0, '\n', delta[0]?.attributes)
|
96
|
+
}
|
97
|
+
} else if (action.type === 'apply') {
|
98
|
+
slotYMap?.set('state', action.value)
|
99
|
+
}
|
100
|
+
})
|
101
|
+
}
|
87
102
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
103
|
+
private makeSharedSlotBySlotLiteral(slotLiteral: SlotLiteral): YMap<any> {
|
104
|
+
const content = new YText()
|
105
|
+
let index = 0
|
106
|
+
slotLiteral.content.forEach(i => {
|
107
|
+
let size: number
|
108
|
+
if (typeof i === 'string') {
|
109
|
+
size = i.length
|
110
|
+
content.insert(index, i)
|
111
|
+
} else {
|
112
|
+
size = 1
|
113
|
+
content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(i))
|
114
|
+
}
|
115
|
+
index += size
|
116
|
+
})
|
117
|
+
const formats = slotLiteral.formats
|
118
|
+
Object.keys(formats).forEach(key => {
|
119
|
+
const formatRanges = formats[key]
|
120
|
+
formatRanges.forEach(formatRange => {
|
121
|
+
content.format(formatRange.startIndex, formatRange.endIndex - formatRange.startIndex, {
|
122
|
+
[key]: formatRange.value
|
123
|
+
})
|
124
|
+
})
|
125
|
+
})
|
101
126
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
endIndex,
|
109
|
-
value: formats[key]
|
110
|
-
}])
|
111
|
-
}
|
127
|
+
const sharedSlot = new YMap()
|
128
|
+
sharedSlot.set('content', content)
|
129
|
+
sharedSlot.set('schema', slotLiteral.schema)
|
130
|
+
sharedSlot.set('state', slotLiteral.state)
|
131
|
+
return sharedSlot
|
132
|
+
}
|
112
133
|
|
113
|
-
|
114
|
-
const
|
115
|
-
|
116
|
-
|
117
|
-
value: formats[key]
|
134
|
+
private makeSharedComponentByComponentLiteral(componentLiteral: ComponentLiteral): YMap<any> {
|
135
|
+
const slots = new YArray()
|
136
|
+
componentLiteral.slots.forEach(item => {
|
137
|
+
slots.push([this.makeSharedSlotBySlotLiteral(item)])
|
118
138
|
})
|
119
|
-
|
120
|
-
|
121
|
-
|
139
|
+
const sharedComponent = new YMap()
|
140
|
+
sharedComponent.set('name', componentLiteral.name)
|
141
|
+
sharedComponent.set('slots', slots)
|
142
|
+
sharedComponent.set('state', componentLiteral.state)
|
143
|
+
return sharedComponent
|
144
|
+
}
|
122
145
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
case 'insert':
|
131
|
-
content.insert(index!, [
|
132
|
-
typeof action.content === 'string' ?
|
133
|
-
action.content :
|
134
|
-
makeSharedComponentByComponentLiteral(action.content)
|
135
|
-
])
|
136
|
-
if (action.formats) {
|
137
|
-
// TODO 根节点样式
|
146
|
+
private getSharedComponentByIndex(host: YText, index: number): YMap<any> | null {
|
147
|
+
const delta = host.toDelta()
|
148
|
+
let i = 0
|
149
|
+
for (const action of delta) {
|
150
|
+
if (action.insert) {
|
151
|
+
if (i === index) {
|
152
|
+
return action.insert instanceof YMap ? action.insert : null
|
138
153
|
}
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
}
|
144
|
-
})
|
145
|
-
}
|
146
|
-
|
147
|
-
function makeSharedSlotBySlotLiteral(slotLiteral: SlotLiteral): YMap<any> {
|
148
|
-
const content = new YArray()
|
149
|
-
let index = 0
|
150
|
-
slotLiteral.content.forEach(i => {
|
151
|
-
let size: number
|
152
|
-
if (typeof i === 'string') {
|
153
|
-
size = i.length
|
154
|
-
content.insert(index, [i])
|
155
|
-
} else {
|
156
|
-
size = 1
|
157
|
-
content.insert(index, [makeSharedComponentByComponentLiteral(i)])
|
154
|
+
i += action.insert instanceof YMap ? 1 : action.insert.length
|
155
|
+
} else {
|
156
|
+
throw collaborateErrorFn('Unexpected delta action.')
|
157
|
+
}
|
158
158
|
}
|
159
|
-
|
160
|
-
}
|
161
|
-
const formats = new YMap()
|
162
|
-
Object.keys(slotLiteral.formats).forEach(key => {
|
163
|
-
formats.set(key, slotLiteral.formats[key])
|
164
|
-
})
|
165
|
-
|
166
|
-
const sharedSlot = new YMap()
|
167
|
-
sharedSlot.set('state', slotLiteral.state)
|
168
|
-
sharedSlot.set('content', content)
|
169
|
-
sharedSlot.set('schema', slotLiteral.schema)
|
170
|
-
sharedSlot.set('formats', formats)
|
171
|
-
return sharedSlot
|
172
|
-
}
|
173
|
-
|
174
|
-
function makeSharedComponentByComponentLiteral(componentLiteral: ComponentLiteral): YMap<any> {
|
175
|
-
const slots = new YArray()
|
176
|
-
componentLiteral.slots.forEach(item => {
|
177
|
-
slots.push([makeSharedSlotBySlotLiteral(item)])
|
178
|
-
})
|
179
|
-
const sharedComponent = new YMap()
|
180
|
-
sharedComponent.set('name', componentLiteral.name)
|
181
|
-
sharedComponent.set('slots', slots)
|
182
|
-
sharedComponent.set('state', componentLiteral.state)
|
183
|
-
return sharedComponent
|
159
|
+
return null
|
160
|
+
}
|
184
161
|
}
|
@@ -1,107 +1,112 @@
|
|
1
|
-
import { Map as YMap, YArrayEvent, YEvent, YMapEvent } from 'yjs'
|
2
|
-
import { ComponentInstance,
|
1
|
+
import { Map as YMap, YArrayEvent, YEvent, YMapEvent, Text as YText, YTextEvent, Array as YArray } from 'yjs'
|
2
|
+
import { ComponentInstance, ComponentLiteral, makeError, Registry, Slot, Translator } from '@textbus/core'
|
3
3
|
|
4
4
|
type YPath = [number, string][]
|
5
|
+
const collaborateErrorFn = makeError('Collaborate')
|
5
6
|
|
6
|
-
export
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
for (let i = 0; i < ev.path.length; i += 2) {
|
11
|
-
path.push(ev.path.slice(i, i + 2) as [number, string])
|
12
|
-
}
|
7
|
+
export class RemoteToLocal {
|
8
|
+
constructor(private translator: Translator,
|
9
|
+
private registry: Registry) {
|
10
|
+
}
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
const
|
17
|
-
applySharedComponentToComponent(ev, path, component, translator, registry)
|
18
|
-
return
|
19
|
-
}
|
12
|
+
transform(events: YEvent[], slot: Slot) {
|
13
|
+
events.forEach(ev => {
|
14
|
+
const path: YPath = []
|
20
15
|
|
21
|
-
|
22
|
-
|
23
|
-
}
|
24
|
-
|
25
|
-
function applySharedComponentToComponent(ev: YEvent, path: YPath, component: ComponentInstance, translator: Translator, registry: Registry,) {
|
26
|
-
if (path.length) {
|
27
|
-
const childPath = path.shift()!
|
28
|
-
const slot = component.slots.get(childPath[0])!
|
29
|
-
applySharedSlotToSlot(ev, path, slot, translator, registry, childPath[1] === 'formats')
|
30
|
-
return
|
31
|
-
}
|
32
|
-
if (ev instanceof YMapEvent) {
|
33
|
-
ev.keysChanged.forEach(key => {
|
34
|
-
if (key === 'state') {
|
35
|
-
const state = (ev.target as YMap<any>).get('state')
|
36
|
-
component.updateState(draft => {
|
37
|
-
Object.assign(draft, state)
|
38
|
-
})
|
16
|
+
for (let i = 0; i < ev.path.length; i += 2) {
|
17
|
+
path.push(ev.path.slice(i, i + 2) as [number, string])
|
39
18
|
}
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
} else if (action.insert) {
|
47
|
-
(action.insert as Array<any>).forEach(item => {
|
48
|
-
slots.insert(translator.createSlot(item.toJSON())!)
|
49
|
-
})
|
50
|
-
} else if (action.delete) {
|
51
|
-
slots.retain(slots.index)
|
52
|
-
slots.delete(action.delete)
|
19
|
+
|
20
|
+
if (path.length) {
|
21
|
+
const componentIndex = path.shift()![0] as number
|
22
|
+
const component = slot.getContentAtIndex(componentIndex) as ComponentInstance
|
23
|
+
this.applySharedComponentToComponent(ev, path, component)
|
24
|
+
return
|
53
25
|
}
|
26
|
+
|
27
|
+
this.applySharedSlotToSlot(ev, path, slot)
|
54
28
|
})
|
55
29
|
}
|
56
|
-
}
|
57
30
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
31
|
+
private applySharedComponentToComponent(ev: YEvent, path: YPath, component: ComponentInstance) {
|
32
|
+
if (path.length) {
|
33
|
+
const childPath = path.shift()!
|
34
|
+
const slot = component.slots.get(childPath[0])!
|
35
|
+
this.applySharedSlotToSlot(ev, path, slot)
|
36
|
+
return
|
37
|
+
}
|
38
|
+
if (ev instanceof YMapEvent) {
|
39
|
+
ev.keysChanged.forEach(key => {
|
40
|
+
if (key === 'state') {
|
41
|
+
const state = (ev.target as YMap<any>).get('state')
|
42
|
+
component.updateState(draft => {
|
43
|
+
Object.assign(draft, state)
|
44
|
+
})
|
45
|
+
}
|
46
|
+
})
|
47
|
+
} else if (ev instanceof YArrayEvent) {
|
48
|
+
const slots = component.slots
|
49
|
+
ev.delta.forEach(action => {
|
50
|
+
if (Reflect.has(action, 'retain')) {
|
51
|
+
slots.retain(action.retain!)
|
52
|
+
} else if (action.insert) {
|
53
|
+
(action.insert as Array<YMap<any>>).forEach(item => {
|
54
|
+
slots.insert(this.createSlotBySharedSlot(item))
|
55
|
+
})
|
56
|
+
} else if (action.delete) {
|
57
|
+
slots.retain(slots.index)
|
58
|
+
slots.delete(action.delete)
|
59
|
+
}
|
60
|
+
})
|
61
|
+
}
|
64
62
|
}
|
65
63
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
64
|
+
private applySharedSlotToSlot(ev: YEvent, path: YPath, slot: Slot) {
|
65
|
+
if (path.length) {
|
66
|
+
path.shift()
|
67
|
+
const delta = (ev.target.parent as YText).toDelta()
|
68
|
+
let componentIndex = 0
|
69
|
+
for (let i = 0; i < delta.length; i++) {
|
70
|
+
const action = delta[i]
|
71
|
+
if (action.insert === ev.target) {
|
72
|
+
break
|
73
|
+
}
|
74
|
+
componentIndex += typeof action.insert === 'string' ? action.insert.length : 1
|
75
|
+
}
|
76
|
+
const component = slot.getContentAtIndex(componentIndex) as ComponentInstance
|
77
|
+
this.applySharedComponentToComponent(ev, path, component)
|
78
|
+
return
|
79
|
+
}
|
80
|
+
|
81
|
+
if (ev instanceof YTextEvent) {
|
82
|
+
slot.retain(0)
|
83
|
+
ev.delta.forEach(action => {
|
84
|
+
if (Reflect.has(action, 'retain')) {
|
85
|
+
if (action.attributes) {
|
86
|
+
slot.retain(action.retain!, Object.keys(action.attributes).map(key => {
|
87
|
+
return [this.registry.getFormatter(key)!, action.attributes![key]]
|
88
|
+
}))
|
89
|
+
}
|
90
|
+
slot.retain(action.retain!)
|
91
|
+
} else if (action.insert) {
|
92
|
+
if (typeof action.insert === 'string') {
|
93
|
+
slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {
|
94
|
+
return [this.registry.getFormatter(key)!, action.attributes![key]]
|
95
|
+
}) : [])
|
74
96
|
} else {
|
75
|
-
|
97
|
+
const component = this.createComponentBySharedComponent(action.insert as YMap<any>)
|
98
|
+
slot.insert(component)
|
76
99
|
}
|
77
|
-
})
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
} else if (ev instanceof YMapEvent) {
|
84
|
-
if (isUpdateFormats) {
|
85
|
-
const json = ev.target.toJSON()
|
86
|
-
ev.keysChanged.forEach(key => {
|
87
|
-
const formats = json[key]
|
88
|
-
const formatter = registry.getFormatter(key)!
|
89
|
-
if (formatter.type !== FormatType.Block) {
|
90
|
-
slot.applyFormat(formatter, {
|
91
|
-
startIndex: 0,
|
92
|
-
endIndex: slot.length,
|
93
|
-
value: null
|
100
|
+
} else if (action.delete) {
|
101
|
+
slot.retain(slot.index)
|
102
|
+
slot.delete(action.delete)
|
103
|
+
} else if (action.attributes) {
|
104
|
+
slot.updateState(draft => {
|
105
|
+
Object.assign(draft, action.attributes)
|
94
106
|
})
|
95
107
|
}
|
96
|
-
formats.forEach(item => {
|
97
|
-
if (formatter.type === FormatType.Block) {
|
98
|
-
slot.applyFormat(formatter, item.value)
|
99
|
-
} else {
|
100
|
-
slot.applyFormat(formatter, item)
|
101
|
-
}
|
102
|
-
})
|
103
108
|
})
|
104
|
-
} else {
|
109
|
+
} else if (ev instanceof YMapEvent) {
|
105
110
|
ev.keysChanged.forEach(key => {
|
106
111
|
if (key === 'state') {
|
107
112
|
const state = (ev.target as YMap<any>).get('state')
|
@@ -112,29 +117,43 @@ function applySharedSlotToSlot(ev: YEvent, path: YPath, slot: Slot, translator:
|
|
112
117
|
})
|
113
118
|
}
|
114
119
|
}
|
115
|
-
}
|
116
120
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
121
|
+
private createComponentBySharedComponent(yMap: YMap<any>): ComponentInstance {
|
122
|
+
const slots = yMap.get('slots') as YArray<YMap<any>>
|
123
|
+
const componentLiteral: ComponentLiteral = {
|
124
|
+
state: yMap.get('state'),
|
125
|
+
name: yMap.get('name'),
|
126
|
+
slots: slots.map(sharedSlot => {
|
127
|
+
return this.createSlotBySharedSlot(sharedSlot).toJSON()
|
128
|
+
})
|
129
|
+
}
|
130
|
+
return this.translator.createComponent(componentLiteral)!
|
131
|
+
}
|
132
|
+
|
133
|
+
private createSlotBySharedSlot(sharedSlot: YMap<any>): Slot {
|
134
|
+
const content = sharedSlot.get('content') as YText
|
135
|
+
const delta = content.toDelta()
|
136
|
+
|
137
|
+
const slot = this.translator.createSlot({
|
138
|
+
schema: sharedSlot.get('schema'),
|
139
|
+
state: sharedSlot.get('state'),
|
140
|
+
formats: {},
|
141
|
+
content: []
|
142
|
+
})
|
143
|
+
|
144
|
+
for (const action of delta) {
|
122
145
|
if (action.insert) {
|
123
|
-
(action.insert
|
124
|
-
|
125
|
-
|
126
|
-
}
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
} else if (Reflect.has(action, 'retain')) {
|
133
|
-
slot.retain(action.retain!)
|
134
|
-
} else if (action.delete) {
|
135
|
-
slot.retain(slot.index)
|
136
|
-
slot.delete(action.delete)
|
146
|
+
if (typeof action.insert === 'string') {
|
147
|
+
slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {
|
148
|
+
return [this.registry.getFormatter(key)!, action.attributes![key]]
|
149
|
+
}) : [])
|
150
|
+
} else {
|
151
|
+
slot.insert(this.createComponentBySharedComponent(action.insert))
|
152
|
+
}
|
153
|
+
} else {
|
154
|
+
throw collaborateErrorFn('Unexpected delta action.')
|
137
155
|
}
|
138
|
-
}
|
156
|
+
}
|
157
|
+
return slot
|
139
158
|
}
|
140
159
|
}
|