-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[Feature] BlockAPI Interface #1075
Changes from all commits
5569965
f172ca3
f126bcd
a6aa989
92506c7
f93383e
fa27828
390004c
0dcdb19
e3225ca
d3afd8f
7d4b4ac
7abed86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,33 @@ Most actual API described by [this interface](../types/api/index.d.ts). | |
|
||
--- | ||
|
||
Blocks have access to the public methods provided by Editor.js API Module. Plugin and Tune Developers | ||
Tools have access to the public methods provided by Editor.js API Module. Plugin and Tune Developers | ||
can use Editor\`s API as they want. | ||
|
||
## Block API | ||
|
||
API for certain Block methods and properties. You can access it through `editor.api.block.getBlockByIndex` method or get it form `block` property of [Tool constructor](../types/tools/block-tool.d.ts) argument. | ||
|
||
`name: string` — Block's Tool name (key, specified in `tools` property of initial configuration) | ||
|
||
`config: ToolConfig` — Tool config passed on Editor initialization | ||
|
||
`holder: HTMLElement` — HTML Element that wraps Tool's HTML content | ||
|
||
`isEmpty: boolean` — `true` if Block has any editable content | ||
|
||
`selected: boolean` - `true` if Block is selected with Cross-Block Selection | ||
|
||
`set stretched(state: boolean)` — set Block's stretch state | ||
|
||
`stretched: boolean` — `true` if Block is stretched | ||
|
||
`call(methodName: string, param?: object): void` — method to call any Tool's instance methods with checks and error handlers under-the-hood. For example, [Block lifecycle hooks](./tools.md#block-lifecycle-hooks) | ||
|
||
`save(): Promise<void|SavedData>` — returns data saved from current Block's state, including Tool name and saving exec time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
General I agree that we have some inconsistency here, but I think it's out of the scope of this feature. |
||
|
||
`validate(data: BlockToolData): Promise<boolean>` — calls Tool's validate method if exists | ||
|
||
## Api object description | ||
|
||
Common API interface. | ||
|
@@ -43,11 +67,11 @@ use 'move' instead) | |
|
||
`getCurrentBlockIndex()` - current Block index | ||
|
||
`getBlockByIndex(index: Number)` - returns Block with passed index | ||
`getBlockByIndex(index: Number)` - returns Block API object by passed index | ||
|
||
`getBlocksCount()` - returns Blocks count | ||
|
||
`stretchBlock(index: number, status: boolean)` - make Block stretched | ||
`stretchBlock(index: number, status: boolean)` - _Deprecated. Use Block API interface instead._ make Block stretched. | ||
|
||
`insertNewBlock()` - __Deprecated__ insert new Block after working place | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import Block from './index'; | ||
import { BlockToolData, ToolConfig } from '../../../types/tools'; | ||
import { SavedData } from '../../types-internal/block-data'; | ||
import { BlockAPI as BlockAPIInterface } from '../../../types/api'; | ||
|
||
/** | ||
* Constructs new BlockAPI object | ||
* | ||
* @class | ||
* | ||
* @param {Block} block - Block to expose | ||
*/ | ||
function BlockAPI(block: Block): void { | ||
neSpecc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const blockAPI: BlockAPIInterface = { | ||
/** | ||
* Tool name | ||
* | ||
* @returns {string} | ||
*/ | ||
get name(): string { | ||
return block.name; | ||
}, | ||
|
||
/** | ||
* Tool config passed on Editor's initialization | ||
* | ||
* @returns {ToolConfig} | ||
*/ | ||
get config(): ToolConfig { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There can be a misunderstanding with existed Tool config term. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's exactly the ToolConfig object |
||
return block.config; | ||
}, | ||
|
||
/** | ||
* .ce-block element, that wraps plugin contents | ||
* | ||
* @returns {HTMLElement} | ||
*/ | ||
get holder(): HTMLElement { | ||
return block.holder; | ||
}, | ||
|
||
/** | ||
* True if Block content is empty | ||
* | ||
* @returns {boolean} | ||
*/ | ||
get isEmpty(): boolean { | ||
return block.isEmpty; | ||
}, | ||
|
||
/** | ||
* True if Block is selected with Cross-Block selection | ||
* | ||
* @returns {boolean} | ||
*/ | ||
get selected(): boolean { | ||
return block.selected; | ||
}, | ||
|
||
/** | ||
* Set Block's stretch state | ||
* | ||
* @param {boolean} state — state to set | ||
*/ | ||
set stretched(state: boolean) { | ||
block.stretched = state; | ||
}, | ||
|
||
/** | ||
* True if Block is stretched | ||
* | ||
* @returns {boolean} | ||
*/ | ||
get stretched(): boolean { | ||
return block.stretched; | ||
}, | ||
|
||
/** | ||
* Call Tool method with errors handler under-the-hood | ||
* | ||
* @param {string} methodName - method to call | ||
* @param {object} param - object with parameters | ||
* | ||
* @returns {void} | ||
*/ | ||
call(methodName: string, param?: object): void { | ||
block.call(methodName, param); | ||
}, | ||
|
||
/** | ||
* Save Block content | ||
* | ||
* @returns {Promise<void|SavedData>} | ||
*/ | ||
save(): Promise<void|SavedData> { | ||
return block.save(); | ||
}, | ||
|
||
/** | ||
* Validate Block data | ||
* | ||
* @param {BlockToolData} data - data to validate | ||
* | ||
* @returns {Promise<boolean>} | ||
*/ | ||
validate(data: BlockToolData): Promise<boolean> { | ||
return block.validate(data); | ||
}, | ||
}; | ||
|
||
Object.setPrototypeOf(this, blockAPI); | ||
} | ||
|
||
export default BlockAPI; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this an element returned by tools render method? The description is not clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We used this definition earlier for the same thing, is it better?
