From 469404c6e1012d6d398382c2ed9a2c604b159496 Mon Sep 17 00:00:00 2001 From: Cole Date: Mon, 1 Jun 2020 19:17:54 +0900 Subject: [PATCH 1/3] move quill relate newline remove logic to y-quill --- src/types/YText.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/types/YText.js b/src/types/YText.js index e02b2514..41fe0fee 100644 --- a/src/types/YText.js +++ b/src/types/YText.js @@ -853,16 +853,8 @@ export class YText extends AbstractType { const currentAttributes = new Map() for (let i = 0; i < delta.length; i++) { const op = delta[i] - if (op.insert !== undefined) { - // Quill assumes that the content starts with an empty paragraph. - // Yjs/Y.Text assumes that it starts empty. We always hide that - // there is a newline at the end of the content. - // If we omit this step, clients will see a different number of - // paragraphs, but nothing bad will happen. - const ins = (typeof op.insert === 'string' && i === delta.length - 1 && pos.right === null && op.insert.slice(-1) === '\n') ? op.insert.slice(0, -1) : op.insert - if (typeof ins !== 'string' || ins.length > 0) { - pos = insertText(transaction, this, pos.left, pos.right, currentAttributes, ins, op.attributes || {}) - } + if (op.insert !== undefined && (typeof op.insert !== 'string' || op.insert.length > 0)) { + pos = insertText(transaction, this, pos.left, pos.right, currentAttributes, op.insert, op.attributes || {}) } else if (op.retain !== undefined) { pos = formatText(transaction, this, pos.left, pos.right, currentAttributes, op.retain, op.attributes || {}) } else if (op.delete !== undefined) { From 9d3dd4e0824b43ba1ab5ed08911fe4a5eca08d68 Mon Sep 17 00:00:00 2001 From: Cole Date: Tue, 2 Jun 2020 09:59:09 +0900 Subject: [PATCH 2/3] Add setter form permit empty paragraph at the end of the content when applyDelta. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index bb6c6238..4299ec11 100644 --- a/README.md +++ b/README.md @@ -399,6 +399,8 @@ YTextEvents compute changes as deltas.
See Quill Delta
length:number
+ permitEmptyParagraph(permit:boolean) +
Permit empty paragraph at the end of the content when applyDelta
toString():string
Transforms this type, without formatting options, into a string.
toJSON():string From e4223760b0c7c923dc0efcbb36ec8b1719340f9b Mon Sep 17 00:00:00 2001 From: Cole Date: Wed, 3 Jun 2020 11:00:50 +0900 Subject: [PATCH 3/3] - rollback shorter url to original and ignore max length check for specific line - add opts sanitize for applyDelata in YText - apply applyDelata document about YText --- README.md | 10 ++++++---- src/types/YText.js | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4299ec11..b683c535 100644 --- a/README.md +++ b/README.md @@ -395,12 +395,14 @@ YTextEvents compute changes as deltas.
format(index:number, length:number, formattingAttributes:Object<string,string>)
Assign formatting attributes to a range in the text
- applyDelta(delta) -
See Quill Delta
+ applyDelta(delta, opts:Object<string,any>) +
+ See Quill Delta + Can set options for preventing remove ending newLines, default is true. +
ytext.applyDelta(delta, { sanitize: false })
+
length:number
- permitEmptyParagraph(permit:boolean) -
Permit empty paragraph at the end of the content when applyDelta
toString():string
Transforms this type, without formatting options, into a string.
toJSON():string diff --git a/src/types/YText.js b/src/types/YText.js index 1a5ca05e..31998b21 100644 --- a/src/types/YText.js +++ b/src/types/YText.js @@ -848,10 +848,13 @@ export class YText extends AbstractType { * Apply a {@link Delta} on this shared YText type. * * @param {any} delta The changes to apply on this element. + * @param {object} [opts] + * @param {boolean} [opts.sanitize] Sanitize input delta. Removes ending newlines if set to true. + * * * @public */ - applyDelta (delta) { + applyDelta (delta, { sanitize = true } = {}) { if (this.doc !== null) { transact(this.doc, transaction => { /** @@ -861,8 +864,16 @@ export class YText extends AbstractType { const currentAttributes = new Map() for (let i = 0; i < delta.length; i++) { const op = delta[i] - if (op.insert !== undefined && (typeof op.insert !== 'string' || op.insert.length > 0)) { - pos = insertText(transaction, this, pos.left, pos.right, currentAttributes, op.insert, op.attributes || {}) + if (op.insert !== undefined) { + // Quill assumes that the content starts with an empty paragraph. + // Yjs/Y.Text assumes that it starts empty. We always hide that + // there is a newline at the end of the content. + // If we omit this step, clients will see a different number of + // paragraphs, but nothing bad will happen. + const ins = (!sanitize && typeof op.insert === 'string' && i === delta.length - 1 && pos.right === null && op.insert.slice(-1) === '\n') ? op.insert.slice(0, -1) : op.insert + if (typeof ins !== 'string' || ins.length > 0) { + pos = insertText(transaction, this, pos.left, pos.right, currentAttributes, ins, op.attributes || {}) + } } else if (op.retain !== undefined) { pos = formatText(transaction, this, pos.left, pos.right, currentAttributes, op.retain, op.attributes || {}) } else if (op.delete !== undefined) {