refactor: convert more files to typescript (#16820)
This commit is contained in:
parent
cfbdc40814
commit
01c442de64
16 changed files with 169 additions and 90 deletions
|
@ -10,6 +10,11 @@ common.defineProperties(exports)
|
|||
for (const module of moduleList) {
|
||||
Object.defineProperty(exports, module.name, {
|
||||
enumerable: !module.private,
|
||||
get: common.memoizedGetter(() => require(`@electron/internal/browser/api/${module.file}.js`))
|
||||
get: common.memoizedGetter(() => {
|
||||
const value = require(`@electron/internal/browser/api/${module.file}.js`)
|
||||
// Handle Typescript modules with an "export default X" statement
|
||||
if (value.__esModule) return value.default
|
||||
return value
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,29 +1,29 @@
|
|||
'use strict'
|
||||
|
||||
const { app, session } = require('electron')
|
||||
import { app, session } from 'electron'
|
||||
|
||||
// Global protocol APIs.
|
||||
module.exports = process.atomBinding('protocol')
|
||||
const protocol = process.atomBinding('protocol')
|
||||
|
||||
// Fallback protocol APIs of default session.
|
||||
Object.setPrototypeOf(module.exports, new Proxy({}, {
|
||||
Object.setPrototypeOf(protocol, new Proxy({}, {
|
||||
get (target, property) {
|
||||
if (!app.isReady()) return
|
||||
|
||||
const protocol = session.defaultSession.protocol
|
||||
const protocol = session.defaultSession!.protocol
|
||||
if (!Object.getPrototypeOf(protocol).hasOwnProperty(property)) return
|
||||
|
||||
// Returning a native function directly would throw error.
|
||||
return (...args) => protocol[property](...args)
|
||||
return (...args: any[]) => (protocol[property as keyof Electron.Protocol] as Function)(...args)
|
||||
},
|
||||
|
||||
ownKeys () {
|
||||
if (!app.isReady()) return []
|
||||
|
||||
return Object.getOwnPropertyNames(Object.getPrototypeOf(session.defaultSession.protocol))
|
||||
return Object.getOwnPropertyNames(Object.getPrototypeOf(session.defaultSession!.protocol))
|
||||
},
|
||||
|
||||
getOwnPropertyDescriptor (target) {
|
||||
return { configurable: true, enumerable: true }
|
||||
}
|
||||
}))
|
||||
|
||||
export default protocol
|
|
@ -1,14 +1,13 @@
|
|||
'use strict'
|
||||
import { shell, Menu } from 'electron'
|
||||
|
||||
const { shell, Menu } = require('electron')
|
||||
const v8Util = process.atomBinding('v8_util')
|
||||
|
||||
const isMac = process.platform === 'darwin'
|
||||
|
||||
const setDefaultApplicationMenu = () => {
|
||||
if (v8Util.getHiddenValue(global, 'applicationMenuSet')) return
|
||||
export const setDefaultApplicationMenu = () => {
|
||||
if (v8Util.getHiddenValue<boolean>(global, 'applicationMenuSet')) return
|
||||
|
||||
const helpMenu = {
|
||||
const helpMenu: Electron.MenuItemConstructorOptions = {
|
||||
role: 'help',
|
||||
submenu: [
|
||||
{
|
||||
|
@ -40,8 +39,9 @@ const setDefaultApplicationMenu = () => {
|
|||
]
|
||||
}
|
||||
|
||||
const template = [
|
||||
...(isMac ? [{ role: 'appMenu' }] : []),
|
||||
const macAppMenu: Electron.MenuItemConstructorOptions = { role: 'appMenu' }
|
||||
const template: Electron.MenuItemConstructorOptions[] = [
|
||||
...(isMac ? [macAppMenu] : []),
|
||||
{ role: 'fileMenu' },
|
||||
{ role: 'editMenu' },
|
||||
{ role: 'viewMenu' },
|
||||
|
@ -52,7 +52,3 @@ const setDefaultApplicationMenu = () => {
|
|||
const menu = Menu.buildFromTemplate(template)
|
||||
Menu.setApplicationMenu(menu)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setDefaultApplicationMenu
|
||||
}
|
|
@ -49,10 +49,15 @@ process.on('uncaughtException', function (error) {
|
|||
}
|
||||
|
||||
// Show error in GUI.
|
||||
const dialog = require('electron').dialog
|
||||
const stack = error.stack ? error.stack : `${error.name}: ${error.message}`
|
||||
const message = 'Uncaught Exception:\n' + stack
|
||||
dialog.showErrorBox('A JavaScript error occurred in the main process', message)
|
||||
// We can't import { dialog } at the top of this file as this file is
|
||||
// responsible for setting up the require hook for the "electron" module
|
||||
// so we import it inside the handler down here
|
||||
import('electron')
|
||||
.then(({ dialog }) => {
|
||||
const stack = error.stack ? error.stack : `${error.name}: ${error.message}`
|
||||
const message = 'Uncaught Exception:\n' + stack
|
||||
dialog.showErrorBox('A JavaScript error occurred in the main process', message)
|
||||
})
|
||||
})
|
||||
|
||||
// Emit 'exit' event on quit.
|
||||
|
@ -195,10 +200,13 @@ app.on('window-all-closed', () => {
|
|||
}
|
||||
})
|
||||
|
||||
const { setDefaultApplicationMenu } = require('@electron/internal/browser/default-menu')
|
||||
|
||||
// Create default menu.
|
||||
app.once('ready', setDefaultApplicationMenu)
|
||||
Promise.all([
|
||||
import('@electron/internal/browser/default-menu'),
|
||||
app.whenReady
|
||||
]).then(([{ setDefaultApplicationMenu }]) => {
|
||||
// Create default menu
|
||||
setDefaultApplicationMenu()
|
||||
})
|
||||
|
||||
if (packagePath) {
|
||||
// Finally load app's main.js and transfer control to C++.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue