vue3?實(shí)現(xiàn)右鍵菜單編輯復(fù)制粘貼功能
更新時(shí)間:2024年10月23日 11:05:51 作者:橫沖直撞de
在瀏覽器中,Vue3編輯器自帶默認(rèn)右鍵菜單,然而,在Electron桌面應(yīng)用中,這一功能需自行編寫代碼實(shí)現(xiàn),本文詳細(xì)介紹了如何在Vue3中手動實(shí)現(xiàn)右鍵菜單的編輯、復(fù)制、粘貼功能,并提供了代碼示例,更多細(xì)節(jié)和相關(guān)教程可參考腳本之家的其他文章
vue3編輯器在瀏覽器有默認(rèn)的右鍵菜單,但是在客戶端electron中要實(shí)現(xiàn)這個(gè)右鍵菜單的功能需要手寫。

代碼如下
<template>
<!-- 右鍵菜單 -->
<div>
<div
v-if="menuVisible"
@mousedown.prevent
class="custom-menu"
:style="{ top: menuY + 'px', left: menuX + 'px' }"
>
<ul>
<li @click="copyText">復(fù)制</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); // 初始化全局點(diǎn)擊事件,隱藏右鍵菜單
initVditor();
});
const vditor = ref(null); // 使用 ref 來存儲 vditor 實(shí)例
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(""); // 用于保存復(fù)制或剪切的文本
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); // 點(diǎn)擊其他地方隱藏菜單
// 保存當(dāng)前聚焦的元素
lastFocusedElement = document.activeElement;
console.log(lastFocusedElement);
}
// 隱藏右鍵菜單
function hideMenu() {
menuVisible.value = false;
// document.removeEventListener('click', hideMenu);
}
// 復(fù)制選中的文本
function copyText() {
if (copiedText.value) {
console.log("復(fù)制的內(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("復(fù)制的內(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; /* 懸停時(shí)背景色變化 */
border-radius: 10px;
}
/* 下拉動畫 */
@keyframes slideDown {
from {
opacity: 0;
transform: translateY(-10px); /* 初始位置稍微上移 */
}
to {
opacity: 1;
transform: translateY(0);
}
}
</style>到此這篇關(guān)于vue3 實(shí)現(xiàn) 右鍵菜單編輯復(fù)制粘貼的文章就介紹到這了,更多相關(guān)vue3 右鍵菜單編輯復(fù)制粘貼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue監(jiān)聽頁面滾動到某個(gè)高度觸發(fā)事件流程
這篇文章主要介紹了vue監(jiān)聽頁面滾動到某個(gè)高度觸發(fā)事件流程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue3+Three.js實(shí)現(xiàn)為模型添加點(diǎn)擊事件
本文主要介紹了如何在Vue3和Three.js環(huán)境中為模型添加點(diǎn)擊事件監(jiān)聽,具體方法是利用Three.js的Vector2和Raycaster,首先,創(chuàng)建一個(gè)Vector2對象來獲取鼠標(biāo)位置;然后,創(chuàng)建一個(gè)Raycaster對象來投射光線2024-10-10
vue+node實(shí)現(xiàn)圖片上傳及預(yù)覽的示例方法
這篇文章主要介紹了vue+node實(shí)現(xiàn)圖片上傳及預(yù)覽的示例方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11

