mirror of
https://github.com/codex-team/editor.js
synced 2024-06-10 09:52:36 +02:00
4c0d806a12
Co-authored-by: Peter Savchenko <specc.dev@gmail.com>
208 lines
5.2 KiB
TypeScript
208 lines
5.2 KiB
TypeScript
import Module from '../../__module';
|
|
|
|
import { Blocks } from '../../../../types/api';
|
|
import { BlockToolData, OutputData, ToolConfig } from '../../../../types';
|
|
import * as _ from './../../utils';
|
|
|
|
/**
|
|
* @class BlocksAPI
|
|
* provides with methods working with Block
|
|
*/
|
|
export default class BlocksAPI extends Module {
|
|
/**
|
|
* Available methods
|
|
*
|
|
* @returns {Blocks}
|
|
*/
|
|
public get methods(): Blocks {
|
|
return {
|
|
clear: (): void => this.clear(),
|
|
render: (data: OutputData): Promise<void> => this.render(data),
|
|
renderFromHTML: (data: string): Promise<void> => this.renderFromHTML(data),
|
|
delete: (): void => this.delete(),
|
|
swap: (fromIndex: number, toIndex: number): void => this.swap(fromIndex, toIndex),
|
|
move: (toIndex: number, fromIndex?: number): void => this.move(toIndex, fromIndex),
|
|
getBlockByIndex: (index: number): HTMLElement => this.getBlockByIndex(index),
|
|
getCurrentBlockIndex: (): number => this.getCurrentBlockIndex(),
|
|
getBlocksCount: (): number => this.getBlocksCount(),
|
|
stretchBlock: (index: number, status = true): void => this.stretchBlock(index, status),
|
|
insertNewBlock: (): void => this.insertNewBlock(),
|
|
insert: this.insert,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Returns Blocks count
|
|
*
|
|
* @returns {number}
|
|
*/
|
|
public getBlocksCount(): number {
|
|
return this.Editor.BlockManager.blocks.length;
|
|
}
|
|
|
|
/**
|
|
* Returns current block index
|
|
*
|
|
* @returns {number}
|
|
*/
|
|
public getCurrentBlockIndex(): number {
|
|
return this.Editor.BlockManager.currentBlockIndex;
|
|
}
|
|
|
|
/**
|
|
* Returns Block holder by Block index
|
|
*
|
|
* @param {number} index - index to get
|
|
*
|
|
* @returns {HTMLElement}
|
|
*/
|
|
public getBlockByIndex(index: number): HTMLElement {
|
|
const block = this.Editor.BlockManager.getBlockByIndex(index);
|
|
|
|
return block.holder;
|
|
}
|
|
|
|
/**
|
|
* Call Block Manager method that swap Blocks
|
|
*
|
|
* @param {number} fromIndex - position of first Block
|
|
* @param {number} toIndex - position of second Block
|
|
* @deprecated — use 'move' instead
|
|
*/
|
|
public swap(fromIndex: number, toIndex: number): void {
|
|
this.Editor.BlockManager.swap(fromIndex, toIndex);
|
|
|
|
/**
|
|
* Move toolbar
|
|
* DO not close the settings
|
|
*/
|
|
this.Editor.Toolbar.move(false);
|
|
}
|
|
|
|
/**
|
|
* Move block from one index to another
|
|
*
|
|
* @param {number} toIndex - index to move to
|
|
* @param {number} fromIndex - index to move from
|
|
*/
|
|
public move(toIndex: number, fromIndex?: number): void {
|
|
this.Editor.BlockManager.move(toIndex, fromIndex);
|
|
|
|
/**
|
|
* Move toolbar
|
|
* DO not close the settings
|
|
*/
|
|
this.Editor.Toolbar.move(false);
|
|
}
|
|
|
|
/**
|
|
* Deletes Block
|
|
*
|
|
* @param {number} blockIndex - index of Block to delete
|
|
*/
|
|
public delete(blockIndex?: number): void {
|
|
this.Editor.BlockManager.removeBlock(blockIndex);
|
|
|
|
/**
|
|
* in case of last block deletion
|
|
* Insert new initial empty block
|
|
*/
|
|
if (this.Editor.BlockManager.blocks.length === 0) {
|
|
this.Editor.BlockManager.insert();
|
|
}
|
|
|
|
/**
|
|
* After Block deletion currentBlock is updated
|
|
*/
|
|
this.Editor.Caret.setToBlock(this.Editor.BlockManager.currentBlock, this.Editor.Caret.positions.END);
|
|
|
|
this.Editor.Toolbar.close();
|
|
}
|
|
|
|
/**
|
|
* Clear Editor's area
|
|
*/
|
|
public clear(): void {
|
|
this.Editor.BlockManager.clear(true);
|
|
this.Editor.InlineToolbar.close();
|
|
}
|
|
|
|
/**
|
|
* Fills Editor with Blocks data
|
|
*
|
|
* @param {OutputData} data — Saved Editor data
|
|
*/
|
|
public render(data: OutputData): Promise<void> {
|
|
this.Editor.BlockManager.clear();
|
|
|
|
return this.Editor.Renderer.render(data.blocks);
|
|
}
|
|
|
|
/**
|
|
* Render passed HTML string
|
|
*
|
|
* @param {string} data - HTML string to render
|
|
* @returns {Promise<void>}
|
|
*/
|
|
public renderFromHTML(data: string): Promise<void> {
|
|
this.Editor.BlockManager.clear();
|
|
|
|
return this.Editor.Paste.processText(data, true);
|
|
}
|
|
|
|
/**
|
|
* Stretch Block's content
|
|
*
|
|
* @param {number} index - index of Block to stretch
|
|
* @param {boolean} status - true to enable, false to disable
|
|
*/
|
|
public stretchBlock(index: number, status = true): void {
|
|
const block = this.Editor.BlockManager.getBlockByIndex(index);
|
|
|
|
if (!block) {
|
|
return;
|
|
}
|
|
|
|
block.stretched = status;
|
|
}
|
|
|
|
/**
|
|
* Insert new Block
|
|
*
|
|
* @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
|
|
*/
|
|
public insert = (
|
|
type: string = this.config.initialBlock,
|
|
data: BlockToolData = {},
|
|
config: ToolConfig = {},
|
|
index?: number,
|
|
needToFocus?: boolean
|
|
): void => {
|
|
this.Editor.BlockManager.insert(
|
|
type,
|
|
data,
|
|
config,
|
|
index,
|
|
needToFocus
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Insert new Block
|
|
* After set caret to this Block
|
|
*
|
|
* @todo remove in 3.0.0
|
|
*
|
|
* @deprecated with insert() method
|
|
*/
|
|
public insertNewBlock(): void {
|
|
_.log('Method blocks.insertNewBlock() is deprecated and it will be removed in next major release. ' +
|
|
'Use blocks.insert() instead.', 'warn');
|
|
this.insert();
|
|
}
|
|
}
|