use-group.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { computed, reactive, ref, Ref, watchEffect } from "vue";
  2. import { PropertyDescribes } from "../html-mount/propertys";
  3. import { installGlobalVar } from "./use-global-vars";
  4. import { inRevise, mergeFuns } from "@/utils/shared";
  5. export const useGlobalDescribes = installGlobalVar(() => {
  6. const shapesDescribes: Record<string, PropertyDescribes> = reactive({});
  7. const shapesSetCount: Record<string, number> = {};
  8. const data: Record<string, { id: string }> = reactive({});
  9. return {
  10. set(item: { id: string }, descs: PropertyDescribes) {
  11. if (item.id in shapesSetCount) {
  12. shapesSetCount[item.id]++
  13. } else {
  14. shapesSetCount[item.id] = 1
  15. }
  16. shapesDescribes[item.id] = descs;
  17. data[item.id] = item;
  18. },
  19. del(id: string) {
  20. if (id in shapesSetCount) {
  21. shapesSetCount[id]--
  22. }
  23. if (shapesSetCount[id] === 0) {
  24. delete shapesDescribes[id];
  25. delete shapesSetCount[id]
  26. delete data[id]
  27. }
  28. },
  29. get(id: string) {
  30. return (
  31. shapesDescribes[id] && { desc: shapesDescribes[id], data: data[id] }
  32. );
  33. },
  34. };
  35. });
  36. export const useComponentsDescribes = (ids: Ref<string[]>) => {
  37. const gdesc = useGlobalDescribes();
  38. const excludeKeys = ["length", "name"];
  39. const groups = computed(() => {
  40. return ids.value.map((id) => gdesc.get(id)).filter((item) => !!item);
  41. });
  42. const shareDescribes = computed(() => {
  43. if (groups.value.length === 0) {
  44. return {};
  45. }
  46. const shareDescribes: Record<
  47. string,
  48. { desc: PropertyDescribes[string][]; data: { id: string }[] }
  49. > = {};
  50. const shareKeys: string[] = Object.keys(groups.value[0].desc);
  51. for (const item of groups.value) {
  52. const keys = Object.keys(item.desc);
  53. for (const key of keys) {
  54. if (shareKeys.includes(key)) {
  55. if (!shareDescribes[key]) {
  56. shareDescribes[key] = { desc: [], data: [] };
  57. } else {
  58. const temp = shareDescribes[key].desc[0];
  59. if (inRevise(temp.props, item.desc[key].props)) {
  60. delete shareDescribes[key];
  61. shareKeys.splice(shareKeys.indexOf(key), 1);
  62. continue;
  63. }
  64. }
  65. shareDescribes[key].desc.push(item.desc[key]);
  66. shareDescribes[key].data.push(item.data);
  67. }
  68. }
  69. for (let i = 0; i < shareKeys.length; i++) {
  70. if (!keys.includes(shareKeys[i])) {
  71. delete shareDescribes[shareKeys[i]];
  72. shareKeys.splice(i--, 1);
  73. }
  74. }
  75. }
  76. return shareDescribes;
  77. });
  78. const stopWatchs: (() => void)[] = []
  79. const mergeDesc = computed(() => {
  80. mergeFuns(stopWatchs)()
  81. stopWatchs.length = 0
  82. const mergeDesc: Record<
  83. string,
  84. PropertyDescribes[string] & { joins: { id: string }[] }
  85. > = {};
  86. for (const key in shareDescribes.value) {
  87. if (excludeKeys.includes(key)) continue;
  88. const { desc: descs, data } = shareDescribes.value[key];
  89. mergeDesc[key] = {
  90. ...descs[0],
  91. joins: data,
  92. };
  93. let value = descs[0].value;
  94. let i = 1;
  95. for (; i < descs.length; i++) {
  96. if (descs[i].value !== value) {
  97. break;
  98. }
  99. }
  100. if (i !== descs.length) {
  101. mergeDesc[key].label = mergeDesc[key].label + '(多值)'
  102. value = undefined
  103. }
  104. Object.defineProperty(mergeDesc[key], "value", {
  105. get() {
  106. return value
  107. },
  108. set(val) {
  109. for (const desc of descs) {
  110. desc.value = val;
  111. }
  112. return true;
  113. },
  114. });
  115. delete mergeDesc[key].default;
  116. }
  117. return mergeDesc;
  118. });
  119. return mergeDesc;
  120. };