Improve caret behaviour (#579)

This commit is contained in:
George Berezhnoy 2018-12-25 18:07:05 +03:00 committed by GitHub
parent 6008c087a4
commit 9d0d638d42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 14 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,6 @@
{
"name": "codex.editor",
"version": "2.7.19",
"version": "2.7.20",
"description": "CodeX Editor. Native JS, based on API and Open Source",
"main": "dist/codex-editor.js",
"types": "./types/index.d.ts",

View file

@ -185,8 +185,8 @@ export default class Block {
* @return {Boolean}
*/
get isEmpty(): boolean {
const emptyText = $.isEmpty(this.pluginsContent),
emptyMedia = !this.hasMedia;
const emptyText = $.isEmpty(this.pluginsContent);
const emptyMedia = !this.hasMedia;
return emptyText && emptyMedia;
}

View file

@ -317,10 +317,14 @@ export default class BlockEvents extends Module {
*/
if (BlockManager.currentBlockIndex === 0) {
Caret.setToBlock(BlockManager.currentBlock);
} else {
Caret.setToBlock(BlockManager.previousBlock, CaretClass.positions.END);
} else if (BlockManager.currentBlock.inputs.length === 0) {
/** If previous (now current) block doesn't contain inputs, remove it */
BlockManager.removeBlock();
BlockManager.insert();
}
Caret.setToBlock(BlockManager.currentBlock, CaretClass.positions.END);
this.Editor.Toolbar.close();
return true;
}
@ -330,8 +334,8 @@ export default class BlockEvents extends Module {
*/
private mergeBlocks() {
const { BlockManager, Caret, Toolbar } = this.Editor;
const targetBlock = BlockManager.getBlockByIndex(BlockManager.currentBlockIndex - 1),
blockToMerge = BlockManager.currentBlock;
const targetBlock = BlockManager.previousBlock;
const blockToMerge = BlockManager.currentBlock;
/**
* Blocks that can be merged:
@ -341,6 +345,15 @@ export default class BlockEvents extends Module {
* other case will handle as usual ARROW LEFT behaviour
*/
if (blockToMerge.name !== targetBlock.name || !targetBlock.mergeable) {
/** If target Block doesn't contain inputs, remove it */
if (targetBlock.inputs.length === 0) {
BlockManager.removeBlock(BlockManager.currentBlockIndex - 1);
Caret.setToBlock(BlockManager.currentBlock);
Toolbar.close();
return;
}
if (Caret.navigatePrevious()) {
Toolbar.close();
}

View file

@ -261,6 +261,10 @@ export default class BlockManager extends Module {
}
this._blocks.remove(index);
if (this.currentBlockIndex >= index) {
this.currentBlockIndex--;
}
/**
* If first Block was removed, insert new Initial Block and set focus on it`s first input
*/

View file

@ -284,7 +284,7 @@ export default class Caret extends Module {
* If last block is empty and it is an initialBlock, set to that.
* Otherwise, append new empty block and set to that
*/
if (lastBlock.isEmpty) {
if (this.Editor.Tools.isInitial(lastBlock.tool) && lastBlock.isEmpty) {
this.setToBlock(lastBlock);
} else {
const newBlock = this.Editor.BlockManager.insertAtEnd();

View file

@ -309,17 +309,18 @@ export default class Toolbox extends Module {
* @param {String} toolName - Tool name
*/
private insertNewBlock(tool: BlockToolConstructable, toolName: string) {
const {BlockManager, Caret} = this.Editor;
/**
* @type {Block}
*/
const currentBlock = this.Editor.BlockManager.currentBlock;
const {currentBlock} = BlockManager;
let newBlock;
if (currentBlock.isEmpty) {
newBlock = this.Editor.BlockManager.replace(toolName);
newBlock = BlockManager.replace(toolName);
} else {
newBlock = this.Editor.BlockManager.insert(toolName);
newBlock = BlockManager.insert(toolName);
}
/**
@ -329,6 +330,16 @@ export default class Toolbox extends Module {
this.Editor.Caret.setToBlock(newBlock);
/** If new block doesn't contain inpus, insert new paragraph above */
if (newBlock.inputs.length === 0) {
if (newBlock === BlockManager.lastBlock) {
BlockManager.insertAtEnd();
Caret.setToBlock(BlockManager.lastBlock);
} else {
Caret.setToBlock(BlockManager.nextBlock);
}
}
/**
* close toolbar when node is changed
*/