@open-artifact/docs-core 0.10.0 → 0.12.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-artifact/docs-core",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "The Docs type's model, free of DOM and React: blocks and characters with stable ids, the edit ops and their guards, markdown in and out, link anchors, suggestions and their decisions, version history rows and the export source.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -16,7 +16,7 @@
16
16
  "NOTICE"
17
17
  ],
18
18
  "dependencies": {
19
- "@open-artifact/live-store": "0.10.0"
19
+ "@open-artifact/live-store": "0.12.0"
20
20
  },
21
21
  "license": "Apache-2.0",
22
22
  "author": {
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The words a chip shows in the editor (a date, a dropdown's option, a person, a tab), worked out the same way on the
3
+ * page and on the server, so the find bar's count for the open tab and the server's count for the others agree.
4
+ */
5
+ /** How a viewer's page writes dates: its locale and time zone (the running system's when left out). */
6
+ export interface DateStyle {
7
+ locale?: string;
8
+ timeZone?: string;
9
+ }
10
+ /** What a chip names, looked up in the doc. Null when it is not there. */
11
+ export interface ChipLookup {
12
+ /** A dropdown option's name, by list id and option index. */
13
+ option(list: string, index: number): string | null;
14
+ /** A tab's name, by its id. */
15
+ tab(id: string): string | null;
16
+ /** A person's name, by the id a mention names them with. */
17
+ person(id: string): string | null;
18
+ }
19
+ export type ChipKind = 'date' | 'dropdown' | 'mention';
20
+ /** The tab a mention links to: `file/<id>` as an agent writes it, or the bare id the editor puts in. */
21
+ export declare function tabOfRef(ref: unknown): string | null;
22
+ /**
23
+ * A date chip's words: month and day, and the year when it is not the viewer's current year. A bare day
24
+ * (`2026-11-23`) is that day in any time zone. '' when the chip holds no date.
25
+ */
26
+ export declare function shownDate(ts: unknown, style?: DateStyle, now?: Date): string;
27
+ /**
28
+ * The words a chip shows, or null when it shows only a placeholder (no date set, no option picked, nobody named). A
29
+ * link to a tab reads as the tab's name now; a person as `@` and their name.
30
+ */
31
+ export declare function chipText(kind: ChipKind, attrs: Readonly<Record<string, unknown>>, lookup: ChipLookup, style?: DateStyle): string | null;
32
+ export declare const isChipKind: (kind: string) => kind is ChipKind;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * The words a chip shows in the editor (a date, a dropdown's option, a person, a tab), worked out the same way on the
3
+ * page and on the server, so the find bar's count for the open tab and the server's count for the others agree.
4
+ */
5
+ const BARE_DATE = /^\d{4}-\d{2}-\d{2}$/;
6
+ const TAB_ID = /^[0-9a-f]{8}-[0-9a-f]{4}$/;
7
+ const words = (value) => (typeof value === 'string' && value.trim() ? value : null);
8
+ /** The tab a mention links to: `file/<id>` as an agent writes it, or the bare id the editor puts in. */
9
+ export function tabOfRef(ref) {
10
+ if (typeof ref !== 'string')
11
+ return null;
12
+ const id = ref.startsWith('file/') ? ref.slice('file/'.length) : ref;
13
+ return TAB_ID.test(id) ? id : null;
14
+ }
15
+ /** `style` as Intl takes it: a locale or time zone it does not know is dropped. */
16
+ function usable(style) {
17
+ try {
18
+ new Intl.DateTimeFormat(style.locale, { timeZone: style.timeZone });
19
+ return style;
20
+ }
21
+ catch {
22
+ try {
23
+ new Intl.DateTimeFormat(undefined, { timeZone: style.timeZone });
24
+ return { timeZone: style.timeZone };
25
+ }
26
+ catch {
27
+ return {};
28
+ }
29
+ }
30
+ }
31
+ const yearIn = (at, timeZone) => new Intl.DateTimeFormat('en-US', { year: 'numeric', timeZone }).format(at);
32
+ /**
33
+ * A date chip's words: month and day, and the year when it is not the viewer's current year. A bare day
34
+ * (`2026-11-23`) is that day in any time zone. '' when the chip holds no date.
35
+ */
36
+ export function shownDate(ts, style = {}, now = new Date()) {
37
+ if (typeof ts !== 'string' || !ts)
38
+ return '';
39
+ const bare = BARE_DATE.test(ts);
40
+ const date = new Date(bare ? `${ts}T00:00:00Z` : ts);
41
+ if (Number.isNaN(date.getTime()))
42
+ return ts;
43
+ const { locale, timeZone } = usable(style);
44
+ const zone = bare ? 'UTC' : timeZone;
45
+ const thisYear = yearIn(date, zone) === yearIn(now, timeZone);
46
+ return new Intl.DateTimeFormat(locale, { month: 'short', day: 'numeric', ...(thisYear ? {} : { year: 'numeric' }), timeZone: zone }).format(date);
47
+ }
48
+ /**
49
+ * The words a chip shows, or null when it shows only a placeholder (no date set, no option picked, nobody named). A
50
+ * link to a tab reads as the tab's name now; a person as `@` and their name.
51
+ */
52
+ export function chipText(kind, attrs, lookup, style = {}) {
53
+ switch (kind) {
54
+ case 'date':
55
+ return shownDate(attrs.ts, style) || null;
56
+ case 'dropdown': {
57
+ const index = typeof attrs.index === 'number' ? attrs.index : typeof attrs.index === 'string' && attrs.index ? Number(attrs.index) : -1;
58
+ return typeof attrs.enum === 'string' && Number.isInteger(index) && index >= 0 ? words(lookup.option(attrs.enum, index)) : null;
59
+ }
60
+ case 'mention': {
61
+ const tab = tabOfRef(attrs.ref);
62
+ if (tab)
63
+ return words(lookup.tab(tab)) ?? words(attrs.label);
64
+ const principal = typeof attrs.principal === 'string' ? attrs.principal : null;
65
+ const name = words(attrs.label) ?? words(attrs.name) ?? (principal ? words(lookup.person(principal)) : null) ?? (principal === 'agent' ? 'agent' : words(attrs.ref));
66
+ return name ? `@${name}` : null;
67
+ }
68
+ }
69
+ }
70
+ export const isChipKind = (kind) => kind === 'date' || kind === 'dropdown' || kind === 'mention';
package/src/index.d.ts CHANGED
@@ -7,3 +7,4 @@ export * from './anchors.ts';
7
7
  export * from './decisions.ts';
8
8
  export * from './history.ts';
9
9
  export * from './export-source.ts';
10
+ export * from './chip-text.ts';
package/src/index.js CHANGED
@@ -7,3 +7,4 @@ export * from "./anchors.js";
7
7
  export * from "./decisions.js";
8
8
  export * from "./history.js";
9
9
  export * from "./export-source.js";
10
+ export * from "./chip-text.js";