Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(comp:text): use css ellipsis at one row to improve performance #1705

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 0 additions & 10 deletions packages/components/text/__tests__/__snapshots__/text.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
exports[`Text > copyable work 1`] = `
"<div class=\\"ix-text\\"><span class=\\"ix-text-inner\\">@idux 是一套企业级中后台 UI 组件库, 致力于提供高效愉悦的开发体验。 基于 Vue 3.x + TypeScript 开发,
全部代码开源并遵循 MIT 协议,任何企业、组织及个人均可免费使用。<!----></span>
<!---->
<!----><span class=\\"ix-text-copy-icon\\"><i class=\\"ix-icon ix-icon-copy\\" role=\\"img\\" aria-label=\\"copy\\"></i></span>
<!---->
<!---->
Expand All @@ -20,7 +19,6 @@ exports[`Text > copyable work 2`] = `
<!---->
<!---->
<!---->
<!---->
</div>"
`;

Expand All @@ -32,7 +30,6 @@ exports[`Text > ellipsis work 1`] = `
<!---->
<!---->
<!---->
<!---->
</div>"
`;

Expand All @@ -44,7 +41,6 @@ exports[`Text > ellipsis work 2`] = `
<!---->
<!---->
<!---->
<!---->
</div>"
`;

Expand All @@ -56,7 +52,6 @@ exports[`Text > expandable work 1`] = `
<!---->
<!---->
<!---->
<!---->
</div>"
`;

Expand All @@ -68,7 +63,6 @@ exports[`Text > expandable work 2`] = `
<!---->
<!---->
<!---->
<!---->
</div>"
`;

Expand All @@ -80,7 +74,6 @@ exports[`Text > render work 1`] = `
<!---->
<!---->
<!---->
<!---->
</div>"
`;

Expand All @@ -95,7 +88,6 @@ exports[`Text > tag work 1`] = `
<!---->
<!---->
<!---->
<!---->
</div>"
`;

Expand All @@ -107,7 +99,6 @@ exports[`Text > tag work 2`] = `
<!---->
<!---->
<!---->
<!---->
</div>"
`;

Expand All @@ -131,7 +122,6 @@ exports[`Text > tooltip work 2`] = `
<!---->
<!---->
<!---->
<!---->
</div>"
`;

Expand Down
14 changes: 10 additions & 4 deletions packages/components/text/src/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@
const mergedPrefixCls = computed(() => `${common.prefixCls}-text`)

const contentRef = shallowRef<HTMLElement>()
const innerRef = shallowRef<HTMLElement>()
const measureRef = shallowRef<HTMLElement>()
const rowMeasureRef = shallowRef<HTMLElement>()

const { expanded, expandable, setExpanded, expandIconRenderer } = useExpandable(props, config)
const { copied, copy, copyIconRenderer } = useCopyable(props, config)
const copyTooltip = useCopyTooltip(props)
const { isEllipsis, measureStatus, onRender, onMeasureRender, renderClampedContent } = useEllipsis(
const { isSimple, isEllipsis, measureStatus, onRender, onMeasureRender, renderClampedContent } = useEllipsis(
props,
expandable,
contentRef,
innerRef,
measureRef,
rowMeasureRef,
)
Expand Down Expand Up @@ -105,71 +108,74 @@
)
}

return () => {
const { tag: Tag, tooltip } = props
const prefixCls = mergedPrefixCls.value
const isNative = tooltip === 'native'
const titleSlot = slots.title || slots.default

const hasExpandIcon = !!slots.expandIcon || !!expandIconRenderer.value
const expandNode =
expandable.value && hasExpandIcon ? (
<div class={`${prefixCls}-expand-icon`} onClick={toggleExpanded}>
{(slots.expandIcon ?? expandIconRenderer.value)!({ expanded: expanded.value })}
</div>
) : undefined

const contentNodes = slots.default?.()

let node = (
<Tag
ref={innerRef}
class={`${prefixCls}-inner`}
title={isNative ? getStringBySlot(titleSlot) : undefined}
onClick={expandable.value && !hasExpandIcon ? toggleExpanded : undefined}
{...attrs}
>
{isEllipsis.value && !expanded.value && measureStatus.value === 'none'
{!isSimple.value && isEllipsis.value && !expanded.value && measureStatus.value === 'none'
? renderClampedContent(contentNodes)
: contentNodes}
{isEllipsis.value && !expanded.value && renderEllipsisNode()}
{!isSimple.value && isEllipsis.value && !expanded.value && renderEllipsisNode()}
</Tag>
)

if (tooltip && !isNative) {
if (isEllipsis.value && !expanded.value && tooltip && !isNative) {
const tooltipProps = isObject(tooltip) ? tooltip : {}
node = (
<IxTooltip {...tooltipProps} v-slots={{ title: titleSlot }}>
{node}
</IxTooltip>
)
}

const classes = normalizeClass({
[prefixCls]: true,
[`${prefixCls}-simple`]: isSimple.value,
[`${prefixCls}-ellipsis`]: isEllipsis.value,
[`${prefixCls}-expandable`]: expandable.value,
[`${prefixCls}-has-expand-icon`]: hasExpandIcon,
})

onRender(contentNodes)

if (measureStatus.value !== 'none') {
onMeasureRender()
}

return (
<div ref={contentRef} class={classes}>
{node}
{slots.suffix?.()}
{renderCopyNode()}
{isEllipsis.value && expandNode}
{measureStatus.value !== 'none' && renderMeasureElement()}
{measureStatus.value === 'preparing' && renderRowMeasureElement()}
</div>
)
}
},
})

Check notice on line 178 in packages/components/text/src/Text.tsx

View check run for this annotation

codefactor.io / CodeFactor

packages/components/text/src/Text.tsx#L111-L178

Complex Method
function getStringBySlot(slot: Slot | undefined) {
const validNode = getFirstValidNode(slot?.())
if (!validNode) {
Expand Down
10 changes: 10 additions & 0 deletions packages/components/text/src/composables/useEllipsis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type MeasureStatus = 'preparing' | 'measuring' | 'none'

export interface EllipsisContext {
isEllipsis: ComputedRef<boolean>
isSimple: ComputedRef<boolean>
measureStatus: ComputedRef<MeasureStatus>
onRender: (nodes: VNode[] | undefined) => void
onMeasureRender: () => void
Expand All @@ -28,7 +29,9 @@ export interface EllipsisContext {

export function useEllipsis(
props: TextProps,
expandable: ComputedRef<boolean>,
contentRef: Ref<HTMLElement | undefined>,
innerRef: Ref<HTMLElement | undefined>,
measureRef: Ref<HTMLElement | undefined>,
rowMeasureRef: Ref<HTMLElement | undefined>,
): EllipsisContext {
Expand Down Expand Up @@ -62,6 +65,7 @@ export function useEllipsis(

return 0
})
const isSimple = computed(() => rows.value === 1 && !expandable.value)

const onRender = (nodes: VNode[] | undefined) => {
setContentNodesLength(getNodesLength(nodes))
Expand Down Expand Up @@ -154,6 +158,11 @@ export function useEllipsis(
return
}

if (isSimple.value) {
setIsEllipsis(!!innerRef.value && innerRef.value.scrollWidth > innerRef.value.clientWidth)
return
}

calculate()
}

Expand All @@ -174,6 +183,7 @@ export function useEllipsis(

return {
isEllipsis,
isSimple,
measureStatus,
onRender,
onMeasureRender,
Expand Down
12 changes: 12 additions & 0 deletions packages/components/text/style/index.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import '../../style/mixins/reset.less';
@import '../../style//mixins/ellipsis.less';

Check warning on line 2 in packages/components/text/style/index.less

View check run for this annotation

codefactor.io / CodeFactor

packages/components/text/style/index.less#L2

Expected "'../../style//mixins/ellipsis.less'" to be "url('../../style//mixins/ellipsis.less')" (import-notation)

.@{text-prefix} {
.reset-component-new();
Expand All @@ -7,6 +8,7 @@
position: relative;
display: inline-block;
vertical-align: bottom;
max-width: 100%;

> .@{text-prefix}-expand-icon {
flex-shrink: 0;
Expand All @@ -15,6 +17,16 @@

overflow: hidden;

&.@{text-prefix}-simple {
display: inline-flex;
align-items: center;
.@{text-prefix}-inner {
display: inline-block;
flex: auto;
.ellipsis();
}
}

&-expandable:not(&-has-expand-icon) &-inner {
cursor: pointer;
}
Expand Down