Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/model #25

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function sprae(el, values) {
// NOTE: secondary directives don't stop flow nor extend state, so no need to check
for (let name of names) {
let dir = directive[name] || directive.default
let evaluate = (dir.parse || parse)(attr.value, parse)
let evaluate = (dir.parse || parse)(attr.value)
let dispose = dir(el, evaluate, state, name);
if (dispose) disposes.push(dispose);
}
Expand All @@ -73,7 +73,7 @@ export default function sprae(el, values) {

// compiler
const evalMemo = {};
const parse = (expr, dir, fn) => {
export const parse = (expr, dir, fn) => {
if (fn = evalMemo[expr = expr.trim()]) return fn

// static-time errors
Expand All @@ -85,7 +85,7 @@ const parse = (expr, dir, fn) => {
}

// wrapped call
export let err = (e, dir, expr = '') => {
export const err = (e, dir, expr = '') => {
throw Object.assign(e, { message: `∴ ${e.message}\n\n${dir}${expr ? `="${expr}"\n\n` : ""}`, expr })
}

Expand Down
4 changes: 2 additions & 2 deletions directive/each.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sprae, { directive, frag } from "../core.js";
import sprae, { directive, frag, parse } from "../core.js";
import store, { _change, _signals } from "../store.js";
import { effect, untracked, computed } from '../signal.js';

Expand Down Expand Up @@ -87,7 +87,7 @@ directive.each = (tpl, [itemVar, idxVar, evaluate], state) => {


// redefine parser to exclude `[a in] b`
directive.each.parse = (expr, parse) => {
directive.each.parse = (expr) => {
let [leftSide, itemsExpr] = expr.split(/\s+in\s+/);
let [itemVar, idxVar = "$"] = leftSide.split(/\s*,\s*/);

Expand Down
37 changes: 24 additions & 13 deletions directive/value.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
import { directive } from "../core.js";
import { directive, parse } from "../core.js";
import { attr } from './default.js';
import { effect } from "../signal.js";

// connect expr to element value
directive.value = (el, evaluate, state) => {
let from, to;
let update = el.type === "text" || el.type === ""
? (value) => el.setAttribute("value", (el.value = value == null ? "" : value))
: el.tagName === "TEXTAREA" || el.type === "text" || el.type === ""
? (value) =>
directive.value = (el, [getValue, setValue], state) => {
const update =
(el.type === "text" || el.type === "") ? (value) => el.setAttribute("value", (el.value = value == null ? "" : value))
: (el.tagName === "TEXTAREA" || el.type === "text" || el.type === "") ? (value, from, to) =>
(
// we retain selection in input
(from = el.selectionStart),
(to = el.selectionEnd),
el.setAttribute("value", (el.value = value == null ? "" : value)),
from && el.setSelectionRange(from, to)
)
: el.type === "checkbox"
? (value) => (el.checked = value, attr(el, "checked", value))
: el.type === "select-one"
? (value) => {
: (el.type === "checkbox") ? (value) => (el.checked = value, attr(el, "checked", value))
: (el.type === "select-one") ? (value) => {
for (let option in el.options) option.removeAttribute("selected");
el.value = value;
el.selectedOptions[0]?.setAttribute("selected", "");
}
: (value) => (el.value = value);
: (value) => (el.value = value);

return effect(() => (update(evaluate(state))));
// bind back
const handleChange = el.type === 'checkbox' ? e => setValue(state, el.checked) : e => setValue(state, el.value)
el.addEventListener('input', handleChange)
el.addEventListener('change', handleChange)

return effect(() => (update(getValue(state))));
};

directive.value.parse = expr => {
let evaluate = [parse(expr)]
try {
// for values like `123 = arguments[1]`, `foo?.bar = arguments[1]`
evaluate.push(parse(`${expr}=arguments[1];`))
}
catch (e) { }
return evaluate
}
26 changes: 18 additions & 8 deletions dist/sprae.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function sprae(el, values) {
let names = attr2.name.slice(1).split(":");
for (let name of names) {
let dir = directive[name] || directive.default;
let evaluate = (dir.parse || parse)(attr2.value, parse);
let evaluate = (dir.parse || parse)(attr2.value);
let dispose = dir(el2, evaluate, state, name);
if (dispose)
disposes.push(dispose);
Expand Down Expand Up @@ -363,10 +363,10 @@ directive.each = (tpl, [itemVar, idxVar, evaluate], state) => {
planned++;
});
};
directive.each.parse = (expr, parse2) => {
directive.each.parse = (expr) => {
let [leftSide, itemsExpr] = expr.split(/\s+in\s+/);
let [itemVar, idxVar = "$"] = leftSide.split(/\s*,\s*/);
return [itemVar, idxVar, parse2(itemsExpr)];
return [itemVar, idxVar, parse(itemsExpr)];
};

// directive/ref.js
Expand Down Expand Up @@ -594,15 +594,25 @@ var dashcase = (str) => {
};

// directive/value.js
directive.value = (el, evaluate, state) => {
let from, to;
let update = el.type === "text" || el.type === "" ? (value) => el.setAttribute("value", el.value = value == null ? "" : value) : el.tagName === "TEXTAREA" || el.type === "text" || el.type === "" ? (value) => (from = el.selectionStart, to = el.selectionEnd, el.setAttribute("value", el.value = value == null ? "" : value), from && el.setSelectionRange(from, to)) : el.type === "checkbox" ? (value) => (el.checked = value, attr(el, "checked", value)) : el.type === "select-one" ? (value) => {
directive.value = (el, [getValue, setValue], state) => {
const update = el.type === "text" || el.type === "" ? (value) => el.setAttribute("value", el.value = value == null ? "" : value) : el.tagName === "TEXTAREA" || el.type === "text" || el.type === "" ? (value, from, to) => (from = el.selectionStart, to = el.selectionEnd, el.setAttribute("value", el.value = value == null ? "" : value), from && el.setSelectionRange(from, to)) : el.type === "checkbox" ? (value) => (el.checked = value, attr(el, "checked", value)) : el.type === "select-one" ? (value) => {
for (let option in el.options)
option.removeAttribute("selected");
el.value = value;
el.selectedOptions[0]?.setAttribute("selected", "");
} : (value) => el.value = value;
return effect(() => update(evaluate(state)));
const handleChange = el.type === "checkbox" ? (e) => setValue(state, el.checked) : (e) => setValue(state, el.value);
el.addEventListener("input", handleChange);
el.addEventListener("change", handleChange);
return effect(() => update(getValue(state)));
};
directive.value.parse = (expr) => {
let evaluate = [parse(expr)];
try {
evaluate.push(parse(`${expr}=arguments[1];`));
} catch (e) {
}
return evaluate;
};

// directive/fx.js
Expand All @@ -612,7 +622,7 @@ directive.fx = (el, evaluate, state) => {

// sprae.js
sprae.use(ulive_es_exports);
sprae.use({ compile: (expr) => sprae.constructor(`__scope`, `with (__scope) { return ${expr} };`) });
sprae.use({ compile: (expr) => sprae.constructor(`with (arguments[0]) { return ${expr} };`) });
var sprae_default = sprae;
export {
sprae_default as default
Expand Down
1 change: 0 additions & 1 deletion dist/sprae.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading