RightClickMenu.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <div class="right-click-menu">
  3. <div
  4. v-if="visible"
  5. ref="RightMenuBody"
  6. v-clickoutside="close"
  7. class="right-menu-body"
  8. :style="styles"
  9. >
  10. <ul>
  11. <li v-if="IS_CONTAINER_ELEMENT" @click="toEdit">
  12. <i class="el-icon-edit-outline"></i>编辑元素
  13. </li>
  14. <li v-if="IS_CONTAINER_ELEMENT" class="li-danger" @click="toDelete">
  15. <i class="el-icon-delete"></i>删除元素
  16. </li>
  17. <li v-if="IS_CONTAINER_ELEMENT" @click="toCopyExplainElement">
  18. <i class="el-icon-copy-document"></i> 复制元素
  19. </li>
  20. <li
  21. v-if="IS_CONTAINER && curCopyElement"
  22. @click="toPasteExplainElement"
  23. >
  24. <i class="el-icon-document-copy"></i> 粘贴元素
  25. </li>
  26. </ul>
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. import { mapState, mapMutations, mapActions } from "vuex";
  32. import { deepCopy } from "../plugins/utils";
  33. import { fetchSameSerialNumberChildrenPositionInfo } from "../store/card";
  34. import Clickoutside from "element-ui/src/utils/clickoutside";
  35. export default {
  36. name: "RightClickMenu",
  37. directives: { Clickoutside },
  38. data() {
  39. return {
  40. visible: false,
  41. curCopyElement: null,
  42. styles: {
  43. position: "fixed",
  44. zIndex: 3000,
  45. },
  46. };
  47. },
  48. computed: {
  49. ...mapState("card", ["curElement", "topics"]),
  50. IS_CONTAINER_ELEMENT() {
  51. return !!this.curElement.container;
  52. },
  53. IS_CONTAINER() {
  54. return (
  55. this.curElement.type === "CONTAINER" ||
  56. (this.curElement.container &&
  57. this.curElement.container.type === "CONTAINER")
  58. );
  59. },
  60. },
  61. mounted() {
  62. this.init();
  63. },
  64. beforeDestroy() {
  65. document.oncontextmenu = null;
  66. document.removeEventListener("mouseup", this.docMouseUp);
  67. },
  68. methods: {
  69. ...mapMutations("card", ["setOpenElementEditDialog"]),
  70. ...mapActions("card", [
  71. "actElementById",
  72. "removeElement",
  73. "removeElementChild",
  74. "pasteExplainElementChild",
  75. "rebuildPages",
  76. "copyExplainChildren",
  77. "deleteExplainChildren",
  78. "topicMoveUp",
  79. ]),
  80. init() {
  81. // 注册自定义右键事件菜单
  82. document.oncontextmenu = function () {
  83. return false;
  84. };
  85. document.addEventListener("mouseup", this.docMouseUp);
  86. },
  87. close() {
  88. this.visible = false;
  89. },
  90. show() {
  91. this.visible = true;
  92. },
  93. docMouseUp(e) {
  94. if (e.button === 2) {
  95. this.rightClick(e);
  96. }
  97. },
  98. rightClick(e) {
  99. const id = this.getRelateElementId(e.target);
  100. if (!id) return;
  101. this.actElementById(id);
  102. let curElement = this.curElement;
  103. const TYPES = ["EXPLAIN", "COMPOSITION"];
  104. if (
  105. TYPES.includes(curElement.type) ||
  106. (curElement.container && TYPES.includes(curElement.container.type))
  107. ) {
  108. if (curElement.container) {
  109. const pos = this.topics.findIndex(
  110. (item) => item.id === curElement.container.id
  111. );
  112. curElement = this.topics[pos];
  113. }
  114. const positionInfos = fetchSameSerialNumberChildrenPositionInfo(
  115. curElement,
  116. this.topics
  117. );
  118. this.showDeleteChildBtn = positionInfos.length >= 2;
  119. }
  120. // todo:
  121. this.show();
  122. this.$nextTick(() => {
  123. const { x: clickLeft, y: clickTop } = e;
  124. const { offsetWidth: menuWidth, offsetHeight: menuHeight } =
  125. this.$refs.RightMenuBody;
  126. const { innerWidth: wWidth, innerHeight: wHeight } = window;
  127. let menuLeft = clickLeft,
  128. menuTop = clickTop;
  129. if (menuWidth + clickLeft > wWidth) {
  130. menuLeft = clickLeft - menuWidth;
  131. }
  132. if (menuHeight + clickTop > wHeight) {
  133. menuTop = clickTop - menuHeight;
  134. }
  135. this.styles = Object.assign({}, this.styles, {
  136. top: menuTop + "px",
  137. left: menuLeft + "px",
  138. });
  139. });
  140. },
  141. getRelateElementId(dom) {
  142. let parentNode = dom;
  143. while (
  144. !(
  145. (parentNode["id"] && parentNode["id"].includes("element-")) ||
  146. parentNode.className.includes("page-column-body") ||
  147. parentNode.className.includes("card-design")
  148. )
  149. ) {
  150. parentNode = parentNode.parentNode;
  151. }
  152. const elementType = parentNode.getAttribute("data-type");
  153. const unValidElement = ["TOPIC_HEAD", "CARD_HEAD"];
  154. return parentNode["id"] &&
  155. elementType &&
  156. !unValidElement.includes(elementType)
  157. ? parentNode["id"]
  158. : null;
  159. },
  160. toEdit() {
  161. this.curElement._edit = true;
  162. this.close();
  163. this.setOpenElementEditDialog(true);
  164. },
  165. toDelete() {
  166. this.close();
  167. this.$confirm("确定要删除当前元素吗?", "提示", {
  168. type: "warning",
  169. })
  170. .then(() => {
  171. this.removeSelectElement();
  172. })
  173. .catch(() => {});
  174. },
  175. toCopyChildren() {
  176. this.close();
  177. this.copyExplainChildren(this.curElement);
  178. this.toRebuildPages();
  179. },
  180. toDeleteChildren() {
  181. this.close();
  182. this.deleteExplainChildren(this.curElement);
  183. this.toRebuildPages();
  184. },
  185. removeSelectElement() {
  186. if (!this.curElement["container"]) return;
  187. this.removeElementChild(this.curElement);
  188. this.toRebuildPages();
  189. },
  190. toCopyExplainElement() {
  191. this.close();
  192. this.curCopyElement = deepCopy(this.curElement);
  193. },
  194. toPasteExplainElement() {
  195. this.close();
  196. const id = this.curElement.container
  197. ? this.curElement.container.id
  198. : this.curElement.id;
  199. const pasteElement =
  200. this.curCopyElement.container.id === id
  201. ? Object.assign({}, this.curCopyElement, {
  202. y: this.curCopyElement.y + 20,
  203. })
  204. : this.curCopyElement;
  205. this.pasteExplainElementChild({
  206. curElement: this.curElement,
  207. pasteElement,
  208. });
  209. this.toRebuildPages();
  210. },
  211. toMoveUpTopic() {
  212. this.close();
  213. this.topicMoveUp(this.curElement.parent.id);
  214. this.toRebuildPages();
  215. },
  216. toMoveDownTopic() {
  217. this.close();
  218. const curTopicPos = this.topicSeries.findIndex(
  219. (item) => item.id === this.curElement.parent.id
  220. );
  221. this.topicMoveUp(this.topicSeries[curTopicPos + 1].id);
  222. this.toRebuildPages();
  223. },
  224. toInsetTopic() {
  225. this.close();
  226. this.$emit("inset-topic", {
  227. id: this.curElement.parent.id,
  228. type: this.curElement.type,
  229. });
  230. },
  231. toRebuildPages() {
  232. this.$nextTick(() => {
  233. this.rebuildPages();
  234. });
  235. },
  236. },
  237. };
  238. </script>