View.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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, bool } from "vue-types";
  7. import { useEditor } from "../../..";
  8. import { useCompRef, useCompEditLayerRef } from "./hooks";
  9. import { DesignComp } from "@/modules/editor/objects/DesignTemp/DesignComp";
  10. import { Design_Page_Size } from "@/modules/editor/dicts/CompOptions";
  11. export const View = defineComponent({
  12. props: {
  13. compId: string().isRequired,
  14. editlayer: bool().def(true),
  15. },
  16. emits: ["dblclick", "click"],
  17. setup(props, { slots, emit }) {
  18. const { store, actions, helper, controls } = useEditor();
  19. const compRef = useCompRef(props.compId);
  20. const editorLayerRef = props.editlayer
  21. ? useCompEditLayerRef(props.compId)
  22. : ref();
  23. return () => {
  24. const comp = helper.findComp(props.compId);
  25. if (!comp) return store.isEditMode ? <div>无效组件</div> : null;
  26. const isStreamCard = helper.isStreamCard(props.compId);
  27. const isGroupComp = helper.isGroupComp(props.compId);
  28. const isStreamChild = helper.isStreamCardChild(props.compId);
  29. const obj = new CompObject(store.compMap[props.compId]);
  30. const m = obj.worldTransform.clone();
  31. m.invert();
  32. let isFocus =
  33. store.isEditMode &&
  34. store.selected.length > 1 &&
  35. store.lastSelected == props.compId;
  36. isFocus = isFocus || store.currCompId == props.compId;
  37. const style = helper.createStyle(comp.layout, comp);
  38. const page = helper.findRootComp() as DesignComp;
  39. if (isStreamCard && page.value.pageMode == "short" && store.isDisplay) {
  40. // const cards = store.streamCardIds;
  41. // const index = cards.indexOf(props.compId)
  42. // style.position = "absolute";
  43. // style.top = index * 100 + "vh";
  44. style.height = helper.designToNaturalSize(Design_Page_Size[1], {
  45. adaptiveH: true,
  46. });
  47. }
  48. if (comp.anim && comp.anim.transition) {
  49. style.transition = "all 1s ease-out";
  50. }
  51. return (
  52. <div
  53. ref={compRef}
  54. class={[
  55. viewStyle,
  56. store.isEditMode &&
  57. controls.selectCtrl._state != 0 &&
  58. editCompStyle,
  59. isGroupComp && groupCompCls,
  60. store.currStreamCardId == props.compId && CurrCompStyle,
  61. isFocus && AnchorCompStyle,
  62. ]}
  63. style={style}
  64. onClick={(e) => {
  65. if (!store.isEditMode) {
  66. e.stopPropagation();
  67. emit("click");
  68. }
  69. }}
  70. onDblclick={() => emit("dblclick")}
  71. >
  72. <div
  73. onMousemove={(e) => {
  74. if (
  75. !store.isEditMode ||
  76. !controls.dragAddCtrl.dragingCompKey ||
  77. !helper.isStreamCard(props.compId)
  78. )
  79. return;
  80. actions.pickComp(props.compId);
  81. }}
  82. >
  83. {slots.default?.()}
  84. </div>
  85. {store.isEditMode &&
  86. isStreamCard &&
  87. store.currStreamCardId == props.compId && (
  88. <Hudop compId={props.compId} />
  89. )}
  90. {store.isEditMode && props.editlayer && (
  91. <div ref={editorLayerRef} class={editAreaStyle}></div>
  92. )}
  93. </div>
  94. );
  95. };
  96. },
  97. });
  98. export const Hudop = defineComponent({
  99. props: {
  100. compId: string().isRequired,
  101. },
  102. setup(props) {
  103. const { store, actions, helper, controls } = useEditor();
  104. const opref = ref();
  105. onMounted(() => {
  106. opref.value.editable = "hudop";
  107. });
  108. return () => (
  109. <div class="hudop shadow" ref={opref}>
  110. {store.streamCardIds.length > 1 && (
  111. <IconMove
  112. class="draganchor"
  113. onMousedown={() => actions.pickComp(props.compId)}
  114. />
  115. )}
  116. {store.streamCardIds.length > 1 && (
  117. <IconDelete
  118. onClick={(e: any) => {
  119. e.stopPropagation();
  120. actions.removeStreamCard(props.compId);
  121. }}
  122. />
  123. )}
  124. <IconAdd
  125. onClick={(e: any) => {
  126. e.stopPropagation();
  127. const index = store.streamCardIds.indexOf(props.compId) + 1;
  128. actions.addCompToDesign("Container", index);
  129. }}
  130. />
  131. </div>
  132. );
  133. },
  134. });
  135. const viewStyle = css`
  136. position: relative;
  137. font-size: 0;
  138. cursor: pointer;
  139. > :first-child {
  140. width: 100%;
  141. height: 100%;
  142. }
  143. .hudop {
  144. position: absolute;
  145. top: 0px;
  146. left: -46px;
  147. background-color: white;
  148. flex-direction: column;
  149. color: black;
  150. display: flex;
  151. font-size: 12px;
  152. width: 28px;
  153. align-items: center;
  154. border-radius: 4px;
  155. z-index: 997;
  156. .inficon {
  157. padding: 8px;
  158. }
  159. }
  160. `;
  161. const editCompStyle = css`
  162. &:hover {
  163. outline: 2px dashed @inf-primary-color;
  164. }
  165. `;
  166. const AnchorCompStyle = css`
  167. outline: 2px dashed @inf-primary-color;
  168. `;
  169. const CurrCompStyle = css`
  170. position: relative;
  171. outline: 1px solid @inf-primary-color;
  172. box-shadow: 0 0 0 3000px rgba(0, 0, 0, 0.5);
  173. z-index: 998;
  174. `;
  175. const groupCompCls = css`
  176. outline: 2px solid @inf-primary-color !important;
  177. `;
  178. const editAreaStyle = css`
  179. position: absolute;
  180. top: 0;
  181. left: 0;
  182. width: 100%;
  183. height: 100%;
  184. pointer-events: none;
  185. `;
  186. const editAreaTestStyle = css`
  187. position: absolute;
  188. top: 0;
  189. left: 0;
  190. width: 100px;
  191. height: 100px;
  192. background-color: red;
  193. `;