@synap-core/react 1.0.0 → 1.2.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.
Files changed (45) hide show
  1. package/README.md +144 -31
  2. package/api/editor.d.ts +178 -0
  3. package/api/whiteboard.d.ts +104 -0
  4. package/dist/editor.cjs +38733 -0
  5. package/dist/editor.css +9953 -0
  6. package/dist/editor.d.cts +178 -0
  7. package/dist/editor.d.ts +178 -0
  8. package/dist/editor.js +38974 -0
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +26 -4
  11. package/dist/index.d.ts +26 -4
  12. package/dist/index.js.map +1 -1
  13. package/dist/whiteboard.cjs +4540 -0
  14. package/dist/whiteboard.css +7913 -0
  15. package/dist/whiteboard.d.cts +104 -0
  16. package/dist/whiteboard.d.ts +104 -0
  17. package/dist/whiteboard.js +4568 -0
  18. package/package.json +198 -14
  19. package/src/SynapProvider.tsx +1 -1
  20. package/src/editor/SynapEditor.tsx +288 -0
  21. package/src/editor/SynapMarkdown.tsx +11 -0
  22. package/src/editor/adapter-source.ts +97 -0
  23. package/src/editor/api.ts +30 -0
  24. package/src/editor/copy.ts +18 -0
  25. package/src/editor/drag.ts +14 -0
  26. package/src/editor/editor-base.css +28 -0
  27. package/src/editor/http-adapter.ts +87 -0
  28. package/src/editor/index.ts +22 -0
  29. package/src/editor/no-default-source.ts +7 -0
  30. package/src/editor/provider.tsx +79 -0
  31. package/src/editor/types.ts +162 -0
  32. package/src/index.ts +3 -3
  33. package/src/shared/read-cache.ts +76 -0
  34. package/src/trpc.ts +3 -2
  35. package/src/whiteboard/SynapBoard.tsx +139 -0
  36. package/src/whiteboard/adapter-source.ts +138 -0
  37. package/src/whiteboard/api.ts +22 -0
  38. package/src/whiteboard/copy.ts +24 -0
  39. package/src/whiteboard/http-adapter.ts +57 -0
  40. package/src/whiteboard/index.ts +17 -0
  41. package/src/whiteboard/no-default-source.ts +7 -0
  42. package/src/whiteboard/provider.tsx +14 -0
  43. package/src/whiteboard/shapes.tsx +157 -0
  44. package/src/whiteboard/types.ts +99 -0
  45. package/src/whiteboard/whiteboard-base.css +87 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@synap-core/react",
3
- "version": "1.0.0",
4
- "description": "React hooks and provider for Synap data pods",
3
+ "version": "1.2.0",
4
+ "description": "React hooks and provider for Synap data pods, and the pod's markdown editor (@synap-core/react/editor) and tldraw board (@synap-core/react/whiteboard) for pod apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
@@ -11,47 +11,231 @@
11
11
  "types": "./dist/index.d.ts",
12
12
  "import": "./dist/index.js",
13
13
  "require": "./dist/index.cjs"
14
- }
14
+ },
15
+ "./editor": {
16
+ "types": {
17
+ "import": "./dist/editor.d.ts",
18
+ "require": "./dist/editor.d.cts"
19
+ },
20
+ "import": "./dist/editor.js",
21
+ "require": "./dist/editor.cjs"
22
+ },
23
+ "./editor.css": "./dist/editor.css",
24
+ "./whiteboard": {
25
+ "types": {
26
+ "import": "./dist/whiteboard.d.ts",
27
+ "require": "./dist/whiteboard.d.cts"
28
+ },
29
+ "import": "./dist/whiteboard.js",
30
+ "require": "./dist/whiteboard.cjs"
31
+ },
32
+ "./whiteboard.css": "./dist/whiteboard.css"
15
33
  },
16
34
  "files": [
17
35
  "dist",
18
- "src"
36
+ "!dist/metafile-*.json",
37
+ "src",
38
+ "!src/__tests__",
39
+ "api"
19
40
  ],
