|
@@ -0,0 +1,181 @@
|
|
|
+import { useEditor } from "@/modules/editor";
|
|
|
+import {
|
|
|
+ DeleteOutlined,
|
|
|
+ FormOutlined,
|
|
|
+ PlusSquareOutlined,
|
|
|
+} from "@ant-design/icons-vue";
|
|
|
+import { css } from "@linaria/core";
|
|
|
+import { Button, Tree } from "ant-design-vue";
|
|
|
+import { queenApi } from "queenjs";
|
|
|
+import { defineComponent } from "vue";
|
|
|
+import { any } from "vue-types";
|
|
|
+
|
|
|
+export default defineComponent({
|
|
|
+ props: {
|
|
|
+ data: any(),
|
|
|
+ },
|
|
|
+ setup(props) {
|
|
|
+ const formatTreeData = (data: any, level = 0) => {
|
|
|
+ let res = [] as any;
|
|
|
+ data?.map((item: any, index: number) => {
|
|
|
+ res[index] = {};
|
|
|
+ res[index].id = item.id;
|
|
|
+ res[index].level = level;
|
|
|
+ res[index].name = item.name;
|
|
|
+ let arr = [] as any;
|
|
|
+ if (Array.isArray(item.children) && item.children.length > 0) {
|
|
|
+ arr = formatTreeData(item.children, level + 1);
|
|
|
+ res[index].children = arr;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return res;
|
|
|
+ };
|
|
|
+
|
|
|
+ return () => (
|
|
|
+ <Tree.DirectoryTree
|
|
|
+ class={TreeRoot}
|
|
|
+ treeData={formatTreeData(props.data)}
|
|
|
+ showIcon={false}
|
|
|
+ blockNode={true}
|
|
|
+ >
|
|
|
+ {{
|
|
|
+ title: (data: any) => {
|
|
|
+ return <TreeNode item={data} />;
|
|
|
+ },
|
|
|
+ }}
|
|
|
+ </Tree.DirectoryTree>
|
|
|
+ );
|
|
|
+ },
|
|
|
+});
|
|
|
+const TreeNode = defineComponent({
|
|
|
+ props: {
|
|
|
+ item: any().isRequired,
|
|
|
+ },
|
|
|
+ setup(props) {
|
|
|
+ const { controls } = useEditor();
|
|
|
+ const addItem = async (parent = "_") => {
|
|
|
+ const name = await queenApi.showInput({
|
|
|
+ title: "分类名称",
|
|
|
+ placeholder: "请输入分类名称",
|
|
|
+ });
|
|
|
+ if (!name) return;
|
|
|
+ controls.categoryCtrl.updateCate({ name, parent });
|
|
|
+ };
|
|
|
+ const deleteItem = async (id: string) => {
|
|
|
+ let sure = await queenApi.showConfirm({
|
|
|
+ type: "danger",
|
|
|
+ title: "删除分类",
|
|
|
+ content: "确定删除该分类?",
|
|
|
+ });
|
|
|
+ if (!sure) return;
|
|
|
+ controls.categoryCtrl.deleteCate(id);
|
|
|
+ };
|
|
|
+ const updateItem = async (item: any) => {
|
|
|
+ let res = await queenApi.showInput({
|
|
|
+ title: "分类名称",
|
|
|
+ defaultValue: item.name,
|
|
|
+ placeholder: "请输入分类名称",
|
|
|
+ });
|
|
|
+ if (!res) return;
|
|
|
+ item.name = res;
|
|
|
+ controls.categoryCtrl.updateCate({ id: item.id, name: item.name });
|
|
|
+ };
|
|
|
+ return () => {
|
|
|
+ const { item } = props;
|
|
|
+ console.log(item);
|
|
|
+ return (
|
|
|
+ <div class={"tree_item"} key={item.id}>
|
|
|
+ <div class="item_txt">{item.name || "undefined"}</div>
|
|
|
+ <div class="item_btn">
|
|
|
+ {item.level != 3 && (
|
|
|
+ <Button
|
|
|
+ type="text"
|
|
|
+ class="tree_btn"
|
|
|
+ title="添加子分类"
|
|
|
+ icon={<PlusSquareOutlined />}
|
|
|
+ onClick={(e) => {
|
|
|
+ e.stopPropagation();
|
|
|
+ addItem(item.id);
|
|
|
+ }}
|
|
|
+ ></Button>
|
|
|
+ )}
|
|
|
+
|
|
|
+ {item.level != 0 && (
|
|
|
+ <>
|
|
|
+ <Button
|
|
|
+ type="text"
|
|
|
+ class="tree_btn"
|
|
|
+ title="编辑分类"
|
|
|
+ icon={<FormOutlined />}
|
|
|
+ onClick={(e) => {
|
|
|
+ e.stopPropagation();
|
|
|
+ updateItem(item);
|
|
|
+ }}
|
|
|
+ ></Button>
|
|
|
+ <Button
|
|
|
+ type="text"
|
|
|
+ class="tree_btn"
|
|
|
+ title="删除分类"
|
|
|
+ icon={<DeleteOutlined />}
|
|
|
+ onClick={(e) => {
|
|
|
+ e.stopPropagation();
|
|
|
+ deleteItem(item.id);
|
|
|
+ }}
|
|
|
+ ></Button>
|
|
|
+ </>
|
|
|
+ )}
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ };
|
|
|
+ },
|
|
|
+});
|
|
|
+
|
|
|
+const TreeRoot = css`
|
|
|
+ width: 100%;
|
|
|
+ .ant-tree-node-selected {
|
|
|
+ .tree_item {
|
|
|
+ .item_btn {
|
|
|
+ .tree_btn {
|
|
|
+ color: #fff;
|
|
|
+ &:hover {
|
|
|
+ color: #fff;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ .tree_item {
|
|
|
+ width: 100%;
|
|
|
+ display: flex;
|
|
|
+ align-items: center;
|
|
|
+ .item_txt {
|
|
|
+ display: flex;
|
|
|
+ align-items: center;
|
|
|
+ .item_tit {
|
|
|
+ font-size: 14px;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ &:hover {
|
|
|
+ .item_btn {
|
|
|
+ display: block;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ .item_btn {
|
|
|
+ flex: 1;
|
|
|
+ display: none;
|
|
|
+ margin-left: 0.2rem;
|
|
|
+ .tree_btn {
|
|
|
+ margin: 0 6px;
|
|
|
+ min-width: auto;
|
|
|
+ color: #666;
|
|
|
+ border: none;
|
|
|
+ background-color: transparent;
|
|
|
+ box-shadow: none;
|
|
|
+ &:hover {
|
|
|
+ color: @inf-primary-color;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+`;
|