b31a0e876135e29bea2f53e3f70ecffc77841652b05f8bf9888d858186d34317f0c1584d944ac6a54618fda975f78b1011bb9fc593322172179da374398255 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { ComponentStructure } from "./componentStructure";
  2. import { isHtmlTag, isTransition } from "../util/tags";
  3. import { resolveComponent, TransitionGroup } from "vue";
  4. function getSlot(slots, key) {
  5. const slotValue = slots[key];
  6. return slotValue ? slotValue() : [];
  7. }
  8. function computeNodes({ $slots, realList, getKey }) {
  9. const normalizedList = realList || [];
  10. const [header, footer] = ["header", "footer"].map(name =>
  11. getSlot($slots, name)
  12. );
  13. const { item } = $slots;
  14. if (!item) {
  15. throw new Error("draggable element must have an item slot");
  16. }
  17. const defaultNodes = normalizedList.flatMap((element, index) =>
  18. item({ element, index }).map(node => {
  19. node.key = getKey(element);
  20. node.props = { ...(node.props || {}), "data-draggable": true };
  21. return node;
  22. })
  23. );
  24. if (defaultNodes.length !== normalizedList.length) {
  25. throw new Error("Item slot must have only one child");
  26. }
  27. return {
  28. header,
  29. footer,
  30. default: defaultNodes
  31. };
  32. }
  33. function getRootInformation(tag) {
  34. const transition = isTransition(tag);
  35. const externalComponent = !isHtmlTag(tag) && !transition;
  36. return {
  37. transition,
  38. externalComponent,
  39. tag: externalComponent
  40. ? resolveComponent(tag)
  41. : transition
  42. ? TransitionGroup
  43. : tag
  44. };
  45. }
  46. function computeComponentStructure({ $slots, tag, realList, getKey }) {
  47. const nodes = computeNodes({ $slots, realList, getKey });
  48. const root = getRootInformation(tag);
  49. return new ComponentStructure({ nodes, root, realList });
  50. }
  51. export { computeComponentStructure };