CompTree.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { css } from "@linaria/core";
  2. import { useReactive } from "@queenjs/use";
  3. import { Tree } from "ant-design-vue";
  4. import { defineComponent } from "vue";
  5. import { string } from "vue-types";
  6. import { useEditor } from "../../../..";
  7. import { DesignComp } from "../../../../objects/DesignTemp/DesignComp";
  8. import { CompUI } from "../../../CompUI";
  9. import { TreeToolbars } from "@/modules/editor/objects/Toolbars";
  10. type TreeItem = {
  11. key: string;
  12. title: string;
  13. value: string;
  14. children: TreeItem[];
  15. };
  16. export const CompTree = defineComponent({
  17. setup() {
  18. const { store, actions, helper } = useEditor();
  19. const state = useReactive(() => ({
  20. expandedKeys() {
  21. const comps = helper.getCompTrees(store.currCompId);
  22. return comps.map((comp) => comp.id);
  23. },
  24. treeData() {
  25. const rootComp = helper.findRootComp();
  26. function getCompChildren(ids: string[]): TreeItem[] {
  27. return ids.map((id) => {
  28. const comp = helper.findComp(id) as DesignComp;
  29. return {
  30. key: comp.id,
  31. title: CompUI[comp.compKey].options.name,
  32. value: comp.id,
  33. children: getCompChildren(helper.getCompChildIds(comp)),
  34. };
  35. });
  36. }
  37. return getCompChildren(rootComp?.id ? [rootComp.id] : []);
  38. },
  39. }));
  40. return () => (
  41. <Tree
  42. class={treeStyle}
  43. treeData={state.treeData}
  44. expandedKeys={state.expandedKeys}
  45. selectedKeys={[store.currCompId]}
  46. blockNode={true}
  47. onSelect={(ids) =>
  48. actions.pickComp(
  49. (ids[0] as string) || state.expandedKeys.at(-2) || "root"
  50. )
  51. }
  52. >
  53. {{
  54. title: (data: any) => {
  55. return <CompNode title={data.title} id={data.key} />;
  56. },
  57. }}
  58. </Tree>
  59. );
  60. },
  61. });
  62. const CompNode = defineComponent({
  63. props: {
  64. id: string().isRequired,
  65. title: string().isRequired,
  66. },
  67. setup(props) {
  68. const editor = useEditor();
  69. const comp = editor.helper.findComp(props.id);
  70. return () => {
  71. if (!comp) return;
  72. const compOpts = CompUI[comp.compKey].options;
  73. const actions = TreeToolbars[comp.compKey] || TreeToolbars.default;
  74. const thumbnail =
  75. comp.compKey === "Image" ? comp.value.url : compOpts.thumbnail;
  76. return (
  77. <div class={nodeStyle}>
  78. <img src={thumbnail} />
  79. <span class="flex-1">{props.title}</span>
  80. <span class="space-x-4px">
  81. {actions.map((action) => {
  82. if (action.disable?.call(editor, comp)) return;
  83. return (
  84. <action.component
  85. class="p-4px"
  86. key={comp.id}
  87. value={action.getValue?.(comp)}
  88. onClick={(e: MouseEvent) => {
  89. e.stopPropagation();
  90. action.onClick.call(editor, comp);
  91. }}
  92. />
  93. );
  94. })}
  95. </span>
  96. </div>
  97. );
  98. };
  99. },
  100. });
  101. const treeStyle = css`
  102. .ant-tree-switcher {
  103. display: flex;
  104. justify-content: center;
  105. align-items: center;
  106. }
  107. `;
  108. const nodeStyle = css`
  109. display: flex;
  110. align-items: center;
  111. padding: 6px;
  112. @apply rounded;
  113. > img {
  114. width: 24px;
  115. height: 24px;
  116. margin-right: 6px;
  117. object-fit: contain;
  118. }
  119. `;