refactor(editor): move focus block commands to blocksuite/shared (#10671)

This commit is contained in:
L-Sun
2025-03-07 03:07:49 +00:00
parent 43ce609cba
commit 98d44d5be5
8 changed files with 13 additions and 10 deletions

View File

@@ -14,6 +14,8 @@ export {
retainFirstModelCommand,
} from './model-crud/index.js';
export {
focusBlockEnd,
focusBlockStart,
getBlockSelectionsCommand,
getImageSelectionsCommand,
getRangeRects,

View File

@@ -0,0 +1,36 @@
import {
type BlockComponent,
type Command,
TextSelection,
} from '@blocksuite/block-std';
/**
* Focus the end of the block
* @param focusBlock - The block to focus
* @param force - If set to true, the selection will be cleared.
* It is useful when the selection is same.
*/
export const focusBlockEnd: Command<{
focusBlock?: BlockComponent;
force?: boolean;
}> = (ctx, next) => {
const { focusBlock, force, std } = ctx;
if (!focusBlock || !focusBlock.model.text) return;
const { selection } = std;
if (force) selection.clear();
selection.setGroup('note', [
selection.create(TextSelection, {
from: {
blockId: focusBlock.blockId,
index: focusBlock.model.text.length,
length: 0,
},
to: null,
}),
]);
return next();
};

View File

@@ -0,0 +1,23 @@
import {
type BlockComponent,
type Command,
TextSelection,
} from '@blocksuite/block-std';
export const focusBlockStart: Command<{
focusBlock?: BlockComponent;
}> = (ctx, next) => {
const { focusBlock, std } = ctx;
if (!focusBlock || !focusBlock.model.text) return;
const { selection } = std;
selection.setGroup('note', [
selection.create(TextSelection, {
from: { blockId: focusBlock.blockId, index: 0, length: 0 },
to: null,
}),
]);
return next();
};

View File

@@ -1,8 +1,10 @@
export { getBlockSelectionsCommand } from './get-block-selections.js';
export { getImageSelectionsCommand } from './get-image-selections.js';
export { focusBlockEnd } from './focus-block-end';
export { focusBlockStart } from './focus-block-start';
export { getBlockSelectionsCommand } from './get-block-selections';
export { getImageSelectionsCommand } from './get-image-selections';
export {
getRangeRects,
getSelectionRectsCommand,
type SelectionRect,
} from './get-selection-rects.js';
export { getTextSelectionCommand } from './get-text-selection.js';
} from './get-selection-rects';
export { getTextSelectionCommand } from './get-text-selection';