* Insert new block into _blocks * * @param {object} options - insert options * @param {string} [options.id] - block's unique id * @param {string} [options.tool] - plugin name, by default method inserts the default block type * @param {object} [options.data] - plugin data * @param {
({
id = undefined,
tool = this.config.defaultBlock,
data = {},
index,
needToFocus = true,
replace = false,
tunes = {},
}: {
id?: string;
tool?: string;
data?: BlockToolData;
index?: number;
needToFocus?: boolean;
replace?: boolean;
tunes?: {[name: string]: BlockTuneData};
} = {})
| 266 | * @returns {Block} |
| 267 | */ |
| 268 | public insert({ |
| 269 | id = undefined, |
| 270 | tool = this.config.defaultBlock, |
| 271 | data = {}, |
| 272 | index, |
| 273 | needToFocus = true, |
| 274 | replace = false, |
| 275 | tunes = {}, |
| 276 | }: { |
| 277 | id?: string; |
| 278 | tool?: string; |
| 279 | data?: BlockToolData; |
| 280 | index?: number; |
| 281 | needToFocus?: boolean; |
| 282 | replace?: boolean; |
| 283 | tunes?: {[name: string]: BlockTuneData}; |
| 284 | } = {}): Block { |
| 285 | let newIndex = index; |
| 286 | |
| 287 | if (newIndex === undefined) { |
| 288 | newIndex = this.currentBlockIndex + (replace ? 0 : 1); |
| 289 | } |
| 290 | |
| 291 | const block = this.composeBlock({ |
| 292 | id, |
| 293 | tool, |
| 294 | data, |
| 295 | tunes, |
| 296 | }); |
| 297 | |
| 298 | /** |
| 299 | * In case of block replacing (Converting OR from Toolbox or Shortcut on empty block OR on-paste to empty block) |
| 300 | * we need to dispatch the 'block-removing' event for the replacing block |
| 301 | */ |
| 302 | if (replace) { |
| 303 | this.blockDidMutated(BlockRemovedMutationType, this.getBlockByIndex(newIndex), { |
| 304 | index: newIndex, |
| 305 | }); |
| 306 | } |
| 307 | |
| 308 | this._blocks.insert(newIndex, block, replace); |
| 309 | |
| 310 | /** |
| 311 | * Force call of didMutated event on Block insertion |
| 312 | */ |
| 313 | this.blockDidMutated(BlockAddedMutationType, block, { |
| 314 | index: newIndex, |
| 315 | }); |
| 316 | |
| 317 | if (needToFocus) { |
| 318 | this.currentBlockIndex = newIndex; |
| 319 | } else if (newIndex <= this.currentBlockIndex) { |
| 320 | this.currentBlockIndex++; |
| 321 | } |
| 322 | |
| 323 | return block; |
| 324 | } |
| 325 |
no test coverage detected