Skip to content

Commit

Permalink
Report active markups even when selection is collapsed
Browse files Browse the repository at this point in the history
This makes `markupsInSelection` report active markups even when the selection is collapsed.

If you run the demo, you can see why this is helpful:

 - mark some words bold
 - move the cursor in and out of the bold section
 - observe that the toolbar button for "Bold" correctly shows active state when you're inside the bold region.
  • Loading branch information
ef4 committed Jan 25, 2016
1 parent 4bd1224 commit b85724e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class Editor {
}

get markupsInSelection() {
if (this.cursor.hasSelection()) {
if (this.cursor.hasCursor()) {
const range = this.cursor.offsets;
return this.post.markupsInRange(range);
} else {
Expand Down
23 changes: 23 additions & 0 deletions tests/acceptance/editor-selections-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,26 @@ test('editor#selectSections works when given an empty array', (assert) => {
editor.selectSections([]);
assert.selectedText(null, 'no text selected after selecting no sections');
});

test('placing cursor inside a strong section should cause markupsInSelection to contain "strong"', (assert) => {
const mobiledoc = Helpers.mobiledoc.build(({post, markupSection, marker, markup}) => {
const b = markup('strong');
return post([markupSection('p', [
marker('before'),
marker('loud',[b]),
marker('after')
])]);
});
editor = new Editor({mobiledoc});
editor.render(editorElement);


Helpers.dom.moveCursorTo($('#editor strong')[0].firstChild, 1);

let bold = editor.builder.createMarkup('strong');
assert.ok(editor.markupsInSelection.indexOf(bold) !== -1, 'strong is in selection');

Helpers.dom.moveCursorTo($('#editor')[0].childNodes[0], 1);

assert.ok(editor.markupsInSelection.indexOf(bold) === -1, 'strong is not in selection');
});

0 comments on commit b85724e

Please sign in to comment.