20
41
  "keywords": [
21
42
  "synap",
22
43
  "react",
23
44
  "trpc",
24
45
  "data-pod",
25
- "hooks"
46
+ "hooks",
47
+ "whiteboard"
26
48
  ],
27
49
  "dependencies": {
28
- "@synap-core/api-types": "^1.23.3",
50
+ "@synap-core/api-types": "^1.25.7",
29
51
  "@tanstack/react-query": "^5.62.10",
30
- "@trpc/client": "^11.10.0",
31
52
  "@trpc/react-query": "^11.10.0",
32
53
  "@trpc/server": "^11.10.0",
33
- "superjson": "^2.2.6",
34
- "@synap-core/sdk": "1.0.0"
54
+ "@synap-core/sdk": "1.32.0"
35
55
  },
36
56
  "peerDependencies": {
37
- "react": "^18.0.0 || ^19.0.0"
57
+ "react": "^18.0.0 || ^19.0.0",
58
+ "@floating-ui/react": "^0.27.16",
59
+ "@synap-core/markdown-core": "^0.1.0",
60
+ "@tiptap/core": "^3.17.0",
61
+ "@tiptap/extension-code-block-lowlight": "^3.17.0",
62
+ "@tiptap/extension-collaboration": "^3.17.0",
63
+ "@tiptap/extension-collaboration-caret": "^3.17.0",
64
+ "@tiptap/extension-color": "^3.17.0",
65
+ "@tiptap/extension-drag-handle": "^3.17.0",
66
+ "@tiptap/extension-drag-handle-react": "^3.17.0",
67
+ "@tiptap/extension-highlight": "^3.17.0",
68
+ "@tiptap/extension-image": "^3.17.0",
69
+ "@tiptap/extension-node-range": "^3.17.0",
70
+ "@tiptap/extension-placeholder": "^3.17.0",
71
+ "@tiptap/extension-table": "^3.17.0",
72
+ "@tiptap/extension-task-item": "^3.17.0",
73
+ "@tiptap/extension-task-list": "^3.17.0",
74
+ "@tiptap/extension-text-style": "^3.17.0",
75
+ "@tiptap/extension-underline": "^3.17.0",
76
+ "@tiptap/markdown": "^3.17.0",
77
+ "@tiptap/pm": "^3.17.0",
78
+ "@tiptap/react": "^3.17.0",
79
+ "@tiptap/starter-kit": "^3.17.0",
80
+ "@tiptap/suggestion": "^3.17.0",
81
+ "@tiptap/y-tiptap": "^3.0.0",
82
+ "@tldraw/tldraw": "^2.4.6",
83
+ "katex": "^0.16.47",
84
+ "lowlight": "^3.3.0",
85
+ "lucide-react": "^0.562.0",
86
+ "mermaid": "^11.15.0",
87
+ "react-aria-components": "^1.17.0",
88
+ "react-dom": "^18.0.0 || ^19.0.0",
89
+ "react-markdown": "^10.1.0",
90
+ "turndown": "^7.2.4",
91
+ "y-indexeddb": "^9.0.12",
92
+ "y-socket.io": "^1.1.3",
93
+ "yjs": "^13.6.30",
94
+ "zod": "^3.25.0 || ^4.0.0"
38
95
  },
39
96
  "devDependencies": {
97
+ "@synap-core/markdown-core": "^0.1.0",
40
98
  "@types/react": "^19.0.0",
41
99
  "tsup": "^8.3.5",
42
- "typescript": "^5.7.3"
100
+ "typescript": "^5.7.3",
101
+ "@synap-core/design-tokens": "0.1.2",
102
+ "@synap-core/headless": "0.1.0",
103
+ "@synap-core/hooks": "0.1.0",
104
+ "@synap-core/types": "1.12.14",
105
+ "@synap-core/web": "0.1.0"
43
106
  },
44
107
  "publishConfig": {
45
108
  "access": "public"
46
109
  },
