Compare commits

...

41 Commits

Author SHA1 Message Date
Kevin Jahns
2fe8907ab0 13.6.8 2023-09-18 10:22:28 +02:00
Kevin Jahns
29270b5f3e fix "can't read origin of undefined" - fixes #417 2023-09-18 09:55:50 +02:00
Kevin Jahns
a099e98bd6 create error on call - fixes #569 2023-09-07 13:41:35 +02:00
Kevin Jahns
1b0da31d00 Merge pull request #567 from stevenfabre/main
Added Liveblocks Yjs to the list of providers
2023-09-06 17:01:29 +02:00
Kevin Jahns
a1fda219e4 lint readme 2023-09-02 17:39:49 +02:00
Kevin Jahns
09687221ac Merge pull request #568 from himself65/docs-update
docs: add `@toeverything/y-indexeddb`
2023-09-01 13:01:57 +02:00
Alex Yang
4d7a366f6e docs: add @toeverything/y-indexeddb 2023-08-31 17:21:36 -05:00
Steven Fabre
0b30413f6e Liveblocks in Yjs README
Signed-off-by: Steven Fabre <hello@stevenfabre.com>
2023-08-31 05:34:05 -04:00
Kevin Jahns
eeae74decf sponsors update 2023-08-29 16:34:26 +02:00
Kevin Jahns
5ac498d62e Merge pull request #566 from IllumiDesk/docs/add-illumidesk-ref
Adds IllumiDesk as a reference to the using list
2023-08-29 16:33:32 +02:00
Greg Werner
9a9a1ffeeb Adds IllumiDesk as a reference to the using list
Signed-off-by: Greg Werner <werner.greg@gmail.com>
2023-08-28 14:45:43 -04:00
Kevin Jahns
1ed12434a1 Merge pull request #556 from AdventureBeard/patch-1
Update README.md
2023-08-28 12:26:31 +02:00
Kevin Jahns
7a4975ee85 Merge branch 'main' into patch-1 2023-08-28 12:26:18 +02:00
Kevin Jahns
92ee76ad6e Merge pull request #564 from akshaykmr/readme-update-who-is-using
add oorja.io to who is using yjs
2023-08-24 14:01:32 +02:00
Kevin Jahns
97c09a6cca fix #509 2023-08-24 13:52:38 +02:00
Kevin Jahns
2e3ba0f81f Merge pull request #565 from himself65/himself65-patch-1
Update README.md
2023-08-23 16:46:17 +02:00
Alex Yang
61abf3a1db Update README.md 2023-08-23 09:25:18 -05:00
Akshay Kumar
bd867cb161 add oorja to who is using yjs 2023-08-23 19:31:58 +05:30
Kevin Jahns
87b7d3e951 add Yjs-compatible ports to documentation 2023-08-23 15:42:48 +02:00
Braden
03b9a806e8 Update README.md 2023-07-23 14:40:03 -05:00
Kevin Jahns
5ee6992d1f 13.6.7 2023-07-17 14:43:07 +02:00
Kevin Jahns
dd31040656 Merge pull request #553 from yjs/simpler-bulk-changes-fix-542
bulk-merging structs - replaces #542, fixes #541
2023-07-17 14:35:18 +02:00
Kevin Jahns
c77dedb68d bulk-merging structs - replaces #542, fixes #541 2023-07-17 14:29:54 +02:00
Kevin Jahns
90f2a06b5e throw error when event changes are computed after a transaction 2023-06-27 13:20:53 +02:00
Kevin Jahns
8586806932 13.6.6 2023-06-25 19:10:34 +02:00
Kevin Jahns
981340139f skip iterating when there are no formatting items - replaces #547 2023-06-25 12:46:02 +02:00
Kevin Jahns
b792902f17 13.6.5 2023-06-22 17:55:45 +02:00
Kevin Jahns
83b7c6839e Merge pull request #548 from YousefED/fix/equalDeleteSets
fix equalDeleteSets
2023-06-22 17:46:34 +02:00
Kevin Jahns
65c4d40a87 Merge branch 'NilSet-path-cache-invalidation' 2023-06-22 17:48:19 +02:00
Kevin Jahns
942c8a267b remove duplicate Transaction.callAll logic 2023-06-22 17:46:49 +02:00
yousefed
eda085936a keep original imports 2023-06-21 18:29:40 +02:00
yousefed
12be6c006a fix equalDeleteSets 2023-06-21 18:28:53 +02:00
Noel Levy
5d862477cd invalidate cached path when changing currentTarget of event
fixes #544
2023-06-19 11:31:45 -07:00
Kevin Jahns
c398448152 add blocksuite editor by affine 2023-06-16 16:04:30 +02:00
Kevin Jahns
2fbba13246 13.6.4 2023-06-15 13:11:40 +02:00
Kevin Jahns
885a740470 heavily improve performance when there are many events 2023-06-15 13:09:30 +02:00
Kevin Jahns
aedd4c8bf3 13.6.3 2023-06-15 12:47:48 +02:00
Kevin Jahns
9563612126 Merge pull request #540 from yjs/ytext-cleanup-538-refactor
Ytext cleanup 538 refactor
2023-06-15 12:39:12 +02:00
Kevin Jahns
ce098d0ac2 refactor #538 (formatting attrs) a bit 2023-06-15 12:40:28 +02:00
Noel Levy
08801dd406 scan the document once for all ytexts when cleaning up
Fixes #522 but is a scarier change
2023-06-12 18:20:22 -07:00
Noel Levy
3741f43a11 group cleanups for YText changes into a single transaction
Fixes #522 but is still massively slow
2023-06-12 16:56:19 -07:00
13 changed files with 243 additions and 110 deletions

