Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e67b129

Browse files
authored
fix(yText): applyDelta should support both Delta and Ops[]
Fixed an issue that the yText.applyDelta() accepted only Ops[], but not Delta.
1 parent 1a0d4aa commit e67b129

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/types/YText.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,15 +972,16 @@ export class YText extends AbstractType {
972972
if (this.doc !== null) {
973973
transact(this.doc, transaction => {
974974
const currPos = new ItemTextListPosition(null, this._start, 0, new Map())
975-
for (let i = 0; i < delta.length; i++) {
976-
const op = delta[i]
975+
const ops = Array.isArray(delta) ? delta : (Array.isArray(delta?.ops) ? delta.ops : []);
976+
for (let i = 0; i < ops.length; i++) {
977+
const op = ops[i]
977978
if (op.insert !== undefined) {
978979
// Quill assumes that the content starts with an empty paragraph.
979980
// Yjs/Y.Text assumes that it starts empty. We always hide that
980981
// there is a newline at the end of the content.
981982
// If we omit this step, clients will see a different number of
982983
// paragraphs, but nothing bad will happen.
983-
const ins = (!sanitize && typeof op.insert === 'string' && i === delta.length - 1 && currPos.right === null && op.insert.slice(-1) === '\n') ? op.insert.slice(0, -1) : op.insert
984+
const ins = (!sanitize && typeof op.insert === 'string' && i === ops.length - 1 && currPos.right === null && op.insert.slice(-1) === '\n') ? op.insert.slice(0, -1) : op.insert
984985
if (typeof ins !== 'string' || ins.length > 0) {
985986
insertText(transaction, this, currPos, ins, op.attributes || {})
986987
}

0 commit comments

Comments
 (0)