Last active
October 23, 2015 13:28
-
-
Save rlivsey/b6e94c97ec3dbeb8d146 to your computer and use it in GitHub Desktop.
Split editor at cursor and return serialized mobiledoc for each side
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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