forked from codex-team/editor.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-blocks.ts
More file actions
120 lines (104 loc) · 2.9 KB
/
api-blocks.ts
File metadata and controls
120 lines (104 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
declare var Module: any;
import { IBlocksAPI } from '../interfaces/api';
import IInputOutputData from '../interfaces/input-output-data';
import IModuleConfig from '../interfaces/module-config';
/**
* @class BlocksAPI
* provides with methods working with Block
*/
export default class BlocksAPI extends Module implements IBlocksAPI {
/**
* Save Editor config. API provides passed configuration to the Blocks
*/
constructor({config}: IModuleConfig) {
super({config});
}
/**
* Available methods
* @return {IBlocksAPI}
*/
get methods(): IBlocksAPI {
return {
clear: () => this.clear(),
render: (data: IInputOutputData) => this.render(data),
delete: () => this.delete(),
swap: (fromIndex: number, toIndex: number) => this.swap(fromIndex, toIndex),
getBlockByIndex: (index: number) => this.getBlockByIndex(index),
getCurrentBlockIndex: () => this.getCurrentBlockIndex(),
getBlocksCount: () => this.getBlocksCount(),
};
}
/**
* Returns Blocks count
* @return {number}
*/
public getBlocksCount(): number {
return this.Editor.BlockManager.blocks.length;
}
/**
* Returns current block index
* @return {number}
*/
public getCurrentBlockIndex(): number {
return this.Editor.BlockManager.currentBlockIndex;
}
/**
* Returns Current Block
* @param {Number} index
*
* @return {Object}
*/
public getBlockByIndex(index: number): object {
return this.Editor.BlockManager.getBlockByIndex(index);
}
/**
* Call Block Manager method that swap Blocks
* @param {number} fromIndex - position of first Block
* @param {number} toIndex - position of second Block
*/
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);
}
/**
* Deletes Block
* @param blockIndex
*/
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();
}
/**
* In case of deletion first block we need to set caret to the current Block
*/
if (this.Editor.BlockManager.currentBlockIndex === 0) {
this.Editor.Caret.setToBlock(this.Editor.BlockManager.currentBlock);
} else {
this.Editor.Caret.navigatePrevious(true);
}
this.Editor.Toolbar.close();
}
/**
* Clear Editor's area
*/
public clear(): void {
this.Editor.BlockManager.clear(true);
}
/**
* Fills Editor with Blocks data
* @param {IInputOutputData} data — Saved Editor data
*/
public render(data: IInputOutputData): void {
this.Editor.BlockManager.clear();
this.Editor.Renderer.render(data.items);
}
}