mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-18 02:26:21 +08:00
refactor(editor): group and expose parameters of createButtonPopper (#10999)
This commit is contained in:
+6
-8
@@ -29,14 +29,12 @@ export class EdgelessFrameOrderButton extends WithDisposable(LitElement) {
|
||||
}
|
||||
|
||||
override firstUpdated() {
|
||||
this._edgelessFrameOrderPopper = createButtonPopper(
|
||||
this._edgelessFrameOrderButton,
|
||||
this._edgelessFrameOrderMenu,
|
||||
({ display }) => this.setPopperShow(display === 'show'),
|
||||
{
|
||||
mainAxis: 22,
|
||||
}
|
||||
);
|
||||
this._edgelessFrameOrderPopper = createButtonPopper({
|
||||
reference: this._edgelessFrameOrderButton,
|
||||
popperElement: this._edgelessFrameOrderMenu,
|
||||
stateUpdated: ({ display }) => this.setPopperShow(display === 'show'),
|
||||
mainAxis: 22,
|
||||
});
|
||||
}
|
||||
|
||||
protected override render() {
|
||||
|
||||
+6
-8
@@ -96,14 +96,12 @@ export class EdgelessNavigatorSettingButton extends WithDisposable(LitElement) {
|
||||
}
|
||||
|
||||
override firstUpdated() {
|
||||
this._navigatorSettingPopper = createButtonPopper(
|
||||
this._navigatorSettingButton,
|
||||
this._navigatorSettingMenu,
|
||||
({ display }) => this.setPopperShow(display === 'show'),
|
||||
{
|
||||
mainAxis: 22,
|
||||
}
|
||||
);
|
||||
this._navigatorSettingPopper = createButtonPopper({
|
||||
reference: this._navigatorSettingButton,
|
||||
popperElement: this._navigatorSettingMenu,
|
||||
stateUpdated: ({ display }) => this.setPopperShow(display === 'show'),
|
||||
mainAxis: 22,
|
||||
});
|
||||
}
|
||||
|
||||
override render() {
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { PANEL_BASE } from '@blocksuite/affine-shared/styles';
|
||||
import { createButtonPopper } from '@blocksuite/affine-shared/utils';
|
||||
import {
|
||||
type ButtonPopperOptions,
|
||||
createButtonPopper,
|
||||
} from '@blocksuite/affine-shared/utils';
|
||||
import { WithDisposable } from '@blocksuite/global/lit';
|
||||
import {
|
||||
css,
|
||||
@@ -25,13 +28,12 @@ export class EditorMenuButton extends WithDisposable(LitElement) {
|
||||
private _popper!: ReturnType<typeof createButtonPopper>;
|
||||
|
||||
override firstUpdated() {
|
||||
this._popper = createButtonPopper(
|
||||
this._trigger,
|
||||
this._content,
|
||||
({ display }) => {
|
||||
this._popper = createButtonPopper({
|
||||
reference: this._trigger,
|
||||
popperElement: this._content,
|
||||
stateUpdated: ({ display }) => {
|
||||
const opened = display === 'show';
|
||||
this._trigger.showTooltip = !opened;
|
||||
|
||||
this.dispatchEvent(
|
||||
new CustomEvent('toggle', {
|
||||
detail: opened,
|
||||
@@ -41,12 +43,11 @@ export class EditorMenuButton extends WithDisposable(LitElement) {
|
||||
})
|
||||
);
|
||||
},
|
||||
{
|
||||
mainAxis: 12,
|
||||
ignoreShift: true,
|
||||
offsetHeight: 6 * 4,
|
||||
}
|
||||
);
|
||||
mainAxis: 12,
|
||||
ignoreShift: true,
|
||||
offsetHeight: 6 * 4,
|
||||
...this.popperOptions,
|
||||
});
|
||||
this._disposables.addFromEvent(this, 'keydown', (e: KeyboardEvent) => {
|
||||
e.stopPropagation();
|
||||
if (e.key === 'Escape') {
|
||||
@@ -93,6 +94,9 @@ export class EditorMenuButton extends WithDisposable(LitElement) {
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor contentPadding: string | undefined = undefined;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor popperOptions: Partial<ButtonPopperOptions> = {};
|
||||
}
|
||||
|
||||
export class EditorMenuContent extends LitElement {
|
||||
|
||||
@@ -172,17 +172,15 @@ export class FramePanelHeader extends WithDisposable(LitElement) {
|
||||
override firstUpdated() {
|
||||
const disposables = this.disposables;
|
||||
|
||||
this._framesSettingMenuPopper = createButtonPopper(
|
||||
this._frameSettingButton,
|
||||
this._frameSettingMenu,
|
||||
({ display }) => {
|
||||
this._framesSettingMenuPopper = createButtonPopper({
|
||||
reference: this._frameSettingButton,
|
||||
popperElement: this._frameSettingMenu,
|
||||
stateUpdated: ({ display }) => {
|
||||
this._settingPopperShow = display === 'show';
|
||||
},
|
||||
{
|
||||
mainAxis: 14,
|
||||
crossAxis: -100,
|
||||
}
|
||||
);
|
||||
mainAxis: 14,
|
||||
crossAxis: -100,
|
||||
});
|
||||
disposables.add(this._framesSettingMenuPopper);
|
||||
}
|
||||
|
||||
|
||||
@@ -152,17 +152,15 @@ export class OutlineNoteCard extends SignalWatcher(
|
||||
}
|
||||
|
||||
override firstUpdated() {
|
||||
this._displayModePopper = createButtonPopper(
|
||||
this._displayModeButtonGroup,
|
||||
this._displayModePanel,
|
||||
({ display }) => {
|
||||
this._displayModePopper = createButtonPopper({
|
||||
reference: this._displayModeButtonGroup,
|
||||
popperElement: this._displayModePanel,
|
||||
stateUpdated: ({ display }) => {
|
||||
this._showPopper$.value = display === 'show';
|
||||
},
|
||||
{
|
||||
mainAxis: 0,
|
||||
crossAxis: -60,
|
||||
}
|
||||
);
|
||||
mainAxis: 0,
|
||||
crossAxis: -60,
|
||||
});
|
||||
|
||||
this.disposables.add(this._displayModePopper);
|
||||
}
|
||||
|
||||
@@ -22,17 +22,15 @@ export class OutlinePanelHeader extends SignalWatcher(
|
||||
private readonly _settingPopperShow$ = signal(false);
|
||||
|
||||
override firstUpdated() {
|
||||
this._notePreviewSettingMenuPopper = createButtonPopper(
|
||||
this._noteSettingButton,
|
||||
this._notePreviewSettingMenu,
|
||||
({ display }) => {
|
||||
this._notePreviewSettingMenuPopper = createButtonPopper({
|
||||
reference: this._noteSettingButton,
|
||||
popperElement: this._notePreviewSettingMenu,
|
||||
stateUpdated: ({ display }) => {
|
||||
this._settingPopperShow$.value = display === 'show';
|
||||
},
|
||||
{
|
||||
mainAxis: 14,
|
||||
crossAxis: -30,
|
||||
}
|
||||
);
|
||||
mainAxis: 14,
|
||||
crossAxis: -30,
|
||||
});
|
||||
this.disposables.add(this._notePreviewSettingMenuPopper);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,17 @@ export function listenClickAway(
|
||||
type Display = 'show' | 'hidden';
|
||||
|
||||
const ATTR_SHOW = 'data-show';
|
||||
|
||||
export type ButtonPopperOptions = {
|
||||
reference: HTMLElement;
|
||||
popperElement: HTMLElement;
|
||||
stateUpdated?: (state: { display: Display }) => void;
|
||||
mainAxis?: number;
|
||||
crossAxis?: number;
|
||||
rootBoundary?: Rect | (() => Rect | undefined);
|
||||
ignoreShift?: boolean;
|
||||
offsetHeight?: number;
|
||||
};
|
||||
/**
|
||||
* Using attribute 'data-show' to control popper visibility.
|
||||
*
|
||||
@@ -44,28 +55,19 @@ const ATTR_SHOW = 'data-show';
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function createButtonPopper(
|
||||
reference: HTMLElement,
|
||||
popperElement: HTMLElement,
|
||||
stateUpdated: (state: { display: Display }) => void = () => {
|
||||
/** DEFAULT EMPTY FUNCTION */
|
||||
},
|
||||
{
|
||||
export function createButtonPopper(options: ButtonPopperOptions) {
|
||||
let display: Display = 'hidden';
|
||||
let cleanup: (() => void) | void;
|
||||
const {
|
||||
reference,
|
||||
popperElement,
|
||||
stateUpdated = () => {},
|
||||
mainAxis,
|
||||
crossAxis,
|
||||
rootBoundary,
|
||||
ignoreShift,
|
||||
offsetHeight,
|
||||
}: {
|
||||
mainAxis?: number;
|
||||
crossAxis?: number;
|
||||
rootBoundary?: Rect | (() => Rect | undefined);
|
||||
ignoreShift?: boolean;
|
||||
offsetHeight?: number;
|
||||
} = {}
|
||||
) {
|
||||
let display: Display = 'hidden';
|
||||
let cleanup: (() => void) | void;
|
||||
} = options;
|
||||
|
||||
const originMaxHeight = window.getComputedStyle(popperElement).maxHeight;
|
||||
|
||||
|
||||
@@ -146,15 +146,14 @@ export class ChatCopyMore extends WithDisposable(LitElement) {
|
||||
this._morePopper?.dispose();
|
||||
this._morePopper = null;
|
||||
} else if (!this._morePopper) {
|
||||
this._morePopper = createButtonPopper(
|
||||
this._moreButton,
|
||||
this._moreMenu,
|
||||
({ display }) => (this._showMoreMenu = display === 'show'),
|
||||
{
|
||||
mainAxis: 0,
|
||||
crossAxis: -100,
|
||||
}
|
||||
);
|
||||
this._morePopper = createButtonPopper({
|
||||
reference: this._moreButton,
|
||||
popperElement: this._moreMenu,
|
||||
stateUpdated: ({ display }) =>
|
||||
(this._showMoreMenu = display === 'show'),
|
||||
mainAxis: 0,
|
||||
crossAxis: -100,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user