Skip to content

Commit

Permalink
https://github.com/yjs/y-prosemirror/pull/62
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHung committed Aug 12, 2021
1 parent 46b7a3c commit b70d6f1
Show file tree
Hide file tree
Showing 13 changed files with 4,561 additions and 49 deletions.
13 changes: 7 additions & 6 deletions demo/dist/prosemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -22989,8 +22989,8 @@
};

const getRelativeSelection = (pmbinding, state) => ({
anchor: absolutePositionToRelativePosition(state.selection.anchor, pmbinding.type, pmbinding.mapping),
head: absolutePositionToRelativePosition(state.selection.head, pmbinding.type, pmbinding.mapping)
anchor: absolutePositionToRelativePosition(state.doc, state.selection.anchor, pmbinding.type, pmbinding.mapping),
head: absolutePositionToRelativePosition(state.doc, state.selection.head, pmbinding.type, pmbinding.mapping)
});

/**
Expand Down Expand Up @@ -23675,13 +23675,14 @@
/**
* Transforms a Prosemirror based absolute position to a Yjs Cursor (relative position in the Yjs model).
*
* @param {Node} doc
* @param {number} pos
* @param {Y.XmlFragment} type
* @param {ProsemirrorMapping} mapping
* @return {any} relative position
*/
const absolutePositionToRelativePosition = (pos, type, mapping) => {
if (pos === 0) {
const absolutePositionToRelativePosition = (doc, pos, type, mapping) => {
if (pos <= Selection.atStart(doc).from) {
return createRelativePositionFromTypeIndex(type, 0)
}
let n = type._first === null ? null : /** @type {Y.ContentType} */ (type._first.content).type;
Expand Down Expand Up @@ -23923,11 +23924,11 @@
/**
* @type {Y.RelativePosition}
*/
const anchor = absolutePositionToRelativePosition(selection.anchor, ystate.type, ystate.binding.mapping);
const anchor = absolutePositionToRelativePosition(view.state.doc, selection.anchor, ystate.type, ystate.binding.mapping);
/**
* @type {Y.RelativePosition}
*/
const head = absolutePositionToRelativePosition(selection.head, ystate.type, ystate.binding.mapping);
const head = absolutePositionToRelativePosition(view.state.doc, selection.head, ystate.type, ystate.binding.mapping);
if (current.cursor == null || !compareRelativePositions(createRelativePositionFromJSON(current.cursor.anchor), anchor) || !compareRelativePositions(createRelativePositionFromJSON(current.cursor.head), head)) {
awareness.setLocalStateField(cursorStateField, {
anchor, head
Expand Down
2 changes: 1 addition & 1 deletion demo/dist/prosemirror.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/src/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function yDocToProsemirror(schema: Schema, ydoc: any): Node;
*/
export function yDocToProsemirrorJSON(ydoc: any, xmlFragment?: string): Record<string, any>;
export function setMeta(view: any, key: any, value: any): void;
export function absolutePositionToRelativePosition(pos: number, type: any, mapping: Map<any, Node<any> | Node<any>[]>): any;
export function absolutePositionToRelativePosition(doc: Node, pos: number, type: any, mapping: Map<any, Node<any> | Node<any>[]>): any;
export function relativePositionToAbsolutePosition(y: any, documentType: any, relPos: any, mapping: Map<any, Node<any> | Node<any>[]>): null | number;
/**
* Either a node if type is YXmlElement or an Array of text nodes if YXmlText
Expand Down
9 changes: 5 additions & 4 deletions dist/test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9698,8 +9698,8 @@ const restoreRelativeSelection = (tr, relSel, binding) => {
};

const getRelativeSelection = (pmbinding, state) => ({
anchor: absolutePositionToRelativePosition(state.selection.anchor, pmbinding.type, pmbinding.mapping),
head: absolutePositionToRelativePosition(state.selection.head, pmbinding.type, pmbinding.mapping)
anchor: absolutePositionToRelativePosition(state.doc, state.selection.anchor, pmbinding.type, pmbinding.mapping),
head: absolutePositionToRelativePosition(state.doc, state.selection.head, pmbinding.type, pmbinding.mapping)
});

/**
Expand Down Expand Up @@ -10351,13 +10351,14 @@ const matchNodeName = (yElement, pNode) => !(pNode instanceof Array) && yElement
/**
* Transforms a Prosemirror based absolute position to a Yjs Cursor (relative position in the Yjs model).
*
* @param {Node} doc
* @param {number} pos
* @param {Y.XmlFragment} type
* @param {ProsemirrorMapping} mapping
* @return {any} relative position
*/
const absolutePositionToRelativePosition = (pos, type, mapping) => {
if (pos === 0) {
const absolutePositionToRelativePosition = (doc, pos, type, mapping) => {
if (pos <= prosemirrorState.Selection.atStart(doc).from) {
return createRelativePositionFromTypeIndex(type, 0)
}
let n = type._first === null ? null : /** @type {Y.ContentType} */ (type._first.content).type;
Expand Down
2 changes: 1 addition & 1 deletion dist/test.cjs.map

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions dist/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24035,8 +24035,8 @@
};

const getRelativeSelection = (pmbinding, state) => ({
anchor: absolutePositionToRelativePosition(state.selection.anchor, pmbinding.type, pmbinding.mapping),
head: absolutePositionToRelativePosition(state.selection.head, pmbinding.type, pmbinding.mapping)
anchor: absolutePositionToRelativePosition(state.doc, state.selection.anchor, pmbinding.type, pmbinding.mapping),
head: absolutePositionToRelativePosition(state.doc, state.selection.head, pmbinding.type, pmbinding.mapping)
});

/**
Expand Down Expand Up @@ -24688,13 +24688,14 @@
/**
* Transforms a Prosemirror based absolute position to a Yjs Cursor (relative position in the Yjs model).
*
* @param {Node} doc
* @param {number} pos
* @param {Y.XmlFragment} type
* @param {ProsemirrorMapping} mapping
* @return {any} relative position
*/
const absolutePositionToRelativePosition = (pos, type, mapping) => {
if (pos === 0) {
const absolutePositionToRelativePosition = (doc, pos, type, mapping) => {
if (pos <= Selection.atStart(doc).from) {
return createRelativePositionFromTypeIndex(type, 0)
}
let n = type._first === null ? null : /** @type {Y.ContentType} */ (type._first.content).type;
Expand Down
2 changes: 1 addition & 1 deletion dist/test.js.map

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions dist/y-prosemirror.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ const restoreRelativeSelection = (tr, relSel, binding) => {
};

const getRelativeSelection = (pmbinding, state) => ({
anchor: absolutePositionToRelativePosition(state.selection.anchor, pmbinding.type, pmbinding.mapping),
head: absolutePositionToRelativePosition(state.selection.head, pmbinding.type, pmbinding.mapping)
anchor: absolutePositionToRelativePosition(state.doc, state.selection.anchor, pmbinding.type, pmbinding.mapping),
head: absolutePositionToRelativePosition(state.doc, state.selection.head, pmbinding.type, pmbinding.mapping)
});

/**
Expand Down Expand Up @@ -874,14 +874,15 @@ const setMeta = (view, key, value) => {
/**
* Transforms a Prosemirror based absolute position to a Yjs Cursor (relative position in the Yjs model).
*
* @param {Node} doc
* @param {number} pos
* @param {Y.XmlFragment} type
* @param {ProsemirrorMapping} mapping
* @return {any} relative position
*/
const absolutePositionToRelativePosition = (pos, type, mapping) => {
if (pos === 0) {
return Y.createRelativePositionFromTypeIndex(type, 0)
const absolutePositionToRelativePosition = (doc, pos, type, mapping) => {
if (pos <= prosemirrorState.Selection.atStart(doc).from) {
return Y.createRelativePositionFromTypeIndex(type, 0, -1)
}
let n = type._first === null ? null : /** @type {Y.ContentType} */ (type._first.content).type;
while (n !== null && type !== n) {
Expand Down Expand Up @@ -1245,11 +1246,11 @@ const yCursorPlugin = (awareness, { cursorBuilder = defaultCursorBuilder, getSel
/**
* @type {Y.RelativePosition}
*/
const anchor = absolutePositionToRelativePosition(selection.anchor, ystate.type, ystate.binding.mapping);
const anchor = absolutePositionToRelativePosition(view.state.doc, selection.anchor, ystate.type, ystate.binding.mapping);
/**
* @type {Y.RelativePosition}
*/
const head = absolutePositionToRelativePosition(selection.head, ystate.type, ystate.binding.mapping);
const head = absolutePositionToRelativePosition(view.state.doc, 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(cursorStateField, {
anchor, head
Expand Down
2 changes: 1 addition & 1 deletion dist/y-prosemirror.cjs.map

Large diffs are not rendered by default.

Loading

0 comments on commit b70d6f1

Please sign in to comment.