preload.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * save all publish assets to disk
  54. *
  55. * @param {string} html html file with embedded state
  56. */
  57. exportPublishAssets(html, customCSSPath, repoPath, assetFilenames) {
  58. ipcRenderer.invoke(
  59. 'export-publish-assets',
  60. html,
  61. customCSSPath,
  62. repoPath,
  63. assetFilenames
  64. )
  65. },
  66. /**
  67. * When from is empty. The resource maybe from
  68. * client paste or screenshoot.
  69. * @param repoPathRoot
  70. * @param to
  71. * @param from?
  72. * @returns {Promise<void>}
  73. */
  74. async copyFileToAssets(repoPathRoot, to, from) {
  75. if (from && fs.statSync(from).isDirectory()) {
  76. throw new Error('not support copy directory')
  77. }
  78. const dest = path.join(repoPathRoot, to)
  79. const assetsRoot = path.dirname(dest)
  80. if (!/assets$/.test(assetsRoot)) {
  81. throw new Error('illegal assets dirname')
  82. }
  83. await fs.promises.mkdir(assetsRoot, { recursive: true })
  84. from = decodeURIComponent(from || getFilePathFromClipboard())
  85. if (from) {
  86. // console.debug('copy file: ', from, dest)
  87. await fs.promises.copyFile(from, dest)
  88. return path.basename(from)
  89. }
  90. // support image
  91. // console.debug('read image: ', from, dest)
  92. const nImg = clipboard.readImage()
  93. if (nImg && !nImg.isEmpty()) {
  94. const rawExt = path.extname(dest)
  95. return await fs.promises.writeFile(
  96. dest.replace(rawExt, '.png'),
  97. nImg.toPNG()
  98. )
  99. }
  100. },
  101. toggleMaxOrMinActiveWindow(isToggleMin = false) {
  102. ipcRenderer.invoke('toggle-max-or-min-active-win', isToggleMin)
  103. },
  104. /**
  105. * internal
  106. * @param type
  107. * @param args
  108. * @private
  109. */
  110. async _callApplication(type, ...args) {
  111. return await ipcRenderer.invoke('call-application', type, ...args)
  112. },
  113. getFilePathFromClipboard,
  114. isClipboardHasImage,
  115. })