Compare commits

..

1 Commits

Author SHA1 Message Date
Kevin Jahns
d50d408cdd v13.0.0-61 -- distribution files 2018-05-18 02:03:20 +02:00
18 changed files with 19554 additions and 498 deletions

View File

@@ -20,7 +20,7 @@ let quill = new Quill('#quill-container', {
[{ header: [1, 2, false] }], [{ header: [1, 2, false] }],
['bold', 'italic', 'underline'], ['bold', 'italic', 'underline'],
['image', 'code-block'], ['image', 'code-block'],
[{ color: [] }, { background: [] }], // Snow theme fills in values [{ color: [] }, { background: [] }], // Snow theme fills in values
[{ script: 'sub' }, { script: 'super' }], [{ script: 'sub' }, { script: 'super' }],
['link', 'image'], ['link', 'image'],
['link', 'code-block'], ['link', 'code-block'],
@@ -31,7 +31,7 @@ let quill = new Quill('#quill-container', {
} }
}, },
placeholder: 'Compose an epic...', placeholder: 'Compose an epic...',
theme: 'snow' // or 'bubble' theme: 'snow' // or 'bubble'
}) })
let cursors = quill.getModule('cursors') let cursors = quill.getModule('cursors')

View File

@@ -13,7 +13,7 @@ let quill = new Quill('#quill-container', {
[{ header: [1, 2, false] }], [{ header: [1, 2, false] }],
['bold', 'italic', 'underline'], ['bold', 'italic', 'underline'],
['image', 'code-block'], ['image', 'code-block'],
[{ color: [] }, { background: [] }], // Snow theme fills in values [{ color: [] }, { background: [] }], // Snow theme fills in values
[{ script: 'sub' }, { script: 'super' }], [{ script: 'sub' }, { script: 'super' }],
['link', 'image'], ['link', 'image'],
['link', 'code-block'], ['link', 'code-block'],
@@ -21,7 +21,7 @@ let quill = new Quill('#quill-container', {
] ]
}, },
placeholder: 'Compose an epic...', placeholder: 'Compose an epic...',
theme: 'snow' // or 'bubble' theme: 'snow' // or 'bubble'
}) })
let yText = y.define('quill', Y.Text) let yText = y.define('quill', Y.Text)

View File

