Further refactor code
This commit is contained in:
parent
53e25e223e
commit
633332d4ea
1 changed files with 13 additions and 6 deletions
19
skooma.js
19
skooma.js
|
@ -10,10 +10,10 @@ or
|
||||||
html.ul([1, 2, 3, 4, 5].map(x => html.li(x)), {class: "numbers"})
|
html.ul([1, 2, 3, 4, 5].map(x => html.li(x)), {class: "numbers"})
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const keyToPropName = key => key.replace(/^[A-Z]/, a => "-"+a).replace(/[A-Z]/g, a => '-'+a.toLowerCase())
|
|
||||||
|
|
||||||
export const empty = Symbol("Explicit empty argument for Skooma")
|
export const empty = Symbol("Explicit empty argument for Skooma")
|
||||||
|
|
||||||
|
const keyToPropName = key => key.replace(/^[A-Z]/, a => "-"+a).replace(/[A-Z]/g, a => '-'+a.toLowerCase())
|
||||||
|
|
||||||
const insertStyles = (rule, styles) => {
|
const insertStyles = (rule, styles) => {
|
||||||
for (const [key, value] of Object.entries(styles))
|
for (const [key, value] of Object.entries(styles))
|
||||||
if (typeof value == "undefined")
|
if (typeof value == "undefined")
|
||||||
|
@ -93,6 +93,11 @@ const nameSpacedProxy = (options={}) => new Proxy(Window, {
|
||||||
has: (_target, _prop) => true,
|
has: (_target, _prop) => true,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const html = nameSpacedProxy({nameFilter: name => name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()})
|
||||||
|
export const svg = nameSpacedProxy({xmlns: "http://www.w3.org/2000/svg"})
|
||||||
|
|
||||||
|
// Other utility exports
|
||||||
|
|
||||||
export const bind = transform => {
|
export const bind = transform => {
|
||||||
let element
|
let element
|
||||||
const inject = next => Object.defineProperty(next, 'current', {get: () => element})
|
const inject = next => Object.defineProperty(next, 'current', {get: () => element})
|
||||||
|
@ -107,11 +112,10 @@ export const bind = transform => {
|
||||||
return update
|
return update
|
||||||
}
|
}
|
||||||
|
|
||||||
export const handle = fn => event => { event.preventDefault(); return fn(event) }
|
// Wraps an event handler in a function that calls preventDefault on the event
|
||||||
|
export const handle = fn => event => { fn(event); event.preventDefault() }
|
||||||
export const html = nameSpacedProxy({nameFilter: name => name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()})
|
|
||||||
export const svg = nameSpacedProxy({xmlns: "http://www.w3.org/2000/svg"})
|
|
||||||
|
|
||||||
|
// Wraps a list of elements in a document fragment
|
||||||
export const fragment = (...elements) => {
|
export const fragment = (...elements) => {
|
||||||
const fragment = new DocumentFragment()
|
const fragment = new DocumentFragment()
|
||||||
for (const element of elements)
|
for (const element of elements)
|
||||||
|
@ -119,6 +123,9 @@ export const fragment = (...elements) => {
|
||||||
return fragment
|
return fragment
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Turns a template literal into document fragment.
|
||||||
|
// Strings are returned as text nodes.
|
||||||
|
// Elements are inserted in between.
|
||||||
const textFromTemplate = (literals, items) => {
|
const textFromTemplate = (literals, items) => {
|
||||||
const fragment = new DocumentFragment()
|
const fragment = new DocumentFragment()
|
||||||
for (const key in items) {
|
for (const key in items) {
|
||||||
|
|
Loading…
Reference in a new issue