Skip to content

Commit

Permalink
Embed should inject content above current paragraph. Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
bedeoverend committed May 26, 2017
1 parent 1c0d693 commit 20b4390
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion simpla-richtext-behavior.html

Large diffs are not rendered by default.

21 changes: 16 additions & 5 deletions src/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,26 @@ const replaceCurrentBlock = node => attrs => (state, dispatch) => {
return true;
};

/**
* Insert block of given node type before current block
* @param {String} node Block node to insert
* @param {Object=} attrs Optional attributes to give to node
* @return {Function} Command to apply to state
*/
const insertBlockBefore = node => attrs => (state, dispatch) => {
let nodeType = state.schema.nodes[node],
{ from, to } = state.selection;

dispatch(state.tr.insert(Math.min(from, to) - 1, nodeType.createAndFill(attrs)));

return true;
}

/**
* Embed the given node into the current state. Embeds can replace current blocks
* or swap the current block to the given node
* @param {string} node Name of node to embed
* @param {Object=} attrs Optional attributes to pass to the node if wrapping
* @return {Function} Command to apply to state
*/
export const embed = ifThenElse(
currentBlockIs,
replaceCurrentBlock,
setBlockTo
);
export const embed = ifThenElse(currentBlockIs, replaceCurrentBlock, insertBlockBefore);
25 changes: 25 additions & 0 deletions test/simpla-richtext.html → test/simpla-richtext-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,31 @@

expect(editor.plugins.image.applicable).to.be.false;
});

it('should add a new line after insertion', () => {
let src = 'http://www.fillmurray.com/100/100',
isAfter,
img,
p;

editor.setHTML('<p></p>');

select(1, 1);
editor.embed('image', { src });

img = editor.element.querySelector('img');
p = editor.element.querySelector('p');

expect(img).to.be.ok;
expect(p).to.be.ok;

isAfter = !!(
img.compareDocumentPosition(p) &
Node.DOCUMENT_POSITION_FOLLOWING
);

expect(isAfter).to.be.true;
});
});
});

Expand Down

0 comments on commit 20b4390

Please sign in to comment.