@@ -264,19 +264,19 @@ extension TextTransaction on Transaction {
264
264
if (index != 0 && attributes == null ) {
265
265
newAttributes =
266
266
textNode.delta.slice (max (index - 1 , 0 ), index).first.attributes;
267
- if (newAttributes ! = null ) {
268
- newAttributes = {...newAttributes}; // make a copy
269
- } else {
270
- newAttributes =
271
- textNode.delta. slice (index, index + length).first.attributes;
267
+ if (newAttributes = = null ) {
268
+ final slicedDelta = textNode.delta. slice (index, index + length);
269
+ if (slicedDelta.isNotEmpty) {
270
+ newAttributes = slicedDelta.first.attributes;
271
+ }
272
272
}
273
273
}
274
274
updateText (
275
275
textNode,
276
276
Delta ()
277
277
..retain (index)
278
278
..delete (length)
279
- ..insert (text, attributes: newAttributes),
279
+ ..insert (text, attributes: {... newAttributes ?? {}} ),
280
280
);
281
281
afterSelection = Selection .collapsed (
282
282
Position (
@@ -291,46 +291,125 @@ extension TextTransaction on Transaction {
291
291
Selection selection,
292
292
List <String > texts,
293
293
) {
294
- if (textNodes.isEmpty) {
294
+ if (textNodes.isEmpty || texts.isEmpty ) {
295
295
return ;
296
296
}
297
297
298
- if (selection.isSingle) {
299
- assert (textNodes.length == 1 && texts.length == 1 );
300
- replaceText (
301
- textNodes.first,
302
- selection.startIndex,
303
- selection.length,
304
- texts.first,
305
- );
306
- } else {
298
+ if (textNodes.length == texts.length) {
307
299
final length = textNodes.length;
308
- for (var i = 0 ; i < length; i++ ) {
300
+
301
+ if (length == 1 ) {
302
+ replaceText (
303
+ textNodes.first,
304
+ selection.startIndex,
305
+ selection.endIndex - selection.startIndex,
306
+ texts.first,
307
+ );
308
+ return ;
309
+ }
310
+
311
+ for (var i = 0 ; i < textNodes.length; i++ ) {
309
312
final textNode = textNodes[i];
310
- final text = texts[i];
311
313
if (i == 0 ) {
312
314
replaceText (
313
315
textNode,
314
316
selection.startIndex,
315
317
textNode.toPlainText ().length,
316
- text ,
318
+ texts.first ,
317
319
);
318
320
} else if (i == length - 1 ) {
319
321
replaceText (
320
322
textNode,
321
323
0 ,
322
324
selection.endIndex,
323
- text ,
325
+ texts.last ,
324
326
);
325
327
} else {
326
328
replaceText (
327
329
textNode,
328
330
0 ,
329
331
textNode.toPlainText ().length,
332
+ texts[i],
333
+ );
334
+ }
335
+ }
336
+ return ;
337
+ }
338
+
339
+ if (textNodes.length > texts.length) {
340
+ final length = textNodes.length;
341
+ for (var i = 0 ; i < textNodes.length; i++ ) {
342
+ final textNode = textNodes[i];
343
+ if (i == 0 ) {
344
+ replaceText (
345
+ textNode,
346
+ selection.startIndex,
347
+ textNode.toPlainText ().length,
348
+ texts.first,
349
+ );
350
+ } else if (i == length - 1 ) {
351
+ replaceText (
352
+ textNode,
353
+ 0 ,
354
+ selection.endIndex,
355
+ texts.last,
356
+ );
357
+ } else {
358
+ if (i < texts.length - 1 ) {
359
+ replaceText (
360
+ textNode,
361
+ 0 ,
362
+ textNode.toPlainText ().length,
363
+ texts[i],
364
+ );
365
+ } else {
366
+ deleteNode (textNode);
367
+ }
368
+ }
369
+ }
370
+ afterSelection = null ;
371
+ return ;
372
+ }
373
+
374
+ if (textNodes.length < texts.length) {
375
+ final length = texts.length;
376
+ for (var i = 0 ; i < texts.length; i++ ) {
377
+ final text = texts[i];
378
+ if (i == 0 ) {
379
+ replaceText (
380
+ textNodes.first,
381
+ selection.startIndex,
382
+ textNodes.first.toPlainText ().length,
383
+ text,
384
+ );
385
+ } else if (i == length - 1 ) {
386
+ replaceText (
387
+ textNodes.last,
388
+ 0 ,
389
+ selection.endIndex,
330
390
text,
331
391
);
392
+ } else {
393
+ if (i < textNodes.length - 1 ) {
394
+ replaceText (
395
+ textNodes[i],
396
+ 0 ,
397
+ textNodes[i].toPlainText ().length,
398
+ text,
399
+ );
400
+ } else {
401
+ var path = textNodes.first.path;
402
+ var j = i - textNodes.length + length - 1 ;
403
+ while (j > 0 ) {
404
+ path = path.next;
405
+ j-- ;
406
+ }
407
+ insertNode (path, TextNode (delta: Delta ()..insert (text)));
408
+ }
332
409
}
333
410
}
411
+ afterSelection = null ;
412
+ return ;
334
413
}
335
414
}
336
415
}
0 commit comments