forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<template> | ||
<div class="draggable-tree"></div> | ||
</template> | ||
|
||
<script setup> | ||
import { computed, defineProps, watch } from 'vue' | ||
const props = defineProps({ | ||
data: { | ||
type: Array, | ||
default: () => [] | ||
}, | ||
idKey: { | ||
type: String, | ||
default: 'id' | ||
}, | ||
labelKey: { | ||
type: String, | ||
default: 'label' | ||
}, | ||
childrenKey: { | ||
type: String, | ||
default: 'children' | ||
} | ||
}) | ||
/** | ||
* @typedef {Object} Node | ||
* @property {string} id | ||
* @property {string} label | ||
* @property {Node[]} [children] | ||
* @property {any} rawData | ||
*/ | ||
/** | ||
* | ||
* @param dataItem | ||
* @returns {Node} | ||
*/ | ||
const normalizeDataItem = (dataItem) => { | ||
const { idKey, labelKey, childrenKey } = props | ||
const id = dataItem[idKey] | ||
const label = dataItem[labelKey] | ||
const children = dataItem[childrenKey] | ||
const result = { id, label, rawData: dataItem } | ||
if (Array.isArray(children)) { | ||
result.children = children.map((child) => normalizeDataItem(child)) | ||
} | ||
return result | ||
} | ||
const normalizeData = (data) => { | ||
if (!Array.isArray(data)) { | ||
return [] | ||
} | ||
return data.map((item) => normalizeDataItem(item)) | ||
} | ||
const normalizedData = computed(() => normalizeData(props.data)) | ||
/** | ||
* @typedef {Object} ListItem | ||
* @property {string} id | ||
* @property {string} label | ||
* @property {number} level level 为 0 表示顶层节点 | ||
* @property {string} [parentId] | ||
* @property {any} rawData | ||
*/ | ||
/** | ||
* | ||
* @param {Node} node | ||
* @param {string} [parentId] | ||
* @param {number} level | ||
* @returns {ListItem[]} | ||
*/ | ||
const flatternNode = (node, parentId, level = 0) => { | ||
const { children, ...rest } = node | ||
const childNodes = (children || []) | ||
.map((child) => flatternNode(child, node.id, level + 1)) | ||
.reduce((acc, current) => acc.concat(current), []) | ||
const listItem = { ...rest, parentId, level } | ||
return [listItem].concat(childNodes) | ||
} | ||
/** | ||
* | ||
* @param {Node[]} nodes | ||
* @returns {ListItem[]} | ||
*/ | ||
const flatternNodes = (nodes) => { | ||
const dummyNode = { children: nodes } | ||
return flatternNode(dummyNode, null, -1).slice(1) | ||
} | ||
const listItems = computed(() => flatternNodes(normalizedData.value)) | ||
watch( | ||
listItems, | ||
(value) => { | ||
// eslint-disable-next-line no-console | ||
console.log(value) | ||
}, | ||
{ | ||
immediate: true | ||
} | ||
) | ||
</script> | ||
|
||
<style lang="less" scoped> | ||
.draggable-tree { | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 原版大纲数功能记录 | ||
|
||
- 展开、收起 | ||
- 全部展开按钮(逻辑实现了,UI 未体现) | ||
- 悬停节点,画布中出现对应的 hover 框 | ||
- 悬停节点,展示“眼睛”。点击“眼睛”,画布中元素显隐 | ||
- 点击节点,画布中选中节点 | ||
- 如果节点是 Block,样式稍微不一样 | ||
|
||
其他测试点: | ||
|
||
- 拖拽 | ||
- 超高列表,带滚动条 |