preload.js 2.9 KB

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