@enspirit/opspicy 0.0.0-stage → 0.2.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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +281 -2
  3. package/bin/entry.js +27 -0
  4. package/bin/opspicy +19 -0
  5. package/dist/addresses.js +27 -0
  6. package/dist/behind.js +64 -0
  7. package/dist/citizens.js +71 -0
  8. package/dist/configuration.d.ts +56 -0
  9. package/dist/configuration.js +66 -0
  10. package/dist/explorer/afresh.js +43 -0
  11. package/dist/explorer/api.js +101 -0
  12. package/dist/explorer/asking.js +59 -0
  13. package/dist/explorer/build.js +11 -0
  14. package/dist/explorer/calls.js +42 -0
  15. package/dist/explorer/document.js +109 -0
  16. package/dist/explorer/elo-refs.js +153 -0
  17. package/dist/explorer/elo.js +95 -0
  18. package/dist/explorer/fonts/OFL.txt +93 -0
  19. package/dist/explorer/fonts/dm-sans-latin.woff2 +0 -0
  20. package/dist/explorer/html.js +59 -0
  21. package/dist/explorer/index.js +61 -0
  22. package/dist/explorer/json.js +33 -0
  23. package/dist/explorer/machine.js +34 -0
  24. package/dist/explorer/masking.js +57 -0
  25. package/dist/explorer/operation.js +94 -0
  26. package/dist/explorer/pages.js +132 -0
  27. package/dist/explorer/script/base.js +103 -0
  28. package/dist/explorer/script/call.js +161 -0
  29. package/dist/explorer/script/calling.js +103 -0
  30. package/dist/explorer/script/connection.js +83 -0
  31. package/dist/explorer/script/copying.js +75 -0
  32. package/dist/explorer/script/filter.js +56 -0
  33. package/dist/explorer/script/formaldef.js +37 -0
  34. package/dist/explorer/script/main.js +82 -0
  35. package/dist/explorer/script/markdown.js +143 -0
  36. package/dist/explorer/script/notes.js +184 -0
  37. package/dist/explorer/script/noting.js +38 -0
  38. package/dist/explorer/script/peek.js +62 -0
  39. package/dist/explorer/script/peeking.js +19 -0
  40. package/dist/explorer/script/ranking.js +91 -0
  41. package/dist/explorer/script/review.js +126 -0
  42. package/dist/explorer/script/rows.js +58 -0
  43. package/dist/explorer/script/run.js +168 -0
  44. package/dist/explorer/script/running.js +66 -0
  45. package/dist/explorer/script/search.js +122 -0
  46. package/dist/explorer/script/sending.js +64 -0
  47. package/dist/explorer/script/served.js +51 -0
  48. package/dist/explorer/serve.js +41 -0
  49. package/dist/explorer/server.js +143 -0
  50. package/dist/explorer/site.js +77 -0
  51. package/dist/explorer/style.css +241 -0
  52. package/dist/explorer/telling.js +30 -0
  53. package/dist/explorer/tests.js +78 -0
  54. package/dist/explorer/wiki.js +76 -0
  55. package/dist/files.js +176 -0
  56. package/dist/finding.js +17 -0
  57. package/dist/judging.js +75 -0
  58. package/dist/lines.js +30 -0
  59. package/dist/main.js +128 -0
  60. package/dist/meaning.js +100 -0
  61. package/dist/modelling.js +115 -0
  62. package/dist/naming.js +84 -0
  63. package/dist/notation.js +119 -0
  64. package/dist/planning.js +32 -0
  65. package/dist/reading.js +120 -0
  66. package/dist/report-file.js +42 -0
  67. package/dist/report.js +62 -0
  68. package/dist/running.js +102 -0
  69. package/dist/selecting.js +71 -0
  70. package/dist/starting.js +41 -0
  71. package/dist/suite.js +29 -0
  72. package/dist/words.js +103 -0
  73. package/dist/world.js +128 -0
  74. package/package.json +57 -4
@@ -0,0 +1,75 @@
1
+ // A call as a command to copy: curl, with a variable wherever something is secret, and nothing
2
+ // else that a shell reads. Pure: what a call is made of is calling.js, and the same text is held
3
+ // by a table of cases outside a browser, and by a real shell given the command.
4
+
5
+ /** The names a secret goes by: those elo-form makes a password field of, and no other for now. */
6
+ const SECRET = /password|passwd|secret/i;
7
+
8
+ /**
9
+ * A name, as the variable a command reads it from: `x-api-key` is `X_API_KEY`. A shell reads a
10
+ * letter or `_` first, then letters, digits and `_`: a name that begins with a digit begins with
11
+ * `_` (`9-lives` is `_9_LIVES`, since `$9` is something else), and one with no letter and no
12
+ * digit is `VALUE`.
13
+ */
14
+ function variableOf(name) {
15
+ const word = name.replace(/[^A-Za-z0-9]+/g, '_').toUpperCase();
16
+ if (!/[A-Z0-9]/.test(word)) return 'VALUE';
17
+ return /^[A-Z_]/.test(word) ? word : `_${word}`;
18
+ }
19
+
20
+ /**
21
+ * A mark none of the texts holds. Where a command reads a variable is said with it while the
22
+ * command is put together (`opspicy.0.TOKEN.`), so that what the command wrote itself is told
23
+ * from what a value happens to say: a `$HOME` typed in a field is a text, not a variable.
24
+ */
25
+ function markFor(...texts) {
26
+ let n = 0;
27
+ while (texts.some((text) => text.includes(`opspicy.${n}.`))) n += 1;
28
+ return `opspicy.${n}.`;
29
+ }
30
+
31
+ /** Where a variable stands in a text of the command, until the command is written; `bare` where what it stands for is no text, and takes no quotes of JSON. */
32
+ const standsFor = (mark, name, bare = false) => `${mark}${bare ? 'bare.' : ''}${variableOf(name)}.`;
33
+
34
+ /**
35
+ * The same value, with the place of a variable where each secret was: a command that is copied
36
+ * holds none. Under a name a secret goes by, everything is one: a text, a number, a yes or a no,
37
+ * and each of them down a list or a record, its variable named after the way to it (the second
38
+ * of `apiSecrets` is `APISECRETS_1`, the `next` of `password` is `PASSWORD_NEXT`).
39
+ */
40
+ function masked(value, mark, under = '') {
41
+ if (Array.isArray(value)) return value.map((one, at) => masked(one, mark, under && `${under}_${at}`));
42
+ if (isRecord(value)) return Object.fromEntries(Object.entries(value).map(([name, one]) => [name, masked(one, mark, under ? `${under}_${name}` : SECRET.test(name) ? name : '')]));
43
+ if (!under || value === null || value === undefined) return value;
44
+ return standsFor(mark, under, typeof value !== 'string');
45
+ }
46
+
47
+ /** A text of the command, in which a variable that stands for no text has lost the quotes JSON gave its place: the shell gives a number as one. */
48
+ const unquoted = (text, mark) => text.replace(new RegExp(`"?${mark.replace(/\./g, '\\.')}bare\\.([A-Z0-9_]+\\.)"?`, 'g'), `${mark}$1`);
49
+
50
+ /** A text as one word of a shell that reads nothing in it: single quotes, and `'\''` for a quote inside. */
51
+ const plainWord = (text) => `'${text.replace(/'/g, "'\\''")}'`;
52
+
53
+ /** A text as one word of a shell, which reads a variable where the mark says one stands (`"$NAME"`) and nothing anywhere else: no `$`, no backtick, no quote. */
54
+ const shellWord = (text, mark) =>
55
+ text
56
+ .split(new RegExp(`${mark.replace(/\./g, '\\.')}([A-Z0-9_]+)\\.`))
57
+ .map((piece, at) => (at % 2 ? `"$${piece}"` : piece ? plainWord(piece) : ''))
58
+ .join('');
59
+
60
+ /**
61
+ * The call as a curl command. Nothing secret is in it: the token is `$TOKEN`, the value of each
62
+ * other header a variable named after it, and so is each secret of the input. No target is
63
+ * `$TARGET`. Those are all the shell reads in it: the mark is one the call itself, as it would be
64
+ * sent, does not hold.
65
+ */
66
+ function curlOf(call, input, connection) {
67
+ const sent = requestOf(call, input);
68
+ const mark = markFor(connection.target, connection.headers.join('\n'), sent.path, JSON.stringify(sent.body) || '');
69
+ const word = (text) => shellWord(unquoted(text, mark), mark);
70
+ const request = requestOf(call, masked(input, mark));
71
+ const url = `${connection.target || standsFor(mark, 'TARGET')}${request.path}`;
72
+ const headers = ['Accept: application/json', ...(connection.token ? [`Authorization: Bearer ${standsFor(mark, 'TOKEN')}`] : []), ...connection.headers.map((name) => `${name}: ${standsFor(mark, name)}`), ...(hasBody(request) ? ['Content-Type: application/json'] : [])];
73
+ const body = hasBody(request) ? [`-d ${word(JSON.stringify(request.body))}`] : [];
74
+ return [`curl -X ${request.method} ${word(url)}`, ...headers.map((header) => `-H ${word(header)}`), ...body].join(' \\\n ');
75
+ }
@@ -0,0 +1,56 @@
1
+ // "Filter this page": the field of the top bar hides the rows of the page shown that do not hold
2
+ // every word, in the row and in its formaldef. It begins again when the page changes.
3
+
4
+ const filter = $('#filter');
5
+ const fcount = $('#fcount');
6
+
7
+ /** A page with fewer rows than this has nothing to filter. */
8
+ const FILTERABLE = 6;
9
+
10
+ /** The rows of a page, the ones a filter keeps or hides: not those that belong to another row. */
11
+ const rowsOf = (page) => $$('table.t > tbody > tr', page).filter((row) => !row.matches(SECONDARY));
12
+
13
+ /** What a row says, for a filter: its cells but the tools, and its formaldef. */
14
+ function sayingOf(row) {
15
+ const cells = [...row.cells].filter((cell) => !cell.classList.contains('rt'));
16
+ const formal = formalOf(row);
17
+ return `${cells.map((cell) => cell.textContent).join(' ')} ${formal ? formal.textContent : ''}`;
18
+ }
19
+
20
+ /** Shows or hides a row and what belongs to it. */
21
+ function showRow(row, on) {
22
+ row.hidden = !on;
23
+ for (let next = row.nextElementSibling; next && next.matches(SECONDARY); next = next.nextElementSibling) next.hidden = !on;
24
+ }
25
+
26
+ /** Hides the rows of the page shown that lack a word, and the groups left empty; says how many remain. */
27
+ function applyFilter() {
28
+ const page = currentPage();
29
+ const words = wordsOf(filter.value);
30
+ const rows = rowsOf(page);
31
+ let kept = 0;
32
+ for (const row of rows) {
33
+ const on = holdsAll(sayingOf(row), words);
34
+ showRow(row, on);
35
+ if (on) kept += 1;
36
+ }
37
+ for (const group of $$('.citizens', page)) group.classList.toggle('none', words.length > 0 && !rowsOf(group).some((row) => !row.hidden));
38
+ fcount.textContent = words.length ? `${kept} of ${rows.length}` : '';
39
+ }
40
+
41
+ /** The filter, empty, for a page that has just been shown: offered when the page has rows enough. */
42
+ function resetFilter(page) {
43
+ filter.value = '';
44
+ filter.hidden = rowsOf(page).length < FILTERABLE;
45
+ applyFilter();
46
+ }
47
+
48
+ filter.addEventListener('input', applyFilter);
49
+
50
+ filter.addEventListener('keydown', (event) => {
51
+ if (event.key !== 'Escape') return;
52
+ event.preventDefault();
53
+ filter.value = '';
54
+ applyFilter();
55
+ filter.blur();
56
+ });
@@ -0,0 +1,37 @@
1
+ // The formaldef of a citizen is folded in a row of its own, under it. It shows at three sizes,
2
+ // each at one click: one row, one table, everything. The choice of everything is remembered.
3
+
4
+ const formalButton = $('#formal');
5
+
6
+ /** The row that holds the formaldef of a citizen's row, when it has one. */
7
+ const formalOf = (row) => {
8
+ const next = row.nextElementSibling;
9
+ return next && next.classList.contains('formal-row') && next.textContent.trim() ? next : null;
10
+ };
11
+
12
+ function setFormal(on) {
13
+ document.body.classList.toggle('formal-all', on);
14
+ formalButton.setAttribute('aria-pressed', String(on));
15
+ local.set('opspicy.formal', on);
16
+ }
17
+
18
+ /** Shows or hides the formaldef of one row, `on` saying which and nothing toggling it; the button of the row says which it is. */
19
+ function toggleRow(row, on) {
20
+ const open = row.classList.toggle('fd-on', on);
21
+ const button = $('.rb.fd', row);
22
+ if (button) button.setAttribute('aria-expanded', String(open));
23
+ }
24
+
25
+ /** Shows or hides the formaldefs of one table, from the button beside its title. */
26
+ function toggleTable(button) {
27
+ const open = button.closest('.citizens').classList.toggle('fd-open');
28
+ button.setAttribute('aria-expanded', String(open));
29
+ }
30
+
31
+ /** A click on a row is for its formaldef, unless it is on something of its own: a link, a field, a text being selected. */
32
+ const forItsFormaldef = (event) => {
33
+ const row = event.target.closest('tr.has-fd');
34
+ return row && !event.target.closest('a, button, input, textarea, select, summary') && !String(getSelection()) ? row : null;
35
+ };
36
+
37
+ formalButton.addEventListener('click', () => setFormal(!document.body.classList.contains('formal-all')));
@@ -0,0 +1,82 @@
1
+ // The page shown, the theme, the clicks and the keys. Which page is shown is decided by the address alone,
2
+ // through `:target` in the style: this part follows it, and never decides it.
3
+
4
+ const theme = $('#thm');
5
+
6
+ /** The theme: the system's, unless one of the two is chosen, and then it is remembered. */
7
+ function setTheme(chosen) {
8
+ const name = ['light', 'dark'].includes(chosen) ? chosen : '';
9
+ if (name) document.documentElement.dataset.theme = name;
10
+ else delete document.documentElement.dataset.theme;
11
+ theme.value = name;
12
+ local.set('opspicy.theme', name);
13
+ }
14
+
15
+ /** Opens or closes every section of a page, from the button at its top. */
16
+ function foldAll(button) {
17
+ const folds = $$('details.fold:not(.try)', button.closest('.page'));
18
+ const open = folds.every((fold) => !fold.open);
19
+ for (const fold of folds) fold.open = open;
20
+ button.textContent = open ? 'Collapse all' : 'Expand all';
21
+ button.setAttribute('aria-expanded', String(open));
22
+ }
23
+
24
+ /** The sidebar says where the page shown is: itself, or the entry it is under. */
25
+ function sayWhere(page) {
26
+ const at = `#${page.dataset.nav || page.id}`;
27
+ for (const link of $$('.side .nav a')) {
28
+ if (link.getAttribute('href') === at) link.setAttribute('aria-current', 'page');
29
+ else link.removeAttribute('aria-current');
30
+ }
31
+ document.title = `${txt($('h1', page))} · opspicy explorer`;
32
+ }
33
+
34
+ /** What follows a change of address: the page it names is prepared, with the notes taken on it, and a row it names shows its formaldef. */
35
+ function route() {
36
+ leaveSearch();
37
+ const page = currentPage();
38
+ const named = target();
39
+ prepare(page);
40
+ resetFilter(page);
41
+ hidePeek();
42
+ showNotes(page);
43
+ addCall(page);
44
+ if (named && named.matches('tr.has-fd')) toggleRow(named, true);
45
+ if (page.id === 'review') renderReview();
46
+ $('#pnote').hidden = formalButton.hidden = !page.dataset.kind;
47
+ sayWhere(page);
48
+ }
49
+
50
+ document.addEventListener('click', (event) => {
51
+ const button = event.target.closest('button');
52
+ if (button && button.matches('.fold-all')) foldAll(button);
53
+ else if (button && button.matches('.fd-toggle')) toggleTable(button);
54
+ const row = forItsFormaldef(event);
55
+ if (row) toggleRow(row);
56
+ });
57
+
58
+ const typing = (event) => /INPUT|TEXTAREA|SELECT/.test(event.target.tagName) || event.target.isContentEditable;
59
+
60
+ document.addEventListener('keydown', (event) => {
61
+ if (event.key === 'Escape' && event.target.matches('.note textarea, .note select')) event.target.blur();
62
+ // A dialog that is open has the keys: the page under it is not there to be noted, filtered or unfolded.
63
+ if (event.metaKey || event.ctrlKey || event.altKey || typing(event) || $('dialog[open]')) return;
64
+ if (event.key === '/') {
65
+ event.preventDefault();
66
+ q.focus();
67
+ } else if (event.key === 'f' && !filter.hidden) {
68
+ event.preventDefault();
69
+ filter.focus();
70
+ } else if (event.key === 'd' && !formalButton.hidden) formalButton.click();
71
+ else if (event.key === 'n' && !$('#pnote').hidden) {
72
+ event.preventDefault();
73
+ pageNote(currentPage(), true);
74
+ }
75
+ });
76
+
77
+ theme.addEventListener('change', () => setTheme(theme.value));
78
+ addEventListener('hashchange', route);
79
+
80
+ setTheme(local.get('opspicy.theme', ''));
81
+ setFormal(local.get('opspicy.formal', false) === true);
82
+ route();
@@ -0,0 +1,143 @@
1
+ // The review file: the notes a reader took, as one Markdown text that a person reads and that an
2
+ // AI agent can map back to the specification without help. Pure: the notes, what the page knows
3
+ // of itself and the time are given, so the same text is held by a table of cases outside a browser.
4
+ //
5
+ // Each note names its item three ways: `ref`, its anchor in the explorer; `source`, the file and
6
+ // the line that declare it; `quote`, its text as written. Nothing of a connection is ever here.
7
+ //
8
+ // The file has a structure its readers rely on: a `##` section per note, then its keys, then its
9
+ // text. Nothing a note, a sentence or a formaldef says can be taken for that structure: a value
10
+ // is on one line, code is between more backticks than it holds, and a note with a line that
11
+ // reads as a heading, a fence or a key is written as a block quote.
12
+
13
+ /** What a note is. */
14
+ const NOTE_KINDS = ['comment', 'question', 'issue', 'suggestion'];
15
+
16
+ /** The citizens that are sentences: named after where they stand, and quoted by what they say. */
17
+ const STANDING = ['Clause', 'Case', 'Condition'];
18
+
19
+ const oneLine = (text) => String(text ?? '').replace(/\s+/g, ' ').trim();
20
+
21
+ /**
22
+ * What a note is about, from the entry the index has of it and where it is written: enough to
23
+ * find it again, and kept with the note, so that it is still told when the specification has
24
+ * lost it. A clause and a case are of their operation; a condition is named by where it stands.
25
+ */
26
+ function itemOf(entry, source) {
27
+ const standing = STANDING.includes(entry.k);
28
+ const inOperation = standing && entry.k !== 'Condition';
29
+ return {
30
+ ref: entry.h,
31
+ kind: entry.k.toLowerCase(),
32
+ where: entry.w || '',
33
+ title: inOperation ? `${entry.ctx} · ${entry.w}` : standing ? entry.w : entry.n,
34
+ operation: inOperation ? entry.ctx : '',
35
+ area: entry.a || '',
36
+ source: source || '',
37
+ quote: oneLine(entry.q || (standing ? entry.n : '')),
38
+ formaldef: (entry.c || '').trim(),
39
+ };
40
+ }
41
+
42
+ const two = (number) => String(number).padStart(2, '0');
43
+
44
+ /** The last run, in a line: when, in the reader's own time, which of the two runs, and what it decided. */
45
+ function runLine(run) {
46
+ if (!run) return 'not run';
47
+ const at = new Date(run.when);
48
+ const when = `${at.getFullYear()}-${two(at.getMonth() + 1)}-${two(at.getDate())} ${two(at.getHours())}:${two(at.getMinutes())}`;
49
+ return `${when}, ${run.mode} run, ${run.passed} passed, ${run.failed} failed${run.notRun ? `, ${run.notRun} not run` : ''}`;
50
+ }
51
+
52
+ /** How many notes, and of each kind: `2 (1 question, 1 issue)`. */
53
+ function countLine(notes) {
54
+ const each = NOTE_KINDS.map((kind) => [kind, notes.filter((note) => note.kind === kind).length]).filter(([, count]) => count);
55
+ const said = each.map(([kind, count]) => `${count} ${kind}${count > 1 ? 's' : ''}`).join(', ');
56
+ return `${notes.length}${said ? ` (${said})` : ''}`;
57
+ }
58
+
59
+ /** The longest row of backticks a text holds. */
60
+ const backticks = (text) => Math.max(0, ...(text.match(/`+/g) || []).map((row) => row.length));
61
+
62
+ /** A text as code on a line: between more backticks than it holds in a row, and set apart from them when it begins or ends with one. */
63
+ function code(text) {
64
+ const said = oneLine(text);
65
+ const fence = '`'.repeat(backticks(said) + 1);
66
+ const apart = /^`|`$/.test(said) ? ' ' : '';
67
+ return `${fence}${apart}${said}${apart}${fence}`;
68
+ }
69
+
70
+ /** A formaldef of one line is inline; a longer one is a block of Elo, under its key, in a fence longer than any row of backticks it holds. */
71
+ function formaldefLine(formaldef) {
72
+ if (!formaldef.includes('\n')) return `- formaldef: ${code(formaldef)}`;
73
+ const fence = '`'.repeat(Math.max(3, backticks(formaldef) + 1));
74
+ return `- formaldef:\n\n ${fence}elo\n${formaldef.replace(/^/gm, ' ')}\n ${fence}`;
75
+ }
76
+
77
+ /** The keys of a section, by their name. */
78
+ const KEYS = ['ref', 'kind', 'operation', 'area', 'source', 'quote', 'formaldef', 'changed'];
79
+
80
+ /**
81
+ * The keys of an item, always in this order; one that does not apply is left out, never written
82
+ * empty. `changed` is last, and there when the item at this `ref` no longer says what it said
83
+ * when the note was taken: it names which of `quote` and `formaldef` the specification says
84
+ * otherwise now, and the item is then told as it was. A quote is between `"`, with `\` before
85
+ * each `"` and each `\` of its own.
86
+ */
87
+ function keysOf(item, changed = []) {
88
+ return [
89
+ `- ref: ${code(item.ref)}`,
90
+ `- kind: ${oneLine(item.kind)}${item.where ? ` (${oneLine(item.where)})` : ''}`,
91
+ item.operation && `- operation: ${code(item.operation)}`,
92
+ item.area && `- area: ${code(item.area)}`,
93
+ item.source && `- source: ${code(item.source)}`,
94
+ item.quote && `- quote: "${oneLine(item.quote).replace(/[\\"]/g, '\\$&')}"`,
95
+ item.formaldef && formaldefLine(item.formaldef),
96
+ changed.length && `- changed: ${changed.join(', ')}`,
97
+ ].filter(Boolean);
98
+ }
99
+
100
+ /** A line of a note that could be taken for the structure of the file: a heading, a quote, a fence, a rule, a key of a section. */
101
+ const STRUCTURE = new RegExp(`^\\s*(#|>|\`\`\`|~~~|([-=*_])\\2{2,}\\s*$|- (${KEYS.join('|')}):)`);
102
+
103
+ /**
104
+ * The text of a note, as lines: as typed. One that holds a line that could be taken for the
105
+ * structure of the file is a block quote instead, `> ` before each of its lines: it reads the
106
+ * same, and no line of it begins a section, a key or a fence. Take `> ` off each line, and it
107
+ * is the note as typed.
108
+ */
109
+ function textOf(text) {
110
+ const lines = text.trim().split('\n');
111
+ return lines.some((line) => STRUCTURE.test(line)) ? lines.map((line) => (line ? `> ${line}` : '>')) : lines;
112
+ }
113
+
114
+ /** One note, as a section: its number, its kind and what it is about, the keys of its item, then its text. */
115
+ const sectionOf = (note, number, level) => [`${level} ${number}. ${note.kind} · ${oneLine(note.item.title)}`, '', ...keysOf(note.item, note.changed), '', ...textOf(note.text), ''];
116
+
117
+ const GONE = ['## No longer in the specification', '', 'The specification this explorer was written from has none of these items any more. Each is told', 'as it was when its note was taken.', ''];
118
+
119
+ /**
120
+ * The review, as Markdown. The notes are given in the order of the specification, each with its
121
+ * item; one whose item the specification no longer has says so (`gone`), and is told last, under
122
+ * a heading of its own, as it was when the note was taken.
123
+ */
124
+ function reviewOf({ meta, exported, notes }) {
125
+ const here = notes.filter((note) => !note.gone);
126
+ const gone = notes.filter((note) => note.gone);
127
+ const head = [
128
+ '# Specification review',
129
+ '',
130
+ `- specification: ${oneLine(meta.name)}`,
131
+ meta.version ? `- version: ${oneLine(meta.version)}` : null,
132
+ `- tests: ${runLine(meta.run)}`,
133
+ `- exported: ${exported.toISOString().replace(/\.\d+Z$/, 'Z')}`,
134
+ `- notes: ${countLine(notes)}`,
135
+ '',
136
+ 'Each note names its item three ways: `ref` is its anchor in explorer.html,',
137
+ '`source` is the file and line that declare it, `quote` is its text as written.',
138
+ '',
139
+ ].filter((line) => line !== null);
140
+ const told = here.flatMap((note, at) => sectionOf(note, at + 1, '##'));
141
+ const lost = gone.length ? [...GONE, ...gone.flatMap((note, at) => sectionOf(note, here.length + at + 1, '###'))] : [];
142
+ return [...head, ...told, ...lost].join('\n').replace(/\n+$/, '\n');
143
+ }
@@ -0,0 +1,184 @@
1
+ // Notes, taken while reading: on a row with its ✎, on the page with "Note" in the bar. A note has
2
+ // a kind and a text, is written in place under what it is about, and is saved as it is typed.
3
+ //
4
+ // They are kept in this browser, apart for each specification, by the anchor of what they are
5
+ // about: a note taken on a row that lists a citizen, or that tells the run of a case, belongs to
6
+ // the citizen and to the case. Each keeps what its item was when it was written, so that it is
7
+ // still told when the specification has lost the item, and told for what it was taken on when
8
+ // something else stands at its anchor (review.js, noting.js, markdown.js).
9
+
10
+ const META = JSON.parse($('#meta').textContent);
11
+ const NOTES = `opspicy.notes:${META.name}`;
12
+ let notes = notesKept(local.get(NOTES, {}));
13
+
14
+ let toldNotKept = false;
15
+
16
+ /**
17
+ * Writes the notes where the browser keeps them. Where it keeps nothing (a private window, a
18
+ * store that is full, a file in a browser that gives files none), the reader is told, once and
19
+ * for long enough to read it: notes typed for an hour must not be found gone at the next visit.
20
+ */
21
+ function keepNotes() {
22
+ if (local.set(NOTES, notes) || toldNotKept || !Object.keys(notes).length) return;
23
+ toldNotKept = true;
24
+ toast('This browser keeps nothing for this page: your notes will be gone when it is closed. Copy the review before you leave.', 12000);
25
+ }
26
+
27
+ /** What an anchor names, as a note tells it: nothing when the specification has no such item. */
28
+ function describe(ref) {
29
+ const entry = byAnchor.get(ref);
30
+ const element = document.getElementById(ref);
31
+ return entry ? itemOf(entry, element ? element.dataset.source : '') : null;
32
+ }
33
+
34
+ const noteCount = () => {
35
+ $('#note-count').textContent = Object.keys(notes).length || '';
36
+ };
37
+
38
+ /**
39
+ * Keeps the note of an anchor, or forgets it when it says nothing. What its item was is kept
40
+ * once, when the note is first written, and not at each edit: an anchor says where an item
41
+ * stands (the second ReqPre), and what stands there later may be something else.
42
+ */
43
+ function saveNote(ref, kind, text) {
44
+ const item = (notes[ref] && notes[ref].item) || describe(ref);
45
+ if (text.trim()) notes[ref] = { kind, text, ...(item ? { item } : {}) };
46
+ else delete notes[ref];
47
+ keepNotes();
48
+ noteCount();
49
+ }
50
+
51
+ /** Says a note is about what its item says now: what the item was is replaced by what it is. */
52
+ function noteAboutNow(ref) {
53
+ const item = describe(ref);
54
+ if (!notes[ref] || !item) return;
55
+ notes[ref] = { ...notes[ref], item };
56
+ keepNotes();
57
+ }
58
+
59
+ /** The field a note is written in: its kind, its text, and the button that deletes it. */
60
+ function noteBox(ref) {
61
+ const note = notes[ref] || { kind: 'comment', text: '' };
62
+ const box = document.createElement('div');
63
+ box.className = 'note';
64
+ box.dataset.ref = ref;
65
+ const kinds = NOTE_KINDS.map((kind) => `<option${kind === note.kind ? ' selected' : ''}>${kind}</option>`).join('');
66
+ box.innerHTML = `<select aria-label="Kind of note">${kinds}</select><textarea rows="1" placeholder="Your note on this" aria-label="Note"></textarea><button type="button" class="rb" data-del title="Delete this note" aria-label="Delete this note">×</button>`;
67
+ $('textarea', box).value = note.text;
68
+ return box;
69
+ }
70
+
71
+ /** The row that holds the note of a citizen's row, among those that belong to it. */
72
+ const noteRowOf = (row) => {
73
+ for (let next = row.nextElementSibling; next && next.matches(SECONDARY); next = next.nextElementSibling) if (next.classList.contains('note-row')) return next;
74
+ return null;
75
+ };
76
+
77
+ function addNoteRow(row) {
78
+ const noteRow = document.createElement('tr');
79
+ const cell = document.createElement('td');
80
+ noteRow.className = 'note-row';
81
+ cell.colSpan = row.cells.length;
82
+ cell.append(noteBox(aboutOf(row)));
83
+ noteRow.append(cell);
84
+ let last = row;
85
+ while (last.nextElementSibling && last.nextElementSibling.matches(SECONDARY)) last = last.nextElementSibling;
86
+ last.after(noteRow);
87
+ return noteRow;
88
+ }
89
+
90
+ /** The ✎ of a row says whether the row has its note open: by its disc to the eye, and as a button that is pressed to who does not see it. */
91
+ function sayNoted(button, on) {
92
+ button.classList.toggle('on', on);
93
+ button.setAttribute('aria-pressed', String(on));
94
+ }
95
+
96
+ /** Opens the note of a row, to write in it when asked; asked again of one that says nothing, closes it. */
97
+ function noteOn(row, focus) {
98
+ let noteRow = noteRowOf(row);
99
+ const button = $('.rb.nt', row);
100
+ if (noteRow && focus && !$('textarea', noteRow).value.trim()) {
101
+ noteRow.remove();
102
+ sayNoted(button, false);
103
+ return;
104
+ }
105
+ if (!noteRow) noteRow = addNoteRow(row);
106
+ sayNoted(button, true);
107
+ if (focus) $('textarea', noteRow).focus();
108
+ }
109
+
110
+ /** Opens the note of a page, under what the page says of itself. */
111
+ function pageNote(page, focus) {
112
+ let box = $(':scope > .pagenote', page);
113
+ if (!box) {
114
+ box = document.createElement('div');
115
+ box.className = 'pagenote';
116
+ box.append(noteBox(page.id));
117
+ ($(':scope > .meta', page) || $(':scope > .lead', page) || $('h1', page)).after(box);
118
+ }
119
+ if (focus) $('textarea', box).focus();
120
+ }
121
+
122
+ /**
123
+ * The notes taken on a page and on its rows are shown each time the page is, where they are not
124
+ * yet: a note belongs to its item, and one taken since on another row of the same item (a
125
+ * listing, the tests) is under this one too.
126
+ */
127
+ function showNotes(page) {
128
+ if (!page.dataset.kind) return;
129
+ if (notes[page.id]) pageNote(page);
130
+ for (const row of $$('tr.citizen', page)) if (notes[aboutOf(row)] && !noteRowOf(row)) noteOn(row);
131
+ }
132
+
133
+ /** What the field of a note was opened from: the ✎ of its row, or "Note" in the bar for the note of a page. */
134
+ function openerOf(box) {
135
+ let row = box.closest('tr.note-row');
136
+ while (row && row.matches(SECONDARY)) row = row.previousElementSibling;
137
+ return row ? $('.rb.nt', row) : $('#pnote');
138
+ }
139
+
140
+ /**
141
+ * Deletes a note, from wherever its field is: under a row, under a page, or in the review. The
142
+ * focus was on the button that goes with the field: it is given to what the field was opened
143
+ * from, or in the review to the note that takes its place, and never left nowhere.
144
+ */
145
+ function deleteNote(box) {
146
+ const ref = box.dataset.ref;
147
+ const item = box.closest('.review-item');
148
+ const opener = item ? null : openerOf(box);
149
+ const place = item ? $$('#review-list .review-item').indexOf(item) : -1;
150
+ saveNote(ref, '', '');
151
+ for (const other of $$('.note')) {
152
+ if (other.dataset.ref !== ref) continue;
153
+ const noteRow = other.closest('tr.note-row');
154
+ if (noteRow) sayNoted(openerOf(other), false);
155
+ (noteRow || other.closest('.pagenote, .review-item') || other).remove();
156
+ }
157
+ if (currentPage().id === 'review') renderReview(place);
158
+ else if (opener) opener.focus();
159
+ }
160
+
161
+ ROW_TOOLS.push((row, about) => (about ? '<button type="button" class="rb nt" title="Write a note on it" aria-label="Write a note on it" aria-pressed="false">✎</button>' : ''));
162
+
163
+ document.addEventListener('input', (event) => {
164
+ const box = event.target.closest('.note');
165
+ if (!box) return;
166
+ const ref = box.dataset.ref;
167
+ saveNote(ref, $('select', box).value, $('textarea', box).value);
168
+ for (const other of $$('.note')) {
169
+ if (other === box || other.dataset.ref !== ref) continue;
170
+ $('select', other).value = $('select', box).value;
171
+ $('textarea', other).value = $('textarea', box).value;
172
+ }
173
+ if (box.closest('#review')) $('#review-md').value = reviewText();
174
+ });
175
+
176
+ document.addEventListener('click', (event) => {
177
+ const button = event.target.closest('button');
178
+ if (!button) return;
179
+ if (button.matches('.rb.nt')) noteOn(button.closest('tr'), true);
180
+ else if (button.matches('.note [data-del]')) deleteNote(button.closest('.note'));
181
+ else if (button.id === 'pnote') pageNote(currentPage(), true);
182
+ });
183
+
184
+ noteCount();
@@ -0,0 +1,38 @@
1
+ // What a note is once the browser has kept it, and gives it back. Pure: what was kept is given,
2
+ // so a table of cases holds it outside a browser.
3
+ //
4
+ // The notes are kept by their anchor, each `{kind, text, item}`: its kind, its text as typed, and
5
+ // what its item was when it was written (markdown.js). The store they come back from was written
6
+ // by a page, and not always by this one: another version of the explorer, another document of
7
+ // the same origin. What is not a note is left out, so that it costs neither the page nor the others.
8
+
9
+ /** What an item says of itself, each a text. */
10
+ const ITEM_KEYS = ['ref', 'kind', 'where', 'title', 'operation', 'area', 'source', 'quote', 'formaldef'];
11
+
12
+ /** What an item was, read back: each of its keys a text, whatever was kept under it. */
13
+ const itemKept = (item) => Object.fromEntries(ITEM_KEYS.map((key) => [key, asText(item[key])]));
14
+
15
+ /** One note read back: a kind there is, a text that says something, and what its item was when one was kept; nothing when it is no note. */
16
+ function noteKept(value) {
17
+ if (!isRecord(value) || !asText(value.text).trim()) return null;
18
+ const kind = NOTE_KINDS.includes(value.kind) ? value.kind : NOTE_KINDS[0];
19
+ return { kind, text: value.text, ...(isRecord(value.item) ? { item: itemKept(value.item) } : {}) };
20
+ }
21
+
22
+ /** The notes read back, by their anchor. */
23
+ const notesKept = (value) =>
24
+ Object.fromEntries(
25
+ Object.entries(isRecord(value) ? value : {})
26
+ .map(([ref, note]) => [ref, noteKept(note)])
27
+ .filter(([, note]) => note),
28
+ );
29
+
30
+ /** What an item says, and a note is taken on: its sentence or its description, and its formaldef. Where it is written and what it is called are how it is found, not what it says. */
31
+ const SAYS = ['quote', 'formaldef'];
32
+
33
+ /**
34
+ * What of an item is no longer what it was when a note was taken on it, among `quote` and
35
+ * `formaldef`: none when it says the same, or when nothing was kept of what it was. An anchor
36
+ * says where an item stands: a clause added above it, and the note is under another sentence.
37
+ */
38
+ const changedSince = (then, now) => (then && now ? SAYS.filter((key) => asText(then[key]) !== asText(now[key])) : []);