mirror of
https://github.com/codex-team/editor.js
synced 2024-06-01 21:42:26 +02:00
Improve caret behaviour (#579)
This commit is contained in:
parent
6008c087a4
commit
9d0d638d42
6
dist/codex-editor.js
vendored
6
dist/codex-editor.js
vendored
File diff suppressed because one or more lines are too long
|
@ -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",
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue