@monster-orange/den-ui 0.7.5 → 0.7.6
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/README.md +33 -0
- package/package.json +3 -1
- package/src/kit.d.ts +7 -0
- package/src/kit.js +10 -0
- package/src/savestate.css +75 -0
- package/src/savestate.d.ts +73 -0
- package/src/savestate.js +294 -0
- package/src/strings.js +16 -0
package/README.md
CHANGED
|
@@ -142,6 +142,7 @@ syncPinToggles }`.
|
|
|
142
142
|
DenUI.update.set({ version }); // the update pill; .hide()
|
|
143
143
|
DenUI.report.open({ prefill }); // ⌘⇧F and any [data-den-report] too
|
|
144
144
|
DenUI.busy.run(action, run, opts) // the working pop-up (busy.js)
|
|
145
|
+
DenUI.saveState(el, { save, copyText, watch }) // save-state line
|
|
145
146
|
|
|
146
147
|
### The adapter
|
|
147
148
|
|
|
@@ -441,6 +442,38 @@ never converted to HTML and back).
|
|
|
441
442
|
- The content element has `data-md-dirty` while it holds unsaved text
|
|
442
443
|
(for an update pill's `askFirst`).
|
|
443
444
|
|
|
445
|
+
## The save-state line (`den-savestate`)
|
|
446
|
+
|
|
447
|
+
- `src/savestate.js`, `src/savestate.css` (exports `./savestate`,
|
|
448
|
+
`./savestate.css`): what an autosaving editor says about its text. It
|
|
449
|
+
works with any input, textarea or editor; the host gives the save.
|
|
450
|
+
|
|
451
|
+
const line = createSaveState(document.querySelector("#save-line"), {
|
|
452
|
+
save: () => api.saveDraft(field.value), // rejects on failure
|
|
453
|
+
copyText: () => `${title.value}\n\n${field.value}`,
|
|
454
|
+
watch: field, // input → changed(), focusout → flush()
|
|
455
|
+
});
|
|
456
|
+
// line.changed(), line.flush(), line.retry(), line.state(), line.destroy()
|
|
457
|
+
|
|
458
|
+
- The element becomes `<p class="den-savestate">` with `role="status"`,
|
|
459
|
+
`aria-live="off"` and `data-state` (`idle`, `saving`, `saved`,
|
|
460
|
+
`error`): a 6 px dot, then the words, 13 px on a 20 px line (idle is
|
|
461
|
+
empty and keeps its height).
|
|
462
|
+
- It saves 800 ms after the last change, and at once on blur and when the
|
|
463
|
+
page goes hidden. "Saving…" shows only when a save takes more than
|
|
464
|
+
400 ms; "Saved" (title "Saved at {time}", from `time.js`) stays until the
|
|
465
|
+
next save. A failure shows "Not saved. Retrying…" with Copy text (what
|
|
466
|
+
`copyText()` returns), retries at 2 s, 4 s and 8 s, then every 30 s, and
|
|
467
|
+
at once when the browser is back online.
|
|
468
|
+
- A visually hidden polite region beside the line says only the error and
|
|
469
|
+
its recovery; `aria-live="off"` keeps the routine "Saved" quiet.
|
|
470
|
+
- Only the dot's colour moves, over `--dur-fast`; none under reduced
|
|
471
|
+
motion. Its words are `kit.savestate.*` in `strings.js` (en, es, ja).
|
|
472
|
+
- `saveScheduler({ save, onChange })` is the same timing with no DOM.
|
|
473
|
+
- Link `savestate.css` in the page's `<head>`. Without that, savestate.js
|
|
474
|
+
links it from `opts.css`, else from beside another kit stylesheet on the
|
|
475
|
+
page.
|
|
476
|
+
|
|
444
477
|
## 0.7.1
|
|
445
478
|
|
|
446
479
|
The top bar's search sits in the same place in every product (issue 303).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@monster-orange/den-ui",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.6",
|
|
4
4
|
"description": "Den's UI kit: tokens, fonts and shared components, framework-free",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"./busy": "./src/busy.js",
|
|
21
21
|
"./busy-words": "./src/busy-words.js",
|
|
22
22
|
"./dialog": "./src/dialog.js",
|
|
23
|
+
"./savestate": "./src/savestate.js",
|
|
23
24
|
"./strings": "./src/strings.js",
|
|
24
25
|
"./icons": "./src/icons.js",
|
|
25
26
|
"./shell-html": "./src/shell-html.js",
|
|
@@ -36,6 +37,7 @@
|
|
|
36
37
|
"./dialog.css": "./src/dialog.css",
|
|
37
38
|
"./markdown-editor": "./src/markdown-editor.js",
|
|
38
39
|
"./markdown-editor.css": "./src/markdown-editor.css",
|
|
40
|
+
"./savestate.css": "./src/savestate.css",
|
|
39
41
|
"./inventory": {
|
|
40
42
|
"types": "./inventory/nav-inventory.d.ts",
|
|
41
43
|
"default": "./inventory/nav-inventory.js"
|
package/src/kit.d.ts
CHANGED
|
@@ -53,6 +53,7 @@ import type {
|
|
|
53
53
|
} from "./shell-html.js";
|
|
54
54
|
import type { syncPinToggles } from "./shell-client.js";
|
|
55
55
|
import type { UpdatePill } from "./update-pill.js";
|
|
56
|
+
import type { SaveState, SaveStateOptions } from "./savestate.js";
|
|
56
57
|
|
|
57
58
|
/** Every call to the product goes through it; the kit names no URL. */
|
|
58
59
|
export interface Adapter extends ReportAdapter {
|
|
@@ -158,6 +159,11 @@ export declare const busy: {
|
|
|
158
159
|
): Promise<T | null>;
|
|
159
160
|
configure: typeof configureBusy;
|
|
160
161
|
};
|
|
162
|
+
/** An autosaving editor's save-state line, in the kit's language. */
|
|
163
|
+
export declare function saveState(
|
|
164
|
+
el: HTMLElement,
|
|
165
|
+
opts: SaveStateOptions,
|
|
166
|
+
): SaveState;
|
|
161
167
|
export declare function hydrate(
|
|
162
168
|
root: ParentNode,
|
|
163
169
|
opts?: KitHydrateOptions,
|
|
@@ -208,6 +214,7 @@ export interface DenUIKit {
|
|
|
208
214
|
update: typeof update;
|
|
209
215
|
report: typeof report;
|
|
210
216
|
busy: typeof busy;
|
|
217
|
+
saveState: typeof saveState;
|
|
211
218
|
}
|
|
212
219
|
declare const DenUI: DenUIKit;
|
|
213
220
|
export default DenUI;
|
package/src/kit.js
CHANGED
|
@@ -42,6 +42,9 @@
|
|
|
42
42
|
* focus goes back to opener (else the
|
|
43
43
|
* control that opened it) on close
|
|
44
44
|
* DenUI.busy.run(action, run, opts) the working pop-up (busy.js)
|
|
45
|
+
* DenUI.saveState(el, { save, copyText, watch }) an autosaving
|
|
46
|
+
* editor's save-state line (savestate.js),
|
|
47
|
+
* in the kit's language
|
|
45
48
|
* DenUI.strings(lang, table) the product's own words, or its versions
|
|
46
49
|
* of the kit's; DenUI.t(key, vars)
|
|
47
50
|
*/
|
|
@@ -92,6 +95,7 @@ import {
|
|
|
92
95
|
import { configureReport, openReport } from "./report.js";
|
|
93
96
|
import { showUpdate } from "./update-pill.js";
|
|
94
97
|
import { busyAction, configureBusy } from "./busy.js";
|
|
98
|
+
import { createSaveState } from "./savestate.js";
|
|
95
99
|
|
|
96
100
|
const state = {
|
|
97
101
|
lang: "en",
|
|
@@ -183,6 +187,11 @@ export const busy = {
|
|
|
183
187
|
configure: configureBusy,
|
|
184
188
|
};
|
|
185
189
|
|
|
190
|
+
/** An autosaving editor's save-state line, in the kit's language. */
|
|
191
|
+
export function saveState(el, opts = {}) {
|
|
192
|
+
return createSaveState(el, { lang, ...opts });
|
|
193
|
+
}
|
|
194
|
+
|
|
186
195
|
/**
|
|
187
196
|
* Binds the drawn shell, the chip and Report (see the top), and the 0.7.0
|
|
188
197
|
* pieces it finds: the scope crumbs (opts.scope), the search
|
|
@@ -294,6 +303,7 @@ const DenUI = {
|
|
|
294
303
|
update,
|
|
295
304
|
report,
|
|
296
305
|
busy,
|
|
306
|
+
saveState,
|
|
297
307
|
};
|
|
298
308
|
|
|
299
309
|
if (typeof window !== "undefined") window.DenUI = DenUI;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/* den-ui: the save-state line's look (savestate.js). Tokens only: no
|
|
2
|
+
colour of its own. Linked, never injected: clean under style-src 'self'.
|
|
3
|
+
The dot's colour is the only thing that moves (var(--dur-fast)). */
|
|
4
|
+
.den-savestate {
|
|
5
|
+
display: flex;
|
|
6
|
+
align-items: center;
|
|
7
|
+
gap: var(--space-1);
|
|
8
|
+
margin: 0;
|
|
9
|
+
min-height: 20px;
|
|
10
|
+
font-family: var(--sans);
|
|
11
|
+
font-size: var(--fs-s);
|
|
12
|
+
font-weight: 400;
|
|
13
|
+
line-height: 20px;
|
|
14
|
+
color: var(--muted);
|
|
15
|
+
}
|
|
16
|
+
.den-savestate::before {
|
|
17
|
+
content: "";
|
|
18
|
+
flex: none;
|
|
19
|
+
width: 6px;
|
|
20
|
+
height: 6px;
|
|
21
|
+
border-radius: 50%;
|
|
22
|
+
background-color: transparent;
|
|
23
|
+
transition: background-color var(--dur-fast) ease;
|
|
24
|
+
}
|
|
25
|
+
/* idle: no words and no dot; the line keeps its height. */
|
|
26
|
+
.den-savestate[data-state="idle"]::before {
|
|
27
|
+
visibility: hidden;
|
|
28
|
+
}
|
|
29
|
+
.den-savestate[data-state="saving"]::before {
|
|
30
|
+
background-color: var(--faint);
|
|
31
|
+
}
|
|
32
|
+
.den-savestate[data-state="saved"]::before {
|
|
33
|
+
background-color: var(--ok);
|
|
34
|
+
}
|
|
35
|
+
.den-savestate[data-state="error"] {
|
|
36
|
+
color: var(--bad);
|
|
37
|
+
}
|
|
38
|
+
.den-savestate[data-state="error"]::before {
|
|
39
|
+
background-color: var(--bad);
|
|
40
|
+
}
|
|
41
|
+
/* Copy text: a link-style button after the words. */
|
|
42
|
+
.den-savestate-copy {
|
|
43
|
+
font: inherit;
|
|
44
|
+
margin: 0;
|
|
45
|
+
margin-inline-start: var(--space-1);
|
|
46
|
+
padding: 0;
|
|
47
|
+
border: 0;
|
|
48
|
+
background: none;
|
|
49
|
+
color: var(--accent-text);
|
|
50
|
+
text-decoration: underline;
|
|
51
|
+
text-underline-offset: 2px;
|
|
52
|
+
cursor: pointer;
|
|
53
|
+
}
|
|
54
|
+
.den-savestate-copy:focus-visible {
|
|
55
|
+
outline: 2px solid var(--accent);
|
|
56
|
+
outline-offset: 2px;
|
|
57
|
+
border-radius: 2px;
|
|
58
|
+
}
|
|
59
|
+
/* The live region beside the line: heard, never seen. */
|
|
60
|
+
.den-savestate-sr {
|
|
61
|
+
position: absolute;
|
|
62
|
+
width: 1px;
|
|
63
|
+
height: 1px;
|
|
64
|
+
padding: 0;
|
|
65
|
+
margin: -1px;
|
|
66
|
+
overflow: hidden;
|
|
67
|
+
clip: rect(0 0 0 0);
|
|
68
|
+
white-space: nowrap;
|
|
69
|
+
border: 0;
|
|
70
|
+
}
|
|
71
|
+
@media (prefers-reduced-motion: reduce) {
|
|
72
|
+
.den-savestate::before {
|
|
73
|
+
transition: none;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** Save 800 ms after the last change. */
|
|
2
|
+
export declare const SAVE_AFTER_MS: number;
|
|
3
|
+
/** "Saving…" only when a save takes longer than this (400 ms). */
|
|
4
|
+
export declare const SAVING_AFTER_MS: number;
|
|
5
|
+
/** The first retries after a failure: 2 s, 4 s, 8 s. */
|
|
6
|
+
export declare const RETRY_MS: readonly number[];
|
|
7
|
+
/** Then a retry every 30 s. */
|
|
8
|
+
export declare const RETRY_EVERY_MS: number;
|
|
9
|
+
|
|
10
|
+
export type SaveStateName = "idle" | "saving" | "saved" | "error";
|
|
11
|
+
|
|
12
|
+
/** One change of state. announce: what a screen reader is told, if anything. */
|
|
13
|
+
export interface SaveStateChange {
|
|
14
|
+
state: SaveStateName;
|
|
15
|
+
/** saved: when it saved. */
|
|
16
|
+
at?: Date;
|
|
17
|
+
/** "error" on the first failure, "recovered" on the success after it. */
|
|
18
|
+
announce: "error" | "recovered" | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The kit's words, replaced for this line only. {time} fills savedAt. */
|
|
22
|
+
export interface SaveStateWords {
|
|
23
|
+
saving?: string;
|
|
24
|
+
saved?: string;
|
|
25
|
+
error?: string;
|
|
26
|
+
copy?: string;
|
|
27
|
+
savedAt?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SaveStateOptions {
|
|
31
|
+
/** The host's save; it rejects when the text did not save. */
|
|
32
|
+
save: () => Promise<unknown>;
|
|
33
|
+
/** What Copy text copies (the title and body, say). None: no Copy text. */
|
|
34
|
+
copyText?: (() => string) | null;
|
|
35
|
+
/** Its `input` is changed(), its `focusout` (blur) is flush(). */
|
|
36
|
+
watch?: EventTarget | null;
|
|
37
|
+
/** The words' language (default: <html lang>). */
|
|
38
|
+
lang?: string | (() => string);
|
|
39
|
+
words?: SaveStateWords;
|
|
40
|
+
/** Where savestate.css is, for a page that doesn't link it. */
|
|
41
|
+
css?: string | null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface SaveScheduler {
|
|
45
|
+
/** The text changed: a save SAVE_AFTER_MS after the last change. */
|
|
46
|
+
changed(): void;
|
|
47
|
+
/** Save now when anything changed; resolves when that save settles. */
|
|
48
|
+
flush(): Promise<void>;
|
|
49
|
+
/** After a failure, try again now (online); else nothing. */
|
|
50
|
+
retry(): Promise<void>;
|
|
51
|
+
state(): SaveStateName;
|
|
52
|
+
/** Every timer stops. */
|
|
53
|
+
destroy(): void;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type SaveState = SaveScheduler;
|
|
57
|
+
|
|
58
|
+
/** The timing with no DOM: onChange on each change of state. */
|
|
59
|
+
export declare function saveScheduler(opts: {
|
|
60
|
+
save: () => Promise<unknown>;
|
|
61
|
+
onChange?: (change: SaveStateChange) => void;
|
|
62
|
+
}): SaveScheduler;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The save-state line in `el`: <p class="den-savestate" data-state
|
|
66
|
+
* role="status" aria-live="off">, with a polite live region beside it for
|
|
67
|
+
* the error and its recovery. destroy() also removes its listeners and the
|
|
68
|
+
* live region.
|
|
69
|
+
*/
|
|
70
|
+
export declare function createSaveState(
|
|
71
|
+
el: HTMLElement,
|
|
72
|
+
opts: SaveStateOptions,
|
|
73
|
+
): SaveState;
|
package/src/savestate.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The save-state line: what an autosaving editor says about its text.
|
|
3
|
+
* Framework-free, and blind to the editor: any input, textarea or editor
|
|
4
|
+
* that tells it "changed" works. The host gives the save and what Copy
|
|
5
|
+
* text copies:
|
|
6
|
+
*
|
|
7
|
+
* const line = createSaveState(el, {
|
|
8
|
+
* save: () => Promise<void>, the host's save (rejects on failure)
|
|
9
|
+
* copyText: () => string, error's Copy text: the title and body,
|
|
10
|
+
* say (none: no Copy text)
|
|
11
|
+
* watch: field, optional: its `input` is changed(), its
|
|
12
|
+
* `focusout` (blur) is flush()
|
|
13
|
+
* lang: "en" | () => "en", default: <html lang>
|
|
14
|
+
* words: { saving, saved, error, copy, savedAt }, optional overrides
|
|
15
|
+
* of the kit's words (strings.js)
|
|
16
|
+
* css: "/kit/savestate.css", where savestate.css is, for a page that
|
|
17
|
+
* doesn't link it (default: beside another
|
|
18
|
+
* kit stylesheet on the page)
|
|
19
|
+
* })
|
|
20
|
+
* line.changed() the text changed: a save SAVE_AFTER_MS after the last
|
|
21
|
+
* line.flush() save now, when anything changed (→ Promise)
|
|
22
|
+
* line.retry() after a failure, try again now (→ Promise)
|
|
23
|
+
* line.state() "idle" | "saving" | "saved" | "error"
|
|
24
|
+
* line.destroy() timers and listeners off; the live region removed
|
|
25
|
+
*
|
|
26
|
+
* The element becomes <p class="den-savestate" data-state="…"
|
|
27
|
+
* role="status" aria-live="off">: a dot, then the words. aria-live="off"
|
|
28
|
+
* keeps the routine "Saved" quiet; a separate, visually hidden polite
|
|
29
|
+
* region beside it says only the error and its recovery.
|
|
30
|
+
*
|
|
31
|
+
* idle nothing (the line keeps its height)
|
|
32
|
+
* saving "Saving…", only when a save takes more than SAVING_AFTER_MS
|
|
33
|
+
* saved "Saved", title "Saved at {time}"; stays until the next save
|
|
34
|
+
* error "Not saved. Retrying…" and Copy text; retries at RETRY_MS,
|
|
35
|
+
* then every RETRY_EVERY_MS, and at once when the browser is
|
|
36
|
+
* back online; a success is "saved" again
|
|
37
|
+
*
|
|
38
|
+
* A page going hidden (visibilitychange) saves at once.
|
|
39
|
+
*
|
|
40
|
+
* saveScheduler({ save, onChange }) is the same timing with no DOM (a
|
|
41
|
+
* server, a test, a host's own view): onChange({ state, at?, announce? })
|
|
42
|
+
* on each change of state; announce is "error" or "recovered", else null.
|
|
43
|
+
*
|
|
44
|
+
* Its look is savestate.css (linked from beside this file).
|
|
45
|
+
*/
|
|
46
|
+
import { kitT } from "./strings.js";
|
|
47
|
+
import { formatTime } from "./time.js";
|
|
48
|
+
|
|
49
|
+
export const SAVE_AFTER_MS = 800;
|
|
50
|
+
export const SAVING_AFTER_MS = 400;
|
|
51
|
+
export const RETRY_MS = [2000, 4000, 8000];
|
|
52
|
+
export const RETRY_EVERY_MS = 30_000;
|
|
53
|
+
|
|
54
|
+
/** The timing of saves and their states, with no DOM. */
|
|
55
|
+
export function saveScheduler({ save, onChange }) {
|
|
56
|
+
let state = "idle";
|
|
57
|
+
let dirty = false;
|
|
58
|
+
let failing = false;
|
|
59
|
+
let again = false;
|
|
60
|
+
let step = 0;
|
|
61
|
+
let dead = false;
|
|
62
|
+
let debounce = 0;
|
|
63
|
+
let retryTimer = 0;
|
|
64
|
+
let running = null;
|
|
65
|
+
|
|
66
|
+
const set = (s, extra) => {
|
|
67
|
+
if (dead) return;
|
|
68
|
+
state = s;
|
|
69
|
+
onChange?.({ state: s, announce: null, ...extra });
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
function run() {
|
|
73
|
+
if (dead) return Promise.resolve();
|
|
74
|
+
// A save is out: save again as soon as it ends.
|
|
75
|
+
if (running) {
|
|
76
|
+
again = true;
|
|
77
|
+
return running;
|
|
78
|
+
}
|
|
79
|
+
clearTimeout(retryTimer);
|
|
80
|
+
retryTimer = 0;
|
|
81
|
+
dirty = false;
|
|
82
|
+
// While in error the line keeps saying "Retrying…".
|
|
83
|
+
const show = failing ? 0 : setTimeout(() => set("saving"), SAVING_AFTER_MS);
|
|
84
|
+
running = (async () => {
|
|
85
|
+
let ok = true;
|
|
86
|
+
try {
|
|
87
|
+
await save();
|
|
88
|
+
} catch {
|
|
89
|
+
ok = false;
|
|
90
|
+
}
|
|
91
|
+
clearTimeout(show);
|
|
92
|
+
running = null;
|
|
93
|
+
if (dead) return;
|
|
94
|
+
if (ok) {
|
|
95
|
+
const recovered = failing;
|
|
96
|
+
failing = false;
|
|
97
|
+
step = 0;
|
|
98
|
+
set("saved", {
|
|
99
|
+
at: new Date(),
|
|
100
|
+
announce: recovered ? "recovered" : null,
|
|
101
|
+
});
|
|
102
|
+
} else {
|
|
103
|
+
dirty = true;
|
|
104
|
+
if (!failing) {
|
|
105
|
+
failing = true;
|
|
106
|
+
set("error", { announce: "error" });
|
|
107
|
+
}
|
|
108
|
+
const wait = RETRY_MS[step] ?? RETRY_EVERY_MS;
|
|
109
|
+
step++;
|
|
110
|
+
retryTimer = setTimeout(() => {
|
|
111
|
+
retryTimer = 0;
|
|
112
|
+
void run();
|
|
113
|
+
}, wait);
|
|
114
|
+
}
|
|
115
|
+
if (again) {
|
|
116
|
+
again = false;
|
|
117
|
+
if (dirty && !failing) return run();
|
|
118
|
+
}
|
|
119
|
+
})();
|
|
120
|
+
return running;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
changed() {
|
|
125
|
+
if (dead) return;
|
|
126
|
+
dirty = true;
|
|
127
|
+
clearTimeout(debounce);
|
|
128
|
+
debounce = setTimeout(() => {
|
|
129
|
+
debounce = 0;
|
|
130
|
+
void run();
|
|
131
|
+
}, SAVE_AFTER_MS);
|
|
132
|
+
},
|
|
133
|
+
flush() {
|
|
134
|
+
clearTimeout(debounce);
|
|
135
|
+
debounce = 0;
|
|
136
|
+
if (dead || !dirty) return running ?? Promise.resolve();
|
|
137
|
+
return run();
|
|
138
|
+
},
|
|
139
|
+
retry() {
|
|
140
|
+
if (dead || !failing) return running ?? Promise.resolve();
|
|
141
|
+
return run();
|
|
142
|
+
},
|
|
143
|
+
state: () => state,
|
|
144
|
+
destroy() {
|
|
145
|
+
dead = true;
|
|
146
|
+
clearTimeout(debounce);
|
|
147
|
+
clearTimeout(retryTimer);
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The look is savestate.css: linked, never injected, so a page under
|
|
154
|
+
* style-src 'self' takes it. A page that links it (in its <head>) keeps
|
|
155
|
+
* its own link. Else it is linked from opts.css, or from the folder of
|
|
156
|
+
* another kit stylesheet on the page (tokens.css, say), as dialog.js does.
|
|
157
|
+
*/
|
|
158
|
+
const KIT_SHEET =
|
|
159
|
+
/(^|\/)(tokens|base|fonts|busy|dialog|shell|update-pill)\.css$/;
|
|
160
|
+
function ensureCss(css) {
|
|
161
|
+
if (document.getElementById("ss-css")) return;
|
|
162
|
+
let near = null;
|
|
163
|
+
for (const l of document.querySelectorAll('link[rel="stylesheet"]')) {
|
|
164
|
+
const u = new URL(l.href, location.href);
|
|
165
|
+
if (/(^|\/)savestate\.css$/.test(u.pathname)) return;
|
|
166
|
+
if (!near && KIT_SHEET.test(u.pathname)) near = u;
|
|
167
|
+
}
|
|
168
|
+
const href =
|
|
169
|
+
css ||
|
|
170
|
+
(near ? new URL("savestate.css", near).href.replace(/\?.*$/, "") : null);
|
|
171
|
+
if (!href) return;
|
|
172
|
+
const l = document.createElement("link");
|
|
173
|
+
l.id = "ss-css";
|
|
174
|
+
l.rel = "stylesheet";
|
|
175
|
+
l.href = href;
|
|
176
|
+
document.head.append(l);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Plain text to the clipboard. Kept here rather than imported from
|
|
181
|
+
* whatsnew.js, which brings the shell's modules with it. The fallback
|
|
182
|
+
* never focuses its textarea: focus stays on Copy text.
|
|
183
|
+
*/
|
|
184
|
+
function copy(text) {
|
|
185
|
+
if (navigator.clipboard?.writeText)
|
|
186
|
+
return navigator.clipboard.writeText(text);
|
|
187
|
+
return new Promise((resolve, reject) => {
|
|
188
|
+
const ta = document.createElement("textarea");
|
|
189
|
+
ta.value = text;
|
|
190
|
+
ta.className = "den-savestate-sr";
|
|
191
|
+
ta.setAttribute("aria-hidden", "true");
|
|
192
|
+
document.body.append(ta);
|
|
193
|
+
ta.select();
|
|
194
|
+
try {
|
|
195
|
+
document.execCommand("copy") ? resolve() : reject(new Error("copy"));
|
|
196
|
+
} catch (e) {
|
|
197
|
+
reject(e);
|
|
198
|
+
} finally {
|
|
199
|
+
ta.remove();
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The save-state line in `el` (see the top of this file). */
|
|
205
|
+
export function createSaveState(el, opts = {}) {
|
|
206
|
+
const { save, copyText, watch, words = {} } = opts;
|
|
207
|
+
const lang = () => {
|
|
208
|
+
const l = typeof opts.lang === "function" ? opts.lang() : opts.lang;
|
|
209
|
+
return l || document.documentElement.lang?.slice(0, 2) || "en";
|
|
210
|
+
};
|
|
211
|
+
const word = (key, vars) =>
|
|
212
|
+
words[key] != null
|
|
213
|
+
? String(words[key]).replace(/\{(\w+)\}/g, (m, k) => vars?.[k] ?? m)
|
|
214
|
+
: kitT(lang(), `kit.savestate.${key}`, vars);
|
|
215
|
+
|
|
216
|
+
ensureCss(opts.css);
|
|
217
|
+
el.classList.add("den-savestate");
|
|
218
|
+
el.setAttribute("role", "status");
|
|
219
|
+
el.setAttribute("aria-live", "off");
|
|
220
|
+
el.dataset.state = "idle";
|
|
221
|
+
const text = document.createElement("span");
|
|
222
|
+
text.className = "den-savestate-text";
|
|
223
|
+
el.replaceChildren(text);
|
|
224
|
+
const live = document.createElement("span");
|
|
225
|
+
live.className = "den-savestate-sr";
|
|
226
|
+
live.setAttribute("aria-live", "polite");
|
|
227
|
+
el.after(live);
|
|
228
|
+
let btn = null;
|
|
229
|
+
|
|
230
|
+
// The error and the recovery alternate, so each is new words to say.
|
|
231
|
+
const say = (s) => {
|
|
232
|
+
live.textContent = s;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
function paint({ state, at, announce }) {
|
|
236
|
+
el.dataset.state = state;
|
|
237
|
+
text.textContent =
|
|
238
|
+
state === "saving"
|
|
239
|
+
? word("saving")
|
|
240
|
+
: state === "saved"
|
|
241
|
+
? word("saved")
|
|
242
|
+
: state === "error"
|
|
243
|
+
? word("error")
|
|
244
|
+
: "";
|
|
245
|
+
const savedAt =
|
|
246
|
+
state === "saved"
|
|
247
|
+
? word("savedAt", { time: formatTime(at, { style: "time" }) })
|
|
248
|
+
: "";
|
|
249
|
+
if (savedAt) el.title = savedAt;
|
|
250
|
+
else el.removeAttribute("title");
|
|
251
|
+
if (state === "error" && copyText && !btn) {
|
|
252
|
+
btn = document.createElement("button");
|
|
253
|
+
btn.type = "button";
|
|
254
|
+
btn.className = "den-savestate-copy";
|
|
255
|
+
btn.textContent = word("copy");
|
|
256
|
+
btn.addEventListener("click", () => {
|
|
257
|
+
void copy(String(copyText() ?? "")).catch(() => {});
|
|
258
|
+
});
|
|
259
|
+
el.append(btn);
|
|
260
|
+
} else if (state !== "error" && btn) {
|
|
261
|
+
btn.remove();
|
|
262
|
+
btn = null;
|
|
263
|
+
}
|
|
264
|
+
if (announce === "error") say(word("error"));
|
|
265
|
+
else if (announce === "recovered") say(savedAt);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const sch = saveScheduler({ save, onChange: paint });
|
|
269
|
+
const onInput = () => sch.changed();
|
|
270
|
+
const onBlur = () => void sch.flush();
|
|
271
|
+
const onHidden = () => {
|
|
272
|
+
if (document.visibilityState === "hidden") void sch.flush();
|
|
273
|
+
};
|
|
274
|
+
const onOnline = () => void sch.retry();
|
|
275
|
+
watch?.addEventListener("input", onInput);
|
|
276
|
+
watch?.addEventListener("focusout", onBlur);
|
|
277
|
+
document.addEventListener("visibilitychange", onHidden);
|
|
278
|
+
window.addEventListener("online", onOnline);
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
changed: sch.changed,
|
|
282
|
+
flush: sch.flush,
|
|
283
|
+
retry: sch.retry,
|
|
284
|
+
state: sch.state,
|
|
285
|
+
destroy() {
|
|
286
|
+
sch.destroy();
|
|
287
|
+
watch?.removeEventListener("input", onInput);
|
|
288
|
+
watch?.removeEventListener("focusout", onBlur);
|
|
289
|
+
document.removeEventListener("visibilitychange", onHidden);
|
|
290
|
+
window.removeEventListener("online", onOnline);
|
|
291
|
+
live.remove();
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
}
|
package/src/strings.js
CHANGED
|
@@ -199,6 +199,12 @@ export const en = {
|
|
|
199
199
|
"kit.md.numbered": "Numbered list",
|
|
200
200
|
"kit.md.quote": "Quote",
|
|
201
201
|
"kit.md.linkText": "text",
|
|
202
|
+
// The save-state line (savestate.js). "Saved" has no closing stop.
|
|
203
|
+
"kit.savestate.saving": "Saving…",
|
|
204
|
+
"kit.savestate.saved": "Saved",
|
|
205
|
+
"kit.savestate.error": "Not saved. Retrying…",
|
|
206
|
+
"kit.savestate.copy": "Copy text",
|
|
207
|
+
"kit.savestate.savedAt": "Saved at {time}",
|
|
202
208
|
};
|
|
203
209
|
|
|
204
210
|
export const es = {
|
|
@@ -395,6 +401,11 @@ export const es = {
|
|
|
395
401
|
"kit.md.numbered": "Lista numerada",
|
|
396
402
|
"kit.md.quote": "Cita",
|
|
397
403
|
"kit.md.linkText": "texto",
|
|
404
|
+
"kit.savestate.saving": "Guardando…",
|
|
405
|
+
"kit.savestate.saved": "Guardado",
|
|
406
|
+
"kit.savestate.error": "No se guardó. Reintentando…",
|
|
407
|
+
"kit.savestate.copy": "Copiar texto",
|
|
408
|
+
"kit.savestate.savedAt": "Guardado a las {time}",
|
|
398
409
|
};
|
|
399
410
|
|
|
400
411
|
export const ja = {
|
|
@@ -586,6 +597,11 @@ export const ja = {
|
|
|
586
597
|
"kit.md.numbered": "番号付きリスト",
|
|
587
598
|
"kit.md.quote": "引用",
|
|
588
599
|
"kit.md.linkText": "テキスト",
|
|
600
|
+
"kit.savestate.saving": "保存しています…",
|
|
601
|
+
"kit.savestate.saved": "保存しました",
|
|
602
|
+
"kit.savestate.error": "保存できていません。再試行しています…",
|
|
603
|
+
"kit.savestate.copy": "テキストをコピー",
|
|
604
|
+
"kit.savestate.savedAt": "{time} に保存しました",
|
|
589
605
|
};
|
|
590
606
|
|
|
591
607
|
/** The kit's tables, by language (en is the fallback for any key). */
|