index.tsx 5.1 KB

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