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 block = { kind: 'block', type: 'shortcode', data, isVoid: true, nodes };
const resolvedChange = editorState.change().insertBlock(block).focus();
this.ref.onChange(resolvedChange);
this.setState({ editorState: resolvedChange.state });
let change = editorState.change();
const { focusBlock } = change.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 = () => {

View File

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