preload.js 4.5 KB

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