Skip to content

Commit 071a555

Browse files
committed
Shrink range to only include sections with non-zero selection when toggling sections 🐔
This commit removes the special casing of ‘ul’ sections (which was only needed to make tests pass).
1 parent e18703b commit 071a555

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

src/js/editor/post.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ function isListSectionTagName(tagName) {
1515
return tagName === 'ul' || tagName === 'ol';
1616
}
1717

18+
function shrinkRange(range) {
19+
const { head, tail } = range;
20+
21+
if (tail.offset === 0 && head.section !== tail.section) {
22+
range.tail = new Position(tail.section.prev, tail.section.prev.length);
23+
}
24+
25+
return range;
26+
}
27+
1828
const CALLBACK_QUEUES = {
1929
BEFORE_COMPLETE: 'beforeComplete',
2030
COMPLETE: 'complete',
@@ -29,7 +39,6 @@ const EDIT_ACTIONS = {
2939
DELETE: 2
3040
};
3141

32-
3342
/**
3443
* The PostEditor is used to modify a post. It should not be instantiated directly.
3544
* Instead, a new instance of a PostEditor is created by the editor and passed
@@ -786,22 +795,13 @@ class PostEditor {
786795
* @public
787796
*/
788797
toggleSection(sectionTagName, range=this._range) {
789-
range = toRange(range);
790-
791-
const { head, tail } = range;
792-
const togglingList = sectionTagName === 'ul';
793-
const tailNotSelected = tail.offset === 0 && head.section !== tail.section;
798+
range = shrinkRange(toRange(range));
794799

795800
sectionTagName = normalizeTagName(sectionTagName);
796801
let { post } = this.editor;
797802

798803
let everySectionHasTagName = true;
799804
post.walkMarkerableSections(range, section => {
800-
// only care about tail section tag if tail section is selected
801-
if (!togglingList && tailNotSelected && tail.section === section) {
802-
return;
803-
}
804-
805805
if (!this._isSameSectionType(section, sectionTagName)) {
806806
everySectionHasTagName = false;
807807
}
@@ -810,14 +810,7 @@ class PostEditor {
810810
let tagName = everySectionHasTagName ? 'p' : sectionTagName;
811811
let sectionTransformations = [];
812812
post.walkMarkerableSections(range, section => {
813-
let changedSection;
814-
815-
// tail section not selected, no transform needed
816-
if (!togglingList && tailNotSelected && tail.section === section) {
817-
changedSection = section;
818-
} else {
819-
changedSection = this.changeSectionTagName(section, tagName);
820-
}
813+
let changedSection = this.changeSectionTagName(section, tagName);
821814

822815
sectionTransformations.push({
823816
from: section,

tests/unit/editor/post-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ test('#toggleSection does not update tail markup if tail offset is 0', assert =>
913913
);
914914
assert.positionIsEqual(
915915
mockEditor._renderedRange.tail,
916-
post.sections.tail.toPosition(0),
916+
post.sections.head.toPosition(3),
917917
'Maintains the selection'
918918
);
919919
});
@@ -1073,7 +1073,7 @@ test('#toggleSection toggle multiple ps -> list and list -> multiple ps', (asser
10731073
assert.equal(listSection.items.head.text, 'abc');
10741074
assert.equal(listSection.items.tail.text, '123');
10751075

1076-
range = Range.create(listSection.items.head, 0, listSection.items.tail, 0);
1076+
range = Range.create(listSection.items.head, 0, listSection.items.tail, 1);
10771077
postEditor = new PostEditor(editor);
10781078
postEditor.toggleSection('ul', range);
10791079
postEditor.complete();
@@ -1196,7 +1196,7 @@ test('#toggleSection untoggle multiple items at end of list changes them to mark
11961196
});
11971197
mockEditor = renderBuiltAbstract(post, mockEditor);
11981198
let range = Range.create(post.sections.head.items.objectAt(1), 0,
1199-
post.sections.head.items.tail, 0);
1199+
post.sections.head.items.tail, 1);
12001200

12011201
postEditor = new PostEditor(mockEditor);
12021202
postEditor.toggleSection('ul', range);
@@ -1226,7 +1226,7 @@ test('#toggleSection untoggle multiple items at start of list changes them to ma
12261226
});
12271227
mockEditor = renderBuiltAbstract(post, mockEditor);
12281228
let range = Range.create(post.sections.head.items.head, 0,
1229-
post.sections.head.items.objectAt(1), 0);
1229+
post.sections.head.items.objectAt(1), 1);
12301230

12311231
postEditor = new PostEditor(mockEditor);
12321232
postEditor.toggleSection('ul', range);
@@ -1262,7 +1262,7 @@ test('#toggleSection untoggle items and overflowing markup sections changes the
12621262
editor.render(editorElement);
12631263
let { post } = editor;
12641264
let range = Range.create(post.sections.head.items.objectAt(1), 0,
1265-
post.sections.tail, 0);
1265+
post.sections.tail, 1);
12661266

12671267
postEditor = new PostEditor(editor);
12681268
postEditor.toggleSection('ul', range);

0 commit comments

Comments
 (0)