Skip to content

Commit

Permalink
refactor(editor): extract filterable list component (#9278)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Dec 24, 2024
1 parent ea0a345 commit 4ce5cf2
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 69 deletions.
1 change: 1 addition & 0 deletions blocksuite/affine/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"./context-menu": "./src/context-menu/index.ts",
"./date-picker": "./src/date-picker/index.ts",
"./drag-indicator": "./src/drag-indicator/index.ts",
"./filterable-list": "./src/filterable-list/index.ts",
"./virtual-keyboard": "./src/virtual-keyboard/index.ts",
"./toggle-button": "./src/toggle-button/index.ts",
"./notification": "./src/notification/index.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import {
type AdvancedPortalOptions,
createLitPortal,
} from '@blocksuite/affine-components/portal';
import { PAGE_HEADER_HEIGHT } from '@blocksuite/affine-shared/consts';
import { WithDisposable } from '@blocksuite/global/utils';
import { DoneIcon, SearchIcon } from '@blocksuite/icons/lit';
import { autoPlacement, offset, type Placement, size } from '@floating-ui/dom';
import { html, LitElement, nothing } from 'lit';
import { property, query, state } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';

import { PAGE_HEADER_HEIGHT } from '../../consts.js';
import { type AdvancedPortalOptions, createLitPortal } from '../portal';
import { filterableListStyles } from './styles.js';
import type { FilterableListItem, FilterableListOptions } from './types.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { PANEL_BASE } from '@blocksuite/affine-shared/styles';
import { PANEL_BASE, scrollbarStyle } from '@blocksuite/affine-shared/styles';
import { css } from 'lit';

import { scrollbarStyle } from '../utils.js';

export const filterableListStyles = css`
:host {
${PANEL_BASE};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { TemplateResult } from 'lit';

export type FilterableListItemKey = string;

export interface FilterableListItem<Props = unknown> {
name: string;
label?: string;
Expand Down
5 changes: 3 additions & 2 deletions blocksuite/affine/shared/src/styles/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { FONT_BASE, FONT_SM, FONT_XS } from './font.js';
export { PANEL_BASE, PANEL_BASE_COLORS } from './panel.js';
export { FONT_BASE, FONT_SM, FONT_XS } from './font';
export { PANEL_BASE, PANEL_BASE_COLORS } from './panel';
export { scrollbarStyle } from './scrollbar-style';
37 changes: 37 additions & 0 deletions blocksuite/affine/shared/src/styles/scrollbar-style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { css, unsafeCSS } from 'lit';

/**
* You should add a container before the scrollbar style to prevent the style pollution of the whole doc.
*/
export const scrollbarStyle = (container: string) => {
if (!container) {
console.error(
'To prevent style pollution of the whole doc, you must add a container before the scrollbar style.'
);
return css``;
}

// sanitize container name
if (container.includes('{') || container.includes('}')) {
console.error('Invalid container name! Please use a valid CSS selector.');
return css``;
}

return css`
${unsafeCSS(container)} {
scrollbar-gutter: stable;
}
${unsafeCSS(container)}::-webkit-scrollbar {
-webkit-appearance: none;
width: 4px;
height: 4px;
}
${unsafeCSS(container)}::-webkit-scrollbar-thumb {
border-radius: 2px;
background-color: #b1b1b1;
}
${unsafeCSS(container)}::-webkit-scrollbar-corner {
display: none;
}
`;
};
1 change: 0 additions & 1 deletion blocksuite/blocks/src/_common/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './ai-item/index.js';
export { scrollbarStyle } from './utils.js';
37 changes: 0 additions & 37 deletions blocksuite/blocks/src/_common/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import type { EditorHost } from '@blocksuite/block-std';
import type { InlineEditor, InlineRange } from '@blocksuite/inline';
import { BlockModel } from '@blocksuite/store';
import { css, unsafeCSS } from 'lit';

export function getQuery(
inlineEditor: InlineEditor,
Expand Down Expand Up @@ -218,39 +217,3 @@ export function cleanSpecifiedTail(
length: 0,
});
}

/**
* You should add a container before the scrollbar style to prevent the style pollution of the whole doc.
*/
export const scrollbarStyle = (container: string) => {
if (!container) {
console.error(
'To prevent style pollution of the whole doc, you must add a container before the scrollbar style.'
);
return css``;
}

// sanitize container name
if (container.includes('{') || container.includes('}')) {
console.error('Invalid container name! Please use a valid CSS selector.');
return css``;
}

return css`
${unsafeCSS(container)} {
scrollbar-gutter: stable;
}
${unsafeCSS(container)}::-webkit-scrollbar {
-webkit-appearance: none;
width: 4px;
height: 4px;
}
${unsafeCSS(container)}::-webkit-scrollbar-thumb {
border-radius: 2px;
background-color: #b1b1b1;
}
${unsafeCSS(container)}::-webkit-scrollbar-corner {
display: none;
}
`;
};
2 changes: 1 addition & 1 deletion blocksuite/blocks/src/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { effects as componentCaptionEffects } from '@blocksuite/affine-component
import { effects as componentContextMenuEffects } from '@blocksuite/affine-components/context-menu';
import { effects as componentDatePickerEffects } from '@blocksuite/affine-components/date-picker';
import { effects as componentDragIndicatorEffects } from '@blocksuite/affine-components/drag-indicator';
import { FilterableListComponent } from '@blocksuite/affine-components/filterable-list';
import { effects as componentPortalEffects } from '@blocksuite/affine-components/portal';
import { effects as componentRichTextEffects } from '@blocksuite/affine-components/rich-text';
import { effects as componentToggleButtonEffects } from '@blocksuite/affine-components/toggle-button';
Expand All @@ -27,7 +28,6 @@ import { EmbedCardStyleMenu } from './_common/components/embed-card/embed-card-s
import { EmbedCardEditCaptionEditModal } from './_common/components/embed-card/modal/embed-card-caption-edit-modal.js';
import { EmbedCardCreateModal } from './_common/components/embed-card/modal/embed-card-create-modal.js';
import { EmbedCardEditModal } from './_common/components/embed-card/modal/embed-card-edit-modal.js';
import { FilterableListComponent } from './_common/components/filterable-list/index.js';
import { AIItemList } from './_common/components/index.js';
import { Loader } from './_common/components/loader.js';
import { SmoothCorner } from './_common/components/smooth-corner.js';
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/blocks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable @typescript-eslint/triple-slash-reference */
/* oxlint-disable @typescript-eslint/triple-slash-reference */
/// <reference path="./effects.ts" />
import { deserializeXYWH, Point } from '@blocksuite/global/utils';

Expand All @@ -8,7 +8,6 @@ import { isCanvasElement } from './root-block/edgeless/utils/query.js';

export * from './_common/adapters/index.js';
export * from './_common/components/ai-item/index.js';
export { scrollbarStyle } from './_common/components/index.js';
export { type NavigatorMode } from './_common/edgeless/frame/consts.js';
export {
ExportManager,
Expand Down Expand Up @@ -91,6 +90,7 @@ export {
} from '@blocksuite/affine-components/toolbar';
export * from '@blocksuite/affine-model';
export * from '@blocksuite/affine-shared/services';
export { scrollbarStyle } from '@blocksuite/affine-shared/styles';
export {
ColorVariables,
FontFamilyVariables,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
MenuItemGroup,
} from '@blocksuite/affine-components/toolbar';
import { renderGroups } from '@blocksuite/affine-components/toolbar';
import { assertExists, noop, WithDisposable } from '@blocksuite/global/utils';
import { noop, WithDisposable } from '@blocksuite/global/utils';
import { flip, offset } from '@floating-ui/dom';
import { css, html, LitElement } from 'lit';
import { property, query, state } from 'lit/decorators.js';
Expand Down Expand Up @@ -68,7 +68,12 @@ export class AffineCodeToolbar extends WithDisposable(LitElement) {

this._currentOpenMenu = this._popMenuAbortController;

assertExists(this._moreButton);
if (!this._moreButton) {
console.error(
'Failed to open more menu in code toolbar! Unexpected missing more button'
);
return;
}

createLitPortal({
template: html`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {
type FilterableListItem,
type FilterableListOptions,
showPopFilterableList,
} from '@blocksuite/affine-components/filterable-list';
import { ArrowDownIcon } from '@blocksuite/affine-components/icons';
import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
import { noop, SignalWatcher, WithDisposable } from '@blocksuite/global/utils';
Expand All @@ -6,11 +11,6 @@ import { property, query } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
import { html } from 'lit/static-html.js';

