migrate insertBlock empty block replacement

This commit is contained in:
Shawn Erquhart 2017-09-28 17:20:03 -04:00
parent cd0254407e
commit 39f65476c3
2 changed files with 20 additions and 17 deletions

View File

@ -135,9 +135,19 @@ export default class Editor extends Component {
}; };
const nodes = [Text.createFromString('')]; const nodes = [Text.createFromString('')];
const block = { kind: 'block', type: 'shortcode', data, isVoid: true, nodes }; const block = { kind: 'block', type: 'shortcode', data, isVoid: true, nodes };
const resolvedChange = editorState.change().insertBlock(block).focus(); let change = editorState.change();
this.ref.onChange(resolvedChange); const { focusBlock } = change.state;
this.setState({ editorState: resolvedChange.state });
if (focusBlock.text === '') {
change = change.setNodeByKey(focusBlock.key, block);
} else {
change = change.insertBlock(block);
}
change = change.focus();
this.ref.onChange(change);
this.setState({ editorState: change.state });
}; };
handleToggle = () => { handleToggle = () => {

View File

@ -8,18 +8,15 @@ const SoftBreak = (options = {}) => ({
if (data.key != 'enter') return; if (data.key != 'enter') return;
if (options.shift && e.shiftKey == false) return; if (options.shift && e.shiftKey == false) return;
const { onlyIn, ignoreIn, closeAfter, unwrapBlocks, defaultBlock = 'paragraph' } = options; const { onlyIn, ignoreIn, defaultBlock = 'paragraph' } = options;
const { type, nodes } = change.state.startBlock; const { type, nodes } = change.state.startBlock;
if (onlyIn && !onlyIn.includes(type)) return; if (onlyIn && !onlyIn.includes(type)) return;
if (ignoreIn && ignoreIn.includes(type)) return; if (ignoreIn && ignoreIn.includes(type)) return;
const shouldClose = nodes.last().characters.takeLast(closeAfter).every(c => c.text === '\n'); const shouldClose = nodes.last().characters.last() === '\n';
if (closeAfter && shouldClose) { if (shouldClose) {
const trimmed = change.deleteBackward(closeAfter); const trimmed = change.deleteBackward(1);
const unwrapped = unwrapBlocks return trimmed.insertBlock(defaultBlock);
? unwrapBlocks.reduce((acc, blockType) => acc.unwrapBlock(blockType), trimmed)
: trimmed;
return unwrapped.insertBlock(defaultBlock);
} }
const textNode = Text.createFromString('\n'); const textNode = Text.createFromString('\n');
@ -33,7 +30,6 @@ const SoftBreak = (options = {}) => ({
const SoftBreakOpts = { const SoftBreakOpts = {
onlyIn: ['quote', 'code'], onlyIn: ['quote', 'code'],
closeAfter: 1
}; };
export const SoftBreakConfigured = SoftBreak(SoftBreakOpts); export const SoftBreakConfigured = SoftBreak(SoftBreakOpts);
@ -67,11 +63,8 @@ const BackspaceCloseBlock = (options = {}) => ({
if (onlyIn && !onlyIn.includes(type)) return; if (onlyIn && !onlyIn.includes(type)) return;
if (ignoreIn && ignoreIn.includes(type)) return; if (ignoreIn && ignoreIn.includes(type)) return;
const characters = startBlock.getFirstText().characters; if (startBlock.text === '') {
const isEmpty = !characters || characters.isEmpty(); return change.setBlock(defaultBlock).focus();
if (isEmpty) {
return change.insertBlock(defaultBlock).focus();
} }
} }
}); });