View.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { IconAdd, IconMove } from "@/assets/icons";
  2. import { CompObject } from "@/modules/editor/controllers/SelectCtrl/compObj";
  3. import { css } from "@linaria/core";
  4. import { IconDelete } from "@queenjs/icons";
  5. import { defineComponent, onMounted, ref } from "vue";
  6. import { string } from "vue-types";
  7. import { useEditor } from "../../..";
  8. import { useCompRef } from "./hooks";
  9. export const View = defineComponent({
  10. props: {
  11. compId: string().isRequired,
  12. },
  13. emits: ["dblclick"],
  14. setup(props, { slots, emit }) {
  15. const { store, actions, helper, controls } = useEditor();
  16. const compRef = useCompRef(props.compId);
  17. return () => {
  18. const comp = helper.findComp(props.compId);
  19. if (!comp) return store.isEditMode ? <div>无效组件</div> : null;
  20. const isStreamCard = helper.isStreamCard(props.compId);
  21. const isGroupComp = helper.isGroupComp(props.compId);
  22. const obj = new CompObject(store.compMap[props.compId]);
  23. const m = obj.worldTransform.clone();
  24. m.invert();
  25. return (
  26. <div
  27. ref={compRef}
  28. class={[
  29. viewStyle,
  30. store.isEditMode && editCompStyle,
  31. isGroupComp && groupCompCls,
  32. store.currStreamCardId == props.compId && CurrCompStyle,
  33. ]}
  34. style={helper.createStyle(comp.layout)}
  35. onClick={(e) => {
  36. e.stopPropagation();
  37. if (store.isEditMode) {
  38. actions.pickComp(props.compId);
  39. }
  40. }}
  41. onDblclick={() => emit("dblclick")}
  42. >
  43. <div
  44. // onMousedown={(e) => {
  45. // if (helper.isGroupCompChild(props.compId)) return;
  46. // e.stopPropagation();
  47. // if (store.isEditMode) {
  48. // actions.pickComp(props.compId);
  49. // }
  50. // }}
  51. onMousemove={(e) => {
  52. if (
  53. !store.isEditMode ||
  54. !controls.dragAddCtrl.dragingCompKey ||
  55. !helper.isStreamCard(props.compId)
  56. )
  57. return;
  58. actions.pickComp(props.compId);
  59. }}
  60. >
  61. {slots.default?.()}
  62. </div>
  63. {store.isEditMode && isStreamCard && <Hudop compId={props.compId} />}
  64. </div>
  65. );
  66. };
  67. },
  68. });
  69. export const Hudop = defineComponent({
  70. props: {
  71. compId: string().isRequired,
  72. },
  73. setup(props) {
  74. const { store, actions, helper, controls } = useEditor();
  75. const opref = ref();
  76. onMounted(() => {
  77. opref.value.editable = "hudop";
  78. });
  79. return () => (
  80. <div class="hudop" ref={opref}>
  81. {store.streamCardIds.length > 1 && (
  82. <IconMove
  83. class="draganchor"
  84. onMousedown={() => actions.pickComp(props.compId)}
  85. />
  86. )}
  87. {store.streamCardIds.length > 1 && (
  88. <IconDelete
  89. onClick={(e: any) => {
  90. e.stopPropagation();
  91. actions.removeStreamCard(props.compId);
  92. }}
  93. />
  94. )}
  95. <IconAdd
  96. onClick={(e: any) => {
  97. e.stopPropagation();
  98. const index = store.streamCardIds.indexOf(props.compId) + 1;
  99. actions.addCompToDesign("Container", index);
  100. }}
  101. />
  102. </div>
  103. );
  104. },
  105. });
  106. const viewStyle = css`
  107. position: relative;
  108. font-size: 0;
  109. cursor: pointer;
  110. > :first-child {
  111. width: 100%;
  112. height: 100%;
  113. }
  114. .hudop {
  115. position: absolute;
  116. top: 0px;
  117. left: -46px;
  118. background-color: white;
  119. flex-direction: column;
  120. color: black;
  121. display: flex;
  122. font-size: 12px;
  123. width: 28px;
  124. align-items: center;
  125. border-radius: 4px;
  126. .inficon {
  127. padding: 8px;
  128. }
  129. z-index: 999;
  130. }
  131. `;
  132. const editCompStyle = css`
  133. &:hover {
  134. outline: 2px dashed @inf-primary-color;
  135. }
  136. `;
  137. const CurrCompStyle = css`
  138. position: relative;
  139. outline: 1px solid @inf-primary-color;
  140. box-shadow: 0 0 0 3000px rgba(0, 0, 0, 0.5);
  141. z-index: 998;
  142. `;
  143. const groupCompCls = css`
  144. outline: 2px solid @inf-primary-color !important;
  145. `;