feat(directives/ellipsis): add ellipsis directive (#2688)

This commit is contained in:
kissGoodBye 2023-04-07 09:22:51 +08:00 committed by GitHub
parent 5c69b3d5a8
commit 122db78e84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,42 @@
import type { CSSProperties, DirectiveBinding, ObjectDirective, App } from 'vue';
interface IValue {
width?: number;
line?: number;
}
interface IOptions {
[key: string]: CSSProperties;
}
const cssProperties: IOptions = {
single: {
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
},
multiple: {
display: '-webkit-box',
overflow: 'hidden',
wordBreak: 'break-all',
},
};
const Ellipsis: ObjectDirective = {
mounted(el: HTMLElement, binding: DirectiveBinding<Array<IValue>>) {
const { value = [100, 1], arg = 'single' } = binding;
const [width, line] = value;
Object.entries(cssProperties[arg]).forEach(([key, value]) => {
el.style[key] = value;
});
el.style.width = `${width}px`;
if (arg === 'multiple') {
el.style.webkitLineClamp = `${line}`;
el.style.webkitBoxOrient = 'vertical';
}
},
};
export function setupEllipsisDirective(app: App) {
app.directive('ellipsis', Ellipsis);
}
export default Ellipsis;

View File

@ -4,8 +4,10 @@
import type { App } from 'vue';
import { setupPermissionDirective } from './permission';
import { setupLoadingDirective } from './loading';
import { setupEllipsisDirective } from './ellipsis';
export function setupGlobDirectives(app: App) {
setupPermissionDirective(app);
setupLoadingDirective(app);
setupEllipsisDirective(app);
}