Skip to content

Commit

Permalink
feat(useEllipsis): init
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo-shen committed Jan 10, 2024
1 parent c066ab3 commit 8d6cca0
Show file tree
Hide file tree
Showing 8 changed files with 664 additions and 127 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@
},
"dependencies": {
"@noi/core": "workspace:*",
<<<<<<< Updated upstream
"vue": "^3.4.7"
=======
"@vueuse/core": "^10.7.1",
"@vueuse/motion": "^2.0.0",
"vue": "^3.4.6"
>>>>>>> Stashed changes
},
"devDependencies": {
"@types/node": "^20.10.8",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './useToast';
export * from './useToast'
export * from './useEllipsis'
34 changes: 31 additions & 3 deletions packages/core/useEllipsis/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { watch, watchEffect, type Ref } from 'vue'
import { useWindowSize } from '@vueuse/core'

interface UseEllipsisOptions {
rows?: number // 同组件 prop default: 1
content: string // 同组件 prop
Expand All @@ -9,15 +12,40 @@ interface UseEllipsisReturn {
state: 'expand' | 'collapse' // 当前状态
toggle: Function // 切换状态
}
const cloneElement = (target: HTMLElement): HTMLElement => {
const originStyle = window.getComputedStyle(target)
const container = document.createElement('div')
const styleNames: string[] = Array.prototype.slice.apply(originStyle)
styleNames.forEach((name) => {
container.style.setProperty(name, originStyle.getPropertyValue(name))
})

container.style.position = 'fixed'
container.style.zIndex = '-9999'
container.style.top = '-9999px'
container.style.height = 'auto'
container.style.minHeight = 'auto'
container.style.maxHeight = 'auto'

return container
}

// eslint-disable-next-line import/prefer-default-export
export const useEllipsis = (
_el: HTMLElement,
_options: UseEllipsisOptions
target: Ref<HTMLElement>,
options: UseEllipsisOptions
): UseEllipsisReturn => {
// const { width } = useWindowSize()
watchEffect(() => {
if (target.value) {
const cloneTarget = cloneElement(target.value)
cloneTarget.innerText = options.content
}
})

const toggle = () => {}
return {
content: '',
content: options.content.slice(0, 10),
state: 'collapse',
toggle,
}
Expand Down
Loading

0 comments on commit 8d6cca0

Please sign in to comment.