main.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. const fs = require('fs-plus')
  2. const { CompositeDisposable } = require('atom')
  3. let MarkdownPreviewView = null
  4. let renderer = null
  5. const isMarkdownPreviewView = function (object) {
  6. if (MarkdownPreviewView == null) {
  7. MarkdownPreviewView = require('./markdown-preview-view')
  8. }
  9. return object instanceof MarkdownPreviewView
  10. }
  11. module.exports = {
  12. activate() {
  13. this.disposables = new CompositeDisposable()
  14. this.commandSubscriptions = new CompositeDisposable()
  15. this.style = new CSSStyleSheet()
  16. // TODO: When we upgrade Electron, we can push onto `adoptedStyleSheets`
  17. // directly. For now, we have to do this silly thing.
  18. let styleSheets = Array.from(document.adoptedStyleSheets ?? [])
  19. styleSheets.push(this.style)
  20. document.adoptedStyleSheets = styleSheets
  21. this.disposables.add(
  22. atom.config.observe('markdown-preview.grammars', grammars => {
  23. this.commandSubscriptions.dispose()
  24. this.commandSubscriptions = new CompositeDisposable()
  25. if (grammars == null) {
  26. grammars = []
  27. }
  28. for (const grammar of grammars.map(grammar =>
  29. grammar.replace(/\./g, ' ')
  30. )) {
  31. this.commandSubscriptions.add(
  32. atom.commands.add(`atom-text-editor[data-grammar='${grammar}']`, {
  33. 'markdown-preview:toggle': () => this.toggle(),
  34. 'markdown-preview:copy-html': {
  35. displayName: 'Markdown Preview: Copy HTML',
  36. didDispatch: () => this.copyHTML()
  37. },
  38. 'markdown-preview:save-as-html': {
  39. displayName: 'Markdown Preview: Save as HTML',
  40. didDispatch: () => this.saveAsHTML()
  41. },
  42. 'markdown-preview:toggle-break-on-single-newline': () => {
  43. const keyPath = 'markdown-preview.breakOnSingleNewline'
  44. atom.config.set(keyPath, !atom.config.get(keyPath))
  45. },
  46. 'markdown-preview:toggle-github-style': () => {
  47. const keyPath = 'markdown-preview.useGitHubStyle'
  48. atom.config.set(keyPath, !atom.config.get(keyPath))
  49. }
  50. })
  51. )
  52. }
  53. })
  54. )
  55. this.disposables.add(
  56. atom.config.observe('editor.fontFamily', (fontFamily) => {
  57. // Keep the user's `fontFamily` setting in sync with preview styles.
  58. // `pre` blocks will use this font automatically, but `code` elements
  59. // need a specific style rule.
  60. //
  61. // Since this applies to all content, we should declare this only once,
  62. // instead of once per preview view.
  63. this.style.replaceSync(`
  64. .markdown-preview code {
  65. font-family: ${fontFamily} !important;
  66. }
  67. `)
  68. })
  69. )
  70. const previewFile = this.previewFile.bind(this)
  71. for (const extension of [
  72. 'markdown',
  73. 'md',
  74. 'mdown',
  75. 'mkd',
  76. 'mkdown',
  77. 'ron',
  78. 'txt'
  79. ]) {
  80. this.disposables.add(
  81. atom.commands.add(
  82. `.tree-view .file .name[data-name$=\\.${extension}]`,
  83. 'markdown-preview:preview-file',
  84. previewFile
  85. )
  86. )
  87. }
  88. this.disposables.add(
  89. atom.workspace.addOpener(uriToOpen => {
  90. let [protocol, path] = uriToOpen.split('://')
  91. if (protocol !== 'markdown-preview') {
  92. return
  93. }
  94. try {
  95. path = decodeURI(path)
  96. } catch (error) {
  97. return
  98. }
  99. if (path.startsWith('editor/')) {
  100. return this.createMarkdownPreviewView({ editorId: path.substring(7) })
  101. } else {
  102. return this.createMarkdownPreviewView({ filePath: path })
  103. }
  104. })
  105. )
  106. },
  107. deactivate() {
  108. this.disposables.dispose()
  109. this.commandSubscriptions.dispose()
  110. },
  111. createMarkdownPreviewView(state) {
  112. if (state.editorId || fs.isFileSync(state.filePath)) {
  113. if (MarkdownPreviewView == null) {
  114. MarkdownPreviewView = require('./markdown-preview-view')
  115. }
  116. return new MarkdownPreviewView(state)
  117. }
  118. },
  119. toggle() {
  120. if (isMarkdownPreviewView(atom.workspace.getActivePaneItem())) {
  121. atom.workspace.destroyActivePaneItem()
  122. return
  123. }
  124. const editor = atom.workspace.getActiveTextEditor()
  125. if (editor == null) {
  126. return
  127. }
  128. const grammars = atom.config.get('markdown-preview.grammars') || []
  129. if (!grammars.includes(editor.getGrammar().scopeName)) {
  130. return
  131. }
  132. if (!this.removePreviewForEditor(editor)) {
  133. return this.addPreviewForEditor(editor)
  134. }
  135. },
  136. uriForEditor(editor) {
  137. return `markdown-preview://editor/${editor.id}`
  138. },
  139. removePreviewForEditor(editor) {
  140. const uri = this.uriForEditor(editor)
  141. const previewPane = atom.workspace.paneForURI(uri)
  142. if (previewPane != null) {
  143. previewPane.destroyItem(previewPane.itemForURI(uri))
  144. return true
  145. } else {
  146. return false
  147. }
  148. },
  149. addPreviewForEditor(editor) {
  150. const uri = this.uriForEditor(editor)
  151. const previousActivePane = atom.workspace.getActivePane()
  152. const options = { searchAllPanes: true }
  153. if (atom.config.get('markdown-preview.openPreviewInSplitPane')) {
  154. options.split = 'right'
  155. }
  156. return atom.workspace
  157. .open(uri, options)
  158. .then(function (markdownPreviewView) {
  159. if (isMarkdownPreviewView(markdownPreviewView)) {
  160. previousActivePane.activate()
  161. }
  162. })
  163. },
  164. previewFile({ target }) {
  165. const filePath = target.dataset.path
  166. if (!filePath) {
  167. return
  168. }
  169. for (const editor of atom.workspace.getTextEditors()) {
  170. if (editor.getPath() === filePath) {
  171. return this.addPreviewForEditor(editor)
  172. }
  173. }
  174. atom.workspace.open(`markdown-preview://${encodeURI(filePath)}`, {
  175. searchAllPanes: true
  176. })
  177. },
  178. async copyHTML() {
  179. const editor = atom.workspace.getActiveTextEditor()
  180. if (editor == null) {
  181. return
  182. }
  183. if (renderer == null) {
  184. renderer = require('./renderer')
  185. }
  186. const text = editor.getSelectedText() || editor.getText()
  187. const html = await renderer.toHTML(
  188. text,
  189. editor.getPath(),
  190. editor.getGrammar(),
  191. editor.id
  192. )
  193. atom.clipboard.write(html)
  194. },
  195. saveAsHTML() {
  196. const activePaneItem = atom.workspace.getActivePaneItem()
  197. if (isMarkdownPreviewView(activePaneItem)) {
  198. atom.workspace.getActivePane().saveItemAs(activePaneItem)
  199. return
  200. }
  201. const editor = atom.workspace.getActiveTextEditor()
  202. if (editor == null) {
  203. return
  204. }
  205. const grammars = atom.config.get('markdown-preview.grammars') || []
  206. if (!grammars.includes(editor.getGrammar().scopeName)) {
  207. return
  208. }
  209. const uri = this.uriForEditor(editor)
  210. const markdownPreviewPane = atom.workspace.paneForURI(uri)
  211. const markdownPreviewPaneItem =
  212. markdownPreviewPane != null
  213. ? markdownPreviewPane.itemForURI(uri)
  214. : undefined
  215. if (isMarkdownPreviewView(markdownPreviewPaneItem)) {
  216. return markdownPreviewPane.saveItemAs(markdownPreviewPaneItem)
  217. }
  218. }
  219. }