Delete Tune improvements

This commit is contained in:
Murod Khaydarov 2018-06-28 13:00:23 +03:00
commit 088a537397
No known key found for this signature in database
GPG key ID: C480BA53A8D274C5
4 changed files with 69 additions and 19 deletions

View file

@ -2376,7 +2376,22 @@ var BlocksAPI = function (_Module) {
value: function _delete(blockIndex) {
this.Editor.BlockManager.removeBlock(blockIndex);
this.Editor.Toolbar.close();
this.Editor.BlockManager.navigatePrevious(true);
/**
* 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 next block by index
* but next jumped up after removing, so set caret to the current block means to set it to next
*/
if (this.Editor.BlockManager.currentBlockIndex === 0) {
this.Editor.Caret.setToBlock(this.Editor.BlockManager.currentBlock);
} else {
this.Editor.BlockManager.navigatePrevious(true);
}
}
}, {
key: 'methods',
@ -2900,16 +2915,14 @@ var BlockManager = function (_Module) {
* Set's caret to the next Block
* Before moving caret, we should check if caret position is at the end of Plugins node
* Using {@link Dom#getDeepestNode} to get a last node and match with current selection
*
* @param {Boolean} force - force navigation
*/
}, {
key: 'navigateNext',
value: function navigateNext() {
var caretAtEnd = this.Editor.Caret.isAtEnd;
if (!caretAtEnd) {
return;
}
var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
var nextBlock = this.nextBlock;
@ -2917,6 +2930,17 @@ var BlockManager = function (_Module) {
return;
}
if (force) {
this.Editor.Caret.setToBlock(nextBlock, 0, true);
return;
}
var caretAtEnd = this.Editor.Caret.isAtEnd;
if (!caretAtEnd) {
return;
}
this.Editor.Caret.setToBlock(nextBlock);
}
@ -2956,7 +2980,7 @@ var BlockManager = function (_Module) {
/**
* Insert new block into _blocks
*
* @param {String} toolName plugin name
* @param {String} toolName plugin name, by default method inserts initial block type
* @param {Object} data plugin data
* @param {Object} settings - default settings
*
@ -2965,7 +2989,8 @@ var BlockManager = function (_Module) {
}, {
key: 'insert',
value: function insert(toolName) {
value: function insert() {
var toolName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.config.initialBlock;
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var settings = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
@ -3335,7 +3360,7 @@ var Blocks = function () {
}, {
key: 'remove',
value: function remove(index) {
if (!index) {
if (isNaN(index)) {
index = this.length - 1;
}

File diff suppressed because one or more lines are too long

View file

@ -49,7 +49,24 @@ export default class BlocksAPI extends Module implements IBlocksAPI {
public delete(blockIndex?: number): void {
this.Editor.BlockManager.removeBlock(blockIndex);
this.Editor.Toolbar.close();
this.Editor.BlockManager.navigatePrevious(true);
/**
* 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 next block by index
* but next jumped up after removing, so set caret to the current block means to set it to next
*/
if (this.Editor.BlockManager.currentBlockIndex === 0) {
this.Editor.Caret.setToBlock(this.Editor.BlockManager.currentBlock);
} else {
this.Editor.BlockManager.navigatePrevious(true);
}
}
}

View file

@ -109,21 +109,29 @@ export default class BlockManager extends Module {
* Set's caret to the next Block
* Before moving caret, we should check if caret position is at the end of Plugins node
* Using {@link Dom#getDeepestNode} to get a last node and match with current selection
*
* @param {Boolean} force - force navigation
*/
navigateNext() {
let caretAtEnd = this.Editor.Caret.isAtEnd;
if (!caretAtEnd) {
return;
}
navigateNext(force = false) {
let nextBlock = this.nextBlock;
if (!nextBlock) {
return;
}
this.Editor.Caret.setToBlock( nextBlock );
if (force) {
this.Editor.Caret.setToBlock(nextBlock, 0, true);
return;
}
let caretAtEnd = this.Editor.Caret.isAtEnd;
if (!caretAtEnd) {
return;
}
this.Editor.Caret.setToBlock(nextBlock);
}
/**
@ -157,13 +165,13 @@ export default class BlockManager extends Module {
/**
* Insert new block into _blocks
*
* @param {String} toolName plugin name
* @param {String} toolName plugin name, by default method inserts initial block type
* @param {Object} data plugin data
* @param {Object} settings - default settings
*
* @return {Block}
*/
insert(toolName, data = {}, settings = {}) {
insert(toolName = this.config.initialBlock, data = {}, settings = {}) {
let block = this.composeBlock(toolName, data, settings);
this._blocks[++this.currentBlockIndex] = block;
@ -465,7 +473,7 @@ class Blocks {
* @param {Number|null} index
*/
remove(index) {
if (!index) {
if (isNaN(index)) {
index = this.length - 1;
}