preload.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. const ALLOWED_EXTERNAL_PROTOCOLS = ['https:', 'http:', 'mailto:', 'zotero:', 'file:']
  7. function getFilePathFromClipboard () {
  8. if (IS_WIN32) {
  9. const rawFilePath = clipboard.read('FileNameW')
  10. return rawFilePath.replace(new RegExp(String.fromCharCode(0), 'g'), '')
  11. } else if (IS_MAC) {
  12. return clipboard.read('public.file-url').replace('file://', '')
  13. } else {
  14. return clipboard.readText()
  15. }
  16. }
  17. /**
  18. * Read the contents of the clipboard for a custom format.
  19. * @param {string} format The custom format to read.
  20. * @returns Buffer containing the contents of the clipboard for the specified format, or null if not available.
  21. */
  22. function getClipboardData (format) {
  23. if (clipboard.has(mime, "clipboard")) {
  24. return clipboard.readBuffer(mime)
  25. }
  26. else {
  27. return null;
  28. }
  29. }
  30. contextBridge.exposeInMainWorld('apis', {
  31. doAction: async (arg) => {
  32. return await ipcRenderer.invoke('main', arg)
  33. },
  34. invoke: async (channel, args) => {
  35. return await ipcRenderer.invoke(channel, ...args)
  36. },
  37. addListener: ipcRenderer.on.bind(ipcRenderer),
  38. removeListener: ipcRenderer.removeListener.bind(ipcRenderer),
  39. removeAllListeners: ipcRenderer.removeAllListeners.bind(ipcRenderer),
  40. on: (channel, callback) => {
  41. const newCallback = (_, data) => callback(data)
  42. ipcRenderer.on(channel, newCallback)
  43. },
  44. off: (channel, callback) => {
  45. if (!callback) {
  46. ipcRenderer.removeAllListeners(channel)
  47. } else {
  48. ipcRenderer.removeListener(channel, callback)
  49. }
  50. },
  51. once: (channel, callback) => {
  52. ipcRenderer.on(channel, callback)
  53. },
  54. checkForUpdates: async (...args) => {
  55. await ipcRenderer.invoke('check-for-updates', ...args)
  56. },
  57. setUpdatesCallback (cb) {
  58. if (typeof cb !== 'function') return
  59. const channel = 'updates-callback'
  60. ipcRenderer.removeAllListeners(channel)
  61. ipcRenderer.on(channel, cb)
  62. },
  63. installUpdatesAndQuitApp () {
  64. ipcRenderer.invoke('install-updates', true)
  65. },
  66. async openExternal (url, options) {
  67. const protocol = new URL(url).protocol
  68. if (!ALLOWED_EXTERNAL_PROTOCOLS.includes(protocol)) {
  69. throw new Error('illegal protocol')
  70. }
  71. await shell.openExternal(url, options)
  72. },
  73. async openPath (path) {
  74. await shell.openPath(path)
  75. },
  76. showItemInFolder (fullpath) {
  77. if (IS_WIN32) {
  78. shell.openPath(path.dirname(fullpath).replaceAll("/", "\\"))
  79. } else {
  80. shell.showItemInFolder(fullpath)
  81. }
  82. },
  83. /**
  84. * save all publish assets to disk
  85. *
  86. * @param {string} html html file with embedded state
  87. */
  88. exportPublishAssets (html, customCSSPath, exportCSSPath, repoPath, assetFilenames, outputDir) {
  89. ipcRenderer.invoke(
  90. 'export-publish-assets',
  91. html,
  92. customCSSPath,
  93. exportCSSPath,
  94. repoPath,
  95. assetFilenames,
  96. outputDir
  97. )
  98. },
  99. /**
  100. * When from is empty. The resource maybe from
  101. * client paste or screenshoot.
  102. * @param repoPathRoot
  103. * @param to
  104. * @param from?
  105. * @returns {Promise<void>}
  106. */
  107. async copyFileToAssets (repoPathRoot, to, from) {
  108. if (from && fs.statSync(from).isDirectory()) {
  109. throw new Error('not support copy directory')
  110. }
  111. const dest = path.join(repoPathRoot, to)
  112. const assetsRoot = path.dirname(dest)
  113. await fs.promises.mkdir(assetsRoot, { recursive: true })
  114. from = from || getFilePathFromClipboard()
  115. if (from) {
  116. try {
  117. // console.debug('copy file: ', from, dest)
  118. await fs.promises.copyFile(from, dest)
  119. return path.basename(from)
  120. } catch (e) {
  121. from = decodeURIComponent(from)
  122. await fs.promises.copyFile(from, dest)
  123. return path.basename(from)
  124. }
  125. }
  126. // support image
  127. // console.debug('read image: ', from, dest)
  128. const nImg = clipboard.readImage()
  129. if (nImg && !nImg.isEmpty()) {
  130. const rawExt = path.extname(dest)
  131. return await fs.promises.writeFile(
  132. dest.replace(rawExt, '.png'),
  133. nImg.toPNG()
  134. )
  135. }
  136. },
  137. toggleMaxOrMinActiveWindow (isToggleMin = false) {
  138. ipcRenderer.invoke('toggle-max-or-min-active-win', isToggleMin)
  139. },
  140. /**
  141. * internal
  142. * @param type
  143. * @param args
  144. * @private
  145. */
  146. async _callApplication (type, ...args) {
  147. return await ipcRenderer.invoke('call-application', type, ...args)
  148. },
  149. /**
  150. * internal
  151. * @param type
  152. * @param args
  153. * @private
  154. */
  155. async _callMainWin (type, ...args) {
  156. return await ipcRenderer.invoke('call-main-win', type, ...args)
  157. },
  158. getFilePathFromClipboard,
  159. getClipboardData,
  160. setZoomFactor (factor) {
  161. webFrame.setZoomFactor(factor)
  162. },
  163. setZoomLevel (level) {
  164. webFrame.setZoomLevel(level)
  165. },
  166. isAbsolutePath: path.isAbsolute.bind(path)
  167. })