From a4ce8ae07d8f98171752b00bb92de8799dcad912 Mon Sep 17 00:00:00 2001 From: Joe Reeve Date: Tue, 31 Mar 2020 16:06:28 +0100 Subject: [PATCH 1/2] :bug: fix for #187 --- src/types/YText.js | 21 ++++++++++++++++++--- tests/y-text.tests.js | 14 ++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/types/YText.js b/src/types/YText.js index 572bba9e..6092f172 100644 --- a/src/types/YText.js +++ b/src/types/YText.js @@ -802,10 +802,25 @@ export class YText extends AbstractType { } case ContentEmbed: packStr() - ops.push({ + /** + * @type {Object} + */ + const attributes = {} + let addAttributes = false; + for (const [key, value] of currentAttributes) { + addAttributes = true; + attributes[key] = value + } + /** + * @type {Object} + */ + const op = { insert: /** @type {ContentEmbed} */ (n.content).embed - }) - break + } + if(addAttributes) { + op.attributes = attributes + } + ops.push(op) case ContentFormat: if (isVisible(n, snapshot)) { packStr() diff --git a/tests/y-text.tests.js b/tests/y-text.tests.js index e59536b3..5b508746 100644 --- a/tests/y-text.tests.js +++ b/tests/y-text.tests.js @@ -158,3 +158,17 @@ export const testToJson = tc => { text0.insert(0, 'abc', { bold: true }) t.assert(text0.toJSON() === 'abc', 'toJSON returns the unformatted text') } + +/** + * @param {t.TestCase} tc + */ +export const testToDeltaEmbedAttributes = tc => { + const { text0 } = init(tc, { users: 1 }) + text0.insertEmbed(0, { image: 'imageSrc.png' }, { width: 100 }) + const [delta0] = text0.toDelta(); + t.assert(!!delta0.attributes, 'toDelta correctly reads attributes') + const { text0: text1 } = init(tc, { users: 1 }) + text1.insertEmbed(1, { image: 'imageSrc.png' }) + const [delta1] = text1.toDelta(); + t.assert(!delta1.hasOwnProperty('attributes'), 'toDelta does not set attributes key when no attributes are present') +} From c87caafeb690af8b85873bcd9955a96dca24410f Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Wed, 1 Apr 2020 23:39:27 +0200 Subject: [PATCH 2/2] lint & refactor PR #187 --- README.md | 7 ++++--- src/types/YText.js | 21 +++++++++------------ tests/y-text.tests.js | 22 +++++++++++++++------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 2e959846..31c8532f 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,10 @@ suited for even large documents. * Podcast [**"Yjs Deep Dive into real time collaborative editing solutions":**](https://www.tag1consulting.com/blog/deep-dive-real-time-collaborative-editing-solutions-tagteamtalk-001-0) * Podcast [**"Google Docs-style editing in Gutenberg with the YJS framework":**](https://publishpress.com/blog/yjs/) -:construction_worker_woman: If you are looking for professional (paid) support to build -collaborative or distributed applications ping us at . Otherwise -you can find help on our [discussion board](https://discuss.yjs.dev). +:construction_worker_woman: If you are looking for professional (paid) support to +build collaborative or distributed applications ping us at +. Otherwise you can find help on our +[discussion board](https://discuss.yjs.dev). ## Table of Contents diff --git a/src/types/YText.js b/src/types/YText.js index 6092f172..7a33cd6d 100644 --- a/src/types/YText.js +++ b/src/types/YText.js @@ -800,27 +800,24 @@ export class YText extends AbstractType { str += /** @type {ContentString} */ (n.content).str break } - case ContentEmbed: + case ContentEmbed: { packStr() - /** - * @type {Object} - */ - const attributes = {} - let addAttributes = false; - for (const [key, value] of currentAttributes) { - addAttributes = true; - attributes[key] = value - } /** * @type {Object} */ const op = { insert: /** @type {ContentEmbed} */ (n.content).embed } - if(addAttributes) { - op.attributes = attributes + if (currentAttributes.size > 0) { + const attrs = /** @type {Object} */ ({}) + op.attributes = attrs + for (const [key, value] of currentAttributes) { + attrs[key] = value + } } ops.push(op) + break + } case ContentFormat: if (isVisible(n, snapshot)) { packStr() diff --git a/tests/y-text.tests.js b/tests/y-text.tests.js index 5b508746..ae3634a2 100644 --- a/tests/y-text.tests.js +++ b/tests/y-text.tests.js @@ -164,11 +164,19 @@ export const testToJson = tc => { */ export const testToDeltaEmbedAttributes = tc => { const { text0 } = init(tc, { users: 1 }) - text0.insertEmbed(0, { image: 'imageSrc.png' }, { width: 100 }) - const [delta0] = text0.toDelta(); - t.assert(!!delta0.attributes, 'toDelta correctly reads attributes') - const { text0: text1 } = init(tc, { users: 1 }) - text1.insertEmbed(1, { image: 'imageSrc.png' }) - const [delta1] = text1.toDelta(); - t.assert(!delta1.hasOwnProperty('attributes'), 'toDelta does not set attributes key when no attributes are present') + text0.insert(0, 'ab', { bold: true }) + text0.insertEmbed(1, { image: 'imageSrc.png' }, { width: 100 }) + const delta0 = text0.toDelta() + t.compare(delta0, [{ insert: 'a', attributes: { bold: true } }, { insert: { image: 'imageSrc.png' }, attributes: { width: 100 } }, { insert: 'b', attributes: { bold: true } }]) +} + +/** + * @param {t.TestCase} tc + */ +export const testToDeltaEmbedNoAttributes = tc => { + const { text0 } = init(tc, { users: 1 }) + text0.insert(0, 'ab', { bold: true }) + text0.insertEmbed(1, { image: 'imageSrc.png' }) + const delta0 = text0.toDelta() + t.compare(delta0, [{ insert: 'a', attributes: { bold: true } }, { insert: { image: 'imageSrc.png' } }, { insert: 'b', attributes: { bold: true } }], 'toDelta does not set attributes key when no attributes are present') }