youzanyun-devtool-worker
Version:
173 lines (164 loc) • 5.02 kB
JavaScript
var isElectron = window.navigator.userAgent.toLowerCase().indexOf('electron') === -1 ? false : true;
var electron;
if (isElectron) {
electron = require("electron");
}
function _selectDirectoryElectron(config) {
const {dialog} = electron.remote;
return new Promise((resolve, reject) => {
/* dialog.showOpenDialog({
defaultPath: config.defaultPath,
title: config.title || '选择目录',
properties: ['openDirectory'],
}).then(({ canceled, filePaths }) => {
resolve({
canceled,
filePath: filePaths.length > 0 ? filePaths[0] : ''
})
}) */
dialog.showOpenDialog({
defaultPath: config.defaultPath,
title: config.title || '选择目录',
properties: ['openDirectory'],
}, (filePaths, bookmarks) => {
const canceled = !filePaths || filePaths.length === 0;
resolve({
canceled,
filePath: canceled ? '' : filePaths[0],
})
});
})
}
function _selectDirectoryElementUi(config) {
let { MessageBox } = config;
return MessageBox.$prompt(config.description || '请输入目录全路径', config.title || '选择目录', {
inputValue: config.defaultPath || '',
closeOnClickModal: false,
closeOnPressEscape: false,
confirmButtonText: '确定',
cancelButtonText: '取消',
}).then(({ value }) => {
return {
canceled: false,
filePath: value
}
}).catch((e) => {
return {
canceled: true,
filePath: ''
}
});
}
function _selectFileElectron(config) {
const {dialog} = electron.remote;
return new Promise((resolve, reject) => {
dialog.showOpenDialog({
defaultPath: config.defaultPath,
title: config.title || '选择文件',
properties: ['openFile']
}, (filePaths, bookmarks) => {
const canceled = !filePaths || filePaths.length === 0;
resolve({
canceled,
filePath: canceled ? '' : filePaths[0],
})
});
})
}
function _selectFileElementUi(config) {
let {MessageBox} = config;
return MessageBox.prompt(config.description || '请输入文件全路径', config.title || '选择文件', {
closeOnClickModal: false,
closeOnPressEscape: false,
confirmButtonText: '确定',
cancelButtonText: '取消',
}).then(({ value }) => {
return {
canceled: false,
filePath: value
}
}).catch((e) => {
return {
canceled: true,
filePath: ''
}
});
}
window.electronBridge = {
isElectron,
openNewWindow: function (url, options = {}) {
var newUrl = url;
if (!url.startsWith('http')) {
newUrl = `${location.origin}${url}`;
}
if (isElectron) {
electron.ipcRenderer.send('open-window', newUrl, options);
} else {
window.open(newUrl);
}
},
selectDirectory(config = {}) {
if (isElectron) {
return _selectDirectoryElectron(config)
} else {
return _selectDirectoryElementUi(config);
}
},
selectFile(config = {}) {
if (isElectron) {
return _selectFileElectron(config)
} else {
return _selectFileElementUi(config);
}
},
// 窗口操作
windowOperate(type) {
if (isElectron) {
var currentWindow = electron.remote.getCurrentWindow();
switch (type) {
case 'fullScreen':
// 当前状态
var isFullScreen = currentWindow.isFullScreen();
currentWindow.setFullScreen(!isFullScreen);
break;
case 'minimize':
currentWindow.minimize();
break;
case 'close':
currentWindow.close();
break;
case 'focus':
currentWindow.focus();
break;
default:
break;
}
}
},
// 窗口状态
isFullScreen() {
if (isElectron) {
var currentWindow = electron.remote.getCurrentWindow();
// 当前状态
var isFullScreen = currentWindow.isFullScreen();
return isFullScreen;
} else {
return true;
}
},
// 打开浏览器
openBrowser(url) {
if (isElectron) {
electron.shell.openExternal(url);
} else {
window.open(url, '_blank');
}
},
getVersion() {
var version = '';
if (isElectron) {
version = electron.remote.app.getVersion();
}
return version;
}
};