Skip to content

Commit

Permalink
Refactor image card to use postEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Aug 17, 2015
1 parent 356468b commit b4db504
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
18 changes: 10 additions & 8 deletions src/js/commands/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
10 changes: 0 additions & 10 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/js/editor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/js/models/markup-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/models/markup-section-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit b4db504

Please sign in to comment.