use atStartOfInput instead of atStartOfFirstInput

This commit is contained in:
Peter Savchenko 2024-04-21 12:57:09 +03:00
parent bb7377c1e1
commit bb7e112a97
2 changed files with 16 additions and 9 deletions

View file

@ -263,12 +263,18 @@ export default class CrossInputSelection extends Module {
const api = this.Editor.API.methods;
useCrossInputSelection(api, {
atStartOfFirstInput: ({ input, block }, { mergeOrNavigatePrevious }) => {
atStartOfInput: ({ input, block }, { mergeOrNavigatePrevious }) => {
console.log('Delete / atStartOfFirstInput: merge or navigate to the previous block');
event.preventDefault();
mergeOrNavigatePrevious();
const isFirstInputInBlock = input === block.inputs[0];
if (isFirstInputInBlock) {
mergeOrNavigatePrevious();
} else {
this.Editor.Caret.navigatePrevious();
}
},
onSingleFullySelectedInput: ({ input }, { clear }) => {
console.info('Delete / onSingleFullySelectedInput: clear input');

View file

@ -5,7 +5,7 @@
*/
import type { API, BlockAPI } from '@types';
import { getClosestElement } from '../dom';
import Dom, { getClosestElement } from '../dom';
import { isEmpty } from './empty';
import { isAtStart } from './selection'
@ -234,8 +234,8 @@ interface CBSOptions {
onSingleFullySelectedInput?: (input: BlockInputIntersected, helpers: CBSHelpers) => void;
onSinglePartiallySelectedInput?: (input: BlockInputIntersected) => void;
onCrossInputSelection?: (selection: CrossInputSelection) => void;
atStartOfFirstInput?: (input: BlockInputIntersected) => void;
atEndOfLastInput?: () => void;
atStartOfInput?: (input: BlockInputIntersected) => void;
atEndOfInput?: (input: BlockInputIntersected) => void;
}
function insertChar(char: string): void {
@ -305,7 +305,7 @@ export function useCrossInputSelection(api: API, options?: CBSOptions): MaybeCro
const isCrossBlockSelection = intersectedBlocks.length > 1;
const firstBlock = intersectedBlocks[0];
const firstBlock = intersectedBlocks[0]; // startingBlock?
const lastBlock = intersectedBlocks[intersectedBlocks.length - 1];
const firstInput = intersectedInputs[0] ?? null;
const lastInput = intersectedInputs[intersectedInputs.length - 1] ?? null;
@ -323,7 +323,8 @@ export function useCrossInputSelection(api: API, options?: CBSOptions): MaybeCro
if (intersectedInputs.length === 1) {
const { input, block } = firstInput;
const isWholeInputSelected = range.toString() === input.textContent;
const isInputEmpty = Dom.isEmpty(input);
const isWholeInputSelected = !isInputEmpty && range.toString() === input.textContent;
const atStart = isAtStart(firstInput?.input);
if (isWholeInputSelected) {
@ -347,7 +348,7 @@ export function useCrossInputSelection(api: API, options?: CBSOptions): MaybeCro
} else if (atStart) {
const mergeOrNavigatePrevious = async function mergeOrNavigatePrevious() {
/**
* In this case first and last block are the same, so we need find previous one
* In this a case when the first and the last block are the same we need to find a previous one
*/
const lastBlockIndex = api.blocks.getBlockIndex(lastBlock.id);
@ -364,7 +365,7 @@ export function useCrossInputSelection(api: API, options?: CBSOptions): MaybeCro
await mergeOrNavigate(api, previousBlock, lastBlock);
}
options?.atStartOfFirstInput?.(firstInput, { mergeOrNavigatePrevious });
options?.atStartOfInput?.(firstInput, { mergeOrNavigatePrevious });
} else {
options?.onSinglePartiallySelectedInput?.(firstInput);
}