mirror of
https://github.com/osmarks/website
synced 2025-08-30 01:07:57 +00:00
updated things???
This commit is contained in:
48
experiments/site-themes/index.js
Normal file
48
experiments/site-themes/index.js
Normal file
@@ -0,0 +1,48 @@
|
||||
let currentStyle = localStorage.getItem("user-stylesheet") || ""
|
||||
|
||||
if (!customStyleEl) {
|
||||
customStyleEl = document.createElement("style")
|
||||
customStyleEl.appendChild(document.createTextNode(customStyle))
|
||||
customStyleEl.onload = () => console.log("Loaded custom styles")
|
||||
customStyleEl.id = "custom-style"
|
||||
document.head.appendChild(customStyleEl)
|
||||
}
|
||||
|
||||
const saveStyles = () => { localStorage.setItem("user-stylesheet", currentStyle) }
|
||||
const applyStyles = () => {
|
||||
try { document.head.removeChild(customStyleEl) } catch {}
|
||||
while (customStyleEl.firstChild) { customStyleEl.removeChild(customStyleEl.firstChild) }
|
||||
customStyleEl.appendChild(document.createTextNode(currentStyle))
|
||||
document.head.appendChild(customStyleEl)
|
||||
}
|
||||
const removeStyles = () => {
|
||||
document.head.removeChild(customStyleEl)
|
||||
}
|
||||
|
||||
const handleTab = ev => {
|
||||
if (ev.keyCode === 9) { // tab
|
||||
const start = ev.target.selectionStart
|
||||
const end = ev.target.selectionEnd
|
||||
const value = ev.target.value
|
||||
ev.target.value = value.substring(0, start) + "\t" + value.substring(end)
|
||||
ev.target.selectionStart = ev.target.selectionEnd = start + 1
|
||||
ev.preventDefault()
|
||||
}
|
||||
}
|
||||
|
||||
const inputOnCreate = vnode => { vnode.dom.value = currentStyle }
|
||||
|
||||
const App = {
|
||||
view: () => m("", [
|
||||
m("em", "Due to browser limitations, this currently cannot tell you about errors or warnings in your stylesheet. You can check your browser console for these, probably."),
|
||||
m(".toolbar", [
|
||||
m("button", { onclick: () => { saveStyles(); applyStyles() } }, "Save & Apply"),
|
||||
m("button", { onclick: saveStyles }, "Save"),
|
||||
m("button", { onclick: applyStyles }, "Apply"),
|
||||
m("button", { onclick: removeStyles }, "Remove")
|
||||
]),
|
||||
m("textarea.style-input", { oncreate: inputOnCreate, oninput: ev => { currentStyle = ev.target.value }, onkeydown: handleTab }),
|
||||
])
|
||||
}
|
||||
|
||||
m.mount(document.getElementById("app"), App)
|
Reference in New Issue
Block a user