-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(paste): Fix insertion into blank section (#466)
* Refactor #insertPost tests to use builder and editor DSL. * Add test helper editor DSL `Helpers.editor.buildFromText(dsl, editorOptions)` * Add test helper `retargetRange` to retarget a range from one post to another. Helps with asserting that the editor's range matches the one from the DSL Fixes #462
- Loading branch information
Showing
4 changed files
with
274 additions
and
1,037 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import PostAbstractHelpers from './post-abstract'; | ||
import Editor from 'mobiledoc-kit/editor/editor'; | ||
import MobiledocRenderer from 'mobiledoc-kit/renderers/mobiledoc/0-3'; | ||
|
||
function retargetPosition(position, fromPost, toPost) { | ||
let sectionIndex; | ||
let retargetedPosition; | ||
fromPost.walkAllLeafSections((section,index) => { | ||
if (sectionIndex !== undefined) { return; } | ||
if (section === position.section) { sectionIndex = index; } | ||
}); | ||
if (sectionIndex === undefined) { | ||
throw new Error('`retargetPosition` could not find section index'); | ||
} | ||
toPost.walkAllLeafSections((section, index) => { | ||
if (retargetedPosition) { return; } | ||
if (index === sectionIndex) { | ||
retargetedPosition = section.toPosition(position.offset); | ||
} | ||
}); | ||
if (!retargetedPosition) { | ||
throw new Error('`retargetPosition` could not find target section'); | ||
} | ||
return retargetedPosition; | ||
} | ||
|
||
function retargetRange(range, toPost) { | ||
let fromPost = range.head.section.post; | ||
let newHead = retargetPosition(range.head, fromPost, toPost); | ||
let newTail = retargetPosition(range.tail, fromPost, toPost); | ||
|
||
return newHead.toRange(newTail); | ||
} | ||
|
||
function buildFromText(texts, editorOptions={}) { | ||
let renderElement = editorOptions.element; | ||
delete editorOptions.element; | ||
|
||
let {post, range} = PostAbstractHelpers.buildFromText(texts); | ||
let mobiledoc = MobiledocRenderer.render(post); | ||
editorOptions.mobiledoc = mobiledoc; | ||
let editor = new Editor(editorOptions); | ||
if (renderElement) { | ||
editor.render(renderElement); | ||
range = retargetRange(range, editor.post); | ||
editor.selectRange(range); | ||
} | ||
return editor; | ||
} | ||
|
||
export { | ||
buildFromText, | ||
retargetRange | ||
}; |
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
Oops, something went wrong.