@textbus/collaborate 2.0.0-alpha.38 → 2.0.0-alpha.39

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,3 @@
1
1
  export * from './collaborate-cursor';
2
- export * from './collaborate-history';
3
2
  export * from './local-to-remote';
4
3
  export * from './remote-to-local';
@@ -1,5 +1,4 @@
1
1
  export * from './collaborate-cursor';
2
- export * from './collaborate-history';
3
2
  export * from './local-to-remote';
4
3
  export * from './remote-to-local';
5
4
  //# sourceMappingURL=_api.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"_api.js","sourceRoot":"","sources":["../../src/collab/_api.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AACrC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA"}
1
+ {"version":3,"file":"_api.js","sourceRoot":"","sources":["../../src/collab/_api.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA","sourcesContent":["export * from './collaborate-cursor'\nexport * from './local-to-remote'\nexport * from './remote-to-local'\n"]}
@@ -16,23 +16,58 @@ import { Selection } from '@textbus/core';
16
16
  import { Subject } from '@tanbo/stream';
17
17
  let CollaborateCursor = class CollaborateCursor {
18
18
  constructor(container, document, nativeSelection, selection) {
19
- this.container = container;
20
- this.document = document;
21
- this.nativeSelection = nativeSelection;
22
- this.selection = selection;
23
- this.canvas = createElement('canvas', {
24
- styles: {
25
- position: 'absolute',
26
- opacity: 0.5,
27
- left: 0,
28
- top: 0,
29
- width: '100%',
30
- height: '100%',
31
- pointerEvents: 'none'
32
- }
19
+ Object.defineProperty(this, "container", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: container
24
+ });
25
+ Object.defineProperty(this, "document", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: document
30
+ });
31
+ Object.defineProperty(this, "nativeSelection", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: nativeSelection
36
+ });
37
+ Object.defineProperty(this, "selection", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: selection
42
+ });
43
+ Object.defineProperty(this, "canvas", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: createElement('canvas', {
48
+ styles: {
49
+ position: 'absolute',
50
+ opacity: 0.5,
51
+ left: 0,
52
+ top: 0,
53
+ width: '100%',
54
+ height: '100%',
55
+ pointerEvents: 'none'
56
+ }
57
+ })
58
+ });
59
+ Object.defineProperty(this, "context", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: this.canvas.getContext('2d')
64
+ });
65
+ Object.defineProperty(this, "onRectChange", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: new Subject()
33
70
  });
34
- this.context = this.canvas.getContext('2d');
35
- this.onRectChange = new Subject();
36
71
  container.appendChild(this.canvas);