@@ -35,7 +35,7 @@ Y({
toolbar: [ toolbar: [
[{ size: ['small', false, 'large', 'huge'] }], [{ size: ['small', false, 'large', 'huge'] }],
['bold', 'italic', 'underline'], ['bold', 'italic', 'underline'],
[{ color: [] }, { background: [] }], // Snow theme fills in values [{ color: [] }, { background: [] }], // Snow theme fills in values
[{ script: 'sub' }, { script: 'super' }], [{ script: 'sub' }, { script: 'super' }],
['link', 'image'], ['link', 'image'],
['link', 'code-block'], ['link', 'code-block'],

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "yjs", "name": "yjs",
"version": "13.0.0-65", "version": "13.0.0-61",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -1,6 +1,6 @@
{ {
"name": "yjs", "name": "yjs",
"version": "13.0.0-65", "version": "13.0.0-61",
"description": "A framework for real-time p2p shared editing on any data", "description": "A framework for real-time p2p shared editing on any data",
"main": "./y.node.js", "main": "./y.node.js",
"browser": "./y.js", "browser": "./y.js",

View File

@@ -1,9 +1,8 @@
/* global MutationObserver, getSelection */ /* global MutationObserver */
import { fromRelativePosition } from '../../Util/relativePosition.js'
import Binding from '../Binding.js' import Binding from '../Binding.js'
import { createAssociation, removeAssociation } from './util.js' import { createAssociation, removeAssociation } from './util.js'
import { beforeTransactionSelectionFixer, afterTransactionSelectionFixer, getCurrentRelativeSelection } from './selection.js' import { beforeTransactionSelectionFixer, afterTransactionSelectionFixer } from './selection.js'
import { defaultFilter, applyFilterOnType } from './filter.js' import { defaultFilter, applyFilterOnType } from './filter.js'
import typeObserver from './typeObserver.js' import typeObserver from './typeObserver.js'
import domObserver from './domObserver.js' import domObserver from './domObserver.js'
@@ -68,25 +67,16 @@ export default class DomBinding extends Binding {
characterData: true, characterData: true,
subtree: true subtree: true
}) })
this._currentSel = null
document.addEventListener('selectionchange', () => {
this._currentSel = getCurrentRelativeSelection(this)
})
const y = type._y const y = type._y
this.y = y
// Force flush dom changes before Type changes are applied (they might // Force flush dom changes before Type changes are applied (they might
// modify the dom) // modify the dom)
this._beforeTransactionHandler = (y, transaction, remote) => { this._beforeTransactionHandler = (y, transaction, remote) => {
this._domObserver(this._mutationObserver.takeRecords()) this._domObserver(this._mutationObserver.takeRecords())
this._mutualExclude(() => { beforeTransactionSelectionFixer(y, this, transaction, remote)
beforeTransactionSelectionFixer(this, remote)
})
} }
y.on('beforeTransaction', this._beforeTransactionHandler) y.on('beforeTransaction', this._beforeTransactionHandler)
this._afterTransactionHandler = (y, transaction, remote) => { this._afterTransactionHandler = (y, transaction, remote) => {
this._mutualExclude(() => { afterTransactionSelectionFixer(y, this, transaction, remote)
afterTransactionSelectionFixer(this, remote)
})
// remove associations // remove associations
// TODO: this could be done more efficiently // TODO: this could be done more efficiently
// e.g. Always delete using the following approach, or removeAssociation // e.g. Always delete using the following approach, or removeAssociation
@@ -125,62 +115,6 @@ export default class DomBinding extends Binding {
// TODO: apply filter to all elements // TODO: apply filter to all elements
} }
_getUndoStackInfo () {
return this.getSelection()
}
_restoreUndoStackInfo (info) {
this.restoreSelection(info)
}
getSelection () {
return this._currentSel
}
restoreSelection (selection) {
if (selection !== null) {
const { to, from } = selection
/**
* There is little information on the difference between anchor/focus and base/extent.
* MDN doesn't even mention base/extent anymore.. though you still have to call
* setBaseAndExtent to change the selection..
* I can observe that base/extend refer to notes higher up in the xml hierachy.
* Espesially for undo/redo this is preferred. If this becomes a problem in the future,
* we should probably go back to anchor/focus.
*/
const browserSelection = getSelection()
let { baseNode, baseOffset, extentNode, extentOffset } = browserSelection
if (from !== null) {
let sel = fromRelativePosition(this.y, from)
if (sel !== null) {
let node = this.typeToDom.get(sel.type)
let offset = sel.offset
if (node !== baseNode || offset !== baseOffset) {
baseNode = node
baseOffset = offset
}
}
}
if (to !== null) {
let sel = fromRelativePosition(this.y, to)
if (sel !== null) {
let node = this.typeToDom.get(sel.type)
let offset = sel.offset
if (node !== extentNode || offset !== extentOffset) {
extentNode = node
extentOffset = offset
}
}
}
browserSelection.setBaseAndExtent(
baseNode,
baseOffset,
extentNode,
extentOffset
)
}
}
/** /**
* Remove all properties that are handled by this class. * Remove all properties that are handled by this class.
*/ */
@@ -196,10 +130,11 @@ export default class DomBinding extends Binding {
super.destroy() super.destroy()
} }
} }
/**
* A filter defines which elements and attributes to share. /**
* Return null if the node should be filtered. Otherwise return the Map of * A filter defines which elements and attributes to share.
* accepted attributes. * Return null if the node should be filtered. Otherwise return the Map of
* * accepted attributes.
* @typedef {function(nodeName: String, attrs: Map): Map|null} FilterFunction *
*/ * @typedef {function(nodeName: String, attrs: Map): Map|null} FilterFunction
*/

View File

@@ -1,35 +1,84 @@
/* globals getSelection */ /* globals getSelection */
import { getRelativePosition } from '../../Util/relativePosition.js' import { getRelativePosition, fromRelativePosition } from '../../Util/relativePosition.js'
let browserSelection = null
let relativeSelection = null let relativeSelection = null
function _getCurrentRelativeSelection (domBinding) { /**
const { baseNode, baseOffset, extentNode, extentOffset } = getSelection() * @private
const baseNodeType = domBinding.domToType.get(baseNode) */
const extentNodeType = domBinding.domToType.get(extentNode) export let beforeTransactionSelectionFixer
if (baseNodeType !== undefined && extentNodeType !== undefined) { if (typeof getSelection !== 'undefined') {
return { beforeTransactionSelectionFixer = function _beforeTransactionSelectionFixer (y, domBinding, transaction, remote) {
from: getRelativePosition(baseNodeType, baseOffset), if (!remote) {
to: getRelativePosition(extentNodeType, extentOffset) return
}
relativeSelection = { from: null, to: null, fromY: null, toY: null }
browserSelection = getSelection()
const anchorNode = browserSelection.anchorNode
const anchorNodeType = domBinding.domToType.get(anchorNode)
if (anchorNode !== null && anchorNodeType !== undefined) {
relativeSelection.from = getRelativePosition(anchorNodeType, browserSelection.anchorOffset)
relativeSelection.fromY = anchorNodeType._y
}
const focusNode = browserSelection.focusNode
const focusNodeType = domBinding.domToType.get(focusNode)
if (focusNode !== null && focusNodeType !== undefined) {
relativeSelection.to = getRelativePosition(focusNodeType, browserSelection.focusOffset)
relativeSelection.toY = focusNodeType._y
} }
} }
return null } else {
} beforeTransactionSelectionFixer = function _fakeBeforeTransactionSelectionFixer () {}
export const getCurrentRelativeSelection = typeof getSelection !== 'undefined' ? _getCurrentRelativeSelection : () => null
export function beforeTransactionSelectionFixer (domBinding, remote) {
if (remote) {
relativeSelection = getCurrentRelativeSelection(domBinding)
}
} }
/** /**
* @private * @private
*/ */
export function afterTransactionSelectionFixer (domBinding, remote) { export function afterTransactionSelectionFixer (y, domBinding, transaction, remote) {
if (relativeSelection !== null && remote) { if (relativeSelection === null || !remote) {
domBinding.restoreSelection(relativeSelection) return
}
const to = relativeSelection.to
const from = relativeSelection.from
const fromY = relativeSelection.fromY
const toY = relativeSelection.toY
let shouldUpdate = false
let anchorNode = browserSelection.anchorNode
let anchorOffset = browserSelection.anchorOffset
let focusNode = browserSelection.focusNode
let focusOffset = browserSelection.focusOffset
if (from !== null) {
let sel = fromRelativePosition(fromY, from)
if (sel !== null) {
let node = domBinding.typeToDom.get(sel.type)
let offset = sel.offset
if (node !== anchorNode || offset !== anchorOffset) {
anchorNode = node
anchorOffset = offset
shouldUpdate = true
}
}
}
if (to !== null) {
let sel = fromRelativePosition(toY, to)
if (sel !== null) {
let node = domBinding.typeToDom.get(sel.type)
let offset = sel.offset
if (node !== focusNode || offset !== focusOffset) {
focusNode = node
focusOffset = offset
shouldUpdate = true
}
}
}
if (shouldUpdate) {
browserSelection.setBaseAndExtent(
anchorNode,
anchorOffset,
focusNode,
focusOffset
)
} }
} }

View File

@@ -87,7 +87,7 @@ export default class Item {
* If the parent refers to this item with some kind of key (e.g. YMap, the * If the parent refers to this item with some kind of key (e.g. YMap, the
* key is specified here. The key is then used to refer to the list in which * key is specified here. The key is then used to refer to the list in which
* to insert this item. If `parentSub = null` type._start is the list in * to insert this item. If `parentSub = null` type._start is the list in
* which to insert to. Otherwise it is `parent._map`. * which to insert to. Otherwise it is `parent._start`.
* @type {String} * @type {String}
*/ */
this._parentSub = null this._parentSub = null
@@ -120,29 +120,17 @@ export default class Item {
* *
* @private * @private
*/ */
_redo (y, redoitems) { _redo (y) {
if (this._redone !== null) { if (this._redone !== null) {
return this._redone return this._redone
} }
let struct = this._copy() let struct = this._copy()
let left, right let left = this._left
if (this._parentSub === null) { let right = this
// Is an array item. Insert at the old position
left = this._left
right = this
} else {
// Is a map item. Insert at the start
left = null
right = this._parent._map.get(this._parentSub)
right._delete(y)
}
let parent = this._parent let parent = this._parent
// make sure that parent is redone // make sure that parent is redone
if (parent._deleted === true && parent._redone === null) { if (parent._deleted === true && parent._redone === null) {
// try to undo parent if it will be undone anyway parent._redo(y)
if (!redoitems.has(parent) || !parent._redo(y, redoitems)) {
return false
}
} }
if (parent._redone !== null) { if (parent._redone !== null) {
parent = parent._redone parent = parent._redone
@@ -169,7 +157,7 @@ export default class Item {
struct._parentSub = this._parentSub struct._parentSub = this._parentSub
struct._integrate(y) struct._integrate(y)
this._redone = struct this._redone = struct
return true return struct
} }
/** /**

View File

@@ -254,7 +254,7 @@ function deleteText (y, length, parent, left, right, currentAttributes) {
* @typedef {Array<Object>} Delta * @typedef {Array<Object>} Delta
*/ */
/** /**
* Attributes that can be assigned to a selection of text. * Attributes that can be assigned to a selection of text.
* *
* @example * @example

View File

@@ -2,7 +2,7 @@ import ID from './ID/ID.js'
import isParentOf from './isParentOf.js' import isParentOf from './isParentOf.js'
class ReverseOperation { class ReverseOperation {
constructor (y, transaction, bindingInfos) { constructor (y, transaction) {
this.created = new Date() this.created = new Date()
const beforeState = transaction.beforeState const beforeState = transaction.beforeState
if (beforeState.has(y.userID)) { if (beforeState.has(y.userID)) {
@@ -12,26 +12,15 @@ class ReverseOperation {
this.toState = null this.toState = null
this.fromState = null this.fromState = null
} }
this.deletedStructs = new Set() this.deletedStructs = transaction.deletedStructs
transaction.deletedStructs.forEach(struct => {
this.deletedStructs.add({
from: struct._id,
len: struct._length
})
})
/**
* Maps from binding to binding information (e.g. cursor information)
*/
this.bindingInfos = bindingInfos
} }
} }
function applyReverseOperation (y, scope, reverseBuffer) { function applyReverseOperation (y, scope, reverseBuffer) {
let performedUndo = false let performedUndo = false
let undoOp
y.transact(() => { y.transact(() => {
while (!performedUndo && reverseBuffer.length > 0) { while (!performedUndo && reverseBuffer.length > 0) {
undoOp = reverseBuffer.pop() let undoOp = reverseBuffer.pop()
// make sure that it is possible to iterate {from}-{to} // make sure that it is possible to iterate {from}-{to}
if (undoOp.fromState !== null) { if (undoOp.fromState !== null) {
y.os.getItemCleanStart(undoOp.fromState) y.os.getItemCleanStart(undoOp.fromState)
@@ -46,39 +35,23 @@ function applyReverseOperation (y, scope, reverseBuffer) {
} }
}) })
} }
const redoitems = new Set() for (let op of undoOp.deletedStructs) {
for (let del of undoOp.deletedStructs) { if (
const fromState = del.from isParentOf(scope, op) &&
const toState = new ID(fromState.user, fromState.clock + del.len - 1) op._parent !== y &&
y.os.getItemCleanStart(fromState) (
y.os.getItemCleanEnd(toState) op._id.user !== y.userID ||
y.os.iterate(fromState, toState, op => { undoOp.fromState === null ||
if ( op._id.clock < undoOp.fromState.clock ||
isParentOf(scope, op) && op._id.clock > undoOp.toState.clock
op._parent !== y && )
( ) {
op._id.user !== y.userID || performedUndo = true
undoOp.fromState === null || op._redo(y)
op._id.clock < undoOp.fromState.clock || }
op._id.clock > undoOp.toState.clock
)
) {
redoitems.add(op)
}
})
} }
redoitems.forEach(op => {
const opUndone = op._redo(y, redoitems)
performedUndo = performedUndo || opUndone
})
} }
}) })
if (performedUndo) {
// should be performed after the undo transaction
undoOp.bindingInfos.forEach((info, binding) => {
binding._restoreUndoStackInfo(info)
})
}
return performedUndo return performedUndo
} }
@@ -93,7 +66,6 @@ export default class UndoManager {
*/ */
constructor (scope, options = {}) { constructor (scope, options = {}) {
this.options = options this.options = options
this._bindings = new Set(options.bindings)
options.captureTimeout = options.captureTimeout == null ? 500 : options.captureTimeout options.captureTimeout = options.captureTimeout == null ? 500 : options.captureTimeout
this._undoBuffer = [] this._undoBuffer = []
this._redoBuffer = [] this._redoBuffer = []
@@ -104,28 +76,16 @@ export default class UndoManager {
const y = scope._y const y = scope._y
this.y = y this.y = y
y._hasUndoManager = true y._hasUndoManager = true
let bindingInfos
y.on('beforeTransaction', (y, transaction, remote) => {
if (!remote) {
// Store binding information before transaction is executed
// By restoring the binding information, we can make sure that the state
// before the transaction can be recovered
bindingInfos = new Map()
this._bindings.forEach(binding => {
bindingInfos.set(binding, binding._getUndoStackInfo())
})
}
})
y.on('afterTransaction', (y, transaction, remote) => { y.on('afterTransaction', (y, transaction, remote) => {
if (!remote && transaction.changedParentTypes.has(scope)) { if (!remote && transaction.changedParentTypes.has(scope)) {
let reverseOperation = new ReverseOperation(y, transaction, bindingInfos) let reverseOperation = new ReverseOperation(y, transaction)
if (!this._undoing) { if (!this._undoing) {
let lastUndoOp = this._undoBuffer.length > 0 ? this._undoBuffer[this._undoBuffer.length - 1] : null let lastUndoOp = this._undoBuffer.length > 0 ? this._undoBuffer[this._undoBuffer.length - 1] : null
if ( if (
this._redoing === false && this._redoing === false &&
this._lastTransactionWasUndo === false && this._lastTransactionWasUndo === false &&
lastUndoOp !== null && lastUndoOp !== null &&
(options.captureTimeout < 0 || reverseOperation.created - lastUndoOp.created <= options.captureTimeout) reverseOperation.created - lastUndoOp.created <= options.captureTimeout
) { ) {
lastUndoOp.created = reverseOperation.created lastUndoOp.created = reverseOperation.created
if (reverseOperation.toState !== null) { if (reverseOperation.toState !== null) {
@@ -150,13 +110,6 @@ export default class UndoManager {
}) })
} }
/**
* Enforce that the next change is created as a separate item in the undo stack
*/
flushChanges () {
this._lastTransactionWasUndo = true
}
/** /**
* Undo the last locally created change. * Undo the last locally created change.
*/ */

View File

@@ -76,10 +76,7 @@ export function fromRelativePosition (y, rpos) {
} else { } else {
id = new RootID(rpos[3], rpos[4]) id = new RootID(rpos[3], rpos[4])
} }
let type = y.os.get(id) const type = y.os.get(id)
while (type._redone !== null) {
type = type._redone
}
if (type === null || type.constructor === GC) { if (type === null || type.constructor === GC) {
return null return null
} }
@@ -90,16 +87,12 @@ export function fromRelativePosition (y, rpos) {
} else { } else {
let offset = 0 let offset = 0
let struct = y.os.findNodeWithUpperBound(new ID(rpos[0], rpos[1])).val let struct = y.os.findNodeWithUpperBound(new ID(rpos[0], rpos[1])).val
const diff = rpos[1] - struct._id.clock
while (struct._redone !== null) {
struct = struct._redone
}
const parent = struct._parent const parent = struct._parent
if (struct.constructor === GC || parent._deleted) { if (struct.constructor === GC || parent._deleted) {
return null return null
} }
if (!struct._deleted) { if (!struct._deleted) {
offset = diff offset = rpos[1] - struct._id.clock
} }
struct = struct._left struct = struct._left
while (struct !== null) { while (struct !== null) {

View File

@@ -111,7 +111,7 @@ function compareEvent (t, is, should) {
t.assert( t.assert(
should[key] === is[key] || should[key] === is[key] ||
JSON.stringify(should[key]) === JSON.stringify(is[key]) JSON.stringify(should[key]) === JSON.stringify(is[key])
, 'event works as expected' , 'event works as expected'
) )
} }
} }

8
y.js

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

503
y.node.js
View File

@@ -1,7 +1,7 @@
/** /**
* yjs - A framework for real-time p2p shared editing on any data * yjs - A framework for real-time p2p shared editing on any data
* @version v13.0.0-65 * @version v13.0.0-61
* @license MIT * @license MIT
*/ */
@@ -1644,7 +1644,7 @@ class Item {
* If the parent refers to this item with some kind of key (e.g. YMap, the * If the parent refers to this item with some kind of key (e.g. YMap, the
* key is specified here. The key is then used to refer to the list in which * key is specified here. The key is then used to refer to the list in which
* to insert this item. If `parentSub = null` type._start is the list in * to insert this item. If `parentSub = null` type._start is the list in
* which to insert to. Otherwise it is `parent._map`. * which to insert to. Otherwise it is `parent._start`.
* @type {String} * @type {String}
*/ */
this._parentSub = null; this._parentSub = null;
@@ -1677,29 +1677,17 @@ class Item {
* *
* @private * @private
*/ */
_redo (y, redoitems) { _redo (y) {
if (this._redone !== null) { if (this._redone !== null) {
return this._redone return this._redone
} }
let struct = this._copy(); let struct = this._copy();
let left, right; let left = this._left;
if (this._parentSub === null) { let right = this;
// Is an array item. Insert at the old position
left = this._left;
right = this;
} else {
// Is a map item. Insert at the start
left = null;
right = this._parent._map.get(this._parentSub);
right._delete(y);
}
let parent = this._parent; let parent = this._parent;
// make sure that parent is redone // make sure that parent is redone
if (parent._deleted === true && parent._redone === null) { if (parent._deleted === true && parent._redone === null) {
// try to undo parent if it will be undone anyway parent._redo(y);
if (!redoitems.has(parent) || !parent._redo(y, redoitems)) {
return false
}
} }
if (parent._redone !== null) { if (parent._redone !== null) {
parent = parent._redone; parent = parent._redone;
@@ -1726,7 +1714,7 @@ class Item {
struct._parentSub = this._parentSub; struct._parentSub = this._parentSub;
struct._integrate(y); struct._integrate(y);
this._redone = struct; this._redone = struct;
return true return struct
} }
/** /**
@@ -3398,7 +3386,7 @@ function deleteText (y, length, parent, left, right, currentAttributes) {
* @typedef {Array<Object>} Delta * @typedef {Array<Object>} Delta
*/ */
/** /**
* Attributes that can be assigned to a selection of text. * Attributes that can be assigned to a selection of text.
* *
* @example * @example
@@ -5007,119 +4995,6 @@ class NamedEventHandler {
} }
} }
// TODO: Implement function to describe ranges
/**
* A relative position that is based on the Yjs model. In contrast to an
* absolute position (position by index), the relative position can be
* recomputed when remote changes are received. For example:
*
* ```Insert(0, 'x')('a|bc') = 'xa|bc'``` Where | is the cursor position.
*
* A relative cursor position can be obtained with the function
* {@link getRelativePosition} and it can be transformed to an absolute position
* with {@link fromRelativePosition}.
*
* Pro tip: Use this to implement shared cursor locations in YText or YXml!
* The relative position is {@link encodable}, so you can send it to other
* clients.
*
* @example
* // Current cursor position is at position 10
* let relativePosition = getRelativePosition(yText, 10)
* // modify yText
* yText.insert(0, 'abc')
* yText.delete(3, 10)
* // Compute the cursor position
* let absolutePosition = fromRelativePosition(y, relativePosition)
* absolutePosition.type // => yText
* console.log('cursor location is ' + absolutePosition.offset) // => cursor location is 3
*
* @typedef {encodable} RelativePosition
*/
/**
* Create a relativePosition based on a absolute position.
*
* @param {YType} type The base type (e.g. YText or YArray).
* @param {Integer} offset The absolute position.
*/
function getRelativePosition (type, offset) {
// TODO: rename to createRelativePosition
let t = type._start;
while (t !== null) {
if (t._deleted === false) {
if (t._length > offset) {
return [t._id.user, t._id.clock + offset]
}
offset -= t._length;
}
t = t._right;
}
return ['endof', type._id.user, type._id.clock || null, type._id.name || null, type._id.type || null]
}
/**
* @typedef {Object} AbsolutePosition The result of {@link fromRelativePosition}
* @property {YType} type The type on which to apply the absolute position.
* @property {Integer} offset The absolute offset.r
*/
/**
* Transforms a relative position back to a relative position.
*
* @param {Y} y The Yjs instance in which to query for the absolute position.
* @param {RelativePosition} rpos The relative position.
* @return {AbsolutePosition} The absolute position in the Yjs model
* (type + offset).
*/
function fromRelativePosition (y, rpos) {
if (rpos[0] === 'endof') {
let id;
if (rpos[3] === null) {
id = new ID(rpos[1], rpos[2]);
} else {
id = new RootID(rpos[3], rpos[4]);
}
let type = y.os.get(id);
while (type._redone !== null) {
type = type._redone;
}
if (type === null || type.constructor === GC) {
return null
}
return {
type,
offset: type.length
}
} else {
let offset = 0;
let struct = y.os.findNodeWithUpperBound(new ID(rpos[0], rpos[1])).val;
const diff = rpos[1] - struct._id.clock;
while (struct._redone !== null) {
struct = struct._redone;
}
const parent = struct._parent;
if (struct.constructor === GC || parent._deleted) {
return null
}
if (!struct._deleted) {
offset = diff;
}
struct = struct._left;
while (struct !== null) {
if (!struct._deleted) {
offset += struct._length;
}
struct = struct._left;
}
return {
type: parent,
offset: offset
}
}
}
// TODO: rename mutex // TODO: rename mutex
/** /**
@@ -5198,37 +5073,192 @@ class Binding {
} }
} }
/* globals getSelection */ // TODO: Implement function to describe ranges
let relativeSelection = null; /**
* A relative position that is based on the Yjs model. In contrast to an
* absolute position (position by index), the relative position can be
* recomputed when remote changes are received. For example:
*
* ```Insert(0, 'x')('a|bc') = 'xa|bc'``` Where | is the cursor position.
*
* A relative cursor position can be obtained with the function
* {@link getRelativePosition} and it can be transformed to an absolute position
* with {@link fromRelativePosition}.
*
* Pro tip: Use this to implement shared cursor locations in YText or YXml!
* The relative position is {@link encodable}, so you can send it to other
* clients.
*
* @example
* // Current cursor position is at position 10
* let relativePosition = getRelativePosition(yText, 10)
* // modify yText
* yText.insert(0, 'abc')
* yText.delete(3, 10)
* // Compute the cursor position
* let absolutePosition = fromRelativePosition(y, relativePosition)
* absolutePosition.type // => yText
* console.log('cursor location is ' + absolutePosition.offset) // => cursor location is 3
*
* @typedef {encodable} RelativePosition
*/
function _getCurrentRelativeSelection (domBinding) { /**
const { baseNode, baseOffset, extentNode, extentOffset } = getSelection(); * Create a relativePosition based on a absolute position.
const baseNodeType = domBinding.domToType.get(baseNode); *
const extentNodeType = domBinding.domToType.get(extentNode); * @param {YType} type The base type (e.g. YText or YArray).
if (baseNodeType !== undefined && extentNodeType !== undefined) { * @param {Integer} offset The absolute position.
return { */
from: getRelativePosition(baseNodeType, baseOffset), function getRelativePosition (type, offset) {
to: getRelativePosition(extentNodeType, extentOffset) // TODO: rename to createRelativePosition
let t = type._start;
while (t !== null) {
if (t._deleted === false) {
if (t._length > offset) {
return [t._id.user, t._id.clock + offset]
}
offset -= t._length;
} }
t = t._right;
} }
return null return ['endof', type._id.user, type._id.clock || null, type._id.name || null, type._id.type || null]
} }
const getCurrentRelativeSelection = typeof getSelection !== 'undefined' ? _getCurrentRelativeSelection : () => null; /**
* @typedef {Object} AbsolutePosition The result of {@link fromRelativePosition}
* @property {YType} type The type on which to apply the absolute position.
* @property {Integer} offset The absolute offset.r
*/
function beforeTransactionSelectionFixer (domBinding, remote) { /**
if (remote) { * Transforms a relative position back to a relative position.
relativeSelection = getCurrentRelativeSelection(domBinding); *
* @param {Y} y The Yjs instance in which to query for the absolute position.
* @param {RelativePosition} rpos The relative position.
* @return {AbsolutePosition} The absolute position in the Yjs model
* (type + offset).
*/
function fromRelativePosition (y, rpos) {
if (rpos[0] === 'endof') {
let id;
if (rpos[3] === null) {
id = new ID(rpos[1], rpos[2]);
} else {
id = new RootID(rpos[3], rpos[4]);
}
const type = y.os.get(id);
if (type === null || type.constructor === GC) {
return null
}
return {
type,
offset: type.length
}
} else {
let offset = 0;
let struct = y.os.findNodeWithUpperBound(new ID(rpos[0], rpos[1])).val;
const parent = struct._parent;
if (struct.constructor === GC || parent._deleted) {
return null
}
if (!struct._deleted) {
offset = rpos[1] - struct._id.clock;
}
struct = struct._left;
while (struct !== null) {
if (!struct._deleted) {
offset += struct._length;
}
struct = struct._left;
}
return {
type: parent,
offset: offset
}
} }
} }
/* globals getSelection */
let browserSelection = null;
let relativeSelection = null;
/**
* @private
*/
let beforeTransactionSelectionFixer;
if (typeof getSelection !== 'undefined') {
beforeTransactionSelectionFixer = function _beforeTransactionSelectionFixer (y, domBinding, transaction, remote) {
if (!remote) {
return
}
relativeSelection = { from: null, to: null, fromY: null, toY: null };
browserSelection = getSelection();
const anchorNode = browserSelection.anchorNode;
const anchorNodeType = domBinding.domToType.get(anchorNode);
if (anchorNode !== null && anchorNodeType !== undefined) {
relativeSelection.from = getRelativePosition(anchorNodeType, browserSelection.anchorOffset);
relativeSelection.fromY = anchorNodeType._y;
}
const focusNode = browserSelection.focusNode;
const focusNodeType = domBinding.domToType.get(focusNode);
if (focusNode !== null && focusNodeType !== undefined) {
relativeSelection.to = getRelativePosition(focusNodeType, browserSelection.focusOffset);
relativeSelection.toY = focusNodeType._y;
}
};
} else {
beforeTransactionSelectionFixer = function _fakeBeforeTransactionSelectionFixer () {};
}
/** /**
* @private * @private
*/ */
function afterTransactionSelectionFixer (domBinding, remote) { function afterTransactionSelectionFixer (y, domBinding, transaction, remote) {
if (relativeSelection !== null && remote) { if (relativeSelection === null || !remote) {
domBinding.restoreSelection(relativeSelection); return
}
const to = relativeSelection.to;
const from = relativeSelection.from;
const fromY = relativeSelection.fromY;
const toY = relativeSelection.toY;
let shouldUpdate = false;
let anchorNode = browserSelection.anchorNode;
let anchorOffset = browserSelection.anchorOffset;
let focusNode = browserSelection.focusNode;
let focusOffset = browserSelection.focusOffset;
if (from !== null) {
let sel = fromRelativePosition(fromY, from);
if (sel !== null) {
let node = domBinding.typeToDom.get(sel.type);
let offset = sel.offset;
if (node !== anchorNode || offset !== anchorOffset) {
anchorNode = node;
anchorOffset = offset;
shouldUpdate = true;
}
}
}
if (to !== null) {
let sel = fromRelativePosition(toY, to);
if (sel !== null) {
let node = domBinding.typeToDom.get(sel.type);
let offset = sel.offset;
if (node !== focusNode || offset !== focusOffset) {
focusNode = node;
focusOffset = offset;
shouldUpdate = true;
}
}
}
if (shouldUpdate) {
browserSelection.setBaseAndExtent(
anchorNode,
anchorOffset,
focusNode,
focusOffset
);
} }
} }
@@ -5511,7 +5541,7 @@ function domObserver (mutations, _document) {
}); });
} }
/* global MutationObserver, getSelection */ /* global MutationObserver */
/** /**
* A binding that binds the children of a YXmlFragment to a DOM element. * A binding that binds the children of a YXmlFragment to a DOM element.
@@ -5573,25 +5603,16 @@ class DomBinding extends Binding {
characterData: true, characterData: true,
subtree: true subtree: true
}); });
this._currentSel = null;
document.addEventListener('selectionchange', () => {
this._currentSel = getCurrentRelativeSelection(this);
});
const y = type._y; const y = type._y;
this.y = y;
// Force flush dom changes before Type changes are applied (they might // Force flush dom changes before Type changes are applied (they might
// modify the dom) // modify the dom)
this._beforeTransactionHandler = (y, transaction, remote) => { this._beforeTransactionHandler = (y, transaction, remote) => {
this._domObserver(this._mutationObserver.takeRecords()); this._domObserver(this._mutationObserver.takeRecords());
this._mutualExclude(() => { beforeTransactionSelectionFixer(y, this, transaction, remote);
beforeTransactionSelectionFixer(this, remote);
});
}; };
y.on('beforeTransaction', this._beforeTransactionHandler); y.on('beforeTransaction', this._beforeTransactionHandler);
this._afterTransactionHandler = (y, transaction, remote) => { this._afterTransactionHandler = (y, transaction, remote) => {
this._mutualExclude(() => { afterTransactionSelectionFixer(y, this, transaction, remote);
afterTransactionSelectionFixer(this, remote);
});
// remove associations // remove associations
// TODO: this could be done more efficiently // TODO: this could be done more efficiently
// e.g. Always delete using the following approach, or removeAssociation // e.g. Always delete using the following approach, or removeAssociation
@@ -5630,62 +5651,6 @@ class DomBinding extends Binding {
// TODO: apply filter to all elements // TODO: apply filter to all elements
} }
_getUndoStackInfo () {
return this.getSelection()
}
_restoreUndoStackInfo (info) {
this.restoreSelection(info);
}
getSelection () {
return this._currentSel
}
restoreSelection (selection) {
if (selection !== null) {
const { to, from } = selection;
/**
* There is little information on the difference between anchor/focus and base/extent.
* MDN doesn't even mention base/extent anymore.. though you still have to call
* setBaseAndExtent to change the selection..
* I can observe that base/extend refer to notes higher up in the xml hierachy.
* Espesially for undo/redo this is preferred. If this becomes a problem in the future,
* we should probably go back to anchor/focus.
*/
const browserSelection = getSelection();
let { baseNode, baseOffset, extentNode, extentOffset } = browserSelection;
if (from !== null) {
let sel = fromRelativePosition(this.y, from);
if (sel !== null) {
let node = this.typeToDom.get(sel.type);
let offset = sel.offset;
if (node !== baseNode || offset !== baseOffset) {
baseNode = node;
baseOffset = offset;
}
}
}
if (to !== null) {
let sel = fromRelativePosition(this.y, to);
if (sel !== null) {
let node = this.typeToDom.get(sel.type);
let offset = sel.offset;
if (node !== extentNode || offset !== extentOffset) {
extentNode = node;
extentOffset = offset;
}
}
}
browserSelection.setBaseAndExtent(
baseNode,
baseOffset,
extentNode,
extentOffset
);
}
}
/** /**
* Remove all properties that are handled by this class. * Remove all properties that are handled by this class.
*/ */
@@ -5701,13 +5666,14 @@ class DomBinding extends Binding {
super.destroy(); super.destroy();
} }
} }
/**
* A filter defines which elements and attributes to share. /**
* Return null if the node should be filtered. Otherwise return the Map of * A filter defines which elements and attributes to share.
* accepted attributes. * Return null if the node should be filtered. Otherwise return the Map of
* * accepted attributes.
* @typedef {function(nodeName: String, attrs: Map): Map|null} FilterFunction *
*/ * @typedef {function(nodeName: String, attrs: Map): Map|null} FilterFunction
*/
/** /**
* Anything that can be encoded with `JSON.stringify` and can be decoded with * Anything that can be encoded with `JSON.stringify` and can be decoded with
@@ -5983,7 +5949,7 @@ Y.extend = function extendYjs () {
}; };
class ReverseOperation { class ReverseOperation {
constructor (y, transaction, bindingInfos) { constructor (y, transaction) {
this.created = new Date(); this.created = new Date();
const beforeState = transaction.beforeState; const beforeState = transaction.beforeState;
if (beforeState.has(y.userID)) { if (beforeState.has(y.userID)) {
@@ -5993,26 +5959,15 @@ class ReverseOperation {
this.toState = null; this.toState = null;
this.fromState = null; this.fromState = null;
} }
this.deletedStructs = new Set(); this.deletedStructs = transaction.deletedStructs;
transaction.deletedStructs.forEach(struct => {
this.deletedStructs.add({
from: struct._id,
len: struct._length
});
});
/**
* Maps from binding to binding information (e.g. cursor information)
*/
this.bindingInfos = bindingInfos;
} }
} }
function applyReverseOperation (y, scope, reverseBuffer) { function applyReverseOperation (y, scope, reverseBuffer) {
let performedUndo = false; let performedUndo = false;
let undoOp;
y.transact(() => { y.transact(() => {
while (!performedUndo && reverseBuffer.length > 0) { while (!performedUndo && reverseBuffer.length > 0) {
undoOp = reverseBuffer.pop(); let undoOp = reverseBuffer.pop();
// make sure that it is possible to iterate {from}-{to} // make sure that it is possible to iterate {from}-{to}
if (undoOp.fromState !== null) { if (undoOp.fromState !== null) {
y.os.getItemCleanStart(undoOp.fromState); y.os.getItemCleanStart(undoOp.fromState);
@@ -6027,39 +5982,23 @@ function applyReverseOperation (y, scope, reverseBuffer) {
} }
}); });
} }
const redoitems = new Set(); for (let op of undoOp.deletedStructs) {
for (let del of undoOp.deletedStructs) { if (
const fromState = del.from; isParentOf(scope, op) &&
const toState = new ID(fromState.user, fromState.clock + del.len - 1); op._parent !== y &&
y.os.getItemCleanStart(fromState); (
y.os.getItemCleanEnd(toState); op._id.user !== y.userID ||
y.os.iterate(fromState, toState, op => { undoOp.fromState === null ||
if ( op._id.clock < undoOp.fromState.clock ||
isParentOf(scope, op) && op._id.clock > undoOp.toState.clock
op._parent !== y && )
( ) {
op._id.user !== y.userID || performedUndo = true;
undoOp.fromState === null || op._redo(y);
op._id.clock < undoOp.fromState.clock || }
op._id.clock > undoOp.toState.clock
)
) {
redoitems.add(op);
}
});
} }
redoitems.forEach(op => {
const opUndone = op._redo(y, redoitems);
performedUndo = performedUndo || opUndone;
});
} }
}); });
if (performedUndo) {
// should be performed after the undo transaction
undoOp.bindingInfos.forEach((info, binding) => {
binding._restoreUndoStackInfo(info);
});
}
return performedUndo return performedUndo
} }
@@ -6074,7 +6013,6 @@ class UndoManager {
*/ */
constructor (scope, options = {}) { constructor (scope, options = {}) {
this.options = options; this.options = options;
this._bindings = new Set(options.bindings);
options.captureTimeout = options.captureTimeout == null ? 500 : options.captureTimeout; options.captureTimeout = options.captureTimeout == null ? 500 : options.captureTimeout;
this._undoBuffer = []; this._undoBuffer = [];
this._redoBuffer = []; this._redoBuffer = [];
@@ -6085,28 +6023,16 @@ class UndoManager {
const y = scope._y; const y = scope._y;
this.y = y; this.y = y;
y._hasUndoManager = true; y._hasUndoManager = true;
let bindingInfos;
y.on('beforeTransaction', (y, transaction, remote) => {
if (!remote) {
// Store binding information before transaction is executed
// By restoring the binding information, we can make sure that the state
// before the transaction can be recovered
bindingInfos = new Map();
this._bindings.forEach(binding => {
bindingInfos.set(binding, binding._getUndoStackInfo());
});
}
});
y.on('afterTransaction', (y, transaction, remote) => { y.on('afterTransaction', (y, transaction, remote) => {
if (!remote && transaction.changedParentTypes.has(scope)) { if (!remote && transaction.changedParentTypes.has(scope)) {
let reverseOperation = new ReverseOperation(y, transaction, bindingInfos); let reverseOperation = new ReverseOperation(y, transaction);
if (!this._undoing) { if (!this._undoing) {
let lastUndoOp = this._undoBuffer.length > 0 ? this._undoBuffer[this._undoBuffer.length - 1] : null; let lastUndoOp = this._undoBuffer.length > 0 ? this._undoBuffer[this._undoBuffer.length - 1] : null;
if ( if (
this._redoing === false && this._redoing === false &&
this._lastTransactionWasUndo === false && this._lastTransactionWasUndo === false &&
lastUndoOp !== null && lastUndoOp !== null &&
(options.captureTimeout < 0 || reverseOperation.created - lastUndoOp.created <= options.captureTimeout) reverseOperation.created - lastUndoOp.created <= options.captureTimeout
) { ) {
lastUndoOp.created = reverseOperation.created; lastUndoOp.created = reverseOperation.created;
if (reverseOperation.toState !== null) { if (reverseOperation.toState !== null) {
@@ -6131,13 +6057,6 @@ class UndoManager {
}); });
} }
/**
* Enforce that the next change is created as a separate item in the undo stack
*/
flushChanges () {
this._lastTransactionWasUndo = true;
}
/** /**
* Undo the last locally created change. * Undo the last locally created change.
*/ */

File diff suppressed because one or more lines are too long

19218
y.test.js Normal file

File diff suppressed because one or more lines are too long

1
y.test.js.map Normal file

File diff suppressed because one or more lines are too long