diff --git a/client/src/components/StreamField/blocks/ListBlock.js b/client/src/components/StreamField/blocks/ListBlock.js index eeaec13e75..3e9346818c 100644 --- a/client/src/components/StreamField/blocks/ListBlock.js +++ b/client/src/components/StreamField/blocks/ListBlock.js @@ -111,11 +111,20 @@ export class ListBlock { ]; } - _onRequestInsert(newIndex) { - /* 'opts' argument is discarded here, since ListBlock's insertion control does not pass any */ - const childBlockDef = this.blockDef.childBlockDef; - const childState = this.blockDef.initialChildState; - const newChild = this._insert(childBlockDef, childState, null, newIndex, { animate: true }); + _getChildDataForInsertion() { + /* Called when an 'insert new block' action is triggered: given a dict of data from the insertion control, + return the block definition and initial state to be used for the new block. + For a ListBlock, no data is passed from the insertion control, as there is a single fixed child block definition. + */ + const blockDef = this.blockDef.childBlockDef; + const initialState = this.blockDef.initialChildState; + return [blockDef, initialState]; + } + + _onRequestInsert(index, opts) { + /* handler for an 'insert new block' action */ + const [blockDef, initialState] = this._getChildDataForInsertion(opts); + const newChild = this._insert(blockDef, initialState, null, index, { animate: true }); newChild.focus(); } diff --git a/client/src/components/StreamField/blocks/StreamBlock.js b/client/src/components/StreamField/blocks/StreamBlock.js index 82a8efd513..c9deadb5be 100644 --- a/client/src/components/StreamField/blocks/StreamBlock.js +++ b/client/src/components/StreamField/blocks/StreamBlock.js @@ -281,11 +281,20 @@ export class StreamBlock { return child; } - _onRequestInsert(index, { type }) { - /* handle selecting an item from the 'add block' menu */ - const childBlockDef = this.blockDef.childBlockDefsByName[type]; - const childState = this.blockDef.initialChildStates[type]; - const newChild = this._insert(childBlockDef, childState, null, index, { animate: true }); + _getChildDataForInsertion({ type }) { + /* Called when an 'insert new block' action is triggered: given a dict of data from the insertion control, + return the block definition and initial state to be used for the new block. + For a StreamBlock, the dict of data consists of 'type' (the chosen block type name, as a string). + */ + const blockDef = this.blockDef.childBlockDefsByName[type]; + const initialState = this.blockDef.initialChildStates[type]; + return [blockDef, initialState]; + } + + _onRequestInsert(index, opts) { + /* handler for an 'insert new block' action */ + const [blockDef, initialState] = this._getChildDataForInsertion(opts); + const newChild = this._insert(blockDef, initialState, null, index, { animate: true }); newChild.focus(); }