Skip to content

Commit 95146e0

Browse files
YoranBrondsemaZeeJab
authored andcommitted
Fix #689
1 parent 4005732 commit 95146e0

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

src/js/editor/post.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,6 @@ class PostEditor {
790790

791791
sectionTagName = normalizeTagName(sectionTagName);
792792
let { post } = this.editor;
793-
let nextRange = range;
794793

795794
let everySectionHasTagName = true;
796795
post.walkMarkerableSections(range, section => {
@@ -800,18 +799,47 @@ class PostEditor {
800799
});
801800

802801
let tagName = everySectionHasTagName ? 'p' : sectionTagName;
803-
let firstChanged;
802+
let sectionTransformations = [];
804803
post.walkMarkerableSections(range, section => {
805804
let changedSection = this.changeSectionTagName(section, tagName);
806-
firstChanged = firstChanged || changedSection;
805+
sectionTransformations.push({
806+
from: section,
807+
to: changedSection
808+
});
807809
});
808810

809-
if (firstChanged) {
810-
nextRange = firstChanged.headPosition().toRange();
811-
}
811+
let nextRange = this._determineNextRangeAfterToggleSection(range, sectionTransformations);
812812
this.setRange(nextRange);
813813
}
814814

815+
_determineNextRangeAfterToggleSection(range, sectionTransformations) {
816+
if (sectionTransformations.length) {
817+
let changedHeadSection = sectionTransformations
818+
.find(({ from }) => { return from === range.headSection; })
819+
.to;
820+
let changedTailSection = sectionTransformations
821+
.find(({ from }) => { return from === range.tailSection; })
822+
.to;
823+
824+
if (changedHeadSection.isListSection || changedTailSection.isListSection) {
825+
// We don't know to which ListItem's the original sections point at, so
826+
// we don't have enough information to reconstruct the range when
827+
// dealing with lists.
828+
return sectionTransformations[0].to.headPosition().toRange();
829+
} else {
830+
return Range.create(
831+
changedHeadSection,
832+
range.headSectionOffset,
833+
changedTailSection,
834+
range.tailSectionOffset,
835+
range.direction
836+
);
837+
}
838+
} else {
839+
return range;
840+
}
841+
}
842+
815843
setAttribute(key, value, range=this._range) {
816844
range = toRange(range);
817845
let { post } = this.editor;

tests/unit/editor/post-test.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,16 @@ test('#toggleSection changes multiple sections to and from tag name', (assert) =
815815
assert.equal(post.sections.head.tagName, 'p');
816816
assert.equal(post.sections.tail.tagName, 'p');
817817

818-
assert.positionIsEqual(mockEditor._renderedRange.head, post.sections.head.headPosition());
818+
assert.positionIsEqual(
819+
mockEditor._renderedRange.head,
820+
post.sections.head.toPosition(2),
821+
'Maintains the selection'
822+
);
823+
assert.positionIsEqual(
824+
mockEditor._renderedRange.tail,
825+
post.sections.tail.toPosition(2),
826+
'Maintains the selection'
827+
);
819828
});
820829

821830
test('#toggleSection skips over non-markerable sections', (assert) => {
@@ -1301,6 +1310,33 @@ test('#toggleSection joins contiguous list items', (assert) => {
13011310
['abc', '123', 'def']);
13021311
});
13031312

1313+
test('#toggleSection maintains the selection when the sections in the selected range are still there', (assert) => {
1314+
let post = Helpers.postAbstract.build(({post, markupSection, marker}) => {
1315+
return post([
1316+
markupSection('p', [marker('abc')])
1317+
]);
1318+
});
1319+
1320+
mockEditor = renderBuiltAbstract(post, mockEditor);
1321+
const range = Range.create(post.sections.head, 1,
1322+
post.sections.head, 2);
1323+
1324+
postEditor = new PostEditor(mockEditor);
1325+
postEditor.toggleSection('h1', range);
1326+
postEditor.complete();
1327+
1328+
assert.positionIsEqual(
1329+
mockEditor._renderedRange.head,
1330+
post.sections.head.toPosition(1),
1331+
'Maintains the selection'
1332+
);
1333+
assert.positionIsEqual(
1334+
mockEditor._renderedRange.tail,
1335+
post.sections.tail.toPosition(2),
1336+
'Maintains the selection'
1337+
);
1338+
});
1339+
13041340
test('#toggleMarkup when cursor is in non-markerable does nothing', (assert) => {
13051341
editor = buildEditorWithMobiledoc(
13061342
({post, markupSection, marker, cardSection}) => {

0 commit comments

Comments
 (0)