Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-715: insert method in blocks API #753

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/components/modules/api/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Module from '../../__module';

import {Blocks} from '../../../../types/api';
import {OutputData} from '../../../../types';
import { Blocks } from '../../../../types/api';
import { BlockToolData, OutputData, ToolConfig } from '../../../../types';
import Block from '../../block';
import {ModuleConfig} from '../../../types-internal/module-config';
import { ModuleConfig } from '../../../types-internal/module-config';

/**
* @class BlocksAPI
Expand All @@ -26,6 +26,12 @@ export default class BlocksAPI extends Module {
getBlocksCount: () => this.getBlocksCount(),
stretchBlock: (index: number, status: boolean = true) => this.stretchBlock(index, status),
insertNewBlock: () => this.insertNewBlock(),
insert: (
index: number,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make index optional and insert new block at the current position?

toolName: string,
data: BlockToolData = {},
settings: ToolConfig = {},
) => this.insert(index, toolName, data, settings),
};
}

Expand Down Expand Up @@ -147,4 +153,22 @@ export default class BlocksAPI extends Module {
const newBlock = this.Editor.BlockManager.insert();
this.Editor.Caret.setToBlock(newBlock);
}

/**
* Insert new block at index with data
*
* @param {number} index — index of new block
* @param {string} toolName — plugin name
* @param {BlockToolData} data — plugin data
* @param {ToolConfig} settings - default settings
*/
public insert(
index: number,
toolName: string,
data: BlockToolData = {},
settings: ToolConfig = {},
) {
const newBlock = this.Editor.BlockManager.insertAt(index, toolName, data, settings);
this.Editor.Caret.setToBlock(newBlock);
}
}
24 changes: 22 additions & 2 deletions src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,30 @@ export default class BlockManager extends Module {
): Block {
// Increment index before construct,
// because developers can use API/Blocks/getCurrentInputIndex on the render() method
const newIndex = ++this.currentBlockIndex;
const newIndex = this.currentBlockIndex + 1;
return this.insertAt(newIndex, toolName, data, settings);
}

/**
* Insert new block into _blocks at index
*
* @param {Number} index — index of new block
* @param {String} toolName — plugin name, by default method inserts initial block type
* @param {Object} data — plugin data
* @param {Object} settings - default settings
*
* @return {Block}
*/
public insertAt(
index: number,
toolName: string = this.config.initialBlock,
data: BlockToolData = {},
settings: ToolConfig = {},
): Block {
this.currentBlockIndex = index;
const block = this.composeBlock(toolName, data, settings);

this._blocks[newIndex] = block;
this._blocks[index] = block;
return block;
}

Expand Down
18 changes: 17 additions & 1 deletion types/api/blocks.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {OutputData} from '../data-formats/output-data';
import { OutputData } from '../data-formats/output-data';
import { BlockToolData } from '../tools/block-tool-data';
import { ToolConfig } from '../tools/tool-config';

/**
* Describes methods to manipulate with Editor`s blocks
Expand Down Expand Up @@ -65,4 +67,18 @@ export interface Blocks {
* Insert new Initial Block after current Block
*/
insertNewBlock(): void;

/**
* Insert new block at index with data
* @param {number} index — index of new block
* @param {string} toolName — plugin name
* @param {BlockToolData} data — plugin data
* @param {ToolConfig} settings - default settings
*/
insert(
index: number,
toolName: string,
data?: BlockToolData,
settings?: ToolConfig,
): void;
}