preload.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. const fs = require('fs')
  2. const path = require('path')
  3. const { ipcRenderer, contextBridge, shell, clipboard, webFrame } = 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. contextBridge.exposeInMainWorld('apis', {
  17. doAction: async (arg) => {
  18. return await ipcRenderer.invoke('main', arg)
  19. },
  20. invoke: async (channel, args) => {
  21. return await ipcRenderer.invoke(channel, ...args)
  22. },
  23. addListener: ipcRenderer.on.bind(ipcRenderer),
  24. removeListener: ipcRenderer.removeListener.bind(ipcRenderer),
  25. removeAllListeners: ipcRenderer.removeAllListeners.bind(ipcRenderer),
  26. on: (channel, callback) => {
  27. const newCallback = (_, data) => callback(data)
  28. ipcRenderer.on(channel, newCallback)
  29. },
  30. off: (channel, callback) => {
  31. if (!callback) {
  32. ipcRenderer.removeAllListeners(channel)
  33. } else {
  34. ipcRenderer.removeListener(channel, callback)
  35. }
  36. },
  37. once: (channel, callback) => {
  38. ipcRenderer.on(channel, callback)
  39. },
  40. checkForUpdates: async (...args) => {
  41. await ipcRenderer.invoke('check-for-updates', ...args)
  42. },
  43. setUpdatesCallback (cb) {
  44. if (typeof cb !== 'function') return
  45. const channel = 'updates-callback'
  46. ipcRenderer.removeAllListeners(channel)
  47. ipcRenderer.on(channel, cb)
  48. },
  49. installUpdatesAndQuitApp () {
  50. ipcRenderer.invoke('install-updates', true)
  51. },
  52. async openExternal (url, options) {
  53. await shell.openExternal(url, options)
  54. },
  55. async openPath (path) {
  56. await shell.openPath(path)
  57. },
  58. showItemInFolder (fullpath) {
  59. if (IS_WIN32) {
  60. shell.openPath(path.dirname(fullpath).replaceAll("/", "\\"))
  61. } else {
  62. shell.showItemInFolder(fullpath)
  63. }
  64. },
  65. /**
  66. * save all publish assets to disk
  67. *
  68. * @param {string} html html file with embedded state
  69. */
  70. exportPublishAssets (html, customCSSPath, exportCSSPath, repoPath, assetFilenames, outputDir) {
  71. ipcRenderer.invoke(
  72. 'export-publish-assets',
  73. html,
  74. customCSSPath,
  75. exportCSSPath,
  76. repoPath,
  77. assetFilenames,
  78. outputDir
  79. )
  80. },
  81. /**
  82. * When from is empty. The resource maybe from
  83. * client paste or screenshoot.
  84. * @param repoPathRoot
  85. * @param to
  86. * @param from?
  87. * @returns {Promise<void>}
  88. */
  89. async copyFileToAssets (repoPathRoot, to, from) {
  90. if (from && fs.statSync(from).isDirectory()) {
  91. throw new Error('not support copy directory')
  92. }
  93. const dest = path.join(repoPathRoot, to)
  94. const assetsRoot = path.dirname(dest)
  95. await fs.promises.mkdir(assetsRoot, { recursive: true })
  96. from = from && decodeURIComponent(from || getFilePathFromClipboard())
  97. if (from) {
  98. // console.debug('copy file: ', from, dest)
  99. await fs.promises.copyFile(from, dest)
  100. return path.basename(from)
  101. }
  102. // support image
  103. // console.debug('read image: ', from, dest)
  104. const nImg = clipboard.readImage()
  105. if (nImg && !nImg.isEmpty()) {
  106. const rawExt = path.extname(dest)
  107. return await fs.promises.writeFile(
  108. dest.replace(rawExt, '.png'),
  109. nImg.toPNG()
  110. )
  111. }
  112. },
  113. toggleMaxOrMinActiveWindow (isToggleMin = false) {
  114. ipcRenderer.invoke('toggle-max-or-min-active-win', isToggleMin)
  115. },
  116. /**
  117. * internal
  118. * @param type
  119. * @param args
  120. * @private
  121. */
  122. async _callApplication (type, ...args) {
  123. return await ipcRenderer.invoke('call-application', type, ...args)
  124. },
  125. /**
  126. * internal
  127. * @param type
  128. * @param args
  129. * @private
  130. */
  131. async _callMainWin (type, ...args) {
  132. return await ipcRenderer.invoke('call-main-win', type, ...args)
  133. },
  134. getFilePathFromClipboard,
  135. setZoomFactor (factor) {
  136. webFrame.setZoomFactor(factor)
  137. },
  138. setZoomLevel (level) {
  139. webFrame.setZoomLevel(level)
  140. },
  141. isAbsolutePath: path.isAbsolute.bind(path)
  142. })