lint & refactor PR #187

This commit is contained in:
Kevin Jahns 2020-04-01 23:39:27 +02:00
parent 195b26d90f
commit c87caafeb6
3 changed files with 28 additions and 22 deletions

View File

@ -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 [**"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/) * 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 :construction_worker_woman: If you are looking for professional (paid) support to
collaborative or distributed applications ping us at <yjs@tag1consulting.com>. Otherwise build collaborative or distributed applications ping us at
you can find help on our [discussion board](https://discuss.yjs.dev). <yjs@tag1consulting.com>. Otherwise you can find help on our
[discussion board](https://discuss.yjs.dev).
## Table of Contents ## Table of Contents

View File

@ -800,27 +800,24 @@ export class YText extends AbstractType {
str += /** @type {ContentString} */ (n.content).str str += /** @type {ContentString} */ (n.content).str
break break
} }
case ContentEmbed: case ContentEmbed: {
packStr() packStr()
/**
* @type {Object<string,any>}
*/
const attributes = {}
let addAttributes = false;
for (const [key, value] of currentAttributes) {
addAttributes = true;
attributes[key] = value
}
/** /**
* @type {Object<string,any>} * @type {Object<string,any>}
*/ */
const op = { const op = {
insert: /** @type {ContentEmbed} */ (n.content).embed insert: /** @type {ContentEmbed} */ (n.content).embed
} }
if(addAttributes) { if (currentAttributes.size > 0) {
op.attributes = attributes const attrs = /** @type {Object<string,any>} */ ({})
op.attributes = attrs
for (const [key, value] of currentAttributes) {
attrs[key] = value
}
} }
ops.push(op) ops.push(op)
break
}
case ContentFormat: case ContentFormat:
if (isVisible(n, snapshot)) { if (isVisible(n, snapshot)) {
packStr() packStr()

View File

@ -164,11 +164,19 @@ export const testToJson = tc => {
*/ */
export const testToDeltaEmbedAttributes = tc => { export const testToDeltaEmbedAttributes = tc => {
const { text0 } = init(tc, { users: 1 }) const { text0 } = init(tc, { users: 1 })
text0.insertEmbed(0, { image: 'imageSrc.png' }, { width: 100 }) text0.insert(0, 'ab', { bold: true })
const [delta0] = text0.toDelta(); text0.insertEmbed(1, { image: 'imageSrc.png' }, { width: 100 })
t.assert(!!delta0.attributes, 'toDelta correctly reads attributes') const delta0 = text0.toDelta()
const { text0: text1 } = init(tc, { users: 1 }) t.compare(delta0, [{ insert: 'a', attributes: { bold: true } }, { insert: { image: 'imageSrc.png' }, attributes: { width: 100 } }, { insert: 'b', attributes: { bold: true } }])
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') /**
* @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')
} }