37
72
  this.onRectChange.subscribe(rect => {
38
73
  this.context.fillStyle = rect.color;
@@ -1 +1 @@
1
- {"version":3,"file":"collaborate-cursor.js","sourceRoot":"","sources":["../../src/collab/collaborate-cursor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAC9C,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AACtG,OAAO,EAAE,SAAS,EAAkB,MAAM,eAAe,CAAA;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAiBvC,IAAa,iBAAiB,GAA9B,MAAa,iBAAiB;IAgB5B,YAA8C,SAAsB,EACrB,QAAkB,EAC7C,eAAgC,EAChC,SAAoB;QAHM,cAAS,GAAT,SAAS,CAAa;QACrB,aAAQ,GAAR,QAAQ,CAAU;QAC7C,oBAAe,GAAf,eAAe,CAAiB;QAChC,c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IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;4BACrB,CAAC,EAAE,IAAI,CAAC,CAAC;4BACT,CAAC,EAAE,IAAI,CAAC,CAAC;4BACT,KAAK,EAAE,CAAC;4BACR,MAAM,EAAE,IAAI,CAAC,MAAM;4BACnB,KAAK,EAAE,IAAI,CAAC,KAAK;yBAClB,CAAC,CAAA;qBACH;iBACF;aACF;QACH,CAAC,CAAC,CAAA;IACJ,CAAC;CACF,CAAA;AAtFY,iBAAiB;IAD7B,UAAU,EAAE;IAiBE,WAAA,MAAM,CAAC,gBAAgB,CAAC,CAAA;IACxB,WAAA,MAAM,CAAC,iBAAiB,CAAC,CAAA;qCADmB,WAAW;QACX,QAAQ;QAC5B,eAAe;QACrB,SAAS;GAnB7B,iBAAiB,CAsF7B;SAtFY,iBAAiB","sourcesContent":["import { Inject, Injectable } from '@tanbo/di'\nimport { createElement, EDITABLE_DOCUMENT, EDITOR_CONTAINER, SelectionBridge } from '@textbus/browser'\nimport { Selection, SelectionPaths } from '@textbus/core'\nimport { Subject } from '@tanbo/stream'\n\nexport interface RemoteSelection {\n color: string\n username: string\n paths: SelectionPaths\n}\n\nexport interface SelectionRect {\n x: number\n y: number\n width: number\n height: number\n color: string\n}\n\n@Injectable()\nexport class CollaborateCursor {\n private canvas = createElement('canvas', {\n styles: {\n position: 'absolute',\n opacity: 0.5,\n left: 0,\n top: 0,\n width: '100%',\n height: '100%',\n pointerEvents: 'none'\n }\n }) as HTMLCanvasElement\n private context = this.canvas.getContext('2d')!\n\n private onRectChange = new Subject<SelectionRect>()\n\n constructor(@Inject(EDITOR_CONTAINER) private container: HTMLElement,\n @Inject(EDITABLE_DOCUMENT) private document: Document,\n private nativeSelection: SelectionBridge,\n private selection: Selection) {\n container.appendChild(this.canvas)\n this.onRectChange.subscribe(rect => {\n this.context.fillStyle = rect.color\n this.context.beginPath()\n this.context.rect(Math.ceil(rect.x), Math.ceil(rect.y), Math.ceil(rect.width), Math.ceil(rect.height))\n this.context.fill()\n this.context.closePath()\n })\n }\n\n draw(paths: RemoteSelection[]) {\n this.canvas.width = this.container.offsetWidth\n this.canvas.height = this.container.offsetHeight\n this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)\n\n paths.filter(i => {\n return i.paths.start.length && i.paths.end.length\n }).forEach(item => {\n const startOffset = item.paths.start.pop()!\n const startSlot = this.selection.findSlotByPaths(item.paths.start)\n const endOffset = item.paths.end.pop()!\n const endSlot = this.selection.findSlotByPaths(item.paths.end)\n\n if (startSlot && endSlot) {\n const position = this.nativeSelection.getPositionByRange({\n startOffset,\n endOffset,\n startSlot,\n endSlot\n })\n if (position.start && position.end) {\n const nativeRange = this.document.createRange()\n nativeRange.setStart(position.start.node, position.start.offset)\n nativeRange.setEnd(position.end.node, position.end.offset)\n\n const rects = nativeRange.getClientRects()\n let prev: any = {}\n for (let i = rects.length - 1; i >= 0; i--) {\n const rect = rects[i]\n if (prev.y === rect.y) {\n continue\n }\n prev = rect\n this.onRectChange.next({\n x: rect.x,\n y: rect.y,\n width: rect.width,\n height: rect.height,\n color: item.color\n })\n }\n\n if (rects.length === 0) {\n const rect = nativeRange.getBoundingClientRect()\n this.onRectChange.next({\n x: rect.x,\n y: rect.y,\n width: 1,\n height: rect.height,\n color: item.color\n })\n }\n }\n }\n })\n }\n}\n"]}
@@ -1,3 +1,11 @@
1
1
  import { Operation } from '@textbus/core';
2
- import { Array as YArray } from 'yjs';
3
- export declare function localToRemote(operation: Operation, root: YArray<any>): void;
2
+ import { Text as YText } from 'yjs';
3
+ export declare class LocalToRemote {
4
+ transform(operation: Operation, root: YText): void;
5
+ private applyComponentOperationToSharedComponent;
6
+ private applySlotOperationToSharedSlot;
7
+ private mergeActionsToSharedSlot;
8
+ private makeSharedSlotBySlotLiteral;
9
+ private makeSharedComponentByComponentLiteral;
10
+ private getSharedComponentByIndex;
11
+ }
@@ -1,176 +1,155 @@
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 { Array as YArray, Map as YMap, Text as YText } from 'yjs';
2
+ export class LocalToRemote {
3
+ transform(operation, root) {
4
+ const path = [...operation.path];
5
+ path.shift();
6
+ if (path.length) {
7
+ const componentIndex = path.shift();
8
+ const sharedComponent = this.getSharedComponentByIndex(root, componentIndex);
9
+ if (sharedComponent) {
10
+ this.applyComponentOperationToSharedComponent(path, operation.apply, sharedComponent);
11
+ }
12
+ return;
13
+ }
14
+ this.mergeActionsToSharedSlot(root, operation.apply);
10
15
  }
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;
16
+ applyComponentOperationToSharedComponent(path, actions, componentYMap) {
17
+ const sharedSlots = componentYMap.get('slots');
18
+ if (path.length) {
19
+ const slotIndex = path.shift();
20
+ const sharedSlot = sharedSlots.get(slotIndex);
21
+ this.applySlotOperationToSharedSlot(path, actions, sharedSlot);
22
+ return;
23
+ }
24
+ let index;
25
+ actions.forEach(action => {
26
+ switch (action.type) {
27
+ case 'retain':
28
+ index = action.offset;
29
+ break;
30
+ case 'insertSlot':
31
+ sharedSlots.insert(index, [this.makeSharedSlotBySlotLiteral(action.slot)]);
32
+ index++;
33
+ break;
34
+ case 'apply':
35
+ componentYMap.set('state', action.value);
36
+ break;
37
+ case 'delete':
38
+ sharedSlots.delete(index, action.count);
39
+ break;
40
+ }
41
+ });
20
42
  }
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;
43
+ applySlotOperationToSharedSlot(path, actions, slotYMap) {
44
+ if (path.length) {
45
+ const componentIndex = path.shift();
46
+ const sharedContent = slotYMap.get('content');
47
+ const sharedComponent = this.getSharedComponentByIndex(sharedContent, componentIndex);
48
+ this.applyComponentOperationToSharedComponent(path, actions, sharedComponent);
49
+ return;
36
50
  }
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;
51
+ const content = slotYMap.get('content');
52
+ this.mergeActionsToSharedSlot(content, actions);
46
53
  }
47
- const content = slotYMap.get('content');
48
- let index;
49
- let len;
50
- actions.forEach(action => {
51
- switch (action.type) {
52
- case 'retain':
54
+ mergeActionsToSharedSlot(content, actions) {
55
+ let index;
56
+ let length;
57
+ actions.forEach(action => {
58
+ var _a;
59
+ if (action.type === 'retain') {
53
60
  if (action.formats) {
54
- mergeSharedFormats(index, action.index, action.formats, slotYMap);
61
+ content.format(index, action.offset, action.formats);
62
+ }
63
+ else {
64
+ index = action.offset;
55
65
  }
56
- index = action.index;
57
- break;
58
- case 'insert':
66
+ }
67
+ else if (action.type === 'insert') {
68
+ const delta = content.toDelta();
69
+ const isEmpty = delta.length === 1 && delta[0].insert === '\n';
59
70
  if (typeof action.content === 'string') {
60
- len = action.content.length;
61
- content.insert(index, action.content.split(''));
71
+ length = action.content.length;
72
+ content.insert(index, action.content);
62
73
  }
63
74
  else {
64
- len = 1;
65
- content.insert(index, [makeSharedComponentByComponentLiteral(action.content)]);
75
+ length = 1;
76
+ content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(action.content));
66
77
  }
67
78
  if (action.formats) {
68
- insertSharedFormats(index, len, action.formats, slotYMap);
79
+ content.format(index, length, action.formats);
69
80
  }
70
- break;
71
- case 'delete':
72
- if (content.length === 0) {
73
- // 当内容为空时,slot 实例内容为 ['\n'],触发删除会导致长度溢出
74
- return;
81
+ if (isEmpty && index === 0) {
82
+ content.delete(content.length - 1, 1);
75
83
  }
84
+ index += length;
85
+ }
86
+ else if (action.type === 'delete') {
87
+ const delta = content.toDelta();
76
88
  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]
89
+ if (content.length === 0) {
90
+ content.insert(0, '\n', (_a = delta[0]) === null || _a === void 0 ? void 0 : _a.attributes);
91
+ }
92
+ }
93
+ else if (action.type === 'apply') {
94
+ content.setAttribute('state', action.value);
95
+ }
112
96
  });
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 根节点样式
97
+ }
98
+ makeSharedSlotBySlotLiteral(slotLiteral) {
99
+ const content = new YText();
100
+ let index = 0;
101
+ slotLiteral.content.forEach(i => {
102
+ let size;
103
+ if (typeof i === 'string') {
104
+ size = i.length;
105
+ content.insert(index, i);
106
+ }
107
+ else {
108
+ size = 1;
109
+ content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(i));
110
+ }
111
+ index += size;
112
+ });
113
+ const formats = slotLiteral.formats;
114
+ Object.keys(formats).forEach(key => {
115
+ const formatRanges = formats[key];
116
+ formatRanges.forEach(formatRange => {
117
+ content.format(formatRange.startIndex, formatRange.endIndex - formatRange.startIndex, {
118
+ [key]: formatRange.value
119
+ });
120
+ });
121
+ });
122
+ const sharedSlot = new YMap();
123
+ sharedSlot.set('content', content);
124
+ sharedSlot.set('schema', slotLiteral.schema);
125
+ return sharedSlot;
126
+ }
127
+ makeSharedComponentByComponentLiteral(componentLiteral) {
128
+ const slots = new YArray();
129
+ componentLiteral.slots.forEach(item => {
130
+ slots.push([this.makeSharedSlotBySlotLiteral(item)]);
131
+ });
132
+ const sharedComponent = new YMap();
133
+ sharedComponent.set('name', componentLiteral.name);
134
+ sharedComponent.set('slots', slots);
135
+ sharedComponent.set('state', componentLiteral.state);
136
+ return sharedComponent;
137
+ }
138
+ getSharedComponentByIndex(host, index) {
139
+ const delta = host.toDelta();
140
+ let i = 0;
141
+ for (const action of delta) {
142
+ if (action.insert) {
143
+ if (i === index) {
144
+ return action.insert instanceof YMap ? action.insert : null;
131
145
  }
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]);
146
+ i += action.insert instanceof YMap ? 1 : action.insert.length;
147
+ }
148
+ else {
149
+ throw new Error('xxx');
150
+ }
147
151
  }
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;
152
+ return null;
153
+ }
175
154
  }
176
155
  //# 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,IAAI,EAAE,MAAM,KAAK,CAAA;AAElD,MAAM,UAAU,aAAa,CAAC,SAAoB,EAAE,IAAiB;IACnE,MAAM,IAAI,GAAG,CAAC,GAAG,SAAS,CAAC,IAAI,CAAC,CAAA;IAChC,IAAI,CAAC,KAAK,EAAE,CAAA;IACZ,IAAI,IAAI,CAAC,MAAM,EAAE;QACf,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,EAAG,CAAA;QACpC,wCAAwC,CAAC,IAAI,EAA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,EAAE,KAAK,CAAC,CAAA;QACnC,eAAe,CAAC,GAAG,CAAC,OAAO,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAA;QACpD,OAAO,eAAe,CAAA;IACxB,CAAC;IAEO,yBAAyB,CAAC,IAAW,EAAE,KAAa;QAC1D,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,EAAE,CAAA;QAC5B,IAAI,CAAC,GAAG,CAAC,CAAA;QACT,KAAK,MAAM,MAAM,IAAI,KAAK,EAAE;YAC1B,IAAI,MAAM,CAAC,MAAM,EAAE;gBACjB,IAAI,CAAC,KAAK,KAAK,EAAE;oBACf,OAAO,MAAM,CAAC,MAAM,YAAY,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAA;iBAC5D;gBACD,CAAC,IAAI,MAAM,CAAC,MAAM,YAAY,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAA;aAC9D;iBAAM;gBACL,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAA;aACvB;SACF;QACD,OAAO,IAAI,CAAA;IACb,CAAC;CACF","sourcesContent":["import { Action, Operation, SlotLiteral, ComponentLiteral } from '@textbus/core'\nimport { Array as YArray, Map as YMap, Text as YText } from 'yjs'\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)\n }\n\n private mergeActionsToSharedSlot(content: YText, actions: Action[]) {\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 content.setAttribute('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 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 new Error('xxx')\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
+ }