@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.
@@ -1,176 +1,158 @@
1
- import { Format } from '@textbus/core';
2
- import { Array as YArray, Map as YMap } from 'yjs';
3
- export function localToRemote(operation, root) {
4
- const path = [...operation.path];
5
- path.shift();
6
- if (path.length) {
7
- const componentIndex = path.shift();
8
- applyComponentOperationToSharedComponent(path, operation.apply, root.get(componentIndex));
9
- return;
1
+ import { makeError } from '@textbus/core';
2
+ import { Array as YArray, Map as YMap, Text as YText } from 'yjs';
3
+ const collaborateErrorFn = makeError('Collaborate');
4
+ export class LocalToRemote {
5
+ transform(operation, root) {
6
+ const path = [...operation.path];
7
+ path.shift();
8
+ if (path.length) {
9
+ const componentIndex = path.shift();
10
+ const sharedComponent = this.getSharedComponentByIndex(root, componentIndex);
11
+ if (sharedComponent) {
12
+ this.applyComponentOperationToSharedComponent(path, operation.apply, sharedComponent);
13
+ }
14
+ return;
15
+ }
16
+ this.mergeActionsToSharedSlot(root, operation.apply);
10
17
  }
11
- insertContent(root, operation.apply);
12
- }
13
- function applyComponentOperationToSharedComponent(path, actions, componentYMap) {
14
- const sharedSlots = componentYMap.get('slots');
15
- if (path.length) {
16
- const slotIndex = path.shift();
17
- const sharedSlot = sharedSlots.get(slotIndex);
18
- applySlotOperationToSharedSlot(path, actions, sharedSlot);
19
- return;
18
+ applyComponentOperationToSharedComponent(path, actions, componentYMap) {
19
+ const sharedSlots = componentYMap.get('slots');
20
+ if (path.length) {
21
+ const slotIndex = path.shift();
22
+ const sharedSlot = sharedSlots.get(slotIndex);
23
+ this.applySlotOperationToSharedSlot(path, actions, sharedSlot);
24
+ return;
25
+ }
26
+ let index;
27
+ actions.forEach(action => {
28
+ switch (action.type) {
29
+ case 'retain':
30
+ index = action.offset;
31
+ break;
32
+ case 'insertSlot':
33
+ sharedSlots.insert(index, [this.makeSharedSlotBySlotLiteral(action.slot)]);
34
+ index++;
35
+ break;
36
+ case 'apply':
37
+ componentYMap.set('state', action.value);
38
+ break;
39
+ case 'delete':
40
+ sharedSlots.delete(index, action.count);
41
+ break;
42
+ }
43
+ });
20
44
  }
21
- let index;
22
- actions.forEach(action => {
23
- switch (action.type) {
24
- case 'retain':
25
- index = action.index;
26
- break;
27
- case 'insertSlot':
28
- sharedSlots.insert(index, [makeSharedSlotBySlotLiteral(action.slot)]);
29
- break;
30
- case 'apply':
31
- componentYMap.set('state', action.value);
32
- break;
33
- case 'delete':
34
- sharedSlots.delete(index, action.count);
35
- break;
45
+ applySlotOperationToSharedSlot(path, actions, slotYMap) {
46
+ if (path.length) {
47
+ const componentIndex = path.shift();
48
+ const sharedContent = slotYMap.get('content');
49
+ const sharedComponent = this.getSharedComponentByIndex(sharedContent, componentIndex);
50
+ this.applyComponentOperationToSharedComponent(path, actions, sharedComponent);
51
+ return;
36
52
  }
37
- });
38
- }
39
- function applySlotOperationToSharedSlot(path, actions, slotYMap) {
40
- if (path.length) {
41
- const componentIndex = path.shift();
42
- const sharedContent = slotYMap.get('content');
43
- const sharedComponent = sharedContent.get(componentIndex);
44
- applyComponentOperationToSharedComponent(path, actions, sharedComponent);
45
- return;
53
+ const content = slotYMap.get('content');
54
+ this.mergeActionsToSharedSlot(content, actions, slotYMap);
46
55
  }
47
- const content = slotYMap.get('content');
48
- let index;
49
- let len;
50
- actions.forEach(action => {
51
- switch (action.type) {
52
- case 'retain':
56
+ mergeActionsToSharedSlot(content, actions, slotYMap) {
57
+ let index;
58
+ let length;
59
+ actions.forEach(action => {
60
+ var _a;
61
+ if (action.type === 'retain') {
53
62
  if (action.formats) {
54
- mergeSharedFormats(index, action.index, action.formats, slotYMap);
63
+ content.format(index, action.offset, action.formats);
64
+ }
65
+ else {
66
+ index = action.offset;
55
67
  }
56
- index = action.index;
57
- break;
58
- case 'insert':
68
+ }
69
+ else if (action.type === 'insert') {
70
+ const delta = content.toDelta();
71
+ const isEmpty = delta.length === 1 && delta[0].insert === '\n';
59
72
  if (typeof action.content === 'string') {
60
- len = action.content.length;
61
- content.insert(index, action.content.split(''));
73
+ length = action.content.length;
74
+ content.insert(index, action.content);
62
75
  }
63
76
  else {
64
- len = 1;
65
- content.insert(index, [makeSharedComponentByComponentLiteral(action.content)]);
77
+ length = 1;
78
+ content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(action.content));
66
79
  }
67
80
  if (action.formats) {
68
- insertSharedFormats(index, len, action.formats, slotYMap);
81
+ content.format(index, length, action.formats);
69
82
  }
70
- break;
71
- case 'delete':
72
- if (content.length === 0) {
73
- // 当内容为空时,slot 实例内容为 ['\n'],触发删除会导致长度溢出
74
- return;
83
+ if (isEmpty && index === 0) {
84
+ content.delete(content.length - 1, 1);
75
85
  }
86
+ index += length;
87
+ }
88
+ else if (action.type === 'delete') {
89
+ const delta = content.toDelta();
76
90
  content.delete(index, action.count);
77
- break;
78
- case 'apply':
79
- slotYMap.set('state', action.value);
80
- break;
81
- }
82
- });
83
- }
84
- function insertSharedFormats(index, distance, formats, sharedSlots) {
85
- const sharedFormats = sharedSlots.get('formats');
86
- const keys = Array.from(sharedFormats.keys());
87
- const expandedValues = Array.from({ length: distance });
88
- keys.forEach(key => {
89
- const formatRanges = sharedFormats.get(key);
90
- const values = Format.tileRanges(formatRanges);
91
- values.splice(index, 0, ...expandedValues);
92
- const newRanges = Format.toRanges(values);
93
- sharedFormats.set(key, newRanges);
94
- });
95
- mergeSharedFormats(index, index + distance, formats, sharedSlots);
96
- }
97
- function mergeSharedFormats(startIndex, endIndex, formats, sharedSlots) {
98
- const sharedFormats = sharedSlots.get('formats');
99
- Object.keys(formats).forEach(key => {
100
- if (!sharedFormats.has(key)) {
101
- sharedFormats.set(key, [{
102
- startIndex,
103
- endIndex,
104
- value: formats[key]
105
- }]);
106
- }
107
- const oldFormatRanges = sharedFormats.get(key);
108
- const formatRanges = Format.normalizeFormatRange(oldFormatRanges, {
109
- startIndex,
110
- endIndex,
111
- value: formats[key]
91
+ if (content.length === 0) {
92
+ content.insert(0, '\n', (_a = delta[0]) === null || _a === void 0 ? void 0 : _a.attributes);
93
+ }
94
+ }
95
+ else if (action.type === 'apply') {
96
+ slotYMap === null || slotYMap === void 0 ? void 0 : slotYMap.set('state', action.value);
97
+ }
112
98
  });
113
- sharedFormats.set(key, formatRanges);
114
- });
115
- }
116
- function insertContent(content, actions) {
117
- let index;
118
- actions.forEach(action => {
119
- switch (action.type) {
120
- case 'retain':
121
- index = action.index;
122
- break;
123
- case 'insert':
124
- content.insert(index, [
125
- typeof action.content === 'string' ?
126
- action.content :
127
- makeSharedComponentByComponentLiteral(action.content)
128
- ]);
129
- if (action.formats) {
130
- // TODO 根节点样式
99
+ }
100
+ makeSharedSlotBySlotLiteral(slotLiteral) {
101
+ const content = new YText();
102
+ let index = 0;
103
+ slotLiteral.content.forEach(i => {
104
+ let size;
105
+ if (typeof i === 'string') {
106
+ size = i.length;
107
+ content.insert(index, i);
108
+ }
109
+ else {
110
+ size = 1;
111
+ content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(i));
112
+ }
113
+ index += size;
114
+ });
115
+ const formats = slotLiteral.formats;
116
+ Object.keys(formats).forEach(key => {
117
+ const formatRanges = formats[key];
118
+ formatRanges.forEach(formatRange => {
119
+ content.format(formatRange.startIndex, formatRange.endIndex - formatRange.startIndex, {
120
+ [key]: formatRange.value
121
+ });
122
+ });
123
+ });
124
+ const sharedSlot = new YMap();
125
+ sharedSlot.set('content', content);
126
+ sharedSlot.set('schema', slotLiteral.schema);
127
+ sharedSlot.set('state', slotLiteral.state);
128
+ return sharedSlot;
129
+ }
130
+ makeSharedComponentByComponentLiteral(componentLiteral) {
131
+ const slots = new YArray();
132
+ componentLiteral.slots.forEach(item => {
133
+ slots.push([this.makeSharedSlotBySlotLiteral(item)]);
134
+ });
135
+ const sharedComponent = new YMap();
136
+ sharedComponent.set('name', componentLiteral.name);
137
+ sharedComponent.set('slots', slots);
138
+ sharedComponent.set('state', componentLiteral.state);
139
+ return sharedComponent;
140
+ }
141
+ getSharedComponentByIndex(host, index) {
142
+ const delta = host.toDelta();
143
+ let i = 0;
144
+ for (const action of delta) {
145
+ if (action.insert) {
146
+ if (i === index) {
147
+ return action.insert instanceof YMap ? action.insert : null;
131
148
  }
132
- break;
133
- case 'delete':
134
- content.delete(index, action.count);
135
- break;
136
- }
137
- });
138
- }
139
- function makeSharedSlotBySlotLiteral(slotLiteral) {
140
- const content = new YArray();
141
- let index = 0;
142
- slotLiteral.content.forEach(i => {
143
- let size;
144
- if (typeof i === 'string') {
145
- size = i.length;
146
- content.insert(index, [i]);
149
+ i += action.insert instanceof YMap ? 1 : action.insert.length;
150
+ }
151
+ else {
152
+ throw collaborateErrorFn('Unexpected delta action.');
153
+ }
147
154
  }
148
- else {
149
- size = 1;
150
- content.insert(index, [makeSharedComponentByComponentLiteral(i)]);
151
- }
152
- index += size;
153
- });
154
- const formats = new YMap();
155
- Object.keys(slotLiteral.formats).forEach(key => {
156
- formats.set(key, slotLiteral.formats[key]);
157
- });
158
- const sharedSlot = new YMap();
159
- sharedSlot.set('state', slotLiteral.state);
160
- sharedSlot.set('content', content);
161
- sharedSlot.set('schema', slotLiteral.schema);
162
- sharedSlot.set('formats', formats);
163
- return sharedSlot;
164
- }
165
- function makeSharedComponentByComponentLiteral(componentLiteral) {
166
- const slots = new YArray();
167
- componentLiteral.slots.forEach(item => {
168
- slots.push([makeSharedSlotBySlotLiteral(item)]);
169
- });
170
- const sharedComponent = new YMap();
171
- sharedComponent.set('name', componentLiteral.name);
172
- sharedComponent.set('slots', slots);
173
- sharedComponent.set('state', componentLiteral.state);
174
- return sharedComponent;
155
+ return null;
156
+ }
175
157
  }
176
158
  //# sourceMappingURL=local-to-remote.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"local-to-remote.js","sourceRoot":"","sources":["../../src/collab/local-to-remote.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoD,MAAM,EAA4B,MAAM,eAAe,CAAA;AAClH,OAAO,EAAE,KAAK,IAAI,MAAM,EAAE,GAAG,IAAI,IA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ACF;QACD,OAAO,IAAI,CAAA;IACb,CAAC;CACF","sourcesContent":["import { Action, Operation, SlotLiteral, ComponentLiteral, makeError } from '@textbus/core'\nimport { Array as YArray, Map as YMap, Text as YText } from 'yjs'\n\nconst collaborateErrorFn = makeError('Collaborate')\n\nexport class LocalToRemote {\n transform(operation: Operation, root: YText) {\n const path = [...operation.path]\n path.shift()\n if (path.length) {\n const componentIndex = path.shift()!\n const sharedComponent = this.getSharedComponentByIndex(root, componentIndex)\n if (sharedComponent) {\n this.applyComponentOperationToSharedComponent(path, operation.apply, sharedComponent)\n }\n return\n }\n this.mergeActionsToSharedSlot(root, operation.apply)\n }\n\n private applyComponentOperationToSharedComponent(path: number[], actions: Action[], componentYMap: YMap<any>) {\n const sharedSlots = componentYMap.get('slots') as YArray<any>\n if (path.length) {\n const slotIndex = path.shift()!\n const sharedSlot = sharedSlots.get(slotIndex)\n this.applySlotOperationToSharedSlot(path, actions, sharedSlot)\n return\n }\n let index: number\n actions.forEach(action => {\n switch (action.type) {\n case 'retain':\n index = action.offset\n break\n case 'insertSlot':\n sharedSlots.insert(index, [this.makeSharedSlotBySlotLiteral(action.slot)])\n index++\n break\n case 'apply':\n componentYMap.set('state', action.value)\n break\n case 'delete':\n sharedSlots.delete(index, action.count)\n break\n }\n })\n }\n\n private applySlotOperationToSharedSlot(path: number[], actions: Action[], slotYMap: YMap<any>) {\n if (path.length) {\n const componentIndex = path.shift()!\n const sharedContent = slotYMap.get('content') as YText\n const sharedComponent = this.getSharedComponentByIndex(sharedContent, componentIndex)!\n this.applyComponentOperationToSharedComponent(path, actions, sharedComponent)\n return\n }\n const content = slotYMap.get('content') as YText\n\n this.mergeActionsToSharedSlot(content, actions, slotYMap)\n }\n\n private mergeActionsToSharedSlot(content: YText, actions: Action[], slotYMap?: YMap<any>) {\n let index: number\n let length: number\n\n actions.forEach(action => {\n if (action.type === 'retain') {\n if (action.formats) {\n content.format(index, action.offset, action.formats)\n } else {\n index = action.offset\n }\n } else if (action.type === 'insert') {\n const delta = content.toDelta()\n const isEmpty = delta.length === 1 && delta[0].insert === '\\n'\n\n if (typeof action.content === 'string') {\n length = action.content.length\n content.insert(index, action.content)\n } else {\n length = 1\n content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(action.content))\n }\n if (action.formats) {\n content.format(index, length, action.formats)\n }\n if (isEmpty && index === 0) {\n content.delete(content.length - 1, 1)\n }\n index += length\n } else if (action.type === 'delete') {\n const delta = content.toDelta()\n content.delete(index, action.count)\n if (content.length === 0) {\n content.insert(0, '\\n', delta[0]?.attributes)\n }\n } else if (action.type === 'apply') {\n slotYMap?.set('state', action.value)\n }\n })\n }\n\n private makeSharedSlotBySlotLiteral(slotLiteral: SlotLiteral): YMap<any> {\n const content = new YText()\n let index = 0\n slotLiteral.content.forEach(i => {\n let size: number\n if (typeof i === 'string') {\n size = i.length\n content.insert(index, i)\n } else {\n size = 1\n content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(i))\n }\n index += size\n })\n const formats = slotLiteral.formats\n Object.keys(formats).forEach(key => {\n const formatRanges = formats[key]\n formatRanges.forEach(formatRange => {\n content.format(formatRange.startIndex, formatRange.endIndex - formatRange.startIndex, {\n [key]: formatRange.value\n })\n })\n })\n\n const sharedSlot = new YMap()\n sharedSlot.set('content', content)\n sharedSlot.set('schema', slotLiteral.schema)\n sharedSlot.set('state', slotLiteral.state)\n return sharedSlot\n }\n\n private makeSharedComponentByComponentLiteral(componentLiteral: ComponentLiteral): YMap<any> {\n const slots = new YArray()\n componentLiteral.slots.forEach(item => {\n slots.push([this.makeSharedSlotBySlotLiteral(item)])\n })\n const sharedComponent = new YMap()\n sharedComponent.set('name', componentLiteral.name)\n sharedComponent.set('slots', slots)\n sharedComponent.set('state', componentLiteral.state)\n return sharedComponent\n }\n\n private getSharedComponentByIndex(host: YText, index: number): YMap<any> | null {\n const delta = host.toDelta()\n let i = 0\n for (const action of delta) {\n if (action.insert) {\n if (i === index) {\n return action.insert instanceof YMap ? action.insert : null\n }\n i += action.insert instanceof YMap ? 1 : action.insert.length\n } else {\n throw collaborateErrorFn('Unexpected delta action.')\n }\n }\n return null\n }\n}\n"]}
@@ -1,3 +1,12 @@
1
1
  import { YEvent } from 'yjs';
2
2
  import { Registry, Slot, Translator } from '@textbus/core';
3
- export declare function remoteToLocal(events: YEvent[], slot: Slot, translator: Translator, registry: Registry): void;
3
+ export declare class RemoteToLocal {
4
+ private translator;
5
+ private registry;
6
+ constructor(translator: Translator, registry: Registry);
7
+ transform(events: YEvent[], slot: Slot): void;
8
+ private applySharedComponentToComponent;
9
+ private applySharedSlotToSlot;
10
+ private createComponentBySharedComponent;
11
+ private createSlotBySharedSlot;
12
+ }
@@ -1,107 +1,121 @@
1
- import { YArrayEvent, YMapEvent } from 'yjs';
2
- import { FormatType } from '@textbus/core';
3
- export function remoteToLocal(events, slot, translator, registry) {
4
- events.forEach(ev => {
5
- const path = [];
6
- for (let i = 0; i < ev.path.length; i += 2) {
7
- path.push(ev.path.slice(i, i + 2));
8
- }
9
- if (path.length) {
10
- const componentIndex = path.shift()[0];
11
- const component = slot.getContentAtIndex(componentIndex);
12
- applySharedComponentToComponent(ev, path, component, translator, registry);
13
- return;
14
- }
15
- apply(ev, slot, translator);
16
- });
17
- }
18
- function applySharedComponentToComponent(ev, path, component, translator, registry) {
19
- if (path.length) {
20
- const childPath = path.shift();
21
- const slot = component.slots.get(childPath[0]);
22
- applySharedSlotToSlot(ev, path, slot, translator, registry, childPath[1] === 'formats');
23
- return;
24
- }
25
- if (ev instanceof YMapEvent) {
26
- ev.keysChanged.forEach(key => {
27
- if (key === 'state') {
28
- const state = ev.target.get('state');
29
- component.updateState(draft => {
30
- Object.assign(draft, state);
31
- });
32
- }
1
+ import { YArrayEvent, YMapEvent, YTextEvent } from 'yjs';
2
+ import { makeError } from '@textbus/core';
3
+ const collaborateErrorFn = makeError('Collaborate');
4
+ export class RemoteToLocal {
5
+ constructor(translator, registry) {
6
+ Object.defineProperty(this, "translator", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: translator
33
11
  });
34
- }
35
- else if (ev instanceof YArrayEvent) {
36
- const slots = component.slots;
37
- ev.delta.forEach(action => {
38
- if (Reflect.has(action, 'retain')) {
39
- slots.retain(action.retain);
40
- }
41
- else if (action.insert) {
42
- action.insert.forEach(item => {
43
- slots.insert(translator.createSlot(item.toJSON()));
44
- });
45
- }
46
- else if (action.delete) {
47
- slots.retain(slots.index);
48
- slots.delete(action.delete);
49
- }
12
+ Object.defineProperty(this, "registry", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: registry
50
17
  });
51
18
  }
52
- }
53
- function applySharedSlotToSlot(ev, path, slot, translator, registry, isUpdateFormats) {
54
- if (path.length) {
55
- const componentIndex = path.shift()[0];
56
- const component = slot.getContentAtIndex(componentIndex);
57
- applySharedComponentToComponent(ev, path, component, translator, registry);
58
- return;
59
- }
60
- if (ev instanceof YArrayEvent) {
61
- ev.delta.forEach(action => {
62
- if (Reflect.has(action, 'retain')) {
63
- slot.retain(action.retain);
64
- }
65
- else if (action.insert) {
66
- action.insert.forEach(item => {
67
- if (typeof item === 'string') {
68
- slot.insert(item);
69
- }
70
- else {
71
- slot.insert(translator.createComponent(item.toJSON()));
72
- }
73
- });
19
+ transform(events, slot) {
20
+ events.forEach(ev => {
21
+ const path = [];
22
+ for (let i = 0; i < ev.path.length; i += 2) {
23
+ path.push(ev.path.slice(i, i + 2));
74
24
  }
75
- else if (action.delete) {
76
- slot.retain(slot.index);
77
- slot.delete(action.delete);
25
+ if (path.length) {
26
+ const componentIndex = path.shift()[0];
27
+ const component = slot.getContentAtIndex(componentIndex);
28
+ this.applySharedComponentToComponent(ev, path, component);
29
+ return;
78
30
  }
31
+ this.applySharedSlotToSlot(ev, path, slot);
79
32
  });
80
33
  }
81
- else if (ev instanceof YMapEvent) {
82
- if (isUpdateFormats) {
83
- const json = ev.target.toJSON();
34
+ applySharedComponentToComponent(ev, path, component) {
35
+ if (path.length) {
36
+ const childPath = path.shift();
37
+ const slot = component.slots.get(childPath[0]);
38
+ this.applySharedSlotToSlot(ev, path, slot);
39
+ return;
40
+ }
41
+ if (ev instanceof YMapEvent) {
84
42
  ev.keysChanged.forEach(key => {
85
- const formats = json[key];
86
- const formatter = registry.getFormatter(key);
87
- if (formatter.type !== FormatType.Block) {
88
- slot.applyFormat(formatter, {
89
- startIndex: 0,
90
- endIndex: slot.length,
91
- value: null
43
+ if (key === 'state') {
44
+ const state = ev.target.get('state');
45
+ component.updateState(draft => {
46
+ Object.assign(draft, state);
92
47
  });
93
48
  }
94
- formats.forEach(item => {
95
- if (formatter.type === FormatType.Block) {
96
- slot.applyFormat(formatter, item.value);
49
+ });
50
+ }
51
+ else if (ev instanceof YArrayEvent) {
52
+ const slots = component.slots;
53
+ ev.delta.forEach(action => {
54
+ if (Reflect.has(action, 'retain')) {
55
+ slots.retain(action.retain);
56
+ }
57
+ else if (action.insert) {
58
+ action.insert.forEach(item => {
59
+ slots.insert(this.createSlotBySharedSlot(item));
60
+ });
61
+ }
62
+ else if (action.delete) {
63
+ slots.retain(slots.index);
64
+ slots.delete(action.delete);
65
+ }
66
+ });
67
+ }
68
+ }
69
+ applySharedSlotToSlot(ev, path, slot) {
70
+ if (path.length) {
71
+ path.shift();
72
+ const delta = ev.target.parent.toDelta();
73
+ let componentIndex = 0;
74
+ for (let i = 0; i < delta.length; i++) {
75
+ const action = delta[i];
76
+ if (action.insert === ev.target) {
77
+ break;
78
+ }
79
+ componentIndex += typeof action.insert === 'string' ? action.insert.length : 1;
80
+ }
81
+ const component = slot.getContentAtIndex(componentIndex);
82
+ this.applySharedComponentToComponent(ev, path, component);
83
+ return;
84
+ }
85
+ if (ev instanceof YTextEvent) {
86
+ slot.retain(0);
87
+ ev.delta.forEach(action => {
88
+ if (Reflect.has(action, 'retain')) {
89
+ if (action.attributes) {
90
+ slot.retain(action.retain, Object.keys(action.attributes).map(key => {
91
+ return [this.registry.getFormatter(key), action.attributes[key]];
92
+ }));
93
+ }
94
+ slot.retain(action.retain);
95
+ }
96
+ else if (action.insert) {
97
+ if (typeof action.insert === 'string') {
98
+ slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {
99
+ return [this.registry.getFormatter(key), action.attributes[key]];
100
+ }) : []);
97
101
  }
98
102
  else {
99
- slot.applyFormat(formatter, item);
103
+ const component = this.createComponentBySharedComponent(action.insert);
104
+ slot.insert(component);
100
105
  }
101
- });
106
+ }
107
+ else if (action.delete) {
108
+ slot.retain(slot.index);
109
+ slot.delete(action.delete);
110
+ }
111
+ else if (action.attributes) {
112
+ slot.updateState(draft => {
113
+ Object.assign(draft, action.attributes);
114
+ });
115
+ }
102
116
  });
103
117
  }
104
- else {
118
+ else if (ev instanceof YMapEvent) {
105
119
  ev.keysChanged.forEach(key => {
106
120
  if (key === 'state') {
107
121
  const state = ev.target.get('state');
@@ -112,32 +126,42 @@ function applySharedSlotToSlot(ev, path, slot, translator, registry, isUpdateFor
112
126
  });
113
127
  }
114
128
  }
115
- }
116
- function apply(ev, slot, translator) {
117
- if (ev instanceof YArrayEvent) {
118
- slot.retain(0);
119
- const delta = ev.delta;
120
- delta.forEach(action => {
129
+ createComponentBySharedComponent(yMap) {
130
+ const slots = yMap.get('slots');
131
+ const componentLiteral = {
132
+ state: yMap.get('state'),
133
+ name: yMap.get('name'),
134
+ slots: slots.map(sharedSlot => {
135
+ return this.createSlotBySharedSlot(sharedSlot).toJSON();
136
+ })
137
+ };
138
+ return this.translator.createComponent(componentLiteral);
139
+ }
140
+ createSlotBySharedSlot(sharedSlot) {
141
+ const content = sharedSlot.get('content');
142
+ const delta = content.toDelta();
143
+ const slot = this.translator.createSlot({
144
+ schema: sharedSlot.get('schema'),
145
+ state: sharedSlot.get('state'),
146
+ formats: {},
147
+ content: []
148
+ });
149
+ for (const action of delta) {
121
150
  if (action.insert) {
122
- action.insert.forEach(item => {
123
- if (typeof item === 'string') {
124
- slot.insert(item);
125
- }
126
- else {
127
- const json = item.toJSON();
128
- const component = translator.createComponent(json);
129
- slot.insert(component);
130
- }
131
- });
132
- }
133
- else if (Reflect.has(action, 'retain')) {
134
- slot.retain(action.retain);
151
+ if (typeof action.insert === 'string') {
152
+ slot.insert(action.insert, action.attributes ? Object.keys(action.attributes).map(key => {
153
+ return [this.registry.getFormatter(key), action.attributes[key]];
154
+ }) : []);
155
+ }
156
+ else {
157
+ slot.insert(this.createComponentBySharedComponent(action.insert));
158
+ }
135
159
  }
136
- else if (action.delete) {
137
- slot.retain(slot.index);
138
- slot.delete(action.delete);
160
+ else {
161
+ throw collaborateErrorFn('Unexpected delta action.');
139
162
  }
140
- });
163
+ }
164
+ return slot;
141
165
  }
142
166
  }
143
167
  //# sourceMappingURL=remote-to-local.js.map