@tangle-network/ui 1.0.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 (220) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/LICENSE +21 -0
  3. package/README.md +33 -0
  4. package/dist/active-sessions-store-CeOmXgv5.d.ts +85 -0
  5. package/dist/artifact-pane-DvJyPWV4.d.ts +24 -0
  6. package/dist/auth.d.ts +74 -0
  7. package/dist/auth.js +15 -0
  8. package/dist/button-CMQuQEW_.d.ts +17 -0
  9. package/dist/chat.d.ts +232 -0
  10. package/dist/chat.js +30 -0
  11. package/dist/chunk-2NFQRQOD.js +1009 -0
  12. package/dist/chunk-2VH6PUXD.js +186 -0
  13. package/dist/chunk-34A66VBG.js +214 -0
  14. package/dist/chunk-3OI2QKFD.js +0 -0
  15. package/dist/chunk-4CLN43XT.js +45 -0
  16. package/dist/chunk-54SQQMMM.js +156 -0
  17. package/dist/chunk-5Z5ZYMOJ.js +0 -0
  18. package/dist/chunk-66BNMOVT.js +167 -0
  19. package/dist/chunk-6BGQA4BQ.js +0 -0
  20. package/dist/chunk-7UO2ZMRQ.js +133 -0
  21. package/dist/chunk-BX6AQMUS.js +183 -0
  22. package/dist/chunk-CD53GZOM.js +59 -0
  23. package/dist/chunk-CSAIKY36.js +54 -0
  24. package/dist/chunk-EEE55AVS.js +1201 -0
  25. package/dist/chunk-GYPQXTJU.js +230 -0
  26. package/dist/chunk-HFL6R6IF.js +37 -0
  27. package/dist/chunk-HJKCSXCH.js +737 -0
  28. package/dist/chunk-LISXUB4D.js +1222 -0
  29. package/dist/chunk-LQS34IGP.js +0 -0
  30. package/dist/chunk-MKTSMWVD.js +109 -0
  31. package/dist/chunk-NKDZ7GZE.js +192 -0
  32. package/dist/chunk-OEX7NZE3.js +321 -0
  33. package/dist/chunk-Q56BYXQF.js +61 -0
  34. package/dist/chunk-Q7EIIWTC.js +0 -0
  35. package/dist/chunk-REJESC5U.js +117 -0
  36. package/dist/chunk-RQGKSCEZ.js +0 -0
  37. package/dist/chunk-RQHJBTEU.js +10 -0
  38. package/dist/chunk-TMFOPHHN.js +299 -0
  39. package/dist/chunk-XGKULLYE.js +40 -0
  40. package/dist/chunk-XIHMJ7ZQ.js +614 -0
  41. package/dist/chunk-YJ2G3XO5.js +1048 -0
  42. package/dist/chunk-YNN4O57I.js +754 -0
  43. package/dist/code-block-DjXf8eOG.d.ts +19 -0
  44. package/dist/document-editor-pane-A5LT5H4N.js +12 -0
  45. package/dist/document-editor-pane-DyDEX_Zm.d.ts +124 -0
  46. package/dist/editor.d.ts +120 -0
  47. package/dist/editor.js +34 -0
  48. package/dist/files.d.ts +175 -0
  49. package/dist/files.js +20 -0
  50. package/dist/hooks.d.ts +56 -0
  51. package/dist/hooks.js +41 -0
  52. package/dist/index.d.ts +43 -0
  53. package/dist/index.js +446 -0
  54. package/dist/markdown.d.ts +15 -0
  55. package/dist/markdown.js +14 -0
  56. package/dist/message-BHWbxBtT.d.ts +15 -0
  57. package/dist/openui.d.ts +115 -0
  58. package/dist/openui.js +12 -0
  59. package/dist/parts-dj7AcUg0.d.ts +36 -0
  60. package/dist/primitives.d.ts +332 -0
  61. package/dist/primitives.js +191 -0
  62. package/dist/run-PfLmDAox.d.ts +41 -0
  63. package/dist/run.d.ts +69 -0
  64. package/dist/run.js +36 -0
  65. package/dist/sdk-hooks.d.ts +285 -0
  66. package/dist/sdk-hooks.js +31 -0
  67. package/dist/stores.d.ts +17 -0
  68. package/dist/stores.js +76 -0
  69. package/dist/tool-call-feed-Bs3MyQMT.d.ts +68 -0
  70. package/dist/tool-display-z4JcDmMQ.d.ts +32 -0
  71. package/dist/tool-previews.d.ts +48 -0
  72. package/dist/tool-previews.js +21 -0
  73. package/dist/types.d.ts +19 -0
  74. package/dist/types.js +1 -0
  75. package/dist/utils.d.ts +45 -0
  76. package/dist/utils.js +32 -0
  77. package/package.json +193 -0
  78. package/src/auth/auth.tsx +228 -0
  79. package/src/auth/index.ts +13 -0
  80. package/src/auth/login-layout.tsx +46 -0
  81. package/src/chat/agent-timeline.stories.tsx +429 -0
  82. package/src/chat/agent-timeline.tsx +360 -0
  83. package/src/chat/chat-container.tsx +486 -0
  84. package/src/chat/chat-input.stories.tsx +142 -0
  85. package/src/chat/chat-input.tsx +389 -0
  86. package/src/chat/chat-message.stories.tsx +237 -0
  87. package/src/chat/chat-message.tsx +129 -0
  88. package/src/chat/index.ts +18 -0
  89. package/src/chat/message-list.stories.tsx +336 -0
  90. package/src/chat/message-list.tsx +79 -0
  91. package/src/chat/thinking-indicator.stories.tsx +56 -0
  92. package/src/chat/thinking-indicator.tsx +30 -0
  93. package/src/chat/user-message.stories.tsx +92 -0
  94. package/src/chat/user-message.tsx +43 -0
  95. package/src/editor/document-editor-pane.tsx +351 -0
  96. package/src/editor/editor-provider.tsx +428 -0
  97. package/src/editor/editor-toolbar.tsx +130 -0
  98. package/src/editor/index.ts +31 -0
  99. package/src/editor/markdown-conversion.ts +21 -0
  100. package/src/editor/markdown-document-editor.tsx +137 -0
  101. package/src/editor/tiptap-editor.tsx +331 -0
  102. package/src/editor/use-editor.ts +221 -0
  103. package/src/files/file-artifact-pane.tsx +183 -0
  104. package/src/files/file-preview.tsx +342 -0
  105. package/src/files/file-tabs.tsx +71 -0
  106. package/src/files/file-tree.tsx +258 -0
  107. package/src/files/index.ts +17 -0
  108. package/src/files/rich-file-tree.stories.tsx +104 -0
  109. package/src/files/rich-file-tree.test.tsx +42 -0
  110. package/src/files/rich-file-tree.tsx +232 -0
  111. package/src/hooks/index.ts +10 -0
  112. package/src/hooks/use-auth.ts +153 -0
  113. package/src/hooks/use-auto-scroll.ts +59 -0
  114. package/src/hooks/use-dropdown-menu.ts +40 -0
  115. package/src/hooks/use-live-time.test.tsx +40 -0
  116. package/src/hooks/use-live-time.ts +27 -0
  117. package/src/hooks/use-realtime-session.ts +319 -0
  118. package/src/hooks/use-run-collapse-state.ts +25 -0
  119. package/src/hooks/use-run-groups.ts +111 -0
  120. package/src/hooks/use-sdk-session.ts +575 -0
  121. package/src/hooks/use-sse-stream.ts +475 -0
  122. package/src/hooks/use-tool-call-stream.ts +96 -0
  123. package/src/index.ts +14 -0
  124. package/src/lib/utils.ts +6 -0
  125. package/src/markdown/code-block.tsx +198 -0
  126. package/src/markdown/index.ts +2 -0
  127. package/src/markdown/markdown.stories.tsx +190 -0
  128. package/src/markdown/markdown.tsx +62 -0
  129. package/src/openui/index.ts +20 -0
  130. package/src/openui/openui-artifact-renderer.tsx +542 -0
  131. package/src/primitives/artifact-pane.tsx +91 -0
  132. package/src/primitives/avatar.stories.tsx +95 -0
  133. package/src/primitives/avatar.tsx +47 -0
  134. package/src/primitives/badge.stories.tsx +57 -0
  135. package/src/primitives/badge.tsx +97 -0
  136. package/src/primitives/button.stories.tsx +48 -0
  137. package/src/primitives/button.tsx +115 -0
  138. package/src/primitives/card.stories.tsx +53 -0
  139. package/src/primitives/card.tsx +98 -0
  140. package/src/primitives/code-block.stories.tsx +115 -0
  141. package/src/primitives/code-block.tsx +22 -0
  142. package/src/primitives/design-tokens.stories.tsx +162 -0
  143. package/src/primitives/dialog.stories.tsx +176 -0
  144. package/src/primitives/dialog.tsx +137 -0
  145. package/src/primitives/drop-zone.stories.tsx +123 -0
  146. package/src/primitives/drop-zone.tsx +131 -0
  147. package/src/primitives/dropdown-menu.stories.tsx +122 -0
  148. package/src/primitives/dropdown-menu.tsx +214 -0
  149. package/src/primitives/empty-state.stories.tsx +81 -0
  150. package/src/primitives/empty-state.tsx +40 -0
  151. package/src/primitives/index.ts +118 -0
  152. package/src/primitives/input.stories.tsx +113 -0
  153. package/src/primitives/input.tsx +136 -0
  154. package/src/primitives/label.stories.tsx +84 -0
  155. package/src/primitives/label.tsx +24 -0
  156. package/src/primitives/progress.stories.tsx +93 -0
  157. package/src/primitives/progress.tsx +50 -0
  158. package/src/primitives/segmented-control.test.tsx +328 -0
  159. package/src/primitives/segmented-control.tsx +154 -0
  160. package/src/primitives/select.stories.tsx +164 -0
  161. package/src/primitives/select.tsx +158 -0
  162. package/src/primitives/sidebar-drop-zone.stories.tsx +100 -0
  163. package/src/primitives/sidebar-drop-zone.tsx +149 -0
  164. package/src/primitives/skeleton.stories.tsx +79 -0
  165. package/src/primitives/skeleton.tsx +55 -0
  166. package/src/primitives/stat-card.stories.tsx +137 -0
  167. package/src/primitives/stat-card.tsx +97 -0
  168. package/src/primitives/switch.stories.tsx +85 -0
  169. package/src/primitives/switch.tsx +28 -0
  170. package/src/primitives/table.stories.tsx +170 -0
  171. package/src/primitives/table.tsx +116 -0
  172. package/src/primitives/tabs.stories.tsx +180 -0
  173. package/src/primitives/tabs.tsx +71 -0
  174. package/src/primitives/terminal-display.stories.tsx +191 -0
  175. package/src/primitives/terminal-display.tsx +189 -0
  176. package/src/primitives/theme-toggle.stories.tsx +32 -0
  177. package/src/primitives/theme-toggle.tsx +96 -0
  178. package/src/primitives/toast.stories.tsx +155 -0
  179. package/src/primitives/toast.tsx +190 -0
  180. package/src/primitives/upload-progress.stories.tsx +120 -0
  181. package/src/primitives/upload-progress.tsx +110 -0
  182. package/src/run/expanded-tool-detail.stories.tsx +182 -0
  183. package/src/run/expanded-tool-detail.tsx +186 -0
  184. package/src/run/index.ts +13 -0
  185. package/src/run/inline-thinking-item.stories.tsx +136 -0
  186. package/src/run/inline-thinking-item.tsx +120 -0
  187. package/src/run/inline-tool-item.stories.tsx +222 -0
  188. package/src/run/inline-tool-item.tsx +190 -0
  189. package/src/run/run-group.stories.tsx +322 -0
  190. package/src/run/run-group.tsx +569 -0
  191. package/src/run/run-item-primitives.tsx +17 -0
  192. package/src/run/tool-call-feed.stories.tsx +294 -0
  193. package/src/run/tool-call-feed.tsx +192 -0
  194. package/src/run/tool-call-step.stories.tsx +198 -0
  195. package/src/run/tool-call-step.tsx +240 -0
  196. package/src/sdk-hooks.ts +38 -0
  197. package/src/stores/active-sessions-store.ts +455 -0
  198. package/src/stores/chat-store.ts +43 -0
  199. package/src/stores/index.ts +2 -0
  200. package/src/tool-previews/command-preview.tsx +116 -0
  201. package/src/tool-previews/diff-preview.tsx +85 -0
  202. package/src/tool-previews/glob-results-preview.tsx +98 -0
  203. package/src/tool-previews/grep-results-preview.tsx +157 -0
  204. package/src/tool-previews/index.ts +22 -0
  205. package/src/tool-previews/preview-primitives.tsx +84 -0
  206. package/src/tool-previews/question-preview.tsx +101 -0
  207. package/src/tool-previews/web-search-preview.tsx +117 -0
  208. package/src/tool-previews/write-file-preview.tsx +80 -0
  209. package/src/types/branding.ts +11 -0
  210. package/src/types/index.ts +5 -0
  211. package/src/types/message.ts +13 -0
  212. package/src/types/parts.ts +51 -0
  213. package/src/types/run.ts +56 -0
  214. package/src/types/tool-display.ts +41 -0
  215. package/src/utils/copy-text.ts +30 -0
  216. package/src/utils/format.test.ts +43 -0
  217. package/src/utils/format.ts +56 -0
  218. package/src/utils/index.ts +10 -0
  219. package/src/utils/time-ago.ts +9 -0
  220. package/src/utils/tool-display.ts +238 -0
