Skip to content

Commit

Permalink
Drop custom list accessors/methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Aug 10, 2015
1 parent 6e12e70 commit 7731668
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 217 deletions.
2 changes: 1 addition & 1 deletion src/js/commands/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class ImageCommand extends Command {
let sections = this.editor.activeSections;
let lastSection = sections[sections.length - 1];
let section = builder.createCardSection('image');
post.insertSectionAfter(section, lastSection);
post.sections.insertAfter(section, lastSection);
sections.forEach(section => section.renderNode.scheduleForRemoval());

this.editor.rerender();
Expand Down
20 changes: 8 additions & 12 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,23 +449,23 @@ class Editor {
// FIXME rightMarker is not guaranteed to be there
let [leftMarker, rightMarker] = newMarkers;

section.insertMarkerAfter(leftMarker, marker);
section.markers.insertAfter(leftMarker, marker);
markerRenderNode.scheduleForRemoval();

const newSection = this.builder.createMarkupSection('p');
newSection.appendMarker(rightMarker);
newSection.markers.append(rightMarker);

let nodeForMove = markerRenderNode.next;
while (nodeForMove) {
nodeForMove.scheduleForRemoval();
let movedMarker = nodeForMove.postNode.clone();
newSection.appendMarker(movedMarker);
newSection.markers.append(movedMarker);

nodeForMove = nodeForMove.next;
}

const post = this.post;
post.insertSectionAfter(newSection, section);
post.sections.insertAfter(newSection, section);

this.rerender();
this.trigger('update');
Expand Down Expand Up @@ -657,8 +657,8 @@ class Editor {
sectionRenderNode.markClean();

let previousSectionRenderNode = previousSection && previousSection.renderNode;
this.post.insertSectionAfter(section, previousSection);
this._renderTree.node.insertAfter(sectionRenderNode, previousSectionRenderNode);
this.post.sections.insertAfter(section, previousSection);
this._renderTree.node.childNodes.insertAfter(sectionRenderNode, previousSectionRenderNode);
}

// may cause duplicates to be included
Expand Down Expand Up @@ -802,16 +802,12 @@ class Editor {
let newRenderNode = this._renderTree.buildRenderNode(newSection);
let renderNodes = this.cursor.activeSections.map(s => s.renderNode);
let lastRenderNode = renderNodes[renderNodes.length-1];
lastRenderNode.parent.insertAfter(newRenderNode, lastRenderNode);
this.post.insertSectionAfter(newSection, lastRenderNode.postNode);
lastRenderNode.parent.childNodes.insertAfter(newRenderNode, lastRenderNode);
this.post.sections.insertAfter(newSection, lastRenderNode.postNode);
renderNodes.forEach(renderNode => renderNode.scheduleForRemoval());
this.trigger('update');
}

removeSection(section) {
this.post.removeSection(section);
}

destroy() {
this.removeAllEventListeners();
this.removeAllViews();
Expand Down
5 changes: 4 additions & 1 deletion src/js/models/card.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import LinkedItem from "content-kit-editor/utils/linked-item";

export const CARD_TYPE = 'card-section';

export default class Card {
export default class Card extends LinkedItem {
constructor(name, payload) {
super();
this.name = name;
this.payload = payload;
this.type = CARD_TYPE;
Expand Down
28 changes: 5 additions & 23 deletions src/js/models/markup-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class Section extends LinkedItem {
this.type = MARKUP_SECTION_TYPE;
this.element = null;

markers.forEach(m => this.appendMarker(m));
markers.forEach(m => this.markers.append(m));
}

set tagName(val) {
Expand Down Expand Up @@ -59,30 +59,10 @@ export default class Section extends LinkedItem {
*/
splitMarker(marker, offset, endOffset=marker.length) {
const newMarkers = marker.split(offset, endOffset);
this.replaceMarker(marker, newMarkers);
this.markers.splice(marker, 1, newMarkers);
return newMarkers;
}

replaceMarker(previousMarker, newMarkers=[]) {
this.markers.splice(previousMarker, 1, newMarkers);
}

prependMarker(marker) {
this.markers.prepend(marker);
}

appendMarker(marker) {
this.markers.append(marker);
}

removeMarker(marker) {
this.markers.remove(marker);
}

insertMarkerAfter(marker, previousMarker) {
this.markers.insertAfter(marker, previousMarker);
}

/**
* @return {Array} 2 new sections
*/
Expand Down Expand Up @@ -116,7 +96,9 @@ export default class Section extends LinkedItem {

// mutates this by appending the other section's (cloned) markers to it
join(otherSection) {
otherSection.markers.forEach(m => this.appendMarker(m.clone()));
otherSection.markers.forEach(m => {
this.markers.append(m.clone());
});
}

/**
Expand Down
28 changes: 3 additions & 25 deletions src/js/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ export default class Post {
}
});
}
appendSection(section) {
this.sections.append(section);
}
prependSection(section) {
this.sections.prepend(section);
}
replaceSection(section, newSection) {
this.sections.insertAfter(newSection, section);
this.sections.remove(section);
}
cutMarkers(markers) {
let firstSection = markers[0].section,
lastSection = markers[markers.length - 1].section;
Expand All @@ -31,7 +21,7 @@ export default class Post {
let removedSections = [],
changedSections = [firstSection, lastSection];

let previousMarker = markers[0].previousSibling;
let previousMarker = markers[0].prev;

markers.forEach(marker => {
if (marker.section !== currentSection) { // this marker is in a section we haven't seen yet
Expand All @@ -43,13 +33,13 @@ export default class Post {
}

currentSection = marker.section;
currentSection.removeMarker(marker);
currentSection.markers.remove(marker);
});

// add a blank marker to any sections that are now empty
changedSections.forEach(section => {
if (section.isEmpty()) {
section.appendMarker(this.builder.createBlankMarker());
section.markers.append(this.builder.createBlankMarker());
}
});

Expand Down Expand Up @@ -89,16 +79,4 @@ export default class Post {
}
}
}

insertSectionAfter(section, nextSection) {
this.sections.insertAfter(section, nextSection);
}

removeSection(section) {
this.sections.remove(section);
}

getPreviousSection(section) {
return section.prev;
}
}
9 changes: 0 additions & 9 deletions src/js/models/render-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ export default class RenderNode extends LinkedItem {
markClean() {
this.isDirty = false;
}
appendChild(child) {
this.childNodes.append(child);
}
removeChild(child) {
this.childNodes.remove(child);
}
insertAfter(node, prev) {
this.childNodes.insertAfter(node, prev);
}
set element(element) {
this._element = element;
this.renderTree.elements.set(element, this);
Expand Down
10 changes: 5 additions & 5 deletions src/js/parsers/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ function parseMarkers(section, builder, topNode) {

if (currentNode.firstChild) {
if (isValidMarkerElement(currentNode) && text !== null) {
section.appendMarker(builder.createMarker(text, markups.slice()));
section.markers.append(builder.createMarker(text, markups.slice()));
text = null;
}
currentNode = currentNode.firstChild;
} else if (currentNode.nextSibling) {
if (currentNode === topNode) {
section.appendMarker(builder.createMarker(text, markups.slice()));
section.markers.append(builder.createMarker(text, markups.slice()));
break;
} else {
currentNode = currentNode.nextSibling;
if (currentNode.nodeType === ELEMENT_NODE && isValidMarkerElement(currentNode) && text !== null) {
section.appendMarker(builder.createMarker(text, markups.slice()));
section.markers.append(builder.createMarker(text, markups.slice()));
text = null;
}
}
} else {
section.appendMarker(builder.createMarker(text, markups.slice()));
section.markers.append(builder.createMarker(text, markups.slice()));

while (currentNode && !currentNode.nextSibling && currentNode !== topNode) {
currentNode = currentNode.parentNode;
Expand Down Expand Up @@ -169,7 +169,7 @@ NewHTMLParser.prototype = {
if (!isEmptyTextNode(sectionElement)) {
section = this.parseSection(previousSection, sectionElement);
if (section !== previousSection) {
post.appendSection(section);
post.sections.append(section);
previousSection = section;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/js/parsers/mobiledoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ export default class MobiledocParser {

parseCardSection([type, name, payload], post) {
const section = this.builder.createCardSection(name, payload);
post.appendSection(section);
post.sections.append(section);
}

parseImageSection([type, src], post) {
const section = this.builder.createImageSection(src);
post.appendSection(section);
post.sections.append(section);
}

parseMarkupSection([type, tagName, markers], post) {
const section = this.builder.createMarkupSection(tagName);
post.appendSection(section);
post.sections.append(section);
this.parseMarkers(markers, section);
}

Expand All @@ -78,7 +78,7 @@ export default class MobiledocParser {
this.markups.push(this.markerTypes[index]);
});
const marker = this.builder.createMarker(value, this.markups.slice());
section.appendMarker(marker);
section.markers.append(marker);
this.markups = this.markups.slice(0, this.markups.length-closeCount);
}
}
10 changes: 5 additions & 5 deletions src/js/parsers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class PostParser {
const post = this.builder.createPost();

forEach(element.childNodes, child => {
post.appendSection(this.sectionParser.parse(child));
post.sections.append(this.sectionParser.parse(child));
});

return post;
Expand Down Expand Up @@ -99,12 +99,12 @@ export default class PostParser {

if (previousMarker) {
// insert this marker after the previous one
section.insertMarkerAfter(marker, previousMarker);
section.renderNode.insertAfter(renderNode, previousMarker.renderNode);
section.markers.insertAfter(marker, previousMarker);
section.renderNode.childNodes.insertAfter(renderNode, previousMarker.renderNode);
} else {
// insert marker at the beginning of the section
section.prependMarker(marker);
section.renderNode.insertAfter(renderNode, null);
section.markers.prepend(marker);
section.renderNode.childNodes.insertAfter(renderNode, null);
}

// find the nextMarkerElement, set it on the render node
Expand Down
8 changes: 4 additions & 4 deletions src/js/parsers/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export default class SectionParser {
// close a trailing text nodes if it exists
if (state.text.length) {
let marker = this.builder.createMarker(state.text, state.markups);
state.section.appendMarker(marker);
state.section.markers.append(marker);
}

if (section.markers.length === 0) {
section.appendMarker(this.builder.createBlankMarker());
section.markers.append(this.builder.createBlankMarker());
}

return section;
Expand All @@ -64,7 +64,7 @@ export default class SectionParser {
if (state.text.length) {
// close previous text marker
let marker = this.builder.createMarker(state.text, state.markups);
state.section.appendMarker(marker);
state.section.markers.append(marker);
state.text = '';
}

Expand All @@ -79,7 +79,7 @@ export default class SectionParser {
// close the marker started for this node and pop
// its markup from the stack
let marker = this.builder.createMarker(state.text, state.markups);
state.section.appendMarker(marker);
state.section.markers.append(marker);
state.markups.pop();
state.text = '';
}
Expand Down
12 changes: 6 additions & 6 deletions src/js/renderers/editor-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ let destroyHooks = {
},
[MARKUP_SECTION_TYPE](renderNode, section) {
let post = renderNode.parent.postNode;
post.removeSection(section);
post.sections.remove(section);
// Some formatting commands remove the element from the DOM during
// formatting. Do not error if this is the case.
if (renderNode.element.parentNode) {
Expand All @@ -224,7 +224,7 @@ let destroyHooks = {
}

if (marker.section) {
marker.section.removeMarker(marker);
marker.section.markers.remove(marker);
}

if (element.parentNode) {
Expand All @@ -235,7 +235,7 @@ let destroyHooks = {

[IMAGE_SECTION_TYPE](renderNode, section) {
let post = renderNode.parent.postNode;
post.removeSection(section);
post.sections.remove(section);
renderNode.element.parentNode.removeChild(renderNode.element);
},

Expand All @@ -244,7 +244,7 @@ let destroyHooks = {
renderNode.cardNode.teardown();
}
let post = renderNode.parent.postNode;
post.removeSection(section);
post.sections.remove(section);
renderNode.element.parentNode.removeChild(renderNode.element);
}
};
Expand All @@ -256,7 +256,7 @@ function removeChildren(parentNode) {
let nextChild = child.next;
if (child.isRemoved) {
destroyHooks[child.postNode.type](child, child.postNode);
parentNode.removeChild(child);
parentNode.childNodes.remove(child);
}
child = nextChild;
}
Expand All @@ -269,7 +269,7 @@ function lookupNode(renderTree, parentNode, postNode, previousNode) {
return postNode.renderNode;
} else {
let renderNode = new RenderNode(postNode);
parentNode.insertAfter(renderNode, previousNode);
parentNode.childNodes.insertAfter(renderNode, previousNode);
postNode.renderNode = renderNode;
return renderNode;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/models/markup-section-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('a section can append a marker', (assert) => {
const s1 = new Section();
const m1 = new Marker('hello');

s1.appendMarker(m1);
s1.markers.append(m1);
assert.equal(s1.markers.length, 1);
});

Expand Down

0 comments on commit 7731668

Please sign in to comment.