Skip to content

Commit

Permalink
Do not permit changes inside cards to reparse
Browse files Browse the repository at this point in the history
* Limit it to card elements, not the wrappers/zwnj
* Refactor section removal and migration to postEditor
* Refactor post reparse to use `run`, fire hooks
  • Loading branch information
mixonic authored and bantic committed Feb 10, 2016
1 parent 7690aa1 commit 7b5c272
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 21 deletions.
14 changes: 3 additions & 11 deletions src/js/editor/edit-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Snapshot {

snapshotRange() {
let { range, cursor } = this.editor;
if (cursor.hasCursor()) {
if (cursor.hasCursor() && !range.isBlank) {
let { head, tail } = range;
this.range = {
head: [head.leafSectionIndex, head.offset],
Expand Down Expand Up @@ -99,16 +99,8 @@ export default class EditHistory {
let { builder, post } = editor;
let restoredPost = mobiledocParsers.parse(builder, mobiledoc);

// remove existing sections
post.sections.toArray().forEach(section => {
postEditor.removeSection(section);
});

// append restored sections
restoredPost.sections.toArray().forEach(section => {
restoredPost.sections.remove(section);
postEditor.insertSectionBefore(post.sections, section, null);
});
postEditor.removeAllSections();
postEditor.migrateSectionsFromPost(restoredPost);

// resurrect snapshotted range if it exists
let newRange = snapshot.getRange(post);
Expand Down
9 changes: 5 additions & 4 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,11 @@ class Editor {
}

_reparsePost() {
this.post = this._parser.parse(this.element);
this._renderTree = new RenderTree(this.post);
clearChildNodes(this.element);
this.rerender();
let post = this._parser.parse(this.element);
this.run(postEditor => {
postEditor.removeAllSections();
postEditor.migrateSectionsFromPost(post);
});

this.runCallbacks(CALLBACK_QUEUES.DID_REPARSE);
this.didUpdate();
Expand Down
15 changes: 9 additions & 6 deletions src/js/editor/mutation-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ export default class MutationHandler {
let nodes = this._findTargetNodes(mutations[i]);

for (let j=0; j < nodes.length; j++) {
let section = this._findSectionFromNode(nodes[j]);
if (section) {
sections.add(section);
let node = nodes[j];
let renderNode = this._findSectionRenderNodeFromNode(node);
if (renderNode) {
if (renderNode.reparsesMutationOfChildNode(node)) {
sections.add(renderNode.postNode);
}
} else {
reparsePost = true;
break;
Expand Down Expand Up @@ -121,10 +124,10 @@ export default class MutationHandler {
return attachedNodes;
}

_findSectionFromNode(node) {
let rn = this.renderTree.findRenderNodeFromElement(node, (rn) => {
_findSectionRenderNodeFromNode(node) {
return this.renderTree.findRenderNodeFromElement(node, (rn) => {
return rn.postNode.isSection;
});
return rn && rn.postNode;
}

}
13 changes: 13 additions & 0 deletions src/js/editor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,19 @@ class PostEditor {
}
}

removeAllSections() {
this.editor.post.sections.toArray().forEach(section => {
this.removeSection(section);
});
}

migrateSectionsFromPost(post) {
post.sections.toArray().forEach(section => {
post.sections.remove(section);
this.insertSectionBefore(this.editor.post.sections, section, null);
});
}

_scheduleListRemovalIfEmpty(listSection) {
this.addCallback(CALLBACK_QUEUES.BEFORE_COMPLETE, () => {
// if the list is attached and blank after we do other rendering stuff,
Expand Down
6 changes: 6 additions & 0 deletions src/js/models/render-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ export default class RenderNode extends LinkedItem {
this.postNode = null;
this.renderTree = null;
}
reparsesMutationOfChildNode(node) {
if (this.postNode.isCardSection) {
return !this.cardNode.element.contains(node);
}
return true;
}
}
33 changes: 33 additions & 0 deletions tests/acceptance/editor-reparse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,36 @@ test('inserting text into text node on left/right of atom is reparsed correctly'
});
});
});

test('mutation inside card element does not cause reparse', (assert) => {
let done = assert.async();
let parseCount = 0;
let myCard = {
name: 'my-card',
type: 'dom',
render() {
return document.createTextNode('howdy');
}
};

editor = Helpers.mobiledoc.renderInto(editorElement, ({post, cardSection}) => {
return post([
cardSection('my-card', {})
]);
}, {
cards: [myCard]
});

editor.didUpdatePost(() => {
parseCount++;
});

let textNode = Helpers.dom.findTextNode(editorElement, 'howdy');
textNode.textContent = 'adios';

// Allow the mutation observer to fire then...
setTimeout(function() {
assert.equal(0, parseCount);
done();
}, 0);
});

0 comments on commit 7b5c272

Please sign in to comment.