ContextMenu.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <t-menu class="context-menu" @change="onMenu" expandType="popup">
  3. <template v-if="props.type === 'anchor'">
  4. <t-menu-item value="addAnchorHand">
  5. <div class="flex">{{$t('添加手柄')}} <span class="flex-grow"></span> H</div>
  6. </t-menu-item>
  7. <t-menu-item value="removeAnchorHand">
  8. <div class="flex">{{$t('删除手柄')}} <span class="flex-grow"></span> D</div>
  9. </t-menu-item>
  10. <t-menu-item value="toggleAnchorHand">
  11. <div class="flex">{{$t('切换手柄')}} <span class="flex-grow"></span> Shift</div>
  12. </t-menu-item>
  13. </template>
  14. <template v-if="props.type === 'pen'">
  15. <t-menu-item v-if="!selections.mode&&hasC()" value="updateC"> {{$t('一键更新我的组件')}} </t-menu-item>
  16. <t-menu-item v-if="isAppend()" value="appendChild"> {{$t('追加状态')}} </t-menu-item>
  17. <t-menu-item v-if="is3D()" value="goto3d"> {{$t('进入')}}3D{{$t('可视化编辑')}} </t-menu-item>
  18. <t-menu-item v-if="is2D()" value="goto2d"> {{$t('进入')}}2D{{$t('可视化编辑')}} </t-menu-item>
  19. <t-menu-item v-if="isV()" value="gotov"> {{$t('进入大屏可视化编辑')}} </t-menu-item>
  20. <t-menu-item v-if="isIframe()" value="replaceScene"> {{$t('替换方案')}} </t-menu-item>
  21. <!-- <t-menu-item v-if="isTableNoFocus()" value="focus">
  22. <div class="flex">进入编辑模式<span class="flex-grow"></span>Enter</div>
  23. </t-menu-item> -->
  24. <!-- <t-menu-item v-if="isTableFocus()" value="unFocus">
  25. <div class="flex">退出编辑模式 <span class="flex-grow"></span>Esc</div>
  26. </t-menu-item> -->
  27. <t-menu-item :disabled="!selections.mode" value="top"> {{$t('置顶')}} </t-menu-item>
  28. <t-menu-item :disabled="!selections.mode" value="bottom">
  29. {{$t('置底')}}
  30. </t-menu-item>
  31. <t-menu-item :disabled="!selections.mode" value="up">
  32. {{$t('上一个图层')}}
  33. </t-menu-item>
  34. <t-menu-item :disabled="!selections.mode" value="down">
  35. {{$t('下一个图层')}}
  36. </t-menu-item>
  37. <template v-if="selections.mode === SelectionMode.Pens">
  38. <t-submenu value="1-1-10" title="画布层">
  39. <t-menu-item :disabled="!allImg()" value="4">
  40. 上层图片层
  41. </t-menu-item>
  42. <t-menu-item :disabled="hasDom()" value="3">
  43. 主画布层
  44. </t-menu-item>
  45. <t-menu-item :disabled="!allImg()" value="2">
  46. 下层图片层
  47. </t-menu-item>
  48. <t-menu-item :disabled="hasDom()" value="1">
  49. 模板层
  50. </t-menu-item>
  51. </t-submenu>
  52. </template>
  53. <template v-if="selections.mode === SelectionMode.Pen">
  54. <t-submenu value="1-1-10" title="画布层">
  55. <t-menu-item :disabled="!isImg()" value="4">
  56. 上层图片层
  57. </t-menu-item>
  58. <t-menu-item value="3">
  59. 主画布层
  60. </t-menu-item>
  61. <t-menu-item :disabled="!isImg()" value="2">
  62. 下层图片层
  63. </t-menu-item>
  64. <t-menu-item value="1">
  65. 模板层
  66. </t-menu-item>
  67. </t-submenu>
  68. </template>
  69. <t-divider />
  70. <template v-if="hasPens">
  71. <t-menu-item value="group"> {{$t('组合')}} </t-menu-item>
  72. <t-menu-item value="states"> {{$t('组合为状态')}} </t-menu-item>
  73. </template>
  74. <t-menu-item v-if="hasChildren" value="unGroup">
  75. <t-popconfirm @cancel="onMenu('xxxx')" @confirm="onMenu('unGroup')" :content="$t('确认取消组合?(将丢失组合图元的数据)')">
  76. <div @click.stop>
  77. {{$t('取消组合')}}
  78. </div>
  79. </t-popconfirm>
  80. </t-menu-item>
  81. <t-menu-item :disabled="!selections.mode" value="lock">
  82. {{ locked ? $t('解锁') : $t('锁定') }}
  83. </t-menu-item>
  84. <t-divider />
  85. <t-menu-item :disabled="!selections.mode" value="del"> {{$t( '删除' )}} </t-menu-item>
  86. <template v-if="isNameLine">
  87. <t-menu-item value="changeType">
  88. {{ isLine ? $t('变成节点') : $t('变成连线') }}
  89. </t-menu-item>
  90. </template>
  91. <t-divider />
  92. <t-menu-item :disabled="cantUndo" value="undo">
  93. <div class="flex">{{$t('撤销')}}<span class="flex-grow"></span>Ctrl + Z</div>
  94. </t-menu-item>
  95. <t-menu-item :disabled="cantRedo" value="redo">
  96. <div class="flex">{{$t('重做')}}<span class="flex-grow"></span>Shift + Z</div>
  97. </t-menu-item>
  98. <t-divider />
  99. <t-menu-item :disabled="!selections.mode" value="cut">
  100. <div class="flex">{{$t('剪切')}}<span class="flex-grow"></span>Ctrl + X</div>
  101. </t-menu-item>
  102. <t-menu-item :disabled="!selections.mode" value="copy">
  103. <div class="flex">{{$t('复制')}}<span class="flex-grow"></span>Ctrl + C</div>
  104. </t-menu-item>
  105. <t-menu-item value="paste">
  106. <div class="flex">{{$t('粘贴')}}<span class="flex-grow"></span>Ctrl + V</div>
  107. </t-menu-item>
  108. </template>
  109. </t-menu>
  110. </template>
  111. <script setup lang="ts">
  112. import { onMounted, ref, getCurrentInstance } from 'vue';
  113. import { LockState, Pen, PenType, isDomShapes } from '@meta2d/core';
  114. import { useSelection, SelectionMode } from '@/services/selections';
  115. import { updateC } from '@/services/updateC';
  116. import { rootDomain } from '@/services/defaults';
  117. const { proxy } = getCurrentInstance();
  118. const $t = proxy.$t
  119. const props = defineProps({
  120. type: String,
  121. });
  122. const emit = defineEmits(['changeVisible','replaceScene']);
  123. const { selections } = useSelection();
  124. const hasChildren = ref(false);
  125. const locked = ref(false);
  126. const isNameLine = ref(false);
  127. const isLine = ref(false);
  128. const cantUndo = ref(false);
  129. const cantRedo = ref(false);
  130. onMounted(() => {
  131. if (selections.mode === SelectionMode.Pen) {
  132. hasChildren.value = meta2d?.store.active[0]?.children?.length > 0;
  133. isNameLine.value = meta2d?.store.active[0]?.name === 'line';
  134. isLine.value = meta2d?.store.active[0]?.type === PenType.Line;
  135. }
  136. if (selections.mode) {
  137. locked.value = meta2d?.store.active?.some((pen: Pen) => pen.locked);
  138. }
  139. cantUndo.value =
  140. !!meta2d?.store.data.locked ||
  141. meta2d?.store.histories.length == 0 ||
  142. meta2d?.store.historyIndex == null ||
  143. meta2d?.store.historyIndex < 0;
  144. cantRedo.value =
  145. !!meta2d?.store.data.locked ||
  146. meta2d?.store.histories.length == 0 ||
  147. meta2d?.store.historyIndex == null ||
  148. meta2d?.store.historyIndex > meta2d?.store.histories.length - 2;
  149. });
  150. const onMenu = (val: string) => {
  151. let id,url;
  152. if(!isNaN(parseInt(val))){
  153. const pens = meta2d.store.active;
  154. if (Array.isArray(pens)) {
  155. for (const pen of pens) {
  156. meta2d.setValue(
  157. {
  158. id: pen.id,
  159. canvasLayer: parseInt(val),
  160. },
  161. {
  162. history: false,
  163. render:false,
  164. doEvent:false
  165. }
  166. );
  167. }
  168. }
  169. meta2d.render();
  170. meta2d.emit('layer');
  171. return;
  172. }
  173. switch (val) {
  174. case 'addAnchorHand':
  175. meta2d.addAnchorHand();
  176. break;
  177. case 'removeAnchorHand':
  178. meta2d.removeAnchorHand();
  179. break;
  180. case 'toggleAnchorHand':
  181. meta2d.toggleAnchorHand();
  182. break;
  183. case 'top':
  184. case 'bottom':
  185. case 'up':
  186. case 'down':
  187. case 'undo':
  188. case 'redo':
  189. case 'cut':
  190. case 'copy':
  191. case 'paste':
  192. (meta2d as any)[val]();
  193. break;
  194. case 'group':
  195. meta2d.combine(meta2d.store.active);
  196. break;
  197. case 'states':
  198. meta2d.combine(meta2d.store.active, 0);
  199. break;
  200. case 'unGroup':
  201. meta2d.uncombine();
  202. break;
  203. case 'lock':
  204. {
  205. const pens = meta2d.store.active;
  206. if (Array.isArray(pens)) {
  207. for (const pen of pens) {
  208. pen.locked = locked.value ? LockState.None : LockState.DisableMove;
  209. }
  210. }
  211. meta2d.render();
  212. }
  213. break;
  214. case 'del':
  215. meta2d.delete();
  216. break;
  217. case 'changeType':
  218. meta2d.setValue(
  219. {
  220. id: meta2d.store.active[0].id,
  221. type: isLine.value ? 0 : 1,
  222. },
  223. {
  224. history: true,
  225. }
  226. );
  227. break;
  228. case 'updateC':
  229. updateC();
  230. break;
  231. case 'appendChild':
  232. meta2d.appendChild();
  233. let pen = meta2d?.store.active.find((pen: any) => pen.name === "combine"&& pen.showChild !== undefined)
  234. meta2d.active([pen],true);
  235. meta2d.render();
  236. break;
  237. case 'focus':
  238. meta2d.store.active[0].calculative.focus = true;
  239. break;
  240. case 'unFocus':
  241. meta2d.store.active[0].calculative.focus = false;
  242. break;
  243. case 'goto3d':
  244. id = meta2d?.store.active[0].iframe.split("id=")[1];
  245. //安装包
  246. url = location.origin+ `/3d/?id=`;
  247. if (window.url3D) {
  248. url = window.url3D;
  249. }
  250. window.open(url + id, '_blank');
  251. break;
  252. case 'goto2d':
  253. id = meta2d?.store.active[0].iframe.split("id=")[1];
  254. url = location.origin+ `/2d/?id=`;
  255. window.open(url + id, '_blank');
  256. break;
  257. case 'gotov':
  258. id = meta2d?.store.active[0].iframe.split("id=")[1];
  259. url = location.origin+ `/v/?id=`;
  260. window.open(url + id, '_blank');
  261. break;
  262. case 'replaceScene':
  263. emit('replaceScene', false);
  264. break;
  265. }
  266. emit('changeVisible', false);
  267. };
  268. const hasPens = () => {
  269. return meta2d?.store.active.length > 1;
  270. };
  271. const hasC =() => {
  272. return meta2d?.store.data.pens?.some((pen: Pen) => pen.name === "combine");
  273. }
  274. const isAppend =() => {
  275. return meta2d?.store.active.length > 1 && meta2d?.store.active.some((pen: any) => pen.name === "combine"&& pen.showChild !== undefined);
  276. }
  277. const isTableNoFocus =() => {
  278. return meta2d?.store.active.length === 1 && meta2d?.store.active[0].name === "tablePlus" && !meta2d?.store.active[0].calculative.focus;
  279. }
  280. const isTableFocus =() => {
  281. return meta2d?.store.active.length === 1 && meta2d?.store.active[0].name === "tablePlus" && meta2d?.store.active[0].calculative.focus;
  282. }
  283. const is3D = ()=>{
  284. return meta2d?.store.active.length === 1 && meta2d?.store.active[0].name === "iframe" && (meta2d?.store.active[0].iframe.indexOf("3d.") > -1||meta2d?.store.active[0].iframe.indexOf("/3d") > -1);
  285. }
  286. const is2D = ()=>{
  287. return meta2d?.store.active.length === 1 && meta2d?.store.active[0].name === "iframe" && (meta2d?.store.active[0].iframe.indexOf("2d.") > -1||meta2d?.store.active[0].iframe.indexOf("/2d") > -1);
  288. }
  289. const isV = ()=>{
  290. return meta2d?.store.active.length === 1 && meta2d?.store.active[0].name === "iframe" && (meta2d?.store.active[0].iframe.indexOf("v.") > -1||meta2d?.store.active[0].iframe.indexOf("/v") > -1);
  291. }
  292. const isIframe = ()=>{
  293. return meta2d?.store.active.length === 1 && meta2d?.store.active[0].name === "iframe"
  294. }
  295. const isImg = ()=>{
  296. return meta2d?.store.active[0].name==='image'
  297. }
  298. const hasDom = () => {
  299. return selections.pens.some((item: Pen) => {
  300. return isDomShapes.includes(item.name) ||
  301. item.name.endsWith('Dom') ||
  302. meta2d.store.options.domShapes.includes(item.name);
  303. });
  304. };
  305. const allImg = () => {
  306. return selections.pens.every((item: Pen) => {
  307. return item.name === 'image';
  308. });
  309. };
  310. </script>
  311. <style lang="postcss" scoped>
  312. .context-menu {
  313. height: auto !important;
  314. }
  315. :deep(.t-menu) {
  316. .t-menu__item {
  317. &.t-is-opened {
  318. background-color: var(--color-background-popup-hover);
  319. transition: none !important;
  320. }
  321. }
  322. .t-fake-arrow {
  323. transform: rotate(-90deg) !important;
  324. }
  325. .t-fake-arrow--active {
  326. transform: rotate(90deg) !important;
  327. }
  328. }
  329. </style>