Skip to content

added 'onBlockBreakout' option to execute a callback when a block brakes #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ You can pass options to the plugin as you call it:
```js
const options = {
breakoutBlockType: 'unordered-list-item',
breakoutBlocks: ['header-one', 'header-two']
breakoutBlocks: ['header-one', 'header-two'],
onBlockBreakout: (blockType) => {
// the block type that was "broken"
console.log(blockType);
}
}
const blockBreakoutPlugin = createBlockBreakoutPlugin(options)
```
Expand All @@ -44,6 +48,7 @@ The options and their defaults are:
| `breakoutBlockType` | `String` | Block type to insert when breaking out | `'unstyled'`
| `breakoutBlocks` | `Array` | List of block types to break out from | `['header-one', 'header-two', 'header-three', 'header-four', 'header-five', 'header-six']`
| `doubleBreakoutBlocks` | `Array` | List of block types to that require return on a blank line in order to break | `['blockquote', 'unordered-list-item', 'ordered-list-item', 'code-block']`
| `onBlockBreakout` | `function` | A callback to be executed when the block brakes. Receives the block type as argument | `() => {}`

## Developing

Expand Down
17 changes: 10 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var _immutable = require('immutable');
var defaults = {
breakoutBlockType: 'unstyled',
breakoutBlocks: ['header-one', 'header-two', 'header-three', 'header-four', 'header-five', 'header-six'],
doubleBreakoutBlocks: ['blockquote', 'unordered-list-item', 'ordered-list-item', 'code-block']
doubleBreakoutBlocks: ['blockquote', 'unordered-list-item', 'ordered-list-item', 'code-block'],
onBlockBreakout: function onBlockBreakout() {}

/**
* Block Breakout Plugin
Expand All @@ -39,6 +40,7 @@ var defaults = {
var breakoutBlockType = options.breakoutBlockType || defaults.breakoutBlockType;
var breakoutBlocks = options.breakoutBlocks || defaults.breakoutBlocks;
var doubleBreakoutBlocks = options.doubleBreakoutBlocks || defaults.doubleBreakoutBlocks;
var onBlockBreakout = options.onBlockBreakout || defaults.onBlockBreakout;

return {
handleReturn: function handleReturn(e, editorState, _ref) {
Expand All @@ -50,10 +52,10 @@ var defaults = {

// Does the current block type match a type we care about?
if (isSingleBreakoutBlock || isDoubleBreakoutBlock) {
var selection = editorState.getSelection
var selection = editorState.getSelection();

// Check if the selection is collapsed
();if (selection.isCollapsed()) {
if (selection.isCollapsed()) {
var contentState = editorState.getCurrentContent();
var currentBlock = contentState.getBlockForKey(selection.getEndKey());
var endOffset = selection.getEndOffset();
Expand All @@ -70,9 +72,9 @@ var defaults = {
characterList: (0, _immutable.List)(),
depth: 0
});
var blockMap = contentState.getBlockMap
var blockMap = contentState.getBlockMap();
// Split the blocks
();var blocksBefore = blockMap.toSeq().takeUntil(function (v) {
var blocksBefore = blockMap.toSeq().takeUntil(function (v) {
return v === currentBlock;
});

Expand Down Expand Up @@ -110,9 +112,10 @@ var defaults = {
focusOffset: 0,
isBackward: false
})
}
});
// Set the state
);setEditorState(_draftJs.EditorState.push(editorState, newContentState, 'split-block'));
setEditorState(_draftJs.EditorState.push(editorState, newContentState, 'split-block'));
onBlockBreakout(currentBlockType);
return 'handled';
}
}
Expand Down
Loading