mirror of
https://github.com/codex-team/editor.js
synced 2024-06-07 16:32:22 +02:00
b46369bff0
* Caret API * Check block existence before setting the caret * Update docs/api.md Co-Authored-By: gohabereg <gohabereg@users.noreply.github.com> * Update docs/api.md Co-Authored-By: gohabereg <gohabereg@users.noreply.github.com> * Update docs/api.md Co-Authored-By: gohabereg <gohabereg@users.noreply.github.com> * Update CHANGELOG.md
116 lines
3 KiB
TypeScript
116 lines
3 KiB
TypeScript
import Module from '../../__module';
|
|
import {Caret} from '../../../../types/api';
|
|
|
|
/**
|
|
* @class CaretAPI
|
|
* provides with methods to work with caret
|
|
*/
|
|
export default class CaretAPI extends Module {
|
|
/**
|
|
* Available methods
|
|
* @return {Caret}
|
|
*/
|
|
get methods(): Caret {
|
|
return {
|
|
setToFirstBlock: this.setToFirstBlock,
|
|
setToLastBlock: this.setToLastBlock,
|
|
setToPreviousBlock: this.setToPreviousBlock,
|
|
setToNextBlock: this.setToNextBlock,
|
|
setToBlock: this.setToBlock,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Sets caret to the first Block
|
|
*
|
|
* @param {string} position - position where to set caret
|
|
* @param {number} offset - caret offset
|
|
*
|
|
* @return {boolean}
|
|
*/
|
|
private setToFirstBlock = (position: string = this.Editor.Caret.positions.DEFAULT, offset: number = 0): boolean => {
|
|
if (!this.Editor.BlockManager.firstBlock) {
|
|
return false;
|
|
}
|
|
|
|
this.Editor.Caret.setToBlock(this.Editor.BlockManager.firstBlock, position, offset);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Sets caret to the last Block
|
|
*
|
|
* @param {string} position - position where to set caret
|
|
* @param {number} offset - caret offset
|
|
*
|
|
* @return {boolean}
|
|
*/
|
|
private setToLastBlock = (position: string = this.Editor.Caret.positions.DEFAULT, offset: number = 0): boolean => {
|
|
if (!this.Editor.BlockManager.lastBlock) {
|
|
return false;
|
|
}
|
|
|
|
this.Editor.Caret.setToBlock(this.Editor.BlockManager.lastBlock, position, offset);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Sets caret to the previous Block
|
|
*
|
|
* @param {string} position - position where to set caret
|
|
* @param {number} offset - caret offset
|
|
*
|
|
* @return {boolean}
|
|
*/
|
|
private setToPreviousBlock = (
|
|
position: string = this.Editor.Caret.positions.DEFAULT,
|
|
offset: number = 0,
|
|
): boolean => {
|
|
if (!this.Editor.BlockManager.previousBlock) {
|
|
return false;
|
|
}
|
|
|
|
this.Editor.Caret.setToBlock(this.Editor.BlockManager.previousBlock, position, offset);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Sets caret to the next Block
|
|
*
|
|
* @param {string} position - position where to set caret
|
|
* @param {number} offset - caret offset
|
|
*
|
|
* @return {boolean}
|
|
*/
|
|
private setToNextBlock = (position: string = this.Editor.Caret.positions.DEFAULT, offset: number = 0): boolean => {
|
|
if (!this.Editor.BlockManager.nextBlock) {
|
|
return false;
|
|
}
|
|
|
|
this.Editor.Caret.setToBlock(this.Editor.BlockManager.nextBlock, position, offset);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Sets caret to the Block by passed index
|
|
*
|
|
* @param {number} index - index of Block where to set caret
|
|
* @param {string} position - position where to set caret
|
|
* @param {number} offset - caret offset
|
|
*
|
|
* @return {boolean}
|
|
*/
|
|
private setToBlock = (
|
|
index: number,
|
|
position: string = this.Editor.Caret.positions.DEFAULT,
|
|
offset: number = 0,
|
|
): boolean => {
|
|
if (!this.Editor.BlockManager.blocks[index]) {
|
|
return false;
|
|
}
|
|
|
|
this.Editor.Caret.setToBlock(this.Editor.BlockManager.blocks[index], position, offset);
|
|
return true;
|
|
}
|
|
}
|