Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default-selection-autofocus #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { updateYFragment} from './plugins/sync-plugin.js' // eslint-disable-line
import * as Y from 'yjs'
import { EditorView } from 'prosemirror-view' // eslint-disable-line
import { Node, Schema } from 'prosemirror-model' // eslint-disable-line
import { Selection } from 'prosemirror-state' // eslint-disable-line
import * as error from 'lib0/error.js'
import * as map from 'lib0/map.js'
import * as eventloop from 'lib0/eventloop.js'
Expand Down Expand Up @@ -47,9 +48,9 @@ export const setMeta = (view, key, value) => {
* @param {ProsemirrorMapping} mapping
* @return {any} relative position
*/
export const absolutePositionToRelativePosition = (pos, type, mapping) => {
if (pos === 0) {
return Y.createRelativePositionFromTypeIndex(type, 0)
export const absolutePositionToRelativePosition = (doc, pos, type, mapping) => {
if (pos <= 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
4 changes: 2 additions & 2 deletions src/plugins/cursor-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ export const yCursorPlugin = (awareness, { cursorBuilder = defaultCursorBuilder,
/**
* @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
4 changes: 2 additions & 2 deletions src/plugins/sync-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ const restoreRelativeSelection = (tr, relSel, binding) => {
}

export 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