@dooc-dev/dooc 0.0.3 → 0.0.5
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/dist/cli.js +98 -56
- package/dist/compiler/asset-references.d.ts +4 -0
- package/dist/compiler/asset-references.js +117 -0
- package/dist/compiler/bundle-urls.d.ts +1 -1
- package/dist/compiler/bundle-urls.js +15 -5
- package/dist/compiler/check.js +8 -5
- package/dist/compiler/code-languages.js +7 -2
- package/dist/compiler/compile.d.ts +5 -0
- package/dist/compiler/compile.js +105 -10
- package/dist/compiler/config.js +2 -1
- package/dist/compiler/export.d.ts +4 -0
- package/dist/compiler/export.js +140 -0
- package/dist/compiler/figures.js +4 -0
- package/dist/compiler/files.d.ts +1 -0
- package/dist/compiler/files.js +96 -38
- package/dist/compiler/fonts.js +4 -1
- package/dist/compiler/icons.d.ts +1 -1
- package/dist/compiler/icons.js +11 -2
- package/dist/compiler/ignored-files.d.ts +3 -0
- package/dist/compiler/ignored-files.js +68 -0
- package/dist/compiler/locales.js +13 -8
- package/dist/compiler/manifest.d.ts +2 -0
- package/dist/compiler/manifest.js +9 -2
- package/dist/compiler/ops.d.ts +6 -0
- package/dist/compiler/ops.js +98 -7
- package/dist/compiler/subformats.d.ts +24 -0
- package/dist/compiler/subformats.js +299 -0
- package/dist/compiler/textfile.js +3 -0
- package/dist/compiler/theme-variants.js +5 -2
- package/dist/core/content-metadata.d.ts +15 -6
- package/dist/core/content-metadata.js +278 -17
- package/dist/core/content-types.d.ts +3 -1
- package/dist/core/figure-frame.js +19 -1
- package/dist/core/files.d.ts +2 -1
- package/dist/core/files.js +6 -8
- package/dist/core/flow-base-css.generated.js +1 -1
- package/dist/core/html.js +3 -3
- package/dist/core/icons.d.ts +3 -0
- package/dist/core/icons.js +3 -0
- package/dist/core/json-tools.d.ts +1 -0
- package/dist/core/json-tools.js +12 -0
- package/dist/core/routing.d.ts +5 -2
- package/dist/core/routing.js +17 -5
- package/dist/core/subformat-record.d.ts +3 -0
- package/dist/core/subformat-record.js +131 -0
- package/dist/core/subformats.d.ts +43 -0
- package/dist/core/subformats.js +81 -0
- package/dist/core/toc-ui.js +25 -23
- package/dist/edit.global.js +322 -95
- package/dist/mcp.js +50 -9
- package/dist/plugins/index.d.ts +1 -0
- package/dist/plugins/local-storage.js +3 -0
- package/dist/plugins/oauth.d.ts +1 -1
- package/dist/plugins/oauth.js +8 -2
- package/dist/plugins/runtime.d.ts +6 -0
- package/dist/plugins/runtime.js +166 -34
- package/dist/plugins/transport.d.ts +22 -0
- package/dist/plugins/transport.js +1 -0
- package/dist/plugins/types.d.ts +3 -1
- package/dist/plugins.global.js +128 -0
- package/dist/subformats.global.js +127 -0
- package/dist/ui.global.js +91 -38
- package/dist/viewer/build-plugins.d.ts +7 -0
- package/dist/viewer/build-plugins.js +70 -0
- package/dist/viewer/build.d.ts +9 -0
- package/dist/viewer/build.js +32 -5
- package/dist/viewer/editor-tools.d.ts +7 -7
- package/dist/viewer/editor-tools.js +36 -5
- package/dist/viewer/font-picker.js +5 -1
- package/dist/viewer/git-diff.d.ts +3 -0
- package/dist/viewer/git-diff.js +47 -0
- package/dist/viewer/hmr.js +4 -1
- package/dist/viewer/i18n.js +7 -5
- package/dist/viewer/language-picker.d.ts +2 -2
- package/dist/viewer/language-picker.js +26 -9
- package/dist/viewer/lazy-graph.d.ts +2 -0
- package/dist/viewer/lazy-graph.js +30 -0
- package/dist/viewer/locales/eng.json +74 -10
- package/dist/viewer/locales/jpn.json +75 -11
- package/dist/viewer/locales/zho.json +75 -11
- package/dist/viewer/nav-focus.js +8 -1
- package/dist/viewer/nav-update.d.ts +2 -0
- package/dist/viewer/nav-update.js +117 -0
- package/dist/viewer/plugin-style.d.ts +1 -0
- package/dist/viewer/plugin-style.js +82 -0
- package/dist/viewer/plugins.js +46 -3
- package/dist/viewer/resize.d.ts +0 -2
- package/dist/viewer/resize.js +2 -98
- package/dist/viewer/resource-library.d.ts +10 -8
- package/dist/viewer/resource-library.js +179 -30
- package/dist/viewer/search.d.ts +4 -2
- package/dist/viewer/search.js +252 -53
- package/dist/viewer/serve.d.ts +10 -5
- package/dist/viewer/serve.js +882 -287
- package/dist/viewer/site-icon.js +13 -6
- package/dist/viewer/site-title.d.ts +13 -0
- package/dist/viewer/site-title.js +50 -0
- package/dist/viewer/textfile.d.ts +3 -0
- package/dist/viewer/textfile.js +19 -2
- package/dist/viewer/theme-picker.d.ts +2 -0
- package/dist/viewer/theme-picker.js +24 -8
- package/dist/viewer/workspace.d.ts +2 -0
- package/dist/viewer/workspace.js +46 -1
- package/package.json +24 -9
- package/dist/compiler/bundle.d.ts +0 -2
- package/dist/compiler/bundle.js +0 -100
package/dist/core/routing.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
// through here and never build paths themselves — a consumer is a display-only wrapper, and this
|
|
3
3
|
// package owns routing.
|
|
4
4
|
//
|
|
5
|
-
//
|
|
6
|
-
// Body links reference source files
|
|
5
|
+
// File-based URLs use a reserved namespace; legacy URLs follow the manifest hierarchy.
|
|
6
|
+
// Body links reference source files, and heading fragments stay separate.
|
|
7
7
|
import { isDocId } from "./content-metadata.js";
|
|
8
|
-
import { localFile } from "./files.js";
|
|
8
|
+
import { fileDocumentId, localFile } from "./files.js";
|
|
9
9
|
import { parseFileReference } from "./textfile.js";
|
|
10
10
|
/** Parse `<docId>` (leading/trailing `/` are ignored). undefined if the shape differs. */
|
|
11
11
|
export const parseRoutePath = (path) => {
|
|
@@ -52,10 +52,22 @@ export const ancestorsOf = (docs, id) => {
|
|
|
52
52
|
};
|
|
53
53
|
/** DOM anchor id for a heading — namespaced by doc id. */
|
|
54
54
|
export const headingAnchor = (docId, slug) => `${docId}__${slug}`;
|
|
55
|
-
/**
|
|
56
|
-
export const docPath = (docs, id) =>
|
|
55
|
+
/** File routes cannot shadow API/tool routes; legacy IDs retain hierarchy paths. */
|
|
56
|
+
export const docPath = (docs, id) => {
|
|
57
|
+
const doc = docs.find((item) => item.id === id);
|
|
58
|
+
const file = doc?.sourceFile ?? doc?.file;
|
|
59
|
+
if (file && fileDocumentId(file) === id)
|
|
60
|
+
return `/_docs/${file.split("/").map(encodeURIComponent).join("/")}`;
|
|
61
|
+
return `/${[...ancestorsOf(docs, id).map((doc) => doc.id), id].join("/")}`;
|
|
62
|
+
};
|
|
57
63
|
/** Resolve only the complete hierarchy path; no legacy single-id aliases. */
|
|
58
64
|
export const resolveDocPath = (docs, path) => {
|
|
65
|
+
const fileDoc = docs.find((doc) => {
|
|
66
|
+
const file = doc.sourceFile ?? doc.file;
|
|
67
|
+
return file && fileDocumentId(file) === doc.id && docPath(docs, doc.id) === path;
|
|
68
|
+
});
|
|
69
|
+
if (fileDoc)
|
|
70
|
+
return fileDoc.id;
|
|
59
71
|
const parts = path.split("/").filter(Boolean);
|
|
60
72
|
const id = parts.at(-1);
|
|
61
73
|
if (!id || !parts.every(isDocId) || !docs.some((doc) => doc.id === id))
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { Subformat } from "./subformats.js";
|
|
2
|
+
export declare const parseSubformatRecord: (content: string, format: Subformat["format"]) => Record<string, unknown>;
|
|
3
|
+
export declare const setSubformatValue: (source: string, format: Subformat["format"], path: (string | number)[], value: unknown) => string;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { parse as parseToml, stringify as stringifyToml } from "smol-toml";
|
|
2
|
+
import { parseTOML } from "toml-eslint-parser";
|
|
3
|
+
import { parseDocument } from "yaml";
|
|
4
|
+
const yamlDocument = (content) => {
|
|
5
|
+
const document = parseDocument(content);
|
|
6
|
+
if (document.errors.length || document.warnings.length)
|
|
7
|
+
throw new Error([...document.errors, ...document.warnings].map((error) => error.message).join("; "));
|
|
8
|
+
return document;
|
|
9
|
+
};
|
|
10
|
+
export const parseSubformatRecord = (content, format) => {
|
|
11
|
+
const value = format === "json"
|
|
12
|
+
? JSON.parse(content)
|
|
13
|
+
: format === "yaml"
|
|
14
|
+
? yamlDocument(content).toJS()
|
|
15
|
+
: parseToml(content, { integersAsBigInt: "asNeeded" });
|
|
16
|
+
const data = JSON.parse(JSON.stringify(value, (_key, item) => {
|
|
17
|
+
if (typeof item === "bigint" ||
|
|
18
|
+
(typeof item === "number" &&
|
|
19
|
+
(!Number.isFinite(item) ||
|
|
20
|
+
(Number.isInteger(item) && !Number.isSafeInteger(item)))))
|
|
21
|
+
throw new Error("Records require finite JSON-compatible numbers");
|
|
22
|
+
return item;
|
|
23
|
+
}));
|
|
24
|
+
if (!data || typeof data !== "object" || Array.isArray(data))
|
|
25
|
+
throw new Error("Each record file must contain one object");
|
|
26
|
+
return data;
|
|
27
|
+
};
|
|
28
|
+
const jsonKey = (value) => JSON.stringify(value, (_key, item) => item && typeof item === "object" && !Array.isArray(item)
|
|
29
|
+
? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b)))
|
|
30
|
+
: item);
|
|
31
|
+
const tomlValue = (value) => {
|
|
32
|
+
if (Array.isArray(value))
|
|
33
|
+
return `[${value.map(tomlValue).join(", ")}]`;
|
|
34
|
+
if (value && typeof value === "object")
|
|
35
|
+
return `{ ${Object.entries(value)
|
|
36
|
+
.map(([key, item]) => `${JSON.stringify(key)} = ${tomlValue(item)}`)
|
|
37
|
+
.join(", ")} }`;
|
|
38
|
+
return stringifyToml({ value })
|
|
39
|
+
.replace(/^value\s*=\s*/, "")
|
|
40
|
+
.trimEnd();
|
|
41
|
+
};
|
|
42
|
+
export const setSubformatValue = (source, format, path, value) => {
|
|
43
|
+
const data = parseSubformatRecord(source, format);
|
|
44
|
+
const key = path.at(-1);
|
|
45
|
+
if (key === undefined)
|
|
46
|
+
throw new Error("A field path is required");
|
|
47
|
+
let parent = data;
|
|
48
|
+
for (const part of path.slice(0, -1)) {
|
|
49
|
+
if (!parent || typeof parent !== "object" || !Object.hasOwn(parent, part))
|
|
50
|
+
throw new Error("The record changed. Try again.");
|
|
51
|
+
parent = parent[part];
|
|
52
|
+
}
|
|
53
|
+
if (!parent || typeof parent !== "object")
|
|
54
|
+
throw new Error("The record changed. Try again.");
|
|
55
|
+
Object.defineProperty(parent, key, {
|
|
56
|
+
value,
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
});
|
|
61
|
+
let content;
|
|
62
|
+
if (format === "yaml") {
|
|
63
|
+
const document = yamlDocument(source);
|
|
64
|
+
document.setIn(path, value);
|
|
65
|
+
content = document.toString();
|
|
66
|
+
}
|
|
67
|
+
else if (format === "toml") {
|
|
68
|
+
let found;
|
|
69
|
+
const visit = (node, keys) => {
|
|
70
|
+
if (JSON.stringify(keys) === JSON.stringify(path))
|
|
71
|
+
found = node;
|
|
72
|
+
if (node.type === "TOMLArray")
|
|
73
|
+
node.elements.forEach((item, index) => {
|
|
74
|
+
visit(item, [...keys, index]);
|
|
75
|
+
});
|
|
76
|
+
else if (node.type === "TOMLInlineTable")
|
|
77
|
+
entries(node.body, keys);
|
|
78
|
+
};
|
|
79
|
+
const entries = (nodes, keys) => {
|
|
80
|
+
for (const node of nodes)
|
|
81
|
+
visit(node.value, [
|
|
82
|
+
...keys,
|
|
83
|
+
...node.key.keys.map((key) => (key.type === "TOMLBare" ? key.name : key.value)),
|
|
84
|
+
]);
|
|
85
|
+
};
|
|
86
|
+
const nodes = parseTOML(source).body[0].body;
|
|
87
|
+
for (const node of nodes) {
|
|
88
|
+
if (node.type === "TOMLTable")
|
|
89
|
+
entries(node.body, node.resolvedKey);
|
|
90
|
+
else
|
|
91
|
+
entries([node], []);
|
|
92
|
+
}
|
|
93
|
+
if (found)
|
|
94
|
+
content =
|
|
95
|
+
source.slice(0, found.range[0]) + tomlValue(value) + source.slice(found.range[1]);
|
|
96
|
+
else {
|
|
97
|
+
let remaining = source;
|
|
98
|
+
for (const node of [...nodes].reverse()) {
|
|
99
|
+
const keys = node.type === "TOMLTable"
|
|
100
|
+
? node.resolvedKey
|
|
101
|
+
: node.key.keys.map((key) => key.type === "TOMLBare" ? key.name : key.value);
|
|
102
|
+
if (path.every((part, index) => keys[index] === part))
|
|
103
|
+
remaining = remaining.slice(0, node.range[0]) + remaining.slice(node.range[1]);
|
|
104
|
+
}
|
|
105
|
+
const table = parseTOML(remaining)
|
|
106
|
+
.body[0].body.filter((node) => node.type === "TOMLTable" &&
|
|
107
|
+
node.resolvedKey.length < path.length &&
|
|
108
|
+
node.resolvedKey.every((part, index) => part === path[index]))
|
|
109
|
+
.sort((a, b) => b.resolvedKey.length - a.resolvedKey.length)[0];
|
|
110
|
+
const keys = path.slice(table?.resolvedKey.length ?? 0);
|
|
111
|
+
if (keys.some((key) => typeof key !== "string"))
|
|
112
|
+
throw new Error("Use TOML text to edit this value.");
|
|
113
|
+
const offset = table ? remaining.indexOf("\n", table.key.range[1]) + 1 : 0;
|
|
114
|
+
if (table && !offset)
|
|
115
|
+
throw new Error("Use TOML text to edit this value.");
|
|
116
|
+
content =
|
|
117
|
+
remaining.slice(0, offset) +
|
|
118
|
+
`${keys.map((key) => (/^[A-Za-z0-9_-]+$/.test(String(key)) ? key : JSON.stringify(key))).join(".")} = ${tomlValue(value)}\n` +
|
|
119
|
+
remaining.slice(offset);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const indent = /\n([ \t]+)"/.exec(source)?.[1];
|
|
124
|
+
content = JSON.stringify(data, null, indent) + (source.endsWith("\n") ? "\n" : "");
|
|
125
|
+
}
|
|
126
|
+
if (source.includes("\r\n"))
|
|
127
|
+
content = content.replace(/\r?\n/g, "\r\n");
|
|
128
|
+
if (jsonKey(parseSubformatRecord(content, format)) !== jsonKey(data))
|
|
129
|
+
throw new Error("The updated record cannot be saved without changing other values");
|
|
130
|
+
return content;
|
|
131
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export declare const SUBFORMAT_SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
|
|
2
|
+
export declare const SUBFORMAT_SCHEMA_TEMPLATE: string;
|
|
3
|
+
export interface Subformat {
|
|
4
|
+
ext: string;
|
|
5
|
+
format: "yaml" | "json" | "toml";
|
|
6
|
+
schema: string;
|
|
7
|
+
layouts?: {
|
|
8
|
+
card_title?: string;
|
|
9
|
+
group_by?: string;
|
|
10
|
+
sort_by?: string;
|
|
11
|
+
sort_order?: "asc" | "desc";
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export interface SubformatField {
|
|
15
|
+
name: string;
|
|
16
|
+
title: string;
|
|
17
|
+
type?: "string" | "number" | "integer" | "boolean";
|
|
18
|
+
enum?: (string | number | boolean | null)[];
|
|
19
|
+
format?: "date" | "date-time";
|
|
20
|
+
readOnly?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface SubformatRecord {
|
|
23
|
+
file: string;
|
|
24
|
+
content: string;
|
|
25
|
+
data: Record<string, unknown>;
|
|
26
|
+
change?: "added" | "modified";
|
|
27
|
+
references?: {
|
|
28
|
+
file: string;
|
|
29
|
+
line: number;
|
|
30
|
+
}[];
|
|
31
|
+
}
|
|
32
|
+
export interface SubformatCollection extends Subformat {
|
|
33
|
+
change?: "added" | "modified";
|
|
34
|
+
baseLayouts?: NonNullable<Subformat["layouts"]>;
|
|
35
|
+
fields: SubformatField[];
|
|
36
|
+
template?: string;
|
|
37
|
+
defaultFolder?: string;
|
|
38
|
+
records: SubformatRecord[];
|
|
39
|
+
}
|
|
40
|
+
export declare const subformatSourcePath: (file: string, base?: string) => string;
|
|
41
|
+
export declare const subformatChange: (collection: SubformatCollection) => SubformatRecord["change"];
|
|
42
|
+
export declare const parseSubformats: (input: unknown) => Subformat[];
|
|
43
|
+
export declare const sortSubformatRecords: (records: readonly SubformatRecord[], field?: SubformatField, descending?: boolean) => SubformatRecord[];
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { localFile } from "./files.js";
|
|
2
|
+
export const SUBFORMAT_SCHEMA_VERSION = "https://json-schema.org/draft/2020-12/schema";
|
|
3
|
+
export const SUBFORMAT_SCHEMA_TEMPLATE = `${JSON.stringify({
|
|
4
|
+
$schema: SUBFORMAT_SCHEMA_VERSION,
|
|
5
|
+
type: "object",
|
|
6
|
+
properties: {},
|
|
7
|
+
additionalProperties: false,
|
|
8
|
+
}, null, 2)}\n`;
|
|
9
|
+
export const subformatSourcePath = (file, base = "") => base + (/^https?:\/\//i.test(base) ? file.split("/").map(encodeURIComponent).join("/") : file);
|
|
10
|
+
export const subformatChange = (collection) => collection.change ??
|
|
11
|
+
(collection.records.some((record) => record.change === "modified")
|
|
12
|
+
? "modified"
|
|
13
|
+
: collection.records.some((record) => record.change === "added")
|
|
14
|
+
? "added"
|
|
15
|
+
: undefined);
|
|
16
|
+
export const parseSubformats = (input) => {
|
|
17
|
+
if (!Array.isArray(input))
|
|
18
|
+
throw new Error("subformats must be a list");
|
|
19
|
+
const seen = new Set();
|
|
20
|
+
return input.map((value) => {
|
|
21
|
+
if (!value ||
|
|
22
|
+
typeof value !== "object" ||
|
|
23
|
+
Array.isArray(value) ||
|
|
24
|
+
Object.keys(value).some((key) => !["ext", "format", "schema", "layouts"].includes(key)) ||
|
|
25
|
+
typeof value.ext !== "string" ||
|
|
26
|
+
!/^[a-z0-9][a-z0-9_-]*$/.test(value.ext) ||
|
|
27
|
+
!["yaml", "json", "toml"].includes(value.format) ||
|
|
28
|
+
!localFile(value.schema))
|
|
29
|
+
throw new Error("Each subformat requires ext (without a dot), format (yaml, json or toml), and a local schema path");
|
|
30
|
+
if (value.layouts !== undefined &&
|
|
31
|
+
(!value.layouts ||
|
|
32
|
+
typeof value.layouts !== "object" ||
|
|
33
|
+
Array.isArray(value.layouts) ||
|
|
34
|
+
Object.entries(value.layouts).some(([key, item]) => key === "sort_order"
|
|
35
|
+
? item !== "asc" && item !== "desc"
|
|
36
|
+
: !["card_title", "group_by", "sort_by"].includes(key) ||
|
|
37
|
+
typeof item !== "string")))
|
|
38
|
+
throw new Error("Subformat layouts accept card_title, group_by, sort_by field names and sort_order (asc or desc)");
|
|
39
|
+
if (seen.has(value.ext))
|
|
40
|
+
throw new Error(`Duplicate subformat extension: ${value.ext}`);
|
|
41
|
+
seen.add(value.ext);
|
|
42
|
+
return {
|
|
43
|
+
ext: value.ext,
|
|
44
|
+
format: value.format,
|
|
45
|
+
schema: value.schema,
|
|
46
|
+
...(value.layouts === undefined ? {} : { layouts: { ...value.layouts } }),
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
export const sortSubformatRecords = (records, field, descending = false) => [...records].sort((a, b) => {
|
|
51
|
+
const value = (record) => {
|
|
52
|
+
if (!field)
|
|
53
|
+
return record.file;
|
|
54
|
+
const item = record.data[field.name];
|
|
55
|
+
if (field.enum) {
|
|
56
|
+
const index = field.enum.indexOf(item);
|
|
57
|
+
return index < 0 ? undefined : index;
|
|
58
|
+
}
|
|
59
|
+
if (item == null)
|
|
60
|
+
return undefined;
|
|
61
|
+
if (field.format) {
|
|
62
|
+
const time = typeof item === "string" ? Date.parse(item) : Number.NaN;
|
|
63
|
+
return Number.isNaN(time) ? undefined : time;
|
|
64
|
+
}
|
|
65
|
+
if (typeof item === "boolean")
|
|
66
|
+
return Number(item);
|
|
67
|
+
return typeof item === "string" || (typeof item === "number" && Number.isFinite(item))
|
|
68
|
+
? item
|
|
69
|
+
: undefined;
|
|
70
|
+
};
|
|
71
|
+
const left = value(a);
|
|
72
|
+
const right = value(b);
|
|
73
|
+
if ((left === undefined) !== (right === undefined))
|
|
74
|
+
return left === undefined ? 1 : -1;
|
|
75
|
+
const order = typeof left === "number" && typeof right === "number"
|
|
76
|
+
? left - right
|
|
77
|
+
: typeof left === "string" && typeof right === "string"
|
|
78
|
+
? left.localeCompare(right, undefined, { numeric: true })
|
|
79
|
+
: 0;
|
|
80
|
+
return order * (descending ? -1 : 1) || a.file.localeCompare(b.file);
|
|
81
|
+
});
|
package/dist/core/toc-ui.js
CHANGED
|
@@ -26,17 +26,15 @@ export function mountToc(doc) {
|
|
|
26
26
|
if (mounted.has(nav))
|
|
27
27
|
continue;
|
|
28
28
|
const article = nav.closest("article");
|
|
29
|
-
const seek = nav.querySelector(".toc-seek");
|
|
30
29
|
const button = nav.querySelector(".toc-current");
|
|
31
30
|
const list = nav.querySelector("ul");
|
|
32
|
-
if (!article || !
|
|
31
|
+
if (!article || !button || !list)
|
|
33
32
|
continue;
|
|
34
33
|
const pane = nav.closest(".preview-pane");
|
|
35
34
|
const scroller = pane ?? doc.scrollingElement;
|
|
36
35
|
if (!scroller)
|
|
37
36
|
continue;
|
|
38
37
|
const header = (pane ?? article.parentElement)?.querySelector(":scope > .pane-actions, :scope > .page-actions");
|
|
39
|
-
header?.prepend(nav);
|
|
40
38
|
const listeners = new AbortController();
|
|
41
39
|
const options = { signal: listeners.signal };
|
|
42
40
|
list.id = `toc-options-${++sequence}`;
|
|
@@ -48,22 +46,30 @@ export function mountToc(doc) {
|
|
|
48
46
|
}))
|
|
49
47
|
.filter((section) => section.heading !== null);
|
|
50
48
|
let active;
|
|
49
|
+
let card = false;
|
|
51
50
|
const update = () => {
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
const narrow = article.clientWidth < 520;
|
|
52
|
+
if (card !== narrow) {
|
|
53
|
+
card = narrow;
|
|
54
|
+
const focused = nav.contains(doc.activeElement);
|
|
55
|
+
list.popover = card ? null : "auto";
|
|
56
|
+
list.removeAttribute("style");
|
|
57
|
+
nav.classList.toggle("toc-card", card);
|
|
58
|
+
if (focused)
|
|
59
|
+
(card ? (active ?? sections[0]?.link) : button)?.focus({
|
|
60
|
+
preventScroll: true,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
const top = (header?.getBoundingClientRect().bottom ??
|
|
64
|
+
pane?.getBoundingClientRect().top ??
|
|
65
|
+
0) + 20;
|
|
60
66
|
let current = sections[0];
|
|
61
67
|
for (const section of sections)
|
|
62
68
|
if ((section.heading?.getBoundingClientRect().top ?? Infinity) <= top)
|
|
63
69
|
current = section;
|
|
64
|
-
if (
|
|
70
|
+
if (scroller.scrollTop > 0 &&
|
|
71
|
+
scroller.scrollTop >= scroller.scrollHeight - scroller.clientHeight - 1)
|
|
65
72
|
current = sections.at(-1);
|
|
66
|
-
seek.setAttribute("aria-valuetext", `${current?.link.textContent ?? ""} · ${Math.round(progress * 100)}%`);
|
|
67
73
|
if (current?.link === active)
|
|
68
74
|
return;
|
|
69
75
|
active?.removeAttribute("aria-current");
|
|
@@ -91,6 +97,8 @@ export function mountToc(doc) {
|
|
|
91
97
|
button.setAttribute("aria-expanded", String(list.matches(":popover-open")));
|
|
92
98
|
}, options);
|
|
93
99
|
list.addEventListener("keydown", (event) => {
|
|
100
|
+
if (card)
|
|
101
|
+
return;
|
|
94
102
|
const index = sections.findIndex(({ link }) => link === doc.activeElement);
|
|
95
103
|
if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
|
|
96
104
|
event.preventDefault();
|
|
@@ -117,22 +125,16 @@ export function mountToc(doc) {
|
|
|
117
125
|
const heading = sections.find((section) => section.link === link)?.heading;
|
|
118
126
|
if (!heading)
|
|
119
127
|
return;
|
|
120
|
-
|
|
121
|
-
|
|
128
|
+
if (!card) {
|
|
129
|
+
list.hidePopover();
|
|
130
|
+
button.focus({ preventScroll: true });
|
|
131
|
+
}
|
|
122
132
|
if (pane) {
|
|
123
133
|
// Pane activation is asynchronous, so resolve the clicked pane directly.
|
|
124
134
|
event.preventDefault();
|
|
125
135
|
heading.scrollIntoView({ block: "start" });
|
|
126
136
|
}
|
|
127
137
|
}, options);
|
|
128
|
-
seek.addEventListener("input", () => {
|
|
129
|
-
const maximum = Math.max(0, scroller.scrollHeight - scroller.clientHeight);
|
|
130
|
-
scroller.scrollTo({
|
|
131
|
-
top: (Number(seek.value) / 1000) * maximum,
|
|
132
|
-
behavior: "instant",
|
|
133
|
-
});
|
|
134
|
-
schedule();
|
|
135
|
-
}, options);
|
|
136
138
|
mounted.set(nav, { article, update, dispose: () => listeners.abort() });
|
|
137
139
|
resize.observe(article);
|
|
138
140
|
}
|