use-group.ts 3.3 KB

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