Themes: init

This commit is contained in:
He4eT 2021-02-19 19:08:31 +05:00
commit cbc6207ad9
9 changed files with 108 additions and 46 deletions

View file

@ -1,10 +1,10 @@
import { h } from 'preact' import { h } from 'preact'
export default function ({ emitName, emitURL }) { export default function ({ setTargetName, setTargetUrl }) {
const fileInputHandler = ({ target }) => { const fileInputHandler = ({ target }) => {
const file = target.files[0] const file = target.files[0]
emitName(file.name) setTargetName(file.name)
emitURL(`${URL.createObjectURL(file)}#${file.name}`) setTargetUrl(`${URL.createObjectURL(file)}#${file.name}`)
target.value = null target.value = null
} }

View file

@ -0,0 +1,15 @@
import { h } from 'preact'
export default function ({ themeList, currentTheme, setTheme }) {
const options = themeList.map(theme => (
<option value={theme}>
{theme}
</option>))
return (
<select
value={currentTheme}
onChange={({ target }) => setTheme(target.value)}>
{options}
</select>)
}

View file

@ -1,16 +1,16 @@
import { h } from 'preact' import { h } from 'preact'
export default function ({ emitName, emitURL }) { export default function ({ setTargetName, setTargetUrl }) {
const reURL = /^(http|https):\/\/[^ "]+$/ const urlRE = /^(http|https):\/\/[^ "]+$/
const emit = url => { const emit = url => {
emitName(url) setTargetName(url)
emitURL(url) setTargetUrl(url)
} }
const urlInputHandler = ({ target }) => { const urlInputHandler = ({ target }) => {
const url = target.value const url = target.value
emit(reURL.test(url) emit(urlRE.test(url)
? url ? url
: null) : null)
} }

View file

@ -1,27 +1,43 @@
import { h, render } from 'preact' import { h, render } from 'preact'
import { Link, Route, Router, Switch } from 'wouter-preact' import { useState } from 'preact/hooks'
import { Route, Router, Switch } from 'wouter-preact'
import { useHashLocation } from '~/src/utils/utils.routing' import { useHashLocation } from '~/src/utils/utils.routing'
import {
DEFAULT_THEME,
themeList,
assertTheme
} from '~/src/themes/themes'
import Home from '~/src/views/Home' import Home from '~/src/views/Home'
import Player from '~/src/views/Player' import Player from '~/src/views/Player'
import '~/src/style/base.css'
function App () { function App () {
const [currentTheme, setCurrentTheme] =
useState(DEFAULT_THEME)
const setTheme = theme =>
setCurrentTheme(assertTheme(theme))
return ( return (
<Router hook={useHashLocation}> <Router hook={useHashLocation}>
<div className='App'> <div className={['app', currentTheme].join(' ')}>
<nav>
<Link href='/'>Root</Link>
</nav>
<main>
<Switch> <Switch>
<Route <Route path='/'>
path='/' <Home {...{
component={Home} /> setTheme,
<Route themeList,
path='/play/:theme/:encodedUrl' currentTheme
component={Player} /> }} />
</Route>
<Route path='/play/:theme/:encodedUrl'>
{params => <Player {...{
setTheme,
...params
}} />}
</Route>
<Route path='/top100'> <Route path='/top100'>
top100 top100
</Route> </Route>
@ -29,7 +45,6 @@ function App () {
404 404
</Route> </Route>
</Switch> </Switch>
</main>
</div> </div>
</Router> </Router>
) )

3
src/style/base.css Normal file
View file

@ -0,0 +1,3 @@
html, body {
margin: 0;
}

15
src/themes/themes.js Normal file
View file

@ -0,0 +1,15 @@
export const themeList = [
'default',
'default-dark',
'nord',
'solarized-dark',
'solarized-light',
'_raw'
]
export const DEFAULT_THEME = themeList[0]
export const assertTheme = theme =>
themeList.includes(theme)
? theme
: DEFAULT_THEME

View file

@ -20,7 +20,7 @@ export const useHashLocation = () => {
return [loc, navigate] return [loc, navigate]
} }
export const buildPlayLinkHref = (url, theme = 'default') => export const buildPlayLinkHref = (url, theme) =>
`/#/play/${theme}/${encodeURIComponent(url)}` `/#/play/${theme}/${encodeURIComponent(url)}`
export const getFileExtension = fileName => export const getFileExtension = fileName =>

View file

@ -5,18 +5,19 @@ import { Link } from 'wouter-preact'
import FileSelector from '~/src/components/FileSelector' import FileSelector from '~/src/components/FileSelector'
import URLSelector from '~/src/components/URLSelector' import URLSelector from '~/src/components/URLSelector'
import ThemeSelector from '~/src/components/ThemeSelector'
import { buildPlayLinkHref } from '~/src/utils/utils.routing' import { buildPlayLinkHref } from '~/src/utils/utils.routing'
export default function () { const playButton = (name, url, theme) => (
const [targetName, setTargetName] = useState(null) <Link href={buildPlayLinkHref(url, theme)}>
const [targetURL, setTargetURL] = useState(null) Play "{name}"
const playButton = (
<Link href={buildPlayLinkHref(targetURL)}>
Play "{targetName}"
</Link>) </Link>)
export default function ({ themeList, currentTheme, setTheme }) {
const [targetName, setTargetName] = useState(null)
const [targetUrl, setTargetUrl] = useState(null)
return ( return (
<main> <main>
<p> <p>
@ -26,16 +27,27 @@ export default function () {
</p> </p>
<ul> <ul>
<li> <li>
<FileSelector <ThemeSelector {...{
emitName={setTargetName} currentTheme,
emitURL={setTargetURL} /> themeList,
setTheme
}} />
</li> </li>
<li> <li>
<URLSelector <FileSelector {...{
emitName={setTargetName} setTargetName,
emitURL={setTargetURL} /> setTargetUrl
}} />
</li>
<li>
<URLSelector {...{
setTargetName,
setTargetUrl
}} />
</li> </li>
</ul> </ul>
{ targetURL ? playButton : null } { targetUrl
? playButton(targetName, targetUrl, currentTheme)
: null }
</main>) </main>)
} }

View file

@ -2,7 +2,9 @@ import { h } from 'preact'
import { getFileExtension } from '~/src/utils/utils.routing' import { getFileExtension } from '~/src/utils/utils.routing'
export default function ({params: {theme, encodedUrl}}) { export default function ({setTheme, theme, encodedUrl}) {
setTheme(theme)
const url = decodeURIComponent(encodedUrl) const url = decodeURIComponent(encodedUrl)
const type = getFileExtension(url) const type = getFileExtension(url)