skilltree-lab 0.1.0__py3-none-any.whl
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.
- skilltree_lab/__init__.py +0 -0
- skilltree_lab/cli.py +82 -0
- skilltree_lab/data/LICENSE.d3-celestial +12 -0
- skilltree_lab/data/NOTICE +14 -0
- skilltree_lab/data/asterisms.lines.json +1 -0
- skilltree_lab/data/snt.lines.json +1 -0
- skilltree_lab/editor.html +418 -0
- skilltree_lab/editor.py +311 -0
- skilltree_lab/figures.py +201 -0
- skilltree_lab/shapes.py +1053 -0
- skilltree_lab/svg.py +61 -0
- skilltree_lab-0.1.0.dist-info/METADATA +266 -0
- skilltree_lab-0.1.0.dist-info/RECORD +18 -0
- skilltree_lab-0.1.0.dist-info/WHEEL +4 -0
- skilltree_lab-0.1.0.dist-info/entry_points.txt +2 -0
- skilltree_lab-0.1.0.dist-info/licenses/LICENSE +21 -0
- skilltree_lab-0.1.0.dist-info/licenses/skilltree_lab/data/LICENSE.d3-celestial +12 -0
- skilltree_lab-0.1.0.dist-info/licenses/skilltree_lab/data/NOTICE +14 -0
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>skilltree edit</title>
|
|
6
|
+
<style>
|
|
7
|
+
* { box-sizing: border-box; }
|
|
8
|
+
body { margin: 0; background: #0c0c0e; color: #c8c8d0; font: 13px sans-serif; display: flex; flex-direction: column; height: 100vh; }
|
|
9
|
+
#bar { display: flex; gap: 6px; align-items: center; padding: 6px 8px; border-bottom: 1px solid #2a2a30; }
|
|
10
|
+
button { background: #1c1c22; color: #e8e8ec; border: 1px solid #3a3a44; border-radius: 4px; padding: 4px 10px; cursor: pointer; font: inherit; }
|
|
11
|
+
button:hover { background: #26262e; }
|
|
12
|
+
button[aria-pressed="true"] { background: #ff9a3c; color: #0c0c0e; border-color: #ff9a3c; }
|
|
13
|
+
#status { margin-left: auto; color: #8a8a96; }
|
|
14
|
+
#main { flex: 1; display: flex; min-height: 0; }
|
|
15
|
+
svg { flex: 1; min-width: 0; touch-action: none; user-select: none; }
|
|
16
|
+
#side { width: 300px; border-left: 1px solid #2a2a30; padding: 8px; overflow: auto; font: 12px monospace; white-space: pre-wrap; }
|
|
17
|
+
#side h3 { font: bold 12px sans-serif; margin: 10px 0 4px; color: #e8e8ec; }
|
|
18
|
+
#side h3.tree { cursor: pointer; } #side input, #side select { background: #0c0c0e; color: #e8e8ec; border: 1px solid #3a3a44; font: inherit; }
|
|
19
|
+
g.n.sel > :not(text):not(.ring) { stroke: #ffffff; stroke-width: 2; }
|
|
20
|
+
.warn { color: #ffcc66; } .err { color: #ff6b6b; }
|
|
21
|
+
g.n { cursor: grab; } g.n.picked circle.ring { display: inline; }
|
|
22
|
+
circle.ring { display: none; fill: none; stroke: #ff9a3c; stroke-width: 2; }
|
|
23
|
+
body.connect g.n, body.add svg { cursor: crosshair; }
|
|
24
|
+
body.movetree g.n { cursor: move; }
|
|
25
|
+
body.delete g.n, body.delete line.hit { cursor: not-allowed; }
|
|
26
|
+
dialog { background: #1c1c22; color: #e8e8ec; border: 1px solid #3a3a44; border-radius: 6px; }
|
|
27
|
+
dialog label { display: block; margin-bottom: 8px; }
|
|
28
|
+
dialog input, dialog select { background: #0c0c0e; color: #e8e8ec; border: 1px solid #3a3a44; font: inherit; }
|
|
29
|
+
body.addtree svg { cursor: copy; } body.removetree g.n { cursor: not-allowed; }
|
|
30
|
+
line.hit { stroke: transparent; stroke-width: 14; }
|
|
31
|
+
body.delete line.hit:hover { stroke: rgba(255,107,107,.35); }
|
|
32
|
+
</style>
|
|
33
|
+
</head>
|
|
34
|
+
<body>
|
|
35
|
+
<div id="bar">
|
|
36
|
+
<button id="save" title="write the hand fixes into the file (Ctrl+S)">Save</button>
|
|
37
|
+
<button id="undo" title="Ctrl+Z">Undo</button>
|
|
38
|
+
<button id="reset" title="back to what the file holds">Reset</button>
|
|
39
|
+
<button id="fit" title="the whole picture in view (wheel: zoom, drag empty space: pan)">Fit</button>
|
|
40
|
+
<button id="add" class="mode" aria-pressed="false" title="click an empty spot: a new node there (Esc to stop)">Add</button>
|
|
41
|
+
<button id="connect" class="mode" aria-pressed="false" title="click one node, then another: a line between them; the same two again take it away (Esc to stop)">Connect</button>
|
|
42
|
+
<button id="delete" class="mode" aria-pressed="false" title="click a node or a line to take it away (Esc to stop)">Delete</button>
|
|
43
|
+
<button id="movetree" class="mode" aria-pressed="false" title="drag any node to move its whole tree (Esc to stop)" hidden>Move tree</button>
|
|
44
|
+
<button id="addtree" class="mode" aria-pressed="false" title="click an empty spot: a new tree there (Esc to stop)">Add tree</button>
|
|
45
|
+
<button id="import" title="bring in the trees of another .toml file">Import</button>
|
|
46
|
+
<button id="removetree" class="mode" aria-pressed="false" title="click a node: its whole tree goes, with its links (Esc to stop)" hidden>Remove tree</button>
|
|
47
|
+
<input id="file" type="file" accept=".toml" hidden>
|
|
48
|
+
<span id="status"></span>
|
|
49
|
+
</div>
|
|
50
|
+
<dialog id="newtree">
|
|
51
|
+
<form method="dialog">
|
|
52
|
+
<label>name <input id="tname" required pattern="[A-Za-z0-9_-]+"></label>
|
|
53
|
+
<label>shape <select id="tshape"></select></label>
|
|
54
|
+
<button value="ok">OK</button> <button value="cancel" formnovalidate>Cancel</button>
|
|
55
|
+
</form>
|
|
56
|
+
</dialog>
|
|
57
|
+
<div id="main">
|
|
58
|
+
<svg id="view"></svg>
|
|
59
|
+
<div id="side"></div>
|
|
60
|
+
</div>
|
|
61
|
+
<script>
|
|
62
|
+
const $ = id => document.getElementById(id);
|
|
63
|
+
const svg = $("view");
|
|
64
|
+
const clone = v => JSON.parse(JSON.stringify(v));
|
|
65
|
+
let file, stem, ver, looks, hand, tree, undo = [], mode = null, first = null, drag = null, dirty = false;
|
|
66
|
+
let sel = null, pan = null, zoomed = false; // the picked node's label; a pan in progress; the user zoomed or panned
|
|
67
|
+
|
|
68
|
+
const U = () => tree.unit;
|
|
69
|
+
const byId = id => tree.nodes.find(n => n.id === id);
|
|
70
|
+
const r2 = v => Math.round(v * 100) / 100;
|
|
71
|
+
const same = (p, a, b) => (p[0] === a && p[1] === b) || (p[0] === b && p[1] === a);
|
|
72
|
+
const H = n => hand.trees[n.tree]; // the hand fixes of a node's tree
|
|
73
|
+
const at = i => (hand.multi ? hand.trees[i].at : [0, 0]); // where that tree stands
|
|
74
|
+
const esc = s => String(s).replace(/&/g, "&").replace(/</g, "<");
|
|
75
|
+
|
|
76
|
+
function status(text) { $("status").textContent = text; }
|
|
77
|
+
|
|
78
|
+
function fit() {
|
|
79
|
+
const xs = tree.nodes.map(n => n.x * U()), ys = tree.nodes.map(n => n.y * U()), pad = 50;
|
|
80
|
+
const x0 = Math.min(...xs) - pad, y0 = Math.min(...ys) - pad;
|
|
81
|
+
svg.setAttribute("viewBox", `${x0} ${y0} ${Math.max(...xs) - x0 + pad} ${Math.max(...ys) - y0 + pad}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// widen the view only when a node (with its label) would fall outside it, so the picture does not jump while working
|
|
85
|
+
function fitIfOut() {
|
|
86
|
+
if (zoomed) return; // the user chose the view: leave it
|
|
87
|
+
const [x0, y0, w, h] = svg.getAttribute("viewBox").split(" ").map(Number);
|
|
88
|
+
if (tree.nodes.some(n => n.x * U() < x0 + 20 || n.y * U() < y0 + 20 || n.x * U() > x0 + w - 40 || n.y * U() > y0 + h - 20)) fit();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function draw() {
|
|
92
|
+
const lines = tree.edges.map(([a, b, join]) => {
|
|
93
|
+
const A = byId(a), B = byId(b);
|
|
94
|
+
const xy = `x1="${A.x * U()}" y1="${A.y * U()}" x2="${B.x * U()}" y2="${B.y * U()}"`;
|
|
95
|
+
return `<line data-a="${a}" data-b="${b}" ${xy} stroke="${join ? "#ff9a3c" : "#55555f"}" stroke-width="3"/>` +
|
|
96
|
+
`<line class="hit" data-a="${a}" data-b="${b}" ${xy}/>`; // a wide, unseen line that is easy to click
|
|
97
|
+
});
|
|
98
|
+
const nodes = tree.nodes.map(n =>
|
|
99
|
+
`<g class="n${first === n.id ? " picked" : ""}${sel === n.label ? " sel" : ""}" data-id="${n.id}" transform="translate(${n.x * U()},${n.y * U()})">` +
|
|
100
|
+
`<circle class="ring" r="15"/>${n.mark}<text x="11" y="-9" font-size="11" fill="#c8c8d0">${esc(n.label)}</text></g>`);
|
|
101
|
+
svg.innerHTML = lines.join("") + nodes.join("");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function toml(v) {
|
|
105
|
+
if (Array.isArray(v)) return "[" + v.map(toml).join(", ") + "]";
|
|
106
|
+
if (v && typeof v === "object") return "{" + Object.entries(v).map(([k, x]) => `${/^[\w-]+$/.test(k) ? k : JSON.stringify(k)} = ${toml(x)}`).join(", ") + "}";
|
|
107
|
+
return JSON.stringify(v);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function side(error) {
|
|
111
|
+
const empty = v => (Array.isArray(v) ? !v.length : !Object.keys(v).length);
|
|
112
|
+
let html = "";
|
|
113
|
+
const n = sel && tree.nodes.find(m => m.label === sel);
|
|
114
|
+
if (n) {
|
|
115
|
+
const own = n.local in H(n).node_shapes, look = own ? String(n.look) : ""; // what the node shows now
|
|
116
|
+
const opts = looks.includes(look) || !look ? looks : [...looks, look]; // a shape by its number of sides too
|
|
117
|
+
html += `<h3>Node ${esc(n.label)}</h3><label>shape <select id="nshape"><option value="">tree default</option>` +
|
|
118
|
+
opts.map(l => `<option${l === look ? " selected" : ""}>${esc(l)}</option>`).join("") + `</select></label>\n` +
|
|
119
|
+
`<label>ratio <input id="nw" size="3" value="${n.ratio[0]}"> : <input id="nh" size="3" value="${n.ratio[1]}"></label> <button id="napply">Apply</button>`;
|
|
120
|
+
}
|
|
121
|
+
hand.trees.forEach((h, i) => {
|
|
122
|
+
const keys = ["at", "add", "move", "shift", "place", "joins", "cut", "remove", "node_shapes"].filter(k => k in h && !empty(h[k]) && !(k === "at" && !h.at[0] && !h.at[1]));
|
|
123
|
+
const head = hand.multi ? `<h3 class="tree" data-i="${i}" title="click to rename">${esc(h.name)} ✎</h3>` : `<h3>${esc(file)}</h3>`;
|
|
124
|
+
html += head + (esc(keys.map(k => `${k} = ${toml(h[k])}`).join("\n")) || "(no hand fixes)");
|
|
125
|
+
});
|
|
126
|
+
if (hand.multi) html += `<h3>links</h3>` + (hand.links.length ? esc(toml(hand.links)) : "(none)");
|
|
127
|
+
html += `<h3>Warnings</h3>` + (tree.warnings.length ? tree.warnings.map(w => `<div class="warn">${esc(w)}</div>`).join("") : "(none)");
|
|
128
|
+
if (error) html += `<h3>Error</h3><div class="err">${esc(error)}</div>`;
|
|
129
|
+
$("side").innerHTML = html;
|
|
130
|
+
for (const el of document.querySelectorAll("#side h3.tree")) el.onclick = () => rename(+el.dataset.i);
|
|
131
|
+
if (n) $("napply").onclick = () => {
|
|
132
|
+
const look = $("nshape").value, w = +$("nw").value, hgt = +$("nh").value;
|
|
133
|
+
const [dw, dh] = tree.ratios[n.tree];
|
|
134
|
+
change(() => {
|
|
135
|
+
const shapes = H(n).node_shapes;
|
|
136
|
+
if (!look) delete shapes[n.local];
|
|
137
|
+
else shapes[n.local] = w * dh === hgt * dw ? look : { shape: look, ratio: [w, hgt] }; // same as the tree's: just the shape
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function rename(i) {
|
|
143
|
+
const old = hand.trees[i].name, want = prompt(`New name for the tree ${old}`, old);
|
|
144
|
+
if (!want || want === old) return;
|
|
145
|
+
if (!/^[A-Za-z0-9_-]+$/.test(want)) { side("a tree name is letters, digits, _ or -"); return; }
|
|
146
|
+
if (hand.trees.some(t => t.name === want)) { side(`a tree is already called ${want}`); return; }
|
|
147
|
+
const was = sel;
|
|
148
|
+
if (sel && sel.startsWith(old + ".")) sel = want + sel.slice(old.length); // so the panel follows the new name
|
|
149
|
+
change(h => {
|
|
150
|
+
h.trees[i].name = want;
|
|
151
|
+
h.links = h.links.map(p => p.map(l => (l.startsWith(old + ".") ? want + l.slice(old.length) : l)));
|
|
152
|
+
}).then(ok => { if (!ok) { sel = was; side(); } });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// the nodes hanging off n by their own lines (grey), n first: what Shift-drag carries along
|
|
156
|
+
function subtree(n) {
|
|
157
|
+
const out = [n.id];
|
|
158
|
+
for (let i = 0; i < out.length; i++)
|
|
159
|
+
for (const [a, b, join] of tree.edges) if (a === out[i] && !join && !out.includes(b)) out.push(b);
|
|
160
|
+
return out;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function post(body) {
|
|
164
|
+
const r = await (await fetch("/build", { method: "POST", body: JSON.stringify({ ...body, version: ver }) })).json();
|
|
165
|
+
if (r.version) ver = r.version;
|
|
166
|
+
return r;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// apply a change to hand and rebuild on the server, which may renumber new nodes; a refused change is rolled back
|
|
170
|
+
async function change(fn) {
|
|
171
|
+
const before = clone(hand);
|
|
172
|
+
fn(hand);
|
|
173
|
+
const r = await post({ hand });
|
|
174
|
+
if (r.error) { hand = before; draw(); side(r.error); return false; }
|
|
175
|
+
undo.push(before);
|
|
176
|
+
hand = r.hand; tree = r.tree; dirty = true;
|
|
177
|
+
fitIfOut(); draw(); side(); bar(); status("not saved");
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function load() {
|
|
182
|
+
const r = await (await fetch("/state")).json();
|
|
183
|
+
if (r.error) { $("side").innerHTML = `<div class="err">${esc(r.error)}</div>`; return; }
|
|
184
|
+
file = r.file; stem = r.stem; ver = r.version; looks = r.looks; sel = null; zoomed = false; hand = r.hand; tree = r.tree; undo = []; dirty = false;
|
|
185
|
+
$("tshape").innerHTML = r.shapes.map(s => `<option>${s}</option>`).join("");
|
|
186
|
+
fit(); draw(); side(); bar(); status("");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// buttons that only mean something with several trees
|
|
190
|
+
function bar() {
|
|
191
|
+
$("movetree").hidden = !hand.multi;
|
|
192
|
+
$("removetree").hidden = !hand.multi || hand.trees.length < 2;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const blank = () => ({ move: {}, place: {}, joins: [], add: {}, remove: [], cut: [], src: null });
|
|
196
|
+
const safe = s => s.replace(/[^A-Za-z0-9_-]/g, "_") || "tree";
|
|
197
|
+
|
|
198
|
+
function freeName(h, want) {
|
|
199
|
+
const used = new Set(h.trees.map(t => t.name));
|
|
200
|
+
let name = safe(want), k = 2;
|
|
201
|
+
while (used.has(name)) name = `${safe(want)}_${k++}`;
|
|
202
|
+
return name;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// a file of one tree that gets a second: the first takes the file's name, and labels gain tree names
|
|
206
|
+
function becomeMulti(h) {
|
|
207
|
+
if (h.multi) return;
|
|
208
|
+
h.multi = true; h.links = [];
|
|
209
|
+
h.trees[0].name = safe(stem); h.trees[0].at = h.trees[0].at || [0, 0];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function askTree(px, py) {
|
|
213
|
+
const box = $("newtree");
|
|
214
|
+
$("tname").value = freeName(hand.multi ? hand : { trees: [{ name: safe(stem) }] }, "tree");
|
|
215
|
+
box.onclose = () => {
|
|
216
|
+
if (box.returnValue !== "ok") return;
|
|
217
|
+
const shape = $("tshape").value, want = $("tname").value;
|
|
218
|
+
change(h => {
|
|
219
|
+
becomeMulti(h);
|
|
220
|
+
h.trees.push({ ...blank(), name: freeName(h, want), at: [r2(px / U()), r2(py / U())], settings: { shape } });
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
box.showModal();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
$("import").onclick = () => $("file").click();
|
|
227
|
+
$("file").onchange = async () => {
|
|
228
|
+
const f = $("file").files[0];
|
|
229
|
+
$("file").value = "";
|
|
230
|
+
if (!f) return;
|
|
231
|
+
const r = await (await fetch("/parse", { method: "POST", body: JSON.stringify({ text: await f.text(), stem: f.name.replace(/\.toml$/i, "") }) })).json();
|
|
232
|
+
if (r.error) { side(r.error); return; }
|
|
233
|
+
// the imported group starts just right of everything already open, whatever its own at values
|
|
234
|
+
const dx = Math.max(...tree.nodes.map(n => n.x)) + 4 - Math.min(...r.trees.map(t => t.at[0]));
|
|
235
|
+
change(h => {
|
|
236
|
+
becomeMulti(h);
|
|
237
|
+
const names = {};
|
|
238
|
+
for (const t of r.trees) {
|
|
239
|
+
names[t.name] = freeName(h, t.name);
|
|
240
|
+
h.trees.push({ ...t, name: names[t.name], at: [r2(t.at[0] + dx), t.at[1]] });
|
|
241
|
+
}
|
|
242
|
+
const re = l => { const [t, ...rest] = l.split("."); return [names[t] ?? t, ...rest].join("."); };
|
|
243
|
+
h.links.push(...r.links.map(p => p.map(re)));
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
function point(e) {
|
|
248
|
+
const p = new DOMPoint(e.clientX, e.clientY).matrixTransform(svg.getScreenCTM().inverse());
|
|
249
|
+
return [p.x, p.y];
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// forget a node everywhere the page wrote it down
|
|
253
|
+
function drop(h, n) {
|
|
254
|
+
const l = n.local;
|
|
255
|
+
delete h.add[l]; delete h.move[l]; delete h.place[l]; delete h.shift[l]; delete h.node_shapes[l];
|
|
256
|
+
h.joins = h.joins.filter(p => !p.includes(l));
|
|
257
|
+
h.cut = h.cut.filter(p => !p.includes(l));
|
|
258
|
+
hand.links = hand.links.filter(p => !p.includes(n.label));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function nearest(px, py) {
|
|
262
|
+
let best = tree.nodes[0], d = Infinity;
|
|
263
|
+
for (const n of tree.nodes) { const e = (n.x * U() - px) ** 2 + (n.y * U() - py) ** 2; if (e < d) { d = e; best = n; } }
|
|
264
|
+
return best;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function toggle(list, a, b) {
|
|
268
|
+
const i = list.findIndex(p => same(p, a, b));
|
|
269
|
+
if (i >= 0) list.splice(i, 1); else list.push([a, b]);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
svg.addEventListener("pointerdown", e => {
|
|
273
|
+
const g = e.target.closest("g.n");
|
|
274
|
+
if (mode === "addtree" && !g) { const [px, py] = point(e); askTree(px, py); return; }
|
|
275
|
+
if (mode === "removetree") {
|
|
276
|
+
if (!g) return;
|
|
277
|
+
const i = byId(+g.dataset.id).tree, name = hand.trees[i].name;
|
|
278
|
+
if (hand.trees.length < 2) { side("at least one tree stays"); return; }
|
|
279
|
+
if (!confirm(`Remove the tree ${name}?`)) return;
|
|
280
|
+
change(h => {
|
|
281
|
+
h.trees.splice(i, 1);
|
|
282
|
+
h.links = h.links.filter(p => !p.some(l => l.startsWith(name + ".")));
|
|
283
|
+
});
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (mode === "add" && !g) {
|
|
287
|
+
const [px, py] = point(e), ti = nearest(px, py).tree; // a new node belongs to the nearest tree
|
|
288
|
+
const used = new Set(tree.nodes.filter(n => n.tree === ti).map(n => n.local));
|
|
289
|
+
let k = 1; while (used.has("n" + k)) k++;
|
|
290
|
+
const [ax, ay] = at(ti);
|
|
291
|
+
change(h => { h.trees[ti].add["n" + k] = [r2(px / U() - ax), r2(py / U() - ay)]; });
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (mode === "delete") {
|
|
295
|
+
if (g) {
|
|
296
|
+
const n = byId(+g.dataset.id);
|
|
297
|
+
change(() => { const h = H(n), own = n.local in h.add; drop(h, n); if (!own) h.remove.push(n.local); });
|
|
298
|
+
} else if (e.target.classList.contains("hit")) {
|
|
299
|
+
const A = byId(+e.target.dataset.a), B = byId(+e.target.dataset.b);
|
|
300
|
+
change(() => {
|
|
301
|
+
if (A.tree !== B.tree) return toggle(hand.links, A.label, B.label); // between trees: always a link
|
|
302
|
+
const h = H(A), i = h.joins.findIndex(p => same(p, A.local, B.local));
|
|
303
|
+
if (i >= 0) h.joins.splice(i, 1); else h.cut.push([A.local, B.local]); // a line the page made just goes; one the shape made is cut
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (!g) {
|
|
309
|
+
if (!mode) { // drag empty space: pan
|
|
310
|
+
const [x, y, w, h] = svg.getAttribute("viewBox").split(" ").map(Number);
|
|
311
|
+
pan = { cx: e.clientX, cy: e.clientY, x, y, w, h };
|
|
312
|
+
svg.setPointerCapture(e.pointerId);
|
|
313
|
+
}
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const n = byId(+g.dataset.id);
|
|
317
|
+
if (mode === "connect") {
|
|
318
|
+
if (first === null) { first = n.id; draw(); return; }
|
|
319
|
+
const A = byId(first); first = null;
|
|
320
|
+
if (A.id === n.id) { draw(); return; }
|
|
321
|
+
change(() => {
|
|
322
|
+
if (A.tree === n.tree) toggle(H(n).joins, A.local, n.local);
|
|
323
|
+
else toggle(hand.links, A.label, n.label);
|
|
324
|
+
});
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const ids = mode === "movetree" ? tree.nodes.filter(m => m.tree === n.tree).map(m => m.id)
|
|
328
|
+
: e.shiftKey ? subtree(n) : [n.id];
|
|
329
|
+
const [px, py] = point(e);
|
|
330
|
+
drag = { n, ids, px, py, from: ids.map(id => [byId(id).x, byId(id).y]), carry: e.shiftKey && mode !== "movetree" };
|
|
331
|
+
svg.setPointerCapture(e.pointerId);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
svg.addEventListener("pointermove", e => {
|
|
335
|
+
if (pan) {
|
|
336
|
+
const k = pan.w / svg.clientWidth;
|
|
337
|
+
svg.setAttribute("viewBox", `${pan.x - (e.clientX - pan.cx) * k} ${pan.y - (e.clientY - pan.cy) * k} ${pan.w} ${pan.h}`);
|
|
338
|
+
zoomed = true;
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (!drag) return;
|
|
342
|
+
const [px, py] = point(e), dx = (px - drag.px) / U(), dy = (py - drag.py) / U();
|
|
343
|
+
drag.ids.forEach((id, i) => {
|
|
344
|
+
const m = byId(id);
|
|
345
|
+
m.x = drag.from[i][0] + dx; m.y = drag.from[i][1] + dy;
|
|
346
|
+
svg.querySelector(`g.n[data-id="${id}"]`).setAttribute("transform", `translate(${m.x * U()},${m.y * U()})`);
|
|
347
|
+
for (const l of svg.querySelectorAll(`line[data-a="${id}"]`)) { l.setAttribute("x1", m.x * U()); l.setAttribute("y1", m.y * U()); }
|
|
348
|
+
for (const l of svg.querySelectorAll(`line[data-b="${id}"]`)) { l.setAttribute("x2", m.x * U()); l.setAttribute("y2", m.y * U()); }
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
svg.addEventListener("pointerup", () => {
|
|
353
|
+
if (pan) { pan = null; return; }
|
|
354
|
+
if (!drag) return;
|
|
355
|
+
const { n, ids, from, carry } = drag; drag = null;
|
|
356
|
+
const dx = r2(n.x - from[0][0]), dy = r2(n.y - from[0][1]);
|
|
357
|
+
if (!dx && !dy) { // a click, not a drag: pick the node for the panel
|
|
358
|
+
if (!mode) { sel = n.label; draw(); side(); }
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
change(() => {
|
|
362
|
+
const h = H(n), [ax, ay] = at(n.tree), l = n.local;
|
|
363
|
+
if (carry) { // Shift: the node with everything hanging off it
|
|
364
|
+
const [sx, sy] = h.shift[l] || [0, 0];
|
|
365
|
+
h.shift[l] = [r2(sx + dx), r2(sy + dy)];
|
|
366
|
+
} else if (ids.length > 1) h.at = [r2(h.at[0] + dx), r2(h.at[1] + dy)]; // the whole tree
|
|
367
|
+
else if (l in h.add) h.add[l] = [r2(n.x - ax), r2(n.y - ay)]; // a new node just stands somewhere else
|
|
368
|
+
else if (l in h.place) h.place[l] = [r2(n.x - ax), r2(n.y - ay)]; // a placed node keeps being placed
|
|
369
|
+
else {
|
|
370
|
+
const [mx, my] = h.move[l] || [0, 0];
|
|
371
|
+
h.move[l] = [r2(mx + dx), r2(my + dy)];
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
function setMode(m) {
|
|
377
|
+
mode = m; first = null;
|
|
378
|
+
for (const b of document.querySelectorAll("button.mode")) b.setAttribute("aria-pressed", b.id === m);
|
|
379
|
+
document.body.className = m || "";
|
|
380
|
+
if (tree) draw();
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
async function save() {
|
|
384
|
+
const r = await post({ hand, save: true });
|
|
385
|
+
if (r.error) { side(r.error); return; }
|
|
386
|
+
hand = r.hand; tree = r.tree; dirty = false; draw(); side(); status("saved");
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
async function back() {
|
|
390
|
+
if (!undo.length) return;
|
|
391
|
+
hand = undo.pop();
|
|
392
|
+
const r = await post({ hand });
|
|
393
|
+
if (!r.error) { hand = r.hand; tree = r.tree; }
|
|
394
|
+
dirty = true; draw(); side(r.error); status(undo.length ? "not saved" : "");
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
svg.addEventListener("wheel", e => { // zoom around the pointer
|
|
398
|
+
e.preventDefault();
|
|
399
|
+
const [x, y, w, h] = svg.getAttribute("viewBox").split(" ").map(Number), [px, py] = point(e), f = e.deltaY > 0 ? 1.15 : 1 / 1.15;
|
|
400
|
+
svg.setAttribute("viewBox", `${px - (px - x) * f} ${py - (py - y) * f} ${w * f} ${h * f}`);
|
|
401
|
+
zoomed = true;
|
|
402
|
+
}, { passive: false });
|
|
403
|
+
|
|
404
|
+
$("fit").onclick = () => { zoomed = false; fit(); };
|
|
405
|
+
$("save").onclick = save;
|
|
406
|
+
$("undo").onclick = back;
|
|
407
|
+
$("reset").onclick = load;
|
|
408
|
+
for (const b of document.querySelectorAll("button.mode")) b.onclick = () => setMode(mode === b.id ? null : b.id);
|
|
409
|
+
document.addEventListener("keydown", e => {
|
|
410
|
+
if (e.key === "Escape") setMode(null);
|
|
411
|
+
if ((e.ctrlKey || e.metaKey) && e.key === "z") { e.preventDefault(); back(); }
|
|
412
|
+
if ((e.ctrlKey || e.metaKey) && e.key === "s") { e.preventDefault(); save(); }
|
|
413
|
+
});
|
|
414
|
+
window.addEventListener("beforeunload", e => { if (dirty) e.preventDefault(); });
|
|
415
|
+
load();
|
|
416
|
+
</script>
|
|
417
|
+
</body>
|
|
418
|
+
</html>
|