@@ -244,7 +244,7 @@ void main() {
244
244
});
245
245
246
246
group ("custom serializers >" , () {
247
- test ("can serialize inline embeds" , () {
247
+ test ("can serialize inline embeds from attributions " , () {
248
248
const userMentionAttribution = _UserTagAttribution ("123456" );
249
249
250
250
final deltas = MutableDocument (
@@ -297,6 +297,164 @@ void main() {
297
297
expect (deltas, quillDocumentEquivalentTo (expectedDeltas));
298
298
});
299
299
300
+ group ("inline placeholders >" , () {
301
+ test ("in the middle of text" , () {
302
+ final deltas = MutableDocument (
303
+ nodes: [
304
+ ParagraphNode (
305
+ id: "1" ,
306
+ text: AttributedText (
307
+ "Before images >< in between images >< after images." ,
308
+ null ,
309
+ {
310
+ 15 : const _InlineImage ("http://www.somedomain.com/image1.png" ),
311
+ 37 : const _InlineImage ("http://www.somedomain.com/image2.png" ),
312
+ },
313
+ ),
314
+ ),
315
+ ],
316
+ ).toQuillDeltas (
317
+ serializers: _serializersWithInlineEmbeds,
318
+ );
319
+
320
+ final expectedDeltas = Delta .fromJson ([
321
+ {"insert" : "Before images >" },
322
+ {
323
+ "insert" : {
324
+ "image" : {
325
+ "url" : "http://www.somedomain.com/image1.png" ,
326
+ },
327
+ },
328
+ },
329
+ {"insert" : "< in between images >" },
330
+ {
331
+ "insert" : {
332
+ "image" : {
333
+ "url" : "http://www.somedomain.com/image2.png" ,
334
+ },
335
+ },
336
+ },
337
+ {"insert" : "< after images.\n " },
338
+ ]);
339
+
340
+ expect (deltas, quillDocumentEquivalentTo (expectedDeltas));
341
+ });
342
+
343
+ test ("at the start and end of text" , () {
344
+ final deltas = MutableDocument (
345
+ nodes: [
346
+ ParagraphNode (
347
+ id: "1" ,
348
+ text: AttributedText (
349
+ " < Text between images > " ,
350
+ null ,
351
+ {
352
+ 0 : const _InlineImage ("http://www.somedomain.com/image1.png" ),
353
+ 26 : const _InlineImage ("http://www.somedomain.com/image2.png" ),
354
+ },
355
+ ),
356
+ ),
357
+ ],
358
+ ).toQuillDeltas (
359
+ serializers: _serializersWithInlineEmbeds,
360
+ );
361
+
362
+ final expectedDeltas = Delta .fromJson ([
363
+ {
364
+ "insert" : {
365
+ "image" : {
366
+ "url" : "http://www.somedomain.com/image1.png" ,
367
+ },
368
+ },
369
+ },
370
+ {"insert" : " < Text between images > " },
371
+ {
372
+ "insert" : {
373
+ "image" : {
374
+ "url" : "http://www.somedomain.com/image2.png" ,
375
+ },
376
+ },
377
+ },
378
+ {"insert" : "\n " },
379
+ ]);
380
+
381
+ expect (deltas, quillDocumentEquivalentTo (expectedDeltas));
382
+ });
383
+
384
+ test ("within attribution spans" , () {
385
+ final deltas = MutableDocument (
386
+ nodes: [
387
+ ParagraphNode (
388
+ id: "1" ,
389
+ text: AttributedText (
390
+ "Before attribution |< text >< text >| after attribution." ,
391
+ AttributedSpans (
392
+ attributions: [
393
+ const SpanMarker (
394
+ attribution: boldAttribution,
395
+ offset: 20 ,
396
+ markerType: SpanMarkerType .start,
397
+ ),
398
+ const SpanMarker (
399
+ attribution: boldAttribution,
400
+ offset: 38 ,
401
+ markerType: SpanMarkerType .end,
402
+ ),
403
+ ],
404
+ ),
405
+ {
406
+ 20 : const _InlineImage ("http://www.somedomain.com/image1.png" ),
407
+ 29 : const _InlineImage ("http://www.somedomain.com/image2.png" ),
408
+ 38 : const _InlineImage ("http://www.somedomain.com/image3.png" ),
409
+ },
410
+ ),
411
+ ),
412
+ ],
413
+ ).toQuillDeltas (
414
+ serializers: _serializersWithInlineEmbeds,
415
+ );
416
+
417
+ final expectedDeltas = Delta .fromJson ([
418
+ {"insert" : "Before attribution |" },
419
+ {
420
+ "insert" : {
421
+ "image" : {
422
+ "url" : "http://www.somedomain.com/image1.png" ,
423
+ },
424
+ },
425
+ "attributes" : {"bold" : true },
426
+ },
427
+ {
428
+ "insert" : "< text >" ,
429
+ "attributes" : {"bold" : true },
430
+ },
431
+ {
432
+ "insert" : {
433
+ "image" : {
434
+ "url" : "http://www.somedomain.com/image2.png" ,
435
+ },
436
+ },
437
+ "attributes" : {"bold" : true },
438
+ },
439
+ {
440
+ "insert" : "< text >" ,
441
+ "attributes" : {"bold" : true },
442
+ },
443
+ {
444
+ "insert" : {
445
+ "image" : {
446
+ "url" : "http://www.somedomain.com/image3.png" ,
447
+ },
448
+ },
449
+ "attributes" : {"bold" : true },
450
+ },
451
+ {"insert" : "| after attribution.\n " },
452
+ ]);
453
+
454
+ expect (deltas, quillDocumentEquivalentTo (expectedDeltas));
455
+ });
456
+ });
457
+
300
458
test ("doesn't merge custom block with previous delta" , () {
301
459
final deltas = MutableDocument (
302
460
nodes: [
@@ -349,13 +507,49 @@ const _serializersWithInlineEmbeds = [
349
507
fileDeltaSerializer,
350
508
];
351
509
352
- const _inlineEmbedSerializers = [_UserTagInlineEmbedSerializer ()];
510
+ const _inlineEmbedSerializers = [
511
+ _InlineImageEmbedSerializer (),
512
+ _UserTagInlineEmbedSerializer (),
513
+ ];
514
+
515
+ class _InlineImageEmbedSerializer implements InlineEmbedDeltaSerializer {
516
+ const _InlineImageEmbedSerializer ();
517
+
518
+ @override
519
+ bool serializeText (String text, Set <Attribution > attributions, Delta deltas) => false ;
520
+
521
+ @override
522
+ bool serializeInlinePlaceholder (Object placeholder, Map <String , dynamic > attributes, Delta deltas) {
523
+ if (placeholder is ! _InlineImage ) {
524
+ return false ;
525
+ }
526
+
527
+ deltas.operations.add (
528
+ Operation .insert (
529
+ {
530
+ "image" : {
531
+ "url" : placeholder.url,
532
+ },
533
+ },
534
+ attributes.isNotEmpty ? attributes : null ,
535
+ ),
536
+ );
537
+
538
+ return true ;
539
+ }
540
+ }
541
+
542
+ class _InlineImage {
543
+ const _InlineImage (this .url);
544
+
545
+ final String url;
546
+ }
353
547
354
548
class _UserTagInlineEmbedSerializer implements InlineEmbedDeltaSerializer {
355
549
const _UserTagInlineEmbedSerializer ();
356
550
357
551
@override
358
- bool serialize (String text, Set <Attribution > attributions, Delta deltas) {
552
+ bool serializeText (String text, Set <Attribution > attributions, Delta deltas) {
359
553
final userTag = attributions.whereType <_UserTagAttribution >().firstOrNull;
360
554
if (userTag == null ) {
361
555
return false ;
@@ -373,6 +567,9 @@ class _UserTagInlineEmbedSerializer implements InlineEmbedDeltaSerializer {
373
567
374
568
return true ;
375
569
}
570
+
571
+ @override
572
+ bool serializeInlinePlaceholder (Object placeholder, Map <String , dynamic > attributes, Delta deltas) => false ;
376
573
}
377
574
378
575
class _UserTagAttribution implements Attribution {
0 commit comments