feat(editor): selection as store extension (#9605)

This commit is contained in:
Saul-Mirone
2025-01-09 11:49:23 +00:00
parent c2d8c23f13
commit 126ab18967
39 changed files with 176 additions and 204 deletions

View File

@@ -84,18 +84,21 @@ export const getInlineRangeProvider: (
}
};
const inlineRange$: InlineRangeProvider['inlineRange$'] = signal(null);
selectionManager.slots.changed.on(selections => {
const textSelection = selections.find(s => s.type === 'text') as
| TextSelection
| undefined;
const range = rangeManager.value;
if (!range || !textSelection) {
inlineRange$.value = null;
return;
}
const inlineRange = calculateInlineRange(range, textSelection);
inlineRange$.value = inlineRange;
});
editorHost.disposables.add(
selectionManager.slots.changed.on(selections => {
const textSelection = selections.find(s => s.type === 'text') as
| TextSelection
| undefined;
const range = rangeManager.value;
if (!range || !textSelection) {
inlineRange$.value = null;
return;
}
const inlineRange = calculateInlineRange(range, textSelection);
inlineRange$.value = inlineRange;
})
);
return {
setInlineRange,

View File

@@ -1,7 +1,7 @@
import { throttle } from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';
import type { BaseSelection, BlockModel } from '@blocksuite/store';
import { type BaseSelection, TextSelection } from '../selection/index.js';
import { TextSelection } from '../selection/index.js';
import type { BlockComponent } from '../view/element/block-component.js';
import { BLOCK_ID_ATTR } from '../view/index.js';
import { RANGE_SYNC_EXCLUDE_ATTR } from './consts.js';
@@ -247,6 +247,9 @@ export class RangeBinding {
};
private readonly _onStdSelectionChanged = (selections: BaseSelection[]) => {
const closestHost = document.activeElement?.closest('editor-host');
if (closestHost && closestHost !== this.host) return;
const text =
selections.find((selection): selection is TextSelection =>
selection.is(TextSelection)