版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Vue3和Electron實(shí)現(xiàn)桌面端應(yīng)用詳解showOpenDialog是打開(kāi)文件的方法,我們這里指定了只打開(kāi)md文件;
獲得文件路徑后,通過(guò)focusedWindow.webContents.send("open-file-path",filePath);這個(gè)方法將文件路徑傳給渲染線程。
渲染線程取到文件路徑,讀取文件內(nèi)容,賦值給markdown編輯器
!--App.vue--
import{ipcRenderer}from"electron";
import{readFileSync}from"fs";
exportdefaultdefineComponent({
//省略...
setup(){
constcontent=ref("");
onMounted(()={
//1.
ipcRenderer.on("open-file-path",(e,filePath:string)={
if(filePathfilePath.length0){
//2.
content.value=readFileSync(filePath).toString();
return{content};
vue添加node支持
!--vue.config.js--
module.exports={
pluginOptions:{
electronBuilder:{
nodeIntegration:true,
效果
markdonw的內(nèi)容存入文件
主線程發(fā)起向渲染線程獲取編輯器內(nèi)容的請(qǐng)求
!--background.js--
if(focusedWindow){
focusedWindow.webContents.send("get-content","");
渲染線程主線程向返回編輯器的內(nèi)容
!--App.vue--
onMounted(()={
ipcRenderer.on("get-content",()={
ipcRenderer.send("save-content",content.value);
主線程收到內(nèi)容然后存入文件
!--background.ts--
//存儲(chǔ)文件
ipcMain.on("save-content",(event:unknown,content:string)={
if(openedFile.length0){
//直接存儲(chǔ)到文件中去
try{
writeFileSync(openedFile,content);
console.log("保存成功");
}catch(error){
console.log("保存失敗");
}else{
constoptions={
title:"保存文件",
defaultPath:"new.md",
filters:[{name:"CustomFileType",extensions:["md"]}],
constfocusedWindow=BrowserWindow.getFocusedWindow();
if(focusedWindow){
dialog
.showSaveDialog(focusedWindow,options)
.then((result:Electron.SaveDialogReturnValue)={
if(result.filePath){
try{
writeFileSync(result.filePath,content);
console.log("保存成功");
openedFile=result.filePath;
}catch(error){
console.log("保存失敗");
.catch((error)={
console.log(error);
效果
打包
設(shè)置應(yīng)用的名字和圖片
!--vue.config.js--
module.exports={
pluginOptions:{
electronBuilder:{
nodeIntegration:true,
//添加的設(shè)置
builderOptions:{
appId:"com.johnny.markdown",
productName:"JJMarkDown",//應(yīng)用的名字
copyright:"Copyright2025",//版權(quán)聲明
mac:{
icon:"./public/icon.icns",//icon
icon.icns生成準(zhǔn)備一個(gè)1024*1024的圖片,同級(jí)目錄下創(chuàng)建一個(gè)為icons.iconset的文件夾;
創(chuàng)建各種不同尺寸要求的圖片文件
sips-z1616icon.png-oicons.iconset/icon_16x16.png
sips-z3232icon.png-oicons.iconset/icon_16x16@2x.png
sips-z3232icon.png-oicons.iconset/icon_32x32.png
sips-z6464icon.png-oicons.iconset/icon_32x32@2x.png
sips-z128128icon.png-oicons.iconset/icon_128x128.png
sips-z256256icon.png-oicons.iconset/icon_128x128@2x.png
sips-z256256icon.png-oicons.iconset/icon_256x256.png
sips-z512512icon.png-oicons.iconset/icon_256x256@2x.png
sips-z512512icon.png-oicons.iconset/icon_512x512.png
sips-z10241024icon.png-oicons.iconset/icon_512x512@2x.png
獲得名為icon.icns的圖標(biāo)文件
iconutil-cicnsicons.iconset-oicon.icns
打包
npmrunelectron:build
結(jié)果
獲得的dmg文件就可以直接安裝使用了。
代碼
!--background.ts--
"usestrict";
import{
app,
protocol,
BrowserWindow,
screen,
Menu,
MenuItem,
shell,
dialog,
ipcMain,
}from"electron";
import{KeyboardEvent,MenuItemConstructorOptions}from"electron/main";
import{createProtocol}from"vue-cli-plugin-electron-builder/lib";
importinstallExtension,{VUEJS3_DEVTOOLS}from"electron-devtools-installer";
constisDevelopment=process.env.NODE_ENV!=="production";
import{writeFileSync}from"fs";
letopenedFile="";
//存儲(chǔ)文件
ipcMain.on("save-content",(event:unknown,content:string)={
if(openedFile.length0){
//直接存儲(chǔ)到文件中去
try{
writeFileSync(openedFile,content);
console.log("保存成功");
}catch(error){
console.log("保存失敗");
}else{
constoptions={
title:"保存文件",
defaultPath:"new.md",
filters:[{name:"CustomFileType",extensions:["md"]}],
constfocusedWindow=BrowserWindow.getFocusedWindow();
if(focusedWindow){
dialog
.showSaveDialog(focusedWindow,options)
.then((result:Electron.SaveDialogReturnValue)={
if(result.filePath){
try{
writeFileSync(result.filePath,content);
console.log("保存成功");
openedFile=result.filePath;
}catch(error){
console.log("保存失敗");
.catch((error)={
console.log(error);
consttemplate:ArrayMenuItemConstructorOptions=[
label:"MarkDown",
submenu:[
label:"關(guān)于",
accelerator:"CmdOrCtrl+W",
role:"about",
label:"退出程序",
accelerator:"CmdOrCtrl+Q",
role:"quit",
label:"文件",
submenu:[
label:"打開(kāi)文件",
accelerator:"CmdOrCtrl+O",
click:(
item:MenuItem,
focusedWindow:BrowserWindow|undefined,
//eslint-disable-next-line@typescript-eslint/no-unused-vars
_event:KeyboardEvent
)={
dialog
.showOpenDialog({
properties:["openFile"],
filters:[{name:"CustomFileType",extensions:["md"]}],
.then((res)={
if(resres["filePaths"].length0){
constfilePath=res["filePaths"][0];
//將文件傳給渲染線程
if(focusedWindow){
focusedWindow.webContents.send("open-file-path",filePath);
openedFile=filePath;
.catch((err)={
console.log(err);
label:"存儲(chǔ)",
accelerator:"CmdOrCtrl+S",
click:(
item:MenuItem,
focusedWindow:BrowserWindow|undefined,
//eslint-disable-next-line@typescript-eslint/no-unused-vars
_event:KeyboardEvent
)={
if(focusedWindow){
focusedWindow.webContents.send("get-content","");
label:"編輯",
submenu:[
label:"撤銷",
accelerator:"CmdOrCtrl+Z",
role:"undo",
label:"重做",
accelerator:"Shift+CmdOrCtrl+Z",
role:"redo",
type:"separator",
label:"剪切",
accelerator:"CmdOrCtrl+X",
role:"cut",
label:"復(fù)制",
accelerator:"CmdOrCtrl+C",
role:"copy",
label:"粘貼",
accelerator:"CmdOrCtrl+V",
role:"paste",
label:"窗口",
role:"window",
submenu:[
label:"最小化",
accelerator:"CmdOrCtrl+M",
role:"minimize",
label:"最大化",
accelerator:"CmdOrCtrl+M",
click:(
item:MenuItem,
focusedWindow:BrowserWindow|undefined,
//eslint-disable-next-line@typescript-eslint/no-unused-vars
_event:KeyboardEvent
)={
if(focusedWindow){
focusedWindow.maximize();
type:"separator",
label:"切換全屏",
accelerator:(function(){
if(process.platform==="darwin"){
return"Ctrl+Command+F";
}else{
return"F11";
})(),
click:(
item:MenuItem,
focusedWindow:BrowserWindow|undefined,
//eslint-disable-next-line@typescript-eslint/no-unused-vars
_event:KeyboardEvent
)={
if(focusedWindow){
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
label:"幫助",
role:"help",
submenu:[
label:"學(xué)習(xí)更多",
click:function(){
shell.openExternal("http://electron.atom.io");
protocol.registerSchemesAsPrivileged([
{scheme:"app",privileges:{secure:true,standard:true}},
asyncfunctioncreateWindow(){
const{width,height}=screen.getPrimaryDisplay().workAreaSize;
constwin=newBrowserWindow({
width,
height,
webPreferences:{
nodeIntegration:true,
contextIsolation:false,
if(process.env.WEBPACK_DEV_SERVER_URL){
//Loadtheurlofthedevserverifindevelopmentmode
awaitwin.loadURL(process.env.WEBPACK_DEV_SERVER_URLasstring);
if(!process.env.IS_TEST)win.webContents.openDevTools();
}else{
createProtocol("app");
//Loadtheindex.htmlwhennotindevelopment
win.loadURL("app://./index.html");
//Quitwhenallwindowsareclosed.
app.on("window-all-closed",()={
//OnmacOSitiscommonforapplicationsandtheirmenubar
//tostayactiveuntiltheuserquitsexplicitlywithCmd+Q
if(process.platform!=="darwin"){
app.quit();
app.on("activate",()={
//OnmacOSit'scommontore-createawindowintheappwhenthe
//dockiconisclic
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 消防知識(shí)競(jìng)賽超全版題庫(kù)
- 中國(guó)消防安全總公司權(quán)威指南
- 牙套佩戴與口腔健康的關(guān)系
- 車站運(yùn)管考試試題及答案
- 醫(yī)學(xué)生理試題及答案
- 2025-2026人教版一年級(jí)體育期末測(cè)試卷
- 腸道微生物組氫氣代謝物與氧化應(yīng)激
- ICU心電圖相關(guān)知識(shí)
- 年產(chǎn)400萬(wàn)米柔紗簾、羅馬簾、200萬(wàn)米卷簾、香格里拉簾項(xiàng)目環(huán)評(píng)報(bào)告
- 教學(xué)樓衛(wèi)生考核制度
- 服務(wù)外包人員保密管理制度(3篇)
- 2026中國(guó)電信四川公用信息產(chǎn)業(yè)有限責(zé)任公司社會(huì)成熟人才招聘?jìng)淇碱}庫(kù)及答案詳解(奪冠系列)
- 成都高新區(qū)桂溪街道公辦幼兒園招聘編外人員考試備考題庫(kù)及答案解析
- 2025年醫(yī)院病歷管理操作規(guī)范
- 2026云南保山電力股份有限公司校園招聘50人筆試備考題庫(kù)及答案解析
- GB 4053.2-2025固定式金屬梯及平臺(tái)安全要求第2部分:斜梯
- 2026屆上海市長(zhǎng)寧區(qū)市級(jí)名校高一上數(shù)學(xué)期末學(xué)業(yè)質(zhì)量監(jiān)測(cè)模擬試題含解析
- 2026年煙草公司筆試綜合試題及考點(diǎn)實(shí)操指引含答案
- 九年級(jí)寒假期末總結(jié)課件
- 壓鑄機(jī)作業(yè)人員安全培訓(xùn)課件
- 新產(chǎn)品研發(fā)質(zhì)量管控流程詳解
評(píng)論
0/150
提交評(píng)論