@textbus/collaborate 2.0.0-alpha.75 → 2.0.0-alpha.79

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,201 +0,0 @@
1
- import { YArrayEvent, YMapEvent, YTextEvent, } from 'yjs';
2
- import { makeError } from '@textbus/core';
3
- const collaborateErrorFn = makeError('Collaborate');
4
- export class RemoteToLocal {
5
- constructor(yDoc, translator, selection, registry) {
6
- Object.defineProperty(this, "yDoc", {
7
- enumerable: true,
8
- configurable: true,
9
- writable: true,
10
- value: yDoc
11
- });
12
- Object.defineProperty(this, "translator", {
13
- enumerable: true,
14
- configurable: true,
15
- writable: true,
16
- value: translator
17
- });
18
- Object.defineProperty(this, "selection", {
19
- enumerable: true,
20
- configurable: true,
21
- writable: true,
22
- value: selection
23
- });
24
- Object.defineProperty(this, "registry", {
25
- enumerable: true,
26
- configurable: true,
27
- writable: true,
28
- value: registry
29
- });
30
- }
31
- transform(events, rootComponent) {
32
- events.forEach(ev => {
33
- const path = [];
34
- for (let i = 0; i < ev.path.length; i += 2) {
35
- path.push(ev.path.slice(i, i + 2));
36
- }
37
- const slot = rootComponent.slots.get(0);
38
- if (!slot) {
39
- throw collaborateErrorFn('cannot find child slot in root component!');
40
- }
41
- if (path.length) {
42
- const componentIndex = path.shift()[0];
43
- const component = slot.getContentAtIndex(componentIndex);
44
- this.applySharedComponentToComponent(ev, path, component);
45
- return;
46
- }
47
- this.applySharedSlotToSlot(ev, path, slot);
48
- });
49
- }
50
- applySharedComponentToComponent(ev, path, component) {
51
- if (path.length) {
52
- const childPath = path.shift();
53
- const slot = component.slots.get(childPath[0]);
54
- this.applySharedSlotToSlot(ev, path, slot);
55
- return;
56
- }
57
- if (ev instanceof YMapEvent) {
58
- ev.keysChanged.forEach(key => {
59
- if (key === 'state') {
60
- const state = ev.target.get('state');
61
- component.updateState(draft => {
62
- Object.assign(draft, state);
63
- });
64
- }
65
- });
66
- }
67
- else if (ev instanceof YArrayEvent) {
68
- const slots = component.slots;
69
- ev.delta.forEach(action => {
70
- if (Reflect.has(action, 'retain')) {
71
- slots.retain(action.retain);
72
- }
73
- else if (action.insert) {
74
- action.insert.forEach(item => {
75
- slots.insert(this.createSlotBySharedSlot(item));
76
- });
77
- }
78
- else if (action.delete) {
79
- slots.retain(slots.index);
80
- slots.delete(action.delete);
81
- }
82
- });
83
- }
84
- }
85
- applySharedSlotToSlot(ev, path, slot) {
86
- if (path.length) {
87
- const childPath = path.shift();
88
- if (childPath.length === 1) {
89
- const delta = ev.target.parent.toDelta();
90
- let componentIndex = 0;
91
- for (let i = 0; i < delta.length; i++) {
92
- const action = delta[i];
93
- if (action.insert === ev.target) {
94
- break;
95
- }
96
- componentIndex += typeof action.insert === 'string' ? action.insert.length : 1;
97
- }
98
- const component = slot.getContentAtIndex(componentIndex);
99
- this.applySharedComponentToComponent(ev, path, component);
100
- }
101
- else {
102
- const component = slot.getContentAtIndex(childPath[0]);
103
- this.applySharedComponentToComponent(ev, path, component);
104
- }
105
- return;
106
- }
107
- if (ev instanceof YTextEvent) {
108
- slot.retain(0);
109
- let offset = 0;
110
- ev.delta.forEach(action => {
111
- if (Reflect.has(action, 'retain')) {
112
- if (action.attributes) {
113
- slot.retain(action.retain, Object.keys(action.attributes).map(key => {
114
- return [this.registry.getFormatter(key), action.attributes[key]];
115
- }));
116
- }
117
- slot.retain(action.retain);
118
- }
119
- else if (action.insert) {
120
- if (typeof action.insert === 'string') {
121
- slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {
122
- return [this.registry.getFormatter(key), action.attributes[key]];
123
- }) : []);
124
- offset += action.insert.length;
125
- }
126
- else {
127
- const component = this.createComponentBySharedComponent(action.insert);
128
- slot.insert(component);
129
- offset += 1;
130
- }
131
- }
132
- else if (action.delete) {
133
- slot.retain(slot.index);
134
- slot.delete(action.delete);
135
- offset -= action.delete;
136
- }
137
- else if (action.attributes) {
138
- slot.updateState(draft => {
139
- Object.assign(draft, action.attributes);
140
- });
141
- }
142
- });
143
- if (this.selection.isSelected) {
144
- if (slot === this.selection.startSlot) {
145
- this.selection.setStart(slot, this.selection.startOffset + offset);
146
- }
147
- if (slot === this.selection.endSlot) {
148
- this.selection.setEnd(slot, this.selection.endOffset + offset);
149
- }
150
- }
151
- }
152
- else if (ev instanceof YMapEvent) {
153
- ev.keysChanged.forEach(key => {
154
- if (key === 'state') {
155
- const state = ev.target.get('state');
156
- slot.updateState(draft => {
157
- Object.assign(draft, state);
158
- });
159
- }
160
- });
161
- }
162
- }
163
- createComponentBySharedComponent(yMap) {
164
- const slots = yMap.get('slots');
165
- const componentLiteral = {
166
- state: yMap.get('state'),
167
- name: yMap.get('name'),
168
- slots: slots.map(sharedSlot => {
169
- return this.createSlotBySharedSlot(sharedSlot).toJSON();
170
- })
171
- };
172
- return this.translator.createComponent(componentLiteral);
173
- }
174
- createSlotBySharedSlot(sharedSlot) {
175
- const content = sharedSlot.get('content');
176
- const delta = content.toDelta();
177
- const slot = this.translator.createSlot({
178
- schema: sharedSlot.get('schema'),
179
- state: sharedSlot.get('state'),
180
- formats: {},
181
- content: []
182
- });
183
- for (const action of delta) {
184
- if (action.insert) {
185
- if (typeof action.insert === 'string') {
186
- slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {
187
- return [this.registry.getFormatter(key), action.attributes[key]];
188
- }) : []);
189
- }
190
- else {
191
- slot.insert(this.createComponentBySharedComponent(action.insert));
192
- }
193
- }
194
- else {
195
- throw collaborateErrorFn('Unexpected delta action.');
196
- }
197
- }
198
- return slot;
199
- }
200
- }
201
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVtb3RlLXRvLWxvY2FsLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2NvbGxhYi9yZW1vdGUtdG8tbG9jYWwudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUdMLFdBQVcsRUFFWCxTQUFTLEVBRVQsVUFBVSxHQUVYLE1BQU0sS0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1Y3Rvcihwcml2YXRlIHlEb2M6IFlEb2MsXG4gICAgICAgICAgICAgIHByaXZhdGUgdHJhbnNsYXRvcjogVHJhbnNsYXRvcixcbiAgICAgICAgICAgICAgcHJpdmF0ZSBzZWxlY3Rpb246IFNlbGVjdGlvbixcbiAgICAgICAgICAgICAgcHJpdmF0ZSByZWdpc3RyeTogUmVnaXN0cnkpIHtcbiAgfVxuXG4gIHRyYW5zZm9ybShldmVudHM6IFlFdmVudFtdLCByb290Q29tcG9uZW50OiBDb21wb25lbnRJbnN0YW5jZSkge1xuICAgIGV2ZW50cy5mb3JFYWNoKGV2ID0+IHtcbiAgICAgIGNvbnN0IHBhdGg6IFlQYXRoID0gW11cblxuICAgICAgZm9yIChsZXQgaSA9IDA7IGkgPCBldi5wYXRoLmxlbmd0aDsgaSArPSAyKSB7XG4gICAgICAgIHBhdGgucHVzaChldi5wYXRoLnNsaWNlKGksIGkgKyAyKSBhcyBbbnVtYmVyLCBzdHJpbmddKVxuICAgICAgfVxuXG4gICAgICBjb25zdCBzbG90ID0gcm9vdENvbXBvbmVudC5zbG90cy5nZXQoMClcblxuICAgICAgaWYgKCFzbG90KSB7XG4gICAgICAgIHRocm93IGNvbGxhYm9yYXRlRXJyb3JGbignY2Fubm90IGZpbmQgY2hpbGQgc2xvdCBpbiByb290IGNvbXBvbmVudCEnKVxuICAgICAgfVxuXG4gICAgICBpZiAocGF0aC5sZW5ndGgpIHtcbiAgICAgICAgY29uc3QgY29tcG9uZW50SW5kZXggPSBwYXRoLnNoaWZ0KCkhWzBdIGFzIG51bWJlclxuICAgICAgICBjb25zdCBjb21wb25lbnQgPSBzbG90LmdldENvbnRlbnRBdEluZGV4KGNvbXBvbmVudEluZGV4KSBhcyBDb21wb25lbnRJbnN0YW5jZVxuICAgICAgICB0aGlzLmFwcGx5U2hhcmVkQ29tcG9uZW50VG9Db21wb25lbnQoZXYsIHBhdGgsIGNvbXBvbmVudClcbiAgICAgICAgcmV0dXJuXG4gICAgICB9XG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4gICAgICAgIH0gZWxzZSBpZiAoYWN0aW9uLmluc2VydCkge1xuICAgICAgICAgIGlmICh0eXBlb2YgYWN0aW9uLmluc2VydCA9PT0gJ3N0cmluZycpIHtcbiAgICAgICAgICAgIHNsb3QuaW5zZXJ0KGFjdGlvbi5pbnNlcnQsIGFjdGlvbi5hdHRyaWJ1dGVzID8gT2JqZWN0LmtleXMoYWN0aW9uLmF0dHJpYnV0ZXMpLm1hcChrZXkgPT4ge1xuICAgICAgICAgICAgICByZXR1cm4gW3RoaXMucmVnaXN0cnkuZ2V0Rm9ybWF0dGVyKGtleSkhLCBhY3Rpb24uYXR0cmlidXRlcyFba2V5XV1cbiAgICAgICAgICAgIH0pIDogW10pXG4gICAgICAgICAgICBvZmZzZXQgKz0gYWN0aW9uLmluc2VydC5sZW5ndGhcbiAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgY29uc3QgY29tcG9uZW50ID0gdGhpcy5jcmVhdGVDb21wb25lbnRCeVNoYXJlZENvbXBvbmVudChhY3Rpb24uaW5zZXJ0IGFzIFlNYXA8YW55PilcbiAgICAgICAgICAgIHNsb3QuaW5zZXJ0KGNvbXBvbmVudClcbiAgICAgICAgICAgIG9mZnNldCArPSAxXG4gICAgICAgICAgfVxuICAgICAgICB9IGVsc2UgaWYgKGFjdGlvbi5kZWxldGUpIHtcbiAgICAgICAgICBzbG90LnJldGFpbihzbG90LmluZGV4KVxuICAgICAgICAgIHNsb3QuZGVsZXRlKGFjdGlvbi5kZWxldGUpXG4gICAgICAgICAgb2Zmc2V0IC09IGFjdGlvbi5kZWxldGVcbiAgICAgICAgfSBlbHNlIGlmIChhY3Rpb24uYXR0cmlidXRlcykge1xuICAgICAgICAgIHNsb3QudXBkYXRlU3RhdGUoZHJhZnQgPT4ge1xuICAgICAgICAgICAgT2JqZWN0LmFzc2lnbihkcmFmdCwgYWN0aW9uLmF0dHJpYnV0ZXMpXG4gICAgICAgICAgfSlcbiAgICAgICAgfVxuICAgICAgfSlcb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ZWdpc3RyeS5nZXRGb3JtYXR0ZXIoa2V5KSEsIGFjdGlvbi5hdHRyaWJ1dGVzIVtrZXldXVxuICAgICAgICAgIH0pIDogW10pXG4gICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgc2xvdC5pbnNlcnQodGhpcy5jcmVhdGVDb21wb25lbnRCeVNoYXJlZENvbXBvbmVudChhY3Rpb24uaW5zZXJ0KSlcbiAgICAgICAgfVxuICAgICAgfSBlbHNlIHtcbiAgICAgICAgdGhyb3cgY29sbGFib3JhdGVFcnJvckZuKCdVbmV4cGVjdGVkIGRlbHRhIGFjdGlvbi4nKVxuICAgICAgfVxuICAgIH1cbiAgICByZXR1cm4gc2xvdFxuICB9XG59XG4iXX0=
@@ -1,161 +0,0 @@
1
- import { Action, Operation, SlotLiteral, ComponentLiteral, makeError } from '@textbus/core'
2
- import { Array as YArray, Map as YMap, Text as YText } from 'yjs'
3
-
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)
19
- }
20
-
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
- })
47
- }
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
56
- }
57
- const content = slotYMap.get('content') as YText
58
-
59
- this.mergeActionsToSharedSlot(content, actions, slotYMap)
60
- }
61
-
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') {
68
- if (action.formats) {
69
- content.format(index, action.offset, action.formats)
70
- } else {
71
- index = action.offset
72
- }
73
- } else if (action.type === 'insert') {
74
- const delta = content.toDelta()
75
- const isEmpty = delta.length === 1 && delta[0].insert === '\n'
76
-
77
- if (typeof action.content === 'string') {
78
- length = action.content.length
79
- content.insert(index, action.content)
80
- } else {
81
- length = 1
82
- content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(action.content))
83
- }
84
- if (action.formats) {
85
- content.format(index, length, action.formats)
86
- }
87
- if (isEmpty && index === 0) {
88
- content.delete(content.length - 1, 1)
89
- }
90
- index += length
91
- } else if (action.type === 'delete') {
92
- const delta = content.toDelta()
93
- content.delete(index, action.count)
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
- }
102
-
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
- })
126
-
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
- }
133
-
134
- private makeSharedComponentByComponentLiteral(componentLiteral: ComponentLiteral): YMap<any> {
135
- const slots = new YArray()
136
- componentLiteral.slots.forEach(item => {
137
- slots.push([this.makeSharedSlotBySlotLiteral(item)])
138
- })
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
- }
145
-
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
153
- }
154
- i += action.insert instanceof YMap ? 1 : action.insert.length
155
- } else {
156
- throw collaborateErrorFn('Unexpected delta action.')
157
- }
158
- }
159
- return null
160
- }
161
- }
@@ -1,192 +0,0 @@
1
- import {
2
- Doc as YDoc,
3
- Map as YMap,
4
- YArrayEvent,
5
- YEvent,
6
- YMapEvent,
7
- Text as YText,
8
- YTextEvent,
9
- Array as YArray,
10
- } from 'yjs'
11
- import { ComponentInstance, ComponentLiteral, makeError, Registry, Selection, Slot, Translator } from '@textbus/core'
12
-
13
- type YPath = [number, string][]
14
- const collaborateErrorFn = makeError('Collaborate')
15
-
16
- export class RemoteToLocal {
17
- constructor(private yDoc: YDoc,
18
- private translator: Translator,
19
- private selection: Selection,
20
- private registry: Registry) {
21
- }
22
-
23
- transform(events: YEvent[], rootComponent: ComponentInstance) {
24
- events.forEach(ev => {
25
- const path: YPath = []
26
-
27
- for (let i = 0; i < ev.path.length; i += 2) {
28
- path.push(ev.path.slice(i, i + 2) as [number, string])
29
- }
30
-
31
- const slot = rootComponent.slots.get(0)
32
-
33
- if (!slot) {
34
- throw collaborateErrorFn('cannot find child slot in root component!')
35
- }
36
-
37
- if (path.length) {
38
- const componentIndex = path.shift()![0] as number
39
- const component = slot.getContentAtIndex(componentIndex) as ComponentInstance
40
- this.applySharedComponentToComponent(ev, path, component)
41
- return
42
- }
43
-
44
- this.applySharedSlotToSlot(ev, path, slot)
45
- })
46
- }
47
-
48
- private applySharedComponentToComponent(ev: YEvent, path: YPath, component: ComponentInstance) {
49
- if (path.length) {
50
- const childPath = path.shift()!
51
- const slot = component.slots.get(childPath[0])!
52
- this.applySharedSlotToSlot(ev, path, slot)
53
- return
54
- }
55
- if (ev instanceof YMapEvent) {
56
- ev.keysChanged.forEach(key => {
57
- if (key === 'state') {
58
- const state = (ev.target as YMap<any>).get('state')
59
- component.updateState(draft => {
60
- Object.assign(draft, state)
61
- })
62
- }
63
- })
64
- } else if (ev instanceof YArrayEvent) {
65
- const slots = component.slots
66
- ev.delta.forEach(action => {
67
- if (Reflect.has(action, 'retain')) {
68
- slots.retain(action.retain!)
69
- } else if (action.insert) {
70
- (action.insert as Array<YMap<any>>).forEach(item => {
71
- slots.insert(this.createSlotBySharedSlot(item))
72
- })
73
- } else if (action.delete) {
74
- slots.retain(slots.index)
75
- slots.delete(action.delete)
76
- }
77
- })
78
- }
79
- }
80
-
81
- private applySharedSlotToSlot(ev: YEvent, path: YPath, slot: Slot) {
82
- if (path.length) {
83
- const childPath = path.shift()!
84
- if ((childPath as any[]).length === 1) {
85
- const delta = (ev.target.parent as YText).toDelta()
86
- let componentIndex = 0
87
- for (let i = 0; i < delta.length; i++) {
88
- const action = delta[i]
89
- if (action.insert === ev.target) {
90
- break
91
- }
92
- componentIndex += typeof action.insert === 'string' ? action.insert.length : 1
93
- }
94
- const component = slot.getContentAtIndex(componentIndex) as ComponentInstance
95
- this.applySharedComponentToComponent(ev, path, component)
96
- } else {
97
- const component = slot.getContentAtIndex(childPath[0]) as ComponentInstance
98
- this.applySharedComponentToComponent(ev, path, component)
99
- }
100
- return
101
- }
102
- if (ev instanceof YTextEvent) {
103
- slot.retain(0)
104
- let offset = 0
105
- ev.delta.forEach(action => {
106
- if (Reflect.has(action, 'retain')) {
107
- if (action.attributes) {
108
- slot.retain(action.retain!, Object.keys(action.attributes).map(key => {
109
- return [this.registry.getFormatter(key)!, action.attributes![key]]
110
- }))
111
- }
112
- slot.retain(action.retain!)
113
- } else if (action.insert) {
114
- if (typeof action.insert === 'string') {
115
- slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {
116
- return [this.registry.getFormatter(key)!, action.attributes![key]]
117
- }) : [])
118
- offset += action.insert.length
119
- } else {
120
- const component = this.createComponentBySharedComponent(action.insert as YMap<any>)
121
- slot.insert(component)
122
- offset += 1
123
- }
124
- } else if (action.delete) {
125
- slot.retain(slot.index)
126
- slot.delete(action.delete)
127
- offset -= action.delete
128
- } else if (action.attributes) {
129
- slot.updateState(draft => {
130
- Object.assign(draft, action.attributes)
131
- })
132
- }
133
- })
134
- if (this.selection.isSelected) {
135
- if (slot === this.selection.startSlot) {
136
- this.selection.setStart(slot, this.selection.startOffset! + offset)
137
- }
138
- if (slot === this.selection.endSlot) {
139
- this.selection.setEnd(slot, this.selection.endOffset! + offset)
140
- }
141
- }
142
- } else if (ev instanceof YMapEvent) {
143
- ev.keysChanged.forEach(key => {
144
- if (key === 'state') {
145
- const state = (ev.target as YMap<any>).get('state')
146
- slot.updateState(draft => {
147
- Object.assign(draft, state)
148
- })
149
- }
150
- })
151
- }
152
- }
153
-
154
- private createComponentBySharedComponent(yMap: YMap<any>): ComponentInstance {
155
- const slots = yMap.get('slots') as YArray<YMap<any>>
156
- const componentLiteral: ComponentLiteral = {
157
- state: yMap.get('state'),
158
- name: yMap.get('name'),
159
- slots: slots.map(sharedSlot => {
160
- return this.createSlotBySharedSlot(sharedSlot).toJSON()
161
- })
162
- }
163
- return this.translator.createComponent(componentLiteral)!
164
- }
165
-
166
- private createSlotBySharedSlot(sharedSlot: YMap<any>): Slot {
167
- const content = sharedSlot.get('content') as YText
168
- const delta = content.toDelta()
169
-
170
- const slot = this.translator.createSlot({
171
- schema: sharedSlot.get('schema'),
172
- state: sharedSlot.get('state'),
173
- formats: {},
174
- content: []
175
- })
176
-
177
- for (const action of delta) {
178
- if (action.insert) {
179
- if (typeof action.insert === 'string') {
180
- slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {
181
- return [this.registry.getFormatter(key)!, action.attributes![key]]
182
- }) : [])
183
- } else {
184
- slot.insert(this.createComponentBySharedComponent(action.insert))
185
- }
186
- } else {
187
- throw collaborateErrorFn('Unexpected delta action.')
188
- }
189
- }
190
- return slot
191
- }
192
- }