@@ -0,0 +1,1201 @@
1
+ import {
2
+ Tabs,
3
+ TabsContent,
4
+ TabsList,
5
+ TabsTrigger
6
+ } from "./chunk-Q56BYXQF.js";
7
+ import {
8
+ ArtifactPane
9
+ } from "./chunk-CSAIKY36.js";
10
+ import {
11
+ Markdown
12
+ } from "./chunk-CD53GZOM.js";
13
+ import {
14
+ cn
15
+ } from "./chunk-RQHJBTEU.js";
16
+
17
+ // src/editor/document-editor-pane.tsx
18
+ import { useEffect as useEffect5, useMemo as useMemo4, useState as useState3 } from "react";
19
+ import { PencilLine, Save, Users, Wifi, WifiOff } from "lucide-react";
20
+
21
+ // src/editor/tiptap-editor.tsx
22
+ import Collaboration from "@tiptap/extension-collaboration";
23
+ import CollaborationCaret from "@tiptap/extension-collaboration-caret";
24
+ import { EditorContent, useEditor } from "@tiptap/react";
25
+ import StarterKit from "@tiptap/starter-kit";
26
+ import { useEffect as useEffect2, useMemo as useMemo2 } from "react";
27
+
28
+ // src/editor/editor-provider.tsx
29
+ import { HocuspocusProvider } from "@hocuspocus/provider";
30
+ import {
31
+ createContext,
32
+ useCallback,
33
+ useContext,
34
+ useEffect,
35
+ useMemo,
36
+ useRef,
37
+ useState
38
+ } from "react";
39
+ import * as Y from "yjs";
40
+ import { jsx } from "react/jsx-runtime";
41
+ var EditorContext = createContext(null);
42
+ function generateUserColor() {
43
+ const colors = [
44
+ "#FF6B6B",
45
+ // Red
46
+ "#4ECDC4",
47
+ // Teal
48
+ "#45B7D1",
49
+ // Blue
50
+ "#96CEB4",
51
+ // Green
52
+ "#FFEAA7",
53
+ // Yellow
54
+ "#DDA0DD",
55
+ // Plum
56
+ "#98D8C8",
57
+ // Mint
58
+ "#F7DC6F",
59
+ // Gold
60
+ "#BB8FCE",
61
+ // Purple
62
+ "#85C1E9"
63
+ // Sky
64
+ ];
65
+ return colors[Math.floor(Math.random() * colors.length)];
66
+ }
67
+ function EditorProvider({
68
+ websocketUrl,
69
+ documentName,
70
+ token,
71
+ tokenExpiresAt,
72
+ user,
73
+ autoConnect = true,
74
+ autoReconnect = true,
75
+ maxReconnectAttempts = 5,
76
+ onConnectionChange,
77
+ onSync,
78
+ onAuthError,
79
+ onRefreshToken,
80
+ children
81
+ }) {
82
+ const [connectionState, setConnectionState] = useState("disconnected");
83
+ const [collaborators, setCollaborators] = useState([]);
84
+ const [isSynced, setIsSynced] = useState(false);
85
+ const docRef = useRef(null);
86
+ const providerRef = useRef(null);
87
+ const reconnectAttemptsRef = useRef(0);
88
+ const tokenRef = useRef(token);
89
+ const tokenExpiryRef = useRef(tokenExpiresAt);
90
+ const refreshPromiseRef = useRef(null);
91
+ const refreshTimerRef = useRef(null);
92
+ tokenRef.current = token;
93
+ tokenExpiryRef.current = tokenExpiresAt;
94
+ if (!docRef.current) {
95
+ docRef.current = new Y.Doc();
96
+ }
97
+ const doc = docRef.current;
98
+ const userColor = useMemo(
99
+ () => user.color ?? generateUserColor(),
100
+ [user.color]
101
+ );
102
+ const updateConnectionState = useCallback(
103
+ (state) => {
104
+ setConnectionState(state);
105
+ onConnectionChange?.(state);
106
+ },
107
+ [onConnectionChange]
108
+ );
109
+ const updateCollaborators = useCallback(
110
+ (awareness) => {
111
+ if (!awareness) return;
112
+ const states = awareness.getStates();
113
+ const collabs = [];
114
+ states.forEach((state, clientId) => {
115
+ if (clientId === awareness.clientID) return;
116
+ if (state.user) {
117
+ collabs.push({
118
+ clientId,
119
+ user: state.user
120
+ });
121
+ }
122
+ });
123
+ setCollaborators(collabs);
124
+ },
125
+ []
126
+ );
127
+ const clearRefreshTimer = useCallback(() => {
128
+ if (refreshTimerRef.current != null) {
129
+ window.clearTimeout(refreshTimerRef.current);
130
+ refreshTimerRef.current = null;
131
+ }
132
+ }, []);
133
+ const refreshToken = useCallback(async () => {
134
+ if (!onRefreshToken) {
135
+ return null;
136
+ }
137
+ if (refreshPromiseRef.current) {
138
+ return refreshPromiseRef.current;
139
+ }
140
+ const refreshPromise = (async () => {
141
+ const next = await onRefreshToken();
142
+ const resolvedToken = typeof next === "string" ? next : next.token;
143
+ const resolvedExpiry = typeof next === "string" ? void 0 : next.expiresAt;
144
+ tokenRef.current = resolvedToken;
145
+ tokenExpiryRef.current = resolvedExpiry;
146
+ return resolvedToken;
147
+ })().catch((error) => {
148
+ onAuthError?.(
149
+ error instanceof Error ? error : new Error(String(error))
150
+ );
151
+ return null;
152
+ }).finally(() => {
153
+ refreshPromiseRef.current = null;
154
+ });
155
+ refreshPromiseRef.current = refreshPromise;
156
+ return refreshPromise;
157
+ }, [onAuthError, onRefreshToken]);
158
+ const scheduleTokenRefresh = useCallback(() => {
159
+ clearRefreshTimer();
160
+ if (!tokenExpiryRef.current || !onRefreshToken || typeof window === "undefined") {
161
+ return;
162
+ }
163
+ const refreshAtMs = tokenExpiryRef.current * 1e3 - 6e4;
164
+ const delay = refreshAtMs - Date.now();
165
+ if (delay <= 0) {
166
+ void refreshToken();
167
+ return;
168
+ }
169
+ refreshTimerRef.current = window.setTimeout(() => {
170
+ void refreshToken();
171
+ }, delay);
172
+ }, [clearRefreshTimer, onRefreshToken, refreshToken]);
173
+ const connect = useCallback(() => {
174
+ if (providerRef.current) {
175
+ providerRef.current.connect();
176
+ return;
177
+ }
178
+ updateConnectionState("connecting");
179
+ const provider = new HocuspocusProvider({
180
+ url: websocketUrl,
181
+ name: documentName,
182
+ document: doc,
183
+ token: async () => tokenRef.current,
184
+ connect: true,
185
+ onConnect: () => {
186
+ reconnectAttemptsRef.current = 0;
187
+ updateConnectionState("connected");
188
+ scheduleTokenRefresh();
189
+ },
190
+ onSynced: () => {
191
+ setIsSynced(true);
192
+ updateConnectionState("synced");
193
+ onSync?.();
194
+ },
195
+ onDisconnect: () => {
196
+ updateConnectionState("disconnected");
197
+ setIsSynced(false);
198
+ clearRefreshTimer();
199
+ if (autoReconnect && reconnectAttemptsRef.current < maxReconnectAttempts) {
200
+ reconnectAttemptsRef.current += 1;
201
+ const delay = Math.min(
202
+ 1e3 * 2 ** reconnectAttemptsRef.current,
203
+ 3e4
204
+ );
205
+ setTimeout(() => {
206
+ if (providerRef.current && !providerRef.current.isConnected) {
207
+ providerRef.current.connect();
208
+ }
209
+ }, delay);
210
+ }
211
+ },
212
+ onAuthenticationFailed: ({ reason }) => {
213
+ const error = new Error(reason ?? "Authentication failed");
214
+ updateConnectionState("disconnected");
215
+ clearRefreshTimer();
216
+ if (onRefreshToken) {
217
+ void refreshToken().then((nextToken) => {
218
+ if (nextToken && providerRef.current) {
219
+ providerRef.current.connect();
220
+ return;
221
+ }
222
+ onAuthError?.(error);
223
+ });
224
+ return;
225
+ }
226
+ onAuthError?.(error);
227
+ },
228
+ onAwarenessUpdate: () => {
229
+ updateCollaborators(provider.awareness);
230
+ }
231
+ });
232
+ provider.awareness?.setLocalStateField("user", {
233
+ name: user.name,
234
+ color: userColor,
235
+ userId: user.userId
236
+ });
237
+ providerRef.current = provider;
238
+ }, [
239
+ websocketUrl,
240
+ documentName,
241
+ doc,
242
+ user.name,
243
+ user.userId,
244
+ userColor,
245
+ autoReconnect,
246
+ maxReconnectAttempts,
247
+ clearRefreshTimer,
248
+ updateConnectionState,
249
+ updateCollaborators,
250
+ onSync,
251
+ onAuthError,
252
+ onRefreshToken,
253
+ refreshToken,
254
+ scheduleTokenRefresh
255
+ ]);
256
+ const disconnect = useCallback(() => {
257
+ if (providerRef.current) {
258
+ providerRef.current.disconnect();
259
+ updateConnectionState("disconnected");
260
+ }
261
+ }, [updateConnectionState]);
262
+ useEffect(() => {
263
+ if (autoConnect) {
264
+ connect();
265
+ }
266
+ return () => {
267
+ clearRefreshTimer();
268
+ if (providerRef.current) {
269
+ providerRef.current.destroy();
270
+ providerRef.current = null;
271
+ }
272
+ };
273
+ }, [autoConnect, clearRefreshTimer, connect]);
274
+ const contextValue = useMemo(
275
+ () => ({
276
+ doc,
277
+ provider: providerRef.current,
278
+ connectionState,
279
+ collaborators,
280
+ isSynced,
281
+ connect,
282
+ disconnect
283
+ }),
284
+ [doc, connectionState, collaborators, isSynced, connect, disconnect]
285
+ );
286
+ return /* @__PURE__ */ jsx(EditorContext.Provider, { value: contextValue, children });
287
+ }
288
+ function useEditorContext() {
289
+ const context = useContext(EditorContext);
290
+ if (!context) {
291
+ throw new Error("useEditorContext must be used within an EditorProvider");
292
+ }
293
+ return context;
294
+ }
295
+
296
+ // src/editor/editor-toolbar.tsx
297
+ import { jsx as jsx2 } from "react/jsx-runtime";
298
+ function EditorToolbar({
299
+ editor,
300
+ className
301
+ }) {
302
+ if (!editor) {
303
+ return null;
304
+ }
305
+ const buttons = [
306
+ {
307
+ id: "bold",
308
+ label: "Bold",
309
+ action: () => editor.chain().focus().toggleBold().run(),
310
+ isActive: editor.isActive("bold"),
311
+ shortcut: "Ctrl+B"
312
+ },
313
+ {
314
+ id: "italic",
315
+ label: "Italic",
316
+ action: () => editor.chain().focus().toggleItalic().run(),
317
+ isActive: editor.isActive("italic"),
318
+ shortcut: "Ctrl+I"
319
+ },
320
+ {
321
+ id: "strike",
322
+ label: "Strike",
323
+ action: () => editor.chain().focus().toggleStrike().run(),
324
+ isActive: editor.isActive("strike"),
325
+ shortcut: "Ctrl+Shift+X"
326
+ },
327
+ {
328
+ id: "code",
329
+ label: "Code",
330
+ action: () => editor.chain().focus().toggleCode().run(),
331
+ isActive: editor.isActive("code"),
332
+ shortcut: "Ctrl+E"
333
+ },
334
+ { id: "sep-1", type: "separator" },
335
+ {
336
+ id: "h1",
337
+ label: "H1",
338
+ action: () => editor.chain().focus().toggleHeading({ level: 1 }).run(),
339
+ isActive: editor.isActive("heading", { level: 1 })
340
+ },
341
+ {
342
+ id: "h2",
343
+ label: "H2",
344
+ action: () => editor.chain().focus().toggleHeading({ level: 2 }).run(),
345
+ isActive: editor.isActive("heading", { level: 2 })
346
+ },
347
+ {
348
+ id: "h3",
349
+ label: "H3",
350
+ action: () => editor.chain().focus().toggleHeading({ level: 3 }).run(),
351
+ isActive: editor.isActive("heading", { level: 3 })
352
+ },
353
+ { id: "sep-2", type: "separator" },
354
+ {
355
+ id: "bullet-list",
356
+ label: "Bullet List",
357
+ action: () => editor.chain().focus().toggleBulletList().run(),
358
+ isActive: editor.isActive("bulletList")
359
+ },
360
+ {
361
+ id: "ordered-list",
362
+ label: "Ordered List",
363
+ action: () => editor.chain().focus().toggleOrderedList().run(),
364
+ isActive: editor.isActive("orderedList")
365
+ },
366
+ {
367
+ id: "code-block",
368
+ label: "Code Block",
369
+ action: () => editor.chain().focus().toggleCodeBlock().run(),
370
+ isActive: editor.isActive("codeBlock")
371
+ },
372
+ {
373
+ id: "blockquote",
374
+ label: "Blockquote",
375
+ action: () => editor.chain().focus().toggleBlockquote().run(),
376
+ isActive: editor.isActive("blockquote")
377
+ }
378
+ ];
379
+ return /* @__PURE__ */ jsx2(
380
+ "div",
381
+ {
382
+ className: cn(
383
+ "flex items-center gap-1 border-border border-b bg-card p-2",
384
+ className
385
+ ),
386
+ children: buttons.map((button) => {
387
+ if ("type" in button && button.type === "separator") {
388
+ return /* @__PURE__ */ jsx2("div", { className: "mx-1 h-6 w-px bg-border" }, button.id);
389
+ }
390
+ return /* @__PURE__ */ jsx2(
391
+ "button",
392
+ {
393
+ onClick: button.action,
394
+ type: "button",
395
+ title: "shortcut" in button ? `${button.label} (${button.shortcut})` : button.label,
396
+ className: cn(
397
+ "rounded px-2 py-1 font-medium text-xs transition-colors",
398
+ "hover:bg-accent hover:text-accent-foreground",
399
+ button.isActive && "bg-accent text-accent-foreground"
400
+ ),
401
+ children: button.label
402
+ },
403
+ button.id
404
+ );
405
+ })
406
+ }
407
+ );
408
+ }
409
+
410
+ // src/editor/tiptap-editor.tsx
411
+ import { jsx as jsx3, jsxs } from "react/jsx-runtime";
412
+ var cursorColors = {
413
+ "#FF6B6B": { background: "#FF6B6B", text: "#FFFFFF" },
414
+ "#4ECDC4": { background: "#4ECDC4", text: "#000000" },
415
+ "#45B7D1": { background: "#45B7D1", text: "#000000" },
416
+ "#96CEB4": { background: "#96CEB4", text: "#000000" },
417
+ "#FFEAA7": { background: "#FFEAA7", text: "#000000" },
418
+ "#DDA0DD": { background: "#DDA0DD", text: "#000000" },
419
+ "#98D8C8": { background: "#98D8C8", text: "#000000" },
420
+ "#F7DC6F": { background: "#F7DC6F", text: "#000000" },
421
+ "#BB8FCE": { background: "#BB8FCE", text: "#000000" },
422
+ "#85C1E9": { background: "#85C1E9", text: "#000000" }
423
+ };
424
+ function getCursorColors(color) {
425
+ return cursorColors[color] ?? { background: color, text: "#FFFFFF" };
426
+ }
427
+ function TiptapEditor({
428
+ initialContent,
429
+ placeholder = "Start writing...",
430
+ readOnly = false,
431
+ autoFocus = false,
432
+ className,
433
+ contentClassName,
434
+ onUpdate,
435
+ onSelectionUpdate,
436
+ onReady
437
+ }) {
438
+ const { doc, provider, connectionState } = useEditorContext();
439
+ const fragment = useMemo2(() => doc.getXmlFragment("prosemirror"), [doc]);
440
+ const extensions = useMemo2(() => {
441
+ const baseExtensions = [
442
+ StarterKit.configure({
443
+ // Disable history - Y.js handles undo/redo
444
+ ...{ history: false },
445
+ // Configure code block for syntax highlighting placeholder
446
+ codeBlock: {
447
+ HTMLAttributes: {
448
+ class: "hljs"
449
+ }
450
+ }
451
+ }),
452
+ Collaboration.configure({
453
+ fragment
454
+ })
455
+ ];
456
+ if (provider?.awareness) {
457
+ baseExtensions.push(
458
+ CollaborationCaret.configure({
459
+ provider,
460
+ user: provider.awareness.getLocalState()?.user ?? {
461
+ name: "Anonymous",
462
+ color: "#808080"
463
+ },
464
+ render: (user) => {
465
+ const { background, text } = getCursorColors(user.color);
466
+ const cursor = document.createElement("span");
467
+ cursor.className = "collaboration-cursor";
468
+ cursor.style.borderColor = background;
469
+ const label = document.createElement("span");
470
+ label.className = "collaboration-cursor-label";
471
+ label.style.backgroundColor = background;
472
+ label.style.color = text;
473
+ label.textContent = user.name;
474
+ cursor.appendChild(label);
475
+ return cursor;
476
+ }
477
+ })
478
+ );
479
+ }
480
+ return baseExtensions;
481
+ }, [fragment, provider]);
482
+ const editor = useEditor({
483
+ extensions,
484
+ editable: !readOnly,
485
+ autofocus: autoFocus,
486
+ editorProps: {
487
+ attributes: {
488
+ class: cn(
489
+ "prose prose-sm sm:prose-base dark:prose-invert max-w-none",
490
+ "focus:outline-none",
491
+ contentClassName
492
+ ),
493
+ "data-placeholder": placeholder
494
+ }
495
+ },
496
+ onUpdate: ({ editor: ed }) => {
497
+ onUpdate?.(ed);
498
+ },
499
+ onSelectionUpdate: ({ editor: ed }) => {
500
+ onSelectionUpdate?.(ed);
501
+ },
502
+ onCreate: ({ editor: ed }) => {
503
+ onReady?.(ed);
504
+ }
505
+ });
506
+ useEffect2(() => {
507
+ if (editor) {
508
+ editor.setEditable(!readOnly);
509
+ }
510
+ }, [editor, readOnly]);
511
+ useEffect2(() => {
512
+ if (editor && initialContent && connectionState === "synced" && editor.isEmpty) {
513
+ editor.commands.setContent(initialContent);
514
+ }
515
+ }, [editor, initialContent, connectionState]);
516
+ return /* @__PURE__ */ jsxs(
517
+ "div",
518
+ {
519
+ className: cn(
520
+ "relative min-h-[200px] w-full rounded-lg border border-border",
521
+ "bg-background",
522
+ className
523
+ ),
524
+ children: [
525
+ /* @__PURE__ */ jsx3("div", { className: "absolute top-2 right-2 z-10", children: /* @__PURE__ */ jsx3(ConnectionIndicator, { state: connectionState }) }),
526
+ /* @__PURE__ */ jsx3("div", { className: "p-4 pt-10", children: /* @__PURE__ */ jsx3(EditorContent, { editor }) }),
527
+ /* @__PURE__ */ jsx3("style", { children: `
528
+ .ProseMirror p.is-editor-empty:first-child::before {
529
+ content: attr(data-placeholder);
530
+ float: left;
531
+ color: var(--muted-foreground, #999);
532
+ pointer-events: none;
533
+ height: 0;
534
+ }
535
+
536
+ .collaboration-cursor {
537
+ position: relative;
538
+ border-left: 2px solid;
539
+ margin-left: -1px;
540
+ margin-right: -1px;
541
+ pointer-events: none;
542
+ word-break: normal;
543
+ }
544
+
545
+ .collaboration-cursor-label {
546
+ position: absolute;
547
+ top: -1.4em;
548
+ left: -1px;
549
+ font-size: 12px;
550
+ font-style: normal;
551
+ font-weight: 600;
552
+ line-height: normal;
553
+ padding: 2px 6px;
554
+ border-radius: 4px 4px 4px 0;
555
+ white-space: nowrap;
556
+ user-select: none;
557
+ }
558
+
559
+ .ProseMirror pre {
560
+ background: var(--muted, #f4f4f5);
561
+ border-radius: 0.375rem;
562
+ padding: 0.75rem 1rem;
563
+ font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
564
+ font-size: 0.875rem;
565
+ overflow-x: auto;
566
+ }
567
+
568
+ .dark .ProseMirror pre {
569
+ background: var(--muted, #27272a);
570
+ }
571
+
572
+ .ProseMirror code {
573
+ background: var(--muted, #f4f4f5);
574
+ border-radius: 0.25rem;
575
+ padding: 0.125rem 0.25rem;
576
+ font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
577
+ font-size: 0.875em;
578
+ }
579
+
580
+ .dark .ProseMirror code {
581
+ background: var(--muted, #27272a);
582
+ }
583
+
584
+ .ProseMirror pre code {
585
+ background: transparent;
586
+ padding: 0;
587
+ font-size: inherit;
588
+ }
589
+ ` })
590
+ ]
591
+ }
592
+ );
593
+ }
594
+ function ConnectionIndicator({
595
+ state
596
+ }) {
597
+ const config = {
598
+ disconnected: {
599
+ color: "bg-red-500",
600
+ label: "Disconnected"
601
+ },
602
+ connecting: {
603
+ color: "bg-yellow-500 animate-pulse",
604
+ label: "Connecting..."
605
+ },
606
+ connected: {
607
+ color: "bg-blue-500",
608
+ label: "Connected"
609
+ },
610
+ synced: {
611
+ color: "bg-green-500",
612
+ label: "Synced"
613
+ }
614
+ }[state];
615
+ return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5 text-muted-foreground text-xs", children: [
616
+ /* @__PURE__ */ jsx3("span", { className: cn("h-2 w-2 rounded-full", config.color) }),
617
+ /* @__PURE__ */ jsx3("span", { children: config.label })
618
+ ] });
619
+ }
620
+ function CollaboratorsList({
621
+ collaborators,
622
+ className
623
+ }) {
624
+ if (collaborators.length === 0) {
625
+ return null;
626
+ }
627
+ return /* @__PURE__ */ jsxs("div", { className: cn("flex items-center gap-1", className), children: [
628
+ collaborators.slice(0, 5).map((collab) => /* @__PURE__ */ jsx3(
629
+ "div",
630
+ {
631
+ className: "flex h-6 w-6 items-center justify-center rounded-full font-medium text-xs",
632
+ style: { backgroundColor: collab.user.color },
633
+ title: collab.user.name,
634
+ children: collab.user.name.charAt(0).toUpperCase()
635
+ },
636
+ collab.clientId
637
+ )),
638
+ collaborators.length > 5 && /* @__PURE__ */ jsxs("div", { className: "flex h-6 w-6 items-center justify-center rounded-full bg-muted font-medium text-xs", children: [
639
+ "+",
640
+ collaborators.length - 5
641
+ ] })
642
+ ] });
643
+ }
644
+
645
+ // src/editor/markdown-document-editor.tsx
646
+ import { EditorContent as EditorContent2, useEditor as useEditor2 } from "@tiptap/react";
647
+ import StarterKit2 from "@tiptap/starter-kit";
648
+ import { useEffect as useEffect3, useMemo as useMemo3, useRef as useRef2 } from "react";
649
+
650
+ // src/editor/markdown-conversion.ts
651
+ import { marked } from "marked";
652
+ import TurndownService from "turndown";
653
+ var turndown = new TurndownService({
654
+ bulletListMarker: "-",
655
+ codeBlockStyle: "fenced",
656
+ emDelimiter: "*",
657
+ headingStyle: "atx"
658
+ });
659
+ function markdownToHtml(markdown) {
660
+ return String(marked.parse(markdown, { async: false, gfm: true }));
661
+ }
662
+ function htmlToMarkdown(html) {
663
+ return turndown.turndown(html);
664
+ }
665
+ function normalizeMarkdown(markdown) {
666
+ return markdown.replace(/\r\n/g, "\n").trimEnd();
667
+ }
668
+
669
+ // src/editor/markdown-document-editor.tsx
670
+ import { jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
671
+ function MarkdownDocumentEditor({
672
+ value = "",
673
+ placeholder = "Start writing...",
674
+ readOnly = false,
675
+ autoFocus = false,
676
+ className,
677
+ contentClassName,
678
+ onChange,
679
+ onReady
680
+ }) {
681
+ const initialHtml = useMemo3(() => markdownToHtml(value), []);
682
+ const lastAppliedMarkdownRef = useRef2(normalizeMarkdown(value));
683
+ const editor = useEditor2({
684
+ extensions: [
685
+ StarterKit2.configure({
686
+ codeBlock: {
687
+ HTMLAttributes: {
688
+ class: "hljs"
689
+ }
690
+ }
691
+ })
692
+ ],
693
+ content: initialHtml,
694
+ editable: !readOnly,
695
+ autofocus: autoFocus,
696
+ editorProps: {
697
+ attributes: {
698
+ class: cn(
699
+ "prose prose-sm sm:prose-base max-w-none focus:outline-none",
700
+ "prose-headings:text-foreground prose-p:text-foreground prose-li:text-foreground",
701
+ "prose-strong:text-foreground prose-code:text-foreground prose-pre:text-foreground",
702
+ contentClassName
703
+ ),
704
+ "data-placeholder": placeholder
705
+ }
706
+ },
707
+ onUpdate: ({ editor: currentEditor }) => {
708
+ const nextMarkdown = normalizeMarkdown(htmlToMarkdown(currentEditor.getHTML()));
709
+ lastAppliedMarkdownRef.current = nextMarkdown;
710
+ onChange?.(nextMarkdown, currentEditor);
711
+ },
712
+ onCreate: ({ editor: currentEditor }) => {
713
+ onReady?.(currentEditor);
714
+ }
715
+ });
716
+ useEffect3(() => {
717
+ if (editor) {
718
+ editor.setEditable(!readOnly);
719
+ }
720
+ }, [editor, readOnly]);
721
+ useEffect3(() => {
722
+ if (!editor) {
723
+ return;
724
+ }
725
+ const normalizedValue = normalizeMarkdown(value);
726
+ if (normalizedValue === lastAppliedMarkdownRef.current) {
727
+ return;
728
+ }
729
+ editor.commands.setContent(markdownToHtml(value), { emitUpdate: false });
730
+ lastAppliedMarkdownRef.current = normalizedValue;
731
+ }, [editor, value]);
732
+ return /* @__PURE__ */ jsxs2(
733
+ "div",
734
+ {
735
+ className: cn(
736
+ "flex min-h-[14rem] w-full flex-col overflow-hidden rounded-lg border border-border bg-background",
737
+ className
738
+ ),
739
+ children: [
740
+ /* @__PURE__ */ jsx4(
741
+ EditorToolbar,
742
+ {
743
+ editor,
744
+ className: "border-border bg-card px-2 py-2"
745
+ }
746
+ ),
747
+ /* @__PURE__ */ jsx4("div", { className: "min-h-0 flex-1 overflow-auto px-5 py-4", children: /* @__PURE__ */ jsx4(EditorContent2, { editor }) }),
748
+ /* @__PURE__ */ jsx4("style", { children: `
749
+ .ProseMirror p.is-editor-empty:first-child::before {
750
+ content: attr(data-placeholder);
751
+ float: left;
752
+ color: hsl(var(--muted-foreground));
753
+ pointer-events: none;
754
+ height: 0;
755
+ }
756
+
757
+ .ProseMirror pre {
758
+ background: hsl(var(--muted));
759
+ border: 1px solid hsl(var(--border));
760
+ border-radius: 0.75rem;
761
+ padding: 0.875rem 1rem;
762
+ overflow-x: auto;
763
+ }
764
+
765
+ .ProseMirror code {
766
+ background: hsl(var(--muted));
767
+ border-radius: 0.35rem;
768
+ padding: 0.12rem 0.3rem;
769
+ }
770
+
771
+ .ProseMirror pre code {
772
+ background: transparent;
773
+ padding: 0;
774
+ }
775
+ ` })
776
+ ]
777
+ }
778
+ );
779
+ }
780
+
781
+ // src/editor/use-editor.ts
782
+ import { useCallback as useCallback2, useEffect as useEffect4, useState as useState2 } from "react";
783
+ function useEditorConnection() {
784
+ const { connectionState, isSynced, connect, disconnect } = useEditorContext();
785
+ const isConnected = connectionState === "connected" || connectionState === "synced";
786
+ const isConnecting = connectionState === "connecting";
787
+ const isDisconnected = connectionState === "disconnected";
788
+ return {
789
+ /** Current connection state string */
790
+ state: connectionState,
791
+ /** Whether connected to the server (connected or synced) */
792
+ isConnected,
793
+ /** Whether currently attempting to connect */
794
+ isConnecting,
795
+ /** Whether disconnected from the server */
796
+ isDisconnected,
797
+ /** Whether the document is synced with the server */
798
+ isSynced,
799
+ /** Connect to the collaboration server */
800
+ connect,
801
+ /** Disconnect from the collaboration server */
802
+ disconnect
803
+ };
804
+ }
805
+ function useCollaborators() {
806
+ const { collaborators } = useEditorContext();
807
+ const count = collaborators.length;
808
+ const hasOthers = count > 0;
809
+ return {
810
+ /** List of active collaborators (excluding self) */
811
+ collaborators,
812
+ /** Number of other collaborators */
813
+ count,
814
+ /** Whether there are other collaborators present */
815
+ hasOthers
816
+ };
817
+ }
818
+ function useCollaboratorPresence(userId) {
819
+ const { collaborators } = useEditorContext();
820
+ return collaborators.find((c) => c.user.userId === userId) ?? null;
821
+ }
822
+ function useYjsState(key, initialValue) {
823
+ const { doc } = useEditorContext();
824
+ const [value, setLocalValue] = useState2(initialValue);
825
+ const metaMap = doc.getMap("metadata");
826
+ useEffect4(() => {
827
+ const updateValue = () => {
828
+ const stored = metaMap.get(key);
829
+ if (stored !== void 0) {
830
+ setLocalValue(stored);
831
+ }
832
+ };
833
+ updateValue();
834
+ metaMap.observe(updateValue);
835
+ return () => {
836
+ metaMap.unobserve(updateValue);
837
+ };
838
+ }, [metaMap, key]);
839
+ const setValue = useCallback2(
840
+ (newValue) => {
841
+ doc.transact(() => {
842
+ metaMap.set(key, newValue);
843
+ });
844
+ },
845
+ [doc, metaMap, key]
846
+ );
847
+ return [value, setValue];
848
+ }
849
+ function useDocumentChanges(onSave) {
850
+ const { doc } = useEditorContext();
851
+ const [isDirty, setIsDirty] = useState2(false);
852
+ const [isSaving, setIsSaving] = useState2(false);
853
+ const [lastSaved, setLastSaved] = useState2(null);
854
+ useEffect4(() => {
855
+ const handleUpdate = () => {
856
+ setIsDirty(true);
857
+ };
858
+ doc.on("update", handleUpdate);
859
+ return () => {
860
+ doc.off("update", handleUpdate);
861
+ };
862
+ }, [doc]);
863
+ const save = useCallback2(async () => {
864
+ if (!onSave || isSaving) return;
865
+ setIsSaving(true);
866
+ try {
867
+ await onSave();
868
+ setIsDirty(false);
869
+ setLastSaved(/* @__PURE__ */ new Date());
870
+ } finally {
871
+ setIsSaving(false);
872
+ }
873
+ }, [onSave, isSaving]);
874
+ return {
875
+ /** Whether there are unsaved changes */
876
+ isDirty,
877
+ /** Whether save is in progress */
878
+ isSaving,
879
+ /** When the document was last saved */
880
+ lastSaved,
881
+ /** Save the document */
882
+ save
883
+ };
884
+ }
885
+ function useAwareness() {
886
+ const { provider } = useEditorContext();
887
+ const [localState, setLocalStateValue] = useState2(
888
+ {}
889
+ );
890
+ const awareness = provider?.awareness;
891
+ useEffect4(() => {
892
+ if (!awareness) return;
893
+ const updateState = () => {
894
+ setLocalStateValue(awareness.getLocalState() ?? {});
895
+ };
896
+ updateState();
897
+ awareness.on("change", updateState);
898
+ return () => {
899
+ awareness.off("change", updateState);
900
+ };
901
+ }, [awareness]);
902
+ const setLocalState = useCallback2(
903
+ (state) => {
904
+ if (!awareness) return;
905
+ const current = awareness.getLocalState() ?? {};
906
+ awareness.setLocalState({ ...current, ...state });
907
+ },
908
+ [awareness]
909
+ );
910
+ const setLocalStateField = useCallback2(
911
+ (field, value) => {
912
+ if (!awareness) return;
913
+ awareness.setLocalStateField(field, value);
914
+ },
915
+ [awareness]
916
+ );
917
+ return {
918
+ /** Current local awareness state */
919
+ localState,
920
+ /** Set the entire local state (merges with existing) */
921
+ setLocalState,
922
+ /** Set a single field in the local state */
923
+ setLocalStateField,
924
+ /** The raw awareness instance */
925
+ awareness
926
+ };
927
+ }
928
+
929
+ // src/editor/document-editor-pane.tsx
930
+ import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
931
+ function connectionTone(state) {
932
+ switch (state) {
933
+ case "synced":
934
+ return "text-[var(--surface-success-text)] border-[var(--surface-success-border)] bg-[var(--surface-success-bg)]";
935
+ case "connected":
936
+ case "connecting":
937
+ return "text-[var(--surface-info-text)] border-[var(--surface-info-border)] bg-[var(--surface-info-bg)]";
938
+ case "disconnected":
939
+ default:
940
+ return "text-[var(--surface-warning-text)] border-[var(--surface-warning-border)] bg-[var(--surface-warning-bg)]";
941
+ }
942
+ }
943
+ function connectionLabel(state) {
944
+ switch (state) {
945
+ case "synced":
946
+ return "Live synced";
947
+ case "connected":
948
+ return "Connected";
949
+ case "connecting":
950
+ return "Connecting";
951
+ case "disconnected":
952
+ default:
953
+ return "Offline";
954
+ }
955
+ }
956
+ function connectionDescription(state, collaborators, readOnly) {
957
+ if (readOnly) {
958
+ return state === "disconnected" ? "Live access is paused. You can keep reading while the editor reconnects." : "You are viewing the live document in read-only mode.";
959
+ }
960
+ switch (state) {
961
+ case "synced":
962
+ return collaborators > 0 ? `You and ${collaborators} collaborator${collaborators === 1 ? "" : "s"} are editing the same document.` : "You are editing the live document. Changes sync automatically.";
963
+ case "connected":
964
+ case "connecting":
965
+ return "Connecting the live document. Local edits stay in place while sync catches up.";
966
+ case "disconnected":
967
+ default:
968
+ return "Live updates are paused. You can keep editing and reconnect when the transport is healthy again.";
969
+ }
970
+ }
971
+ function CollaborativeDocumentSurface({
972
+ markdown,
973
+ placeholder,
974
+ autoFocus,
975
+ readOnly,
976
+ className,
977
+ onChange
978
+ }) {
979
+ const { state } = useEditorConnection();
980
+ const { collaborators } = useCollaborators();
981
+ const initialContent = useMemo4(() => markdownToHtml(markdown), [markdown]);
982
+ const collaboratorCount = collaborators.length + 1;
983
+ return /* @__PURE__ */ jsxs3("div", { className: cn("flex h-full min-h-0 flex-col gap-3", className), children: [
984
+ /* @__PURE__ */ jsxs3("div", { className: "flex flex-wrap items-center justify-between gap-3 rounded-[var(--radius-lg)] border border-border bg-card px-3 py-2", children: [
985
+ /* @__PURE__ */ jsxs3("div", { className: "min-w-0 space-y-2", children: [
986
+ /* @__PURE__ */ jsxs3("div", { className: "flex flex-wrap items-center gap-2 text-xs text-muted-foreground", children: [
987
+ /* @__PURE__ */ jsxs3(
988
+ "span",
989
+ {
990
+ className: cn(
991
+ "inline-flex items-center gap-1.5 rounded-full border px-2.5 py-1 font-medium",
992
+ connectionTone(state)
993
+ ),
994
+ children: [
995
+ state === "disconnected" ? /* @__PURE__ */ jsx5(WifiOff, { className: "h-3.5 w-3.5" }) : /* @__PURE__ */ jsx5(Wifi, { className: "h-3.5 w-3.5" }),
996
+ connectionLabel(state)
997
+ ]
998
+ }
999
+ ),
1000
+ /* @__PURE__ */ jsxs3("span", { className: "inline-flex items-center gap-1.5 rounded-full border border-border bg-background px-2.5 py-1", children: [
1001
+ /* @__PURE__ */ jsx5(Users, { className: "h-3.5 w-3.5" }),
1002
+ collaborators.length === 0 ? "Solo editing" : `${collaboratorCount} active`
1003
+ ] })
1004
+ ] }),
1005
+ /* @__PURE__ */ jsx5("p", { className: "text-xs text-muted-foreground", children: connectionDescription(state, collaborators.length, readOnly) })
1006
+ ] }),
1007
+ /* @__PURE__ */ jsx5(CollaboratorsList, { collaborators })
1008
+ ] }),
1009
+ /* @__PURE__ */ jsx5(
1010
+ TiptapEditor,
1011
+ {
1012
+ initialContent,
1013
+ placeholder,
1014
+ autoFocus,
1015
+ readOnly,
1016
+ className: cn("h-full min-h-[16rem]", className),
1017
+ onUpdate: (editor) => {
1018
+ onChange?.(normalizeMarkdown(htmlToMarkdown(editor.getHTML())));
1019
+ }
1020
+ }
1021
+ )
1022
+ ] });
1023
+ }
1024
+ function DocumentEditorPane({
1025
+ eyebrow,
1026
+ title,
1027
+ subtitle,
1028
+ meta,
1029
+ headerActions,
1030
+ footer,
1031
+ className,
1032
+ contentClassName,
1033
+ tabs,
1034
+ toolbar,
1035
+ markdown = "",
1036
+ mode,
1037
+ defaultMode = "preview",
1038
+ onModeChange,
1039
+ backend = "local",
1040
+ placeholder = "Start writing...",
1041
+ autoFocus = false,
1042
+ readOnly = false,
1043
+ onChange,
1044
+ onSave,
1045
+ saving = false,
1046
+ saveLabel = "Save changes",
1047
+ previewClassName,
1048
+ editorClassName,
1049
+ collaboration
1050
+ }) {
1051
+ const [draft, setDraft] = useState3(markdown);
1052
+ const [uncontrolledMode, setUncontrolledMode] = useState3(defaultMode);
1053
+ const activeMode = mode ?? uncontrolledMode;
1054
+ const isCollaborative = backend === "collaborative" && Boolean(collaboration);
1055
+ const isDirty = normalizeMarkdown(draft) !== normalizeMarkdown(markdown);
1056
+ const saveStateLabel = readOnly ? "Read only" : isCollaborative ? isDirty ? "Snapshot pending" : "Live document current" : isDirty ? "Unsaved changes" : "Saved";
1057
+ useEffect5(() => {
1058
+ setDraft(markdown);
1059
+ }, [markdown]);
1060
+ useEffect5(() => {
1061
+ if (mode === void 0) {
1062
+ setUncontrolledMode(defaultMode);
1063
+ }
1064
+ }, [defaultMode, mode]);
1065
+ const setMode = (nextMode) => {
1066
+ if (mode === void 0) {
1067
+ setUncontrolledMode(nextMode);
1068
+ }
1069
+ onModeChange?.(nextMode);
1070
+ };
1071
+ const handleChange = (nextMarkdown) => {
1072
+ setDraft(nextMarkdown);
1073
+ onChange?.(nextMarkdown);
1074
+ };
1075
+ const editorToolbar = /* @__PURE__ */ jsxs3("div", { className: "flex flex-wrap items-center justify-between gap-3", children: [
1076
+ /* @__PURE__ */ jsxs3(
1077
+ TabsList,
1078
+ {
1079
+ variant: "underline",
1080
+ className: "h-auto gap-4 border-0 bg-transparent p-0 text-muted-foreground",
1081
+ children: [
1082
+ /* @__PURE__ */ jsx5(
1083
+ TabsTrigger,
1084
+ {
1085
+ value: "preview",
1086
+ variant: "underline",
1087
+ className: "pb-2 data-[state=active]:border-primary data-[state=active]:text-foreground",
1088
+ children: "Preview"
1089
+ }
1090
+ ),
1091
+ /* @__PURE__ */ jsxs3(
1092
+ TabsTrigger,
1093
+ {
1094
+ value: "edit",
1095
+ variant: "underline",
1096
+ className: "flex items-center gap-2 pb-2 data-[state=active]:border-primary data-[state=active]:text-foreground",
1097
+ children: [
1098
+ /* @__PURE__ */ jsx5(PencilLine, { className: "h-3.5 w-3.5" }),
1099
+ isCollaborative ? "Live edit" : "Edit"
1100
+ ]
1101
+ }
1102
+ )
1103
+ ]
1104
+ }
1105
+ ),
1106
+ /* @__PURE__ */ jsxs3("div", { className: "flex flex-wrap items-center gap-2 text-xs text-muted-foreground", children: [
1107
+ toolbar,
1108
+ /* @__PURE__ */ jsx5("span", { className: "rounded-full border border-border bg-card px-2.5 py-1 font-medium", children: isCollaborative ? "Live document" : "Local draft" }),
1109
+ /* @__PURE__ */ jsx5("span", { className: "rounded-full border border-border bg-background px-2.5 py-1", children: saveStateLabel }),
1110
+ onSave && !readOnly && /* @__PURE__ */ jsxs3(
1111
+ "button",
1112
+ {
1113
+ type: "button",
1114
+ onClick: () => void onSave(draft),
1115
+ disabled: saving || !isDirty,
1116
+ className: "inline-flex items-center gap-2 rounded-[var(--radius-full)] border border-border bg-card px-3 py-1.5 text-xs font-semibold text-foreground transition-colors hover:border-primary/40 hover:text-foreground disabled:cursor-not-allowed disabled:opacity-50",
1117
+ children: [
1118
+ /* @__PURE__ */ jsx5(Save, { className: "h-3.5 w-3.5" }),
1119
+ saving ? "Saving..." : saveLabel
1120
+ ]
1121
+ }
1122
+ )
1123
+ ] })
1124
+ ] });
1125
+ const preview = /* @__PURE__ */ jsx5(
1126
+ "div",
1127
+ {
1128
+ className: cn(
1129
+ "rounded-[var(--radius-lg)] border border-border bg-background p-5",
1130
+ previewClassName
1131
+ ),
1132
+ children: /* @__PURE__ */ jsx5(Markdown, { className: "prose-sm max-w-none", children: draft })
1133
+ }
1134
+ );
1135
+ const localEditor = /* @__PURE__ */ jsx5(
1136
+ MarkdownDocumentEditor,
1137
+ {
1138
+ value: draft,
1139
+ placeholder,
1140
+ autoFocus,
1141
+ readOnly,
1142
+ onChange: (nextMarkdown) => {
1143
+ handleChange(nextMarkdown);
1144
+ },
1145
+ className: editorClassName
1146
+ }
1147
+ );
1148
+ const collaborativeEditor = collaboration ? /* @__PURE__ */ jsx5(EditorProvider, { ...collaboration, children: /* @__PURE__ */ jsx5(
1149
+ CollaborativeDocumentSurface,
1150
+ {
1151
+ markdown: draft,
1152
+ placeholder,
1153
+ autoFocus,
1154
+ readOnly,
1155
+ className: editorClassName,
1156
+ onChange: handleChange
1157
+ }
1158
+ ) }, collaboration.documentName) : localEditor;
1159
+ return /* @__PURE__ */ jsx5(
1160
+ Tabs,
1161
+ {
1162
+ value: activeMode,
1163
+ onValueChange: (nextValue) => setMode(nextValue),
1164
+ className: "h-full",
1165
+ children: /* @__PURE__ */ jsxs3(
1166
+ ArtifactPane,
1167
+ {
1168
+ eyebrow,
1169
+ title,
1170
+ subtitle,
1171
+ meta,
1172
+ headerActions,
1173
+ footer,
1174
+ tabs,
1175
+ className,
1176
+ contentClassName,
1177
+ toolbar: editorToolbar,
1178
+ children: [
1179
+ /* @__PURE__ */ jsx5(TabsContent, { value: "preview", className: "mt-0 h-full px-4 py-4", children: preview }),
1180
+ /* @__PURE__ */ jsx5(TabsContent, { value: "edit", className: "mt-0 h-full px-4 py-4", children: isCollaborative ? collaborativeEditor : localEditor })
1181
+ ]
1182
+ }
1183
+ )
1184
+ }
1185
+ );
1186
+ }
1187
+
1188
+ export {
1189
+ EditorProvider,
1190
+ useEditorContext,
1191
+ EditorToolbar,
1192
+ TiptapEditor,
1193
+ CollaboratorsList,
1194
+ useEditorConnection,
1195
+ useCollaborators,
1196
+ useCollaboratorPresence,
1197
+ useYjsState,
1198
+ useDocumentChanges,
1199
+ useAwareness,
1200
+ DocumentEditorPane
1201
+ };