-
Notifications
You must be signed in to change notification settings - Fork 156
/
post.js
1039 lines (913 loc) · 31.1 KB
/
post.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
DEFAULT_TAG_NAME as DEFAULT_MARKUP_SECTION_TAG_NAME
} from '../models/markup-section';
import { POST_TYPE, MARKUP_SECTION_TYPE, LIST_ITEM_TYPE, LIST_SECTION_TYPE } from '../models/types';
import Position from '../utils/cursor/position';
import { isArrayEqual, forEach, filter, compact } from '../utils/array-utils';
import { DIRECTION } from '../utils/key';
import LifecycleCallbacksMixin from '../utils/lifecycle-callbacks';
import mixin from '../utils/mixin';
import assert from '../utils/assert';
function isJoinable(section1, section2) {
return section1.isMarkerable &&
section2.isMarkerable &&
section1.type === section2.type &&
section1.tagName === section2.tagName;
}
function endPosition(section) {
if (section.isMarkerable) {
return new Position(section, section.length);
} else if (section.type === LIST_SECTION_TYPE) {
return endPosition(section.items.tail);
} else {
return new Position(section, 0);
}
}
function isMarkupSection(section) {
return section.type === MARKUP_SECTION_TYPE;
}
function isListItem(section) {
return section.type === LIST_ITEM_TYPE;
}
function isBlankAndListItem(section) {
return isListItem(section) && section.isBlank;
}
const CALLBACK_QUEUES = {
BEFORE_COMPLETE: 'beforeComplete',
COMPLETE: 'complete',
AFTER_COMPLETE: 'afterComplete'
};
class PostEditor {
constructor(editor) {
this.editor = editor;
this.builder = this.editor.builder;
this._didScheduleRerender = false;
this._didScheduleUpdate = false;
this._didComplete = false;
}
/**
* Remove a range from the post
*
* Usage:
*
* const range = editor.cursor.offsets;
* editor.run((postEditor) => {
* postEditor.deleteRange(range);
* });
*
* @method deleteRange
* @param {Range} range Cursor Range object with head and tail Positions
* @return {Position} The position where the cursor would go after deletion
* @public
*/
deleteRange(range) {
// types of selection deletion:
// * a selection starts at the beginning of a section
// -- cursor should end up at the beginning of that section
// -- if the section not longer has markers, add a blank one for the cursor to focus on
// * a selection is entirely within a section
// -- split the markers with the selection, remove those new markers from their section
// -- cursor goes at end of the marker before the selection start, or if the
// -- selection was at the start of the section, cursor goes at section start
// * a selection crosses multiple sections
// -- remove all the sections that are between (exclusive) selection start and end
// -- join the start and end sections
// -- mark the end section for removal
// -- cursor goes at end of marker before the selection start
let {
head: {section: headSection, offset: headSectionOffset},
tail: {section: tailSection, offset: tailSectionOffset}
} = range;
const { post } = this.editor;
let nextPosition;
if (headSection === tailSection) {
nextPosition = this.cutSection(headSection, headSectionOffset, tailSectionOffset);
} else {
let removedSections = post.sectionsContainedBy(range);
let appendSection = headSection;
post.walkLeafSections(range, section => {
switch (section) {
case headSection:
if (section.isCardSection) {
appendSection = this.builder.createMarkupSection();
if (headSectionOffset === 0) {
removedSections.push(section);
nextPosition = new Position(appendSection, 0);
} else {
nextPosition = new Position(section, 1);
}
} else {
nextPosition = this.cutSection(section, headSectionOffset, section.length);
}
break;
case tailSection:
if (section.isCardSection) {
if (tailSectionOffset === 1) {
removedSections.push(section);
}
} else {
section.markersFor(tailSectionOffset, section.text.length).forEach(m => {
appendSection.markers.append(m);
});
this._markDirty(headSection); // May have added nodes
removedSections.push(section);
}
break;
default:
if (removedSections.indexOf(section) === -1) {
removedSections.push(section);
}
}
});
if (headSection !== appendSection) {
this.insertSectionBefore(post.sections, appendSection, headSection.next);
}
removedSections.forEach(section => this.removeSection(section) );
}
return nextPosition;
}
/**
* @return {Position}
*/
cutSection(section, headSectionOffset, tailSectionOffset) {
if (section.isBlank || headSectionOffset === tailSectionOffset) {
return new Position(section, headSectionOffset);
}
if (section.isCardSection) {
let newSection = this.builder.createMarkupSection();
this.replaceSection(section, newSection);
return new Position(newSection, 0);
}
let adjustedHead = 0,
marker = section.markers.head,
adjustedTail = marker.length;
// Walk to the first node inside the headSectionOffset, splitting
// a marker if needed. Leave marker as the first node inside.
while (marker) {
if (adjustedTail >= headSectionOffset) {
let splitOffset = headSectionOffset - adjustedHead;
let { afterMarker } = this.splitMarker(marker, splitOffset);
adjustedHead = adjustedHead + splitOffset;
// FIXME: That these two loops cannot agree on adjustedTail being
// incremented at the start or end seems prime for refactoring.
adjustedTail = adjustedHead;
marker = afterMarker;
break;
}
adjustedHead += marker.length;
marker = marker.next;
if (marker) {
adjustedTail += marker.length;
}
}
// Walk each marker inside, removing it if needed. when the last is
// reached split it and remove the part inside the tailSectionOffset
while (marker) {
adjustedTail += marker.length;
if (adjustedTail >= tailSectionOffset) {
let splitOffset = marker.length - (adjustedTail - tailSectionOffset);
let {
beforeMarker
} = this.splitMarker(marker, splitOffset);
if (beforeMarker) {
this.removeMarker(beforeMarker);
}
break;
}
adjustedHead += marker.length;
let nextMarker = marker.next;
this.removeMarker(marker);
marker = nextMarker;
}
return new Position(section, headSectionOffset);
}
_coalesceMarkers(section) {
if (section.isMarkerable) {
this._removeEmptyMarkers(section);
this._joinSimilarMarkers(section);
}
}
_removeEmptyMarkers(section) {
forEach(
filter(section.markers, m => m.isEmpty),
m => this.removeMarker(m)
);
}
// joins markers that have identical markups
_joinSimilarMarkers(section) {
let marker = section.markers.head;
let nextMarker;
while (marker && marker.next) {
nextMarker = marker.next;
if (isArrayEqual(marker.markups, nextMarker.markups)) {
nextMarker.value = marker.value + nextMarker.value;
this._markDirty(nextMarker);
this.removeMarker(marker);
}
marker = nextMarker;
}
}
removeMarker(marker) {
this._scheduleForRemoval(marker);
if (marker.section) {
this._markDirty(marker.section);
marker.section.markers.remove(marker);
}
}
_scheduleForRemoval(postNode) {
if (postNode.renderNode) {
postNode.renderNode.scheduleForRemoval();
this.scheduleRerender();
this.scheduleDidUpdate();
}
}
_markDirty(postNode) {
if (postNode.renderNode) {
postNode.renderNode.markDirty();
this.scheduleRerender();
this.scheduleDidUpdate();
}
if (postNode.section) {
this._markDirty(postNode.section);
}
if (postNode.isMarkerable) {
this.addCallback(
CALLBACK_QUEUES.BEFORE_COMPLETE, () => this._coalesceMarkers(postNode));
}
}
/**
* Remove a character from a {marker, offset} position, in either
* forward or backward (default) direction.
*
* Usage:
*
* let marker = editor.post.sections.head.markers.head;
* // marker has text of "Howdy!"
* editor.run((postEditor) => {
* postEditor.deleteFrom({section, offset: 3});
* });
* // marker has text of "Hody!"
*
* `deleteFrom` may remove a character from a different marker or join the
* marker's section with the previous/next section (depending on the
* deletion direction) if direction is `BACKWARD` and the offset is 0,
* or direction is `FORWARD` and the offset is equal to the length of the
* marker.
*
* @method deleteFrom
* @param {Position} position object with {section, offset} the marker and offset to delete from
* @param {Number} direction The direction to delete in (default is BACKWARD)
* @return {Position} for positioning the cursor
* @public
*/
deleteFrom(position, direction=DIRECTION.BACKWARD) {
if (direction === DIRECTION.BACKWARD) {
return this._deleteBackwardFrom(position);
} else {
return this._deleteForwardFrom(position);
}
}
_joinPositionToPreviousSection(position) {
const {section } = position;
let nextPosition = position.clone();
if (!section.isMarkerable) {
throw new Error('Cannot join non-markerable section to previous section');
} else if (isListItem(section)) {
nextPosition = this._convertListItemToMarkupSection(section);
} else {
const prevSection = section.previousLeafSection();
if (prevSection) {
if (prevSection.isCardSection) {
if (section.isBlank) {
this.removeSection(section);
}
nextPosition = new Position(prevSection, 1);
} else {
const { beforeMarker } = prevSection.join(section);
this._markDirty(prevSection);
this.removeSection(section);
nextPosition.section = prevSection;
nextPosition.offset = beforeMarker ?
prevSection.offsetOfMarker(beforeMarker, beforeMarker.length) : 0;
}
}
}
return nextPosition;
}
/**
* delete 1 character in the FORWARD direction from the given position
* @method _deleteForwardFrom
* @param {Position} position
* @private
*/
_deleteForwardFrom(position) {
const { section, offset } = position;
if (section.isBlank) {
// remove this section, focus on start of next markerable section
const nextPosition = position.clone();
const next = section.immediatelyNextMarkerableSection();
if (next) {
this.removeSection(section);
nextPosition.section = next;
nextPosition.offset = 0;
}
return nextPosition;
} else if (offset === section.length) {
if (section.isCardSection) {
if (section.next && section.next.isBlank) {
this.removeSection(section.next);
}
return position;
} else {
// join next markerable section to this one
return this._joinPositionToNextSection(position);
}
} else {
if (section.isCardSection) {
if (offset === 0) {
let newSection = this.builder.createMarkupSection();
this.replaceSection(section, newSection);
return new Position(newSection, 0);
}
} else {
return this._deleteForwardFromMarkerPosition(position.markerPosition);
}
}
}
_joinPositionToNextSection(position) {
const { section } = position;
let nextPosition = position.clone();
if (!section.isMarkerable) {
throw new Error('Cannot join non-markerable section to next section');
} else {
const next = section.immediatelyNextMarkerableSection();
if (next) {
section.join(next);
this._markDirty(section);
this.removeSection(next);
}
}
return nextPosition;
}
_deleteForwardFromMarkerPosition(markerPosition) {
const {marker, offset} = markerPosition;
const {section} = marker;
let nextPosition = new Position(section, section.offsetOfMarker(marker, offset));
if (offset === marker.length) {
const nextMarker = marker.next;
if (nextMarker) {
const nextMarkerPosition = {marker: nextMarker, offset: 0};
return this._deleteForwardFromMarkerPosition(nextMarkerPosition);
} else {
const nextSection = marker.section.next;
if (nextSection && isMarkupSection(nextSection)) {
const currentSection = marker.section;
currentSection.join(nextSection);
this._markDirty(currentSection);
this.removeSection(nextSection);
}
}
} else {
marker.deleteValueAtOffset(offset);
this._markDirty(marker);
}
return nextPosition;
}
_convertListItemToMarkupSection(listItem) {
const listSection = listItem.parent;
const newSections = listItem.splitIntoSections();
const newMarkupSection = newSections[1];
this._replaceSection(listSection, compact(newSections));
return new Position(newMarkupSection, 0);
}
/**
* delete 1 character in the BACKWARD direction from the given position
* @method _deleteBackwardFrom
* @param {Position} position
* @return {Position} The position the cursor should be put after this deletion
* @private
*/
_deleteBackwardFrom(position) {
const { section, offset:sectionOffset } = position;
if (sectionOffset === 0) {
if (section.isCardSection) {
if (section.prev && section.prev.isBlank) {
this.removeSection(section.prev);
}
return position;
} else {
return this._joinPositionToPreviousSection(position);
}
}
let nextPosition = position.clone();
// if position is end of a card, replace the card with a markup section
if (section.isCardSection) {
let newSection = this.builder.createMarkupSection();
this.replaceSection(section, newSection);
return new Position(newSection, 0);
}
const { marker, offset:markerOffset } = position.markerPosition;
const offsetToDeleteAt = markerOffset - 1;
marker.deleteValueAtOffset(offsetToDeleteAt);
nextPosition.offset -= 1;
this._markDirty(marker);
return nextPosition;
}
/**
* Split markers at two positions, once at the head, and if necessary once
* at the tail. This method is designed to accept a range
* (e.g. `editor.cursor.offsets`) as an argument.
*
* Usage:
*
* let markerRange = this.cursor.offsets;
* editor.run((postEditor) => {
* postEditor.splitMarkers(markerRange);
* });
*
* The return value will be marker object completely inside the offsets
* provided. Markers on the outside of the split may also have been modified.
*
* @method splitMarkers
* @param {Range} markerRange
* @return {Array} of markers that are inside the split
* @private
*/
splitMarkers(range) {
const { post } = this.editor;
const { head, tail } = range;
this.splitSectionMarkerAtOffset(head.section, head.offset);
this.splitSectionMarkerAtOffset(tail.section, tail.offset);
return post.markersContainedByRange(range);
}
splitSectionMarkerAtOffset(section, offset) {
const edit = section.splitMarkerAtOffset(offset);
edit.removed.forEach(m => this.removeMarker(m));
}
splitMarker(marker, offset) {
let beforeMarker, afterMarker;
if (offset === 0) {
beforeMarker = marker.prev;
afterMarker = marker;
} else if (offset === marker.length) {
beforeMarker = marker;
afterMarker = marker.next;
} else {
let { builder } = this.editor,
{ section } = marker;
beforeMarker = builder.createMarker(marker.value.substring(0, offset), marker.markups);
afterMarker = builder.createMarker(marker.value.substring(offset, marker.length), marker.markups);
section.markers.splice(marker, 1, [beforeMarker, afterMarker]);
this.removeMarker(marker);
this._markDirty(section);
}
return { beforeMarker, afterMarker };
}
/**
* Split a section at one position. This method is designed to accept
* `editor.cursor.offsets` as an argument, but will only split at the
* head of the cursor position.
*
* Usage:
*
* let marker = editor.post.sections.head.marker.head;
* editor.run((postEditor) => {
* postEditor.splitSection({
* headSection: section,
* headSectionOffset: 3
* });
* });
* // Will result in the marker and its old section being removed from
* // the post and rendered DOM, and in the creation of two new sections
* // replacing the old one.
*
* The return value will be the two new sections. One or both of these
* sections can be blank (contain only a blank marker), for example if the
* headMarkerOffset is 0.
*
* @method splitSection
* @param {Position} position
* @return {Array} new sections, one for the first half and one for the second
* @public
*/
splitSection(position) {
const { section } = position;
if (section.isCardSection) {
return this._splitCardSection(section, position);
}
const [beforeSection, afterSection] = section.splitAtPosition(position);
this._coalesceMarkers(beforeSection);
this._coalesceMarkers(afterSection);
const newSections = [beforeSection, afterSection];
let replacementSections = [beforeSection, afterSection];
if (isBlankAndListItem(beforeSection) && isBlankAndListItem(section)) {
const isLastItemInList = section === section.parent.sections.tail;
if (isLastItemInList) {
// when hitting enter in a final empty list item, do not insert a new
// empty item
replacementSections.shift();
}
}
this._replaceSection(section, replacementSections);
// FIXME we must return 2 sections because other code expects this to always return 2
return newSections;
}
/**
* @method _splitCardSection
* @param {Section} cardSection
* @param {Position} position to split at
* @return {Array} pre and post-split sections
* @private
*/
_splitCardSection(cardSection, position) {
let { offset } = position;
assert('Cards section must be split at offset 0 or 1',
offset === 0 || offset === 1);
let newSection = this.builder.createMarkupSection();
let nextSection;
let surroundingSections;
if (offset === 0) {
nextSection = cardSection;
surroundingSections = [newSection, cardSection];
} else {
nextSection = cardSection.next;
surroundingSections = [cardSection, newSection];
}
let collection = this.editor.post.sections;
this.insertSectionBefore(collection, newSection, nextSection);
return surroundingSections;
}
/**
* @method replaceSection
* @param {Section} section
* @param {Section} newSection
* @return null
* @public
*/
replaceSection(section, newSection) {
if (!section) {
// The section may be undefined if the user used the embed intent
// ("+" icon) to insert a new "ul" section in a blank post
this.insertSectionBefore(this.editor.post.sections, newSection);
} else {
this._replaceSection(section, [newSection]);
}
}
moveSectionBefore(collection, renderedSection, beforeSection) {
const newSection = renderedSection.clone();
this.removeSection(renderedSection);
this.insertSectionBefore(collection, newSection, beforeSection);
return newSection;
}
/**
* @method moveSectionUp
* @param {Section} section A section that is already in DOM
* @public
*/
moveSectionUp(renderedSection) {
const isFirst = !renderedSection.prev;
if (isFirst) {
return renderedSection;
}
const collection = renderedSection.parent.sections;
const beforeSection = renderedSection.prev;
return this.moveSectionBefore(collection, renderedSection, beforeSection);
}
/**
* @method moveSectionDown
* @param {Section} section A section that is already in DOM
* @public
*/
moveSectionDown(renderedSection) {
const isLast = !renderedSection.next;
if (isLast) {
return renderedSection;
}
const beforeSection = renderedSection.next.next;
const collection = renderedSection.parent.sections;
return this.moveSectionBefore(collection, renderedSection, beforeSection);
}
insertText(position, text) {
let section = position.section;
if (!section.isMarkerable) {
return;
}
let {marker,offset} = position.markerPosition;
let nextPosition = position;
if (!marker) {
marker = this.builder.createMarker(text);
section.markers.append(marker);
this._markDirty(section);
nextPosition = new Position(section, 1);
} else if (marker) {
let markerHead = marker.value.slice(0, offset);
let markerTail = marker.value.slice(offset, marker.length);
marker.value = `${markerHead}${text}${markerTail}`;
this._markDirty(marker);
nextPosition = position.moveRight();
}
return nextPosition;
}
_replaceSection(section, newSections) {
let nextSection = section.next;
let collection = section.parent.sections;
let nextNewSection = newSections[0];
if (isMarkupSection(nextNewSection) && isListItem(section)) {
// put the new section after the ListSection (section.parent) instead of after the ListItem
collection = section.parent.parent.sections;
nextSection = section.parent.next;
}
newSections.forEach(s => this.insertSectionBefore(collection, s, nextSection));
this.removeSection(section);
}
/**
* Given a markerRange (for example `editor.cursor.offsets`) mark all markers
* inside it as a given markup. The markup must be provided as a post
* abstract node.
*
* Usage:
*
* const range = editor.cursor.offsets;
* const strongMarkup = editor.builder.createMarkup('strong');
* editor.run((postEditor) => {
* postEditor.addMarkupToRange(range, strongMarkup);
* });
* // Will result some markers possibly being split, and the markup
* // being applied to all markers between the split.
*
* The return value will be all markers between the split, the same return
* value as `splitMarkers`.
*
* @method addMarkupToRange
* @param {Range} range
* @param {Markup} markup A markup post abstract node
* @public
*/
addMarkupToRange(range, markup) {
if (range.isCollapsed) {
return;
}
this.splitMarkers(range).forEach(marker => {
marker.addMarkup(markup);
this._markDirty(marker);
});
}
/**
* Given a markerRange (for example `editor.cursor.offsets`) remove the given
* markup from all contained markers. The markup must be provided as a post
* abstract node.
*
* Usage:
*
* const range = editor.cursor.offsets;
* const markup = markerRange.headMarker.markups[0];
* editor.run(postEditor => {
* postEditor.removeMarkupFromRange(range, markup);
* });
* // Will result in some markers possibly being split, and the markup
* // being removed from all markers between the split.
*
* The return value will be all markers between the split, the same return
* value as `splitMarkers`.
*
* @method removeMarkupFromRange
* @param {Range} range Object with offsets
* @param {Markup} markup A markup post abstract node
* @private
*/
removeMarkupFromRange(range, markupOrMarkupCallback) {
if (range.isCollapsed) {
return;
}
this.splitMarkers(range).forEach(marker => {
marker.removeMarkup(markupOrMarkupCallback);
this._markDirty(marker);
});
}
/**
* Toggle the given markup on the current selection. If anything in the current
* selection has the markup, the markup will be removed from it. If nothing in the selection
* has the markup, the markup will be added to everything in the selection.
*
* Usage:
*
* // Remove any 'strong' markup if it exists in the selection, otherwise
* // make it all 'strong'
* editor.run(postEditor => postEditor.toggleMarkup('strong'));
*
* // add/remove a link to 'bustle.com' to the selection
* editor.run(postEditor => {
* const linkMarkup = postEditor.builder.createMarkup('a', {href: 'http://bustle.com'});
* postEditor.toggleMarkup(linkMarkup);
* });
*
* @method toggleMarkup
* @param {Markup|String} markupOrString Either a markup object created using
* the builder (useful when adding a markup with attributes, like an 'a' markup),
* or, if a string, the tag name of the markup (e.g. 'strong', 'em') to toggle.
*/
toggleMarkup(markupOrMarkupString) {
const range = this.editor.cursor.offsets;
if (range.isCollapsed) {
return;
}
const markup = typeof markupOrMarkupString === 'string' ?
this.builder.createMarkup(markupOrMarkupString) :
markupOrMarkupString;
const hasMarkup = this.editor.detectMarkupInRange(range, markup.tagName);
// FIXME: This implies only a single markup in a range. This may not be
// true for links (which are not the same object instance like multiple
// strong tags would be).
if (hasMarkup) {
this.removeMarkupFromRange(range, hasMarkup);
} else {
this.addMarkupToRange(range, markup);
}
this.scheduleAfterRender(() => this.editor.selectRange(range));
}
changeSectionTagName(section, newTagName) {
section.markers.forEach(m => {
m.clearMarkups();
this._markDirty(m);
});
section.tagName = newTagName;
this._markDirty(section);
}
resetSectionTagName(section) {
this.changeSectionTagName(section, DEFAULT_MARKUP_SECTION_TAG_NAME);
}
/**
* Insert a given section before another one, updating the post abstract
* and the rendered UI.
*
* Usage:
*
* let markerRange = editor.cursor.offsets;
* let sectionWithCursor = markerRange.headMarker.section;
* let section = editor.builder.createCardSection('my-image');
* let collection = sectionWithCursor.parent.sections;
* editor.run((postEditor) => {
* postEditor.insertSectionBefore(collection, section, sectionWithCursor);
* });
*
* @method insertSectionBefore
* @param {LinkedList} collection The list of sections to insert into
* @param {Object} section The new section
* @param {Object} beforeSection Optional The section "before" is relative to,
* if falsy the new section will be appended to the collection
* @public
*/
insertSectionBefore(collection, section, beforeSection) {
collection.insertBefore(section, beforeSection);
this._markDirty(section.parent);
}
/**
* Insert the given section after the current active section, or, if no
* section is active, at the end of the document.
* @method insertSection
* @param {Section} section
* @public
*/
insertSection(section) {
const activeSection = this.editor.activeSection;
const nextSection = activeSection && activeSection.next;
const collection = this.editor.post.sections;
this.insertSectionBefore(collection, section, nextSection);
}
/**
* Insert the given section at the end of the document.
* @method insertSectionAtEnd
* @param {Section} section
* @public
*/
insertSectionAtEnd(section) {
this.insertSectionBefore(this.editor.post.sections, section, null);
}
/**
* @method insertPost
* @param {Position} position
* @param {Post} post
* @return {Position} position at end of inserted content
* @private
*/
insertPost(position, newPost) {
if (newPost.isBlank) {
return position;
}
const post = this.editor.post;
let [preSplit, postSplit] = this.splitSection(position);
let nextPosition = position.clone();
newPost.sections.forEach((section, index) => {
if (index === 0 &&
isJoinable(preSplit, section)) {
preSplit.join(section);
this._markDirty(preSplit);
nextPosition = endPosition(preSplit);
} else {
section = section.clone();
this.insertSectionBefore(post.sections, section, postSplit);
nextPosition = endPosition(section);
}
});
if (postSplit.isBlank) {
this.removeSection(postSplit);
}
if (isJoinable(preSplit, postSplit) &&
preSplit.next === postSplit) {
nextPosition = endPosition(preSplit);
preSplit.join(postSplit);
this._markDirty(preSplit);
this.removeSection(postSplit);
} else if (preSplit.isBlank) {
this.removeSection(preSplit);
}
return nextPosition;
}
/**
* Remove a given section from the post abstract and the rendered UI.
*
* Usage:
*
* const range = editor.cursor.offsets;
* const sectionWithCursor = range.head.section;
* editor.run((postEditor) => {
* postEditor.removeSection(sectionWithCursor);
* });
*
* @method removeSection
* @param {Object} section The section to remove
* @public
*/
removeSection(section) {
const parent = section.parent;
const parentIsRemoved = parent.renderNode.isRemoved;
if (parentIsRemoved) {
// This can happen if we remove a list section and later
// try to remove one of the section's list items;
return;
}
this._scheduleForRemoval(section);
parent.sections.remove(section);
if (parent.isBlank && parent.type !== POST_TYPE) {
// If we removed the last child from a parent (e.g. the last li in a ul),
// also remove the parent
this.removeSection(parent);
}
}
/**
* A method for adding work the deferred queue
*
* @method schedule
* @param {Function} callback to run during completion
* @public
*/
schedule(callback) {
if (this._didComplete) {
throw new Error('Work can only be scheduled before a post edit has completed');
}
this.addCallback(CALLBACK_QUEUES.COMPLETE, callback);
}
/**
* Add a rerender job to the queue
*
* @method scheduleRerender
* @public
*/
scheduleRerender() {
if (this._didScheduleRerender) { return; }
this.schedule(() => this.editor.rerender());
this._didScheduleRerender = true;
}