47
110
  "repository": {
48
111
  "type": "git",
49
- "url": "https://github.com/synap/synap-app.git",
112
+ "url": "git+https://github.com/Synap-core/app-bases.git",
50
113
  "directory": "packages/react"
51
114
  },
52
115
  "license": "MIT",
116
+ "sideEffects": [
117
+ "./dist/editor.css",
118
+ "./dist/whiteboard.css",
119
+ "**/*.css"
120
+ ],
121
+ "peerDependenciesMeta": {
122
+ "@floating-ui/react": {
123
+ "optional": true
124
+ },
125
+ "@synap-core/markdown-core": {
126
+ "optional": true
127
+ },
128
+ "@tiptap/core": {
129
+ "optional": true
130
+ },
131
+ "@tiptap/extension-code-block-lowlight": {
132
+ "optional": true
133
+ },
134
+ "@tiptap/extension-collaboration": {
135
+ "optional": true
136
+ },
137
+ "@tiptap/extension-collaboration-caret": {
138
+ "optional": true
139
+ },
140
+ "@tiptap/extension-color": {
141
+ "optional": true
142
+ },
143
+ "@tiptap/extension-drag-handle": {
144
+ "optional": true
145
+ },
146
+ "@tiptap/extension-drag-handle-react": {
147
+ "optional": true
148
+ },
149
+ "@tiptap/extension-highlight": {
150
+ "optional": true
151
+ },
152
+ "@tiptap/extension-image": {
153
+ "optional": true
154
+ },
155
+ "@tiptap/extension-node-range": {
156
+ "optional": true
157
+ },
158
+ "@tiptap/extension-placeholder": {
159
+ "optional": true
160
+ },
161
+ "@tiptap/extension-table": {
162
+ "optional": true
163
+ },
164
+ "@tiptap/extension-task-item": {
165
+ "optional": true
166
+ },
167
+ "@tiptap/extension-task-list": {
168
+ "optional": true
169
+ },
170
+ "@tiptap/extension-text-style": {
171
+ "optional": true
172
+ },
173
+ "@tiptap/extension-underline": {
174
+ "optional": true
175
+ },
176
+ "@tiptap/markdown": {
177
+ "optional": true
178
+ },
179
+ "@tiptap/pm": {
180
+ "optional": true
181
+ },
182
+ "@tiptap/react": {
183
+ "optional": true
184
+ },
185
+ "@tiptap/starter-kit": {
186
+ "optional": true
187
+ },
188
+ "@tiptap/suggestion": {
189
+ "optional": true
190
+ },
191
+ "@tiptap/y-tiptap": {
192
+ "optional": true
193
+ },
194
+ "@tldraw/tldraw": {
195
+ "optional": true
196
+ },
197
+ "katex": {
198
+ "optional": true
199
+ },
200
+ "lowlight": {
201
+ "optional": true
202
+ },
203
+ "lucide-react": {
204
+ "optional": true
205
+ },
206
+ "mermaid": {
207
+ "optional": true
208
+ },
209
+ "react-aria-components": {
210
+ "optional": true
211
+ },
212
+ "react-dom": {
213
+ "optional": true
214
+ },
215
+ "react-markdown": {
216
+ "optional": true
217
+ },
218
+ "turndown": {
219
+ "optional": true
220
+ },
221
+ "y-indexeddb": {
222
+ "optional": true
223
+ },
224
+ "y-socket.io": {
225
+ "optional": true
226
+ },
227
+ "yjs": {
228
+ "optional": true
229
+ },
230
+ "zod": {
231
+ "optional": true
232
+ }
233
+ },
53
234
  "scripts": {
54
- "build": "tsup",
55
- "typecheck": "tsc --noEmit"
235
+ "build": "rm -rf dist && tsup",
236
+ "typecheck": "tsc --noEmit",
237
+ "check:editor-api": "node scripts/editor-api.mjs",
238
+ "smoke:editor": "node scripts/editor-smoke.mjs",
239
+ "smoke:whiteboard": "node scripts/whiteboard-smoke.mjs"
56
240
  }
