pi-durable-bot 0.8.15 → 0.8.17
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/CHANGELOG.md +37 -0
- package/README.md +6 -6
- package/dist/agent/judge.js +32 -7
- package/dist/agent/judge.js.map +1 -1
- package/dist/agent/models.js +48 -26
- package/dist/agent/models.js.map +1 -1
- package/dist/bot/bot.js +1 -0
- package/dist/bot/bot.js.map +1 -1
- package/dist/bot/prompt.js +3 -2
- package/dist/bot/prompt.js.map +1 -1
- package/dist/cli/ai.js +3 -4
- package/dist/cli/ai.js.map +1 -1
- package/dist/cli/commands.js +3 -1
- package/dist/cli/commands.js.map +1 -1
- package/dist/cli/service.js +91 -3
- package/dist/cli/service.js.map +1 -1
- package/dist/core/judge.js +11 -1
- package/dist/core/judge.js.map +1 -1
- package/dist/gateway/judge-input.js +22 -0
- package/dist/gateway/judge-input.js.map +1 -1
- package/dist/gateway/waker.js +23 -11
- package/dist/gateway/waker.js.map +1 -1
- package/dist/main.js +38 -3
- package/dist/main.js.map +1 -1
- package/dist/pages/login-page.js +2 -4
- package/dist/pages/login-page.js.map +1 -1
- package/dist/pages/login.js +49 -6
- package/dist/pages/login.js.map +1 -1
- package/dist/pages/server.js +5 -3
- package/dist/pages/server.js.map +1 -1
- package/dist/pages/store.js +4 -4
- package/dist/pages/store.js.map +1 -1
- package/docs/for-agents.md +1 -1
- package/kit/login/login.js +1 -2
- package/kit/pots-ui.js +63 -8
- package/kit/pots.css +87 -1
- package/kit/pots.js +720 -28
- package/package.json +1 -1
- package/skills/pages/SKILL.md +56 -172
package/kit/pots.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Pots page kit: sends the answers of a page's forms to Pots,
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* Pots page kit: sends the answers of a page's forms to Pots, all at once or part by part, keeps what a person filled
|
|
3
|
+
* in until it is sent, shows data from JSON files as text, reloads a page that opts in when it changes, and gives a
|
|
4
|
+
* page that has a light and a dark look a switch between them. No build step, no dependencies, and no look of its own
|
|
5
|
+
* beyond a plain one for the parts it adds, which the page's CSS overrides.
|
|
6
|
+
*
|
|
7
|
+
* Every page of a Pots host shares one browser origin, so what one page keeps in the browser every other page can read.
|
|
8
|
+
* The kit keeps two things there: the reader's light or dark choice, which is not private, and the draft of each form,
|
|
9
|
+
* encrypted with a key made from the page's link, which other pages do not know. A draft goes when its form is sent.
|
|
6
10
|
*
|
|
7
|
-
* <link rel="stylesheet" href="../../__pots/kit/pots.css">
|
|
8
11
|
* <script src="../../__pots/kit/pots.js" defer></script>
|
|
9
12
|
*
|
|
10
13
|
* The pages skill (skills/pages/SKILL.md) explains the attributes. In short:
|
|
@@ -13,7 +16,11 @@
|
|
|
13
16
|
* data-pots-text="…" the question's text, when its legend, heading or label is not the text
|
|
14
17
|
* data-pots-comment a text field that is a comment on its question, not its answer
|
|
15
18
|
* data-pots-status where the form shows how sending goes (made after the submit button if absent)
|
|
16
|
-
*
|
|
19
|
+
* .pots-partial made at the end of a form of two or more questions: stays in view while the
|
|
20
|
+
* form does, and sends the questions answered or changed since the last send,
|
|
21
|
+
* as a partial answer; each question whose answer Pots has gets data-pots-sent
|
|
22
|
+
* <body data-pots-refresh="5"> check every 5 seconds whether the page changed, and reload it (a .pots-banner
|
|
23
|
+
* with a Reload button instead, when the person has started to fill a form)
|
|
17
24
|
* data-pots-source="data.json" fill the elements inside from that JSON file, as text:
|
|
18
25
|
* data-pots-fill="a.b" this element's text is that value
|
|
19
26
|
* data-pots-list="items" repeat this element's first child for each item of that list
|
|
@@ -133,17 +140,70 @@
|
|
|
133
140
|
return out;
|
|
134
141
|
}
|
|
135
142
|
|
|
136
|
-
/**
|
|
143
|
+
/** The questions' IDs, in order; a repeated ID gets "-2", "-3" so no answer is lost. */
|
|
144
|
+
function uniqueIds(questions) {
|
|
145
|
+
var taken = {};
|
|
146
|
+
return questions.map(function (question) {
|
|
147
|
+
var id = question.id;
|
|
148
|
+
for (var n = 2; Object.prototype.hasOwnProperty.call(taken, id); n++) id = question.id + "-" + n;
|
|
149
|
+
taken[id] = true;
|
|
150
|
+
return id;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** All answers of a form, by question ID. */
|
|
137
155
|
function answersFrom(questions) {
|
|
138
156
|
var answers = {};
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
answers[id] = questionAnswer(question);
|
|
157
|
+
var ids = uniqueIds(questions);
|
|
158
|
+
questions.forEach(function (question, i) {
|
|
159
|
+
answers[ids[i]] = questionAnswer(question);
|
|
143
160
|
});
|
|
144
161
|
return answers;
|
|
145
162
|
}
|
|
146
163
|
|
|
164
|
+
/**
|
|
165
|
+
* Whether a question's answer ({answer, comment}) has anything in it: a choice (not one whose value is empty, such
|
|
166
|
+
* as a menu's "Choose…"), a text that is not only spaces, a comment.
|
|
167
|
+
*/
|
|
168
|
+
function answered(entry) {
|
|
169
|
+
var has = function (value) {
|
|
170
|
+
if (value == null) return false;
|
|
171
|
+
if (typeof value === "string") return value.trim() !== "";
|
|
172
|
+
if (Array.isArray(value)) return value.some(has);
|
|
173
|
+
if (typeof value === "object") {
|
|
174
|
+
if ("value" in value && "label" in value) return String(value.value) !== "";
|
|
175
|
+
for (var key in value) if (Object.prototype.hasOwnProperty.call(value, key) && has(value[key])) return true;
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
};
|
|
180
|
+
return !!entry && (has(entry.answer) || has(entry.comment));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** What a question says, to compare with what was sent of it: its answer and its comment, not its text. */
|
|
184
|
+
function fingerprint(entry) {
|
|
185
|
+
return JSON.stringify([entry && entry.answer !== undefined ? entry.answer : null, (entry && entry.comment) || ""]);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* What a partial send takes of a form's answers: each question that does not say what was sent of it (sent: {id:
|
|
190
|
+
* fingerprint}), when it was sent before, so an answer taken back goes too, or the person filled it now (filled: {id:
|
|
191
|
+
* true}; without it, each answered question).
|
|
192
|
+
* {answers, ids}
|
|
193
|
+
*/
|
|
194
|
+
function unsent(answers, sent, filled) {
|
|
195
|
+
var out = {};
|
|
196
|
+
var ids = [];
|
|
197
|
+
Object.keys(answers).forEach(function (id) {
|
|
198
|
+
var was = sent && Object.prototype.hasOwnProperty.call(sent, id) ? sent[id] : undefined;
|
|
199
|
+
var now = filled ? filled[id] === true : answered(answers[id]);
|
|
200
|
+
if (fingerprint(answers[id]) === was || (was === undefined && !now)) return;
|
|
201
|
+
out[id] = answers[id];
|
|
202
|
+
ids.push(id);
|
|
203
|
+
});
|
|
204
|
+
return { answers: out, ids: ids };
|
|
205
|
+
}
|
|
206
|
+
|
|
147
207
|
/** Whether a failed answer is worth another try: no answer, a timeout, too many requests, or a server error. */
|
|
148
208
|
function retryable(status) {
|
|
149
209
|
return status === 0 || status === 408 || status === 425 || status === 429 || status >= 500;
|
|
@@ -204,9 +264,11 @@
|
|
|
204
264
|
*
|
|
205
265
|
* io: {fetch(url, init) → Promise<Response>, sleep(ms) → Promise, randomId() → string,
|
|
206
266
|
* version() → Promise<string>, onStatus(state, detail)}
|
|
267
|
+
* options: {partial: true} for a part of the answers, which Pots tells the bot is a part.
|
|
207
268
|
* Resolves {ok: true} or {ok: false, status, message}.
|
|
208
269
|
*/
|
|
209
|
-
function deliver(base, answers, io) {
|
|
270
|
+
function deliver(base, answers, io, options) {
|
|
271
|
+
var partial = !!(options && options.partial);
|
|
210
272
|
var submission = io.randomId();
|
|
211
273
|
var attempt = 0;
|
|
212
274
|
var status = io.onStatus || function () {};
|
|
@@ -222,7 +284,10 @@
|
|
|
222
284
|
.version()
|
|
223
285
|
.then(
|
|
224
286
|
function (version) {
|
|
225
|
-
var
|
|
287
|
+
var payload = { submission: submission, version: version };
|
|
288
|
+
if (partial) payload.partial = true;
|
|
289
|
+
payload.answers = answers;
|
|
290
|
+
var body = JSON.stringify(payload);
|
|
226
291
|
if (utf8Length(body) > MAX_BYTES) return { final: true, status: 413, message: "too-large", bytes: utf8Length(body) };
|
|
227
292
|
return io
|
|
228
293
|
.fetch(base + "__pots/submit", {
|
|
@@ -276,6 +341,9 @@
|
|
|
276
341
|
utf8Length: utf8Length,
|
|
277
342
|
questionAnswer: questionAnswer,
|
|
278
343
|
answersFrom: answersFrom,
|
|
344
|
+
answered: answered,
|
|
345
|
+
fingerprint: fingerprint,
|
|
346
|
+
unsent: unsent,
|
|
279
347
|
retryable: retryable,
|
|
280
348
|
servedVersion: servedVersion,
|
|
281
349
|
readVersion: readVersion,
|
|
@@ -284,7 +352,9 @@
|
|
|
284
352
|
root.PotsKit = api;
|
|
285
353
|
if (typeof document === "undefined" || typeof window === "undefined") return;
|
|
286
354
|
|
|
287
|
-
|
|
355
|
+
/** The page's link path, "/p/<token>/": the key of its drafts comes from it. null for a page not under a link. */
|
|
356
|
+
var linked = pageBase(location.pathname);
|
|
357
|
+
var base = linked || new URL("./", location.href).pathname;
|
|
288
358
|
/**
|
|
289
359
|
* The version of the page as it was loaded: the one Pots sent with the HTML file, or else read once, as soon as the
|
|
290
360
|
* kit runs, and kept, also when it failed. A later read could see a newer page than the one on screen.
|
|
@@ -321,15 +391,23 @@
|
|
|
321
391
|
});
|
|
322
392
|
}
|
|
323
393
|
|
|
324
|
-
/**
|
|
394
|
+
/**
|
|
395
|
+
* Sends answers built by the page itself: {"<id>": {"question": "…", "answer": …}, …}. options: {partial: true} when
|
|
396
|
+
* they are a part and more may follow; onStatus(state, detail) to follow the send.
|
|
397
|
+
*/
|
|
325
398
|
api.submit = function (answers, options) {
|
|
326
|
-
return deliver(
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
399
|
+
return deliver(
|
|
400
|
+
base,
|
|
401
|
+
answers,
|
|
402
|
+
{
|
|
403
|
+
fetch: function (url, init) { return fetch(url, init); },
|
|
404
|
+
sleep: sleep,
|
|
405
|
+
randomId: randomId,
|
|
406
|
+
version: loadVersion,
|
|
407
|
+
onStatus: (options && options.onStatus) || null,
|
|
408
|
+
},
|
|
409
|
+
{ partial: !!(options && options.partial) },
|
|
410
|
+
);
|
|
333
411
|
};
|
|
334
412
|
api.version = loadVersion;
|
|
335
413
|
|
|
@@ -421,7 +499,8 @@
|
|
|
421
499
|
: question.dom[0].labels && question.dom[0].labels.length > 0 && question.fields[0].kind !== "radio" && question.fields[0].kind !== "checkbox"
|
|
422
500
|
? ownText(question.dom[0].labels[0])
|
|
423
501
|
: question.id;
|
|
424
|
-
|
|
502
|
+
var changed = question.dom.some(function (field) { return fromDefault(field); });
|
|
503
|
+
return { id: question.id, text: text, fields: question.fields, element: question.element, changed: changed };
|
|
425
504
|
});
|
|
426
505
|
}
|
|
427
506
|
|
|
@@ -436,9 +515,39 @@
|
|
|
436
515
|
gone: "This link no longer works. Ask for a new one.",
|
|
437
516
|
tooLarge: "Your answers are too long to send. Shorten them and send again.",
|
|
438
517
|
noVersion: "This page could not reach Pots when it opened. Copy what you wrote, reload the page, and send again.",
|
|
518
|
+
noVersionKept: "This page could not reach Pots when it opened. Reload the page and send again: what you filled stays.",
|
|
439
519
|
failed: "Pots did not take the answers",
|
|
440
520
|
};
|
|
441
521
|
|
|
522
|
+
/** The words of the partial send's bar and marks, in Chinese on a Chinese page. */
|
|
523
|
+
var WORDS = /^zh/i.test(document.documentElement.lang || "")
|
|
524
|
+
? {
|
|
525
|
+
label: "先发一部分答案",
|
|
526
|
+
send: "先发已填的",
|
|
527
|
+
sent: "✓ 已发送",
|
|
528
|
+
count: function (filled, total, sent) { return "已填 " + filled + "/" + total + (sent ? " · 已发 " + sent : ""); },
|
|
529
|
+
sending: "发送中…",
|
|
530
|
+
retrying: "连不上 Pots,正在重试…",
|
|
531
|
+
gone: "这个链接已失效,请要一个新的。",
|
|
532
|
+
noVersion: "页面打开时连不上 Pots。请复制你写的内容,重新加载页面再发。",
|
|
533
|
+
noVersionKept: "页面打开时连不上 Pots。请重新加载页面再发,已填的会保留。",
|
|
534
|
+
tooLarge: "内容太长,发不出去。请改短再发。",
|
|
535
|
+
failed: "没发出去",
|
|
536
|
+
}
|
|
537
|
+
: {
|
|
538
|
+
label: "Send part of the answers",
|
|
539
|
+
send: "Send what's filled",
|
|
540
|
+
sent: "✓ Sent",
|
|
541
|
+
count: function (filled, total, sent) { return filled + "/" + total + " filled" + (sent ? " · " + sent + " sent" : ""); },
|
|
542
|
+
sending: MESSAGES.sending,
|
|
543
|
+
retrying: MESSAGES.retrying,
|
|
544
|
+
gone: MESSAGES.gone,
|
|
545
|
+
noVersion: MESSAGES.noVersion,
|
|
546
|
+
noVersionKept: MESSAGES.noVersionKept,
|
|
547
|
+
tooLarge: MESSAGES.tooLarge,
|
|
548
|
+
failed: "Not sent",
|
|
549
|
+
};
|
|
550
|
+
|
|
442
551
|
function statusElement(form) {
|
|
443
552
|
var element = form.querySelector("[data-pots-status]");
|
|
444
553
|
if (element) return element;
|
|
@@ -473,9 +582,11 @@
|
|
|
473
582
|
event.preventDefault();
|
|
474
583
|
if (form.getAttribute("data-pots-state") === "sending" || form.getAttribute("data-pots-state") === "done") return;
|
|
475
584
|
var answers = api.collect(form);
|
|
585
|
+
var kept = !!formState(form).place;
|
|
476
586
|
form.setAttribute("data-pots-state", "sending");
|
|
477
587
|
busy = true;
|
|
478
588
|
setDisabled(form, true);
|
|
589
|
+
update(form);
|
|
479
590
|
api
|
|
480
591
|
.submit(answers, {
|
|
481
592
|
onStatus: function (state, detail) {
|
|
@@ -489,22 +600,485 @@
|
|
|
489
600
|
if (result.ok) {
|
|
490
601
|
form.setAttribute("data-pots-state", "done");
|
|
491
602
|
show(form, "done", form.getAttribute("data-pots-done") || MESSAGES.done);
|
|
603
|
+
forget(form);
|
|
492
604
|
return;
|
|
493
605
|
}
|
|
494
606
|
form.removeAttribute("data-pots-state");
|
|
495
607
|
if (result.status === 404) {
|
|
496
608
|
form.setAttribute("data-pots-state", "gone");
|
|
497
609
|
show(form, "failed", MESSAGES.gone);
|
|
610
|
+
forget(form);
|
|
498
611
|
return;
|
|
499
612
|
}
|
|
500
613
|
// The answers stay on screen, so the person can send them again or copy them.
|
|
501
614
|
setDisabled(form, false);
|
|
502
|
-
|
|
615
|
+
update(form);
|
|
616
|
+
if (result.message === "no-version") show(form, "failed", kept ? MESSAGES.noVersionKept : MESSAGES.noVersion);
|
|
503
617
|
else if (result.message === "too-large") show(form, "failed", MESSAGES.tooLarge);
|
|
504
618
|
else show(form, "failed", MESSAGES.failed + " (" + result.status + ")" + (result.message ? ": " + clean(result.message).slice(0, 200) : "."));
|
|
505
619
|
});
|
|
506
620
|
}
|
|
507
621
|
|
|
622
|
+
// ---- Drafts and partial sends ----
|
|
623
|
+
|
|
624
|
+
/** The kit's state of each form[data-pots]. */
|
|
625
|
+
var states = new Map();
|
|
626
|
+
/** Set while a draft is put back, so the events it sends are not the person's changes. */
|
|
627
|
+
var restoring = false;
|
|
628
|
+
/** The questions the person clicked or typed in, also to keep an answer the page had chosen for them. */
|
|
629
|
+
var worked = new WeakSet();
|
|
630
|
+
var DRAFT_PREFIX = "pots-draft:";
|
|
631
|
+
/** A draft not changed for this many days goes, also when its page is never opened again. */
|
|
632
|
+
var DRAFT_DAYS = 30;
|
|
633
|
+
|
|
634
|
+
function formState(form) {
|
|
635
|
+
var state = states.get(form);
|
|
636
|
+
if (!state) {
|
|
637
|
+
// place: where the draft is kept and its key; sent: {question ID: fingerprint} of what Pots has; note: what the
|
|
638
|
+
// bar says about a partial send until the next change, "" for how much is filled and sent.
|
|
639
|
+
state = { place: null, ready: false, touched: false, sent: {}, bar: null, note: "", sending: false, writing: false, again: false, written: null, timer: 0 };
|
|
640
|
+
states.set(form, state);
|
|
641
|
+
}
|
|
642
|
+
return state;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/** Whether a form is done with: all of it sent, or its link taken back. */
|
|
646
|
+
function ended(form) {
|
|
647
|
+
var state = form.getAttribute("data-pots-state");
|
|
648
|
+
return state === "done" || state === "gone";
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
function storage() {
|
|
652
|
+
try {
|
|
653
|
+
return window.localStorage || null;
|
|
654
|
+
} catch (e) {
|
|
655
|
+
return null;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function utf8(text) {
|
|
660
|
+
return new TextEncoder().encode(text);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
function toHex(buffer) {
|
|
664
|
+
var bytes = new Uint8Array(buffer);
|
|
665
|
+
var out = "";
|
|
666
|
+
for (var i = 0; i < bytes.length; i++) out += (bytes[i] < 16 ? "0" : "") + bytes[i].toString(16);
|
|
667
|
+
return out;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function toBase64(bytes) {
|
|
671
|
+
var text = "";
|
|
672
|
+
for (var i = 0; i < bytes.length; i++) text += String.fromCharCode(bytes[i]);
|
|
673
|
+
return btoa(text);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function fromBase64(text) {
|
|
677
|
+
var raw = atob(text);
|
|
678
|
+
var bytes = new Uint8Array(raw.length);
|
|
679
|
+
for (var i = 0; i < raw.length; i++) bytes[i] = raw.charCodeAt(i);
|
|
680
|
+
return bytes;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Where a form keeps its draft and the draft's key (AES-GCM), both made from the page's link, which other pages of
|
|
685
|
+
* the host do not know, and the form. null when the page is not under a link, or the browser cannot keep or encrypt
|
|
686
|
+
* a draft (Web Crypto needs HTTPS or this computer): then nothing is kept.
|
|
687
|
+
*/
|
|
688
|
+
function draftPlace(form, index) {
|
|
689
|
+
var subtle = window.crypto && crypto.subtle;
|
|
690
|
+
if (!linked || !subtle || !storage()) return Promise.resolve(null);
|
|
691
|
+
var label = linked + "\n" + (form.getAttribute("id") || form.getAttribute("name") || "#" + index);
|
|
692
|
+
return Promise.all([subtle.digest("SHA-256", utf8("pots-draft-name\n" + label)), subtle.digest("SHA-256", utf8("pots-draft-key\n" + label))])
|
|
693
|
+
.then(function (digests) {
|
|
694
|
+
return subtle.importKey("raw", digests[1], { name: "AES-GCM" }, false, ["encrypt", "decrypt"]).then(function (key) {
|
|
695
|
+
return { item: DRAFT_PREFIX + toHex(digests[0]).slice(0, 32), key: key };
|
|
696
|
+
});
|
|
697
|
+
})
|
|
698
|
+
.then(null, function () { return null; });
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
function readDraft(place) {
|
|
702
|
+
return Promise.resolve()
|
|
703
|
+
.then(function () {
|
|
704
|
+
var kept = JSON.parse(storage().getItem(place.item) || "null");
|
|
705
|
+
if (!kept) return null;
|
|
706
|
+
return crypto.subtle.decrypt({ name: "AES-GCM", iv: fromBase64(kept.iv) }, place.key, fromBase64(kept.data)).then(function (plain) {
|
|
707
|
+
var draft = JSON.parse(new TextDecoder().decode(plain));
|
|
708
|
+
return draft && typeof draft === "object" ? draft : null;
|
|
709
|
+
});
|
|
710
|
+
})
|
|
711
|
+
.then(null, function () { return null; });
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/** A draft, encrypted, as the browser keeps it; null when it could not be encrypted. */
|
|
715
|
+
function sealDraft(place, draft) {
|
|
716
|
+
var iv = crypto.getRandomValues(new Uint8Array(12));
|
|
717
|
+
return crypto.subtle.encrypt({ name: "AES-GCM", iv: iv }, place.key, utf8(JSON.stringify(draft))).then(
|
|
718
|
+
function (data) { return JSON.stringify({ at: Date.now(), iv: toBase64(iv), data: toBase64(new Uint8Array(data)) }); },
|
|
719
|
+
function () { return null; },
|
|
720
|
+
);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/** The drafts of every page of the host that did not change for DRAFT_DAYS. */
|
|
724
|
+
function sweepDrafts() {
|
|
725
|
+
var store = storage();
|
|
726
|
+
if (!store) return;
|
|
727
|
+
try {
|
|
728
|
+
var old = [];
|
|
729
|
+
for (var i = 0; i < store.length; i++) {
|
|
730
|
+
var item = store.key(i);
|
|
731
|
+
if (!item || item.indexOf(DRAFT_PREFIX) !== 0) continue;
|
|
732
|
+
var at = 0;
|
|
733
|
+
try {
|
|
734
|
+
at = Number(JSON.parse(store.getItem(item)).at) || 0;
|
|
735
|
+
} catch (e) {}
|
|
736
|
+
if (!(Date.now() - at < DRAFT_DAYS * 86400000)) old.push(item);
|
|
737
|
+
}
|
|
738
|
+
old.forEach(function (item) { store.removeItem(item); });
|
|
739
|
+
} catch (e) {}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* The fields a draft keeps, each with a key that finds it again after a reload: its name, a choice's value too, and
|
|
744
|
+
* its place among fields of the same key. Not passwords, files or hidden fields: the page sets those.
|
|
745
|
+
*/
|
|
746
|
+
function draftFields(form) {
|
|
747
|
+
var seen = {};
|
|
748
|
+
var out = [];
|
|
749
|
+
Array.prototype.forEach.call(form.elements, function (field) {
|
|
750
|
+
var type = (field.type || "").toLowerCase();
|
|
751
|
+
if (!/^(INPUT|SELECT|TEXTAREA)$/.test(field.tagName) || SKIPPED[type] || type === "password" || type === "hidden") return;
|
|
752
|
+
var check = type === "radio" || type === "checkbox";
|
|
753
|
+
var key = (field.name || field.id || "") + (check ? "=" + field.value : "");
|
|
754
|
+
seen[key] = (seen[key] || 0) + 1;
|
|
755
|
+
out.push({ field: field, check: check, key: seen[key] > 1 ? key + "#" + seen[key] : key });
|
|
756
|
+
});
|
|
757
|
+
return out;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/** A field's value now, or as the page's HTML set it. */
|
|
761
|
+
function fieldValue(entry, initial) {
|
|
762
|
+
var field = entry.field;
|
|
763
|
+
if (entry.check) return initial ? field.defaultChecked : field.checked;
|
|
764
|
+
if (field.tagName !== "SELECT") return initial ? field.defaultValue : field.value;
|
|
765
|
+
var options = Array.prototype.slice.call(field.options);
|
|
766
|
+
var chosen = options.filter(function (option) { return initial ? option.defaultSelected : option.selected; });
|
|
767
|
+
// A menu with no choice marked in the HTML shows its first one that can be chosen.
|
|
768
|
+
if (initial && chosen.length === 0 && !field.multiple && field.size <= 1) chosen = options.filter(function (option) { return !option.disabled; }).slice(0, 1);
|
|
769
|
+
return chosen.map(function (option) { return option.value; });
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/** Whether a field's value is not the one in the page's HTML. */
|
|
773
|
+
function fromDefault(field) {
|
|
774
|
+
var type = (field.type || "").toLowerCase();
|
|
775
|
+
var entry = { field: field, check: type === "radio" || type === "checkbox" };
|
|
776
|
+
return JSON.stringify(fieldValue(entry, false)) !== JSON.stringify(fieldValue(entry, true));
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/** What the person changed in a form: each field whose value is not the one in the page's HTML. */
|
|
780
|
+
function changes(form) {
|
|
781
|
+
var fields = {};
|
|
782
|
+
draftFields(form).forEach(function (entry) {
|
|
783
|
+
if (fromDefault(entry.field)) fields[entry.key] = fieldValue(entry, false);
|
|
784
|
+
});
|
|
785
|
+
return fields;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/** Puts a draft's values back, and tells the page's own scripts as typing would. */
|
|
789
|
+
function restore(form, fields) {
|
|
790
|
+
draftFields(form).forEach(function (entry) {
|
|
791
|
+
if (!Object.prototype.hasOwnProperty.call(fields, entry.key)) return;
|
|
792
|
+
var value = fields[entry.key];
|
|
793
|
+
var field = entry.field;
|
|
794
|
+
if (JSON.stringify(fieldValue(entry, false)) === JSON.stringify(value)) return;
|
|
795
|
+
if (entry.check) {
|
|
796
|
+
if (typeof value !== "boolean") return;
|
|
797
|
+
field.checked = value;
|
|
798
|
+
} else if (field.tagName === "SELECT") {
|
|
799
|
+
if (!Array.isArray(value)) return;
|
|
800
|
+
Array.prototype.forEach.call(field.options, function (option) {
|
|
801
|
+
option.selected = value.indexOf(option.value) >= 0;
|
|
802
|
+
});
|
|
803
|
+
} else if (typeof value === "string") field.value = value;
|
|
804
|
+
else return;
|
|
805
|
+
// A comment kept in a closed <details> is shown again.
|
|
806
|
+
if (value === true || (typeof value === "string" && value !== "") || (Array.isArray(value) && value.length > 0))
|
|
807
|
+
for (var fold = field.closest("details"); fold; fold = fold.parentElement && fold.parentElement.closest("details")) fold.open = true;
|
|
808
|
+
field.dispatchEvent(new Event("input", { bubbles: true }));
|
|
809
|
+
field.dispatchEvent(new Event("change", { bubbles: true }));
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/** Keeps the form's draft: what the person changed and what was sent. One write at a time; the last one wins. */
|
|
814
|
+
function save(form) {
|
|
815
|
+
var state = formState(form);
|
|
816
|
+
if (!state.place || !state.ready || ended(form)) return;
|
|
817
|
+
if (state.writing) {
|
|
818
|
+
state.again = true;
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
var draft = { fields: changes(form), sent: state.sent };
|
|
822
|
+
var text = JSON.stringify(draft);
|
|
823
|
+
if (text === state.written) return;
|
|
824
|
+
var place = state.place;
|
|
825
|
+
if (Object.keys(draft.fields).length === 0 && Object.keys(draft.sent).length === 0) {
|
|
826
|
+
try {
|
|
827
|
+
storage().removeItem(place.item);
|
|
828
|
+
} catch (e) {}
|
|
829
|
+
state.written = text;
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
state.writing = true;
|
|
833
|
+
sealDraft(place, draft).then(function (record) {
|
|
834
|
+
state.writing = false;
|
|
835
|
+
// A form sent meanwhile keeps no draft.
|
|
836
|
+
if (record && !ended(form)) {
|
|
837
|
+
try {
|
|
838
|
+
storage().setItem(place.item, record);
|
|
839
|
+
state.written = text;
|
|
840
|
+
} catch (e) {}
|
|
841
|
+
}
|
|
842
|
+
if (state.again) {
|
|
843
|
+
state.again = false;
|
|
844
|
+
save(form);
|
|
845
|
+
}
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/** All of a form is sent, or its link is gone: its draft and its marks go. */
|
|
850
|
+
function forget(form) {
|
|
851
|
+
var state = formState(form);
|
|
852
|
+
state.sent = {};
|
|
853
|
+
if (state.place) {
|
|
854
|
+
try {
|
|
855
|
+
storage().removeItem(state.place.item);
|
|
856
|
+
} catch (e) {}
|
|
857
|
+
}
|
|
858
|
+
update(form);
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/**
|
|
862
|
+
* A form's questions, with the IDs their answers go under, their elements and their answers, and whether the person
|
|
863
|
+
* filled them: an answer that is only the page's own (a box checked in its HTML) is not one, unless they were at it.
|
|
864
|
+
*/
|
|
865
|
+
function questionsOf(form) {
|
|
866
|
+
var questions = describeForm(form);
|
|
867
|
+
var ids = uniqueIds(questions);
|
|
868
|
+
return questions.map(function (question, i) {
|
|
869
|
+
var answer = questionAnswer(question);
|
|
870
|
+
var filled = answered(answer) && (question.changed || (question.element !== null && worked.has(question.element)));
|
|
871
|
+
return { id: ids[i], element: question.element, answer: answer, filled: filled };
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/** The answers of a form's questions, and the questions the person filled. */
|
|
876
|
+
function answersOf(questions) {
|
|
877
|
+
var out = { answers: {}, filled: {} };
|
|
878
|
+
questions.forEach(function (question) {
|
|
879
|
+
out.answers[question.id] = question.answer;
|
|
880
|
+
if (question.filled) out.filled[question.id] = true;
|
|
881
|
+
});
|
|
882
|
+
return out;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
/** Brings a form's bar and the marks of its questions up to date. */
|
|
886
|
+
function update(form) {
|
|
887
|
+
var state = formState(form);
|
|
888
|
+
clearTimeout(state.timer);
|
|
889
|
+
state.timer = 0;
|
|
890
|
+
var questions = questionsOf(form);
|
|
891
|
+
var now = answersOf(questions);
|
|
892
|
+
var filled = 0;
|
|
893
|
+
var sent = 0;
|
|
894
|
+
questions.forEach(function (question) {
|
|
895
|
+
var mark = answered(question.answer) && !ended(form) && state.sent[question.id] === fingerprint(question.answer);
|
|
896
|
+
if (question.filled || mark) filled++;
|
|
897
|
+
if (mark) sent++;
|
|
898
|
+
if (!question.element) return;
|
|
899
|
+
if (mark) question.element.setAttribute("data-pots-sent", WORDS.sent);
|
|
900
|
+
else question.element.removeAttribute("data-pots-sent");
|
|
901
|
+
});
|
|
902
|
+
if (!state.bar && questions.length >= 2 && !ended(form)) makeBar(form, state);
|
|
903
|
+
if (!state.bar) return;
|
|
904
|
+
// One line: how a send goes, while it goes or when it failed; else how much is filled and sent.
|
|
905
|
+
var text = state.note || WORDS.count(filled, questions.length, sent);
|
|
906
|
+
if (state.text.textContent !== text) state.text.textContent = text;
|
|
907
|
+
state.button.disabled = state.sending || ended(form) || form.getAttribute("data-pots-state") === "sending" || unsent(now.answers, state.sent, now.filled).ids.length === 0;
|
|
908
|
+
state.wanted = form.getAttribute("data-pots-state") !== "done" && (filled > 0 || sent > 0 || state.note !== "");
|
|
909
|
+
showBar(form, state);
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/** Brings a form's bar up to date soon: once for a burst of typing. */
|
|
913
|
+
function later(form) {
|
|
914
|
+
var state = formState(form);
|
|
915
|
+
if (!state.timer) state.timer = setTimeout(function () { update(form); }, 150);
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/** The partial send's bar, at the end of the form: in view while the form is, with a plain look the page can change. */
|
|
919
|
+
function makeBar(form, state) {
|
|
920
|
+
partLook();
|
|
921
|
+
var bar = document.createElement("div");
|
|
922
|
+
bar.className = "pots-partial";
|
|
923
|
+
bar.setAttribute("role", "group");
|
|
924
|
+
bar.setAttribute("aria-label", WORDS.label);
|
|
925
|
+
bar.hidden = true;
|
|
926
|
+
var text = document.createElement("span");
|
|
927
|
+
text.className = "pots-partial-status";
|
|
928
|
+
text.setAttribute("role", "status");
|
|
929
|
+
text.setAttribute("aria-live", "polite");
|
|
930
|
+
var button = document.createElement("button");
|
|
931
|
+
button.type = "button";
|
|
932
|
+
button.textContent = WORDS.send;
|
|
933
|
+
button.addEventListener("click", function () { sendPart(form); });
|
|
934
|
+
bar.appendChild(text);
|
|
935
|
+
bar.appendChild(button);
|
|
936
|
+
form.appendChild(bar);
|
|
937
|
+
state.bar = bar;
|
|
938
|
+
state.text = text;
|
|
939
|
+
state.button = button;
|
|
940
|
+
placeBar(state);
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
/**
|
|
944
|
+
* Whether position: sticky keeps the bar in view: not when the nearest box around it that clips its content does not
|
|
945
|
+
* scroll, as when a page sets overflow-x: hidden on both html and body.
|
|
946
|
+
*/
|
|
947
|
+
function stickyFollows(element) {
|
|
948
|
+
var html = document.documentElement;
|
|
949
|
+
var open = function (style) { return /^(visible|clip)$/.test(style.overflowX) && /^(visible|clip)$/.test(style.overflowY); };
|
|
950
|
+
for (var box = element.parentElement; box && box !== html; box = box.parentElement) {
|
|
951
|
+
var style = getComputedStyle(box);
|
|
952
|
+
if (open(style)) continue;
|
|
953
|
+
// The body's overflow is the window's, unless html has its own.
|
|
954
|
+
if (box === document.body && open(getComputedStyle(html))) continue;
|
|
955
|
+
return /^(auto|scroll)$/.test(style.overflowY) && box.scrollHeight > box.clientHeight;
|
|
956
|
+
}
|
|
957
|
+
return true;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/** Sticky where it follows the screen; else fixed to the screen's bottom while the form crosses it. */
|
|
961
|
+
function placeBar(state) {
|
|
962
|
+
state.fixed = !stickyFollows(state.bar);
|
|
963
|
+
if (state.fixed) state.bar.setAttribute("data-pots-fixed", "");
|
|
964
|
+
else state.bar.removeAttribute("data-pots-fixed");
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
function showBar(form, state) {
|
|
968
|
+
var on = !!state.wanted;
|
|
969
|
+
if (on && state.fixed) {
|
|
970
|
+
var box = form.getBoundingClientRect();
|
|
971
|
+
on = box.top < innerHeight && box.bottom > innerHeight;
|
|
972
|
+
}
|
|
973
|
+
if (state.bar.hidden === on) state.bar.hidden = !on;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
/** Sends the questions answered or changed since the last send, as a part: the form stays open for the rest. */
|
|
977
|
+
function sendPart(form) {
|
|
978
|
+
var state = formState(form);
|
|
979
|
+
if (state.sending || ended(form) || form.getAttribute("data-pots-state") === "sending") return;
|
|
980
|
+
var now = answersOf(questionsOf(form));
|
|
981
|
+
var part = unsent(now.answers, state.sent, now.filled);
|
|
982
|
+
if (part.ids.length === 0) return;
|
|
983
|
+
state.sending = true;
|
|
984
|
+
state.note = WORDS.sending;
|
|
985
|
+
update(form);
|
|
986
|
+
api
|
|
987
|
+
.submit(part.answers, {
|
|
988
|
+
partial: true,
|
|
989
|
+
onStatus: function (status) {
|
|
990
|
+
if (status !== "retrying") return;
|
|
991
|
+
state.note = WORDS.retrying;
|
|
992
|
+
update(form);
|
|
993
|
+
},
|
|
994
|
+
})
|
|
995
|
+
.then(function (result) {
|
|
996
|
+
state.sending = false;
|
|
997
|
+
if (result.ok) {
|
|
998
|
+
part.ids.forEach(function (id) {
|
|
999
|
+
state.sent[id] = fingerprint(part.answers[id]);
|
|
1000
|
+
});
|
|
1001
|
+
state.note = "";
|
|
1002
|
+
save(form);
|
|
1003
|
+
} else if (result.status === 404) {
|
|
1004
|
+
form.setAttribute("data-pots-state", "gone");
|
|
1005
|
+
show(form, "failed", MESSAGES.gone);
|
|
1006
|
+
state.note = WORDS.gone;
|
|
1007
|
+
forget(form);
|
|
1008
|
+
} else if (result.message === "no-version") state.note = state.place ? WORDS.noVersionKept : WORDS.noVersion;
|
|
1009
|
+
else if (result.message === "too-large") state.note = WORDS.tooLarge;
|
|
1010
|
+
else state.note = WORDS.failed + " (" + result.status + ")";
|
|
1011
|
+
update(form);
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
/** Starts a form: its bar, and its draft put back. */
|
|
1016
|
+
function setUp(form, index) {
|
|
1017
|
+
var state = formState(form);
|
|
1018
|
+
update(form);
|
|
1019
|
+
draftPlace(form, index)
|
|
1020
|
+
.then(function (place) {
|
|
1021
|
+
state.place = place;
|
|
1022
|
+
return place ? readDraft(place) : null;
|
|
1023
|
+
})
|
|
1024
|
+
.then(function (draft) {
|
|
1025
|
+
if (draft) {
|
|
1026
|
+
if (draft.sent && typeof draft.sent === "object") {
|
|
1027
|
+
for (var id in draft.sent) if (!Object.prototype.hasOwnProperty.call(state.sent, id)) state.sent[id] = String(draft.sent[id]);
|
|
1028
|
+
}
|
|
1029
|
+
// What the person typed before the draft was read wins over it.
|
|
1030
|
+
if (!state.touched && draft.fields && typeof draft.fields === "object") {
|
|
1031
|
+
restoring = true;
|
|
1032
|
+
try {
|
|
1033
|
+
restore(form, draft.fields);
|
|
1034
|
+
} finally {
|
|
1035
|
+
restoring = false;
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
state.ready = true;
|
|
1040
|
+
update(form);
|
|
1041
|
+
if (state.touched) save(form);
|
|
1042
|
+
});
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
/** The person changed a field of a form: keep it, and bring the bar up to date. */
|
|
1046
|
+
function changed(event) {
|
|
1047
|
+
var form = event.target && event.target.closest && event.target.closest("form[data-pots]");
|
|
1048
|
+
if (!form || restoring) return;
|
|
1049
|
+
var question = questionOf(event.target);
|
|
1050
|
+
if (question) worked.add(question);
|
|
1051
|
+
var state = formState(form);
|
|
1052
|
+
state.touched = true;
|
|
1053
|
+
if (!state.sending && !ended(form)) state.note = "";
|
|
1054
|
+
save(form);
|
|
1055
|
+
later(form);
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
function saveAll() {
|
|
1059
|
+
states.forEach(function (state, form) {
|
|
1060
|
+
save(form);
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
/** The bars that are fixed to the screen show only while their form crosses its bottom. */
|
|
1065
|
+
var looking = 0;
|
|
1066
|
+
var resized = false;
|
|
1067
|
+
function lookAgain(event) {
|
|
1068
|
+
if (event && event.type === "resize") resized = true;
|
|
1069
|
+
if (looking) return;
|
|
1070
|
+
looking = requestAnimationFrame(function () {
|
|
1071
|
+
var again = resized;
|
|
1072
|
+
looking = 0;
|
|
1073
|
+
resized = false;
|
|
1074
|
+
states.forEach(function (state, form) {
|
|
1075
|
+
if (!state.bar) return;
|
|
1076
|
+
if (again) placeBar(state);
|
|
1077
|
+
showBar(form, state);
|
|
1078
|
+
});
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
|
|
508
1082
|
// ---- Data shown as text ----
|
|
509
1083
|
|
|
510
1084
|
function valueAt(data, path) {
|
|
@@ -610,7 +1184,9 @@
|
|
|
610
1184
|
|
|
611
1185
|
// Light or dark: the reader's choice, from a switch at the page's top corner. It is kept in the browser for every
|
|
612
1186
|
// page of this host, as it is not private; a choice that is the system's own is forgotten, so the page follows the
|
|
613
|
-
// system again.
|
|
1187
|
+
// system again. A page gets the switch when it has a light and a dark look: pots.css, or CSS of its own with
|
|
1188
|
+
// color-scheme: light dark. The choice sets data-pots-theme and color-scheme on <html>, so the page's light-dark()
|
|
1189
|
+
// colours follow it. pots-ui.js has the same switch; the first of the two makes it.
|
|
614
1190
|
function themeSwitch() {
|
|
615
1191
|
if (root.PotsTheme) return;
|
|
616
1192
|
var html = document.documentElement, KEY = "pots-theme", button = null;
|
|
@@ -624,14 +1200,34 @@
|
|
|
624
1200
|
}
|
|
625
1201
|
};
|
|
626
1202
|
var shown = function () { return read() || (system.matches ? "dark" : "light"); };
|
|
1203
|
+
var kitLook = function () { return !!getComputedStyle(html).getPropertyValue("--pots-bg"); };
|
|
1204
|
+
// A colour scheme the page set in <html style>, kept when the reader's choice goes.
|
|
1205
|
+
var own = html.style.colorScheme;
|
|
1206
|
+
// Whether the page itself has both looks, apart from the reader's choice. A page with only one keeps its colour
|
|
1207
|
+
// scheme: dark on a page that set only dark text would hide the text.
|
|
1208
|
+
var both = function () {
|
|
1209
|
+
if (kitLook()) return true;
|
|
1210
|
+
var attribute = html.getAttribute("data-pots-theme"), inline = html.style.colorScheme;
|
|
1211
|
+
html.removeAttribute("data-pots-theme");
|
|
1212
|
+
html.style.colorScheme = own;
|
|
1213
|
+
var scheme = getComputedStyle(html).colorScheme || "";
|
|
1214
|
+
if (attribute) html.setAttribute("data-pots-theme", attribute);
|
|
1215
|
+
html.style.colorScheme = inline;
|
|
1216
|
+
return /\blight\b/.test(scheme) && /\bdark\b/.test(scheme);
|
|
1217
|
+
};
|
|
627
1218
|
var apply = function () {
|
|
628
1219
|
var v = read();
|
|
629
1220
|
if (v) html.setAttribute("data-pots-theme", v);
|
|
630
1221
|
else html.removeAttribute("data-pots-theme");
|
|
1222
|
+
html.style.colorScheme = v && both() ? v : own;
|
|
631
1223
|
if (!button) return;
|
|
632
1224
|
var dark = shown() === "dark";
|
|
633
|
-
button.setAttribute("data-icon", dark ? "sun" : "moon");
|
|
634
1225
|
button.setAttribute("aria-pressed", String(dark));
|
|
1226
|
+
if (!button.ownIcons) button.setAttribute("data-icon", dark ? "sun" : "moon");
|
|
1227
|
+
else if (button.firstChild !== button.ownIcons[dark ? 1 : 0]) {
|
|
1228
|
+
while (button.firstChild) button.removeChild(button.firstChild);
|
|
1229
|
+
button.appendChild(button.ownIcons[dark ? 1 : 0]);
|
|
1230
|
+
}
|
|
635
1231
|
};
|
|
636
1232
|
var set = function (v) {
|
|
637
1233
|
try {
|
|
@@ -644,15 +1240,20 @@
|
|
|
644
1240
|
root.PotsTheme = {
|
|
645
1241
|
get: shown,
|
|
646
1242
|
set: set,
|
|
647
|
-
// The button, once the body is there, and only on a page that
|
|
1243
|
+
// The button, once the body is there, and only on a page that has both looks. On a page without pots.css it
|
|
1244
|
+
// draws its own icons and has a plain look that the page's own CSS for .pots-theme overrides.
|
|
648
1245
|
button: function () {
|
|
649
|
-
if (button || !document.body || !
|
|
1246
|
+
if (button || !document.body || !both()) return;
|
|
650
1247
|
var zh = /^zh/i.test(html.lang || "");
|
|
651
1248
|
button = document.createElement("button");
|
|
652
1249
|
button.className = "pots-theme";
|
|
653
1250
|
button.type = "button";
|
|
654
1251
|
button.setAttribute("aria-label", zh ? "深色模式" : "Dark mode");
|
|
655
1252
|
button.title = zh ? "切换浅色或深色" : "Switch light or dark";
|
|
1253
|
+
if (!kitLook()) {
|
|
1254
|
+
button.ownIcons = [themeIcon("moon"), themeIcon("sun")];
|
|
1255
|
+
themeLook();
|
|
1256
|
+
}
|
|
656
1257
|
button.addEventListener("click", function () { set(shown() === "dark" ? "light" : "dark"); });
|
|
657
1258
|
document.body.insertBefore(button, document.body.firstChild);
|
|
658
1259
|
apply();
|
|
@@ -662,6 +1263,76 @@
|
|
|
662
1263
|
window.addEventListener("storage", function (event) { if (event.key === KEY) apply(); });
|
|
663
1264
|
}
|
|
664
1265
|
|
|
1266
|
+
/** The switch's icon on a page without pots.css: the kit's moon or sun, drawn with the button's text colour. */
|
|
1267
|
+
function themeIcon(name) {
|
|
1268
|
+
var ns = "http://www.w3.org/2000/svg";
|
|
1269
|
+
var svg = document.createElementNS(ns, "svg");
|
|
1270
|
+
var attributes = { viewBox: "0 0 24 24", width: "18", height: "18", fill: "none", stroke: "currentColor", "stroke-width": "2", "stroke-linecap": "round", "stroke-linejoin": "round", "aria-hidden": "true" };
|
|
1271
|
+
for (var key in attributes) svg.setAttribute(key, attributes[key]);
|
|
1272
|
+
var shapes = name === "sun"
|
|
1273
|
+
? [["circle", { cx: "12", cy: "12", r: "4" }], ["path", { d: "M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4" }]]
|
|
1274
|
+
: [["path", { d: "M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z" }]];
|
|
1275
|
+
shapes.forEach(function (shape) {
|
|
1276
|
+
var element = document.createElementNS(ns, shape[0]);
|
|
1277
|
+
for (var attribute in shape[1]) element.setAttribute(attribute, shape[1][attribute]);
|
|
1278
|
+
svg.appendChild(element);
|
|
1279
|
+
});
|
|
1280
|
+
return svg;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
/**
|
|
1284
|
+
* The switch's plain look on a page without pots.css: at the top corner, in the page's text colour. Every rule is in
|
|
1285
|
+
* :where(), so any rule of the page's own for .pots-theme, or for its buttons, wins.
|
|
1286
|
+
*/
|
|
1287
|
+
function themeLook() {
|
|
1288
|
+
addLook(
|
|
1289
|
+
":where(.pots-theme){position:absolute;top:8px;inset-inline-end:8px;z-index:10;display:grid;place-items:center;" +
|
|
1290
|
+
"width:32px;height:32px;margin:0;padding:0;border:0;border-radius:6px;background:transparent;color:inherit;" +
|
|
1291
|
+
"opacity:.6;cursor:pointer}:where(.pots-theme:hover,.pots-theme:focus-visible){opacity:1}" +
|
|
1292
|
+
"@media print{:where(.pots-theme){display:none}}",
|
|
1293
|
+
);
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
/**
|
|
1297
|
+
* The plain look of the partial send's bar and of a question's mark when Pots has its answer: the bar floats at the
|
|
1298
|
+
* bottom of the screen, at the end of the line. Every rule is in :where(), so any rule of the page's own for
|
|
1299
|
+
* .pots-partial, its buttons or [data-pots-sent] wins; only a hidden bar stays hidden.
|
|
1300
|
+
*/
|
|
1301
|
+
var partLooked = false;
|
|
1302
|
+
function partLook() {
|
|
1303
|
+
if (partLooked) return;
|
|
1304
|
+
partLooked = true;
|
|
1305
|
+
addLook(
|
|
1306
|
+
":where(.pots-partial){position:sticky;bottom:max(12px,env(safe-area-inset-bottom,0px));z-index:5;grid-column:1/-1;display:flex;" +
|
|
1307
|
+
"flex-wrap:wrap;align-items:center;justify-content:flex-end;gap:4px 12px;box-sizing:border-box;width:fit-content;" +
|
|
1308
|
+
"max-width:100%;margin-block:16px 4px;margin-inline:auto 0;padding:6px 6px 6px 14px;" +
|
|
1309
|
+
"border:1px solid color-mix(in srgb,currentColor 20%,transparent);border-radius:10px;" +
|
|
1310
|
+
"background:var(--pots-bg,Canvas);color:var(--pots-text,CanvasText);box-shadow:0 4px 18px rgb(0 0 0/.14);" +
|
|
1311
|
+
"font-size:15px;line-height:1.4}" +
|
|
1312
|
+
":where(.pots-partial[hidden]){display:none!important}" +
|
|
1313
|
+
":where(.pots-partial[data-pots-fixed]){position:fixed;inset-inline-end:12px;margin:0}" +
|
|
1314
|
+
":where(.pots-partial button){margin:0;padding:6px 14px;border:0;border-radius:7px;" +
|
|
1315
|
+
"background:var(--pots-accent,CanvasText);color:var(--pots-bg,Canvas);font:inherit;font-weight:600;cursor:pointer}" +
|
|
1316
|
+
":where(.pots-partial button:disabled){opacity:.45;cursor:default}" +
|
|
1317
|
+
":where([data-pots-question][data-pots-sent])::after{content:attr(data-pots-sent);display:block;margin-top:6px;" +
|
|
1318
|
+
"font-size:.875em;font-weight:normal;color:light-dark(#2e7d4f,#7cc99a)}" +
|
|
1319
|
+
"@media print{:where(.pots-partial){display:none}}",
|
|
1320
|
+
);
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
/** Adds CSS of the kit's own to the page, after the page's own. */
|
|
1324
|
+
function addLook(rules) {
|
|
1325
|
+
try {
|
|
1326
|
+
var sheet = new CSSStyleSheet();
|
|
1327
|
+
sheet.replaceSync(rules);
|
|
1328
|
+
document.adoptedStyleSheets = document.adoptedStyleSheets.concat([sheet]);
|
|
1329
|
+
} catch (e) {
|
|
1330
|
+
var style = document.createElement("style");
|
|
1331
|
+
style.textContent = rules;
|
|
1332
|
+
document.head.appendChild(style);
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
|
|
665
1336
|
function start() {
|
|
666
1337
|
try {
|
|
667
1338
|
root.PotsTheme.button();
|
|
@@ -673,6 +1344,27 @@
|
|
|
673
1344
|
document.addEventListener("change", function (event) {
|
|
674
1345
|
if (event.target && event.target.closest && event.target.closest("form")) dirty = true;
|
|
675
1346
|
});
|
|
1347
|
+
// Drafts and partial sends. A click can change fields through the page's own script, which sends no event.
|
|
1348
|
+
document.addEventListener("input", changed);
|
|
1349
|
+
document.addEventListener("change", changed);
|
|
1350
|
+
document.addEventListener("click", function (event) {
|
|
1351
|
+
var form = event.target && event.target.closest && event.target.closest("form[data-pots]");
|
|
1352
|
+
if (!form || !states.has(form)) return;
|
|
1353
|
+
var question = questionOf(event.target);
|
|
1354
|
+
if (question) worked.add(question);
|
|
1355
|
+
setTimeout(function () {
|
|
1356
|
+
save(form);
|
|
1357
|
+
later(form);
|
|
1358
|
+
}, 0);
|
|
1359
|
+
});
|
|
1360
|
+
document.addEventListener("visibilitychange", function () {
|
|
1361
|
+
if (document.visibilityState === "hidden") saveAll();
|
|
1362
|
+
});
|
|
1363
|
+
window.addEventListener("pagehide", saveAll);
|
|
1364
|
+
window.addEventListener("scroll", lookAgain, { passive: true });
|
|
1365
|
+
window.addEventListener("resize", lookAgain);
|
|
1366
|
+
if (linked) sweepDrafts();
|
|
1367
|
+
Array.prototype.forEach.call(document.querySelectorAll("form[data-pots]"), setUp);
|
|
676
1368
|
var refresh = document.body.getAttribute("data-pots-refresh");
|
|
677
1369
|
loadSources();
|
|
678
1370
|
if (refresh !== null) watch(Number(refresh) || 5);
|