vue3?實現(xiàn)右鍵菜單編輯復制粘貼功能
更新時間:2024年10月23日 11:05:51 作者:橫沖直撞de
在瀏覽器中,Vue3編輯器自帶默認右鍵菜單,然而,在Electron桌面應(yīng)用中,這一功能需自行編寫代碼實現(xiàn),本文詳細介紹了如何在Vue3中手動實現(xiàn)右鍵菜單的編輯、復制、粘貼功能,并提供了代碼示例,更多細節(jié)和相關(guān)教程可參考腳本之家的其他文章
vue3編輯器在瀏覽器有默認的右鍵菜單,但是在客戶端electron中要實現(xiàn)這個右鍵菜單的功能需要手寫。
代碼如下
<template> <!-- 右鍵菜單 --> <div> <div v-if="menuVisible" @mousedown.prevent class="custom-menu" :style="{ top: menuY + 'px', left: menuX + 'px' }" > <ul> <li @click="copyText">復制</li> <li @click="cutText">剪切</li> <li @click="pasteText">粘貼</li> </ul> </div> <div id="vditor" @contextmenu.prevent="showMenu"></div> </div> </template>
<script setup> // 安裝插件vditor import Vditor from "vditor"; import "vditor/dist/index.css"; import { ref, onMounted } from "vue"; onMounted(() => { document.addEventListener("click", hideMenu); // 初始化全局點擊事件,隱藏右鍵菜單 initVditor(); }); const vditor = ref(null); // 使用 ref 來存儲 vditor 實例 const initVditor = () => { if (vditor.value) { vditor.value.setValue("你好,這是測試"); } else { console.log("初始化macket", import.meta.env.MODE); vditor.value = new Vditor("vditor", { width: "100%", cdn: import.meta.env.MODE === "development" ? "./public" : "./", placeholder: "請輸入內(nèi)容...", toolbar: [], preview: { markdown: { sanitize: false, }, }, after() { vditor.value.setValue("你好,這是測試"); // 一次性設(shè)置所有內(nèi)容,并移除開頭的多余空格 }, input(value) { const markdownContent = vditor.value.getValue(); }, }); } }; // 用于控制右鍵菜單的顯示和位置 const menuVisible = ref(false); const menuX = ref(0); const menuY = ref(0); const copiedText = ref(""); // 用于保存復制或剪切的文本 let lastFocusedElement = null; // 用于保存最后聚焦的元素 const fuzhitext = ref(""); // 顯示右鍵菜單 function showMenu(event) { copiedText.value = vditor.value.getSelection(); menuVisible.value = true; menuX.value = event.clientX; menuY.value = event.clientY; document.addEventListener("click", hideMenu); // 點擊其他地方隱藏菜單 // 保存當前聚焦的元素 lastFocusedElement = document.activeElement; console.log(lastFocusedElement); } // 隱藏右鍵菜單 function hideMenu() { menuVisible.value = false; // document.removeEventListener('click', hideMenu); } // 復制選中的文本 function copyText() { if (copiedText.value) { console.log("復制的內(nèi)容:", copiedText.value); fuzhitext.value = copiedText.value; } else { console.log("沒有選中的文本"); } // 延遲聚焦以保持選中狀態(tài) setTimeout(() => { vditor.value.focus(); }, 0); vditor.value.updateValue(copiedText.value); hideMenu(); } // 粘貼文本 function pasteText() { if (fuzhitext.value) { vditor.value.deleteValue(); // 這里你可以使用插入內(nèi)容的API來粘貼文本 vditor.value.insertValue(fuzhitext.value); } setTimeout(() => { lastFocusedElement.focus(); }, 0); hideMenu(); } // 剪切 function cutText() { if (copiedText.value) { console.log("復制的內(nèi)容:", copiedText.value); fuzhitext.value = copiedText.value; } else { console.log("沒有選中的文本"); } vditor.value.deleteValue(); setTimeout(() => { lastFocusedElement.focus(); }, 0); hideMenu(); } </script> <style scoped> .custom-menu { position: absolute; background-color: white; border: 1px solid #ccc; border-radius: 10px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2); z-index: 1000; animation: slideDown 0.2s ease-in-out; /* 添加下拉動畫 */ } .custom-menu ul { list-style-type: none; padding: 0; /* 移除內(nèi)邊距 */ margin: 0; /* 移除外邊距 */ min-width: 95px; } .custom-menu ul li { padding: 8px 30px; /* 增加上下內(nèi)邊距 */ cursor: pointer; transition: background-color 0.2s; /* 添加背景色過渡效果 */ } .custom-menu ul li:hover { background-color: #e9f5ff; /* 懸停時背景色變化 */ border-radius: 10px; } /* 下拉動畫 */ @keyframes slideDown { from { opacity: 0; transform: translateY(-10px); /* 初始位置稍微上移 */ } to { opacity: 1; transform: translateY(0); } } </style>
到此這篇關(guān)于vue3 實現(xiàn) 右鍵菜單編輯復制粘貼的文章就介紹到這了,更多相關(guān)vue3 右鍵菜單編輯復制粘貼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue監(jiān)聽頁面滾動到某個高度觸發(fā)事件流程
這篇文章主要介紹了vue監(jiān)聽頁面滾動到某個高度觸發(fā)事件流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04Vue3+Three.js實現(xiàn)為模型添加點擊事件
本文主要介紹了如何在Vue3和Three.js環(huán)境中為模型添加點擊事件監(jiān)聽,具體方法是利用Three.js的Vector2和Raycaster,首先,創(chuàng)建一個Vector2對象來獲取鼠標位置;然后,創(chuàng)建一個Raycaster對象來投射光線2024-10-10