preload.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const fs = require('fs')
  2. const path = require('path')
  3. const { ipcRenderer, contextBridge, shell, clipboard } = 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. if (IS_WIN32) {
  47. shell.openPath(path.dirname(fullpath))
  48. } else {
  49. shell.showItemInFolder(fullpath)
  50. }
  51. },
  52. /**
  53. * When from is empty. The resource maybe from
  54. * client paste or screenshoot.
  55. * @param repoPathRoot
  56. * @param to
  57. * @param from?
  58. * @returns {Promise<void>}
  59. */
  60. async copyFileToAssets (repoPathRoot, to, from) {
  61. if (from && fs.statSync(from).isDirectory()) {
  62. throw new Error('not support copy directory')
  63. }
  64. const dest = path.join(repoPathRoot, to)
  65. const assetsRoot = path.dirname(dest)
  66. if (!/assets$/.test(assetsRoot)) {
  67. throw new Error('illegal assets dirname')
  68. }
  69. await fs.promises.mkdir(assetsRoot, { recursive: true })
  70. from = decodeURIComponent(from || getFilePathFromClipboard())
  71. if (from) {
  72. // console.debug('copy file: ', from, dest)
  73. await fs.promises.copyFile(from, dest)
  74. return path.basename(from)
  75. }
  76. // support image
  77. // console.debug('read image: ', from, dest)
  78. const nImg = clipboard.readImage()
  79. if (nImg && !nImg.isEmpty()) {
  80. const rawExt = path.extname(dest)
  81. return await fs.promises.writeFile(
  82. dest.replace(rawExt, '.png'),
  83. nImg.toPNG()
  84. )
  85. }
  86. },
  87. toggleMaxOrMinActiveWindow (isToggleMin = false) {
  88. ipcRenderer.invoke('toggle-max-or-min-active-win', isToggleMin)
  89. },
  90. /**
  91. * internal
  92. * @param type
  93. * @param args
  94. * @private
  95. */
  96. async _callApplication (type, ...args) {
  97. return await ipcRenderer.invoke('call-application', type, ...args)
  98. },
  99. getFilePathFromClipboard,
  100. isClipboardHasImage
  101. })