RightClickMenu.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <div class="right-click-menu">
  3. <div
  4. ref="RightMenuBody"
  5. class="right-menu-body"
  6. :style="styles"
  7. v-clickoutside="close"
  8. v-if="visible"
  9. >
  10. <ul>
  11. <template v-if="!IS_NOT_TOPIC">
  12. <li @click="toEdit">
  13. <i class="el-icon-edit-outline"></i>
  14. {{ IS_CONTAINER_ELEMENT ? "编辑元素" : "编辑大题" }}
  15. </li>
  16. <li class="li-danger" @click="toDelete">
  17. <i class="el-icon-delete"></i>
  18. {{ IS_CONTAINER_ELEMENT ? "删除元素" : "删除大题" }}
  19. </li>
  20. </template>
  21. <li
  22. v-if="IS_CONTAINER_ELEMENT && (IS_EXPLAIN || IS_COMPOSITION)"
  23. @click="toCopyExplainElement"
  24. >
  25. <i class="el-icon-copy-document"></i> 复制元素
  26. </li>
  27. <li
  28. v-if="(IS_EXPLAIN || IS_COMPOSITION) && curCopyElement"
  29. @click="toPasteExplainElement"
  30. >
  31. <i class="el-icon-document-copy"></i> 粘贴元素
  32. </li>
  33. <template v-if="IS_EXPLAIN || IS_COMPOSITION">
  34. <li @click="toCopyChildren">
  35. <i class="el-icon-circle-plus-outline"></i> 新增答题区
  36. </li>
  37. <li
  38. class="li-danger"
  39. @click="toDeleteChildren"
  40. v-if="showDeleteChildBtn"
  41. >
  42. <i class="el-icon-delete"></i> 删除答题区
  43. </li>
  44. </template>
  45. <!-- <li v-if="CAN_MOVE_UP" @click="toMoveUpTopic">
  46. <i class="el-icon-upload2"></i> 上移大题
  47. </li>
  48. <li v-if="CAN_MOVE_DOWN" @click="toMoveDownTopic">
  49. <i class="el-icon-download"></i> 下移大题
  50. </li>
  51. <li v-if="!IS_CONTAINER_ELEMENT && !IS_NOT_TOPIC" @click="toInsetTopic">
  52. <i class="el-icon-add-location"></i> 插入大题
  53. </li> -->
  54. <li @click="toInsertForbidAnswer">
  55. <i class="el-icon-crop"></i> 插入禁答区
  56. </li>
  57. <li v-if="IS_NOT_TOPIC" class="li-danger" @click="toDelete">
  58. <i class="el-icon-delete"></i> 删除禁答区
  59. </li>
  60. </ul>
  61. </div>
  62. </div>
  63. </template>
  64. <script>
  65. import { mapState, mapMutations, mapActions } from "vuex";
  66. import { deepCopy } from "../plugins/utils";
  67. import { fetchSameSerialNumberChildrenPositionInfo } from "../store/card";
  68. import Clickoutside from "element-ui/src/utils/clickoutside";
  69. import { getElementModel, EDITABLE_NOT_TOPIC } from "../elementModel";
  70. export default {
  71. name: "right-click-menu",
  72. directives: { Clickoutside },
  73. data() {
  74. return {
  75. visible: false,
  76. showDeleteChildBtn: false,
  77. curCopyElement: null,
  78. styles: {
  79. position: "fixed",
  80. zIndex: 3000,
  81. },
  82. };
  83. },
  84. computed: {
  85. ...mapState("card", ["curElement", "topics", "topicSeries"]),
  86. IS_CONTAINER_ELEMENT() {
  87. return !!this.curElement.container;
  88. },
  89. IS_EXPLAIN() {
  90. return (
  91. this.curElement.type === "EXPLAIN" ||
  92. (this.curElement.container &&
  93. this.curElement.container.type === "EXPLAIN")
  94. );
  95. },
  96. IS_COMPOSITION() {
  97. return (
  98. this.curElement.type === "COMPOSITION" ||
  99. (this.curElement.container &&
  100. this.curElement.container.type === "COMPOSITION")
  101. );
  102. },
  103. CAN_MOVE_UP() {
  104. if (this.IS_CONTAINER_ELEMENT || this.IS_NOT_TOPIC) return false;
  105. const curTopicPos = this.topicSeries.findIndex(
  106. (item) => item.id === this.curElement.parent.id
  107. );
  108. return (
  109. curTopicPos &&
  110. this.topicSeries[curTopicPos - 1].sign ===
  111. this.topicSeries[curTopicPos].sign
  112. );
  113. },
  114. CAN_MOVE_DOWN() {
  115. if (this.IS_CONTAINER_ELEMENT || this.IS_NOT_TOPIC) return false;
  116. const curTopicPos = this.topicSeries.findIndex(
  117. (item) => item.id === this.curElement.parent.id
  118. );
  119. return (
  120. curTopicPos !== this.topicSeries.length - 1 &&
  121. this.topicSeries[curTopicPos + 1].sign ===
  122. this.topicSeries[curTopicPos].sign
  123. );
  124. },
  125. IS_NOT_TOPIC() {
  126. return EDITABLE_NOT_TOPIC.includes(this.curElement.type);
  127. },
  128. },
  129. mounted() {
  130. this.init();
  131. },
  132. methods: {
  133. ...mapMutations("card", ["setOpenElementEditDialog"]),
  134. ...mapActions("card", [
  135. "actElementById",
  136. "removeElement",
  137. "removeElementChild",
  138. "pasteExplainElementChild",
  139. "rebuildPages",
  140. "copyExplainChildren",
  141. "deleteExplainChildren",
  142. "topicMoveUp",
  143. "addForbidArea",
  144. "scrollToElementPage",
  145. ]),
  146. init() {
  147. // 注册自定义右键事件菜单
  148. document.oncontextmenu = function () {
  149. return false;
  150. };
  151. document.addEventListener("mouseup", this.docMouseUp);
  152. },
  153. close() {
  154. this.visible = false;
  155. },
  156. show() {
  157. this.visible = true;
  158. },
  159. docMouseUp(e) {
  160. if (e.button === 2) {
  161. this.rightClick(e);
  162. }
  163. },
  164. rightClick(e) {
  165. const id = this.getRelateElementId(e.target);
  166. if (!id) return;
  167. this.actElementById(id);
  168. let curElement = this.curElement;
  169. const TYPES = ["EXPLAIN", "COMPOSITION"];
  170. if (
  171. TYPES.includes(curElement.type) ||
  172. (curElement.container && TYPES.includes(curElement.container.type))
  173. ) {
  174. if (curElement.container) {
  175. const pos = this.topics.findIndex(
  176. (item) => item.id === curElement.container.id
  177. );
  178. curElement = this.topics[pos];
  179. }
  180. const positionInfos = fetchSameSerialNumberChildrenPositionInfo(
  181. curElement,
  182. this.topics
  183. );
  184. this.showDeleteChildBtn = positionInfos.length >= 2;
  185. }
  186. this.show();
  187. this.$nextTick(() => {
  188. const { x: clickLeft, y: clickTop } = e;
  189. const { offsetWidth: menuWidth, offsetHeight: menuHeight } =
  190. this.$refs.RightMenuBody;
  191. const { innerWidth: wWidth, innerHeight: wHeight } = window;
  192. let menuLeft = clickLeft,
  193. menuTop = clickTop;
  194. if (menuWidth + clickLeft > wWidth) {
  195. menuLeft = clickLeft - menuWidth;
  196. }
  197. if (menuHeight + clickTop > wHeight) {
  198. menuTop = clickTop - menuHeight;
  199. }
  200. this.styles = Object.assign({}, this.styles, {
  201. top: menuTop + "px",
  202. left: menuLeft + "px",
  203. });
  204. });
  205. },
  206. getRelateElementId(dom) {
  207. let parentNode = dom;
  208. while (
  209. !(
  210. (parentNode["id"] && parentNode["id"].includes("element-")) ||
  211. parentNode.className.includes("page-column-body")
  212. )
  213. ) {
  214. parentNode = parentNode.parentNode;
  215. }
  216. const elementType = parentNode.getAttribute("data-type");
  217. const unValidElement = ["TOPIC_HEAD", "CARD_HEAD"];
  218. return parentNode["id"] &&
  219. elementType &&
  220. !unValidElement.includes(elementType)
  221. ? parentNode["id"]
  222. : null;
  223. },
  224. toEdit() {
  225. this.curElement._edit = true;
  226. this.close();
  227. this.setOpenElementEditDialog(true);
  228. },
  229. toDelete() {
  230. this.close();
  231. if (this.IS_NOT_TOPIC) {
  232. this.removeSelectElement();
  233. return;
  234. }
  235. const name = this.IS_CONTAINER_ELEMENT ? "元素" : "大题";
  236. this.$confirm(`确定要删除当前${name}吗?`, "提示", {
  237. type: "warning",
  238. })
  239. .then(() => {
  240. this.removeSelectElement();
  241. })
  242. .catch(() => {});
  243. },
  244. toCopyChildren() {
  245. this.close();
  246. this.copyExplainChildren(this.curElement);
  247. this.toRebuildPages();
  248. },
  249. toDeleteChildren() {
  250. this.close();
  251. this.deleteExplainChildren(this.curElement);
  252. this.toRebuildPages();
  253. },
  254. removeSelectElement() {
  255. if (this.curElement["container"]) {
  256. this.removeElementChild(this.curElement);
  257. } else {
  258. this.removeElement(this.curElement);
  259. }
  260. this.toRebuildPages();
  261. },
  262. toCopyExplainElement() {
  263. this.close();
  264. this.curCopyElement = deepCopy(this.curElement);
  265. },
  266. toPasteExplainElement() {
  267. this.close();
  268. const id = this.curElement.container
  269. ? this.curElement.container.id
  270. : this.curElement.id;
  271. const pasteElement =
  272. this.curCopyElement.container.id === id
  273. ? Object.assign({}, this.curCopyElement, {
  274. y: this.curCopyElement.y + 20,
  275. })
  276. : this.curCopyElement;
  277. this.pasteExplainElementChild({
  278. curElement: this.curElement,
  279. pasteElement,
  280. });
  281. this.toRebuildPages();
  282. },
  283. toMoveUpTopic() {
  284. this.close();
  285. this.topicMoveUp(this.curElement.parent.id);
  286. this.toRebuildPages();
  287. },
  288. toMoveDownTopic() {
  289. this.close();
  290. const curTopicPos = this.topicSeries.findIndex(
  291. (item) => item.id === this.curElement.parent.id
  292. );
  293. this.topicMoveUp(this.topicSeries[curTopicPos + 1].id);
  294. this.toRebuildPages();
  295. },
  296. toInsetTopic() {
  297. this.close();
  298. this.$emit("inset-topic", {
  299. id: this.curElement.parent.id,
  300. type: this.curElement.type,
  301. });
  302. },
  303. toInsertForbidAnswer() {
  304. this.close();
  305. let element = getElementModel("FORBID_AREA");
  306. element.w = document.getElementById("topic-column").offsetWidth;
  307. element.sign = this.curElement.sign;
  308. this.addForbidArea({ element, beforeElementId: this.curElement.id });
  309. this.$nextTick(() => {
  310. this.rebuildPages();
  311. this.$nextTick(() => {
  312. this.scrollToElementPage(element);
  313. });
  314. });
  315. },
  316. toRebuildPages() {
  317. this.$nextTick(() => {
  318. this.rebuildPages();
  319. });
  320. },
  321. },
  322. beforeDestroy() {
  323. document.oncontextmenu = null;
  324. document.removeEventListener("mouseup", this.docMouseUp);
  325. },
  326. };
  327. </script>