Skip to content

Commit

Permalink
Fix target ranges when selecting nodes - fixes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Dec 10, 2020
1 parent 25cea84 commit 0be628c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo/prosemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { keymap } from 'prosemirror-keymap'

window.addEventListener('load', () => {
const ydoc = new Y.Doc()
const provider = new WebrtcProvider('prosemirror-demo', ydoc)
const provider = new WebrtcProvider('prosemirror-debug', ydoc)
const type = ydoc.getXmlFragment('prosemirror')

const editor = document.createElement('div')
Expand Down
15 changes: 13 additions & 2 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,23 @@ export const absolutePositionToRelativePosition = (pos, type, mapping) => {
throw error.unexpectedCase()
}
if (pos === 0 && n.constructor !== Y.XmlText && n !== type) { // TODO: set to <= 0
return new Y.RelativePosition(n._item === null ? null : n._item.id, n._item === null ? Y.findRootTypeKey(n) : null, null)
return createRelativePosition(n._item.parent, n._item)
}
}
return Y.createRelativePositionFromTypeIndex(type, type._length)
}

const createRelativePosition = (type, item) => {
let typeid = null
let tname = null
if (type._item === null) {
tname = Y.findRootTypeKey(type)
} else {
typeid = Y.createID(type._item.id.client, type._item.id.clock)
}
return new Y.RelativePosition(typeid, tname, item.id)
}

/**
* @param {Y.Doc} y
* @param {Y.XmlFragment} documentType Top level type that is bound to pView
Expand All @@ -116,7 +127,7 @@ export const absolutePositionToRelativePosition = (pos, type, mapping) => {
*/
export const relativePositionToAbsolutePosition = (y, documentType, relPos, mapping) => {
const decodedPos = Y.createAbsolutePositionFromRelativePosition(relPos, y)
if (decodedPos === null || !Y.isParentOf(documentType, decodedPos.type._item)) {
if (decodedPos === null || (decodedPos.type !== documentType && !Y.isParentOf(documentType, decodedPos.type._item))) {
return null
}
let type = decodedPos.type
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/cursor-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import * as Y from 'yjs'
import { Decoration, DecorationSet } from 'prosemirror-view' // eslint-disable-line
import { Plugin, PluginKey } from 'prosemirror-state' // eslint-disable-line
import { Plugin } from 'prosemirror-state' // eslint-disable-line
import { Awareness } from 'y-protocols/awareness.js' // eslint-disable-line
import { absolutePositionToRelativePosition, relativePositionToAbsolutePosition, setMeta } from '../lib.js'
import { yCursorPluginKey, ySyncPluginKey } from './keys.js'
Expand Down

0 comments on commit 0be628c

Please sign in to comment.