mirror of
https://github.com/codex-team/editor.js
synced 2024-06-26 09:20:07 +02:00
Fix position detection at the edge
This commit is contained in:
parent
f650c4a55f
commit
67a90afe47
|
@ -16,7 +16,7 @@ import $ from '../dom';
|
|||
import * as _ from '../utils';
|
||||
|
||||
/**
|
||||
* The result of detecting an next or previous line
|
||||
* The result of detection an next or previous line
|
||||
*/
|
||||
interface Position {
|
||||
/**
|
||||
|
@ -635,17 +635,29 @@ export default class Caret extends Module {
|
|||
|
||||
const currentBoundingClientRect = range.getBoundingClientRect();
|
||||
|
||||
const currentXDistance = Math.abs(caretBoundingClientRect.x - currentBoundingClientRect.x);
|
||||
const previousXDistance = previousBoundingClientRect && Math.abs(caretBoundingClientRect.x - previousBoundingClientRect.x);
|
||||
if (previousBoundingClientRect) {
|
||||
const caretX = caretBoundingClientRect.x + (direction === 'previous' ? caretBoundingClientRect.width : 0);
|
||||
const currentX = currentBoundingClientRect.x + (direction === 'previous' ? currentBoundingClientRect.width : 0);
|
||||
const previousX = previousBoundingClientRect.x + (direction === 'previous' ? previousBoundingClientRect.width : 0);
|
||||
|
||||
if (previousXDistance !== undefined && previousXDistance < currentXDistance) {
|
||||
position = {
|
||||
block,
|
||||
input,
|
||||
offset: offset + (direction === 'next' ? -1 : 1),
|
||||
};
|
||||
if (Math.abs(caretX - previousX) < Math.abs(caretX - currentX)) {
|
||||
let detectedOffset = offset + (direction === 'next' ? -1 : 1);
|
||||
|
||||
return true;
|
||||
/**
|
||||
* Set the caret to the right side of detected offset
|
||||
*/
|
||||
if (direction === 'previous' && currentBoundingClientRect.y >= previousBoundingClientRect.y) {
|
||||
detectedOffset++;
|
||||
}
|
||||
|
||||
position = {
|
||||
block,
|
||||
input,
|
||||
offset: detectedOffset,
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
direction === 'next' ? offset++ : offset--;
|
||||
|
@ -720,7 +732,7 @@ export default class Caret extends Module {
|
|||
const boundingClientRect = range.getBoundingClientRect();
|
||||
|
||||
/**
|
||||
* Search a next line by finding a character below the caret.
|
||||
* Search an next line by finding a character below the caret.
|
||||
*/
|
||||
if (direction === 'next' && caretBoundingClientRect.y < boundingClientRect.y) {
|
||||
return true;
|
||||
|
|
Loading…
Reference in a new issue