123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- var Popup = () => {
- var state = {
- compiler: '',
- options: {},
- content: {},
- theme: '',
- themes: {},
- _themes: [
- 'github',
- 'github-dark',
- // 'air',
- 'almond',
- 'awsm',
- 'axist',
- 'bamboo',
- 'bullframe',
- 'holiday',
- 'kacit',
- 'latex',
- 'marx',
- 'mini',
- 'modest',
- 'new',
- 'no-class',
- 'pico',
- 'retro',
- 'sakura',
- 'sakura-vader',
- 'semantic',
- 'simple',
- // 'splendor',
- 'style-sans',
- 'style-serif',
- 'stylize',
- 'superstylin',
- 'tacit',
- 'vanilla',
- 'water',
- 'water-dark',
- 'writ',
- 'custom',
- ],
- _width: [
- 'auto',
- 'full',
- 'wide',
- 'large',
- 'medium',
- 'small',
- 'tiny',
- ],
- raw: false,
- tab: '',
- tabs: ['theme', 'compiler', 'content'],
- compilers: [],
- description: {
- themes: {},
- compiler: {},
- content: {
- autoreload: 'Auto reload on file change',
- emoji: 'Convert emoji :shortnames: into EmojiOne images',
- toc: 'Generate Table of Contents',
- mathjax: 'Render MathJax formulas',
- mermaid: 'Mermaid diagrams',
- syntax: 'Syntax highlighting for fenced code blocks',
- }
- },
- settings: {}
- }
- var events = {
- tab: (e) => {
- state.tab = e.target.hash.replace('#tab-', '')
- localStorage.setItem('tab', state.tab)
- return false
- },
- compiler: {
- name: (e) => {
- state.compiler = state.compilers[e.target.selectedIndex]
- chrome.runtime.sendMessage({
- message: 'popup.compiler.name',
- compiler: state.compiler,
- }, () => {
- chrome.runtime.sendMessage({message: 'popup'}, init)
- })
- },
- options: (e) => {
- state.options[e.target.name] = !state.options[e.target.name]
- chrome.runtime.sendMessage({
- message: 'popup.compiler.options',
- compiler: state.compiler,
- options: state.options,
- })
- }
- },
- content: (e) => {
- state.content[e.target.name] = !state.content[e.target.name]
- chrome.runtime.sendMessage({
- message: 'popup.content',
- content: state.content,
- })
- },
- themes: (e) => {
- state.themes.width = state._width[e.target.selectedIndex]
- chrome.runtime.sendMessage({
- message: 'popup.themes',
- themes: state.themes,
- })
- },
- theme: (e) => {
- state.theme = state._themes[e.target.selectedIndex]
- chrome.runtime.sendMessage({
- message: 'popup.theme',
- theme: state.theme
- })
- },
- raw: () => {
- state.raw = !state.raw
- chrome.runtime.sendMessage({
- message: 'popup.raw',
- raw: state.raw
- })
- },
- defaults: () => {
- chrome.runtime.sendMessage({
- message: 'popup.defaults'
- }, () => {
- chrome.runtime.sendMessage({message: 'popup'}, init)
- localStorage.removeItem('tab')
- state._tabs.activeTabIndex = 0
- })
- },
- advanced: () => {
- chrome.runtime.sendMessage({message: 'popup.advanced'})
- }
- }
- var init = (res) => {
- state.compiler = res.compiler
- state.options = res.options
- state.content = res.content
- state.theme = res.theme
- state.themes = res.themes
- state.raw = res.raw
- state.tab = localStorage.getItem('tab') || 'theme'
- state.compilers = res.compilers
- state.description.compiler = res.description
- state.settings = res.settings
- document.querySelector('body').classList.add(state.settings.theme)
- m.redraw()
- }
- chrome.runtime.sendMessage({message: 'popup'}, init)
- var oncreate = {
- ripple: (vnode) => {
- mdc.ripple.MDCRipple.attachTo(vnode.dom)
- },
- tabs: (vnode) => {
- state._tabs = mdc.tabs.MDCTabBar.attachTo(vnode.dom)
- setTimeout(() => {
- state._tabs.activeTabIndex = state.tabs.indexOf(state.tab)
- }, 250)
- }
- }
- var onupdate = (tab, key) => (vnode) => {
- var value = tab === 'compiler' ? state.options[key]
- : tab === 'content' ? state.content[key]
- : null
- if (vnode.dom.classList.contains('is-checked') !== value) {
- vnode.dom.classList.toggle('is-checked')
- }
- }
- var render = () =>
- m('#popup',
- // raw
- m('button.mdc-button mdc-button--raised m-button', {
- oncreate: oncreate.ripple,
- onclick: events.raw
- },
- (state.raw ? 'Html' : 'Markdown')
- ),
- // defaults
- m('button.mdc-button mdc-button--raised m-button', {
- oncreate: oncreate.ripple,
- onclick: events.defaults
- },
- 'Defaults'
- ),
- // tabs
- m('nav.mdc-tab-bar m-tabs', {
- oncreate: oncreate.tabs,
- onclick: events.tab
- },
- state.tabs.map((tab) =>
- m('a.mdc-tab', {
- href: '#tab-' + tab,
- },
- tab
- )),
- m('span.mdc-tab-bar__indicator')
- ),
- m('.m-panels',
- // theme
- m('.m-panel', {
- class: state.tab === 'theme' ? 'is-active' : ''
- },
- m('select.mdc-elevation--z2 m-select', {
- onchange: events.theme
- },
- state._themes.map((theme) =>
- m('option', {selected: state.theme === theme}, theme)
- )
- ),
- m('select.mdc-elevation--z2 m-select', {
- onchange: events.themes
- },
- state._width.map((width) =>
- m('option', {
- selected: state.themes.width === width,
- }, width)
- )
- ),
- ),
- // compiler
- m('.m-panel', {
- class: state.tab === 'compiler' ? 'is-active' : ''
- },
- m('select.mdc-elevation--z2 m-select', {
- onchange: events.compiler.name
- },
- state.compilers.map((name) =>
- m('option', {selected: state.compiler === name}, name)
- )
- ),
- m('.scroll', {
- class: Object.keys(state.options)
- .filter((key) => typeof state.options[key] === 'boolean')
- .length > 8
- ? 'max' : ''
- },
- Object.keys(state.options)
- .filter((key) => typeof state.options[key] === 'boolean')
- .map((key) =>
- m('label.mdc-switch m-switch', {
- onupdate: onupdate('compiler', key),
- title: state.description.compiler[key]
- },
- m('input.mdc-switch__native-control', {
- type: 'checkbox',
- name: key,
- checked: state.options[key],
- onchange: events.compiler.options
- }),
- m('.mdc-switch__background', m('.mdc-switch__knob')),
- m('span.mdc-switch-label', key)
- )
- )
- )
- ),
- // content
- m('.m-panel', {
- class: state.tab === 'content' ? 'is-active' : ''
- },
- m('.scroll', Object.keys(state.content).map((key) =>
- m('label.mdc-switch m-switch', {
- onupdate: onupdate('content', key),
- title: state.description.content[key]
- },
- m('input.mdc-switch__native-control', {
- type: 'checkbox',
- name: key,
- checked: state.content[key],
- onchange: events.content
- }),
- m('.mdc-switch__background', m('.mdc-switch__knob')),
- m('span.mdc-switch-label', key)
- ))
- )
- )
- ),
- // advanced options
- m('button.mdc-button mdc-button--raised m-button', {
- oncreate: oncreate.ripple,
- onclick: events.advanced
- },
- 'Advanced Options'
- )
- )
- var options = () =>
- m('.row m-settings hidden',
- m('.col-xxl-4.col-xl-4.col-lg-6.col-md-6.col-sm-12',
- m('h3', 'Theme'),
- m('.bs-callout m-theme',
- m('.row',
- m('.col-xxl-6.col-xl-6.col-lg-6.col-md-6.col-sm-12',
- m('span.m-label',
- 'Content Theme'
- )
- ),
- m('.col-xxl-6.col-xl-6.col-lg-6.col-md-6.col-sm-12',
- m('select.mdc-elevation--z2 m-select', {
- onchange: events.theme
- },
- state._themes.map((theme) =>
- m('option', {selected: state.theme === theme}, theme)
- )
- )
- ),
- ),
- m('.row',
- m('.col-xxl-6.col-xl-6.col-lg-6.col-md-6.col-sm-12',
- m('span.m-label',
- 'Content Width'
- )
- ),
- m('.col-xxl-6.col-xl-6.col-lg-6.col-md-6.col-sm-12',
- m('select.mdc-elevation--z2 m-select', {
- onchange: events.themes
- },
- state._width.map((width) =>
- m('option', {
- selected: state.themes.width === width,
- }, width)
- )
- )
- ),
- ),
- settings.render()
- ),
- state.theme === 'custom' &&
- custom.render()
- ),
- m('.col-xxl-4.col-xl-4.col-lg-6.col-md-6.col-sm-12',
- m('h3', 'Compiler'),
- m('.bs-callout m-compiler',
- m('select.mdc-elevation--z2 m-select', {
- onchange: events.compiler.name
- },
- state.compilers.map((name) =>
- m('option', {selected: state.compiler === name}, name)
- )
- ),
- m('.scroll', {
- class: Object.keys(state.options)
- .filter((key) => typeof state.options[key] === 'boolean')
- .length > 8
- ? 'max' : ''
- },
- Object.keys(state.options)
- .filter((key) => typeof state.options[key] === 'boolean')
- .map((key) =>
- m('label.mdc-switch m-switch', {
- onupdate: onupdate('compiler', key),
- title: state.description.compiler[key]
- },
- m('input.mdc-switch__native-control', {
- type: 'checkbox',
- name: key,
- checked: state.options[key],
- onchange: events.compiler.options
- }),
- m('.mdc-switch__background', m('.mdc-switch__knob')),
- m('span.mdc-switch-label', key)
- )
- )
- )
- ),
- ),
- m('.col-xxl-4.col-xl-4.col-lg-6.col-md-6.col-sm-12',
- m('h3', 'Content'),
- m('.bs-callout m-content',
- m('.scroll', Object.keys(state.content).map((key) =>
- m('label.mdc-switch m-switch', {
- onupdate: onupdate('content', key),
- title: state.description.content[key]
- },
- m('input.mdc-switch__native-control', {
- type: 'checkbox',
- name: key,
- checked: state.content[key],
- onchange: events.content
- }),
- m('.mdc-switch__background', m('.mdc-switch__knob')),
- m('span.mdc-switch-label', key)
- ))
- )
- ),
- ),
- )
- return {state, render, options}
- }
- if (document.querySelector('.is-popup')) {
- var popup = Popup()
- m.mount(document.querySelector('body'), {
- view: (vnode) => popup.render()
- })
- }
- else {
- var settings = Settings()
- var custom = Custom()
- }
|