Skip to content

Commit

Permalink
fix: errors
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBLT committed Jan 30, 2024
1 parent 65d9836 commit 4c5d12b
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 103 deletions.
2 changes: 1 addition & 1 deletion packages/@hec.js/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"main": "./lib/index.js",
"types": "./lib/types/index.d.ts",
"scripts": {
"types": "npx -p typescript tsc './lib/index.js' --declaration --allowJs --emitDeclarationOnly --outDir types && rm -rf ./lib/types && mv types ./lib/types"
"build:types": "npx -p typescript tsc './lib/index.js' --declaration --allowJs --emitDeclarationOnly --outDir types && rm -rf ./lib/types && mv types ./lib/types"
},
"dependencies": {
"@hec.js/api": "file:./",
Expand Down
2 changes: 1 addition & 1 deletion packages/@hec.js/ui/dist/hec.esm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/@hec.js/ui/dist/hec.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion packages/@hec.js/ui/lib/src/plugins/data-lazy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { notifyVisible } from '../notify/visible.js';
import { templateByNode } from '../template.js';
import { executeNodeAttributesTemplate, templateByNode } from '../template.js';

const loaded = new WeakSet();

Expand All @@ -19,6 +19,8 @@ export const dataLazyPlugin = {

const execute = () => {
node.removeAttribute('data-lazy');

executeNodeAttributesTemplate(node, props);

for (const child of node.childNodes) {
templateByNode(child, props);
Expand Down
207 changes: 109 additions & 98 deletions packages/@hec.js/ui/lib/src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,81 +79,6 @@ export function templateByString(template, props = {}) {
*/
export function templateByNode(template, props = {}) {

/**
* @param { string } text
* @param { function(string): void } update
*/
const bindExpressions = (text, update) => {

/** @type {import("./expression.js").Expression[]} */
let expressions = text.match(/{{[^}]+}}/g).map(expression),
sourceText = text;

const evaluate = () => {
text = sourceText;

for (const exp of expressions) {
const v = f(exp.value);
text = text.replace(exp.text, v ?? '<null>');
}

return text;
};

for (const exp of expressions) {
const value = prop(props, exp.prop);

if (value == undefined) {
console.warn(`{{ ${exp.prop} }}: No value for this key`, { key: exp.prop, value });
}

if (isSignal(value)) {
/** @type { import("./signal.js").Signal<any> } */
let signal = value;

for (const key in exp.meta) {
if (pipes[key]) {
const pipe = pipes[key];

signal = signal.map((v) =>
pipe({
value: v,
key: exp.prop,
param: exp.meta[key],
options: exp.meta,
})
);
}
}

exp.value = signal;

signal.subscribe({ next: () => update(evaluate()) });
} else {

let v = value;

for (const key in exp.meta) {

if (pipes[key]) {
const pipe = pipes[key];

v = pipe({
value: f(value),
key: exp.prop,
param: exp.meta[key],
options: exp.meta,
});
}
}

exp.value = () => v;
}
}

update(evaluate());
};

/**
* @param { Node } node
* @param { WeakSet<Node> } done
Expand All @@ -170,7 +95,7 @@ export function templateByNode(template, props = {}) {
}

if (node instanceof HTMLElement) {
const attributeNames = node.getAttributeNames();


setPropsOf(node, props);

Expand All @@ -180,30 +105,11 @@ export function templateByNode(template, props = {}) {
}
}

for (const attributeName of attributeNames) {
const attribute = node.getAttribute(attributeName);

if (attribute?.includes('{{')) {

bindExpressions(attribute, (text) => {
text = text.trim().replace(/ +/, ' ');

if (text === '<null>') {
node.removeAttribute(attributeName);
} else {
node.setAttribute(attributeName, text);
}

});

} else if (node.localName.includes('-') && hasProp(props, attribute)) {
node.setAttribute(attributeName, `@parent.${attribute}`);
}
}

if (stopFlag) {
return;
}

executeNodeAttributesTemplate(node, props);

} else if (node instanceof Text && node.textContent.includes('{{')) {
const parts = node.textContent.split(/{{|}}/g);
Expand All @@ -212,7 +118,7 @@ export function templateByNode(template, props = {}) {
text = document.createTextNode(parts[i]);

if ((i % 2) != 0) {
bindExpressions(`{{${parts[i]}}}`, (v) => text.data = v.replaceAll('<null>', ''));
bindExpressions(`{{${parts[i]}}}`, props, (v) => text.data = v.replaceAll('<null>', ''));
}

done.add(text);
Expand All @@ -234,3 +140,108 @@ export function templateByNode(template, props = {}) {

return template;
}

/**
* @param { HTMLElement } node
* @param { {[key: string]: any } } props
*/
export const executeNodeAttributesTemplate = (node, props) => {
const attributeNames = node.getAttributeNames();

for (const attributeName of attributeNames) {
const attribute = node.getAttribute(attributeName);

if (attribute?.includes('{{')) {

bindExpressions(attribute, props, (text) => {
text = text.trim().replace(/ +/, ' ');

if (text === '<null>') {
node.removeAttribute(attributeName);
} else {
node.setAttribute(attributeName, text);
}

});

} else if (node.localName.includes('-') && hasProp(props, attribute)) {
node.setAttribute(attributeName, `@parent.${attribute}`);
}
}
}

/**
* @param { string } text
* @param { {[key: string]: any } } props
* @param { function(string): void } update
*/
const bindExpressions = (text, props, update) => {

/** @type {import("./expression.js").Expression[]} */
let expressions = text.match(/{{[^}]+}}/g).map(expression),
sourceText = text;

const evaluate = () => {
text = sourceText;

for (const exp of expressions) {
const v = f(exp.value);
text = text.replace(exp.text, v ?? '<null>');
}

return text;
};

for (const exp of expressions) {
const value = prop(props, exp.prop);

if (value == undefined) {
console.warn(`{{ ${exp.prop} }}: No value for this key`, { key: exp.prop, value });
}

if (isSignal(value)) {
/** @type { import("./signal.js").Signal<any> } */
let signal = value;

for (const key in exp.meta) {
if (pipes[key]) {
const pipe = pipes[key];

signal = signal.map((v) =>
pipe({
value: v,
key: exp.prop,
param: exp.meta[key],
options: exp.meta,
})
);
}
}

exp.value = signal;

signal.subscribe({ next: () => update(evaluate()) });
} else {

let v = value;

for (const key in exp.meta) {

if (pipes[key]) {
const pipe = pipes[key];

v = pipe({
value: f(value),
key: exp.prop,
param: exp.meta[key],
options: exp.meta,
});
}
}

exp.value = () => v;
}
}

update(evaluate());
};
2 changes: 1 addition & 1 deletion packages/@hec.js/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"main": "./lib/index.js",
"scripts": {
"build": "esbuild --minify --format=esm --bundle ./lib/index.js > ./dist/hec.esm.min.js && esbuild --minify --bundle ./lib/global.js > ./dist/hec.min.js"
"build:esm": "esbuild --minify --format=esm --bundle ./lib/index.js > ./dist/hec.esm.min.js && esbuild --minify --bundle ./lib/global.js > ./dist/hec.min.js"
},
"dependencies": {
"@hec.js/ui": "file:./",
Expand Down

0 comments on commit 4c5d12b

Please sign in to comment.