View File

@@ -60,12 +60,18 @@ on Yjs. [![Become a Sponsor](https://img.shields.io/static/v1?label=Become%20a%2
sharing analyses, documentation, spreadsheets, and dashboards. sharing analyses, documentation, spreadsheets, and dashboards.
* [Nosgestesclimat](https://nosgestesclimat.fr/groupe) The french carbon * [Nosgestesclimat](https://nosgestesclimat.fr/groupe) The french carbon
footprint calculator has a group P2P mode based on yjs footprint calculator has a group P2P mode based on yjs
* [oorja.io](https://oorja.io) Online meeting spaces extensible with
collaborative apps, end-to-end encrypted.
* [LegendKeeper](https://legendkeeper.com) Collaborative campaign planner and
worldbuilding app for tabletop RPGs.
* [IllumiDesk](https://illumidesk.com/) Build courses and content with A.I.
## Table of Contents ## Table of Contents
* [Overview](#Overview) * [Overview](#Overview)
* [Bindings](#Bindings) * [Bindings](#Bindings)
* [Providers](#Providers) * [Providers](#Providers)
* [Ports](#Ports)
* [Getting Started](#Getting-Started) * [Getting Started](#Getting-Started)
* [API](#API) * [API](#API)
* [Shared Types](#Shared-Types) * [Shared Types](#Shared-Types)
@@ -91,6 +97,7 @@ are implemented in separate modules.
| [CodeMirror](https://codemirror.net/) | ✔ | [y-codemirror](https://github.com/yjs/y-codemirror) | [demo](https://demos.yjs.dev/codemirror/codemirror.html) | | [CodeMirror](https://codemirror.net/) | ✔ | [y-codemirror](https://github.com/yjs/y-codemirror) | [demo](https://demos.yjs.dev/codemirror/codemirror.html) |
| [Monaco](https://microsoft.github.io/monaco-editor/) | ✔ | [y-monaco](https://github.com/yjs/y-monaco) | [demo](https://demos.yjs.dev/monaco/monaco.html) | | [Monaco](https://microsoft.github.io/monaco-editor/) | ✔ | [y-monaco](https://github.com/yjs/y-monaco) | [demo](https://demos.yjs.dev/monaco/monaco.html) |
| [Slate](https://github.com/ianstormtaylor/slate) | ✔ | [slate-yjs](https://github.com/bitphinix/slate-yjs) | [demo](https://bitphinix.github.io/slate-yjs-example) | | [Slate](https://github.com/ianstormtaylor/slate) | ✔ | [slate-yjs](https://github.com/bitphinix/slate-yjs) | [demo](https://bitphinix.github.io/slate-yjs-example) |
| [BlockSuite](https://github.com/toeverything/blocksuite) | ✔ | (native) | [demo](https://blocksuite-toeverything.vercel.app/?init) |
| [valtio](https://github.com/pmndrs/valtio) | | [valtio-yjs](https://github.com/dai-shi/valtio-yjs) | [demo](https://codesandbox.io/s/valtio-yjs-demo-ox3iy) | | [valtio](https://github.com/pmndrs/valtio) | | [valtio-yjs](https://github.com/dai-shi/valtio-yjs) | [demo](https://codesandbox.io/s/valtio-yjs-demo-ox3iy) |
| [immer](https://github.com/immerjs/immer) | | [immer-yjs](https://github.com/sep2/immer-yjs) | [demo](https://codesandbox.io/s/immer-yjs-demo-6e0znb) | | [immer](https://github.com/immerjs/immer) | | [immer-yjs](https://github.com/sep2/immer-yjs) | [demo](https://codesandbox.io/s/immer-yjs-demo-6e0znb) |
| React / Vue / Svelte / MobX | | [SyncedStore](https://syncedstore.org) | [demo](https://syncedstore.org/docs/react) | | React / Vue / Svelte / MobX | | [SyncedStore](https://syncedstore.org) | [demo](https://syncedstore.org/docs/react) |
@@ -136,6 +143,14 @@ Also includes a peer-sync mechanism to catch up on missed updates.
an append-only log of CRDT local updates (hypercore). Multifeed manages and sync an append-only log of CRDT local updates (hypercore). Multifeed manages and sync
hypercores and y-dat listens to changes and applies them to the Yjs document. hypercores and y-dat listens to changes and applies them to the Yjs document.
</dd> </dd>
<dt><a href="https://github.com/liveblocks/liveblocks">@liveblocks/yjs</a></dt>
<dd>
<a href="https://liveblocks.io/document/yjs">Liveblocks Yjs</a> provides a fully
hosted WebSocket infrastructure and persisted data store for Yjs
documents. No configuration or maintenance is required. It also features
Yjs webhook events, REST API to read and update Yjs documents, and a
browser DevTools extension.
</dd>
<dt><a href="https://github.com/yousefED/matrix-crdt">Matrix-CRDT</a></dt> <dt><a href="https://github.com/yousefED/matrix-crdt">Matrix-CRDT</a></dt>
<dd> <dd>
Use <a href="https://www.matrix.org">Matrix</a> as an off-the-shelf backend for Use <a href="https://www.matrix.org">Matrix</a> as an off-the-shelf backend for
@@ -149,9 +164,30 @@ Encryption (E2EE).
<dd> <dd>
Adds persistent storage to a server with MongoDB. Can be used with the Adds persistent storage to a server with MongoDB. Can be used with the
y-websocket provider. y-websocket provider.
</dd>
<dt><a href="https://github.com/toeverything/AFFiNE/tree/master/packages/y-indexeddb">
@toeverything/y-indexeddb</a></dt>
<dd>
Like y-indexeddb, but with sub-documents support and fully TypeScript.
</dd> </dd>
</dl> </dl>
# Ports
There are several Yjs-compatible ports to other programming languages.
* [y-octo](https://github.com/toeverything/y-octo) - Rust implementation by
[AFFiNE](https://affine.pro)
* [y-crdt](https://github.com/y-crdt/y-crdt) - Rust implementation with multiple
language bindings to other languages
* [yrs](https://github.com/y-crdt/y-crdt/tree/main/yrs) - Rust interface
* [ypy](https://github.com/y-crdt/ypy) - Python binding
* [yrb](https://github.com/y-crdt/yrb) - Ruby binding
* [yrb](https://github.com/y-crdt/yswift) - Swift binding
* [yffi](https://github.com/y-crdt/y-crdt/tree/main/yffi) - C-FFI
* [ywasm](https://github.com/y-crdt/y-crdt/tree/main/ywasm) - WASM binding
* [ycs](https://github.com/yjs/ycs) - .Net compatible C# implementation.
## Getting Started ## Getting Started
Install Yjs and a provider with your favorite package manager: Install Yjs and a provider with your favorite package manager:

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "yjs", "name": "yjs",
"version": "13.6.2", "version": "13.6.8",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "yjs", "name": "yjs",
"version": "13.6.2", "version": "13.6.8",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"lib0": "^0.2.74" "lib0": "^0.2.74"

View File

@@ -1,6 +1,6 @@
{ {
"name": "yjs", "name": "yjs",
"version": "13.6.2", "version": "13.6.8",
"description": "Shared Editing Library", "description": "Shared Editing Library",
"main": "./dist/yjs.cjs", "main": "./dist/yjs.cjs",
"module": "./dist/yjs.mjs", "module": "./dist/yjs.mjs",

View File

@@ -1,6 +1,6 @@
import { import {
AbstractType, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Item, StructStore, Transaction // eslint-disable-line YText, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Item, StructStore, Transaction // eslint-disable-line
} from '../internals.js' } from '../internals.js'
import * as error from 'lib0/error' import * as error from 'lib0/error'
@@ -47,28 +47,30 @@ export class ContentFormat {
} }
/** /**
* @param {number} offset * @param {number} _offset
* @return {ContentFormat} * @return {ContentFormat}
*/ */
splice (offset) { splice (_offset) {
throw error.methodUnimplemented() throw error.methodUnimplemented()
} }
/** /**
* @param {ContentFormat} right * @param {ContentFormat} _right
* @return {boolean} * @return {boolean}
*/ */
mergeWith (right) { mergeWith (_right) {
return false return false
} }
/** /**
* @param {Transaction} transaction * @param {Transaction} _transaction
* @param {Item} item * @param {Item} item
*/ */
integrate (transaction, item) { integrate (_transaction, item) {
// @todo searchmarker are currently unsupported for rich text documents // @todo searchmarker are currently unsupported for rich text documents
/** @type {AbstractType<any>} */ (item.parent)._searchMarker = null const p = /** @type {YText} */ (item.parent)
p._searchMarker = null
p._hasFormatting = true
} }
/** /**

View File

@@ -108,7 +108,7 @@ export class ContentType {
while (item !== null) { while (item !== null) {
if (!item.deleted) { if (!item.deleted) {
item.delete(transaction) item.delete(transaction)
} else { } else if (item.id.clock < (transaction.beforeState.get(item.id.client) || 0)) {
// This will be gc'd later and we want to merge it if possible // This will be gc'd later and we want to merge it if possible
// We try to merge all deleted items after each transaction, // We try to merge all deleted items after each transaction,
// but we have no knowledge about that this needs to be merged // but we have no knowledge about that this needs to be merged
@@ -120,7 +120,7 @@ export class ContentType {
this.type._map.forEach(item => { this.type._map.forEach(item => {
if (!item.deleted) { if (!item.deleted) {
item.delete(transaction) item.delete(transaction)
} else { } else if (item.id.clock < (transaction.beforeState.get(item.id.client) || 0)) {
// same as above // same as above
transaction._mergeStructs.push(item) transaction._mergeStructs.push(item)
} }

View File

@@ -389,9 +389,8 @@ export class Item extends AbstractStruct {
} }
if ((this.left && this.left.constructor === GC) || (this.right && this.right.constructor === GC)) { if ((this.left && this.left.constructor === GC) || (this.right && this.right.constructor === GC)) {
this.parent = null this.parent = null
} } else if (!this.parent) {
// only set parent if this shouldn't be garbage collected // only set parent if this shouldn't be garbage collected
if (!this.parent) {
if (this.left && this.left.constructor === Item) { if (this.left && this.left.constructor === Item) {
this.parent = this.left.parent this.parent = this.left.parent
this.parentSub = this.left.parentSub this.parentSub = this.left.parentSub

View File

@@ -683,7 +683,7 @@ export const typeListInsertGenericsAfter = (transaction, parent, referenceItem,
packJsonContent() packJsonContent()
} }
const lengthExceeded = error.create('Length exceeded!') const lengthExceeded = () => error.create('Length exceeded!')
/** /**
* @param {Transaction} transaction * @param {Transaction} transaction
@@ -696,7 +696,7 @@ const lengthExceeded = error.create('Length exceeded!')
*/ */
export const typeListInsertGenerics = (transaction, parent, index, content) => { export const typeListInsertGenerics = (transaction, parent, index, content) => {
if (index > parent._length) { if (index > parent._length) {
throw lengthExceeded throw lengthExceeded()
} }
if (index === 0) { if (index === 0) {
if (parent._searchMarker) { if (parent._searchMarker) {
@@ -798,7 +798,7 @@ export const typeListDelete = (transaction, parent, index, length) => {
n = n.right n = n.right
} }
if (length > 0) { if (length > 0) {
throw lengthExceeded throw lengthExceeded()
} }
if (parent._searchMarker) { if (parent._searchMarker) {
updateMarkerChanges(parent._searchMarker, startIndex, -startLength + length /* in case we remove the above exception */) updateMarkerChanges(parent._searchMarker, startIndex, -startLength + length /* in case we remove the above exception */)

View File

@@ -476,6 +476,56 @@ export const cleanupYTextFormatting = type => {
return res return res
} }
/**
* This will be called by the transction once the event handlers are called to potentially cleanup
* formatting attributes.
*
* @param {Transaction} transaction
*/
export const cleanupYTextAfterTransaction = transaction => {
/**
* @type {Set<YText>}
*/
const needFullCleanup = new Set()
// check if another formatting item was inserted
const doc = transaction.doc
for (const [client, afterClock] of transaction.afterState.entries()) {
const clock = transaction.beforeState.get(client) || 0
if (afterClock === clock) {
continue
}
iterateStructs(transaction, /** @type {Array<Item|GC>} */ (doc.store.clients.get(client)), clock, afterClock, item => {
if (
!item.deleted && /** @type {Item} */ (item).content.constructor === ContentFormat && item.constructor !== GC
) {
needFullCleanup.add(/** @type {any} */ (item).parent)
}
})
}
// cleanup in a new transaction
transact(doc, (t) => {
iterateDeletedStructs(transaction, transaction.deleteSet, item => {
if (item instanceof GC || !(/** @type {YText} */ (item.parent)._hasFormatting) || needFullCleanup.has(/** @type {YText} */ (item.parent))) {
return
}
const parent = /** @type {YText} */ (item.parent)
if (item.content.constructor === ContentFormat) {
needFullCleanup.add(parent)
} else {
// If no formatting attribute was inserted or deleted, we can make due with contextless
// formatting cleanups.
// Contextless: it is not necessary to compute currentAttributes for the affected position.
cleanupContextlessFormattingGap(t, item)
}
})
// If a formatting item was inserted, we simply clean the whole type.
// We need to compute currentAttributes for the current position anyway.
for (const yText of needFullCleanup) {
cleanupYTextFormatting(yText)
}
})
}
/** /**
* @param {Transaction} transaction * @param {Transaction} transaction
* @param {ItemTextListPosition} currPos * @param {ItemTextListPosition} currPos
@@ -809,9 +859,14 @@ export class YText extends AbstractType {
*/ */
this._pending = string !== undefined ? [() => this.insert(0, string)] : [] this._pending = string !== undefined ? [() => this.insert(0, string)] : []
/** /**
* @type {Array<ArraySearchMarker>} * @type {Array<ArraySearchMarker>|null}
*/ */
this._searchMarker = [] this._searchMarker = []
/**
* Whether this YText contains formatting attributes.
* This flag is updated when a formatting item is integrated (see ContentFormat.integrate)
*/
this._hasFormatting = false
} }
/** /**
@@ -859,55 +914,10 @@ export class YText extends AbstractType {
_callObserver (transaction, parentSubs) { _callObserver (transaction, parentSubs) {
super._callObserver(transaction, parentSubs) super._callObserver(transaction, parentSubs)
const event = new YTextEvent(this, transaction, parentSubs) const event = new YTextEvent(this, transaction, parentSubs)
const doc = transaction.doc
callTypeObservers(this, transaction, event) callTypeObservers(this, transaction, event)
// If a remote change happened, we try to cleanup potential formatting duplicates. // If a remote change happened, we try to cleanup potential formatting duplicates.
if (!transaction.local) { if (!transaction.local && this._hasFormatting) {
// check if another formatting item was inserted transaction._needFormattingCleanup = true
let foundFormattingItem = false
for (const [client, afterClock] of transaction.afterState.entries()) {
const clock = transaction.beforeState.get(client) || 0
if (afterClock === clock) {
continue
}
iterateStructs(transaction, /** @type {Array<Item|GC>} */ (doc.store.clients.get(client)), clock, afterClock, item => {
if (!item.deleted && /** @type {Item} */ (item).content.constructor === ContentFormat) {
foundFormattingItem = true
}
})
if (foundFormattingItem) {
break
}
}
if (!foundFormattingItem) {
iterateDeletedStructs(transaction, transaction.deleteSet, item => {
if (item instanceof GC || foundFormattingItem) {
return
}
if (item.parent === this && item.content.constructor === ContentFormat) {
foundFormattingItem = true
}
})
}
transact(doc, (t) => {
if (foundFormattingItem) {
// If a formatting item was inserted, we simply clean the whole type.
// We need to compute currentAttributes for the current position anyway.
cleanupYTextFormatting(this)
} else {
// If no formatting attribute was inserted, we can make due with contextless
// formatting cleanups.
// Contextless: it is not necessary to compute currentAttributes for the affected position.
iterateDeletedStructs(t, t.deleteSet, item => {
if (item instanceof GC) {
return
}
if (item.parent === this) {
cleanupContextlessFormattingGap(t, item)
}
})
}
})
} }
} }

View File

@@ -335,7 +335,7 @@ export const readAndApplyDeleteSet = (decoder, transaction, store) => {
*/ */
export const equalDeleteSets = (ds1, ds2) => { export const equalDeleteSets = (ds1, ds2) => {
if (ds1.clients.size !== ds2.clients.size) return false if (ds1.clients.size !== ds2.clients.size) return false
ds1.clients.forEach((deleteItems1, client) => { for (const [client, deleteItems1] of ds1.clients.entries()) {
const deleteItems2 = /** @type {Array<import('../internals.js').DeleteItem>} */ (ds2.clients.get(client)) const deleteItems2 = /** @type {Array<import('../internals.js').DeleteItem>} */ (ds2.clients.get(client))
if (deleteItems2 === undefined || deleteItems1.length !== deleteItems2.length) return false if (deleteItems2 === undefined || deleteItems1.length !== deleteItems2.length) return false
for (let i = 0; i < deleteItems1.length; i++) { for (let i = 0; i < deleteItems1.length; i++) {
@@ -345,6 +345,6 @@ export const equalDeleteSets = (ds1, ds2) => {
return false return false
} }
} }
}) }
return true return true
} }

View File

@@ -11,6 +11,7 @@ import {
Item, Item,
generateNewClientId, generateNewClientId,
createID, createID,
cleanupYTextAfterTransaction,
UpdateEncoderV1, UpdateEncoderV2, GC, StructStore, AbstractType, AbstractStruct, YEvent, Doc // eslint-disable-line UpdateEncoderV1, UpdateEncoderV2, GC, StructStore, AbstractType, AbstractStruct, YEvent, Doc // eslint-disable-line
} from '../internals.js' } from '../internals.js'
@@ -114,6 +115,10 @@ export class Transaction {
* @type {Set<Doc>} * @type {Set<Doc>}
*/ */
this.subdocsLoaded = new Set() this.subdocsLoaded = new Set()
/**
* @type {boolean}
*/
this._needFormattingCleanup = false
} }
} }
@@ -161,18 +166,29 @@ export const addChangedTypeToTransaction = (transaction, type, parentSub) => {
/** /**
* @param {Array<AbstractStruct>} structs * @param {Array<AbstractStruct>} structs
* @param {number} pos * @param {number} pos
* @return {number} # of merged structs
*/ */
const tryToMergeWithLeft = (structs, pos) => { const tryToMergeWithLefts = (structs, pos) => {
const left = structs[pos - 1] let right = structs[pos]
const right = structs[pos] let left = structs[pos - 1]
if (left.deleted === right.deleted && left.constructor === right.constructor) { let i = pos
if (left.mergeWith(right)) { for (; i > 0; right = left, left = structs[--i - 1]) {
structs.splice(pos, 1) if (left.deleted === right.deleted && left.constructor === right.constructor) {
if (right instanceof Item && right.parentSub !== null && /** @type {AbstractType<any>} */ (right.parent)._map.get(right.parentSub) === right) { if (left.mergeWith(right)) {
/** @type {AbstractType<any>} */ (right.parent)._map.set(right.parentSub, /** @type {Item} */ (left)) if (right instanceof Item && right.parentSub !== null && /** @type {AbstractType<any>} */ (right.parent)._map.get(right.parentSub) === right) {
/** @type {AbstractType<any>} */ (right.parent)._map.set(right.parentSub, /** @type {Item} */ (left))
}
continue
} }
} }
break
} }
const merged = pos - i
if (merged) {
// remove all merged structs from the array
structs.splice(pos + 1 - merged, merged)
}
return merged
} }
/** /**
@@ -219,9 +235,9 @@ const tryMergeDeleteSet = (ds, store) => {
for ( for (
let si = mostRightIndexToCheck, struct = structs[si]; let si = mostRightIndexToCheck, struct = structs[si];
si > 0 && struct.id.clock >= deleteItem.clock; si > 0 && struct.id.clock >= deleteItem.clock;
struct = structs[--si] struct = structs[si]
) { ) {
tryToMergeWithLeft(structs, si) si -= 1 + tryToMergeWithLefts(structs, si)
} }
} }
}) })
@@ -270,31 +286,34 @@ const cleanupTransactions = (transactionCleanups, i) => {
) )
fs.push(() => { fs.push(() => {
// deep observe events // deep observe events
transaction.changedParentTypes.forEach((events, type) => transaction.changedParentTypes.forEach((events, type) => {
fs.push(() => { // We need to think about the possibility that the user transforms the
// We need to think about the possibility that the user transforms the // Y.Doc in the event.
// Y.Doc in the event. if (type._dEH.l.length > 0 && (type._item === null || !type._item.deleted)) {
if (type._item === null || !type._item.deleted) { events = events
events = events .filter(event =>
.filter(event => event.target._item === null || !event.target._item.deleted
event.target._item === null || !event.target._item.deleted )
) events
events .forEach(event => {
.forEach(event => { event.currentTarget = type
event.currentTarget = type // path is relative to the current target
}) event._path = null
// sort events by path length so that top-level events are fired first. })
events // sort events by path length so that top-level events are fired first.
.sort((event1, event2) => event1.path.length - event2.path.length) events
// We don't need to check for events.length .sort((event1, event2) => event1.path.length - event2.path.length)
// because we know it has at least one element // We don't need to check for events.length
callEventHandlerListeners(type._dEH, events, transaction) // because we know it has at least one element
} callEventHandlerListeners(type._dEH, events, transaction)
}) }
) })
fs.push(() => doc.emit('afterTransaction', [transaction, doc]))
}) })
fs.push(() => doc.emit('afterTransaction', [transaction, doc]))
callAll(fs, []) callAll(fs, [])
if (transaction._needFormattingCleanup) {
cleanupYTextAfterTransaction(transaction)
}
} finally { } finally {
// Replace deleted items with ItemDeleted / GC. // Replace deleted items with ItemDeleted / GC.
// This is where content is actually remove from the Yjs Doc. // This is where content is actually remove from the Yjs Doc.
@@ -310,23 +329,25 @@ const cleanupTransactions = (transactionCleanups, i) => {
const structs = /** @type {Array<GC|Item>} */ (store.clients.get(client)) const structs = /** @type {Array<GC|Item>} */ (store.clients.get(client))
// we iterate from right to left so we can safely remove entries // we iterate from right to left so we can safely remove entries
const firstChangePos = math.max(findIndexSS(structs, beforeClock), 1) const firstChangePos = math.max(findIndexSS(structs, beforeClock), 1)
for (let i = structs.length - 1; i >= firstChangePos; i--) { for (let i = structs.length - 1; i >= firstChangePos;) {
tryToMergeWithLeft(structs, i) i -= 1 + tryToMergeWithLefts(structs, i)
} }
} }
}) })
// try to merge mergeStructs // try to merge mergeStructs
// @todo: it makes more sense to transform mergeStructs to a DS, sort it, and merge from right to left // @todo: it makes more sense to transform mergeStructs to a DS, sort it, and merge from right to left
// but at the moment DS does not handle duplicates // but at the moment DS does not handle duplicates
for (let i = 0; i < mergeStructs.length; i++) { for (let i = mergeStructs.length - 1; i >= 0; i--) {
const { client, clock } = mergeStructs[i].id const { client, clock } = mergeStructs[i].id
const structs = /** @type {Array<GC|Item>} */ (store.clients.get(client)) const structs = /** @type {Array<GC|Item>} */ (store.clients.get(client))
const replacedStructPos = findIndexSS(structs, clock) const replacedStructPos = findIndexSS(structs, clock)
if (replacedStructPos + 1 < structs.length) { if (replacedStructPos + 1 < structs.length) {
tryToMergeWithLeft(structs, replacedStructPos + 1) if (tryToMergeWithLefts(structs, replacedStructPos + 1) > 1) {
continue // no need to perform next check, both are already merged
}
} }
if (replacedStructPos > 0) { if (replacedStructPos > 0) {
tryToMergeWithLeft(structs, replacedStructPos) tryToMergeWithLefts(structs, replacedStructPos)
} }
} }
if (!transaction.local && transaction.afterState.get(doc.clientID) !== transaction.beforeState.get(doc.clientID)) { if (!transaction.local && transaction.afterState.get(doc.clientID) !== transaction.beforeState.get(doc.clientID)) {

View File

@@ -15,6 +15,7 @@ import {
import * as time from 'lib0/time' import * as time from 'lib0/time'
import * as array from 'lib0/array' import * as array from 'lib0/array'
import * as logging from 'lib0/logging'
import { Observable } from 'lib0/observable' import { Observable } from 'lib0/observable'
export class StackItem { export class StackItem {
@@ -169,6 +170,7 @@ export class UndoManager extends Observable {
* @type {Array<AbstractType<any>>} * @type {Array<AbstractType<any>>}
*/ */
this.scope = [] this.scope = []
this.doc = doc
this.addToScope(typeScope) this.addToScope(typeScope)
this.deleteFilter = deleteFilter this.deleteFilter = deleteFilter
trackedOrigins.add(this) trackedOrigins.add(this)
@@ -189,7 +191,6 @@ export class UndoManager extends Observable {
*/ */
this.undoing = false this.undoing = false
this.redoing = false this.redoing = false
this.doc = doc
this.lastChange = 0 this.lastChange = 0
this.ignoreRemoteMapChanges = ignoreRemoteMapChanges this.ignoreRemoteMapChanges = ignoreRemoteMapChanges
this.captureTimeout = captureTimeout this.captureTimeout = captureTimeout
@@ -263,6 +264,7 @@ export class UndoManager extends Observable {
ytypes = array.isArray(ytypes) ? ytypes : [ytypes] ytypes = array.isArray(ytypes) ? ytypes : [ytypes]
ytypes.forEach(ytype => { ytypes.forEach(ytype => {
if (this.scope.every(yt => yt !== ytype)) { if (this.scope.every(yt => yt !== ytype)) {
if (ytype.doc !== this.doc) logging.warn('[yjs#509] Not same Y.Doc') // use MultiDocUndoManager instead. also see https://github.com/yjs/yjs/issues/509
this.scope.push(ytype) this.scope.push(ytype)
} }
}) })

View File

@@ -6,6 +6,9 @@ import {
import * as set from 'lib0/set' import * as set from 'lib0/set'
import * as array from 'lib0/array' import * as array from 'lib0/array'
import * as error from 'lib0/error'
const errorComputeChanges = 'You must not compute changes after the event-handler fired.'
/** /**
* @template {AbstractType<any>} T * @template {AbstractType<any>} T
@@ -44,6 +47,10 @@ export class YEvent {
* @type {null | Array<{ insert?: string | Array<any> | object | AbstractType<any>, retain?: number, delete?: number, attributes?: Object<string, any> }>} * @type {null | Array<{ insert?: string | Array<any> | object | AbstractType<any>, retain?: number, delete?: number, attributes?: Object<string, any> }>}
*/ */
this._delta = null this._delta = null
/**
* @type {Array<string|number>|null}
*/
this._path = null
} }
/** /**
@@ -60,8 +67,7 @@ export class YEvent {
* type === event.target // => true * type === event.target // => true
*/ */
get path () { get path () {
// @ts-ignore _item is defined because target is integrated return this._path || (this._path = getPathTo(this.currentTarget, this.target))
return getPathTo(this.currentTarget, this.target)
} }
/** /**
@@ -81,6 +87,9 @@ export class YEvent {
*/ */
get keys () { get keys () {
if (this._keys === null) { if (this._keys === null) {
if (this.transaction.doc._transactionCleanups.length === 0) {
throw error.create(errorComputeChanges)
}
const keys = new Map() const keys = new Map()
const target = this.target const target = this.target
const changed = /** @type Set<string|null> */ (this.transaction.changed.get(target)) const changed = /** @type Set<string|null> */ (this.transaction.changed.get(target))
@@ -164,6 +173,9 @@ export class YEvent {
get changes () { get changes () {
let changes = this._changes let changes = this._changes
if (changes === null) { if (changes === null) {
if (this.transaction.doc._transactionCleanups.length === 0) {
throw error.create(errorComputeChanges)
}
const target = this.target const target = this.target
const added = set.create() const added = set.create()
const deleted = set.create() const deleted = set.create()

View File

@@ -8,6 +8,29 @@ import * as Y from '../src/index.js'
import * as t from 'lib0/testing' import * as t from 'lib0/testing'
import * as prng from 'lib0/prng' import * as prng from 'lib0/prng'
/**
* Computing event changes after transaction should result in an error. See yjs#539
*
* @param {t.TestCase} _tc
*/
export const testMapEventError = _tc => {
const doc = new Y.Doc()
const ymap = doc.getMap()
/**
* @type {any}
*/
let event = null
ymap.observe((e) => {
event = e
})
t.fails(() => {
t.info(event.keys)
})
t.fails(() => {
t.info(event.keys)
})
}
/** /**
* @param {t.TestCase} tc * @param {t.TestCase} tc
*/ */
@@ -337,6 +360,34 @@ export const testObserversUsingObservedeep = tc => {
compare(users) compare(users)
} }
/**
* @param {t.TestCase} tc
*/
export const testPathsOfSiblingEvents = tc => {
const { users, map0 } = init(tc, { users: 2 })
/**
* @type {Array<Array<string|number>>}
*/
const pathes = []
let calls = 0
const doc = users[0]
map0.set('map', new Y.Map())
map0.get('map').set('text1', new Y.Text('initial'))
map0.observeDeep(events => {
events.forEach(event => {
pathes.push(event.path)
})
calls++
})
doc.transact(() => {
map0.get('map').get('text1').insert(0, 'post-')
map0.get('map').set('text2', new Y.Text('new'))
})
t.assert(calls === 1)
t.compare(pathes, [['map'], ['map', 'text1']])
compare(users)
}
// TODO: Test events in Y.Map // TODO: Test events in Y.Map
/** /**
* @param {Object<string,any>} is * @param {Object<string,any>} is