Commit 122db78e8487c2b9a0b1c86733d136e890b9eaeb
Committed by
GitHub
1 parent
5c69b3d5
feat(directives/ellipsis): add ellipsis directive (#2688)
Showing
2 changed files
with
44 additions
and
0 deletions
src/directives/ellipsis.ts
0 → 100644
1 | +import type { CSSProperties, DirectiveBinding, ObjectDirective, App } from 'vue'; | |
2 | + | |
3 | +interface IValue { | |
4 | + width?: number; | |
5 | + line?: number; | |
6 | +} | |
7 | + | |
8 | +interface IOptions { | |
9 | + [key: string]: CSSProperties; | |
10 | +} | |
11 | + | |
12 | +const cssProperties: IOptions = { | |
13 | + single: { | |
14 | + overflow: 'hidden', | |
15 | + textOverflow: 'ellipsis', | |
16 | + whiteSpace: 'nowrap', | |
17 | + }, | |
18 | + multiple: { | |
19 | + display: '-webkit-box', | |
20 | + overflow: 'hidden', | |
21 | + wordBreak: 'break-all', | |
22 | + }, | |
23 | +}; | |
24 | + | |
25 | +const Ellipsis: ObjectDirective = { | |
26 | + mounted(el: HTMLElement, binding: DirectiveBinding<Array<IValue>>) { | |
27 | + const { value = [100, 1], arg = 'single' } = binding; | |
28 | + const [width, line] = value; | |
29 | + Object.entries(cssProperties[arg]).forEach(([key, value]) => { | |
30 | + el.style[key] = value; | |
31 | + }); | |
32 | + el.style.width = `${width}px`; | |
33 | + if (arg === 'multiple') { | |
34 | + el.style.webkitLineClamp = `${line}`; | |
35 | + el.style.webkitBoxOrient = 'vertical'; | |
36 | + } | |
37 | + }, | |
38 | +}; | |
39 | +export function setupEllipsisDirective(app: App) { | |
40 | + app.directive('ellipsis', Ellipsis); | |
41 | +} | |
42 | +export default Ellipsis; | |
... | ... |
src/directives/index.ts
... | ... | @@ -4,8 +4,10 @@ |
4 | 4 | import type { App } from 'vue'; |
5 | 5 | import { setupPermissionDirective } from './permission'; |
6 | 6 | import { setupLoadingDirective } from './loading'; |
7 | +import { setupEllipsisDirective } from './ellipsis'; | |
7 | 8 | |
8 | 9 | export function setupGlobDirectives(app: App) { |
9 | 10 | setupPermissionDirective(app); |
10 | 11 | setupLoadingDirective(app); |
12 | + setupEllipsisDirective(app); | |
11 | 13 | } |
... | ... |