@mdbase-dev/ui 0.1.0-beta.166 → 0.1.0-beta.171

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.
@@ -592,6 +592,15 @@ export const defaultCollectionCopy = {
592
592
  context: "Collection access interface"
593
593
  });
594
594
  },
595
+ get lockedNotice() {
596
+ return i18n._(
597
+ /** i18n */
598
+ {
599
+ id: "OSzcEZ",
600
+ message: "This is a Private collection, and this device doesn't have its key yet.",
601
+ context: "Collection access interface"
602
+ });
603
+ },
595
604
  get unavailableHeading() {
596
605
  return i18n._({
597
606
  ...
@@ -10,8 +10,8 @@ export function collectionView(snapshot, options) {
10
10
  ...options.copy
11
11
  };
12
12
  const app = options.appName;
13
- const phase = snapshot.phase;
14
- const currentId = currentCollectionId(phase);
13
+ const shown = snapshot.phase;
14
+ const currentId = currentCollectionId(shown);
15
15
  const collections = collectionRows(snapshot.collections, currentId, copy);
16
16
  const current = collections.find(row => row.current) ?? null;
17
17
  const nameOf = (id, fallback = null) => id && collections.find(row => row.id === id)?.name || fallback;
@@ -30,7 +30,7 @@ export function collectionView(snapshot, options) {
30
30
  unsyncedWork: hasUnsyncedWork(snapshot)
31
31
  };
32
32
  // Not part of every SDK's phase union yet (see unsynced.ts).
33
- const unsynced = unsyncedPhase(phase);
33
+ const unsynced = unsyncedPhase(shown);
34
34
  if (unsynced) return {
35
35
  ...common,
36
36
  kind: "unsynced",
@@ -39,6 +39,34 @@ export function collectionView(snapshot, options) {
39
39
  heading: app,
40
40
  lead: null
41
41
  };
42
+ // A Private collection this device has joined but holds no key for. These
43
+ // screens have no unlock form, so it is shown as a collection that cannot be
44
+ // opened here: the SDK's description of a failed unlock when there is one,
45
+ // otherwise why it is closed. The person can still choose another collection.
46
+ const locked = lockedPhase(shown);
47
+ if (locked) {
48
+ const name = nameOf(locked.collectionId, locked.displayName);
49
+ const notice = locked.problem ? quietNotice(locked.problem) : {
50
+ key: "needs_unlock",
51
+ tone: "info",
52
+ title: null,
53
+ message: copy.lockedNotice,
54
+ control: null,
55
+ transient: false
56
+ };
57
+ return {
58
+ ...common,
59
+ kind: "problem",
60
+ collectionId: locked.collectionId,
61
+ notice,
62
+ unsynced: false,
63
+ heading: name ? say(copy.problemHeading, name) : say(copy.problemHeadingUnnamed),
64
+ lead: null
65
+ };
66
+ }
67
+ // Both phases above have returned; what is left is exhaustive against SDK
68
+ // releases with or without them.
69
+ const phase = shown;
42
70
  switch (phase.kind) {
43
71
  case "starting":
44
72
  return {
@@ -295,7 +323,19 @@ function approvalNotice(outcome, copy) {
295
323
  transient: false
296
324
  };
297
325
  }
326
+ function lockedPhase(phase) {
327
+ const value = phase;
328
+ if (value.kind !== "needs_unlock" || typeof value.collectionId !== "string") return null;
329
+ return {
330
+ kind: "needs_unlock",
331
+ collectionId: value.collectionId,
332
+ displayName: typeof value.displayName === "string" ? value.displayName : null,
333
+ problem: value.problem ?? null
334
+ };
335
+ }
298
336
  function currentCollectionId(phase) {
337
+ const locked = lockedPhase(phase);
338
+ if (locked) return locked.collectionId;
299
339
  switch (phase.kind) {
300
340
  case "opening":
301
341
  case "needs_setup":
@@ -0,0 +1,31 @@
1
+ import { type Extension } from "@codemirror/state";
2
+ import { EditorView } from "@codemirror/view";
3
+ import { type CommentTarget } from "./comments.js";
4
+ import "./i18n.js";
5
+ export interface CommentAnchor {
6
+ /** The thread's first comment's path. */
7
+ readonly id: string;
8
+ /** Located in the editor's own text when shown; a detached anchor is not shown. */
9
+ readonly target: CommentTarget;
10
+ /** A suggestion's replacement text. */
11
+ readonly replacement?: string;
12
+ }
13
+ /** What a hover over an anchor shows of its thread. */
14
+ export interface CommentAnchorCard {
15
+ readonly kind: "comment" | "suggestion";
16
+ readonly author: string;
17
+ readonly when: string;
18
+ readonly text: string;
19
+ readonly replies: number;
20
+ }
21
+ /**
22
+ * Anchors, a click on one (its thread's id), and a card on hover saying who
23
+ * said what. Call `showCommentAnchors` whenever the threads change.
24
+ */
25
+ export declare function commentAnchors(onAnchor: (id: string) => void, card?: (id: string) => CommentAnchorCard | null): Extension;
26
+ export declare function showCommentAnchors(view: EditorView, anchors: readonly CommentAnchor[], active: string | null): void;
27
+ /** Where the anchor of thread `id` is in the editor's text now, or null when it is not shown. */
28
+ export declare function commentAnchorRange(view: EditorView, id: string): {
29
+ from: number;
30
+ to: number;
31
+ } | null;
@@ -0,0 +1,183 @@
1
+ // Comment anchors in a CodeMirror editor: commented passages are highlighted,
2
+ // and a suggestion shows as a tracked change (the text it replaces struck
3
+ // through, its replacement inline after it). Anchors follow edits between
4
+ // updates. Style them with `@mdbase-dev/ui/comments.css`.
5
+ import { i18n as _i18n } from "@lingui/core";
6
+ import { StateEffect, StateField } from "@codemirror/state";
7
+ import { Decoration, EditorView, hoverTooltip, WidgetType } from "@codemirror/view";
8
+ import { editorTarget, locate } from "./comments.js";
9
+ import "./i18n.js";
10
+ const setAnchors = StateEffect.define();
11
+ class Insertion extends WidgetType {
12
+ id;
13
+ text;
14
+ active;
15
+ constructor(id, text, active) {
16
+ super();
17
+ this.id = id;
18
+ this.text = text;
19
+ this.active = active;
20
+ }
21
+ eq(other) {
22
+ return other.id === this.id && other.text === this.text && other.active === this.active;
23
+ }
24
+ toDOM() {
25
+ const span = document.createElement("span");
26
+ // An empty insertion is a comment on a point between two characters.
27
+ span.className = `${this.text ? "cm-suggest-insert" : "cm-comment-point"}${this.active ? " is-active" : ""}`;
28
+ span.dataset["comment"] = this.id;
29
+ span.textContent = this.text;
30
+ span.title = this.text ? _i18n._(
31
+ /** i18n */
32
+ {
33
+ id: "KPbDqD",
34
+ message: "Suggested insertion"
35
+ }) : _i18n._(
36
+ /** i18n */
37
+ {
38
+ id: "NBdIgR",
39
+ message: "Comment"
40
+ });
41
+ return span;
42
+ }
43
+ ignoreEvent() {
44
+ return false;
45
+ }
46
+ }
47
+ function decorations(text, anchors, active) {
48
+ const ranges = [];
49
+ for (const a of anchors) {
50
+ const at = locate(text, editorTarget(a.target));
51
+ if (!at) continue;
52
+ const {
53
+ from,
54
+ to
55
+ } = at;
56
+ const isActive = a.id === active;
57
+ const suggestion = a.replacement !== undefined;
58
+ if (to > from) {
59
+ const className = `${suggestion ? "cm-suggest-delete" : "cm-comment"}${isActive ? " is-active" : ""}`;
60
+ // A suggestion that only replaces is still a deletion followed by an insertion.
61
+ ranges.push(Decoration.mark({
62
+ class: className,
63
+ attributes: {
64
+ "data-comment": a.id
65
+ }
66
+ }).range(from, to));
67
+ }
68
+ if (suggestion && a.replacement) ranges.push(Decoration.widget({
69
+ widget: new Insertion(a.id, a.replacement, isActive),
70
+ side: 1
71
+ }).range(to));else if (!suggestion && to === from) ranges.push(Decoration.widget({
72
+ widget: new Insertion(a.id, "", isActive),
73
+ side: 1
74
+ }).range(to));
75
+ }
76
+ return Decoration.set(ranges, true);
77
+ }
78
+ const anchorField = StateField.define({
79
+ create: () => Decoration.none,
80
+ update(value, tr) {
81
+ for (const e of tr.effects) if (e.is(setAnchors)) return decorations(tr.state.doc.toString(), e.value.anchors, e.value.active);
82
+ return tr.docChanged ? value.map(tr.changes) : value;
83
+ },
84
+ provide: f => EditorView.decorations.from(f)
85
+ });
86
+ const anchorId = element => element instanceof Element ? element.closest("[data-comment]")?.dataset["comment"] : undefined;
87
+ /**
88
+ * Anchors, a click on one (its thread's id), and a card on hover saying who
89
+ * said what. Call `showCommentAnchors` whenever the threads change.
90
+ */
91
+ export function commentAnchors(onAnchor, card) {
92
+ const hover = hoverTooltip((view, pos) => {
93
+ if (!card) return null;
94
+ let hit = null;
95
+ view.state.field(anchorField).between(pos, pos, (from, to, value) => {
96
+ const spec = value.spec;
97
+ const id = spec.attributes?.["data-comment"] ?? spec.widget?.id;
98
+ if (id && !hit) hit = {
99
+ id,
100
+ from,
101
+ to
102
+ };
103
+ });
104
+ if (!hit) return null;
105
+ const {
106
+ id,
107
+ from,
108
+ to
109
+ } = hit;
110
+ const info = card(id);
111
+ if (!info) return null;
112
+ return {
113
+ pos: from,
114
+ end: to,
115
+ above: true,
116
+ create: () => {
117
+ const dom = document.createElement("div");
118
+ dom.className = "cm-comment-tip";
119
+ const who = document.createElement("div");
120
+ who.className = "cm-comment-who";
121
+ who.textContent = `${info.author} · ${info.when}`;
122
+ const body = document.createElement("div");
123
+ body.className = "cm-comment-said";
124
+ const said = info.text || (info.kind === "suggestion" ? _i18n._(
125
+ /** i18n */
126
+ {
127
+ id: "j37rm8",
128
+ message: "Suggested edit"
129
+ }) : "");
130
+ body.textContent = said.length > 240 ? `${said.slice(0, 239)}…` : said;
131
+ dom.append(who, body);
132
+ if (info.replies) {
133
+ const replies = document.createElement("div");
134
+ replies.className = "cm-comment-who";
135
+ replies.textContent = _i18n._(
136
+ /** i18n */
137
+ {
138
+ id: "vbsPap",
139
+ message: "{0, plural, one {# reply} other {# replies}}",
140
+ values: {
141
+ 0: info.replies
142
+ }
143
+ });
144
+ dom.append(replies);
145
+ }
146
+ return {
147
+ dom
148
+ };
149
+ }
150
+ };
151
+ }, {
152
+ hoverTime: 250
153
+ });
154
+ return [anchorField, hover, EditorView.domEventHandlers({
155
+ click(event) {
156
+ const id = anchorId(event.target);
157
+ if (id) onAnchor(id);
158
+ return false;
159
+ }
160
+ })];
161
+ }
162
+ export function showCommentAnchors(view, anchors, active) {
163
+ view.dispatch({
164
+ effects: setAnchors.of({
165
+ anchors,
166
+ active
167
+ })
168
+ });
169
+ }
170
+ /** Where the anchor of thread `id` is in the editor's text now, or null when it is not shown. */
171
+ export function commentAnchorRange(view, id) {
172
+ let found = null;
173
+ const cursor = view.state.field(anchorField).iter();
174
+ while (cursor.value && !found) {
175
+ const spec = cursor.value.spec;
176
+ if ((spec.attributes?.["data-comment"] ?? spec.widget?.id) === id) found = {
177
+ from: cursor.from,
178
+ to: cursor.to
179
+ };
180
+ cursor.next();
181
+ }
182
+ return found;
183
+ }
@@ -0,0 +1,202 @@
1
+ export declare const COMMENT_CONTRACT: {
2
+ readonly id: "mdbase.comment";
3
+ readonly version: "1.0.0";
4
+ };
5
+ interface CommentQuote {
6
+ readonly exact: string;
7
+ readonly prefix?: string;
8
+ readonly suffix?: string;
9
+ }
10
+ interface CommentTextPosition {
11
+ readonly basis: {
12
+ readonly profile: "markdown-body";
13
+ readonly hash: string;
14
+ };
15
+ readonly unit: "unicode_code_point";
16
+ readonly start: number;
17
+ readonly end: number;
18
+ }
19
+ export interface CommentTarget {
20
+ readonly quote: CommentQuote;
21
+ readonly text_position?: CommentTextPosition;
22
+ }
23
+ type CommentMotivation = "commenting" | "replying" | "editing";
24
+ /** One comment record, through the contract's field names. */
25
+ export interface CommentRecord {
26
+ readonly path: string;
27
+ /** The link to the commented record, as stored (`[[chapters/method]]`). */
28
+ readonly document: string;
29
+ readonly inReplyTo?: string;
30
+ readonly motivation: CommentMotivation;
31
+ readonly target?: CommentTarget;
32
+ readonly suggestion?: {
33
+ readonly replacement: string;
34
+ readonly outcome?: "accepted" | "rejected";
35
+ };
36
+ readonly status: "open" | "resolved";
37
+ readonly resolvedBy?: string;
38
+ readonly resolvedAt?: string;
39
+ readonly createdBy?: string;
40
+ readonly createdAt: string;
41
+ readonly deletedAt?: string;
42
+ /** The comment's text (its Markdown body). */
43
+ readonly text: string;
44
+ }
45
+ export interface CommentThread {
46
+ readonly root: CommentRecord;
47
+ /** Replies, oldest first. */
48
+ readonly replies: readonly CommentRecord[];
49
+ }
50
+ /** The contract's fields as JSON, under the contract's names. */
51
+ export type CommentFields = Record<string, unknown>;
52
+ /**
53
+ * A comment from a record's contract projection (frontmatter under the
54
+ * contract's field names) and body; null when it is not a usable comment.
55
+ */
56
+ export declare function commentFromRecord(path: string, fields: Readonly<CommentFields> | undefined, body: string): CommentRecord | null;
57
+ /** The path part of a link (`[[chapters/method|Method]]` → `chapters/method`), or the value as it is. */
58
+ export declare function linkPath(link: string): string;
59
+ /** The link a comment stores for a record path: `[[chapters/method]]`. */
60
+ export declare const recordLink: (path: string) => string;
61
+ /**
62
+ * The record paths of one collection generation, for following the links
63
+ * comments store. A link names an exact collection path, a path relative to
64
+ * the linking record, or a file name that is unique in the collection.
65
+ */
66
+ export declare class RecordPaths {
67
+ private readonly paths;
68
+ private readonly basenames;
69
+ constructor(paths: Iterable<string>);
70
+ /** The record a link written in `from` leads to, or null. */
71
+ resolve(link: string, from: string): string | null;
72
+ }
73
+ /**
74
+ * Comments grouped into threads, oldest thread first. A reply whose first
75
+ * comment is missing (deleted by hand, not yet synced) becomes a thread of its own.
76
+ */
77
+ export declare function commentThreads(comments: readonly CommentRecord[]): CommentThread[];
78
+ /** Code points in `text` before UTF-16 offset `offset`. */
79
+ export declare function codePointOffset(text: string, offset: number): number;
80
+ /** The UTF-16 offset of code point `points` in `text` (the end when past it). */
81
+ export declare function utf16Offset(text: string, points: number): number;
82
+ /** `sha256:` and the hex SHA-256 of the body's UTF-8 bytes (the text_position basis). */
83
+ export declare function bodyHash(body: string): Promise<string>;
84
+ /** The target for the text between UTF-16 offsets `from` and `to` of `body`. */
85
+ export declare function targetFor(body: string, from: number, to: number, hash: string): CommentTarget;
86
+ export interface Located {
87
+ readonly from: number;
88
+ readonly to: number;
89
+ }
90
+ /**
91
+ * Where a target's quote is in `body` (UTF-16 offsets), or null when it is
92
+ * detached. The recorded position is used when the quote is still there;
93
+ * otherwise the occurrence whose surroundings best match the recorded prefix
94
+ * and suffix wins, then the one nearest the recorded position.
95
+ */
96
+ export declare function locate(body: string, target: CommentTarget): Located | null;
97
+ /**
98
+ * A target as an editor that keeps its text with `\n` line breaks sees it.
99
+ * The recorded position counted the stored line breaks, so it is dropped when
100
+ * they differ and the quote alone finds the passage.
101
+ */
102
+ export declare function editorTarget(target: CommentTarget): CommentTarget;
103
+ /** The body with a suggestion applied, or null when its quote cannot be found exactly. */
104
+ export declare function applySuggestion(body: string, target: CommentTarget, replacement: string): string | null;
105
+ export interface PlacedThread {
106
+ readonly thread: CommentThread;
107
+ /** Where its passage is now: "whole" for a comment on the whole record, null when detached. */
108
+ readonly at: Located | "whole" | null;
109
+ }
110
+ /** The threads whose first comment is on `record`. */
111
+ export declare function recordThreads(record: string, comments: readonly CommentRecord[], paths: RecordPaths): CommentThread[];
112
+ /**
113
+ * Threads where they are in `body` now, in reading order: whole-record
114
+ * threads, then by position, then detached threads.
115
+ */
116
+ export declare function placeThreads(threads: readonly CommentThread[], body: string): PlacedThread[];
117
+ interface NewComment {
118
+ /** Path of the commented record. */
119
+ readonly document: string;
120
+ readonly target?: CommentTarget;
121
+ /** The thread a reply belongs to. */
122
+ readonly thread?: CommentRecord;
123
+ /** A suggested edit: the text to put in place of the target's quote. */
124
+ readonly replacement?: string;
125
+ }
126
+ export type CommentChange = {
127
+ readonly kind: "resolve";
128
+ readonly outcome?: "accepted" | "rejected";
129
+ } | {
130
+ readonly kind: "reopen";
131
+ } | {
132
+ readonly kind: "withdraw";
133
+ };
134
+ /** A new comment's path: `comments/` and a sortable, unique name. */
135
+ export declare function commentPath(now: Date): string;
136
+ /** The contract fields of a new comment; `author` is a link to a person record. */
137
+ export declare function newCommentFields(input: NewComment, now: Date, author: string | undefined): CommentFields;
138
+ /** The contract fields a change writes, and the body it leaves (undefined keeps it). */
139
+ export declare function changeFields(comment: CommentRecord, change: CommentChange, now: Date, author: string | undefined): {
140
+ fields: CommentFields;
141
+ body?: string;
142
+ };
143
+ /**
144
+ * Whether new comments are signed, and if not, why: the account has no person
145
+ * record here, several records (or an invalid one) claim it, the app was not
146
+ * allowed to see the account, or the account could not be checked.
147
+ */
148
+ type CommentSigning = {
149
+ readonly kind: "linked";
150
+ } | {
151
+ readonly kind: "unlinked";
152
+ } | {
153
+ readonly kind: "conflict";
154
+ readonly paths: readonly string[];
155
+ } | {
156
+ readonly kind: "not-approved";
157
+ } | {
158
+ readonly kind: "unavailable";
159
+ readonly reason: string;
160
+ };
161
+ /** Person records by link key (see `personKey`), and the signed-in account's own. */
162
+ export interface CommentPeople {
163
+ readonly names: ReadonlyMap<string, string>;
164
+ /** The link to the signed-in account's person record, when it has exactly one. */
165
+ readonly me?: {
166
+ readonly link: string;
167
+ readonly name: string;
168
+ };
169
+ /** Unknown until the account has been checked. */
170
+ readonly signing?: CommentSigning;
171
+ }
172
+ export declare const NO_COMMENT_PEOPLE: CommentPeople;
173
+ /** A record path or link, compared as links resolve: without `.md`, ignoring case. */
174
+ export declare const personKey: (pathOrLink: string) => string;
175
+ /** The display name for an author link: its person record's name, else the link's own text. */
176
+ export declare function personName(link: string | undefined, people: CommentPeople): string | undefined;
177
+ /** Whether `comment` was written by the signed-in account's person. */
178
+ export declare function writtenByMe(comment: CommentRecord, people: CommentPeople): boolean;
179
+ /**
180
+ * A passage of Markdown as it reads: emphasis, code and link markup dropped,
181
+ * soft line breaks as spaces ("*modal picture*" → "modal picture").
182
+ */
183
+ export declare function plainPassage(passage: string): string;
184
+ /** A passage on one line, shortened to about 60 characters. */
185
+ export declare function clipPassage(passage: string): string;
186
+ /** A run of comment text as it reads: plain, emphasised, code, or a link. */
187
+ type InlinePiece = {
188
+ readonly kind: "text" | "em" | "strong" | "code";
189
+ readonly text: string;
190
+ } | {
191
+ readonly kind: "link";
192
+ readonly text: string;
193
+ readonly href: string;
194
+ };
195
+ /**
196
+ * The light Markdown a comment uses (emphasis, code, links), as pieces to
197
+ * render; anything else stays as written. Only http(s) links are links.
198
+ */
199
+ export declare function inlinePieces(text: string): InlinePiece[];
200
+ /** "5 minutes ago", "yesterday", or a date for anything older than a week, in `locale`. */
201
+ export declare function when(iso: string, locale: string | undefined, now?: number): string;
202
+ export {};