feat: focus the create page item when query returns no result in at menu (#10060)

fix AF-2191
This commit is contained in:
pengx17
2025-02-10 15:51:55 +00:00
parent f774868f0e
commit fd25cd875b
4 changed files with 86 additions and 1 deletions

View File

@@ -45,6 +45,23 @@ export interface LinkedWidgetConfig {
abortSignal: AbortSignal
) => Promise<LinkedMenuGroup[]> | LinkedMenuGroup[];
/**
* Auto focused item
*
* Will be called when the menu is
* - opened
* - query changed
* - menu group or its items changed
*
* If the return value is not null, no action will be taken.
*/
autoFocusedItem?: (
menus: LinkedMenuGroup[],
query: string,
editorHost: EditorHost,
inlineEditor: AffineInlineEditor
) => LinkedMenuItem | null;
mobile: {
useScreenHeight?: boolean;
/**

View File

@@ -17,6 +17,7 @@ import {
throttle,
WithDisposable,
} from '@blocksuite/global/utils';
import { effect } from '@preact/signals-core';
import { css, html, LitElement, nothing } from 'lit';
import { property, query, queryAll, state } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
@@ -47,6 +48,8 @@ export class LinkedDocPopover extends SignalWatcher(
private readonly _expanded = new Map<string, boolean>();
private _menusItemsEffectCleanup: () => void = () => {};
private readonly _updateLinkedDocGroup = async () => {
const query = this._query;
if (this._updateLinkedDocGroupAbortController) {
@@ -65,6 +68,30 @@ export class LinkedDocPopover extends SignalWatcher(
this.context.inlineEditor,
this._updateLinkedDocGroupAbortController.signal
);
this._menusItemsEffectCleanup();
// need to rebind the effect because this._linkedDocGroup has changed.
this._menusItemsEffectCleanup = effect(() => {
this._updateAutoFocusedItem();
});
};
private readonly _updateAutoFocusedItem = () => {
if (!this._query) {
return;
}
const autoFocusedItem = this.context.config.autoFocusedItem?.(
this._linkedDocGroup,
this._query,
this.context.std.host,
this.context.inlineEditor
);
if (autoFocusedItem) {
this._activatedItemIndex = this._flattenActionList.findIndex(
item => item.key === autoFocusedItem.key
);
}
};
private _updateLinkedDocGroupAbortController: AbortController | null = null;
@@ -217,6 +244,11 @@ export class LinkedDocPopover extends SignalWatcher(
});
}
override disconnectedCallback() {
super.disconnectedCallback();
this._menusItemsEffectCleanup();
}
override render() {
const MAX_HEIGHT = 380;
const style = this._position