isAtEnd and isAtStart works with ranges

This commit is contained in:
georgyb 2019-06-04 20:45:45 +03:00
parent d0f6f81964
commit 1d1bfa9884
2 changed files with 45 additions and 33 deletions

57
dist/editor.js vendored
View file

@ -13570,7 +13570,6 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
_this.finishSelection = function (e) { _this.finishSelection = function (e) {
var BlockManager = _this.Editor.BlockManager; var BlockManager = _this.Editor.BlockManager;
console.log(e);
_this.mousePressed = false; _this.mousePressed = false;
_this.shouldClearSelectionOnClick = false; _this.shouldClearSelectionOnClick = false;
@ -13603,8 +13602,6 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
}, { }, {
key: "selectNextBlock", key: "selectNextBlock",
value: function selectNextBlock() { value: function selectNextBlock() {
console.log(this.Editor.Caret.isAtEnd);
if (!this.Editor.BlockSelection.anyBlockSelected && !this.Editor.Caret.isAtEnd && !this.lastTarget) { if (!this.Editor.BlockSelection.anyBlockSelected && !this.Editor.Caret.isAtEnd && !this.lastTarget) {
return; return;
} }
@ -13621,25 +13618,34 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
selection.removeAllRanges(); selection.removeAllRanges();
} }
console.log(this.lastTarget);
var lastTargetIndex = BlockManager.blocks.indexOf(this.lastTarget); var lastTargetIndex = BlockManager.blocks.indexOf(this.lastTarget);
var firstTargetIndex = BlockManager.blocks.indexOf(this.firstTarget);
var nextTarget = BlockManager.blocks[lastTargetIndex + 1]; var nextTarget = BlockManager.blocks[lastTargetIndex + 1];
var nextTargetIndex = lastTargetIndex + 1; // if (nextTarget === this.firstTarget) {
// nextTarget = BlockManager.blocks[lastTargetIndex + 2];
// nextTargetIndex = lastTargetIndex + 2;
// }
var isDownward = nextTargetIndex > firstTargetIndex;
if (!nextTarget) { if (!nextTarget) {
// this.lastTarget.selected = true;
return; return;
} }
if (this.isDownward) { if (isDownward) {
this.lastTarget.selected = true; this.lastTarget.selected = true;
this.lastTarget = nextTarget; this.lastTarget = nextTarget;
this.lastTarget.selected = !this.lastTarget.selected; this.lastTarget.selected = this.lastTarget.selected;
} else { } else {
this.lastTarget.selected = !this.lastTarget.selected; this.lastTarget.selected = nextTargetIndex === firstTargetIndex;
this.lastTarget = nextTarget; this.lastTarget = nextTarget;
} }
} }
}, { }, {
key: "selectPrevioiusBlock", key: "selectPreviousBlock",
value: function selectPrevioiusBlock() { value: function selectPreviousBlock() {
if (!this.Editor.BlockSelection.anyBlockSelected && !this.Editor.Caret.isAtStart && !this.lastTarget) { if (!this.Editor.BlockSelection.anyBlockSelected && !this.Editor.Caret.isAtStart && !this.lastTarget) {
return; return;
} }
@ -13657,18 +13663,25 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
} }
var lastTargetIndex = BlockManager.blocks.indexOf(this.lastTarget); var lastTargetIndex = BlockManager.blocks.indexOf(this.lastTarget);
var firstTargetIndex = BlockManager.blocks.indexOf(this.firstTarget);
var nextTarget = BlockManager.blocks[lastTargetIndex - 1]; var nextTarget = BlockManager.blocks[lastTargetIndex - 1];
var nextTargetIndex = lastTargetIndex - 1;
var isDownward = nextTargetIndex > firstTargetIndex; // if (nextTarget === this.firstTarget) {
// nextTarget = BlockManager.blocks[lastTargetIndex - 2];
// nextTargetIndex = lastTargetIndex - 2;
// }
if (!nextTarget) { if (!nextTarget) {
// this.lastTarget.selected = true;
return; return;
} }
if (!this.isDownward) { if (!isDownward) {
this.lastTarget.selected = true; this.lastTarget.selected = true;
this.lastTarget = nextTarget; this.lastTarget = nextTarget;
this.lastTarget.selected = !this.lastTarget.selected; this.lastTarget.selected = !this.lastTarget.selected;
} else { } else {
this.lastTarget.selected = !this.lastTarget.selected; this.lastTarget.selected = nextTargetIndex === firstTargetIndex;
this.lastTarget = nextTarget; this.lastTarget = nextTarget;
} }
} }
@ -15596,7 +15609,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
var _this2 = this; var _this2 = this;
if (event.shiftKey && event.keyCode === _utils.default.keyCodes.UP) { if (event.shiftKey && event.keyCode === _utils.default.keyCodes.UP) {
this.Editor.CBS.selectPrevioiusBlock(); this.Editor.CBS.selectPreviousBlock();
return; return;
} }
@ -17273,10 +17286,9 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
/** /**
* Don't handle ranges * Don't handle ranges
*/ */
if (!_selection.default.isCollapsed) { // if (!Selection.isCollapsed) {
return false; // return false;
} // }
if (!this.Editor.BlockManager.currentBlock) { if (!this.Editor.BlockManager.currentBlock) {
return false; return false;
} }
@ -17285,7 +17297,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
var firstNode = _dom.default.getDeepestNode(this.Editor.BlockManager.currentBlock.currentInput); var firstNode = _dom.default.getDeepestNode(this.Editor.BlockManager.currentBlock.currentInput);
var anchorNode = selection.anchorNode; var anchorNode = selection.focusNode;
if (!anchorNode) { if (!anchorNode) {
return false; return false;
@ -17320,7 +17332,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
*/ */
var anchorOffset = selection.anchorOffset; var anchorOffset = selection.focusOffset;
if (anchorNode.nodeType !== Node.TEXT_NODE && anchorNode.childNodes.length) { if (anchorNode.nodeType !== Node.TEXT_NODE && anchorNode.childNodes.length) {
if (anchorNode.childNodes[anchorOffset]) { if (anchorNode.childNodes[anchorOffset]) {
@ -17369,17 +17381,16 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
/** /**
* Don't handle ranges * Don't handle ranges
*/ */
if (!_selection.default.isCollapsed) { // if (!Selection.isCollapsed) {
return false; // return false;
} // }
if (!this.Editor.BlockManager.currentBlock) { if (!this.Editor.BlockManager.currentBlock) {
return false; return false;
} }
var selection = _selection.default.get(); var selection = _selection.default.get();
var anchorNode = selection.anchorNode; var anchorNode = selection.focusNode;
if (!anchorNode) { if (!anchorNode) {
return false; return false;
@ -17403,7 +17414,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
*/ */
var anchorOffset = selection.anchorOffset; var anchorOffset = selection.focusOffset;
if (anchorNode.nodeType !== Node.TEXT_NODE && anchorNode.childNodes.length) { if (anchorNode.nodeType !== Node.TEXT_NODE && anchorNode.childNodes.length) {
if (anchorNode.childNodes[anchorOffset - 1]) { if (anchorNode.childNodes[anchorOffset - 1]) {

View file

@ -51,9 +51,9 @@ export default class Caret extends Module {
/** /**
* Don't handle ranges * Don't handle ranges
*/ */
if (!Selection.isCollapsed) { // if (!Selection.isCollapsed) {
return false; // return false;
} // }
if (!this.Editor.BlockManager.currentBlock) { if (!this.Editor.BlockManager.currentBlock) {
return false; return false;
@ -61,7 +61,7 @@ export default class Caret extends Module {
const selection = Selection.get(); const selection = Selection.get();
const firstNode = $.getDeepestNode(this.Editor.BlockManager.currentBlock.currentInput); const firstNode = $.getDeepestNode(this.Editor.BlockManager.currentBlock.currentInput);
let anchorNode = selection.anchorNode; let anchorNode = selection.focusNode;
if (!anchorNode) { if (!anchorNode) {
return false; return false;
@ -92,7 +92,7 @@ export default class Caret extends Module {
* *
* So we use child with anchorOffset index as new anchorNode. * So we use child with anchorOffset index as new anchorNode.
*/ */
let anchorOffset = selection.anchorOffset; let anchorOffset = selection.focusOffset;
if (anchorNode.nodeType !== Node.TEXT_NODE && anchorNode.childNodes.length) { if (anchorNode.nodeType !== Node.TEXT_NODE && anchorNode.childNodes.length) {
if (anchorNode.childNodes[anchorOffset]) { if (anchorNode.childNodes[anchorOffset]) {
anchorNode = anchorNode.childNodes[anchorOffset]; anchorNode = anchorNode.childNodes[anchorOffset];
@ -134,16 +134,16 @@ export default class Caret extends Module {
/** /**
* Don't handle ranges * Don't handle ranges
*/ */
if (!Selection.isCollapsed) { // if (!Selection.isCollapsed) {
return false; // return false;
} // }
if (!this.Editor.BlockManager.currentBlock) { if (!this.Editor.BlockManager.currentBlock) {
return false; return false;
} }
const selection = Selection.get(); const selection = Selection.get();
let anchorNode = selection.anchorNode; let anchorNode = selection.focusNode;
if (!anchorNode) { if (!anchorNode) {
return false; return false;
@ -165,7 +165,8 @@ export default class Caret extends Module {
* *
* So we use child with anchorOffset - 1 as new anchorNode. * So we use child with anchorOffset - 1 as new anchorNode.
*/ */
let anchorOffset = selection.anchorOffset; let anchorOffset = selection.focusOffset;
if (anchorNode.nodeType !== Node.TEXT_NODE && anchorNode.childNodes.length) { if (anchorNode.nodeType !== Node.TEXT_NODE && anchorNode.childNodes.length) {
if (anchorNode.childNodes[anchorOffset - 1]) { if (anchorNode.childNodes[anchorOffset - 1]) {
anchorNode = anchorNode.childNodes[anchorOffset - 1]; anchorNode = anchorNode.childNodes[anchorOffset - 1];