@openbooknetwork/hesap 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +112 -2
- package/package.json +45 -4
- package/scripts/assemble.mjs +40 -0
- package/src/client.css +186 -0
- package/src/client.js +111 -0
- package/src/families/counters/bonds.js +414 -0
- package/src/families/counters/client.css +96 -0
- package/src/families/counters/client.js +21 -0
- package/src/families/counters/counter.js +32 -0
- package/src/families/counters/driver.js +16 -0
- package/src/families/counters/figures.js +172 -0
- package/src/families/counters/frame.js +28 -0
- package/src/families/counters/goals.js +75 -0
- package/src/families/counters/i18n/en.yaml +52 -0
- package/src/families/counters/i18n/tr.yaml +52 -0
- package/src/families/counters/index.mjs +103 -0
- package/src/families/counters/model.js +86 -0
- package/src/families/counters/move.js +42 -0
- package/src/families/counters/places.js +45 -0
- package/src/families.mjs +5 -0
- package/src/i18n/en.yaml +67 -0
- package/src/i18n/tr.yaml +67 -0
- package/src/index.mjs +82 -0
- package/src/kit/dom.js +32 -0
- package/src/kit/drag.js +40 -0
- package/src/kit/format.js +34 -0
- package/src/kit/goal.js +49 -0
- package/src/kit/i18n/en.yaml +15 -0
- package/src/kit/i18n/tr.yaml +15 -0
- package/src/kit/kit.css +89 -0
- package/src/kit/motion.js +44 -0
- package/src/kit/panel.js +70 -0
- package/src/kit/play.js +26 -0
- package/src/kit/random.js +35 -0
- package/src/kit/text.js +25 -0
- package/src/node.mjs +43 -0
package/src/kit/panel.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// The controls and readouts docked under a figure. Every family builds its panel from these parts, so a slider, a
|
|
2
|
+
// chip and a button look and behave the same on every page.
|
|
3
|
+
import { h, icon } from './dom.js';
|
|
4
|
+
import { math } from './text.js';
|
|
5
|
+
|
|
6
|
+
// slider is a labelled range input with its value written beside it. write turns the number into words, input runs
|
|
7
|
+
// while the reader drags, and change runs when the reader lets go. A slider with no value to read, such as time,
|
|
8
|
+
// passes output: false, and its value is still spoken.
|
|
9
|
+
export function slider({ id, label, min, max, step = 1, value, write = String, input, change, output = true, cls = '' }) {
|
|
10
|
+
const range = h('input', { type: 'range', min, max, step }), out = h('output', { hidden: !output });
|
|
11
|
+
const row = h('label', { class: `hesap-ctl ${cls}`.trim(), 'data-ref': id }, [h('span', {}, label), range, out]);
|
|
12
|
+
const show = v => { const words = write(v); out.textContent = words; range.setAttribute('aria-valuetext', words); };
|
|
13
|
+
const set = v => { range.value = v; show(Number(range.value)); };
|
|
14
|
+
range.addEventListener('input', () => { show(Number(range.value)); input?.(Number(range.value)); });
|
|
15
|
+
range.addEventListener('change', () => change?.(Number(range.value)));
|
|
16
|
+
set(value);
|
|
17
|
+
// width keeps the value's room steady while it changes, counted in characters.
|
|
18
|
+
return { el: row, range, set, show, get: () => Number(range.value), width: ch => { out.style.minWidth = `${ch}ch`; } };
|
|
19
|
+
}
|
|
20
|
+
// chip is a readout: a name and a value that follows the figure. A chip the reader can press is a button. swatch
|
|
21
|
+
// is a class for a dot of the colour the figure draws the measure in, and short is a name for a phone.
|
|
22
|
+
export function chip({ id, label, short, swatch, press, cls = '' }) {
|
|
23
|
+
const small = h('small', {}, short ? [h('span', { class: 'hesap-long' }, label), h('span', { class: 'hesap-short' }, short)] : label ?? ''), b = h('b');
|
|
24
|
+
const dot = swatch ? [h('i', { class: `hesap-swatch ${swatch}`, 'aria-hidden': 'true' })] : [];
|
|
25
|
+
const el = h(press ? 'button' : 'span', { class: `hesap-chip ${cls}`.trim(), 'data-ref': id, type: press ? 'button' : null }, label === null ? [...dot, b] : [...dot, small, b]);
|
|
26
|
+
if (press) el.addEventListener('click', () => press(id));
|
|
27
|
+
return {
|
|
28
|
+
el, id, value: b,
|
|
29
|
+
// set writes a new value and says whether it changed, so the family can mark the change.
|
|
30
|
+
set(text) { if (b.textContent === text) return false; const first = !b.textContent; b.textContent = text; return !first; },
|
|
31
|
+
// name writes the readout's name. A chip with a short name keeps it, and takes a new one as the second word.
|
|
32
|
+
name(text, brief) {
|
|
33
|
+
const long = small.querySelector('.hesap-long');
|
|
34
|
+
if (!long) { if (small.textContent !== text) small.textContent = text; return; }
|
|
35
|
+
long.textContent = text;
|
|
36
|
+
if (brief !== undefined) small.querySelector('.hesap-short').textContent = brief;
|
|
37
|
+
},
|
|
38
|
+
// tex sets the value as a formula.
|
|
39
|
+
tex(source, options) { math(b, source, false, options); },
|
|
40
|
+
width: ch => { b.style.minWidth = `${ch}ch`; },
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
// button is an action. A primary button starts something, and at most one stands on a page. A compact button with
|
|
44
|
+
// a glyph shows the glyph alone on a phone and keeps its label as its name.
|
|
45
|
+
export function button({ label, act, press, primary = false, glyph = null, compact = false, cls = '' }) {
|
|
46
|
+
const el = h('button', { type: 'button', class: `hesap-btn${primary ? ' hesap-primary' : ''}${compact && glyph ? ' hesap-compact' : ''} ${cls}`.trim(), 'data-act': act, 'aria-label': compact ? label : null, title: compact ? label : null }, [...(glyph ? [icon(glyph)] : []), h('span', {}, label)]);
|
|
47
|
+
if (press) el.addEventListener('click', press);
|
|
48
|
+
return el;
|
|
49
|
+
}
|
|
50
|
+
// segment is one choice out of a few, each a button. items are [value, words] pairs.
|
|
51
|
+
export function segment({ id, label, items, value, pick }) {
|
|
52
|
+
const buttons = items.map(([v, words]) => h('button', { type: 'button', 'data-v': v }, words));
|
|
53
|
+
const el = h('div', { class: 'hesap-seg', role: 'group', 'aria-label': label, 'data-ref': id }, [...(label ? [h('span', { class: 'hesap-seg-name' }, label)] : []), ...buttons]);
|
|
54
|
+
const set = v => { value = v; buttons.forEach((b, i) => b.setAttribute('aria-pressed', String(items[i][0]) === String(v))); };
|
|
55
|
+
buttons.forEach((b, i) => b.addEventListener('click', () => { if (String(items[i][0]) === String(value)) return; set(items[i][0]); pick(items[i][0]); }));
|
|
56
|
+
set(value);
|
|
57
|
+
return { el, set, get: () => value };
|
|
58
|
+
}
|
|
59
|
+
// stepper is a whole number with a button on each side to take one away or add one.
|
|
60
|
+
export function stepper({ id, label, min, max, step = 1, value, write = String, change }) {
|
|
61
|
+
const out = h('output'), less = h('button', { type: 'button', 'aria-label': `${label} −` }, '−'), more = h('button', { type: 'button', 'aria-label': `${label} +` }, '+');
|
|
62
|
+
const el = h('div', { class: 'hesap-step', role: 'group', 'aria-label': label, 'data-ref': id }, [h('span', {}, label), less, out, more]);
|
|
63
|
+
const set = v => { value = Math.max(min, Math.min(max, v)); out.textContent = write(value); less.disabled = value <= min; more.disabled = value >= max; };
|
|
64
|
+
less.addEventListener('click', () => { set(value - step); change(value); });
|
|
65
|
+
more.addEventListener('click', () => { set(value + step); change(value); });
|
|
66
|
+
set(value);
|
|
67
|
+
return { el, set, get: () => value };
|
|
68
|
+
}
|
|
69
|
+
// row groups parts of the panel: ctl for controls, out for readouts, acts for buttons.
|
|
70
|
+
export const row = (kind, parts) => h('div', { class: `hesap-${kind}` }, parts.filter(Boolean).map(p => p.el ?? p));
|
package/src/kit/play.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// A run that plays by itself. Start plays it once to its end, and a slider beside it goes back and forth through
|
|
2
|
+
// the same run. There is no Pause: a reader who drags the slider takes over.
|
|
3
|
+
// player drives time t from 0 to 1 over duration milliseconds. at draws the run at t, and now reads where the run
|
|
4
|
+
// stands. ease maps the share of time that has passed to t.
|
|
5
|
+
export function player({ duration, at, now, moving, ease = x => x, onState }) {
|
|
6
|
+
let raf = 0;
|
|
7
|
+
const state = on => onState?.(on);
|
|
8
|
+
function halt() { if (!raf) return; cancelAnimationFrame(raf); raf = 0; state(false); }
|
|
9
|
+
// A run that has ended plays again from the beginning, and again runs first to let the family draw a new one.
|
|
10
|
+
function start(again) {
|
|
11
|
+
halt();
|
|
12
|
+
let from = now();
|
|
13
|
+
if (from >= 1) { again?.(); from = 0; }
|
|
14
|
+
if (!moving()) return at(1);
|
|
15
|
+
const span = duration * (1 - from); let t0 = 0;
|
|
16
|
+
state(true);
|
|
17
|
+
const step = t => {
|
|
18
|
+
t0 ||= t;
|
|
19
|
+
const x = Math.min(1, (t - t0) / span);
|
|
20
|
+
at(from + (1 - from) * ease(x));
|
|
21
|
+
if (x < 1) raf = requestAnimationFrame(step); else { raf = 0; state(false); }
|
|
22
|
+
};
|
|
23
|
+
raf = requestAnimationFrame(step);
|
|
24
|
+
}
|
|
25
|
+
return { start, halt, playing: () => !!raf };
|
|
26
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Seeded random numbers. One seed always gives the same numbers, so a test replays a run and a build checks the
|
|
2
|
+
// challenges a page can draw. A reader's page takes a fresh seed each time.
|
|
3
|
+
|
|
4
|
+
// mulberry32 is small, fast and even enough for coins, dice and drawn challenges.
|
|
5
|
+
export function generator(seed) {
|
|
6
|
+
let a = seed >>> 0;
|
|
7
|
+
return () => {
|
|
8
|
+
a = a + 0x6D2B79F5 | 0;
|
|
9
|
+
let t = Math.imul(a ^ a >>> 15, 1 | a);
|
|
10
|
+
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
|
|
11
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// hash runs FNV-1a over a text, to make a page's own seed when its page.yaml names none.
|
|
16
|
+
export function hash(text) {
|
|
17
|
+
let h = 0x811c9dc5;
|
|
18
|
+
for (const c of String(text)) h = Math.imul(h ^ c.codePointAt(0), 0x01000193);
|
|
19
|
+
return h >>> 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// seedOf uses the page's seed for the first run and makes a new one for each run after it.
|
|
23
|
+
export const seedOf = (base, run) => run ? hash(`${base}:${run}`) : base >>> 0;
|
|
24
|
+
|
|
25
|
+
// fresh gives a seed no reader can predict, for a challenge drawn as its page opens.
|
|
26
|
+
export const fresh = () => (globalThis.crypto?.getRandomValues ? globalThis.crypto.getRandomValues(new Uint32Array(1))[0] : Math.floor(Math.random() * 4294967296)) >>> 0;
|
|
27
|
+
|
|
28
|
+
// dice wraps a seed in the draws a goal needs: a whole number in a range, one of a list, a list in a new order and
|
|
29
|
+
// a few of a list.
|
|
30
|
+
export function dice(seed) {
|
|
31
|
+
const next = generator(seed);
|
|
32
|
+
const int = (lo, hi) => lo + Math.floor(next() * (hi - lo + 1));
|
|
33
|
+
const shuffle = items => { const a = [...items]; for (let i = a.length - 1; i > 0; i--) { const j = int(0, i); [a[i], a[j]] = [a[j], a[i]]; } return a; };
|
|
34
|
+
return { next, int, pick: items => items[int(0, items.length - 1)], shuffle, some: (items, n) => shuffle(items).slice(0, n) };
|
|
35
|
+
}
|
package/src/kit/text.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// The values in a page's text follow the figure. A text links a value as [words](@id) and a worked formula as
|
|
2
|
+
// [words](@formula-id), and kitap leaves a span with data-ref in their place.
|
|
3
|
+
// live writes every link of the story. value gives the words of an id or null for a link the family does not own,
|
|
4
|
+
// and formula gives the TeX of a worked formula. tex gives the TeX of a value that is set as a formula in its line,
|
|
5
|
+
// such as a fraction, and null for a value written as words.
|
|
6
|
+
export function live(host, { value, formula, tex, katex = {} }) {
|
|
7
|
+
for (const span of host.el.story.querySelectorAll('.place[data-ref]')) {
|
|
8
|
+
const id = span.dataset.ref;
|
|
9
|
+
if (id.startsWith('formula-')) {
|
|
10
|
+
const tex = formula?.(id.slice(8));
|
|
11
|
+
if (tex == null || span.dataset.tex === tex) continue;
|
|
12
|
+
span.dataset.tex = tex; span.dataset.formula = ''; span.removeAttribute('tabindex');
|
|
13
|
+
window.katex.render(tex, span, { displayMode: true, throwOnError: false, ...katex });
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const source = tex?.(id);
|
|
17
|
+
if (source != null) { math(span, source, false, katex); span.dataset.live = ''; continue; }
|
|
18
|
+
const text = value(id);
|
|
19
|
+
if (text == null) continue;
|
|
20
|
+
if (span.textContent !== text) span.textContent = text;
|
|
21
|
+
span.dataset.live = '';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// math sets a short piece of TeX inside an element. options go to KaTeX, such as trust for \\htmlClass.
|
|
25
|
+
export const math = (node, tex, display = false, options = {}) => { if (node.dataset.tex !== tex) { node.dataset.tex = tex; window.katex.render(tex, node, { displayMode: display, throwOnError: false, ...options }); } return node; };
|
package/src/node.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// What every family's Node half shares: escaping, the link pattern of a text, the links object kitap asks for and
|
|
2
|
+
// the checks every challenge gets.
|
|
3
|
+
import { dice } from './kit/random.js';
|
|
4
|
+
|
|
5
|
+
export const esc = s => String(s ?? '').replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
6
|
+
export const list = ids => [...ids].join(', ');
|
|
7
|
+
// A text links a measure, a control or a worked formula as [words](@id).
|
|
8
|
+
export const LINK = /\[([^\]]+)\]\(@([^)\s]+)\)/g;
|
|
9
|
+
|
|
10
|
+
// links tells kitap which ids a page's text may link. has says whether an id exists, and offers names what to link.
|
|
11
|
+
export const links = (has, offers) => ({
|
|
12
|
+
has, attr: id => ` data-ref="${esc(id)}"`, names: () => [],
|
|
13
|
+
missing: (name, id) => `[${name}](@${id}) links to "${id}", link ${offers}`,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// challenge finds the goal a page names and fails a page whose goal is unknown or belongs to another scene. A goal
|
|
17
|
+
// lists the scenes it works on as scenes, and a goal without the list works on every scene of its family.
|
|
18
|
+
export function challenge(c, goals, { scene, at, ctx }) {
|
|
19
|
+
const goal = goals[c.goal];
|
|
20
|
+
if (!goal) { ctx.fail(at, `challenge.goal is "${c.goal}", the goals are ${list(Object.keys(goals))}`); return null; }
|
|
21
|
+
if (goal.scenes && !goal.scenes.includes(scene)) { ctx.fail(at, `challenge.goal ${c.goal} works on ${list(goal.scenes)}, this page shows ${scene}`); return null; }
|
|
22
|
+
return goal;
|
|
23
|
+
}
|
|
24
|
+
// drawn checks a goal whose target the page draws as it opens. It draws SAMPLES targets from seeds 1 to SAMPLES and
|
|
25
|
+
// fails the page when the goal has nothing to draw, when a draw is solved as the page opens, or when the page's
|
|
26
|
+
// controls cannot reach it. starts gives the page's state for a target, and the goal's reach names what stops a
|
|
27
|
+
// target from being reached.
|
|
28
|
+
const SAMPLES = 300;
|
|
29
|
+
export function drawn(goal, c, spec, { at, ctx, starts }) {
|
|
30
|
+
for (let seed = 1; seed <= SAMPLES; seed++) {
|
|
31
|
+
let target;
|
|
32
|
+
try { target = goal.draw(c, spec, dice(seed)); } catch (err) { return ctx.fail(at, `challenge.goal ${c.goal} cannot draw a target: ${err.message}`); }
|
|
33
|
+
if (target === null || target === undefined) return ctx.fail(at, `challenge.goal ${c.goal} draws nothing from what the page declares, widen its ranges`);
|
|
34
|
+
const state = starts(spec, target), words = JSON.stringify(target);
|
|
35
|
+
if (goal.holds(state, spec, c, target).done) return ctx.fail(at, `challenge.goal ${c.goal} can draw ${words}, which is reached as the page opens`);
|
|
36
|
+
const stop = goal.reach?.(spec, c, target);
|
|
37
|
+
if (stop) return ctx.fail(at, `challenge.goal ${c.goal} can draw ${words}, and ${stop}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// hidden fails a page whose readout shows a measure that gives the challenge's answer away.
|
|
41
|
+
export function hidden(readout, hides, at, ctx) {
|
|
42
|
+
for (const id of readout) if (hides.includes(id)) ctx.fail(at, `readout names "${id}", which gives the challenge's answer away, leave it out`);
|
|
43
|
+
}
|