Skip to content

Commit

Permalink
Allow two editor bindings using the same awareness instance. fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Sep 12, 2020
1 parent cbf7226 commit b8c0555
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export const absolutePositionToRelativePosition = (pos, type, mapping) => {
* @param {Y.XmlFragment} documentType Top level type that is bound to pView
* @param {any} relPos Encoded Yjs based relative position
* @param {ProsemirrorMapping} mapping
* @return {null|number}
*/
export const relativePositionToAbsolutePosition = (y, documentType, relPos, mapping) => {
const decodedPos = Y.createAbsolutePositionFromRelativePosition(relPos, y)
Expand Down
12 changes: 7 additions & 5 deletions src/plugins/cursor-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ export const createDecorations = (state, awareness, createCursor) => {
* @param {object} [opts]
* @param {function(any):HTMLElement} [opts.cursorBuilder]
* @param {function(any):any} [opts.getSelection]
* @param {string} [opts.cursorStateField] By default all editor bindings use the awareness 'cursor' field to propagate cursor information.
* @return {any}
*/
export const yCursorPlugin = (awareness, { cursorBuilder = defaultCursorBuilder, getSelection = state => state.selection } = {}) => new Plugin({
export const yCursorPlugin = (awareness, { cursorBuilder = defaultCursorBuilder, getSelection = state => state.selection } = {}, cursorStateField = 'cursor') => new Plugin({
key: yCursorPluginKey,
state: {
init (_, state) {
Expand Down Expand Up @@ -126,12 +127,13 @@ export const yCursorPlugin = (awareness, { cursorBuilder = defaultCursorBuilder,
*/
const head = absolutePositionToRelativePosition(selection.head, ystate.type, ystate.binding.mapping)
if (current.cursor == null || !Y.compareRelativePositions(Y.createRelativePositionFromJSON(current.cursor.anchor), anchor) || !Y.compareRelativePositions(Y.createRelativePositionFromJSON(current.cursor.head), head)) {
awareness.setLocalStateField('cursor', {
awareness.setLocalStateField(cursorStateField, {
anchor, head
})
}
} else if (current.cursor != null) {
awareness.setLocalStateField('cursor', null)
} else if (current.cursor != null && relativePositionToAbsolutePosition(ystate.doc, ystate.type, Y.createRelativePositionFromJSON(current.cursor.anchor), ystate.binding.mapping) !== null) {
// delete cursor information if current cursor information is owned by this editor binding
awareness.setLocalStateField(cursorStateField, null)
}
}
awareness.on('change', awarenessListener)
Expand All @@ -141,7 +143,7 @@ export const yCursorPlugin = (awareness, { cursorBuilder = defaultCursorBuilder,
update: updateCursorInfo,
destroy: () => {
awareness.off('change', awarenessListener)
awareness.setLocalStateField('cursor', null)
awareness.setLocalStateField(cursorStateField, null)
}
}
}
Expand Down

0 comments on commit b8c0555

Please sign in to comment.