From 54e55eb8997da6ae094305801a3d59cc8d2222fb Mon Sep 17 00:00:00 2001 From: Dong Xia Date: Fri, 29 Nov 2024 13:06:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89loading=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/background.js | 90 ++++++++++++++++++++------------------ src/directive/directive.js | 57 +++++++++++++++++------- 2 files changed, 87 insertions(+), 60 deletions(-) diff --git a/src/background.js b/src/background.js index c42341e..8fe8996 100644 --- a/src/background.js +++ b/src/background.js @@ -11,51 +11,55 @@ protocol.registerSchemesAsPrivileged([ ]) let mainWin = null async function createWindow() { - mainWin = new BrowserWindow({ - width: 1400, - height: 1000, - show: false, - backgroundColor: '#2e2c29', - title: '系统软件安装管理平台', - webPreferences: { - nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION, - contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION, - webSecurity: false - } - }) - const menuTemplate = Menu.buildFromTemplate([ - { - label: '返回&(Ctrl+B)', - accelerator: 'Ctrl+B', - click: () => { - mainWin.webContents.goBack() + try{ + mainWin = new BrowserWindow({ + width: 1400, + height: 1000, + show: false, + backgroundColor: '#2e2c29', + title: '系统软件安装管理平台', + webPreferences: { + nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION, + contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION, + webSecurity: false + } + }) + const menuTemplate = Menu.buildFromTemplate([ + { + label: '返回&(Ctrl+B)', + accelerator: 'Ctrl+B', + click: () => { + mainWin.webContents.goBack() + } + }, + { + label: '全屏&(Ctrl+F)', + accelerator: 'Ctrl+F', + role: 'togglefullscreen' + }, + { + label: '退出&(Ctrl+Q)', + accelerator: 'Ctrl+Q', + role: 'close' + }, + { + label: '强制刷新&(Ctrl+S)', + accelerator: 'Ctrl+S', + role: 'reload' } - }, - { - label: '全屏&(Ctrl+F)', - accelerator: 'Ctrl+F', - role: 'togglefullscreen' - }, - { - label: '退出&(Ctrl+Q)', - accelerator: 'Ctrl+Q', - role: 'close' - }, - { - label: '强制刷新&(Ctrl+S)', - accelerator: 'Ctrl+S', - role: 'reload' + ]) + Menu.setApplicationMenu(menuTemplate) + mainWin.once('ready-to-show', () => { + mainWin.maximize() + mainWin.show() + }) + createProtocol('app') + mainWin.loadURL('app://./index.html') + toOpenDevTools = function() { + mainWin.webContents.openDevTools() } - ]) - Menu.setApplicationMenu(menuTemplate) - mainWin.once('ready-to-show', () => { - mainWin.maximize() - mainWin.show() - }) - createProtocol('app') - mainWin.loadURL('app://./index.html') - toOpenDevTools = function() { - mainWin.webContents.openDevTools() + } catch(error) { + console.log(error) } } diff --git a/src/directive/directive.js b/src/directive/directive.js index 8893db6..1c48cfe 100644 --- a/src/directive/directive.js +++ b/src/directive/directive.js @@ -1,14 +1,24 @@ +const CUSTOMIZE_CLASS_NAME = 'customize'; +let formElementCache; + function checkValue(el, binding) { - const oldClassName = 'ant-row ant-form-item' - if (binding.value === '' || !binding.value) { - el.className = oldClassName + ' customize' - document.getElementsByClassName('table-form')[0]['validate'] = () => { - return false + if (!formElementCache) { + formElementCache = document.getElementsByClassName('table-form')[0]; + if (!formElementCache) { + console.error('Element with class "table-form" not found'); + return; + } + } + + if (!binding.value) { + el.classList.add(CUSTOMIZE_CLASS_NAME); + formElementCache['validate'] = () => { + return false; } } else { - el.className = oldClassName - document.getElementsByClassName('table-form')[0]['validate'] = () => { - return true + el.classList.remove(CUSTOMIZE_CLASS_NAME); + formElementCache['validate'] = () => { + return true; } } } @@ -23,17 +33,30 @@ let validator = { } -function loading(value) { - let type = typeof value - if (type !== 'boolean') { - console.error(new Error(`Expected a Boolean value but got one ${type}`)) +function isBoolean(value) { + return typeof value === 'boolean'; +} + +let cachedCanvasId = null; + +/** + * 控制加载状态的显示与隐藏 + * @param {boolean} value - 是否显示加载状态 + * @param {function} [onError] - 错误处理回调函数 + */ +function loading(value, onError) { + if (!isBoolean(value)) { + const error = new Error(`Expected a Boolean value but got one ${typeof value}`); + console.error(error); + if (onError && typeof onError === 'function') { + onError(error); + } + return; } - let canvasId = document.getElementById('canvas-box') - if (!!value) { - canvasId.style.display = 'block' - } else { - canvasId.style.display = 'none' + if (!cachedCanvasId) { + cachedCanvasId = document.getElementById('canvas-box'); } + cachedCanvasId.style.display = value ? 'block' : 'none'; } let waiting = { -- Gitee