Skip to content

Instantly share code, notes, and snippets.

@rlivsey
Last active October 23, 2015 13:28
Show Gist options
  • Save rlivsey/b6e94c97ec3dbeb8d146 to your computer and use it in GitHub Desktop.
Save rlivsey/b6e94c97ec3dbeb8d146 to your computer and use it in GitHub Desktop.
Split editor at cursor and return serialized mobiledoc for each side
// Splits the current editor at the cursor into two mobiledocs
function splitAtCursor(editor) {
return editor.run(postEditor => {
const splitSections = postEditor.splitSection(editor.cursor.offsets.head);
const afterSection = splitSections[1];
let currentSection = afterSection;
let sectionsToMove = [afterSection];
while (currentSection.next) {
currentSection = currentSection.next;
sectionsToMove.push(currentSection);
}
const newEditor = new self.ContentKit.Editor();
// we need to render somwhere for newEditor.run to work
newEditor.render(document.createElement('div'));
newEditor.run(newPostEditor => {
sectionsToMove.forEach(section => {
postEditor.removeSection(section);
newPostEditor.insertSectionAtEnd(section);
});
});
return [
editor.serialize(),
newEditor.serialize()
]
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment