mirror of https://github.com/n2geoff/uhm
release v0.8.0
This commit is contained in:
parent
bb89b46303
commit
93364bc203
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@n2geoff/uhm",
|
"name": "@n2geoff/uhm",
|
||||||
"version": "0.7.0",
|
"version": "0.8.0",
|
||||||
"exports": "./index.js",
|
"exports": "./index.js",
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"dev": "deno run --watch index.js",
|
"dev": "deno run --watch index.js",
|
||||||
|
|
|
@ -121,7 +121,7 @@ var diff = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/*! Uhm v0.7.0 | MIT LICENSE | https://github.com/n2geoff/uhm */
|
/*! Uhm v0.8.0 | MIT LICENSE | https://github.com/n2geoff/uhm */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* App Builder
|
* App Builder
|
||||||
|
@ -129,20 +129,19 @@ var diff = {
|
||||||
* Composes state, actions, view together as
|
* Composes state, actions, view together as
|
||||||
* mountable ui
|
* mountable ui
|
||||||
*
|
*
|
||||||
|
* @param {String} mount element or querySelector value
|
||||||
* @param {Object} opts options bag of state, view, actions, and mount
|
* @param {Object} opts options bag of state, view, actions, and mount
|
||||||
* @param {Object} opts.state initial app object state
|
* @param {Object} opts.state initial app object state
|
||||||
* @param {Function} opts.view function that returns dom. state and actions are passed in
|
* @param {Function} opts.view function that returns dom. state and actions are passed in
|
||||||
* @param {Object} opts.actions object functions includes and return state
|
* @param {Object} opts.actions object functions includes and return state
|
||||||
* @param {String} opts.mount querySelector value
|
|
||||||
*
|
*
|
||||||
* @returns {Object} state and update() interface
|
* @returns {Object} state and update() interface
|
||||||
*/
|
*/
|
||||||
function app(opts) {
|
function app(mount = 'body', opts = {}) {
|
||||||
// initial setup
|
// initial setup
|
||||||
const state = opts.state || {};
|
const state = opts.state || {};
|
||||||
const view = opts.view || (() => null);
|
const view = opts.view || (() => null);
|
||||||
const actions = opts.actions || {};
|
const actions = opts.actions || {};
|
||||||
const mount = opts.mount || 'body';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assigns Dispatch-able Actions into App
|
* Assigns Dispatch-able Actions into App
|
||||||
|
@ -168,7 +167,7 @@ function app(opts) {
|
||||||
|
|
||||||
/** update dom */
|
/** update dom */
|
||||||
const update = () => {
|
const update = () => {
|
||||||
const parentNode = document.querySelector(mount);
|
const parentNode = typeof mount === 'string' ? document.querySelector(mount) : mount;
|
||||||
let result = view(state, actions);
|
let result = view(state, actions);
|
||||||
|
|
||||||
// handle multiple nodes
|
// handle multiple nodes
|
||||||
|
@ -265,8 +264,7 @@ function h(tagName, props, ...children) {
|
||||||
return el;
|
return el;
|
||||||
}
|
}
|
||||||
|
|
||||||
// source: https://github.com/dy/xhtm | MIT
|
/*! xhtm v1.6.2 | MIT LICENSE | https://github.com/dy/xhtm */
|
||||||
|
|
||||||
const FIELD = '\ue000', QUOTES = '\ue001';
|
const FIELD = '\ue000', QUOTES = '\ue001';
|
||||||
|
|
||||||
function htm (statics) {
|
function htm (statics) {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
/*! Emerj v1.0.0 | MIT LICENSE | https://github.com/bryhoyt/emerj */
|
/*! Emerj v1.0.0 | MIT LICENSE | https://github.com/bryhoyt/emerj */
|
||||||
var diff={attrs(elem){const attrs={};for(let i=0;i<elem.attributes.length;i++){const attr=elem.attributes[i];attrs[attr.name]=attr.value}return attrs},nodesByKey(parent,makeKey){const map={};for(let j=0;j<parent.childNodes.length;j++){const key=makeKey(parent.childNodes[j]);key&&(map[key]=parent.childNodes[j])}return map},merge(base,modified,opts){if((opts=opts||{}).key=opts.key||(node=>node.id),"string"==typeof modified){const html=modified;(modified=document.createElement(base.tagName)).innerHTML=html}const nodesByKey={old:this.nodesByKey(base,opts.key),new:this.nodesByKey(modified,opts.key)};let idx;for(idx=0;modified.firstChild;idx++){const newNode=modified.removeChild(modified.firstChild);if(idx>=base.childNodes.length){base.appendChild(newNode);continue}let baseNode=base.childNodes[idx];const newKey=opts.key(newNode);if(opts.key(baseNode)||newKey){const match=newKey&&newKey in nodesByKey.old?nodesByKey.old[newKey]:newNode;match!==baseNode&&(baseNode=base.insertBefore(match,baseNode))}if(baseNode.nodeType!==newNode.nodeType||baseNode.tagName!==newNode.tagName)base.replaceChild(newNode,baseNode);else if([Node.TEXT_NODE,Node.COMMENT_NODE].indexOf(baseNode.nodeType)>=0){if(baseNode.textContent===newNode.textContent)continue;baseNode.textContent=newNode.textContent}else if(baseNode!==newNode){const attrs={base:this.attrs(baseNode),new:this.attrs(newNode)};for(const attr in attrs.base)attr in attrs.new||baseNode.removeAttribute(attr);for(const attr in attrs.new)attr in attrs.base&&attrs.base[attr]===attrs.new[attr]||baseNode.setAttribute(attr,attrs.new[attr]);this.merge(baseNode,newNode)}}for(;base.childNodes.length>idx;)base.removeChild(base.lastChild)}};
|
var diff={attrs(elem){const attrs={};for(let i=0;i<elem.attributes.length;i++){const attr=elem.attributes[i];attrs[attr.name]=attr.value}return attrs},nodesByKey(parent,makeKey){const map={};for(let j=0;j<parent.childNodes.length;j++){const key=makeKey(parent.childNodes[j]);key&&(map[key]=parent.childNodes[j])}return map},merge(base,modified,opts){if((opts=opts||{}).key=opts.key||(node=>node.id),"string"==typeof modified){const html=modified;(modified=document.createElement(base.tagName)).innerHTML=html}const nodesByKey={old:this.nodesByKey(base,opts.key),new:this.nodesByKey(modified,opts.key)};let idx;for(idx=0;modified.firstChild;idx++){const newNode=modified.removeChild(modified.firstChild);if(idx>=base.childNodes.length){base.appendChild(newNode);continue}let baseNode=base.childNodes[idx];const newKey=opts.key(newNode);if(opts.key(baseNode)||newKey){const match=newKey&&newKey in nodesByKey.old?nodesByKey.old[newKey]:newNode;match!==baseNode&&(baseNode=base.insertBefore(match,baseNode))}if(baseNode.nodeType!==newNode.nodeType||baseNode.tagName!==newNode.tagName)base.replaceChild(newNode,baseNode);else if([Node.TEXT_NODE,Node.COMMENT_NODE].indexOf(baseNode.nodeType)>=0){if(baseNode.textContent===newNode.textContent)continue;baseNode.textContent=newNode.textContent}else if(baseNode!==newNode){const attrs={base:this.attrs(baseNode),new:this.attrs(newNode)};for(const attr in attrs.base)attr in attrs.new||baseNode.removeAttribute(attr);for(const attr in attrs.new)attr in attrs.base&&attrs.base[attr]===attrs.new[attr]||baseNode.setAttribute(attr,attrs.new[attr]);this.merge(baseNode,newNode)}}for(;base.childNodes.length>idx;)base.removeChild(base.lastChild)}};
|
||||||
/*! Uhm v0.7.0 | MIT LICENSE | https://github.com/n2geoff/uhm */function app(opts){const state=opts.state||{},view=opts.view||(()=>null),actions=opts.actions||{},mount=opts.mount||"body";const update=()=>{const parentNode=document.querySelector(mount);let result=view(state,actions);if(Array.isArray(result)){const fragment=document.createDocumentFragment();fragment.append(...result.filter((node=>null!=node))),result=fragment}else if("string"==typeof result){const temp=document.createElement(parentNode.tagName);temp.innerHTML=result,result=temp}diff.merge(parentNode,result)};return opts.view&&mount&&function(data,actions){Object.entries(actions).forEach((([name,action])=>{"function"==typeof action&&(actions[name]=(...args)=>{Object.assign(state,action(data,...args)),setTimeout((()=>update()),20)})})),update()}(state,actions),{state:state,update:update}}function h(tagName,props,...children){const el=tagName===DocumentFragment?document.createDocumentFragment():document.createElement(tagName),booleanAttrs=["disabled","checked","selected","hidden","readonly","required","open","autoplay","loop","muted"];if(null==props||"object"!=typeof props||Array.isArray(props))null!=props&&children.unshift(props);else for(const[key,value]of Object.entries(props))null!=value&&(booleanAttrs.includes(key)?!0===value?(el.setAttribute(key,""),el[key]=!0):!1===value&&(el.removeAttribute(key),el[key]=!1):key.startsWith("on")&&"function"==typeof value?el.addEventListener(key.slice(2).toLowerCase(),value):"class"!==key?"style"!==key||"object"!=typeof value?(el.setAttribute(key,value),key in el&&(el[key]=value)):Object.assign(el.style,value):el.className=value);for(const child of children)if(null!=child)if("string"==typeof(value=child)||"number"==typeof value)el.appendChild(document.createTextNode(child));else if(Array.isArray(child)){const fragment=document.createDocumentFragment();fragment.append(...child.filter((c=>null!=c))),el.appendChild(fragment)}else child instanceof Node?el.appendChild(child):"boolean"==typeof child?console.warn(`Boolean child ${child} passed to h() for tag "${tagName}". Booleans are not rendered.`):console.error(`Unsupported child type: ${typeof child} for tag "${tagName}" in h() function`);var value;return el}const FIELD="",QUOTES="";function htm(statics){let args,name,value,last,h=this,prev=0,current=[null],field=0,quotes=[],quote=0,level=0,pre=!1;const evaluate=(str,parts=[],raw)=>{let i=0;return(str=raw||""!==str?str.replace(/\ue001/g,(m=>quotes[quote++])):quotes[quote++].slice(1,-1))?(str.replace(/\ue000/g,((match,idx)=>(idx&&parts.push(str.slice(i,idx)),i=idx+1,parts.push(arguments[++field])))),i<str.length&&parts.push(str.slice(i)),parts.length>1?parts:parts[0]):str},up=()=>{[current,last,...args]=current,current.push(h(last,...args)),pre===level--&&(pre=!1)};return statics.join("").replace(/<!--[^]*?-->/g,"").replace(/<!\[CDATA\[[^]*\]\]>/g,"").replace(/('|")[^\1]*?\1/g,(match=>(quotes.push(match),""))).replace(/(?:^|>)((?:[^<]|<[^\w\ue000\/?!>])*)(?:$|<)/g,((match,text,idx,str)=>{let tag,close;if(idx&&str.slice(prev,idx).replace(/(\S)\/$/,"$1 /").split(/\s+/).map(((part,i)=>{if("/"===part[0]){if(part=part.slice(1),EMPTY[part])return;close=tag||part||1}else if(i){if(part){let props=current[2]||(current[2]={});"..."===part.slice(0,3)?Object.assign(props,arguments[++field]):([name,value]=part.split("="),Array.isArray(value=props[evaluate(name)]=!value||evaluate(value))&&(value.toString=value.join.bind(value,"")))}}else{if(tag=evaluate(part),"string"==typeof tag)for(tag=tag.toLowerCase();CLOSE[current[1]+tag];)up();current=[current,tag,null],level++,!pre&&PRE[tag]&&(pre=level),EMPTY[tag]&&(close=tag)}})),close)for(current[0]||err(`Wrong close tag \`${close}\``),up();last!==close&&CLOSE[last];)up();prev=idx+match.length,pre||(text=text.replace(/\s*\n\s*/g,"").replace(/\s+/g," ")),text&&evaluate((last=0,text),current,!0)})),current[0]&&CLOSE[current[1]]&&up(),level&&err(`Unclosed \`${current[1]}\`.`),current.length<3?current[1]:(current.shift(),current)}const err=msg=>{throw SyntaxError(msg)},EMPTY=htm.empty={},CLOSE=htm.close={},PRE=htm.pre={},html=htm.bind(h);export{app,h,html};
|
/*! Uhm v0.8.0 | MIT LICENSE | https://github.com/n2geoff/uhm */function app(mount="body",opts={}){const state=opts.state||{},view=opts.view||(()=>null),actions=opts.actions||{};const update=()=>{const parentNode="string"==typeof mount?document.querySelector(mount):mount;let result=view(state,actions);if(Array.isArray(result)){const fragment=document.createDocumentFragment();fragment.append(...result.filter((node=>null!=node))),result=fragment}else if("string"==typeof result){const temp=document.createElement(parentNode.tagName);temp.innerHTML=result,result=temp}diff.merge(parentNode,result)};return opts.view&&mount&&function(data,actions){Object.entries(actions).forEach((([name,action])=>{"function"==typeof action&&(actions[name]=(...args)=>{Object.assign(state,action(data,...args)),setTimeout((()=>update()),20)})})),update()}(state,actions),{state:state,update:update}}function h(tagName,props,...children){const el=tagName===DocumentFragment?document.createDocumentFragment():document.createElement(tagName),booleanAttrs=["disabled","checked","selected","hidden","readonly","required","open","autoplay","loop","muted"];if(null==props||"object"!=typeof props||Array.isArray(props))null!=props&&children.unshift(props);else for(const[key,value]of Object.entries(props))null!=value&&(booleanAttrs.includes(key)?!0===value?(el.setAttribute(key,""),el[key]=!0):!1===value&&(el.removeAttribute(key),el[key]=!1):key.startsWith("on")&&"function"==typeof value?el.addEventListener(key.slice(2).toLowerCase(),value):"class"!==key?"style"!==key||"object"!=typeof value?(el.setAttribute(key,value),key in el&&(el[key]=value)):Object.assign(el.style,value):el.className=value);for(const child of children)if(null!=child)if("string"==typeof(value=child)||"number"==typeof value)el.appendChild(document.createTextNode(child));else if(Array.isArray(child)){const fragment=document.createDocumentFragment();fragment.append(...child.filter((c=>null!=c))),el.appendChild(fragment)}else child instanceof Node?el.appendChild(child):"boolean"==typeof child?console.warn(`Boolean child ${child} passed to h() for tag "${tagName}". Booleans are not rendered.`):console.error(`Unsupported child type: ${typeof child} for tag "${tagName}" in h() function`);var value;return el}
|
||||||
|
/*! xhtm v1.6.2 | MIT LICENSE | https://github.com/dy/xhtm */const FIELD="",QUOTES="";function htm(statics){let args,name,value,last,h=this,prev=0,current=[null],field=0,quotes=[],quote=0,level=0,pre=!1;const evaluate=(str,parts=[],raw)=>{let i=0;return(str=raw||""!==str?str.replace(/\ue001/g,(m=>quotes[quote++])):quotes[quote++].slice(1,-1))?(str.replace(/\ue000/g,((match,idx)=>(idx&&parts.push(str.slice(i,idx)),i=idx+1,parts.push(arguments[++field])))),i<str.length&&parts.push(str.slice(i)),parts.length>1?parts:parts[0]):str},up=()=>{[current,last,...args]=current,current.push(h(last,...args)),pre===level--&&(pre=!1)};return statics.join("").replace(/<!--[^]*?-->/g,"").replace(/<!\[CDATA\[[^]*\]\]>/g,"").replace(/('|")[^\1]*?\1/g,(match=>(quotes.push(match),""))).replace(/(?:^|>)((?:[^<]|<[^\w\ue000\/?!>])*)(?:$|<)/g,((match,text,idx,str)=>{let tag,close;if(idx&&str.slice(prev,idx).replace(/(\S)\/$/,"$1 /").split(/\s+/).map(((part,i)=>{if("/"===part[0]){if(part=part.slice(1),EMPTY[part])return;close=tag||part||1}else if(i){if(part){let props=current[2]||(current[2]={});"..."===part.slice(0,3)?Object.assign(props,arguments[++field]):([name,value]=part.split("="),Array.isArray(value=props[evaluate(name)]=!value||evaluate(value))&&(value.toString=value.join.bind(value,"")))}}else{if(tag=evaluate(part),"string"==typeof tag)for(tag=tag.toLowerCase();CLOSE[current[1]+tag];)up();current=[current,tag,null],level++,!pre&&PRE[tag]&&(pre=level),EMPTY[tag]&&(close=tag)}})),close)for(current[0]||err(`Wrong close tag \`${close}\``),up();last!==close&&CLOSE[last];)up();prev=idx+match.length,pre||(text=text.replace(/\s*\n\s*/g,"").replace(/\s+/g," ")),text&&evaluate((last=0,text),current,!0)})),current[0]&&CLOSE[current[1]]&&up(),level&&err(`Unclosed \`${current[1]}\`.`),current.length<3?current[1]:(current.shift(),current)}const err=msg=>{throw SyntaxError(msg)},EMPTY=htm.empty={},CLOSE=htm.close={},PRE=htm.pre={},html=htm.bind(h);export{app,h,html};
|
||||||
//# sourceMappingURL=true
|
//# sourceMappingURL=true
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue