@composable-os/sdk 0.1.0-beta.10 → 0.1.0-beta.13
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/acknowledge.d.ts +38 -0
- package/dist/acknowledge.js +30 -0
- package/dist/agreements.d.ts +80 -0
- package/dist/agreements.js +109 -0
- package/dist/allowance.d.ts +47 -5
- package/dist/allowance.js +59 -4
- package/dist/automation-check.d.ts +6 -1
- package/dist/automation-check.js +93 -8
- package/dist/automation.d.ts +62 -4
- package/dist/automation.js +32 -9
- package/dist/behaviour.js +25 -6
- package/dist/calendar-entry.d.ts +97 -0
- package/dist/calendar-entry.js +180 -0
- package/dist/capacity.d.ts +69 -0
- package/dist/capacity.js +63 -2
- package/dist/checklists.d.ts +41 -0
- package/dist/checklists.js +75 -0
- package/dist/contract.d.ts +527 -26
- package/dist/contract.js +21 -3
- package/dist/discount.d.ts +135 -0
- package/dist/discount.js +281 -0
- package/dist/document.d.ts +2 -1
- package/dist/document.js +2 -1
- package/dist/field-types.d.ts +1 -1
- package/dist/field-types.js +5 -0
- package/dist/formula/compile.d.ts +15 -0
- package/dist/formula/compile.js +9 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/intake.d.ts +134 -0
- package/dist/intake.js +239 -0
- package/dist/journey-signals.d.ts +89 -0
- package/dist/journey-signals.js +151 -0
- package/dist/manifest.d.ts +1060 -60
- package/dist/manifest.js +1111 -61
- package/dist/mine.d.ts +45 -0
- package/dist/mine.js +119 -0
- package/dist/occupies.d.ts +64 -0
- package/dist/occupies.js +122 -0
- package/dist/questions.d.ts +179 -0
- package/dist/questions.js +514 -0
- package/dist/recurring.d.ts +33 -4
- package/dist/recurring.js +50 -3
- package/dist/restricted-fields.d.ts +15 -0
- package/dist/restricted-fields.js +97 -0
- package/dist/rich-text.d.ts +22 -0
- package/dist/rich-text.js +91 -0
- package/dist/routine-mail.d.ts +400 -0
- package/dist/routine-mail.js +955 -0
- package/dist/runtime.d.ts +5 -11
- package/dist/runtime.js +21 -11
- package/dist/screens.d.ts +3 -0
- package/dist/screens.js +74 -0
- package/dist/sellable.d.ts +231 -2
- package/dist/sellable.js +342 -1
- package/dist/slug.d.ts +2 -0
- package/dist/slug.js +6 -2
- package/dist/staffed.d.ts +178 -0
- package/dist/staffed.js +287 -0
- package/dist/states.d.ts +74 -4
- package/dist/states.js +154 -58
- package/dist/stock.d.ts +129 -0
- package/dist/stock.js +205 -0
- package/dist/team.d.ts +28 -0
- package/dist/team.js +52 -0
- package/dist/testing.d.ts +22 -1
- package/dist/testing.js +47 -5
- package/dist/worked-on.d.ts +42 -0
- package/dist/worked-on.js +79 -0
- package/package.json +9 -1
- package/src/acknowledge.ts +100 -0
- package/src/agreements.ts +185 -0
- package/src/allowance.ts +98 -8
- package/src/automation-check.ts +120 -9
- package/src/automation.ts +78 -10
- package/src/behaviour.ts +26 -6
- package/src/calendar-entry.ts +258 -0
- package/src/capacity.ts +103 -2
- package/src/checklists.ts +118 -0
- package/src/contract.ts +514 -27
- package/src/discount.ts +408 -0
- package/src/document.ts +4 -2
- package/src/field-types.ts +5 -0
- package/src/formula/compile.ts +10 -0
- package/src/index.ts +17 -0
- package/src/intake.ts +380 -0
- package/src/journey-signals.ts +215 -0
- package/src/manifest.ts +1313 -66
- package/src/mine.ts +161 -0
- package/src/occupies.ts +192 -0
- package/src/questions.ts +681 -0
- package/src/recurring.ts +90 -9
- package/src/restricted-fields.ts +129 -0
- package/src/rich-text.ts +112 -0
- package/src/routine-mail.ts +1272 -0
- package/src/runtime.ts +25 -12
- package/src/screens.ts +91 -0
- package/src/sellable.ts +595 -1
- package/src/slug.ts +7 -2
- package/src/staffed.ts +474 -0
- package/src/states.ts +220 -64
- package/src/stock.ts +332 -0
- package/src/team.ts +83 -0
- package/src/testing.ts +70 -6
- package/src/worked-on.ts +134 -0
package/dist/recurring.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
// The `recurring` behaviour (stage 31, ADR 0500): a record (a series: one line of a timetable)
|
|
2
|
-
// declares a
|
|
3
|
-
// generates (its classes) ahead of time. Its declaration, its manifest schema and its contract
|
|
2
|
+
// declares a rule in local wall-clock time (weekly, or every N weeks, months or years: ADR 0860),
|
|
3
|
+
// and the host makes the dated records it generates (its classes) ahead of time. Its declaration, its manifest schema and its contract
|
|
4
4
|
// checks live here; the host's side is packages/core/src/recurring/.
|
|
5
5
|
import { z } from "zod";
|
|
6
6
|
/** The weekday values a rule's `weekdays` field must use (ISO order, Monday first). */
|
|
7
7
|
export const WEEKDAYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
|
8
|
+
/** The values a rule's `unit` field may use (ADR 0860): every N weeks, months or years. */
|
|
9
|
+
export const RECURRING_UNITS = ["week", "month", "year"];
|
|
10
|
+
/** The largest N of "every N units". */
|
|
11
|
+
export const RECURRING_EVERY_MAX = 100;
|
|
8
12
|
/** How far ahead the host makes records when a series does not say (days). */
|
|
9
13
|
export const RECURRING_HORIZON_DAYS = 56;
|
|
10
14
|
/** The most days ahead a series may ask for. */
|
|
@@ -16,12 +20,17 @@ export const recurringManifestSchema = z
|
|
|
16
20
|
link: fieldName,
|
|
17
21
|
rule: z
|
|
18
22
|
.object({
|
|
19
|
-
weekdays: fieldName,
|
|
23
|
+
weekdays: fieldName.optional(),
|
|
20
24
|
time: fieldName,
|
|
21
25
|
length: fieldName.optional(),
|
|
22
26
|
from: fieldName,
|
|
23
27
|
until: fieldName.optional(),
|
|
24
28
|
zone: fieldName.optional(),
|
|
29
|
+
unit: fieldName.optional(),
|
|
30
|
+
every: fieldName.optional(),
|
|
31
|
+
day: fieldName.optional(),
|
|
32
|
+
month: fieldName.optional(),
|
|
33
|
+
lengthDays: fieldName.optional(),
|
|
25
34
|
})
|
|
26
35
|
.strict(),
|
|
27
36
|
into: z
|
|
@@ -47,6 +56,7 @@ export const recurringManifestSchema = z
|
|
|
47
56
|
.optional(),
|
|
48
57
|
horizonDays: z.number().int().min(1).max(RECURRING_HORIZON_MAX).optional(),
|
|
49
58
|
horizonAtLeast: z.object({ object: fieldName, field: fieldName }).strict().optional(),
|
|
59
|
+
horizonSetting: z.string().max(64).optional(),
|
|
50
60
|
})
|
|
51
61
|
.strict();
|
|
52
62
|
/** Reports problems with `objects.<name>.recurring` through `contract`. */
|
|
@@ -67,6 +77,31 @@ export function checkObjectRecurring(m, name, object, contract) {
|
|
|
67
77
|
return field;
|
|
68
78
|
};
|
|
69
79
|
const rule = recurring.rule;
|
|
80
|
+
// The interval rule's unit (ADR 0860): a select of some of week, month and year.
|
|
81
|
+
const unit = expect(object, name, `${path}.rule.unit`, rule.unit, ["select"], "a select field");
|
|
82
|
+
const units = (unit?.options ?? []).map((o) => o.value);
|
|
83
|
+
if (unit) {
|
|
84
|
+
const wrong = units.filter((v) => !RECURRING_UNITS.includes(v));
|
|
85
|
+
if (wrong.length || !units.length) {
|
|
86
|
+
contract(`${path}.rule.unit`, `"${rule.unit}"'s options must be units (${RECURRING_UNITS.join(", ")})`);
|
|
87
|
+
}
|
|
88
|
+
if (unit.editableOptions) {
|
|
89
|
+
contract(`${path}.rule.unit`, "The unit field cannot have editableOptions");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (const key of ["every", "day", "month"]) {
|
|
93
|
+
const local = rule[key];
|
|
94
|
+
if (local === undefined)
|
|
95
|
+
continue;
|
|
96
|
+
if (rule.unit === undefined) {
|
|
97
|
+
contract(`${path}.rule.${key}`, `"${key}" needs a unit field (rule.unit)`);
|
|
98
|
+
}
|
|
99
|
+
expect(object, name, `${path}.rule.${key}`, local, ["number"], "a number field");
|
|
100
|
+
}
|
|
101
|
+
// Weekdays are needed while the rule can be weekly: no unit, or a unit that may be "week".
|
|
102
|
+
if (rule.weekdays === undefined && (rule.unit === undefined || units.includes("week"))) {
|
|
103
|
+
contract(`${path}.rule.weekdays`, "A rule that can be weekly needs a weekdays field");
|
|
104
|
+
}
|
|
70
105
|
const weekdays = expect(object, name, `${path}.rule.weekdays`, rule.weekdays, ["multiSelect"], "a multiSelect field");
|
|
71
106
|
if (weekdays) {
|
|
72
107
|
const values = (weekdays.options ?? []).map((o) => o.value);
|
|
@@ -80,6 +115,10 @@ export function checkObjectRecurring(m, name, object, contract) {
|
|
|
80
115
|
}
|
|
81
116
|
expect(object, name, `${path}.rule.time`, rule.time, ["text"], "a text field (HH:MM)");
|
|
82
117
|
expect(object, name, `${path}.rule.length`, rule.length, ["number"], "a number field (minutes)");
|
|
118
|
+
expect(object, name, `${path}.rule.lengthDays`, rule.lengthDays, ["number"], "a number field (days)");
|
|
119
|
+
if (rule.length !== undefined && rule.lengthDays !== undefined) {
|
|
120
|
+
contract(`${path}.rule.lengthDays`, "A rule's length is in minutes or in days, not both");
|
|
121
|
+
}
|
|
83
122
|
expect(object, name, `${path}.rule.from`, rule.from, ["date"], "a date field");
|
|
84
123
|
expect(object, name, `${path}.rule.until`, rule.until, ["date"], "a date field");
|
|
85
124
|
expect(object, name, `${path}.rule.zone`, rule.zone, ["timezone"], "a timezone field");
|
|
@@ -142,6 +181,14 @@ export function checkObjectRecurring(m, name, object, contract) {
|
|
|
142
181
|
contract(`${path}.cancel`, `${generatedName} needs a move into "${recurring.cancel}" that the host may make`);
|
|
143
182
|
}
|
|
144
183
|
}
|
|
184
|
+
if (recurring.horizonSetting !== undefined) {
|
|
185
|
+
const setting = Object.hasOwn(m.settings, recurring.horizonSetting)
|
|
186
|
+
? m.settings[recurring.horizonSetting]
|
|
187
|
+
: undefined;
|
|
188
|
+
if (setting?.type !== "number") {
|
|
189
|
+
contract(`${path}.horizonSetting`, `"${recurring.horizonSetting}" must be a number setting of this extension (days)`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
145
192
|
if (recurring.horizonAtLeast) {
|
|
146
193
|
const h = recurring.horizonAtLeast;
|
|
147
194
|
// One of this extension's objects, or another extension's object with a number field this
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ExtensionManifest, FieldManifest } from "./manifest.ts";
|
|
2
|
+
/** The field types a restricted field may have: words a person writes. */
|
|
3
|
+
export declare const RESTRICTED_FIELD_TYPES: readonly ["text", "longText"];
|
|
4
|
+
/** The most characters a restricted value holds (the store's own check, migration 1190). */
|
|
5
|
+
export declare const RESTRICTED_MAX_CHARS = 4000;
|
|
6
|
+
/** Whether `field` is declared restricted. */
|
|
7
|
+
export declare function isRestrictedField(field: Pick<FieldManifest, "restricted">): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Reports problems with restricted fields through `contract`: each is a text field of one of the
|
|
10
|
+
* pack's own objects, which declares a customer who reads and writes it; it names one of the
|
|
11
|
+
* pack's restricted permissions; nothing else of the object names it (its title, an index, a
|
|
12
|
+
* uniqueness rule, what the public or every signed-in customer reads, a version's content, a
|
|
13
|
+
* seed); and it is on no other object.
|
|
14
|
+
*/
|
|
15
|
+
export declare function checkRestrictedFields(m: ExtensionManifest, contract: (path: string, message: string) => void): void;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/** The field types a restricted field may have: words a person writes. */
|
|
2
|
+
export const RESTRICTED_FIELD_TYPES = ["text", "longText"];
|
|
3
|
+
/** The most characters a restricted value holds (the store's own check, migration 1190). */
|
|
4
|
+
export const RESTRICTED_MAX_CHARS = 4000;
|
|
5
|
+
/** Whether `field` is declared restricted. */
|
|
6
|
+
export function isRestrictedField(field) {
|
|
7
|
+
return typeof field.restricted === "string";
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Reports problems with restricted fields through `contract`: each is a text field of one of the
|
|
11
|
+
* pack's own objects, which declares a customer who reads and writes it; it names one of the
|
|
12
|
+
* pack's restricted permissions; nothing else of the object names it (its title, an index, a
|
|
13
|
+
* uniqueness rule, what the public or every signed-in customer reads, a version's content, a
|
|
14
|
+
* seed); and it is on no other object.
|
|
15
|
+
*/
|
|
16
|
+
export function checkRestrictedFields(m, contract) {
|
|
17
|
+
const restricted = new Set(m.restricted ?? []);
|
|
18
|
+
for (const [target, fields] of Object.entries(m.fields)) {
|
|
19
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
20
|
+
if (isRestrictedField(field)) {
|
|
21
|
+
contract(`fields.${target}.${name}.restricted`, "A restricted field is a field of one of this extension's own objects, never one added to another object");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
for (const [objectName, object] of Object.entries(m.objects)) {
|
|
26
|
+
const names = Object.entries(object.fields)
|
|
27
|
+
.filter(([, field]) => isRestrictedField(field))
|
|
28
|
+
.map(([name]) => name);
|
|
29
|
+
if (!names.length)
|
|
30
|
+
continue;
|
|
31
|
+
const path = `objects.${objectName}`;
|
|
32
|
+
for (const name of names) {
|
|
33
|
+
const field = object.fields[name];
|
|
34
|
+
const at = `${path}.fields.${name}`;
|
|
35
|
+
if (!RESTRICTED_FIELD_TYPES.includes(field.type)) {
|
|
36
|
+
contract(`${at}.type`, "A restricted field is a text or longText field");
|
|
37
|
+
}
|
|
38
|
+
if (!restricted.has(field.restricted)) {
|
|
39
|
+
contract(`${at}.restricted`, `"${field.restricted}" must be one of this extension's restricted permissions (restricted)`);
|
|
40
|
+
}
|
|
41
|
+
for (const key of [
|
|
42
|
+
"required",
|
|
43
|
+
"readOnly",
|
|
44
|
+
"default",
|
|
45
|
+
"defaultSetting",
|
|
46
|
+
"track",
|
|
47
|
+
"personApproves",
|
|
48
|
+
"renamedFrom",
|
|
49
|
+
]) {
|
|
50
|
+
if (field[key] !== undefined) {
|
|
51
|
+
contract(`${at}.${key}`, `A restricted field takes no ${key}: its author writes it, and nothing else sees it`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const customer = object.customer;
|
|
55
|
+
if (!customer) {
|
|
56
|
+
contract(`${at}.restricted`, "A restricted field is on an object that declares a customer: its author writes it");
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
if (customer.field === name) {
|
|
60
|
+
contract(`${at}.restricted`, "The customer field cannot be restricted");
|
|
61
|
+
}
|
|
62
|
+
if (!customer.write.includes(name) || !customer.read.includes(name)) {
|
|
63
|
+
contract(`${at}.restricted`, "A restricted field is one the customer reads and writes (customer.read and customer.write)");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const named = (where, list) => {
|
|
68
|
+
for (const name of names) {
|
|
69
|
+
if (list?.includes(name)) {
|
|
70
|
+
contract(`${path}.${where}`, `"${name}" is restricted: it is kept out of every list, view and read but its own`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
named("titleField", [object.titleField]);
|
|
75
|
+
object.indexes?.forEach((index, i) => {
|
|
76
|
+
named(`indexes.${i}`, index);
|
|
77
|
+
});
|
|
78
|
+
object.unique?.forEach((rule, i) => {
|
|
79
|
+
named(`unique.${i}.fields`, rule.fields);
|
|
80
|
+
});
|
|
81
|
+
object.requireOneOf?.forEach((group, i) => {
|
|
82
|
+
named(`requireOneOf.${i}`, group);
|
|
83
|
+
});
|
|
84
|
+
named("public.read", object.public?.read);
|
|
85
|
+
named("signedIn.read", object.signedIn?.read);
|
|
86
|
+
named("customerVia.read", object.customerVia?.read);
|
|
87
|
+
named("publishable.content", object.publishable?.content);
|
|
88
|
+
for (const [fieldName, field] of Object.entries(object.fields)) {
|
|
89
|
+
for (const option of field.options ?? []) {
|
|
90
|
+
named(`fields.${fieldName}.options`, option.requires);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
for (const [key, values] of Object.entries(object.seed ?? {})) {
|
|
94
|
+
named(`seed.${key}`, Object.keys(values));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
package/dist/rich-text.d.ts
CHANGED
|
@@ -46,3 +46,25 @@ export declare function documentMaxBytesOf(field: {
|
|
|
46
46
|
type?: string | undefined;
|
|
47
47
|
maxBytes?: number | undefined;
|
|
48
48
|
}): number | undefined;
|
|
49
|
+
/** The longest a heading's anchor is, before a `-<n>` that tells two apart. */
|
|
50
|
+
export declare const ANCHOR_MAX = 60;
|
|
51
|
+
interface RichTextLike {
|
|
52
|
+
blocks?: readonly unknown[] | null;
|
|
53
|
+
}
|
|
54
|
+
/** Each heading block's anchor, in the order the headings come in the document. */
|
|
55
|
+
export declare function headingAnchors(value: RichTextLike | null | undefined): string[];
|
|
56
|
+
/** One section of a document: what comes before the first heading, or a heading and its blocks. */
|
|
57
|
+
export interface RichTextSection {
|
|
58
|
+
/** The heading's anchor; null for what comes before the first heading. */
|
|
59
|
+
anchor: string | null;
|
|
60
|
+
/** The heading's words; empty before the first heading. */
|
|
61
|
+
heading: string;
|
|
62
|
+
/** The words of the section's blocks, the heading's own left out. */
|
|
63
|
+
text: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A document split at its headings: the blocks before the first heading (only when they hold
|
|
67
|
+
* words), then one section per heading, with the anchor `headingAnchors` gives it.
|
|
68
|
+
*/
|
|
69
|
+
export declare function richTextSections(value: RichTextLike | null | undefined): RichTextSection[];
|
|
70
|
+
export {};
|
package/dist/rich-text.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { hyphenate } from "./slug.js";
|
|
1
2
|
/** The block types, in the order a renderer lists them. */
|
|
2
3
|
export const RICH_TEXT_BLOCKS = [
|
|
3
4
|
"paragraph",
|
|
@@ -182,3 +183,93 @@ export function documentMaxBytesOf(field) {
|
|
|
182
183
|
return field.maxBytes;
|
|
183
184
|
return field.type === "richText" ? RICH_TEXT_DEFAULT_BYTES : undefined;
|
|
184
185
|
}
|
|
186
|
+
// --- Sections and anchors (stage 38, ADR 0700) ---------------------------------------------------
|
|
187
|
+
//
|
|
188
|
+
// A heading starts a section. Its anchor is made from its text and its place among the document's
|
|
189
|
+
// headings, never stored: the host's published-text index, `content.search`'s results and both
|
|
190
|
+
// renderers (a heading's `id` on the web, its `nativeID` in the app) all call `headingAnchors`, so
|
|
191
|
+
// a link to `#<anchor>` lands on the heading the search found. Two headings with the same words
|
|
192
|
+
// are told apart by their order (`-2`, `-3`); a heading with no letters or digits is `section`.
|
|
193
|
+
/** The longest a heading's anchor is, before a `-<n>` that tells two apart. */
|
|
194
|
+
export const ANCHOR_MAX = 60;
|
|
195
|
+
/** The words of inline runs, without marks. A line break is a space. */
|
|
196
|
+
function runsText(content) {
|
|
197
|
+
return (content ?? [])
|
|
198
|
+
.map((run) => (run?.type === "text" && typeof run.text === "string" ? run.text : " "))
|
|
199
|
+
.join("")
|
|
200
|
+
.replace(/\s+/g, " ")
|
|
201
|
+
.trim();
|
|
202
|
+
}
|
|
203
|
+
function anchorBase(text) {
|
|
204
|
+
return hyphenate(text, ANCHOR_MAX) || "section";
|
|
205
|
+
}
|
|
206
|
+
/** Each heading block's anchor, in the order the headings come in the document. */
|
|
207
|
+
export function headingAnchors(value) {
|
|
208
|
+
const taken = new Set();
|
|
209
|
+
const out = [];
|
|
210
|
+
for (const block of value?.blocks ?? []) {
|
|
211
|
+
const b = block;
|
|
212
|
+
if (b?.type !== "heading")
|
|
213
|
+
continue;
|
|
214
|
+
const base = anchorBase(runsText(b.content));
|
|
215
|
+
let anchor = base;
|
|
216
|
+
for (let n = 2; taken.has(anchor); n += 1)
|
|
217
|
+
anchor = `${base}-${n}`;
|
|
218
|
+
taken.add(anchor);
|
|
219
|
+
out.push(anchor);
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
/** A block's words (a list's items, a picture's caption, a button's label). */
|
|
224
|
+
function blockText(block) {
|
|
225
|
+
const b = block;
|
|
226
|
+
const list = (body) => (body?.items ?? []).flatMap((item) => {
|
|
227
|
+
const i = item;
|
|
228
|
+
return [runsText(i.content), ...(i.list ? list(i.list) : [])];
|
|
229
|
+
});
|
|
230
|
+
switch (b?.type) {
|
|
231
|
+
case "paragraph":
|
|
232
|
+
case "heading":
|
|
233
|
+
case "quote":
|
|
234
|
+
case "callout":
|
|
235
|
+
return runsText(b.content);
|
|
236
|
+
case "list":
|
|
237
|
+
return list(b).filter(Boolean).join(" ");
|
|
238
|
+
case "image":
|
|
239
|
+
return runsText(b.caption);
|
|
240
|
+
case "embed":
|
|
241
|
+
return typeof b.caption === "string" ? b.caption : "";
|
|
242
|
+
case "button":
|
|
243
|
+
return typeof b.label === "string" ? b.label : "";
|
|
244
|
+
default:
|
|
245
|
+
return "";
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* A document split at its headings: the blocks before the first heading (only when they hold
|
|
250
|
+
* words), then one section per heading, with the anchor `headingAnchors` gives it.
|
|
251
|
+
*/
|
|
252
|
+
export function richTextSections(value) {
|
|
253
|
+
const anchors = headingAnchors(value);
|
|
254
|
+
const sections = [
|
|
255
|
+
{ anchor: null, heading: "", parts: [] },
|
|
256
|
+
];
|
|
257
|
+
let heading = 0;
|
|
258
|
+
for (const block of value?.blocks ?? []) {
|
|
259
|
+
if (block?.type === "heading") {
|
|
260
|
+
sections.push({
|
|
261
|
+
anchor: anchors[heading] ?? null,
|
|
262
|
+
heading: blockText(block),
|
|
263
|
+
parts: [],
|
|
264
|
+
});
|
|
265
|
+
heading += 1;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
const text = blockText(block);
|
|
269
|
+
if (text)
|
|
270
|
+
sections.at(-1)?.parts.push(text);
|
|
271
|
+
}
|
|
272
|
+
return sections
|
|
273
|
+
.map((s) => ({ anchor: s.anchor, heading: s.heading, text: s.parts.join("\n") }))
|
|
274
|
+
.filter((s) => s.anchor !== null || s.text);
|
|
275
|
+
}
|