2
2
import {
3
3
EditorState ,
4
4
EditorView ,
5
- DOMParser ,
6
5
Schema ,
7
6
baseSchema ,
8
7
history ,
@@ -25,15 +24,13 @@ import {
25
24
yUndoPlugin ,
26
25
yUndo ,
27
26
yRedo ,
28
- prosemirrorToYXmlFragment ,
29
27
} from 'da-y-wrapper' ;
30
28
31
29
// DA
32
30
import prose2aem from '../../shared/prose2aem.js' ;
33
31
import menu from './plugins/menu.js' ;
34
32
import imageDrop from './plugins/imageDrop.js' ;
35
33
import linkConverter from './plugins/linkConverter.js' ;
36
- import { aem2prose , parse } from '../utils/helpers.js' ;
37
34
import { COLLAB_ORIGIN , getDaAdmin } from '../../shared/constants.js' ;
38
35
import { addLocNodes , getLocClass } from './loc-utils.js' ;
39
36
@@ -58,6 +55,8 @@ function addCustomMarks(marks) {
58
55
. addToEnd ( 'contextHighlightingMark' , contextHighlight ) ;
59
56
}
60
57
58
+ // Note: until getSchema() is separated in its own module, this function needs to be kept in-sync
59
+ // with the getSchema() function in da-collab src/collab.js
61
60
export function getSchema ( ) {
62
61
const { marks, nodes : baseNodes } = baseSchema . spec ;
63
62
const withLocNodes = addLocNodes ( baseNodes ) ;
@@ -101,18 +100,6 @@ function pollForUpdates() {
101
100
} , 500 ) ;
102
101
}
103
102
104
- // Apply the document in AEM doc format to the editor.
105
- // For this it's converted to Prose and then applied to the current ydoc as an XML fragment
106
- function setAEMDocInEditor ( aemDoc , yXmlFragment , schema ) {
107
- const doc = parse ( aemDoc ) ;
108
- const pdoc = aem2prose ( doc ) ;
109
- const docc = document . createElement ( 'div' ) ;
110
- docc . append ( ...pdoc ) ;
111
- const parser = DOMParser . fromSchema ( schema ) ;
112
- const fin = parser . parse ( docc ) ;
113
- prosemirrorToYXmlFragment ( fin , yXmlFragment ) ;
114
- }
115
-
116
103
function handleAwarenessUpdates ( wsProvider , daTitle , win ) {
117
104
const users = new Set ( ) ;
118
105
@@ -148,45 +135,13 @@ export function createAwarenessStatusWidget(wsProvider, win) {
148
135
return daTitle ;
149
136
}
150
137
151
- export function handleYDocUpdates ( {
152
- daTitle, editor, ydoc, path, schema, wsProvider, yXmlFragment, fnInitProse,
153
- } , win = window , fnSetAEMDocInEditor = setAEMDocInEditor ) {
154
- let firstUpdate = true ;
155
- ydoc . on ( 'update' , ( _ , originWS ) => {
156
- if ( firstUpdate ) {
157
- firstUpdate = false ;
158
-
159
- // Do the following async to allow the ydoc to init itself with any
160
- // changes coming from other editors
161
- setTimeout ( ( ) => {
162
- const aemMap = ydoc . getMap ( 'aem' ) ;
163
- const current = aemMap . get ( 'content' ) ;
164
- const inital = aemMap . get ( 'initial' ) ;
165
- if ( ! current && inital ) {
166
- fnSetAEMDocInEditor ( inital , yXmlFragment , schema ) ;
167
- }
168
- } , 1 ) ;
169
- }
170
-
171
- const serverInvKey = 'svrinv' ;
172
- const svrUpdate = ydoc . getMap ( 'aem' ) . get ( serverInvKey ) ;
173
- if ( svrUpdate ) {
174
- // push update from the server: re-init document
175
- delete daTitle . collabStatus ;
176
- delete daTitle . collabUsers ;
177
- ydoc . destroy ( ) ;
178
- wsProvider . destroy ( ) ;
179
- editor . innerHTML = '' ;
180
- fnInitProse ( { editor, path } ) ;
181
- return ;
182
- }
183
-
184
- if ( originWS && originWS !== wsProvider ) {
185
- const proseEl = win . view . root . querySelector ( '.ProseMirror' ) ;
186
- const clone = proseEl . cloneNode ( true ) ;
187
- const aem = prose2aem ( clone ) ;
188
- const aemMap = ydoc . getMap ( 'aem' ) ;
189
- aemMap . set ( 'content' , aem ) ;
138
+ function registerErrorHandler ( ydoc ) {
139
+ ydoc . on ( 'update' , ( ) => {
140
+ const errorMap = ydoc . getMap ( 'error' ) ;
141
+ if ( errorMap && errorMap . size > 0 ) {
142
+ // eslint-disable-next-line no-console
143
+ console . log ( 'Error from server' , JSON . stringify ( errorMap ) ) ;
144
+ errorMap . clear ( ) ;
190
145
}
191
146
} ) ;
192
147
}
@@ -226,12 +181,10 @@ export default function initProse({ editor, path }) {
226
181
}
227
182
228
183
const wsProvider = new WebsocketProvider ( server , roomName , ydoc , opts ) ;
229
- const daTitle = createAwarenessStatusWidget ( wsProvider , window ) ;
184
+ createAwarenessStatusWidget ( wsProvider , window ) ;
185
+ registerErrorHandler ( ydoc ) ;
230
186
231
187
const yXmlFragment = ydoc . getXmlFragment ( 'prosemirror' ) ;
232
- handleYDocUpdates ( {
233
- daTitle, editor, ydoc, path, schema, wsProvider, yXmlFragment, fnInitProse : initProse ,
234
- } ) ;
235
188
236
189
if ( window . adobeIMS ?. isSignedInUser ( ) ) {
237
190
window . adobeIMS . getProfile ( ) . then (
@@ -310,4 +263,6 @@ export default function initProse({ editor, path }) {
310
263
311
264
document . execCommand ( 'enableObjectResizing' , false , 'false' ) ;
312
265
document . execCommand ( 'enableInlineTableEditing' , false , 'false' ) ;
266
+
267
+ return wsProvider ;
313
268
}
0 commit comments