popup.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var state = {
  2. compiler: {},
  3. themes: [],
  4. theme: '',
  5. raw: false
  6. }
  7. var events = {
  8. changeCompiler: (e) => {
  9. state.compiler[e.target.name] = !state.compiler[e.target.name]
  10. chrome.runtime.sendMessage({
  11. message: 'compiler',
  12. compiler: state.compiler
  13. })
  14. },
  15. changeTheme: (e) => {
  16. state.theme = state.themes[e.target.selectedIndex]
  17. chrome.runtime.sendMessage({
  18. message: 'theme',
  19. theme: state.theme
  20. })
  21. },
  22. viewRaw: () => {
  23. state.raw = !state.raw
  24. chrome.runtime.sendMessage({
  25. message: 'raw',
  26. raw: state.raw,
  27. theme: state.theme
  28. })
  29. },
  30. setDefaults: () => {
  31. chrome.runtime.sendMessage({
  32. message: 'defaults'
  33. }, (res) => {
  34. chrome.runtime.sendMessage({message: 'settings'}, init)
  35. })
  36. },
  37. advancedOptions: () => {
  38. chrome.runtime.sendMessage({message: 'advanced'})
  39. }
  40. }
  41. var description = {
  42. gfm: 'Enable GFM\n(GitHub Flavored Markdown)',
  43. tables: 'Enable GFM tables\n(requires the gfm option to be true)',
  44. breaks: 'Enable GFM line breaks\n(requires the gfm option to be true)',
  45. pedantic: 'Don\'t fix any of the original markdown\nbugs or poor behavior',
  46. sanitize: 'Ignore any HTML\nthat has been input',
  47. smartLists: 'Use smarter list behavior\nthan the original markdown',
  48. smartypants: 'Use "smart" typograhic punctuation\nfor things like quotes and dashes'
  49. }
  50. function init (res) {
  51. state.compiler = res.compiler
  52. state.theme = res.theme
  53. state.themes = chrome.runtime.getManifest().web_accessible_resources
  54. .filter((file) => (file.indexOf('/themes/') === 0))
  55. .map((file) => (file.replace(/\/themes\/(.*)\.css/, '$1')))
  56. state.raw = res.raw
  57. m.redraw()
  58. }
  59. function oncreate (vnode) {
  60. componentHandler.upgradeElements(vnode.dom)
  61. }
  62. var onupdate = (key) => (vnode) => {
  63. if (vnode.dom.classList.contains('is-checked') !== state.compiler[key]) {
  64. vnode.dom.classList.toggle('is-checked')
  65. }
  66. }
  67. chrome.runtime.sendMessage({message: 'settings'}, init)
  68. m.mount(document.querySelector('body'), {
  69. view: (vnode) =>
  70. m('#popup', [
  71. m('button.mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect',
  72. {oncreate, onclick: events.viewRaw},
  73. (state.raw ? 'Html' : 'Markdown')),
  74. m('button.mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect',
  75. {oncreate, onclick: events.setDefaults},
  76. 'Defaults'),
  77. m('.mdl-tabs mdl-js-tabs mdl-js-ripple-effect', {oncreate},
  78. m('.mdl-tabs__tab-bar',
  79. m('a.mdl-tabs__tab', {href: '#tab-theme', class: 'is-active'}, 'Theme')
  80. ),
  81. m('.mdl-tabs__panel #tab-theme', {class: 'is-active'},
  82. m('select.mdl-shadow--2dp', {onchange: events.changeTheme}, state.themes.map((theme) =>
  83. m('option', {selected: state.theme === theme}, theme)
  84. ))
  85. )
  86. ),
  87. m('.mdl-tabs mdl-js-tabs mdl-js-ripple-effect', {oncreate},
  88. m('.mdl-tabs__tab-bar',
  89. m('a.mdl-tabs__tab', {href: '#tab-compiler', class: 'is-active'}, 'Compiler'),
  90. m('a.mdl-tabs__tab', {href: '#tab-content'}, 'Content')
  91. ),
  92. m('.mdl-tabs__panel #tab-compiler', {class: 'is-active'},
  93. m('.mdl-grid', Object.keys(state.compiler).map((key) =>
  94. m('.mdl-cell',
  95. m('label.mdl-switch mdl-js-switch mdl-js-ripple-effect',
  96. {oncreate, onupdate: onupdate(key), title: description[key]}, [
  97. m('input[type="checkbox"].mdl-switch__input', {
  98. name: key,
  99. checked: state.compiler[key],
  100. onchange: events.changeCompiler
  101. }),
  102. m('span.mdl-switch__label', key)
  103. ])
  104. )
  105. ))
  106. ),
  107. m('.mdl-tabs__panel #tab-content',
  108. m('.mdl-grid',
  109. m('.mdl-cell', 'Content')
  110. )
  111. )
  112. ),
  113. m('button.mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect',
  114. {oncreate, onclick: events.advancedOptions},
  115. 'Advanced Options')
  116. ])
  117. })