-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.js
More file actions
181 lines (172 loc) · 5.03 KB
/
Copy pathview.js
File metadata and controls
181 lines (172 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* ==============================================
* VIEW — one spec, three (<=50 col house)
* targets: ASCII, React, React Native.
* ----------------------------------------------
* NEXT.md item 3. The ascii kits (ui.js,
* store-ui.js) already prove
* state -> view-tree -> string. This module
* generalizes the middle: components return a
* NEUTRAL VIEW NODE —
*
* { kind, props, children }
*
* — and that node IS the DSL (Domain-Specific
* Language). Three thin backends walk it:
*
* toAscii(node) -> string
* toReact(node, world) -> element tree
* toReactNative(node, world) -> element tree
*
* The DSL starts deliberately tiny — box, text,
* row, button — and grows only as screens
* demand. An unknown kind THROWS: the set is
* closed, same doctrine as platform.js.
*
* No React in this repo. The React backends
* take an injected `world` ({ createElement,
* dispatch, View, Text, Pressable }) — the
* same "control the world" move as deps.js, so
* tests fake the framework and CI never needs
* npm install. A button's `action` prop is a
* plain ACES action; pressing it just calls
* world.dispatch(action). The view layer never
* mutates anything — it asks, like react().
* ============================================ */
'use strict';
// -- constructors: the whole vocabulary --------
const node = (kind, props, children) =>
({ kind, props: props || {},
children: children || [] });
const text = value =>
node('text', { value });
const button = (label, action) =>
node('button', { label, action });
const row = (...children) =>
node('row', null, children);
const box = (title, ...children) =>
node('box', { title }, children);
function unknown(n) {
return new Error(
'unknown view kind: ' + n.kind);
}
/* -- backend 1: ASCII --------------------------
* Every node renders to an array of lines;
* row zips arrays side by side, box frames
* them. Pure string math, zero I/O.
*/
const pad = (s, w) =>
s + ' '.repeat(w - s.length);
const widest = ls => Math.max(
0, ...ls.map(l => l.length));
function lines(n) {
switch (n.kind) {
case 'text':
return [String(n.props.value)];
case 'button':
return ['[ ' + n.props.label + ' ]'];
case 'row': {
const cols = n.children.map(lines);
const h = Math.max(
0, ...cols.map(c => c.length));
const ws = cols.map(widest);
const out = [];
for (let i = 0; i < h; i++)
out.push(cols.map((c, j) =>
pad(c[i] || '', ws[j]))
.join(' ').trimEnd());
return out;
}
case 'box': {
const inner =
n.children.flatMap(lines);
const t = n.props.title;
const head = t ? '- ' + t + ' ' : '';
const w = Math.max(widest(inner),
head.length + 2);
return [
'.-' + head +
'-'.repeat(w - head.length) + '-.',
...inner.map(l =>
'| ' + pad(l, w) + ' |'),
"'-" + '-'.repeat(w) + "-'",
];
}
default: throw unknown(n);
}
}
const toAscii = n => lines(n).join('\n');
/* -- backend 2: React (DOM) --------------------
* world = { createElement, dispatch }
*/
function toReact(n, world) {
const ce = world.createElement;
const kids = n.children.map(
c => toReact(c, world));
switch (n.kind) {
case 'text':
return ce('span', null,
String(n.props.value));
case 'button':
return ce('button', {
onClick: () =>
world.dispatch(n.props.action),
}, n.props.label);
case 'row':
return ce('div', { style: {
display: 'flex',
flexDirection: 'row',
gap: 8 } }, ...kids);
case 'box':
return ce('div', { style: {
border: '1px solid currentColor',
padding: 8 } },
n.props.title
? ce('strong', null, n.props.title)
: null,
...kids);
default: throw unknown(n);
}
}
/* -- backend 3: React Native -------------------
* world = { createElement, dispatch,
* View, Text, Pressable }
* RN has no div/span: raw strings must live
* inside <Text>, presses use <Pressable>. Those
* three components are injected, never imported.
*/
function toReactNative(n, world) {
const ce = world.createElement;
const kids = n.children.map(
c => toReactNative(c, world));
switch (n.kind) {
case 'text':
return ce(world.Text, null,
String(n.props.value));
case 'button':
return ce(world.Pressable, {
onPress: () =>
world.dispatch(n.props.action),
}, ce(world.Text, null,
n.props.label));
case 'row':
return ce(world.View, { style: {
flexDirection: 'row',
gap: 8 } }, ...kids);
case 'box':
return ce(world.View, { style: {
borderWidth: 1,
padding: 8 } },
n.props.title
? ce(world.Text,
{ style: {
fontWeight: 'bold' } },
n.props.title)
: null,
...kids);
default: throw unknown(n);
}
}
module.exports = {
node, text, button, row, box,
toAscii, toReact, toReactNative,
};