preload.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const fs = require('fs')
  2. const path = require('path')
  3. const { ipcRenderer, contextBridge, shell, clipboard, BrowserWindow } = require('electron')
  4. const IS_MAC = process.platform === 'darwin'
  5. const IS_WIN32 = process.platform === 'win32'
  6. function getFilePathFromClipboard () {
  7. if (IS_WIN32) {
  8. const rawFilePath = clipboard.read('FileNameW')
  9. return rawFilePath.replace(new RegExp(String.fromCharCode(0), 'g'), '')
  10. }
  11. return clipboard.read('public.file-url').replace('file://', '')
  12. }
  13. function isClipboardHasImage () {
  14. return !clipboard.readImage().isEmpty()
  15. }
  16. contextBridge.exposeInMainWorld('apis', {
  17. doAction: async (arg) => {
  18. return await ipcRenderer.invoke('main', arg)
  19. },
  20. on: (channel, callback) => {
  21. const newCallback = (_, data) => callback(data)
  22. ipcRenderer.on(channel, newCallback)
  23. },
  24. checkForUpdates: async (...args) => {
  25. await ipcRenderer.invoke('check-for-updates', ...args)
  26. },
  27. setUpdatesCallback (cb) {
  28. if (typeof cb !== 'function') return
  29. const channel = 'updates-callback'
  30. ipcRenderer.removeAllListeners(channel)
  31. ipcRenderer.on(channel, cb)
  32. },
  33. installUpdatesAndQuitApp () {
  34. ipcRenderer.invoke('install-updates', true)
  35. },
  36. async openExternal (url, options) {
  37. await shell.openExternal(url, options)
  38. },
  39. async openPath (path) {
  40. await shell.openPath(path)
  41. },
  42. showItemInFolder (fullpath) {
  43. shell.showItemInFolder(fullpath)
  44. },
  45. /**
  46. * When from is empty. The resource maybe from
  47. * client paste or screenshoot.
  48. * @param repoPathRoot
  49. * @param to
  50. * @param from?
  51. * @returns {Promise<void>}
  52. */
  53. async copyFileToAssets (repoPathRoot, to, from) {
  54. if (from && fs.statSync(from).isDirectory()) {
  55. throw new Error('not support copy directory')
  56. }
  57. const dest = path.join(repoPathRoot, to)
  58. const assetsRoot = path.dirname(dest)
  59. if (!/assets$/.test(assetsRoot)) {
  60. throw new Error('illegal assets dirname')
  61. }
  62. await fs.promises.mkdir(assetsRoot, { recursive: true })
  63. from = decodeURIComponent(from || getFilePathFromClipboard())
  64. if (from) {
  65. // console.debug('copy file: ', from, dest)
  66. await fs.promises.copyFile(from, dest)
  67. return path.basename(from)
  68. }
  69. // support image
  70. // console.debug('read image: ', from, dest)
  71. const nImg = clipboard.readImage()
  72. if (nImg && !nImg.isEmpty()) {
  73. const rawExt = path.extname(dest)
  74. return await fs.promises.writeFile(
  75. dest.replace(rawExt, '.png'),
  76. nImg.toPNG()
  77. )
  78. }
  79. },
  80. toggleMaxOrMinActiveWindow (isToggleMin = false) {
  81. ipcRenderer.invoke('toggle-max-or-min-active-win', isToggleMin)
  82. },
  83. /**
  84. * internal
  85. * @param type
  86. * @param args
  87. * @private
  88. */
  89. async _callApplication (type, ...args) {
  90. return await ipcRenderer.invoke('call-application', type, ...args)
  91. },
  92. getFilePathFromClipboard,
  93. isClipboardHasImage
  94. })