Skip to content

Commit

Permalink
refactor post editor to use callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Sep 23, 2015
1 parent de3113d commit b53ae7a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
48 changes: 26 additions & 22 deletions src/js/editor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
isArrayEqual, forEach, filter, compact
} from '../utils/array-utils';
import { DIRECTION } from '../utils/key';
import LifecycleCallbacksMixin from '../utils/lifecycle-callbacks';
import mixin from '../utils/mixin';

function isMarkupSection(section) {
return section.type === MARKUP_SECTION_TYPE;
Expand All @@ -24,15 +26,17 @@ function isMarkerable(section) {
return !!section.markers;
}

const CALLBACK_QUEUES = {
BEFORE_COMPLETE: 'beforeComplete',
COMPLETE: 'complete',
AFTER_COMPLETE: 'afterComplete'
};

class PostEditor {
constructor(editor) {
this.editor = editor;
this.builder = this.editor.builder;
this._queues = {
beforeCompletion: [],
completion: [],
afterCompletion: []
};

this._didScheduleRerender = false;
this._didScheduleUpdate = false;
this._didComplete = false;
Expand Down Expand Up @@ -204,7 +208,8 @@ class PostEditor {
this._markDirty(postNode.section);
}
if (isMarkerable(postNode)) {
this._queues.beforeCompletion.push(() => this._coalesceMarkers(postNode));
this.addCallback(
CALLBACK_QUEUES.BEFORE_COMPLETE, () => this._coalesceMarkers(postNode));
}
}

Expand Down Expand Up @@ -738,7 +743,7 @@ class PostEditor {
if (this._didComplete) {
throw new Error('Work can only be scheduled before a post edit has completed');
}
this._queues.completion.push(callback);
this.addCallback(CALLBACK_QUEUES.COMPLETE, callback);
}

/**
Expand All @@ -748,10 +753,10 @@ class PostEditor {
* @public
*/
scheduleRerender() {
if (!this._didScheduleRerender) {
this.schedule(() => this.editor.rerender());
this._didScheduleRerender = true;
}
if (this._didScheduleRerender) { return; }

this.schedule(() => this.editor.rerender());
this._didScheduleRerender = true;
}

/**
Expand All @@ -761,14 +766,14 @@ class PostEditor {
* @public
*/
scheduleDidUpdate() {
if (!this._didScheduleUpdate) {
this.schedule(() => this.editor.didUpdate());
this._didScheduleUpdate = true;
}
if (this._didScheduleUpdate) { return; }

this.schedule(() => this.editor.didUpdate());
this._didScheduleUpdate = true;
}

scheduleAfterRender(callback) {
this._queues.afterCompletion.push(callback);
this.addCallback(CALLBACK_QUEUES.AFTER_COMPLETE, callback);
}

/**
Expand All @@ -783,14 +788,13 @@ class PostEditor {
throw new Error('Post editing can only be completed once');
}

this._runCallbacks([this._queues.beforeCompletion]);
this.runCallbacks(CALLBACK_QUEUES.BEFORE_COMPLETE);
this._didComplete = true;
this._runCallbacks([this._queues.completion, this._queues.afterCompletion]);
}

_runCallbacks(queues) {
queues.forEach(queue => queue.forEach(cb => cb()));
this.runCallbacks(CALLBACK_QUEUES.COMPLETE);
this.runCallbacks(CALLBACK_QUEUES.AFTER_COMPLETE);
}
}

mixin(PostEditor, LifecycleCallbacksMixin);

export default PostEditor;
7 changes: 5 additions & 2 deletions tests/unit/editor/post-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function renderBuiltAbstract(post) {
let renderer = new EditorDomRenderer(mockEditor, [], () => {}, {});
let renderTree = new RenderTree(post);
renderer.render(renderTree);
return mockEditor;
}

module('Unit: PostEditor with mobiledoc', {
Expand Down Expand Up @@ -385,8 +386,9 @@ test('#splitMarkers when headMarker = tailMarker', (assert) => {
post = buildPost([ section ]);
});

renderBuiltAbstract(post);
let mockEditor = renderBuiltAbstract(post);

const postEditor = new PostEditor(mockEditor);
const range = Range.create(section, 1, section, 3);
const markers = postEditor.splitMarkers(range);
postEditor.complete();
Expand All @@ -402,10 +404,11 @@ test('#splitMarkers when head section = tail section, but different markers', (a
])
);

renderBuiltAbstract(post);
let mockEditor = renderBuiltAbstract(post);

const section = post.sections.head;
const range = Range.create(section, 2, section, 5);
const postEditor = new PostEditor(mockEditor);
const markers = postEditor.splitMarkers(range);
postEditor.complete();

Expand Down

0 comments on commit b53ae7a

Please sign in to comment.