@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,184 +1,161 @@
1
- import { Action, Operation, SlotLiteral, ComponentLiteral, Format, FormatRange, FormatValue } from '@textbus/core'
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
- export function localToRemote(operation: Operation, root: YArray<any>) {
5
- const path = [...operation.path]
6
- path.shift()
7
- if (path.length) {
8
- const componentIndex = path.shift()!
9
- applyComponentOperationToSharedComponent(path, operation.apply, root.get(componentIndex))
10
- return
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
- function applyComponentOperationToSharedComponent(path: number[], actions: Action[], componentYMap: YMap<any>) {
16
- const sharedSlots = componentYMap.get('slots') as YArray<any>
17
- if (path.length) {
18
- const slotIndex = path.shift()!
19
- const sharedSlot = sharedSlots.get(slotIndex)
20
- applySlotOperationToSharedSlot(path, actions, sharedSlot)
21
- return
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
- let index: number
24
- actions.forEach(action => {
25
- switch (action.type) {
26
- case 'retain':
27
- index = action.index
28
- break
29
- case 'insertSlot':
30
- sharedSlots.insert(index, [makeSharedSlotBySlotLiteral(action.slot)])
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
- function applySlotOperationToSharedSlot(path: number[], actions: Action[], slotYMap: YMap<any>) {
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
- let index: number
53
- let len: number
54
- actions.forEach(action => {
55
- switch (action.type) {
56
- case 'retain':
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
- mergeSharedFormats(index, action.index, action.formats, slotYMap)
69
+ content.format(index, action.offset, action.formats)
70
+ } else {
71
+ index = action.offset
59
72
  }
60
- index = action.index
61
- break
62
- case 'insert':
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
- len = action.content.length
65
- content.insert(index, action.content.split(''))
78
+ length = action.content.length
79
+ content.insert(index, action.content)
66
80
  } else {
67
- len = 1
68
- content.insert(index, [makeSharedComponentByComponentLiteral(action.content)])
81
+ length = 1
82
+ content.insertEmbed(index, this.makeSharedComponentByComponentLiteral(action.content))
69
83
  }
70
84
  if (action.formats) {
71
- insertSharedFormats(index, len, action.formats, slotYMap)
85
+ content.format(index, length, action.formats)
72
86
  }
73
- break
74
- case 'delete':
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
- break
81
- case 'apply':
82
- slotYMap.set('state', action.value)
83
- break
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
- function insertSharedFormats(index: number, distance: number, formats: Record<string, FormatValue>, sharedSlots: YMap<any>) {
89
- const sharedFormats = sharedSlots.get('formats') as YMap<FormatRange[]>
90
- const keys = Array.from(sharedFormats.keys())
91
- const expandedValues = Array.from<string>({length: distance})
92
- keys.forEach(key => {
93
- const formatRanges = sharedFormats.get(key)!
94
- const values = Format.tileRanges(formatRanges)
95
- values.splice(index, 0, ...expandedValues)
96
- const newRanges = Format.toRanges(values)
97
- sharedFormats.set(key, newRanges)
98
- })
99
- mergeSharedFormats(index, index + distance, formats, sharedSlots)
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
- function mergeSharedFormats(startIndex: number, endIndex: number, formats: Record<string, FormatValue>, sharedSlots: YMap<any>) {
103
- const sharedFormats = sharedSlots.get('formats') as YMap<FormatRange[]>
104
- Object.keys(formats).forEach(key => {
105
- if (!sharedFormats.has(key)) {
106
- sharedFormats.set(key, [{
107
- startIndex,
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
- const oldFormatRanges = sharedFormats.get(key)!
114
- const formatRanges = Format.normalizeFormatRange(oldFormatRanges, {
115
- startIndex,
116
- endIndex,
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
- sharedFormats.set(key, formatRanges)
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
- function insertContent(content: YArray<any>, actions: Action[]) {
124
- let index: number
125
- actions.forEach(action => {
126
- switch (action.type) {
127
- case 'retain':
128
- index = action.index
129
- break
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
- break
140
- case 'delete':
141
- content.delete(index, action.count)
142
- break
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
- index += size
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, FormatType, Registry, Slot, Translator } from '@textbus/core'
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 function remoteToLocal(events: YEvent[], slot: Slot, translator: Translator, registry: Registry) {
7
- events.forEach(ev => {
8
- const path: YPath = []
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
- if (path.length) {
15
- const componentIndex = path.shift()![0] as number
16
- const component = slot.getContentAtIndex(componentIndex) as ComponentInstance
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
- apply(ev, slot, translator)
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
- } else if (ev instanceof YArrayEvent) {
42
- const slots = component.slots
43
- ev.delta.forEach(action => {
44
- if (Reflect.has(action, 'retain')) {
45
- slots.retain(action.retain!)
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
- function applySharedSlotToSlot(ev: YEvent, path: YPath, slot: Slot, translator: Translator, registry: Registry, isUpdateFormats: boolean) {
59
- if (path.length) {
60
- const componentIndex = path.shift()![0]
61
- const component = slot.getContentAtIndex(componentIndex) as ComponentInstance
62
- applySharedComponentToComponent(ev, path, component, translator, registry)
63
- return
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
- if (ev instanceof YArrayEvent) {
67
- ev.delta.forEach(action => {
68
- if (Reflect.has(action, 'retain')) {
69
- slot.retain(action.retain!)
70
- } else if (action.insert) {
71
- (action.insert as Array<any>).forEach(item => {
72
- if (typeof item === 'string') {
73
- slot.insert(item)
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
- slot.insert(translator.createComponent(item.toJSON())!)
97
+ const component = this.createComponentBySharedComponent(action.insert as YMap<any>)
98
+ slot.insert(component)
76
99
  }
77
- })
78
- } else if (action.delete) {
79
- slot.retain(slot.index)
80
- slot.delete(action.delete)
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
- function apply(ev: YEvent, slot: Slot, translator: Translator) {
118
- if (ev instanceof YArrayEvent) {
119
- slot.retain(0)
120
- const delta = ev.delta
121
- delta.forEach(action => {
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 as Array<string | YMap<any>>).forEach(item => {
124
- if (typeof item === 'string') {
125
- slot.insert(item)
126
- } else {
127
- const json = item.toJSON()
128
- const component = translator.createComponent(json)!
129
- slot.insert(component)
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
  }