Comparing version 0.0.11 to 0.0.12
@@ -29,9 +29,43 @@ function forEach(item, fn) { | ||
class EventEmitter extends EventTarget { | ||
on(event, fn, opts) { | ||
this.addEventListener(event, (e) => { | ||
fn(...e._args); | ||
}, opts); | ||
} | ||
off(event, fn) { | ||
this.removeEventListener(event, fn); | ||
} | ||
once(event, fn) { | ||
this.on(event, fn, {once: true}); | ||
} | ||
emit(event, ...args) { | ||
const e = new Event(event); | ||
e._args = args; | ||
this.dispatchEvent(e); | ||
} | ||
} | ||
const $actions = new EventEmitter(); | ||
function $style(obj) { | ||
if (!obj) return ''; | ||
if (typeof obj === 'string') return obj; | ||
if (Array.isArray(obj)) { | ||
return obj.map($style).filter(Boolean).join(';'); | ||
} | ||
return Object.entries(obj).map(([key, val]) => { | ||
if (!val) return false; | ||
return `${key}: ${val}`; | ||
}).filter(Boolean).join('; '); | ||
}).filter(Boolean).join(';'); | ||
} | ||
function $class(obj) { | ||
if (!obj) return ''; | ||
if (Array.isArray(obj)) { | ||
return obj.map($class).filter(Boolean).join(' '); | ||
} | ||
return obj; | ||
} | ||
function $select(selector) { | ||
@@ -41,15 +75,12 @@ return document.querySelectorAll(selector); | ||
function $selectAllByClass(cls) { | ||
function $selectByClass(cls) { | ||
return document.getElementsByClassName(cls); | ||
} | ||
function $selectByClass(cls) { | ||
return $selectAllByClass(cls)[0]; | ||
} | ||
function _toArr(refs) { | ||
if (!refs) return []; | ||
if (typeof refs === 'string') { | ||
return $select(refs); | ||
} | ||
return Array.isArray(refs) ? refs : [refs]; | ||
return refs.length === undefined ? [refs] : refs; | ||
} | ||
@@ -125,4 +156,14 @@ | ||
function $navigate(url) { | ||
window.location = url; | ||
function $navigate(url, opts) { | ||
if (opts) { | ||
if (typeof opts === 'string') { | ||
opts = {target: opts}; | ||
} | ||
if (opts.target) { | ||
window.open(url, target); | ||
} | ||
} | ||
else { | ||
window.location = url; | ||
} | ||
} | ||
@@ -288,3 +329,6 @@ | ||
if (!el) return; | ||
return $jsUrl(ref.getAttribute(attr)); | ||
if (el._jjson === undefined) { | ||
el._jjson = $jsUrl(ref.getAttribute(attr)); | ||
} | ||
return el._jjson; | ||
} |
@@ -114,4 +114,5 @@ const parser = require('htmljs-parser'); | ||
let currentNode; | ||
let actualCurrentNode; | ||
function addNode(node) { | ||
function addNode(node, {isNode = true} = {}) { | ||
if (!currentNode) { | ||
@@ -124,3 +125,4 @@ rootNodes.push(node); | ||
currentNode = node; | ||
if (isNode) currentNode = node; | ||
actualCurrentNode = node; | ||
} | ||
@@ -138,4 +140,3 @@ | ||
addNode(node); | ||
currentNode = currentNode.parent; | ||
addNode(node, {isNode: false}); | ||
}, | ||
@@ -151,4 +152,3 @@ | ||
addNode(node); | ||
currentNode = currentNode.parent; | ||
addNode(node, {isNode: false}); | ||
}, | ||
@@ -164,4 +164,3 @@ | ||
addNode(node); | ||
currentNode = currentNode.parent; | ||
addNode(node, {isNode: false}); | ||
}, | ||
@@ -178,4 +177,3 @@ | ||
addNode(node); | ||
currentNode = currentNode.parent; | ||
addNode(node, {isNode: false}); | ||
}, | ||
@@ -272,4 +270,15 @@ | ||
onText(e) { | ||
const value = e.value.trim(); | ||
if (!value) return; | ||
let value = e.value; | ||
if (/^ +$/.test(value)) { | ||
// preserve single space | ||
value = ' '; | ||
} | ||
else { | ||
value = value.trim(); | ||
if (!value) return; | ||
if (e.value.endsWith(' ') && actualCurrentNode && actualCurrentNode.type === 'placeholder') { | ||
// preserve space in case of ${placeholder} #text | ||
value = value + ' '; | ||
} | ||
} | ||
@@ -280,7 +289,7 @@ const node = new DomNode(); | ||
node.value = value; | ||
node.originalValue = e.value; | ||
node.children = []; | ||
node.attributes = []; | ||
addNode(node); | ||
currentNode = currentNode.parent; | ||
addNode(node, {isNode: false}); | ||
}, | ||
@@ -297,2 +306,11 @@ | ||
if ( | ||
actualCurrentNode && | ||
actualCurrentNode.type === 'text' && | ||
actualCurrentNode.originalValue.startsWith(' ') | ||
) { | ||
// preserve space in case of #text ${placeholder} | ||
actualCurrentNode.value = ' ' + actualCurrentNode.value; | ||
} | ||
const node = new DomNode(); | ||
@@ -305,4 +323,3 @@ node.type = 'placeholder'; | ||
addNode(node); | ||
currentNode = currentNode.parent; | ||
addNode(node, {isNode: false}); | ||
}, | ||
@@ -321,4 +338,3 @@ | ||
addNode(node); | ||
currentNode = currentNode.parent; | ||
addNode(node, {isNode: false}); | ||
}, | ||
@@ -325,0 +341,0 @@ |
{ | ||
"name": "nixy", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "HTML templating framework focused on SSR and performance", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
50916
1775