MCPcopy
hub / github.com/codex-team/editor.js / insert

Method insert

src/components/blocks.ts:189–222  ·  view source on GitHub ↗

* Insert new Block at passed index * * @param {number} index — index to insert Block * @param {Block} block — Block to insert * @param {boolean} replace — it true, replace block on given index

(index: number, block: Block, replace = false)

Source from the content-addressed store, hash-verified

187 * @param {boolean} replace — it true, replace block on given index
188 */
189 public insert(index: number, block: Block, replace = false): void {
190 if (!this.length) {
191 this.push(block);
192
193 return;
194 }
195
196 if (index > this.length) {
197 index = this.length;
198 }
199
200 if (replace) {
201 this.blocks[index].holder.remove();
202 this.blocks[index].call(BlockToolAPI.REMOVED);
203 }
204
205 const deleteCount = replace ? 1 : 0;
206
207 this.blocks.splice(index, deleteCount, block);
208
209 if (index > 0) {
210 const previousBlock = this.blocks[index - 1];
211
212 this.insertToDOM(block, 'afterend', previousBlock);
213 } else {
214 const nextBlock = this.blocks[index + 1];
215
216 if (nextBlock) {
217 this.insertToDOM(block, 'beforebegin', nextBlock);
218 } else {
219 this.insertToDOM(block);
220 }
221 }
222 }
223
224 /**
225 * Replaces block under passed index with passed block

Callers 1

insertAfterMethod · 0.95

Calls 4

pushMethod · 0.95
insertToDOMMethod · 0.95
callMethod · 0.65
removeMethod · 0.45

Tested by

no test coverage detected