57
241
  }
@@ -10,7 +10,7 @@
10
10
  * return (
11
11
  * <SynapProvider
12
12
  * podUrl="https://pod.example.com"
13
- * apiKey="sk_live_..."
13
+ * sessionToken="..."
14
14
  * workspaceId="ws_..."
15
15
  * >
16
16
  * <YourApp />
@@ -0,0 +1,288 @@
1
+ /**
2
+ * `<SynapEditor documentId>`: the pod's markdown editor on one document, for a
3
+ * pod app. Loads it through the provider's adapter, saves after a pause with
4
+ * the revision it read (`baseRevision`), and says what the pod did with each
5
+ * save: applied, waiting for the person's review, refused, or changed
6
+ * elsewhere. A dropped pod object becomes a chip AND a relation from
7
+ * `subjectId` (`linkObjects`), whose answer is said the same way.
8
+ *
9
+ * A save the pod refuses as `conflict` (the document changed elsewhere) stops
10
+ * the autosave and keeps the person's words: "Reload" takes theirs, "Keep mine"
11
+ * reads the new revision and saves the words over it. `onReady` hands the host
12
+ * edits it makes from beside the editor (dictation, a quoted idea), saved like
13
+ * typing.
14
+ *
15
+ * Collaboration (a live Yjs room) is not wired: slice S3 adds the room ticket.
16
+ */
17
+ import React, { useCallback, useEffect, useRef, useState } from "react";
18
+ import {
19
+ AdvancedEditor,
20
+ SpatialButton,
21
+ SpatialLink,
22
+ type EditorDroppedObject,
23
+ type EditorMarkdownHandle,
24
+ } from "@synap-core/web/markdown-engine/embeddable";
25
+ import { DocumentDataScope, useAppEditorData } from "./provider";
26
+ import {
27
+ EditorReadError,
28
+ type EditorDocument,
29
+ type EditorWriteResult,
30
+ type SynapEditorHandle,
31
+ type SynapEditorProps,
32
+ } from "./types";
33
+ import { EDITOR_COPY } from "./copy";
34
+
35
+ type Load =
36
+ | { state: "loading" }
37
+ | { state: "failed"; error: unknown }
38
+ | { state: "ready"; doc: EditorDocument };
39
+
40
+ type SaveState =
41
+ | { state: "idle" }
42
+ | { state: "saving" }
43
+ | { state: "saved" }
44
+ | { state: "proposed"; reviewUrl: string }
45
+ | { state: "failed" }
46
+ | { state: "conflict" };
47
+
48
+ type LinkState =
49
+ | { state: "idle" }
50
+ | { state: "linked" }
51
+ | { state: "proposed"; reviewUrl: string }
52
+ | { state: "failed" };
53
+
54
+ /** What a write answered, as the status line's state. */
55
+ export function saveStateOf(result: EditorWriteResult): SaveState {
56
+ if (result.ok) return result.status === "applied" ? { state: "saved" } : { state: "proposed", reviewUrl: result.reviewUrl };
57
+ return result.error.code === "conflict" ? { state: "conflict" } : { state: "failed" };
58
+ }
59
+
60
+ export function SynapEditor({
61
+ documentId,
62
+ subjectId,
63
+ relationType = "references",
64
+ workspaceId,
65
+ autoSaveMs = 1500,
66
+ editable = true,
67
+ onObjectClick,
68
+ onReady,
69
+ className,
70
+ }: SynapEditorProps) {
71
+ const { adapter } = useAppEditorData();
72
+ const [load, setLoad] = useState<Load>({ state: "loading" });
73
+ const [save, setSave] = useState<SaveState>({ state: "idle" });
74
+ const [link, setLink] = useState<LinkState>({ state: "idle" });
75
+ const [attempt, setAttempt] = useState(0);
76
+
77
+ /** The revision the next save is based on, and the markdown last sent. */
78
+ const revision = useRef(0);
79
+ const lastSent = useRef<string | null>(null);
80
+ const pending = useRef<string | null>(null);
81
+ const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
82
+ const halted = useRef(false);
83
+
84
+ useEffect(() => {
85
+ let live = true;
86
+ setLoad({ state: "loading" });
87
+ adapter.getDocument(documentId).then(
88
+ (doc) => {
89
+ if (!live) return;
90
+ revision.current = doc.revision;
91
+ lastSent.current = doc.content;
92
+ halted.current = false;
93
+ setSave({ state: "idle" });
94
+ setLoad({ state: "ready", doc });
95
+ },
96
+ (error: unknown) => live && setLoad({ state: "failed", error }),
97
+ );
98
+ return () => {
99
+ live = false;
100
+ };
101
+ }, [adapter, documentId, attempt]);
102
+
103
+ const flush = useCallback(async () => {
104
+ timer.current = null;
105
+ const markdown = pending.current;
106
+ if (markdown === null || markdown === lastSent.current || halted.current) return;
107
+ pending.current = null;
108
+ setSave({ state: "saving" });
109
+ const result: EditorWriteResult = await adapter
110
+ .saveDocument(documentId, markdown, revision.current)
111
+ .catch((err: unknown) => ({
112
+ ok: false as const,
113
+ error: { code: "unreachable", status: null, detail: err instanceof Error ? err.message : String(err) },
114
+ }));
115
+ const next = saveStateOf(result);
116
+ if (result.ok) lastSent.current = markdown;
117
+ // Refused: keep the words, so "Try again" (or the next keystroke) sends them.
118
+ else pending.current ??= markdown;
119
+ if (next.state === "conflict") halted.current = true;
120
+ if (result.ok && result.status === "applied") {
121
+ // The pod moved the revision: read it back before the next save.
122
+ try {
123
+ revision.current = (await adapter.getDocument(documentId)).revision;
124
+ } catch {
125
+ // The save landed; only the read-back failed. The next save then
126
+ // carries the old revision and the pod answers `conflict`, said as such.
127
+ }
128
+ }
129
+ setSave(next);
130
+ if (result.ok && pending.current !== null && !halted.current) timer.current = setTimeout(flush, autoSaveMs);
131
+ }, [adapter, documentId, autoSaveMs]);
132
+
133
+ useEffect(
134
+ () => () => {
135
+ if (timer.current) clearTimeout(timer.current);
136
+ },
137
+ [],
138
+ );
139
+
140
+ const onUpdate = useCallback(
141
+ ({ markdown }: { markdown: string }) => {
142
+ pending.current = markdown;
143
+ if (timer.current) clearTimeout(timer.current);
144
+ timer.current = setTimeout(flush, autoSaveMs);
145
+ },
146
+ [flush, autoSaveMs],
147
+ );
148
+
149
+ /** "Keep mine": save the person's words over the revision that changed elsewhere. */
150
+ const keepMine = useCallback(async () => {
151
+ try {
152
+ revision.current = (await adapter.getDocument(documentId)).revision;
153
+ } catch {
154
+ setSave({ state: "failed" });
155
+ return;
156
+ }
157
+ halted.current = false;
158
+ await flush();
159
+ }, [adapter, documentId, flush]);
160
+
161
+ const onReadyRef = useRef(onReady);
162
+ onReadyRef.current = onReady;
163
+ const onEditorReady = useCallback((handle: EditorMarkdownHandle | null) => {
164
+ const host: SynapEditorHandle | null = handle
165
+ ? {
166
+ getMarkdown: () => handle.getMarkdown(),
167
+ insertText: (text) => handle.insertText(text),
168
+ appendMarkdown: (markdown) => handle.appendMarkdown(markdown),
169
+ }
170
+ : null;
171
+ onReadyRef.current?.(host);
172
+ }, []);
173
+
174
+ const onObjectDrop = useCallback(
175
+ (object: EditorDroppedObject) => {
176
+ if (!subjectId || object.id === subjectId) return;
177
+ adapter.linkObjects({ sourceId: subjectId, targetId: object.id, type: relationType }).then(
178
+ (result) =>
179
+ setLink(
180
+ result.ok
181
+ ? result.status === "applied"
182
+ ? { state: "linked" }
183
+ : { state: "proposed", reviewUrl: result.reviewUrl }
184
+ : { state: "failed" },
185
+ ),
186
+ () => setLink({ state: "failed" }),
187
+ );
188
+ },
189
+ [adapter, subjectId, relationType],
190
+ );
191
+
192
+ if (load.state === "loading") {
193
+ return <div className={cx("synap-editor synap-editor--loading", className)} aria-busy="true">{EDITOR_COPY.loading}</div>;
194
+ }
195
+ if (load.state === "failed") {
196
+ const status = load.error instanceof EditorReadError ? load.error.error.status : null;
197
+ return (
198
+ <div className={cx("synap-editor synap-editor--failed", className)} role="alert" data-status={status ?? undefined}>
199
+ <p>{EDITOR_COPY.loadFailed}</p>
200
+ <SpatialButton size="sm" onClick={() => setAttempt((n) => n + 1)}>
201
+ {EDITOR_COPY.retry}
202
+ </SpatialButton>
203
+ </div>
204
+ );
205
+ }
206
+
207
+ return (
208
+ <div className={cx("synap-editor", className)}>
209
+ <DocumentDataScope
210
+ workspaceId={workspaceId ?? load.doc.workspaceId}
211
+ onObjectDrop={editable ? onObjectDrop : undefined}
212
+ >
213
+ <AdvancedEditor
214
+ content={load.doc.content}
215
+ editable={editable && save.state !== "conflict"}
216
+ onUpdate={onUpdate}
217
+ onObjectClick={onObjectClick}
218
+ onEditorReady={onEditorReady}
219
+ workspaceId={workspaceId ?? load.doc.workspaceId ?? undefined}
220
+ />
221
+ </DocumentDataScope>
222
+ <EditorStatus
223
+ save={save}
224
+ link={link}
225
+ onRetry={() => void flush()}
226
+ onReload={() => setAttempt((n) => n + 1)}
227
+ onKeepMine={() => void keepMine()}
228
+ />
229
+ </div>
230
+ );
231
+ }
232
+
233
+ function EditorStatus({
234
+ save,
235
+ link,
236
+ onRetry,
237
+ onReload,
238
+ onKeepMine,
239
+ }: {
240
+ save: SaveState;
241
+ link: LinkState;
242
+ onRetry: () => void;
243
+ onReload: () => void;
244
+ onKeepMine: () => void;
245
+ }) {
246
+ const saveLine =
247
+ save.state === "saving" ? (
248
+ <span>{EDITOR_COPY.saving}</span>
249
+ ) : save.state === "saved" ? (
250
+ <span data-tone="success">{EDITOR_COPY.saved}</span>
251
+ ) : save.state === "proposed" ? (
252
+ <span data-tone="warning">
253
+ {EDITOR_COPY.proposed} · <SpatialLink href={save.reviewUrl} external>{EDITOR_COPY.openReview}</SpatialLink>
254
+ </span>
255
+ ) : save.state === "failed" ? (
256
+ <span data-tone="danger" role="alert">
257
+ {EDITOR_COPY.saveFailed}{" "}
258
+ <SpatialButton size="xs" variant="quiet" onClick={onRetry}>{EDITOR_COPY.retry}</SpatialButton>
259
+ </span>
260
+ ) : save.state === "conflict" ? (
261
+ <span data-tone="danger" role="alert">
262
+ {EDITOR_COPY.conflict}{" "}
263
+ <SpatialButton size="xs" variant="quiet" onClick={onReload}>{EDITOR_COPY.reload}</SpatialButton>{" "}
264
+ <SpatialButton size="xs" variant="quiet" onClick={onKeepMine}>{EDITOR_COPY.keepMine}</SpatialButton>
265
+ </span>
266
+ ) : null;
267
+ const linkLine =
268
+ link.state === "linked" ? (
269
+ <span data-tone="success">{EDITOR_COPY.linked}</span>
270
+ ) : link.state === "proposed" ? (
271
+ <span data-tone="warning">
272
+ {EDITOR_COPY.linkProposed} · <SpatialLink href={link.reviewUrl} external>{EDITOR_COPY.openReview}</SpatialLink>
273
+ </span>
274
+ ) : link.state === "failed" ? (
275
+ <span data-tone="danger" role="alert">{EDITOR_COPY.linkFailed}</span>
276
+ ) : null;
277
+ if (!saveLine && !linkLine) return null;
278
+ return (
279
+ <div className="synap-editor__status" aria-live="polite">
280
+ {saveLine}
281
+ {linkLine}
282
+ </div>
283
+ );
284
+ }
285
+
286
+ function cx(...names: Array<string | undefined>): string {
287
+ return names.filter(Boolean).join(" ");
288
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * `<SynapMarkdown content>`: the pod's markdown reader (GFM, reference chips,
3
+ * `:::synap-*` embeds, sections, columns), reading through the provider above.
4
+ */
5
+ import React from "react";
6
+ import { MarkdownRenderer } from "@synap-core/web/markdown-engine/embeddable";
7
+ import type { SynapMarkdownProps } from "./types";
8
+
9
+ export function SynapMarkdown({ content, onObjectClick, className }: SynapMarkdownProps) {
10
+ return <MarkdownRenderer content={content} onObjectClick={onObjectClick} className={className} />;
11
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * An app's async `EditorDataAdapter` as the editor's data source (the hooks
3
+ * the bundled editor reads through, `@synap-core/web/markdown-engine/embeddable`).
4
+ *
5
+ * One small read cache per provider: the same chip id is asked once, a failed
6
+ * read stays failed (with a retry) and never turns into an empty answer.
7
+ */
8
+ import { useCallback } from "react";
9
+ import { referenceRead } from "@synap-core/markdown-core/object-ids";
10
+ import type {
11
+ EditorDataSource,
12
+ EditorEntityRow,
13
+ EditorList,
14
+ EditorObjectRef,
15
+ EditorViewRow,
16
+ } from "@synap-core/web/markdown-engine/embeddable";
17
+ import type { EditorDataAdapter } from "./types";
18
+ import { createReadCache, useRead, type Entry, type ReadCache } from "../shared/read-cache";
19
+
20
+ export { createReadCache, type ReadCache };
21
+
22
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
23
+
24
+ function listOf<T>(entry: Entry, refetch: () => void): EditorList<T> {
25
+ return {
26
+ items: entry.status === "ready" && Array.isArray(entry.data) ? (entry.data as T[]) : [],
27
+ isLoading: entry.status === "loading",
28
+ error: entry.status === "failed" ? entry.error : null,
29
+ refetch,
30
+ };
31
+ }
32
+
33
+ export interface AdapterSourceOptions {
34
+ /** The space whose views the `/` menu offers (the document's own). */
35
+ workspaceId?: string | null;
36
+ onObjectDrop?: EditorDataSource["onObjectDrop"];
37
+ embeds?: EditorDataSource["embeds"];
38
+ }
39
+
40
+ export function adapterSource(
41
+ adapter: EditorDataAdapter,
42
+ cache: ReadCache,
43
+ options: AdapterSourceOptions = {},
44
+ ): EditorDataSource {
45
+ function useObjectRef(id: string, options: { enabled?: boolean } = {}): EditorObjectRef {
46
+ const lookup = (options.enabled ?? true) && UUID_RE.test(id);
47
+ const load = useCallback(() => adapter.resolveRef(id), [id]);
48
+ const { entry, refetch } = useRead(cache, lookup ? `ref:${id}` : null, load);
49
+ if (!lookup) {
50
+ return {
51
+ read: options.enabled === false ? { state: "unresolved" } : { state: "unknown" },
52
+ retry: refetch,
53
+ isFetching: false,
54
+ };
55
+ }
56
+ return {
57
+ read: referenceRead({
58
+ isLoading: entry.status === "loading" && entry.data === undefined,
59
+ error: entry.status === "failed" ? entry.error : null,
60
+ data: entry.data as Parameters<typeof referenceRead>[0]["data"],
61
+ }),
62
+ retry: refetch,
63
+ isFetching: entry.status === "loading",
64
+ };
65
+ }
66
+
67
+ function useEntityList(workspaceId: string | undefined, options: { enabled: boolean }): EditorList<EditorEntityRow> {
68
+ const ws = workspaceId ?? "";
69
+ const load = useCallback(
70
+ () =>
71
+ adapter.listEntities(ws).then((rows) =>
72
+ rows.map((r) => ({ id: r.id, title: r.title ?? undefined, profileSlug: r.profileSlug, type: r.profileSlug })),
73
+ ),
74
+ [ws],
75
+ );
76
+ const { entry, refetch } = useRead(cache, options.enabled && ws ? `entities:${ws}` : null, load);
77
+ return listOf<EditorEntityRow>(entry, refetch);
78
+ }
79
+
80
+ function useViewList(workspaceId: string | undefined, options: { enabled: boolean }): EditorList<EditorViewRow> {
81
+ const ws = workspaceId ?? "";
82
+ const load = useCallback(() => adapter.listViews(ws), [ws]);
83
+ const { entry, refetch } = useRead(cache, options.enabled && ws ? `views:${ws}` : null, load);
84
+ return listOf<EditorViewRow>(entry, refetch);
85
+ }
86
+
87
+ return {
88
+ useObjectRef,
89
+ useEntityList,
90
+ useViewList,
91
+ useInsertableViews(): EditorList<EditorViewRow> {
92
+ return useViewList(options.workspaceId ?? undefined, { enabled: !!options.workspaceId });
93
+ },
94
+ ...(options.onObjectDrop ? { onObjectDrop: options.onObjectDrop } : {}),
95
+ ...(options.embeds ? { embeds: options.embeds } : {}),
96
+ };
97
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The declared API of `@synap-core/react/editor`: what `dist/editor.d.ts` is
3
+ * built from (tsup `dts.entry`). `index.ts` binds each value to its type here,
4
+ * so the implementation cannot drift from it without a type error, and the
5
+ * snapshot test (`api/editor.d.ts`) makes every change to it deliberate.
6
+ */
7
+ import type { ReactElement } from "react";
8
+ import type {
9
+ EditorDataAdapter,
10
+ EditorDataProviderProps,
11
+ HttpEditorAdapterOptions,
12
+ SynapDraggedObject,
13
+ SynapEditorProps,
14
+ SynapMarkdownProps,
15
+ } from "./types";
16
+
17
+ export * from "./types";
18
+
19
+ /** Every Synap editor and reader below it reads through `adapter`. */
20
+ export declare function EditorDataProvider(props: EditorDataProviderProps): ReactElement;
21
+ /** The pod's markdown editor on one document: load, autosave with `baseRevision`, drop-to-link. */
22
+ export declare function SynapEditor(props: SynapEditorProps): ReactElement | null;
23
+ /** The pod's markdown reader. */
24
+ export declare function SynapMarkdown(props: SynapMarkdownProps): ReactElement | null;
25
+ /** The adapter over the app's own routes (`createEditorHandlers`, `@synap-core/sdk/connect`). */
26
+ export declare function createHttpEditorAdapter(options?: HttpEditorAdapterOptions): EditorDataAdapter;
27
+ /** The drag payload type `<SynapEditor>` takes: JSON `{ id, kind, label }`. */
28
+ export declare const SYNAP_OBJECT_MIME: "text/x-synap-object";
29
+ /** Set a pod object on a drag, so `<SynapEditor>` takes the drop. */
30
+ export declare function setSynapObjectDrag(dataTransfer: DataTransfer, object: SynapDraggedObject): void;