Skip to content

Commit 162b7ad

Browse files
committed
fix(editor): should check text length and stop event propagation when adding a link (#10391)
1 parent 6289981 commit 162b7ad

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

blocksuite/affine/components/src/rich-text/format/config.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EditorHost } from '@blocksuite/block-std';
1+
import { type EditorHost, TextSelection } from '@blocksuite/block-std';
22
import type { TemplateResult } from 'lit';
33

44
import {
@@ -26,6 +26,7 @@ export interface TextFormatConfig {
2626
hotkey?: string;
2727
activeWhen: (host: EditorHost) => boolean;
2828
action: (host: EditorHost) => void;
29+
textChecker?: (host: EditorHost) => boolean;
2930
}
3031

3132
export const textFormatConfigs: TextFormatConfig[] = [
@@ -124,5 +125,14 @@ export const textFormatConfigs: TextFormatConfig[] = [
124125
action: host => {
125126
host.std.command.chain().pipe(toggleLink).run();
126127
},
128+
// should check text length
129+
textChecker: host => {
130+
const textSelection = host.std.selection.find(TextSelection);
131+
if (!textSelection || textSelection.isCollapsed()) return false;
132+
133+
return Boolean(
134+
textSelection.from.length + (textSelection.to?.length ?? 0)
135+
);
136+
},
127137
},
128138
];

blocksuite/affine/components/src/rich-text/keymap/format.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ export const textFormatKeymap = (std: BlockStdScope) =>
2020
const textSelection = selection.find(TextSelection);
2121
if (!textSelection) return;
2222

23+
const allowed = config.textChecker?.(std.host) ?? true;
24+
if (!allowed) return;
25+
26+
const event = ctx.get('keyboardState').raw;
27+
event.stopPropagation();
28+
event.preventDefault();
29+
2330
config.action(std.host);
24-
ctx.get('keyboardState').raw.preventDefault();
2531
return true;
2632
},
2733
};

packages/frontend/core/src/components/hooks/use-register-workspace-commands.ts

+3-27
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import { I18nService } from '@affine/core/modules/i18n';
88
import { UrlService } from '@affine/core/modules/url';
99
import { WorkspaceService } from '@affine/core/modules/workspace';
1010
import { useI18n } from '@affine/i18n';
11-
import { TextSelection } from '@blocksuite/affine/block-std';
1211
import { useService, useServiceOptional } from '@toeverything/infra';
1312
import { useStore } from 'jotai';
1413
import { useTheme } from 'next-themes';
1514
import { useEffect } from 'react';
1615

17-
import type { AffineEditorContainer } from '../../blocksuite/block-suite-editor';
1816
import { usePageHelper } from '../../blocksuite/block-suite-page-list/utils';
1917
import {
2018
PreconditionStrategy,
@@ -29,40 +27,19 @@ import {
2927
} from '../../commands';
3028
import { EditorSettingService } from '../../modules/editor-setting';
3129
import { CMDKQuickSearchService } from '../../modules/quicksearch/services/cmdk';
32-
import { useActiveBlocksuiteEditor } from './use-block-suite-editor';
3330
import { useNavigateHelper } from './use-navigate-helper';
3431

35-
function hasLinkPopover(editor: AffineEditorContainer | null) {
36-
const textSelection = editor?.host?.std.selection.find(TextSelection);
37-
if (editor && textSelection && textSelection.from.length > 0) {
38-
const formatBar = editor.host?.querySelector('affine-format-bar-widget');
39-
if (formatBar) {
40-
return true;
41-
}
42-
}
43-
return false;
44-
}
45-
46-
function registerCMDKCommand(
47-
service: CMDKQuickSearchService,
48-
editor: AffineEditorContainer | null
49-
) {
32+
function registerCMDKCommand(service: CMDKQuickSearchService) {
5033
return registerAffineCommand({
5134
id: 'affine:show-quick-search',
5235
preconditionStrategy: PreconditionStrategy.Never,
5336
category: 'affine:general',
5437
keyBinding: {
5538
binding: '$mod+K',
56-
capture: true,
5739
},
5840
label: '',
5941
icon: '',
6042
run() {
61-
// Due to a conflict with the shortcut for creating a link after selecting text in blocksuite,
62-
// opening the quick search modal is disabled when link-popup is visitable.
63-
if (hasLinkPopover(editor)) {
64-
return;
65-
}
6643
service.toggle();
6744
},
6845
});
@@ -76,7 +53,6 @@ export function useRegisterWorkspaceCommands() {
7653
const urlService = useService(UrlService);
7754
const pageHelper = usePageHelper(currentWorkspace.docCollection);
7855
const navigationHelper = useNavigateHelper();
79-
const [editor] = useActiveBlocksuiteEditor();
8056
const cmdkQuickSearchService = useService(CMDKQuickSearchService);
8157
const editorSettingService = useService(EditorSettingService);
8258
const workspaceDialogService = useService(WorkspaceDialogService);
@@ -88,12 +64,12 @@ export function useRegisterWorkspaceCommands() {
8864
useServiceOptional(DesktopApiService)?.handler.updater.quitAndInstall;
8965

9066
useEffect(() => {
91-
const unsub = registerCMDKCommand(cmdkQuickSearchService, editor);
67+
const unsub = registerCMDKCommand(cmdkQuickSearchService);
9268

9369
return () => {
9470
unsub();
9571
};
96-
}, [cmdkQuickSearchService, editor]);
72+
}, [cmdkQuickSearchService]);
9773

9874
// register AffineUpdatesCommands
9975
useEffect(() => {

0 commit comments

Comments
 (0)