Skip to content

Commit

Permalink
fix Y.Text formatting issue - closes #606
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jan 21, 2024
1 parent 7a8ca6e commit 1cb52dc
Show file tree
Hide file tree
Showing 36 changed files with 62 additions and 46 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ on Yjs. [![Become a Sponsor](https://img.shields.io/static/v1?label=Become%20a%2

## Professional Support

* [Support Contract with the Maintainer](https://github.com/sponsors/dmonad) - By contributing financially to the open-source Yjs project, you can receive professional support directly from the author. This includes the opportunity for weekly video calls to discuss your specific challenges.
* [Synergy Codes](https://synergycodes.com/yjs-services/) - Specializing in consulting and developing real-time collaborative editing solutions for visual apps, Synergy Codes focuses on interactive diagrams, complex graphs, charts, and various data visualization types. Their expertise empowers developers to build engaging and interactive visual experiences leveraging the power of Yjs. See their work in action at [Visual Collaboration Showcase](https://yjs-diagram.synergy.codes/).
* [Support Contract with the Maintainer](https://github.com/sponsors/dmonad) -
By contributing financially to the open-source Yjs project, you can receive
professional support directly from the author. This includes the opportunity for
weekly video calls to discuss your specific challenges.
* [Synergy Codes](https://synergycodes.com/yjs-services/) - Specializing in
consulting and developing real-time collaborative editing solutions for visual
apps, Synergy Codes focuses on interactive diagrams, complex graphs, charts, and
various data visualization types. Their expertise empowers developers to build
engaging and interactive visual experiences leveraging the power of Yjs. See
their work in action at [Visual Collaboration
Showcase](https://yjs-diagram.synergy.codes/).

## Who is using Yjs

Expand Down
1 change: 0 additions & 1 deletion src/internals.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export * from './utils/AbstractConnector.js'
export * from './utils/DeleteSet.js'
export * from './utils/Doc.js'
Expand Down
1 change: 0 additions & 1 deletion src/structs/AbstractStruct.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
UpdateEncoderV1, UpdateEncoderV2, ID, Transaction // eslint-disable-line
} from '../internals.js'
Expand Down
1 change: 0 additions & 1 deletion src/structs/ContentDeleted.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
addToDeleteSet,
UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, StructStore, Item, Transaction // eslint-disable-line
Expand Down
1 change: 0 additions & 1 deletion src/structs/ContentDoc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
Doc, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, StructStore, Transaction, Item // eslint-disable-line
} from '../internals.js'
Expand Down
1 change: 0 additions & 1 deletion src/structs/ContentEmbed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, StructStore, Item, Transaction // eslint-disable-line
} from '../internals.js'
Expand Down
1 change: 0 additions & 1 deletion src/structs/ContentFormat.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
YText, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Item, StructStore, Transaction // eslint-disable-line
} from '../internals.js'
Expand Down
1 change: 0 additions & 1 deletion src/structs/ContentType.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
readYArray,
readYMap,
Expand Down
1 change: 0 additions & 1 deletion src/structs/GC.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
AbstractStruct,
addStruct,
Expand Down
1 change: 0 additions & 1 deletion src/structs/Item.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
GC,
getState,
Expand Down
1 change: 0 additions & 1 deletion src/structs/Skip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
AbstractStruct,
UpdateEncoderV1, UpdateEncoderV2, StructStore, Transaction, ID // eslint-disable-line
Expand Down
1 change: 0 additions & 1 deletion src/types/AbstractType.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
removeEventHandlerListener,
callEventHandlerListeners,
Expand Down
1 change: 0 additions & 1 deletion src/types/YMap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* @module YMap
*/
Expand Down
22 changes: 11 additions & 11 deletions src/types/YText.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* @module YText
*/
Expand Down Expand Up @@ -118,14 +117,15 @@ const findNextPosition = (transaction, pos, count) => {
* @param {Transaction} transaction
* @param {AbstractType<any>} parent
* @param {number} index
* @param {boolean} useSearchMarker
* @return {ItemTextListPosition}
*
* @private
* @function
*/
const findPosition = (transaction, parent, index) => {
const findPosition = (transaction, parent, index, useSearchMarker) => {
const currentAttributes = new Map()
const marker = findMarker(parent, index)
const marker = useSearchMarker ? findMarker(parent, index) : null
if (marker) {
const pos = new ItemTextListPosition(marker.p.left, marker.p, marker.index, currentAttributes)
return findNextPosition(transaction, pos, index - marker.index)
Expand Down Expand Up @@ -1120,7 +1120,7 @@ export class YText extends AbstractType {
const y = this.doc
if (y !== null) {
transact(y, transaction => {
const pos = findPosition(transaction, this, index)
const pos = findPosition(transaction, this, index, !attributes)
if (!attributes) {
attributes = {}
// @ts-ignore
Expand All @@ -1138,20 +1138,20 @@ export class YText extends AbstractType {
*
* @param {number} index The index to insert the embed at.
* @param {Object | AbstractType<any>} embed The Object that represents the embed.
* @param {TextAttributes} attributes Attribute information to apply on the
* @param {TextAttributes} [attributes] Attribute information to apply on the
* embed
*
* @public
*/
insertEmbed (index, embed, attributes = {}) {
insertEmbed (index, embed, attributes) {
const y = this.doc
if (y !== null) {
transact(y, transaction => {
const pos = findPosition(transaction, this, index)
insertText(transaction, this, pos, embed, attributes)
const pos = findPosition(transaction, this, index, !attributes)
insertText(transaction, this, pos, embed, attributes || {})
})
} else {
/** @type {Array<function>} */ (this._pending).push(() => this.insertEmbed(index, embed, attributes))
/** @type {Array<function>} */ (this._pending).push(() => this.insertEmbed(index, embed, attributes || {}))
}
}

Expand All @@ -1170,7 +1170,7 @@ export class YText extends AbstractType {
const y = this.doc
if (y !== null) {
transact(y, transaction => {
deleteText(transaction, findPosition(transaction, this, index), length)
deleteText(transaction, findPosition(transaction, this, index, true), length)
})
} else {
/** @type {Array<function>} */ (this._pending).push(() => this.delete(index, length))
Expand All @@ -1194,7 +1194,7 @@ export class YText extends AbstractType {
const y = this.doc
if (y !== null) {
transact(y, transaction => {
const pos = findPosition(transaction, this, index)
const pos = findPosition(transaction, this, index, false)
if (pos.right === null) {
return
}
Expand Down
1 change: 0 additions & 1 deletion src/types/YXmlEvent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
YEvent,
YXmlText, YXmlElement, YXmlFragment, Transaction // eslint-disable-line
Expand Down
1 change: 0 additions & 1 deletion src/types/YXmlHook.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
YMap,
YXmlHookRefID,
Expand Down
1 change: 0 additions & 1 deletion src/types/YXmlText.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
YText,
YXmlTextRefID,
Expand Down
1 change: 0 additions & 1 deletion src/utils/AbstractConnector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { Observable } from 'lib0/observable'

import {
Expand Down
1 change: 0 additions & 1 deletion src/utils/DeleteSet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
findIndexSS,
getState,
Expand Down
1 change: 0 additions & 1 deletion src/utils/ID.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { AbstractType } from '../internals.js' // eslint-disable-line

import * as decoding from 'lib0/decoding'
Expand Down
1 change: 0 additions & 1 deletion src/utils/PermanentUserData.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
YArray,
YMap,
Expand Down
1 change: 0 additions & 1 deletion src/utils/RelativePosition.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
writeID,
readID,
Expand Down
1 change: 0 additions & 1 deletion src/utils/Snapshot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
isDeleted,
createDeleteSetFromStructStore,
Expand Down
1 change: 0 additions & 1 deletion src/utils/StructStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
GC,
splitItem,
Expand Down
1 change: 0 additions & 1 deletion src/utils/Transaction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
getState,
writeStructsFromTransaction,
Expand Down
1 change: 0 additions & 1 deletion src/utils/UpdateEncoder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import * as error from 'lib0/error'
import * as encoding from 'lib0/encoding'

Expand Down
1 change: 0 additions & 1 deletion src/utils/YEvent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
isDeleted,
Item, AbstractType, Transaction, AbstractStruct // eslint-disable-line
Expand Down
1 change: 0 additions & 1 deletion src/utils/encoding.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* @module encoding
*/
Expand Down
1 change: 0 additions & 1 deletion src/utils/isParentOf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { AbstractType, Item } from '../internals.js' // eslint-disable-line

/**
Expand Down
1 change: 0 additions & 1 deletion src/utils/logging.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
AbstractType // eslint-disable-line
} from '../internals.js'
Expand Down
1 change: 0 additions & 1 deletion src/utils/updates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import * as binary from 'lib0/binary'
import * as decoding from 'lib0/decoding'
import * as encoding from 'lib0/encoding'
Expand Down
1 change: 0 additions & 1 deletion tests/compatibility.tests.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tests/doc.tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import * as Y from '../src/index.js'
import * as t from 'lib0/testing'

Expand Down
1 change: 0 additions & 1 deletion tests/relativePositions.tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import * as Y from '../src/index.js'
import * as t from 'lib0/testing'

Expand Down
1 change: 0 additions & 1 deletion tests/testHelper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import * as t from 'lib0/testing'
import * as prng from 'lib0/prng'
import * as encoding from 'lib0/encoding'
Expand Down
40 changes: 40 additions & 0 deletions tests/undo-redo.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,46 @@ import { init } from './testHelper.js' // eslint-disable-line
import * as Y from '../src/index.js'
import * as t from 'lib0/testing'

export const testInconsistentFormat = () => {
/**
* @param {Y.Doc} ydoc
*/
const testYjsMerge = ydoc => {
const content = /** @type {Y.XmlText} */ (ydoc.get('text', Y.XmlText))
content.format(0, 6, { bold: null })
content.format(6, 4, { type: 'text' })
t.compare(content.toDelta(), [
{
attributes: { type: 'text' },
insert: 'Merge Test'
},
{
attributes: { type: 'text', italic: true },
insert: ' After'
}
])
}
const initializeYDoc = () => {
const yDoc = new Y.Doc({ gc: false })

const content = /** @type {Y.XmlText} */ (yDoc.get('text', Y.XmlText))
content.insert(0, ' After', { type: 'text', italic: true })
content.insert(0, 'Test', { type: 'text' })
content.insert(0, 'Merge ', { type: 'text', bold: true })
return yDoc
}
{
const yDoc = initializeYDoc()
testYjsMerge(yDoc)
}
{
const initialYDoc = initializeYDoc()
const yDoc = new Y.Doc({ gc: false })
Y.applyUpdate(yDoc, Y.encodeStateAsUpdate(initialYDoc))
testYjsMerge(yDoc)
}
}

/**
* @param {t.TestCase} tc
*/
Expand Down

0 comments on commit 1cb52dc

Please sign in to comment.