diff --git a/src/js/commands/image.js b/src/js/commands/image.js index 3557f9d77..97c1d2a5f 100644 --- a/src/js/commands/image.js +++ b/src/js/commands/image.js @@ -9,14 +9,16 @@ export default class ImageCommand extends Command { } exec() { - let {post, builder} = this.editor; - let sections = this.editor.activeSections; - let lastSection = sections[sections.length - 1]; - let section = builder.createCardSection('image'); - post.sections.insertAfter(section, lastSection); - sections.forEach(section => section.renderNode.scheduleForRemoval()); + let {headMarker} = this.editor.cursor.offsets; + let beforeSection = headMarker.section; + let afterSection = beforeSection.next; + let section = this.editor.builder.createCardSection('image'); - this.editor.rerender(); - this.editor.didUpdate(); + this.editor.run((postEditor) => { + if (beforeSection.isBlank) { + postEditor.removeSection(beforeSection); + } + postEditor.insertSectionBefore(section, afterSection); + }); } } diff --git a/src/js/editor/editor.js b/src/js/editor/editor.js index 0d398dceb..26d2eee02 100644 --- a/src/js/editor/editor.js +++ b/src/js/editor/editor.js @@ -615,16 +615,6 @@ class Editor { this._views = []; } - insertSectionAtCursor(newSection) { - let newRenderNode = this._renderTree.buildRenderNode(newSection); - let renderNodes = this.cursor.activeSections.map(s => s.renderNode); - let lastRenderNode = renderNodes[renderNodes.length-1]; - lastRenderNode.parent.childNodes.insertAfter(newRenderNode, lastRenderNode); - this.post.sections.insertAfter(newSection, lastRenderNode.postNode); - renderNodes.forEach(renderNode => renderNode.scheduleForRemoval()); - this.trigger('update'); - } - destroy() { this.removeAllEventListeners(); this.removeAllViews(); diff --git a/src/js/editor/post.js b/src/js/editor/post.js index cb87c646b..e1ee9d48d 100644 --- a/src/js/editor/post.js +++ b/src/js/editor/post.js @@ -206,6 +206,20 @@ class PostEditor { return markers; } + insertSectionBefore(section, beforeSection) { + this.editor.post.sections.insertBefore(section, beforeSection); + this.editor.post.renderNode.markDirty(); + + this.rerender(); + this.didUpdate(); + } + + removeSection(section) { + section.renderNode.scheduleForRemoval(); + + this.rerender(); + this.didUpdate(); + } /** * A method for adding work the deferred queue diff --git a/src/js/models/markup-section.js b/src/js/models/markup-section.js index 5dc09994c..b3c139a3d 100644 --- a/src/js/models/markup-section.js +++ b/src/js/models/markup-section.js @@ -41,8 +41,14 @@ export default class Section extends LinkedItem { return this._tagName; } - get isEmpty() { - return this.markers.isEmpty; + get isBlank() { + if (!this.markers.length) { + return true; + } + let markerWithLength = this.markers.detect((marker) => { + return !!marker.length; + }); + return !markerWithLength; } setTagName(newTagName) { diff --git a/src/js/models/post.js b/src/js/models/post.js index 14d61aa8f..b420f153f 100644 --- a/src/js/models/post.js +++ b/src/js/models/post.js @@ -38,7 +38,7 @@ export default class Post { // add a blank marker to any sections that are now empty changedSections.forEach(section => { - if (section.isEmpty) { + if (section.markers.isEmpty) { section.markers.append(this.builder.createBlankMarker()); } }); diff --git a/tests/unit/models/markup-section-test.js b/tests/unit/models/markup-section-test.js index 80a40f9d0..b1295778d 100644 --- a/tests/unit/models/markup-section-test.js +++ b/tests/unit/models/markup-section-test.js @@ -149,3 +149,21 @@ test('#coalesceMarkers appends a single blank marker if all the markers were bla assert.equal(s.markers.length, 1, 'has 1 marker after coalescing'); assert.ok(s.markers.head.isEmpty, 'remaining marker is empty'); }); + +test('#isBlank returns true if the text length is zero for two markers', (assert) => { + const m1 = builder.createBlankMarker(); + const m2 = builder.createBlankMarker(); + const s = builder.createMarkupSection('p', [m1,m2]); + assert.ok(s.isBlank, 'section with two blank markers is blank'); +}); + +test('#isBlank returns true if there are no markers', (assert) => { + const s = builder.createMarkupSection('p'); + assert.ok(s.isBlank, 'section with no markers is blank'); +}); + +test('#isBlank returns false if there is a marker with length', (assert) => { + const m = builder.createMarker('a'); + const s = builder.createMarkupSection('p', [m]); + assert.ok(!s.isBlank, 'section with marker is not blank'); +});