-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
326 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Implements JsonML *like* element creation (http://www.jsonml.org/) | ||
* | ||
* The major difference is this takes event handlers for `on` functions | ||
* and supports nested attributes? Also allows elements. | ||
* | ||
* ```js | ||
* document.body.appendChild(makeElem([ | ||
* 'style', | ||
* '.bold { font-weight: bold; }', | ||
* '.italic { font-style: italic; }', | ||
* ])); | ||
* | ||
* document.body.appendChild(makeElem([ | ||
* 'div', | ||
* 'This next word is ', | ||
* ['span', {style: 'color: red'}, 'red'], // style is string | ||
* ' and this next word is ', | ||
* ['span', {style: {color: 'blue'}}, 'blue'], // style is object | ||
* ' and this next word is ', | ||
* ['span', {className: 'bold'}, 'bold'], // className works | ||
* ' and this next word is ', | ||
* ['span', {class: 'italic bold'}, 'italic-bold'], // class works too? | ||
* ])); | ||
* | ||
* document.body.appendChild(makeElem([ | ||
* 'form', | ||
* 'Enter name:', | ||
* ['input', {type: 'text', placeholder: 'Jane Doe'}], | ||
* [ | ||
* 'button', | ||
* { | ||
* type: 'button', | ||
* onClick: (e) => { | ||
* console.log('name:', e.target.previousElementSibling.value); | ||
* }, | ||
* }, | ||
* 'submit', | ||
* ], | ||
* ])); | ||
* ``` | ||
*/ | ||
export function makeElem(elemSpec) { | ||
const tag = elemSpec[0]; | ||
if (tag instanceof Node) { | ||
return tag; | ||
} | ||
const elem = document.createElement(tag); | ||
|
||
let firstChildNdx = 1; | ||
if (typeof elemSpec[1] !== Node && typeof elemSpec[1] !== 'string' && !Array.isArray(elemSpec[1])) { | ||
firstChildNdx = 2; | ||
for (const [key, value] of Object.entries(elemSpec[1])) { | ||
if (typeof value === 'function' && key.startsWith('on')) { | ||
const eventName = key.substring(2).toLowerCase(); | ||
elem.addEventListener(eventName, value, {passive: false}); | ||
} else if (typeof value === 'object') { | ||
for (const [k, v] of Object.entries(value)) { | ||
elem[key][k] = v; | ||
} | ||
} else if (elem[key] === undefined) { | ||
elem.setAttribute(key, value); | ||
} else { | ||
elem[key] = value; | ||
} | ||
} | ||
} | ||
|
||
for (let ndx = firstChildNdx; ndx < elemSpec.length; ++ndx) { | ||
const v = elemSpec[ndx]; | ||
if (typeof v === 'string') { | ||
elem.appendChild(document.createTextNode(v)); | ||
} else if (v instanceof Node) { | ||
elem.appendChild(v); | ||
} else { | ||
elem.appendChild(makeElem(v)); | ||
} | ||
} | ||
return elem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
:root { | ||
--whl-th-bg-color: lightblue; | ||
--whl-row-bg-color: linear-gradient(#FFF, #EEE); | ||
} | ||
@media (prefers-color-scheme: dark) { | ||
:root { | ||
--whl-th-bg-color: blue; | ||
--whl-row-bg-color: linear-gradient(#333, #111); | ||
} | ||
} | ||
div[data-diagram="typedArrays"] { | ||
font-family: monospace; | ||
font-size: small; | ||
display: flex; | ||
justify-content: center; | ||
flex-direction: column; | ||
overflow-x: auto; | ||
|
||
label { display: flex; align-items: center;} | ||
table { border-collapse: collapse; line-height: 1.5; width: max-content; } | ||
thead { background-color: var(--whl-th-bg-color);} | ||
th, td { border: 1px solid gray; position: relative; padding: 0.2em; margin: 0 } | ||
input[type="text"] { border: none; font-family: monospace; background-color: inherit; width: 100%; padding: 0; margin: 0; text-align: right; } | ||
.error { background-color: red; } | ||
|
||
/* | ||
tr:nth-child(even) { background-color: #333;} | ||
tr:nth-child(odd) { background-color: #222;} | ||
*/ | ||
tr { background: var(--whl-row-bg-color); } | ||
|
||
td[colspan="1"] { width: 3em } | ||
td[colspan="2"] { width: 6em } | ||
td[colspan="4"] { width: 12em } | ||
td[colspan="8"] { width: 24em } | ||
} | ||
|
||
@media (width < 740px) { | ||
div[data-diagram="typedArrays"] { | ||
font-size: x-small; | ||
display: block; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters