@mdbase-dev/ui 0.2.5 → 0.2.7

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.
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { useEffect, useMemo, useRef, useState } from "react";
3
3
  import { Combo } from "../Combo.js";
4
- import { DateField } from "../DatePicker.js";
4
+ import { DateField, displayLocale, formatDay } from "../DatePicker.js";
5
5
  import { Glyph } from "../glyphs.js";
6
6
  import { useLocaleRevision } from "../i18n.js";
7
7
  import { LinkPicker } from "../LinkPicker.js";
@@ -9,7 +9,7 @@ import { Select } from "../Select.js";
9
9
  import { Switch } from "../Switch.js";
10
10
  import { CommitArea, CommitInput } from "./fields.js";
11
11
  import { propertyLabels } from "./labels.js";
12
- import { isLink, linkLabel, linkToPath, parseValue, pathToLink, valueLine } from "./model.js";
12
+ import { isLink, linkName, linkToPath, localMoment, parseValue, pathToLink, storedMoment, valueLine } from "./model.js";
13
13
  const noRecords = {
14
14
  describe: () => undefined,
15
15
  search: () => []
@@ -52,10 +52,18 @@ export function PropertyValue(props) {
52
52
  if (kind === "links") return _jsx(LinksValue, {
53
53
  name: name,
54
54
  value: value,
55
+ info: info,
55
56
  labels: labels,
56
57
  records: records,
57
- onChange: onChange
58
+ autoFocus: Boolean(props.autoFocus),
59
+ onChange: onChange,
60
+ onDone: props.onDone
58
61
  });
62
+ if (!onChange && kind === "datetime" && typeof value === "string") return readOnly(new Intl.DateTimeFormat(displayLocale(), {
63
+ dateStyle: "medium",
64
+ timeStyle: "short"
65
+ }).format(new Date(value)));
66
+ if (!onChange && kind === "date" && typeof value === "string") return readOnly(formatDay(value));
59
67
  if (!onChange) return readOnly(line(value) ?? (value === undefined || value === null ? undefined : typeof value === "string" ? value : JSON.stringify(value)));
60
68
  switch (kind) {
61
69
  case "choice":
@@ -87,14 +95,19 @@ export function PropertyValue(props) {
87
95
  "aria-label": name
88
96
  });
89
97
  case "tags":
90
- return _jsx(TagsValue, {
91
- name: name,
92
- items: Array.isArray(value) ? value.map(String) : [],
93
- labels: labels,
94
- autoFocus: Boolean(props.autoFocus),
95
- onChange: items => onChange(items.length ? items : value === undefined ? null : []),
96
- onDone: props.onDone
97
- });
98
+ {
99
+ // A list of numbers stays one: what is typed into it is read as a number where it is one.
100
+ const numbers = info?.items ? info.items.kind === "number" : Array.isArray(value) && value.length > 0 && value.every(item => typeof item === "number");
101
+ const typed = item => numbers && item.trim() !== "" && Number.isFinite(Number(item)) ? Number(item) : item;
102
+ return _jsx(TagsValue, {
103
+ name: name,
104
+ items: Array.isArray(value) ? value.map(String) : value === undefined || value === null || value === "" ? [] : [String(value)],
105
+ labels: labels,
106
+ autoFocus: Boolean(props.autoFocus),
107
+ onChange: items => onChange(items.length ? items.map(typed) : value === undefined ? null : []),
108
+ onDone: props.onDone
109
+ });
110
+ }
98
111
  case "number":
99
112
  return _jsx(CommitInput, {
100
113
  className: "mdbase-property-input",
@@ -102,6 +115,7 @@ export function PropertyValue(props) {
102
115
  placeholder: labels.empty,
103
116
  label: name,
104
117
  inputMode: "decimal",
118
+ restate: typeof value === "string" && value !== "",
105
119
  autoFocus: Boolean(props.autoFocus),
106
120
  onCommit: typed => {
107
121
  const number = Number(typed);
@@ -120,6 +134,17 @@ export function PropertyValue(props) {
120
134
  onChange: onChange,
121
135
  onClose: props.onDone
122
136
  });
137
+ case "datetime":
138
+ return _jsx(CommitInput, {
139
+ className: "mdbase-property-input",
140
+ moment: true,
141
+ value: localMoment(value),
142
+ label: name,
143
+ title: labels.localTime(Intl.DateTimeFormat().resolvedOptions().timeZone),
144
+ autoFocus: Boolean(props.autoFocus),
145
+ onCommit: typed => onChange(storedMoment(typed)),
146
+ onDone: props.onDone
147
+ });
123
148
  case "long":
124
149
  return _jsx(CommitArea, {
125
150
  className: "mdbase-property-input",
@@ -148,9 +173,10 @@ export function PropertyValue(props) {
148
173
  onCancel: props.onDone
149
174
  }) : _jsx(CommitInput, {
150
175
  className: "mdbase-property-input",
151
- value: typeof value === "string" ? value : "",
176
+ value: typeof value === "string" ? value : typeof value === "number" ? String(value) : "",
152
177
  placeholder: labels.empty,
153
178
  label: name,
179
+ restate: typeof value === "number",
154
180
  autoFocus: Boolean(props.autoFocus),
155
181
  onCommit: typed => onChange(typedValue(typed)),
156
182
  onDone: props.onDone
@@ -217,39 +243,78 @@ function TagsValue(props) {
217
243
  })]
218
244
  });
219
245
  }
220
- /** Links to other records as chips that open the record; more are found by typing. */
246
+ /**
247
+ * Links to other records as chips that open the record; more are found by typing, and where the
248
+ * host can make records, made from what was typed.
249
+ */
221
250
  function LinksValue(props) {
222
251
  const {
223
252
  labels,
224
- records
253
+ records,
254
+ name
225
255
  } = props;
226
- const [picking, setPicking] = useState(false);
256
+ const [picking, setPicking] = useState(props.autoFocus);
227
257
  const [query, setQuery] = useState("");
228
- const single = !Array.isArray(props.value);
229
- const links = (Array.isArray(props.value) ? props.value : [props.value]).filter(isLink);
230
- const paths = links.map(link => linkToPath(link));
231
- const options = useMemo(() => picking ? records.search(query).filter(found => !paths.includes(found.path)).slice(0, 30) : [],
258
+ const root = useRef(null);
259
+ const declared = props.info?.link;
260
+ // A declared link is one or several as its type says; any other is as it is written.
261
+ const single = declared ? !declared.many : !Array.isArray(props.value);
262
+ const held = Array.isArray(props.value) ? props.value : props.value === undefined || props.value === null || props.value === "" ? [] : [props.value];
263
+ // What a type calls a link is one however it is written; elsewhere only a whole wikilink is.
264
+ const links = held.filter(item => declared ? typeof item === "string" : isLink(item));
265
+ const leads = link => records.resolve ? records.resolve(link) : linkToPath(link);
266
+ const paths = links.map(leads);
267
+ const options = useMemo(() => picking ? records.search(query, name).filter(found => !paths.includes(found.path)).slice(0, 30) : [],
232
268
  // eslint-disable-next-line react-hooks/exhaustive-deps -- the paths are compared by what they say.
233
- [picking, query, records, paths.join("\n")]);
234
- const write = next => props.onChange?.(next.length ? single && next.length === 1 ? next[0] : next : null);
269
+ [picking, query, records, name, paths.join("\n")]);
270
+ // A record made through a form is linked later: by then the property may hold other links.
271
+ const latest = useRef(links);
272
+ latest.current = links;
273
+ const write = next => {
274
+ // A declared link keeps the shape its type gives it; any other becomes a list once it holds two.
275
+ const one = declared ? !declared.many : single && next.length === 1;
276
+ props.onChange?.(next.length ? one ? next[next.length - 1] : next : declared?.many ? [] : null);
277
+ };
278
+ const add = (path, title) => {
279
+ const link = records.link ? records.link(path, title, name) : pathToLink(path);
280
+ write(declared && !declared.many ? [link] : [...latest.current, link]);
281
+ };
282
+ const focusProperty = () => root.current?.querySelector("input, .mdbase-token-add, .mdbase-token button")?.focus({
283
+ preventScroll: true
284
+ });
285
+ const close = () => {
286
+ setQuery("");
287
+ setPicking(false);
288
+ props.onDone?.();
289
+ };
235
290
  const open = records.open;
291
+ const creates = props.onChange && records.create ? records.creates?.(name) ?? [] : [];
292
+ // A property declared to link one record offers no second.
293
+ const full = declared !== undefined && !declared.many && links.length > 0;
236
294
  return _jsxs("div", {
295
+ ref: root,
237
296
  className: "mdbase-tokens",
238
297
  "data-links": "",
239
298
  children: [links.map((link, index) => {
240
- const path = paths[index] ?? null;
241
- const known = path === null ? undefined : records.describe(path);
242
- const title = known?.title ?? linkLabel(link);
299
+ const path = paths[index];
300
+ const known = typeof path === "string" ? records.describe(path) : undefined;
301
+ const title = known?.title ?? linkName(link);
302
+ // A link still being looked up is not yet one that leads nowhere.
303
+ const missing = records.resolve ? path === null : !known;
243
304
  return _jsxs("span", {
244
305
  className: "mdbase-token",
245
306
  "data-link": "",
246
- "data-missing": known ? undefined : "",
247
- children: [open && path !== null ? _jsx("button", {
307
+ "data-missing": missing ? "" : undefined,
308
+ children: [open && typeof path === "string" ? _jsxs("button", {
248
309
  type: "button",
249
310
  className: "mdbase-token-open",
250
311
  title: path,
251
312
  onClick: () => open(path),
252
- children: title
313
+ children: [known?.icon ? _jsx("span", {
314
+ className: "mdbase-token-icon",
315
+ "aria-hidden": "true",
316
+ children: known.icon
317
+ }) : null, title]
253
318
  }) : _jsx("span", {
254
319
  className: "mdbase-token-open",
255
320
  title: path ?? undefined,
@@ -264,9 +329,12 @@ function LinksValue(props) {
264
329
  })
265
330
  }) : null]
266
331
  }, link);
267
- }), props.onChange ? picking ? _jsx(LinkPicker, {
332
+ }), !props.onChange ? links.length ? null : _jsx("span", {
333
+ className: "mdbase-property-static is-empty",
334
+ children: labels.empty
335
+ }) : picking ? _jsx(LinkPicker, {
268
336
  className: "mdbase-property-link-picker",
269
- label: labels.linkRecord(props.name),
337
+ label: labels.linkRecord(name),
270
338
  placeholder: labels.findRecord,
271
339
  query: query,
272
340
  onQueryChange: setQuery,
@@ -275,30 +343,42 @@ function LinksValue(props) {
275
343
  title: found.title,
276
344
  ...(found.detail ? {
277
345
  detail: found.detail
346
+ } : {}),
347
+ ...(found.icon ? {
348
+ icon: found.icon
278
349
  } : {})
279
350
  })),
351
+ creates: creates,
352
+ createLabel: (create, title) => labels.createRecord(create.label, title),
280
353
  emptyMessage: labels.noMatches,
281
354
  autoFocus: true,
282
355
  onPick: option => {
283
- write([...links, pathToLink(option.id)]);
284
- setQuery("");
285
- setPicking(false);
356
+ add(option.id, option.title);
357
+ close();
286
358
  },
287
- onClose: () => {
288
- setQuery("");
289
- setPicking(false);
290
- }
291
- }) : _jsx("button", {
359
+ onCreate: (create, title) => {
360
+ records.create?.(create.id, title, {
361
+ property: name,
362
+ anchor: () => root.current?.getBoundingClientRect(),
363
+ link: (path, made) => add(path, made),
364
+ restoreFocus: focusProperty
365
+ });
366
+ close();
367
+ },
368
+ onClose: close
369
+ }) : full ? null : _jsx("button", {
292
370
  type: "button",
293
- className: "mdbase-token-add",
371
+ className: links.length ? "mdbase-token-add" : "mdbase-token-add is-first",
294
372
  "data-autofocus": "",
295
- "aria-label": labels.linkRecord(props.name),
296
- title: labels.linkRecord(props.name),
373
+ "aria-label": labels.linkRecord(name),
374
+ title: labels.linkRecord(name),
297
375
  onClick: () => setPicking(true),
298
- children: _jsx(Glyph, {
376
+ children: links.length ? _jsx(Glyph, {
299
377
  name: "plus",
300
378
  size: 12
379
+ }) : _jsx("span", {
380
+ children: labels.empty
301
381
  })
302
- }) : null]
382
+ })]
303
383
  });
304
384
  }
@@ -7,6 +7,11 @@ export declare function CommitInput(props: {
7
7
  readonly autoFocus?: boolean | undefined;
8
8
  readonly className: string;
9
9
  readonly inputMode?: "decimal" | "text" | undefined;
10
+ /** A date and time in the reader's own zone, picked with the browser's control, in place of text. */
11
+ readonly moment?: boolean | undefined;
12
+ readonly title?: string | undefined;
13
+ /** The text is how a value of another kind reads: Enter hands it over as typed though nothing was changed. */
14
+ readonly restate?: boolean | undefined;
10
15
  readonly onCommit: (value: string) => void;
11
16
  readonly onDone?: (() => void) | undefined;
12
17
  }): JSX.Element;
@@ -19,7 +19,9 @@ export function CommitInput(props) {
19
19
  ref: ref,
20
20
  className: props.className,
21
21
  "data-autofocus": props.autoFocus ? "" : undefined,
22
- type: "text",
22
+ type: props.moment ? "datetime-local" : "text",
23
+ step: props.moment ? 1 : undefined,
24
+ title: props.title,
23
25
  inputMode: props.inputMode,
24
26
  value: text,
25
27
  placeholder: props.placeholder,
@@ -37,6 +39,8 @@ export function CommitInput(props) {
37
39
  if (event.key === "Escape") {
38
40
  cancelled.current = true;
39
41
  setText(props.value);
42
+ } else if (props.restate && text === props.value) {
43
+ props.onCommit(text);
40
44
  }
41
45
  event.currentTarget.blur();
42
46
  }
@@ -5,7 +5,11 @@ export interface PropertyLabels {
5
5
  empty: string;
6
6
  noProperties: string;
7
7
  addProperty: string;
8
+ /** Adding a part to a value that has parts, which is named. */
9
+ addPropertyTo: (property: string) => string;
8
10
  findProperty: string;
11
+ /** In a value that takes only the parts its type names. */
12
+ findDeclaredProperty: string;
9
13
  /** Before a typed name that no property has yet. */
10
14
  newProperty: string;
11
15
  /** Before typed text that none of the offered values is. */
@@ -15,9 +19,22 @@ export interface PropertyLabels {
15
19
  propertyNameInvalid: string;
16
20
  removeProperty: (name: string) => string;
17
21
  removeValue: (value: string) => string;
22
+ /** Beside a property the record is not valid without. */
23
+ required: string;
24
+ /** How many parts a value with parts holds. */
25
+ partCount: (count: number) => string;
26
+ /** How many values a list holds. */
27
+ itemCount: (count: number) => string;
28
+ addItem: string;
29
+ addItemTo: (property: string) => string;
30
+ removeItem: (property: string, place: number) => string;
31
+ /** Says whose clock a date and time is in. */
32
+ localTime: (zone: string) => string;
18
33
  clearValue: string;
19
34
  linkRecord: (property: string) => string;
20
35
  findRecord: string;
36
+ /** A way to make a record from what was typed: the kind of record, then its title. */
37
+ createRecord: (kind: string, title: string) => string;
21
38
  noMatches: string;
22
39
  /** A true or false value where it is only shown. */
23
40
  yes: string;
@@ -22,12 +22,27 @@ export function propertyLabels() {
22
22
  id: "F-NRCj",
23
23
  message: "Add property"
24
24
  }),
25
+ addPropertyTo: property => _i18n._(
26
+ /** i18n */
27
+ {
28
+ id: "GkYnD8",
29
+ message: "Add property to {property}",
30
+ values: {
31
+ property: property
32
+ }
33
+ }),
25
34
  findProperty: _i18n._(
26
35
  /** i18n */
27
36
  {
28
37
  id: "aRQRsv",
29
38
  message: "Find a property, or name a new one\u2026"
30
39
  }),
40
+ findDeclaredProperty: _i18n._(
41
+ /** i18n */
42
+ {
43
+ id: "5vv-EU",
44
+ message: "Find a property\u2026"
45
+ }),
31
46
  newProperty: _i18n._(
32
47
  /** i18n */
33
48
  {
@@ -80,6 +95,65 @@ export function propertyLabels() {
80
95
  value: value
81
96
  }
82
97
  }),
98
+ required: _i18n._(
99
+ /** i18n */
100
+ {
101
+ id: "xymyof",
102
+ message: "Required",
103
+ context: "A property a record must have"
104
+ }),
105
+ partCount: count => _i18n._(
106
+ /** i18n */
107
+ {
108
+ id: "f4tG3Y",
109
+ message: "{count, plural, one {# property} other {# properties}}",
110
+ values: {
111
+ count: count
112
+ }
113
+ }),
114
+ itemCount: count => _i18n._(
115
+ /** i18n */
116
+ {
117
+ id: "Md_eZS",
118
+ message: "{count, plural, one {# item} other {# items}}",
119
+ values: {
120
+ count: count
121
+ }
122
+ }),
123
+ addItem: _i18n._(
124
+ /** i18n */
125
+ {
126
+ id: "NQ8llq",
127
+ message: "Add item"
128
+ }),
129
+ addItemTo: property => _i18n._(
130
+ /** i18n */
131
+ {
132
+ id: "mS1yMl",
133
+ message: "Add item to {property}",
134
+ values: {
135
+ property: property
136
+ }
137
+ }),
138
+ removeItem: (property, place) => _i18n._(
139
+ /** i18n */
140
+ {
141
+ id: "DYvuIE",
142
+ message: "Remove item {place} from {property}",
143
+ values: {
144
+ place: place,
145
+ property: property
146
+ }
147
+ }),
148
+ localTime: zone => _i18n._(
149
+ /** i18n */
150
+ {
151
+ id: "GiK2sQ",
152
+ message: "Local time ({zone})",
153
+ values: {
154
+ zone: zone
155
+ }
156
+ }),
83
157
  clearValue: _i18n._(
84
158
  /** i18n */
85
159
  {
@@ -102,6 +176,16 @@ export function propertyLabels() {
102
176
  id: "VWWfAK",
103
177
  message: "Find a record\u2026"
104
178
  }),
179
+ createRecord: (kind, title) => _i18n._(
180
+ /** i18n */
181
+ {
182
+ id: "PgVnNr",
183
+ message: "{kind} \u201C{title}\u201D",
184
+ values: {
185
+ kind: kind,
186
+ title: title
187
+ }
188
+ }),
105
189
  noMatches: _i18n._(
106
190
  /** i18n */
107
191
  {
@@ -4,14 +4,32 @@ export type PropertyScalar = string | number | boolean | null;
4
4
  /** What is known about a property: what a type declares for it, or what records hold for it. */
5
5
  export interface PropertyInfo {
6
6
  readonly name: string;
7
- readonly kind: "text" | "number" | "boolean" | "list" | "other";
7
+ /** `group` is a value with named parts of its own; `list` is several values. */
8
+ readonly kind: "text" | "number" | "boolean" | "list" | "group" | "other";
8
9
  /** The values the property may take, when they are listed. */
9
10
  readonly options?: readonly PropertyScalar[] | undefined;
10
11
  /** `date` or `date-time`, when that is known. */
11
12
  readonly format?: string | undefined;
13
+ /** The record is not valid without it: its row is always shown and it cannot be taken off. */
14
+ readonly required?: boolean | undefined;
15
+ /** What the property is for, in the type's words. */
16
+ readonly description?: string | undefined;
17
+ /** The parts a group names, in the type's order. */
18
+ readonly parts?: readonly PropertyInfo[] | undefined;
19
+ /** What a part the group does not name holds, or false when it may hold no such part. */
20
+ readonly rest?: PropertyInfo | false | undefined;
21
+ /** What each value of a list is, when that is known. Its name is not used. */
22
+ readonly items?: PropertyInfo | undefined;
23
+ /** The property links to other records: to one, or to several. */
24
+ readonly link?: {
25
+ readonly many: boolean;
26
+ } | undefined;
12
27
  }
13
- /** How a property is shown and edited. */
14
- export type PropertyKind = "choice" | "boolean" | "date" | "number" | "tags" | "links" | "text" | "long" | "other";
28
+ /**
29
+ * How a property is shown and edited. A `group` and a list of `items` are shown part by part,
30
+ * each part with the control its own value calls for.
31
+ */
32
+ export type PropertyKind = "choice" | "boolean" | "date" | "datetime" | "number" | "tags" | "links" | "text" | "long" | "group" | "items" | "other";
15
33
  export declare const isLink: (value: unknown) => value is string;
16
34
  export declare const isScalar: (value: unknown) => value is PropertyScalar;
17
35
  export declare const isGroup: (value: unknown) => value is Readonly<Record<string, unknown>>;
@@ -23,8 +41,22 @@ export declare function isEmptyValue(value: unknown): boolean;
23
41
  export declare function parseValue(text: string): PropertyScalar;
24
42
  /** What a value is, going by what it holds first and by what is known of the property when it holds nothing. */
25
43
  export declare function propertyKind(value: unknown, info: PropertyInfo | undefined): PropertyKind;
26
- /** What is known about a property: what the record's type declares, or failing that what other records hold for it. */
27
- export declare function describeProperty(property: string, declared: readonly PropertyInfo[], inUse: readonly PropertyInfo[]): {
44
+ /** A moment as a `datetime-local` field holds it: the reader's own date and time, to the second. Empty when the text is not a moment. */
45
+ export declare function localMoment(value: unknown): string;
46
+ /** The moment a `datetime-local` field holds, as it is stored: in UTC. Null when the field holds none. */
47
+ export declare function storedMoment(local: string): string | null;
48
+ /**
49
+ * What a property holds when it is first given a place: nothing, or for a value with parts the
50
+ * parts it must have, each as blank as its kind allows.
51
+ */
52
+ export declare function blankValue(info: PropertyInfo | undefined): unknown;
53
+ /** How many values a value holds once its lists and groups are opened up, counted no further than `limit`. */
54
+ export declare function valueCount(value: unknown, limit?: number): number;
55
+ /**
56
+ * What is known about a property: what the record's type declares, or failing that what other
57
+ * records hold for it, or what a group says of the parts it does not name.
58
+ */
59
+ export declare function describeProperty(property: string, declared: readonly PropertyInfo[], inUse: readonly PropertyInfo[], unnamed?: PropertyInfo | false | undefined): {
28
60
  info: PropertyInfo | undefined;
29
61
  suggestions: readonly string[] | undefined;
30
62
  own: boolean;
@@ -35,6 +67,11 @@ export declare function linkToPath(link: string): string | null;
35
67
  export declare function pathToLink(path: string): string;
36
68
  /** `[[sources/crime|Crime and Punishment]]` as "Crime and Punishment". Other text is left alone. */
37
69
  export declare function linkLabel(value: string): string;
70
+ /**
71
+ * What a link is called while its record is not known: the name it gives, or failing that the
72
+ * last part of where it leads. Reads a wikilink, a Markdown link and a bare path.
73
+ */
74
+ export declare function linkName(link: string): string;
38
75
  /** A value as one short line of text, or undefined when it does not fit on one. */
39
76
  export declare function valueLine(value: unknown, words: {
40
77
  readonly yes: string;
@@ -28,21 +28,82 @@ export function parseValue(text) {
28
28
  /** What a value is, going by what it holds first and by what is known of the property when it holds nothing. */
29
29
  export function propertyKind(value, info) {
30
30
  const unset = value === undefined || value === null || value === "";
31
+ // A property declared as a link is one whenever its value has the shape links are written in.
32
+ if (info?.link && (unset || typeof value === "string" || Array.isArray(value) && value.every(item => typeof item === "string"))) return "links";
31
33
  if (Array.isArray(value) ? value.length > 0 && value.every(isLink) : isLink(value)) return "links";
34
+ if (isGroup(value)) return "group";
32
35
  if (info?.options?.length && (unset || isScalar(value))) return "choice";
33
36
  if (typeof value === "boolean" || unset && info?.kind === "boolean") return "boolean";
34
- if (Array.isArray(value)) return value.every(item => typeof item === "string" || typeof item === "number") ? "tags" : "other";
35
- if (unset && info?.kind === "list") return "tags";
37
+ if (Array.isArray(value)) return !partedItems(info) && value.every(item => typeof item === "string" || typeof item === "number") ? "tags" : "items";
38
+ // One value where the type says a list of short ones is that list's first.
39
+ if (!unset && info?.kind === "list" && !partedItems(info) && (typeof value === "string" || typeof value === "number")) return "tags";
40
+ if (unset && info?.kind === "list") return partedItems(info) ? "items" : "tags";
41
+ if (unset && info?.kind === "group") return "group";
42
+ // Text where the type says a number, or the other way about, is edited as the type says: what is typed puts it right.
43
+ if (typeof value === "string" && info?.kind === "number") return "number";
44
+ if (typeof value === "number" && info?.kind === "text") return "text";
36
45
  if (typeof value === "number" || unset && info?.kind === "number") return "number";
37
46
  if (typeof value === "string") {
38
47
  if (info?.format === "date" ? day.test(value) : !info && day.test(value)) return "date";
48
+ // Only a declared moment is edited as one: writing it back puts it in UTC, which a type that says so expects.
49
+ if (info?.format === "date-time" && localMoment(value)) return "datetime";
39
50
  return value.includes("\n") || value.length > 90 ? "long" : "text";
40
51
  }
41
- if (unset) return info?.format === "date" ? "date" : "text";
52
+ if (unset) return info?.format === "date" ? "date" : info?.format === "date-time" ? "datetime" : "text";
42
53
  return "other";
43
54
  }
44
- /** What is known about a property: what the record's type declares, or failing that what other records hold for it. */
45
- export function describeProperty(property, declared, inUse) {
55
+ /** A moment as a `datetime-local` field holds it: the reader's own date and time, to the second. Empty when the text is not a moment. */
56
+ export function localMoment(value) {
57
+ if (typeof value !== "string" || !instant.test(value)) return "";
58
+ const at = new Date(value);
59
+ if (Number.isNaN(at.getTime())) return "";
60
+ return new Date(at.getTime() - at.getTimezoneOffset() * 60_000).toISOString().slice(0, 19);
61
+ }
62
+ /** The moment a `datetime-local` field holds, as it is stored: in UTC. Null when the field holds none. */
63
+ export function storedMoment(local) {
64
+ const at = new Date(local);
65
+ return local === "" || Number.isNaN(at.getTime()) ? null : at.toISOString();
66
+ }
67
+ /** Whether a list's values are known to be more than short text or numbers: they are then shown one by one. */
68
+ function partedItems(info) {
69
+ const kind = info?.items?.kind;
70
+ return kind !== undefined && kind !== "text" && kind !== "number";
71
+ }
72
+ /**
73
+ * What a property holds when it is first given a place: nothing, or for a value with parts the
74
+ * parts it must have, each as blank as its kind allows.
75
+ */
76
+ export function blankValue(info) {
77
+ if (!info || info.options?.length || info.link) return null;
78
+ if (info.kind === "group") {
79
+ const parts = [];
80
+ for (const part of info.parts ?? []) {
81
+ const blank = part.required ? blankValue(part) : null;
82
+ if (blank !== null) parts.push([part.name, blank]);
83
+ }
84
+ return Object.fromEntries(parts);
85
+ }
86
+ if (info.kind === "list") return [];
87
+ if (info.kind === "boolean") return false;
88
+ return info.kind === "text" && !info.format ? "" : null;
89
+ }
90
+ /** How many values a value holds once its lists and groups are opened up, counted no further than `limit`. */
91
+ export function valueCount(value, limit = 64) {
92
+ if (Array.isArray(value) || isGroup(value)) {
93
+ let count = 0;
94
+ for (const inner of Array.isArray(value) ? value : Object.values(value)) {
95
+ count += valueCount(inner, limit - count);
96
+ if (count >= limit) return limit;
97
+ }
98
+ return count;
99
+ }
100
+ return 1;
101
+ }
102
+ /**
103
+ * What is known about a property: what the record's type declares, or failing that what other
104
+ * records hold for it, or what a group says of the parts it does not name.
105
+ */
106
+ export function describeProperty(property, declared, inUse, unnamed) {
46
107
  const own = declared.find(field => field.name === property);
47
108
  if (own) return {
48
109
  info: own,
@@ -51,7 +112,10 @@ export function describeProperty(property, declared, inUse) {
51
112
  };
52
113
  const seen = inUse.find(field => field.name === property);
53
114
  if (!seen) return {
54
- info: undefined,
115
+ info: unnamed ? {
116
+ ...unnamed,
117
+ name: property
118
+ } : undefined,
55
119
  suggestions: undefined,
56
120
  own: false
57
121
  };
@@ -90,6 +154,17 @@ export function linkLabel(value) {
90
154
  return (alias ?? pathStem(target.split("#")[0])).trim();
91
155
  });
92
156
  }
157
+ /**
158
+ * What a link is called while its record is not known: the name it gives, or failing that the
159
+ * last part of where it leads. Reads a wikilink, a Markdown link and a bare path.
160
+ */
161
+ export function linkName(link) {
162
+ const text = link.trim();
163
+ if (wholeLink.test(text)) return linkLabel(text);
164
+ const markdown = /^!?\[([^\]]*)\]\(<?([^)>]*)>?\)$/u.exec(text);
165
+ if (markdown) return markdown[1].trim() || pathStem(markdown[2]);
166
+ return pathStem(text) || text;
167
+ }
93
168
  /** A value as one short line of text, or undefined when it does not fit on one. */
94
169
  export function valueLine(value, words) {
95
170
  if (typeof value === "string") {
@@ -6,8 +6,8 @@
6
6
  export { PropertyKindIcon, PropertyList } from "./properties/PropertyList.js";
7
7
  export type { PropertyListProps } from "./properties/PropertyList.js";
8
8
  export { PropertyValue } from "./properties/PropertyValue.js";
9
- export type { PropertyRecords, PropertyValueProps } from "./properties/PropertyValue.js";
9
+ export type { PropertyLinkCreation, PropertyRecord, PropertyRecords, PropertyValueProps } from "./properties/PropertyValue.js";
10
10
  export { propertyLabels } from "./properties/labels.js";
11
11
  export type { PropertyLabels } from "./properties/labels.js";
12
- export { describeProperty, filterFieldsFromProperties, inferProperty, isEmptyValue, isPropertyName, linkLabel, linkToPath, parseValue, pathToLink, propertiesInUse, propertyKind, valueLine, } from "./properties/model.js";
12
+ export { blankValue, describeProperty, filterFieldsFromProperties, inferProperty, isEmptyValue, isPropertyName, linkLabel, linkToPath, parseValue, pathToLink, propertiesInUse, propertyKind, valueLine, } from "./properties/model.js";
13
13
  export type { PropertyInfo, PropertyKind, PropertyScalar } from "./properties/model.js";
@@ -6,4 +6,4 @@
6
6
  export { PropertyKindIcon, PropertyList } from "./properties/PropertyList.js";
7
7
  export { PropertyValue } from "./properties/PropertyValue.js";
8
8
  export { propertyLabels } from "./properties/labels.js";
9
- export { describeProperty, filterFieldsFromProperties, inferProperty, isEmptyValue, isPropertyName, linkLabel, linkToPath, parseValue, pathToLink, propertiesInUse, propertyKind, valueLine } from "./properties/model.js";
9
+ export { blankValue, describeProperty, filterFieldsFromProperties, inferProperty, isEmptyValue, isPropertyName, linkLabel, linkToPath, parseValue, pathToLink, propertiesInUse, propertyKind, valueLine } from "./properties/model.js";