Skip to content

Commit

Permalink
Merge pull request #101 from wsfe/feat/dash-density
Browse files Browse the repository at this point in the history
feat: add dashDensity in showLine
  • Loading branch information
ChuChencheng authored Aug 10, 2024
2 parents 549e4fd + f5fb500 commit 8c45198
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 91 deletions.
8 changes: 7 additions & 1 deletion examples/Feature.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<div style="height: 300px">
<VTree
:data="showLineTreeData"
:showLine="{ type: showLineType, polyline: showLinePolyline }"
:showLine="{ type: showLineType, polyline: showLinePolyline, dashDensity }"
defaultExpandAll
animation
/>
Expand All @@ -169,6 +169,9 @@
{{ showLineType }}
</button>
<p>当前连接线类型:{{showLineType}}</p>
<p v-if="showLineType === 'dashed'">
虚线密度:<input type="range" v-model.number="dashDensity" :min="1" :max="10" :step="1" /> {{ dashDensity }}
</p>
</div>
<div class="desc-block">
showLine.polyline:
Expand Down Expand Up @@ -437,6 +440,8 @@ export default defineComponent({
showLineType.value = type
}
const dashDensity = ref(3)
const showLinePolyline = ref(false)
const onShowLinePolylineBtnClick = (polyline: boolean) => {
showLinePolyline.value = polyline
Expand Down Expand Up @@ -485,6 +490,7 @@ export default defineComponent({
onShowLineTypeBtnClick,
showLinePolyline,
onShowLinePolylineBtnClick,
dashDensity,
// 自定义节点
nodeSlotDescText,
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wsfe/vue-tree",
"version": "4.0.1",
"version": "4.1.0",
"types": "./types",
"description": "A vue tree component using virtual list.",
"main": "./dist/vue-tree.umd.js",
Expand Down Expand Up @@ -67,7 +67,6 @@
"@vue/repl": "^4.3.0",
"@vue/test-utils": "^2.4.6",
"@vue/vue3-jest": "^29.2.6",
"@wsfe/vue-tree": "^4.0.1",
"autoprefixer": "^10.4.19",
"happy-dom": "^14.12.0",
"less": "^4.2.0",
Expand Down
7 changes: 0 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions site/.vitepress/code/ShowLine.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<input type="radio" id="showline-polyline-false" :value="false" v-model="polyline" />
<label for="showline-polyline-false">false</label>
</div>

<div v-if="type === 'dashed'">
<span>showLine.dashDensity: </span>
<input type="range" :min="1" :max="10" :step="1" v-model.number="dashDensity" />
<span>{{ dashDensity }}</span>
</div>
</div>
<VTree :data="data" :showLine="showLine" animation defaultExpandAll />
</template>
Expand All @@ -29,11 +35,13 @@ import VTree from '@wsfe/vue-tree'
const type = ref('solid')
const polyline = ref(false)
const dashDensity = ref(3)
const showLine = computed(() => {
return {
type: type.value,
polyline: polyline.value,
dashDensity: dashDensity.value,
}
})
Expand Down
2 changes: 1 addition & 1 deletion site/.vitepress/components/PlaygroundLink.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<a :href="withBase(`/playground${serializedCode}`)" target="_blank" rel="noreferrer noopener">{{ text || i18n.openInPlayground }}</a>
<a :href="withBase('/playground') + serializedCode" target="_blank" rel="noreferrer noopener">{{ text || i18n.openInPlayground }}</a>
</template>

<script setup lang="ts">
Expand Down
9 changes: 9 additions & 0 deletions site/.vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'node:path'
import { defineConfig } from 'vitepress'
import zh from './zh.mjs'
import en from './en.mjs'
Expand Down Expand Up @@ -31,6 +32,14 @@ export default defineConfig({
{ icon: 'github', link: 'https://github.com/wsfe/vue-tree' }
]
},
vite: {
resolve: {
alias: {
'@wsfe/vue-tree/style.css': path.resolve('src/styles/index.less'),
'@wsfe/vue-tree': path.resolve('src'),
},
},
},

locales: {
root: {
Expand Down
74 changes: 37 additions & 37 deletions site/api/vtree.md

Large diffs are not rendered by default.

74 changes: 37 additions & 37 deletions site/en/api/vtree.md

Large diffs are not rendered by default.

19 changes: 13 additions & 6 deletions src/components/TreeNode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
width: `${nodeIndent}px`,
}"
>
<polyline
<path
fill="none"
:points="polylinePoints(index === data._level - 1)"
:d="polylinePoints(index === data._level - 1)"
:stroke-width="strokeWidth"
:stroke="showLineParams.color"
:stroke-dasharray="strokeDasharray"
Expand Down Expand Up @@ -114,14 +114,21 @@ const showLineParams = computed(() => {
type: showLineType.solid,
color: '#D3D3D3',
polyline: false,
dashDensity: 3,
}
let params: Required<ShowLine> = defaultParams
if (typeof props.showLine === 'object') {
let dashDensity = defaultParams.dashDensity
if (typeof props.showLine.dashDensity === 'number' && props.showLine.dashDensity >= 1 && props.showLine.dashDensity <= 10) {
dashDensity = props.showLine.dashDensity
}
params = {
width: props.showLine.width ?? defaultParams.width,
type: props.showLine.type ?? defaultParams.type,
color: props.showLine.color ?? defaultParams.color,
polyline: props.showLine.polyline ?? defaultParams.polyline,
dashDensity,
}
}
return params
Expand All @@ -132,18 +139,18 @@ const strokeWidth = computed(() => showLineParams.value.width * 100 / props.node
const strokeDasharray = computed(() => {
switch (showLineParams.value.type) {
case showLineType.dashed:
return '25'
return 100 / (showLineParams.value.dashDensity * 2)
default:
break
}
return 'none'
})
const polylinePoints = (isDirectParentLine: boolean) => {
if (!showLineParams.value.polyline || !isDirectParentLine) return '50,0 50,100'
if (!showLineParams.value.polyline || !isDirectParentLine) return 'M50,0 L50,100'
const parent = props.getNode(props.data[props.keyField])?._parent
if (parent && props.noSiblingNodeMap[parent[props.keyField]] && props.noSiblingNodeMap[props.data[props.keyField]]) return '50,0 50,50 100,50 50,50'
return '50,0 50,50 100,50 50,50 50,100'
if (parent && props.noSiblingNodeMap[parent[props.keyField]] && props.noSiblingNodeMap[props.data[props.keyField]]) return 'M50,0 L50,50 M100,50 L50,50'
return 'M50,0 L50,100 M100,50 L50,50'
}
const {
Expand Down
8 changes: 8 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ export interface INonReactiveData {
blockNodes: TreeNode[]
}

// Utils to generate types like 1 | 2 | 3
type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N
? Acc[number]
: Enumerate<N, [...Acc, Acc['length']]>

type IntRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F>>

export interface ShowLine {
/** 连接线宽度,svg stroke-width, 默认 1px */
width?: number
type?: showLineType
color?: string
polyline?: boolean
dashDensity?: IntRange<1, 11>
}

0 comments on commit 8c45198

Please sign in to comment.