index.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { IconRotate } from "@/assets/icons";
  2. import { CompToolbars } from "@/modules/editor/objects/Toolbars";
  3. import { css } from "@linaria/core";
  4. import { defineComponent, onMounted, onUnmounted } from "vue";
  5. import { useEditor } from "../../../..";
  6. export const Transfer = defineComponent({
  7. setup() {
  8. const editor = useEditor();
  9. const { controls, helper } = editor;
  10. const { transferCtrl } = controls;
  11. const { transferStyle } = transferCtrl;
  12. onMounted(() => {
  13. setTimeout(() => {
  14. const pageEl = helper.findRootComp()?.$el;
  15. if (pageEl) {
  16. transferCtrl.init(pageEl.firstChild as HTMLElement);
  17. }
  18. });
  19. });
  20. onUnmounted(() => {
  21. transferCtrl.destroy();
  22. // console.log("transferCtrl.destroy========================");
  23. });
  24. return () => {
  25. const comp = transferCtrl.currComp;
  26. const toolbarOpts =
  27. CompToolbars[transferCtrl.currComp?.compKey] || CompToolbars.default;
  28. // const showTransfer =
  29. // store.isEditComp || store.pageCompIds.includes(comp.id);
  30. const showTransfer = true;
  31. return (
  32. transferStyle.width && (
  33. <div
  34. class="absolute transfer"
  35. style={{
  36. top: transferStyle.top,
  37. left: transferStyle.left,
  38. width: transferStyle.width,
  39. transform: `translateX(${transferStyle.transform.translateX}) translateY(${transferStyle.transform.translateY})`,
  40. }}
  41. >
  42. {showTransfer && (
  43. <div class={toolbarStyle}>
  44. {toolbarOpts.map((item) => {
  45. return item.getVisible.call(editor, comp) ? (
  46. <item.component
  47. class="p-4px"
  48. value={item.getValue?.(comp)}
  49. onClick={() => item.onClick.call(editor, comp)}
  50. />
  51. ) : null;
  52. })}
  53. </div>
  54. )}
  55. <div
  56. style={{
  57. transform: `rotate(${transferStyle.transform.rotate})`,
  58. transformOrigin: `center ${
  59. parseInt(transferStyle.height.split("px")[0]) / 2
  60. }px`,
  61. }}
  62. >
  63. <div
  64. class={borderStyle}
  65. style={{
  66. width: transferStyle.width,
  67. height: transferStyle.height,
  68. }}
  69. />
  70. {showTransfer && (
  71. <>
  72. <div
  73. class={resizeStyle}
  74. style={{ marginBottom: "-" + transferStyle.height }}
  75. onMousedown={(e) =>
  76. transferCtrl.mousedown(
  77. e,
  78. comp.compKey === "Image" ? "resizeXY" : "scale"
  79. )
  80. }
  81. />
  82. <div
  83. class={transformBtnsStyle}
  84. style={{ marginBottom: "-" + transferStyle.height }}
  85. >
  86. <div
  87. class={transBtnStyle}
  88. onMousedown={(e) => transferCtrl.mousedown(e, "rotate")}
  89. >
  90. <IconRotate />
  91. </div>
  92. </div>
  93. <div
  94. class={resizeHeightBtnCls}
  95. style={{ top: transferStyle.height }}
  96. onMousedown={(e) => transferCtrl.mousedown(e, "resizeY")}
  97. />
  98. <div
  99. class={resizeWidthBtnCls}
  100. style={{ top: parseInt(transferStyle.height) / 2 + "px" }}
  101. onMousedown={(e) => transferCtrl.mousedown(e, "resizeX")}
  102. />
  103. </>
  104. )}
  105. </div>
  106. </div>
  107. )
  108. );
  109. };
  110. },
  111. });
  112. const borderStyle = css`
  113. position: absolute;
  114. top: 0;
  115. left: 0;
  116. width: 100%;
  117. height: 100%;
  118. outline: 2px solid @inf-primary-color;
  119. pointer-events: none;
  120. z-index: 999;
  121. `;
  122. const resizeStyle = css`
  123. position: absolute;
  124. bottom: 0;
  125. right: 0;
  126. width: 16px;
  127. height: 16px;
  128. border-radius: 50%;
  129. background-color: #fff;
  130. z-index: 999;
  131. transform: translate(50%, 50%);
  132. box-shadow: 0 0 2px 2px rgba(0, 0, 0, 0.2);
  133. cursor: nwse-resize;
  134. &:hover {
  135. border-color: @inf-primary-color;
  136. }
  137. `;
  138. const transformBtnsStyle = css`
  139. @apply space-x-10px whitespace-nowrap;
  140. position: absolute;
  141. bottom: 0;
  142. left: 50%;
  143. font-size: 16px;
  144. z-index: 999;
  145. transform: translate(-50%, 50px);
  146. `;
  147. const transBtnStyle = css`
  148. display: inline-block;
  149. width: 36px;
  150. height: 36px;
  151. border-radius: 50%;
  152. background-color: #fff;
  153. text-align: center;
  154. line-height: 36px;
  155. font-size: 20px;
  156. color: #333;
  157. @apply shadow cursor-move;
  158. &:hover {
  159. color: #fff;
  160. background-color: @inf-primary-color;
  161. }
  162. `;
  163. const toolbarStyle = css`
  164. @apply bg-white shadow rounded space-x-4px p-4px whitespace-nowrap;
  165. position: absolute;
  166. top: 0;
  167. left: 50%;
  168. transform: translate(-50%, -60px);
  169. z-index: 999;
  170. `;
  171. const resizeHeightBtnCls = css`
  172. position: absolute;
  173. width: 30px;
  174. height: 8px;
  175. border-radius: 4px;
  176. left: 50%;
  177. transform: translate(-50%, -4px);
  178. cursor: ns-resize;
  179. background: rgba(255, 255, 255, 0.3);
  180. &:hover {
  181. background: @inf-primary-color;
  182. }
  183. @apply shadow;
  184. z-index: 999;
  185. `;
  186. const resizeWidthBtnCls = css`
  187. position: absolute;
  188. width: 8px;
  189. height: 30px;
  190. border-radius: 4px;
  191. right: 0;
  192. transform: translate(4px, -50%);
  193. cursor: ew-resize;
  194. background: rgba(255, 255, 255, 0.3);
  195. &:hover {
  196. background: @inf-primary-color;
  197. }
  198. @apply shadow;
  199. z-index: 999;
  200. `;