Skip to content

improve block insert method #5

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

Open
wants to merge 11 commits into
base: next
Choose a base branch
from
2 changes: 1 addition & 1 deletion example/tools/embed
33 changes: 12 additions & 21 deletions src/components/modules/api/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BlockAPI as BlockAPIInterface, Blocks } from '../../../../types/api';
import { BlockToolData, OutputData, ToolConfig } from '../../../../types';
import {InsertedBlock} from '../../../../types/api';
import * as _ from './../../utils';
import BlockAPI from '../../block/api';
import Module from '../../__module';
Expand Down Expand Up @@ -222,30 +223,20 @@ export default class BlocksAPI extends Module {

/**
* Insert new Block and returns it's API
*
* @param {string} type — Tool name
* @param {BlockToolData} data — Tool data to insert
* @param {ToolConfig} config — Tool config
* @param {number?} index — index where to insert new Block
* @param {boolean?} needToFocus - flag to focus inserted Block
* @param replace - pass true to replace the Block existed under passed index
* @param {InsertedBlock} block - The block being inserted
*/
public insert = (
type: string = this.config.defaultBlock,
data: BlockToolData = {},
config: ToolConfig = {},
index?: number,
needToFocus?: boolean,
replace?: boolean
): BlockAPIInterface => {
public insert = (block: InsertedBlock = {
type : this.config.defaultBlock,
data: {},
config: {}
}): BlockAPIInterface => {
const insertedBlock = this.Editor.BlockManager.insert({
tool: type,
data,
index,
needToFocus,
replace,
tool: block.type,
data: block.data,
index: block.index,
needToFocus: block.needToFocus,
replace: block.replace,
});

return new BlockAPI(insertedBlock);
}

Expand Down
14 changes: 7 additions & 7 deletions src/components/ui/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Popover, { PopoverEvent, PopoverItem } from '../utils/popover';
import I18n from '../i18n';
import { I18nInternalNS } from '../i18n/namespace-internal';


/**
* @todo the first Tab on the Block — focus Plus Button, the second — focus Block Tunes Toggler, the third — focus next Block
*/
Expand Down Expand Up @@ -404,14 +405,13 @@ export default class Toolbox extends EventsDispatcher<ToolboxEvent> {
blockData = Object.assign(defaultBlockData, blockDataOverrides);
}

const newBlock = this.api.blocks.insert(
toolName,
blockData,
undefined,
const newBlock = this.api.blocks.insert({
type: toolName,
data: blockData,
config: undefined,
index,
undefined,
currentBlock.isEmpty
);
replace: currentBlock.isEmpty,
});

/**
* Apply callback before inserting html
Expand Down
36 changes: 36 additions & 0 deletions types/api/block.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,39 @@ export interface BlockAPI {
*/
dispatchChange(): void;
}

/**
* @interface InsertedBLock Describes methods and properties of inserted blocks
*/

export interface InsertedBlock{
/**
* Tool name
*/
type: string,

/**
* Tool data to insert
*/
data: BlockToolData,

/**
* Tool config
*/
config: ToolConfig,

/**
* index where to insert new block
*/
index?: number,

/**
* flag to focus inserted block
*/
needToFocus?: boolean,

/**
* pass true to replace the Block existed under passed index
*/
replace?: boolean
}
19 changes: 4 additions & 15 deletions types/api/blocks.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {InsertedBlock} from '.';
import {OutputData} from '../data-formats/output-data';
import {BlockToolData, ToolConfig} from '../tools';
import {BlockAPI} from './block';
Expand Down Expand Up @@ -96,21 +97,9 @@ export interface Blocks {
/**
* Insert new Block and return inserted Block API
*
* @param {string} type — Tool name
* @param {BlockToolData} data — Tool data to insert
* @param {ToolConfig} config — Tool config
* @param {number?} index — index where to insert new Block
* @param {boolean?} needToFocus - flag to focus inserted Block
* @param {boolean?} replace - should the existed Block on that index be replaced or not
*/
insert(
type?: string,
data?: BlockToolData,
config?: ToolConfig,
index?: number,
needToFocus?: boolean,
replace?: boolean,
): BlockAPI;
* @param {InsertedBlock} block - new block config
*/
insert(block: InsertedBlock): BlockAPI;


/**
Expand Down