preload.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 && decodeURIComponent(from || getFilePathFromClipboard())
  102. if (from) {
  103. // console.debug('copy file: ', from, dest)
  104. await fs.promises.copyFile(from, dest)
  105. return path.basename(from)
  106. }
  107. // support image
  108. // console.debug('read image: ', from, dest)
  109. const nImg = clipboard.readImage()
  110. if (nImg && !nImg.isEmpty()) {
  111. const rawExt = path.extname(dest)
  112. return await fs.promises.writeFile(
  113. dest.replace(rawExt, '.png'),
  114. nImg.toPNG()
  115. )
  116. }
  117. },
  118. toggleMaxOrMinActiveWindow (isToggleMin = false) {
  119. ipcRenderer.invoke('toggle-max-or-min-active-win', isToggleMin)
  120. },
  121. /**
  122. * internal
  123. * @param type
  124. * @param args
  125. * @private
  126. */
  127. async _callApplication (type, ...args) {
  128. return await ipcRenderer.invoke('call-application', type, ...args)
  129. },
  130. /**
  131. * internal
  132. * @param type
  133. * @param args
  134. * @private
  135. */
  136. async _callMainWin (type, ...args) {
  137. return await ipcRenderer.invoke('call-main-win', type, ...args)
  138. },
  139. getFilePathFromClipboard,
  140. setZoomFactor (factor) {
  141. webFrame.setZoomFactor(factor)
  142. },
  143. setZoomLevel (level) {
  144. webFrame.setZoomLevel(level)
  145. },
  146. isAbsolutePath: path.isAbsolute.bind(path)
  147. })