import {
type FilterableListItem,
type FilterableListOptions,
showPopFilterableList,
} from '../../../../_common/components/filterable-list/index.js';
import type { CodeBlockComponent } from '../../../../code-block/code-block.js';

export class LanguageListButton extends WithDisposable(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { scrollbarStyle } from '@blocksuite/affine-shared/styles';
import { on, stopPropagation } from '@blocksuite/affine-shared/utils';
import type { EditorHost } from '@blocksuite/block-std';
import { WithDisposable } from '@blocksuite/global/utils';
import { css, html, LitElement, nothing } from 'lit';
import { property } from 'lit/decorators.js';

import type { AIItemGroupConfig } from '../../../_common/components/ai-item/types.js';
import { scrollbarStyle } from '../../../_common/components/utils.js';
import type { EdgelessRootBlockComponent } from '../../edgeless/edgeless-root-block.js';

export class EdgelessCopilotPanel extends WithDisposable(LitElement) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { scrollbarStyle } from '@blocksuite/affine-shared/styles';
import { css } from 'lit';

import { scrollbarStyle } from '../../../_common/components/utils.js';

const paragraphButtonStyle = css`
.paragraph-button-icon > svg:nth-child(2) {
transition-duration: 0.3s;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { scrollbarStyle } from '@blocksuite/affine-shared/styles';
import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
import { css } from 'lit';

import { scrollbarStyle } from '../../../_common/components/utils.js';

export const TOOLBAR_HEIGHT = 46;

export const keyboardToolbarStyles = css`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { scrollbarStyle } from '@blocksuite/affine-shared/styles';
import { unsafeCSSVar, unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
import { baseTheme } from '@toeverything/theme';
import { css, unsafeCSS } from 'lit';

import { scrollbarStyle } from '../../../_common/components/utils.js';

export const linkedDocWidgetStyles = css`
.input-mask {
position: absolute;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { scrollbarStyle } from '@blocksuite/affine-shared/styles';
import { baseTheme } from '@toeverything/theme';
import { css, unsafeCSS } from 'lit';

import { scrollbarStyle } from '../../../_common/components/utils.js';

export const styles = css`
.overlay-mask {
pointer-events: auto;
Expand Down

0 comments on commit 4ce5cf2

Please sign in to comment.