@@ -227,7 +227,7 @@ void main() {
227
227
});
228
228
229
229
group ("custom serializers >" , () {
230
- test ("can serialize inline embeds" , () {
230
+ test ("can serialize inline embeds from attributions " , () {
231
231
const userMentionAttribution = _UserTagAttribution ("123456" );
232
232
233
233
final deltas = MutableDocument (
@@ -280,6 +280,164 @@ void main() {
280
280
expect (deltas, quillDocumentEquivalentTo (expectedDeltas));
281
281
});
282
282
283
+ group ("inline placeholders >" , () {
284
+ test ("in the middle of text" , () {
285
+ final deltas = MutableDocument (
286
+ nodes: [
287
+ ParagraphNode (
288
+ id: "1" ,
289
+ text: AttributedText (
290
+ "Before images >< in between images >< after images." ,
291
+ null ,
292
+ {
293
+ 15 : const _InlineImage ("http://www.somedomain.com/image1.png" ),
294
+ 37 : const _InlineImage ("http://www.somedomain.com/image2.png" ),
295
+ },
296
+ ),
297
+ ),
298
+ ],
299
+ ).toQuillDeltas (
300
+ serializers: _serializersWithInlineEmbeds,
301
+ );
302
+
303
+ final expectedDeltas = Delta .fromJson ([
304
+ {"insert" : "Before images >" },
305
+ {
306
+ "insert" : {
307
+ "image" : {
308
+ "url" : "http://www.somedomain.com/image1.png" ,
309
+ },
310
+ },
311
+ },
312
+ {"insert" : "< in between images >" },
313
+ {
314
+ "insert" : {
315
+ "image" : {
316
+ "url" : "http://www.somedomain.com/image2.png" ,
317
+ },
318
+ },
319
+ },
320
+ {"insert" : "< after images.\n " },
321
+ ]);
322
+
323
+ expect (deltas, quillDocumentEquivalentTo (expectedDeltas));
324
+ });
325
+
326
+ test ("at the start and end of text" , () {
327
+ final deltas = MutableDocument (
328
+ nodes: [
329
+ ParagraphNode (
330
+ id: "1" ,
331
+ text: AttributedText (
332
+ " < Text between images > " ,
333
+ null ,
334
+ {
335
+ 0 : const _InlineImage ("http://www.somedomain.com/image1.png" ),
336
+ 26 : const _InlineImage ("http://www.somedomain.com/image2.png" ),
337
+ },
338
+ ),
339
+ ),
340
+ ],
341
+ ).toQuillDeltas (
342
+ serializers: _serializersWithInlineEmbeds,
343
+ );
344
+
345
+ final expectedDeltas = Delta .fromJson ([
346
+ {
347
+ "insert" : {
348
+ "image" : {
349
+ "url" : "http://www.somedomain.com/image1.png" ,
350
+ },
351
+ },
352
+ },
353
+ {"insert" : " < Text between images > " },
354
+ {
355
+ "insert" : {
356
+ "image" : {
357
+ "url" : "http://www.somedomain.com/image2.png" ,
358
+ },
359
+ },
360
+ },
361
+ {"insert" : "\n " },
362
+ ]);
363
+
364
+ expect (deltas, quillDocumentEquivalentTo (expectedDeltas));
365
+ });
366
+
367
+ test ("within attribution spans" , () {
368
+ final deltas = MutableDocument (
369
+ nodes: [
370
+ ParagraphNode (
371
+ id: "1" ,
372
+ text: AttributedText (
373
+ "Before attribution |< text >< text >| after attribution." ,
374
+ AttributedSpans (
375
+ attributions: [
376
+ const SpanMarker (
377
+ attribution: boldAttribution,
378
+ offset: 20 ,
379
+ markerType: SpanMarkerType .start,
380
+ ),
381
+ const SpanMarker (
382
+ attribution: boldAttribution,
383
+ offset: 38 ,
384
+ markerType: SpanMarkerType .end,
385
+ ),
386
+ ],
387
+ ),
388
+ {
389
+ 20 : const _InlineImage ("http://www.somedomain.com/image1.png" ),
390
+ 29 : const _InlineImage ("http://www.somedomain.com/image2.png" ),
391
+ 38 : const _InlineImage ("http://www.somedomain.com/image3.png" ),
392
+ },
393
+ ),
394
+ ),
395
+ ],
396
+ ).toQuillDeltas (
397
+ serializers: _serializersWithInlineEmbeds,
398
+ );
399
+
400
+ final expectedDeltas = Delta .fromJson ([
401
+ {"insert" : "Before attribution |" },
402
+ {
403
+ "insert" : {
404
+ "image" : {
405
+ "url" : "http://www.somedomain.com/image1.png" ,
406
+ },
407
+ },
408
+ "attributes" : {"bold" : true },
409
+ },
410
+ {
411
+ "insert" : "< text >" ,
412
+ "attributes" : {"bold" : true },
413
+ },
414
+ {
415
+ "insert" : {
416
+ "image" : {
417
+ "url" : "http://www.somedomain.com/image2.png" ,
418
+ },
419
+ },
420
+ "attributes" : {"bold" : true },
421
+ },
422
+ {
423
+ "insert" : "< text >" ,
424
+ "attributes" : {"bold" : true },
425
+ },
426
+ {
427
+ "insert" : {
428
+ "image" : {
429
+ "url" : "http://www.somedomain.com/image3.png" ,
430
+ },
431
+ },
432
+ "attributes" : {"bold" : true },
433
+ },
434
+ {"insert" : "| after attribution.\n " },
435
+ ]);
436
+
437
+ expect (deltas, quillDocumentEquivalentTo (expectedDeltas));
438
+ });
439
+ });
440
+
283
441
test ("doesn't merge custom block with previous delta" , () {
284
442
final deltas = MutableDocument (
285
443
nodes: [
@@ -332,13 +490,49 @@ const _serializersWithInlineEmbeds = [
332
490
fileDeltaSerializer,
333
491
];
334
492
335
- const _inlineEmbedSerializers = [_UserTagInlineEmbedSerializer ()];
493
+ const _inlineEmbedSerializers = [
494
+ _InlineImageEmbedSerializer (),
495
+ _UserTagInlineEmbedSerializer (),
496
+ ];
497
+
498
+ class _InlineImageEmbedSerializer implements InlineEmbedDeltaSerializer {
499
+ const _InlineImageEmbedSerializer ();
500
+
501
+ @override
502
+ bool serializeText (String text, Set <Attribution > attributions, Delta deltas) => false ;
503
+
504
+ @override
505
+ bool serializeInlinePlaceholder (Object placeholder, Map <String , dynamic > attributes, Delta deltas) {
506
+ if (placeholder is ! _InlineImage ) {
507
+ return false ;
508
+ }
509
+
510
+ deltas.operations.add (
511
+ Operation .insert (
512
+ {
513
+ "image" : {
514
+ "url" : placeholder.url,
515
+ },
516
+ },
517
+ attributes.isNotEmpty ? attributes : null ,
518
+ ),
519
+ );
520
+
521
+ return true ;
522
+ }
523
+ }
524
+
525
+ class _InlineImage {
526
+ const _InlineImage (this .url);
527
+
528
+ final String url;
529
+ }
336
530
337
531
class _UserTagInlineEmbedSerializer implements InlineEmbedDeltaSerializer {
338
532
const _UserTagInlineEmbedSerializer ();
339
533
340
534
@override
341
- bool serialize (String text, Set <Attribution > attributions, Delta deltas) {
535
+ bool serializeText (String text, Set <Attribution > attributions, Delta deltas) {
342
536
final userTag = attributions.whereType <_UserTagAttribution >().firstOrNull;
343
537
if (userTag == null ) {
344
538
return false ;
@@ -356,6 +550,9 @@ class _UserTagInlineEmbedSerializer implements InlineEmbedDeltaSerializer {
356
550
357
551
return true ;
358
552
}
553
+
554
+ @override
555
+ bool serializeInlinePlaceholder (Object placeholder, Map <String , dynamic > attributes, Delta deltas) => false ;
359
556
}
360
557
361
558
class _UserTagAttribution implements Attribution {
0 commit comments