@ephia/dova-sdk 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 (247) hide show
  1. package/README.md +89 -0
  2. package/dist/EphiaBinding-BvRmlqqC.d.ts +36 -0
  3. package/dist/EphiaFloatingButton-CxiF86VW.d.ts +65 -0
  4. package/dist/EphiaTextarea-B4_CAVUg.d.ts +183 -0
  5. package/dist/NativeBinding-ChG0GeSz.d.ts +53 -0
  6. package/dist/TargetBinding-BKGQwUMc.d.ts +89 -0
  7. package/dist/TiptapBinding-B-agfV2H.d.ts +45 -0
  8. package/dist/Transport-zdeA4Pou.d.ts +63 -0
  9. package/dist/audio-state-kZ3KSvux.d.ts +39 -0
  10. package/dist/chunk-35AJK2IO.js +1 -0
  11. package/dist/chunk-35AJK2IO.js.map +1 -0
  12. package/dist/chunk-3LXZODL4.js +886 -0
  13. package/dist/chunk-3LXZODL4.js.map +1 -0
  14. package/dist/chunk-5IK5TLSK.js +67 -0
  15. package/dist/chunk-5IK5TLSK.js.map +1 -0
  16. package/dist/chunk-7E43RY75.js +9 -0
  17. package/dist/chunk-7E43RY75.js.map +1 -0
  18. package/dist/chunk-A5UEXJ5R.js +183 -0
  19. package/dist/chunk-A5UEXJ5R.js.map +1 -0
  20. package/dist/chunk-AEE554FT.js +51 -0
  21. package/dist/chunk-AEE554FT.js.map +1 -0
  22. package/dist/chunk-DIEWY3IT.js +1332 -0
  23. package/dist/chunk-DIEWY3IT.js.map +1 -0
  24. package/dist/chunk-EGIAN7FH.js +18 -0
  25. package/dist/chunk-EGIAN7FH.js.map +1 -0
  26. package/dist/chunk-EMOEAPVU.js +486 -0
  27. package/dist/chunk-EMOEAPVU.js.map +1 -0
  28. package/dist/chunk-IDC7FHIZ.js +40 -0
  29. package/dist/chunk-IDC7FHIZ.js.map +1 -0
  30. package/dist/chunk-ITJFN3VM.js +601 -0
  31. package/dist/chunk-ITJFN3VM.js.map +1 -0
  32. package/dist/chunk-K24GNU27.js +22 -0
  33. package/dist/chunk-K24GNU27.js.map +1 -0
  34. package/dist/chunk-LXMCRXXF.js +778 -0
  35. package/dist/chunk-LXMCRXXF.js.map +1 -0
  36. package/dist/chunk-MJCEOOLW.js +122 -0
  37. package/dist/chunk-MJCEOOLW.js.map +1 -0
  38. package/dist/chunk-N7U5M3VZ.js +33 -0
  39. package/dist/chunk-N7U5M3VZ.js.map +1 -0
  40. package/dist/chunk-PSYX674B.js +27 -0
  41. package/dist/chunk-PSYX674B.js.map +1 -0
  42. package/dist/chunk-RFQRV7ML.js +33 -0
  43. package/dist/chunk-RFQRV7ML.js.map +1 -0
  44. package/dist/chunk-THNHRV2B.js +18 -0
  45. package/dist/chunk-THNHRV2B.js.map +1 -0
  46. package/dist/chunk-VSLGR64U.js +62 -0
  47. package/dist/chunk-VSLGR64U.js.map +1 -0
  48. package/dist/chunk-W2ZP674X.js +346 -0
  49. package/dist/chunk-W2ZP674X.js.map +1 -0
  50. package/dist/chunk-YWZUMUYE.js +695 -0
  51. package/dist/chunk-YWZUMUYE.js.map +1 -0
  52. package/dist/client-options-Uo6jXO8k.d.ts +64 -0
  53. package/dist/connection-state-Bk33YprE.d.ts +32 -0
  54. package/dist/core/bindings/index.d.ts +24 -0
  55. package/dist/core/bindings/index.js +1025 -0
  56. package/dist/core/bindings/index.js.map +1 -0
  57. package/dist/core/index.d.ts +383 -0
  58. package/dist/core/index.js +1284 -0
  59. package/dist/core/index.js.map +1 -0
  60. package/dist/createEphiaClient-BhdZ183V.d.ts +69 -0
  61. package/dist/devices/speechmike/index.d.ts +148 -0
  62. package/dist/devices/speechmike/index.js +40 -0
  63. package/dist/devices/speechmike/index.js.map +1 -0
  64. package/dist/headless/index.d.ts +10 -0
  65. package/dist/headless/index.js +25 -0
  66. package/dist/headless/index.js.map +1 -0
  67. package/dist/index.d.ts +18 -0
  68. package/dist/index.js +119 -0
  69. package/dist/index.js.map +1 -0
  70. package/dist/react/index.d.ts +38 -0
  71. package/dist/react/index.js +70 -0
  72. package/dist/react/index.js.map +1 -0
  73. package/dist/rich-editor/index.d.ts +46 -0
  74. package/dist/rich-editor/index.js +13 -0
  75. package/dist/rich-editor/index.js.map +1 -0
  76. package/dist/schema-B2ycPlNB.d.ts +87 -0
  77. package/dist/session-APaXR48R.d.ts +12 -0
  78. package/dist/shared/index.d.ts +16 -0
  79. package/dist/shared/index.js +30 -0
  80. package/dist/shared/index.js.map +1 -0
  81. package/dist/style.css +1093 -0
  82. package/dist/testing/index.d.ts +84 -0
  83. package/dist/testing/index.js +36 -0
  84. package/dist/testing/index.js.map +1 -0
  85. package/dist/types-D5SXPSwR.d.ts +32 -0
  86. package/dist/ui/index.d.ts +30 -0
  87. package/dist/ui/index.js +34 -0
  88. package/dist/ui/index.js.map +1 -0
  89. package/dist/useEphiaSpeechMike-CjD7DWnh.d.ts +64 -0
  90. package/package.json +110 -0
  91. package/src/core/audio/audio-worklet-source.ts +30 -0
  92. package/src/core/audio/index.ts +3 -0
  93. package/src/core/audio/voice-level-meter.test.ts +27 -0
  94. package/src/core/audio/voice-level-meter.ts +270 -0
  95. package/src/core/bindings/EphiaBinding.ts +41 -0
  96. package/src/core/bindings/SegmentBindingBridge.test.ts +422 -0
  97. package/src/core/bindings/SegmentBindingBridge.ts +377 -0
  98. package/src/core/bindings/TargetBinding.ts +142 -0
  99. package/src/core/bindings/adapters/NativeAdapter.test.ts +85 -0
  100. package/src/core/bindings/adapters/NativeAdapter.ts +216 -0
  101. package/src/core/bindings/adapters/ProseMirrorAdapter.ts +231 -0
  102. package/src/core/bindings/adapters/index.ts +2 -0
  103. package/src/core/bindings/binding-factory.ts +78 -0
  104. package/src/core/bindings/detect-editor-type.ts +87 -0
  105. package/src/core/bindings/index.ts +13 -0
  106. package/src/core/bindings/insertion-boundary.test.ts +38 -0
  107. package/src/core/bindings/insertion-boundary.ts +26 -0
  108. package/src/core/bindings/native/NativeBinding.test.ts +277 -0
  109. package/src/core/bindings/native/NativeBinding.ts +239 -0
  110. package/src/core/bindings/resolver.ts +18 -0
  111. package/src/core/bindings/targets/codemirror.binding.ts +293 -0
  112. package/src/core/bindings/targets/contenteditable.binding.ts +452 -0
  113. package/src/core/bindings/targets/index.ts +10 -0
  114. package/src/core/bindings/targets/monaco.binding.ts +315 -0
  115. package/src/core/bindings/targets/tiptap.binding.test.ts +417 -0
  116. package/src/core/bindings/targets/tiptap.binding.ts +1192 -0
  117. package/src/core/bindings/tiptap/TiptapBinding.test.ts +63 -0
  118. package/src/core/bindings/tiptap/TiptapBinding.ts +464 -0
  119. package/src/core/bindings/types.ts +41 -0
  120. package/src/core/client/EphiaAudioClient.ts +654 -0
  121. package/src/core/client/audio-capture.ts +263 -0
  122. package/src/core/client/client-options.ts +39 -0
  123. package/src/core/client/client-state.ts +18 -0
  124. package/src/core/client/constants.ts +23 -0
  125. package/src/core/client/session-api.ts +415 -0
  126. package/src/core/connection/connection-state.ts +78 -0
  127. package/src/core/connection/index.ts +6 -0
  128. package/src/core/index.ts +47 -0
  129. package/src/core/operations/textToDocumentOperations.test.ts +69 -0
  130. package/src/core/operations/textToDocumentOperations.ts +92 -0
  131. package/src/core/runtime/DictationRuntime.test.ts +578 -0
  132. package/src/core/runtime/DictationRuntime.ts +434 -0
  133. package/src/core/runtime/TranscriptApplier.test.ts +355 -0
  134. package/src/core/runtime/TranscriptApplier.ts +229 -0
  135. package/src/core/runtime/index.ts +18 -0
  136. package/src/core/session/index.ts +2 -0
  137. package/src/core/session/session-machine.test.ts +16 -0
  138. package/src/core/session/session-machine.ts +59 -0
  139. package/src/core/targets/EditorContextCollector.ts +71 -0
  140. package/src/core/targets/TargetManager.test.ts +194 -0
  141. package/src/core/targets/TargetManager.ts +194 -0
  142. package/src/core/targets/index.ts +10 -0
  143. package/src/core/text-processing/index.ts +11 -0
  144. package/src/core/text-processing/overlap.test.ts +35 -0
  145. package/src/core/text-processing/overlap.ts +101 -0
  146. package/src/core/text-processing/voice-formatting.normalizer.test.ts +132 -0
  147. package/src/core/text-processing/voice-formatting.normalizer.ts +284 -0
  148. package/src/core/transcript/client-transcript.reducer.ts +366 -0
  149. package/src/core/transcript/client-transcript.state.ts +25 -0
  150. package/src/core/transcript/index.ts +19 -0
  151. package/src/core/transcript/transcript.assembler.test.ts +205 -0
  152. package/src/core/transcript/transcript.assembler.ts +152 -0
  153. package/src/core/transcript/transcript.reducer.test.ts +199 -0
  154. package/src/core/transcript/transcript.reducer.ts +771 -0
  155. package/src/core/transcript/transcript.state.ts +123 -0
  156. package/src/core/transport/LiveKitTransport.publish.test.ts +226 -0
  157. package/src/core/transport/LiveKitTransport.ts +459 -0
  158. package/src/core/transport/MockTransport.ts +231 -0
  159. package/src/core/transport/Transport.ts +82 -0
  160. package/src/debug/sdk-debug-collector.ts +79 -0
  161. package/src/devices/index.ts +2 -0
  162. package/src/devices/speechmike/__tests__/EphiaSpeechMikeProvider.test.tsx +99 -0
  163. package/src/devices/speechmike/__tests__/speechmike-audio-resolver.test.ts +96 -0
  164. package/src/devices/speechmike/__tests__/speechmike-button-router.test.ts +66 -0
  165. package/src/devices/speechmike/__tests__/speechmike-device-manager.test.ts +201 -0
  166. package/src/devices/speechmike/__tests__/speechmike-led-controller.test.ts +68 -0
  167. package/src/devices/speechmike/browser.ts +80 -0
  168. package/src/devices/speechmike/constants.ts +74 -0
  169. package/src/devices/speechmike/dictation-support-loader.ts +81 -0
  170. package/src/devices/speechmike/index.ts +11 -0
  171. package/src/devices/speechmike/react/EphiaSpeechMikeContext.ts +34 -0
  172. package/src/devices/speechmike/react/EphiaSpeechMikeProvider.tsx +287 -0
  173. package/src/devices/speechmike/react/useEphiaSpeechMike.ts +26 -0
  174. package/src/devices/speechmike/speechmike-audio-resolver.ts +58 -0
  175. package/src/devices/speechmike/speechmike-button-router.ts +73 -0
  176. package/src/devices/speechmike/speechmike-device-manager.ts +461 -0
  177. package/src/devices/speechmike/speechmike-led-controller.ts +78 -0
  178. package/src/devices/speechmike/types.ts +96 -0
  179. package/src/dictation_support.d.ts +31 -0
  180. package/src/global.d.ts +10 -0
  181. package/src/headless/createEphiaClient.ts +220 -0
  182. package/src/headless/index.ts +18 -0
  183. package/src/index.ts +89 -0
  184. package/src/react/EphiaAuto.tsx +87 -0
  185. package/src/react/components/EphiaDictationButton.tsx +88 -0
  186. package/src/react/components/EphiaStatusBar.tsx +59 -0
  187. package/src/react/components/EphiaTextarea.tsx +295 -0
  188. package/src/react/ephia-react.css +318 -0
  189. package/src/react/hooks/targets/index.ts +3 -0
  190. package/src/react/hooks/targets/useEphiaCodemirror.ts +35 -0
  191. package/src/react/hooks/targets/useEphiaMonaco.ts +35 -0
  192. package/src/react/hooks/targets/useEphiaTiptap.ts +23 -0
  193. package/src/react/hooks/useEphia.lifecycle.test.tsx +389 -0
  194. package/src/react/hooks/useEphia.ts +367 -0
  195. package/src/react/hooks/useEphiaDiscardTarget.ts +53 -0
  196. package/src/react/hooks/useEphiaServerEvent.ts +33 -0
  197. package/src/react/hooks/useEphiaTarget.ts +47 -0
  198. package/src/react/hooks/useEphiaTranscript.ts +22 -0
  199. package/src/react/index.ts +58 -0
  200. package/src/react/provider/EphiaContext.ts +63 -0
  201. package/src/react/provider/EphiaInternalContext.ts +32 -0
  202. package/src/react/provider/EphiaProvider.tsx +373 -0
  203. package/src/react/registry/binding-factory.ts +7 -0
  204. package/src/react/registry/detect-editor-type.ts +2 -0
  205. package/src/react/registry/events.ts +37 -0
  206. package/src/react/registry/registries/CodeMirrorInstanceRegistry.ts +24 -0
  207. package/src/react/registry/registries/MonacoInstanceRegistry.ts +23 -0
  208. package/src/react/registry/registries/TargetRegistry.ts +327 -0
  209. package/src/react/registry/registries/TiptapInstanceRegistry.ts +43 -0
  210. package/src/react/registry/registries/index.ts +5 -0
  211. package/src/react/store/create-ephia-store.ts +36 -0
  212. package/src/react/store/types.ts +41 -0
  213. package/src/react/utils/flash-range.ts +24 -0
  214. package/src/react/utils/index.ts +1 -0
  215. package/src/rich-editor/adapters/tiptap.test.ts +86 -0
  216. package/src/rich-editor/adapters/tiptap.ts +23 -0
  217. package/src/rich-editor/index.ts +3 -0
  218. package/src/rich-editor/types.ts +24 -0
  219. package/src/rich-editor/use-ephia-rich-editor.test.tsx +202 -0
  220. package/src/rich-editor/use-ephia-rich-editor.ts +47 -0
  221. package/src/shared/config/endpoint.test.ts +45 -0
  222. package/src/shared/config/endpoint.ts +39 -0
  223. package/src/shared/config/schema.ts +32 -0
  224. package/src/shared/effective-text.ts +13 -0
  225. package/src/shared/errors/EphiaSdkError.ts +54 -0
  226. package/src/shared/errors/messages.ts +40 -0
  227. package/src/shared/index.ts +27 -0
  228. package/src/shared/state/audio-state.ts +45 -0
  229. package/src/shared/state/index.ts +2 -0
  230. package/src/shared/store/document-store.ts +32 -0
  231. package/src/shared/store/index.ts +2 -0
  232. package/src/shared/types/editors.ts +28 -0
  233. package/src/shared/types/session.ts +12 -0
  234. package/src/style.css +2 -0
  235. package/src/testing/index.tsx +60 -0
  236. package/src/ui/assets/ephia-logo.svg +4 -0
  237. package/src/ui/components/EphiaLogo.tsx +77 -0
  238. package/src/ui/index.ts +24 -0
  239. package/src/ui/primitives/Button.tsx +53 -0
  240. package/src/ui/primitives/Spinner.tsx +21 -0
  241. package/src/ui/primitives/index.ts +5 -0
  242. package/src/ui/recorder/EphiaFloatingButton.tsx +489 -0
  243. package/src/ui/recorder/MinimalProcessingBars.tsx +122 -0
  244. package/src/ui/recorder/StandardIntensityVisualizer.tsx +148 -0
  245. package/src/ui/recorder/appearance.ts +9 -0
  246. package/src/ui/recorder/index.ts +8 -0
  247. package/src/ui/theme.css +775 -0
@@ -0,0 +1,293 @@
1
+ import type { CMEditorView } from "../../../shared/types/editors";
2
+ import type {
3
+ BeginSessionOptions,
4
+ SessionAnchor,
5
+ TargetBinding,
6
+ } from "../TargetBinding";
7
+ import { pickJoiner } from "../TargetBinding";
8
+ import type { DocumentOperation } from "ephia-protocol";
9
+
10
+ interface SessionSegment {
11
+ start: number;
12
+ end: number;
13
+ revision: number;
14
+ }
15
+
16
+ interface PendingPartial {
17
+ segmentId: string;
18
+ text: string;
19
+ insertAt: number;
20
+ revision: number;
21
+ }
22
+
23
+ /**
24
+ * Binding CodeMirror 6. Prend une `EditorView` duck-typed.
25
+ *
26
+ * Les partials ne sont PAS écrits dans le document CM6 — ils restent en mémoire
27
+ * et sont émis via `ephia:partial` sur le container DOM pour un éventuel overlay.
28
+ * Seuls les finals (`commitFinal`) s'appliquent via `view.dispatch`.
29
+ */
30
+ export function createCodeMirrorBinding(view: CMEditorView): TargetBinding {
31
+ let anchor: SessionAnchor | null = null;
32
+ let segments = new Map<string, SessionSegment>();
33
+ let pendingPartial: PendingPartial | null = null;
34
+
35
+ const container = view.dom;
36
+
37
+ const getDocLength = () => view.state.doc.length;
38
+ const getText = () => view.state.doc.toString();
39
+
40
+ const dispatchEdit = (from: number, to: number, insert: string, newCursor?: number) => {
41
+ const changes = { from, to, insert };
42
+ const selectionAnchor = newCursor ?? from + insert.length;
43
+ view.dispatch({ changes, selection: { anchor: selectionAnchor } });
44
+ };
45
+
46
+ const dispatchPartialEvent = (text: string | null, insertAt: number) => {
47
+ container.dispatchEvent(
48
+ new CustomEvent(text === null ? "ephia:partial-cleared" : "ephia:partial", {
49
+ bubbles: false,
50
+ detail: text === null ? undefined : { text, insertAt },
51
+ })
52
+ );
53
+ };
54
+
55
+ return {
56
+ kind: "codemirror",
57
+
58
+ attach(): void {},
59
+
60
+ detach(): void {
61
+ segments.clear();
62
+ pendingPartial = null;
63
+ anchor = null;
64
+ },
65
+
66
+ // ─── Session lifecycle ─────────────────────────────────────────────────────
67
+
68
+ beginSession(opts: BeginSessionOptions = {}): SessionAnchor {
69
+ const replaceSelection = opts.replaceSelection ?? true;
70
+ const sel = view.state.selection.main;
71
+ const isFocused = document.activeElement === view.contentDOM || view.contentDOM.contains(document.activeElement);
72
+
73
+ let from: number;
74
+ let to: number;
75
+ let hadSelection = false;
76
+
77
+ if (!sel.empty && isFocused) {
78
+ from = sel.from;
79
+ to = sel.to;
80
+ hadSelection = true;
81
+ } else if (isFocused) {
82
+ from = sel.head;
83
+ to = sel.head;
84
+ } else {
85
+ from = getDocLength();
86
+ to = from;
87
+ }
88
+
89
+ // Ne pas supprimer la sélection ici — différé au premier insert.
90
+
91
+ anchor = {
92
+ initialStart: from,
93
+ initialEnd: to, // conserver la vraie fin de sélection
94
+ hadSelection,
95
+ end: from, // point d'insertion = début de sélection
96
+ selectionPendingDelete: hadSelection && replaceSelection,
97
+ };
98
+ segments.clear();
99
+ pendingPartial = null;
100
+ return anchor;
101
+ },
102
+
103
+ endSession(): void {
104
+ if (pendingPartial) dispatchPartialEvent(null, 0);
105
+ anchor = null;
106
+ segments.clear();
107
+ pendingPartial = null;
108
+ },
109
+
110
+ getSessionAnchor(): SessionAnchor | null {
111
+ return anchor;
112
+ },
113
+
114
+ getCursorRect(): DOMRect | null {
115
+ // CM6 expose getBoundingClientRect via les coordonnées du curseur
116
+ try {
117
+ const pos = anchor?.end ?? view.state.selection.main.head;
118
+ const coords = (view as unknown as { coordsAtPos?: (pos: number) => { top: number; bottom: number; left: number } | null }).coordsAtPos?.(pos);
119
+ if (!coords) return container.getBoundingClientRect();
120
+ const height = coords.bottom - coords.top;
121
+ return new DOMRect(coords.left, coords.top, 2, height);
122
+ } catch {
123
+ return container.getBoundingClientRect();
124
+ }
125
+ },
126
+
127
+ // ─── Streaming ─────────────────────────────────────────────────────────────
128
+
129
+ insertPartial(segmentId: string, text: string, revision: number): void {
130
+ if (!anchor) {
131
+ const pos = view.state.selection.main.head;
132
+ anchor = { initialStart: pos, initialEnd: pos, hadSelection: false, end: pos, selectionPendingDelete: false };
133
+ }
134
+
135
+ if (pendingPartial?.segmentId === segmentId) {
136
+ if (revision < pendingPartial.revision) return;
137
+ pendingPartial.text = text;
138
+ pendingPartial.revision = revision;
139
+ } else {
140
+ pendingPartial = { segmentId, text, insertAt: anchor.end, revision };
141
+ }
142
+
143
+ dispatchPartialEvent(text, pendingPartial.insertAt);
144
+ },
145
+
146
+ commitFinal(segmentId: string, text: string): void {
147
+ if (!anchor) {
148
+ const pos = view.state.selection.main.head;
149
+ const before = getText().slice(0, pos);
150
+ const joiner = pickJoiner(before, text);
151
+ dispatchEdit(pos, pos, joiner + text);
152
+ return;
153
+ }
154
+
155
+ // Suppression lazy de la sélection au premier commit
156
+ if (anchor.selectionPendingDelete && anchor.initialEnd > anchor.initialStart) {
157
+ dispatchEdit(anchor.initialStart, anchor.initialEnd, "");
158
+ anchor.end = anchor.initialStart;
159
+ anchor.selectionPendingDelete = false;
160
+ }
161
+
162
+ if (pendingPartial?.segmentId === segmentId) {
163
+ const insertAt = pendingPartial.insertAt;
164
+ const before = getText().slice(0, insertAt);
165
+ const joiner = pickJoiner(before, text);
166
+ const full = joiner + text;
167
+
168
+ dispatchPartialEvent(null, 0);
169
+ dispatchEdit(insertAt, insertAt, full, insertAt + full.length);
170
+
171
+ segments.set(segmentId, { start: insertAt + joiner.length, end: insertAt + full.length, revision: 0 });
172
+ anchor.end = insertAt + full.length;
173
+ pendingPartial = null;
174
+ } else {
175
+ const existing = segments.get(segmentId);
176
+ if (existing) {
177
+ const oldLen = existing.end - existing.start;
178
+ dispatchEdit(existing.start, existing.end, text, existing.start + text.length);
179
+ existing.end = existing.start + text.length;
180
+ existing.revision += 1;
181
+ anchor.end += text.length - oldLen;
182
+ } else {
183
+ const before = getText().slice(0, anchor.end);
184
+ const joiner = pickJoiner(before, text);
185
+ const full = joiner + text;
186
+ dispatchEdit(anchor.end, anchor.end, full, anchor.end + full.length);
187
+ segments.set(segmentId, { start: anchor.end + joiner.length, end: anchor.end + full.length, revision: 0 });
188
+ anchor.end += full.length;
189
+ }
190
+ }
191
+ },
192
+
193
+ clearPartial(segmentId: string): void {
194
+ if (pendingPartial?.segmentId === segmentId) {
195
+ pendingPartial = null;
196
+ dispatchPartialEvent(null, 0);
197
+ }
198
+ },
199
+
200
+ clearAll(): void {
201
+ if (pendingPartial) {
202
+ pendingPartial = null;
203
+ dispatchPartialEvent(null, 0);
204
+ }
205
+ },
206
+
207
+ getSegmentRange(segmentId: string): { start: number; end: number } | null {
208
+ const seg = segments.get(segmentId);
209
+ return seg ? { start: seg.start, end: seg.end } : null;
210
+ },
211
+
212
+ getText(): string {
213
+ return getText();
214
+ },
215
+
216
+ getRangeRect(start: number, end: number): DOMRect | null {
217
+ try {
218
+ const startCoords = (view as unknown as { coordsAtPos?: (pos: number) => { top: number; bottom: number; left: number; right: number } | null }).coordsAtPos?.(start);
219
+ const endCoords = (view as unknown as { coordsAtPos?: (pos: number) => { top: number; bottom: number; left: number; right: number } | null }).coordsAtPos?.(end);
220
+ if (!startCoords || !endCoords) return null;
221
+ const top = Math.min(startCoords.top, endCoords.top);
222
+ const bottom = Math.max(startCoords.bottom, endCoords.bottom);
223
+ const left = Math.min(startCoords.left, endCoords.left);
224
+ const right = Math.max(startCoords.right, endCoords.right);
225
+ return new DOMRect(left, top, Math.max(2, right - left), bottom - top);
226
+ } catch {
227
+ return null;
228
+ }
229
+ },
230
+
231
+ getCursorOffset(): number | null {
232
+ return view.state.selection.main.head;
233
+ },
234
+
235
+ getSelection() {
236
+ const sel = view.state.selection.main;
237
+ if (sel.empty) return null;
238
+ const text = getText();
239
+ return {
240
+ text: text.slice(sel.from, sel.to),
241
+ range: { start: sel.from, end: sel.to },
242
+ };
243
+ },
244
+
245
+ applyOperation(operation: DocumentOperation): void {
246
+ const text = getText();
247
+ switch (operation.type) {
248
+ case "replace": {
249
+ let start: number, end: number;
250
+ if (operation.range) {
251
+ start = operation.range.start;
252
+ end = operation.range.end;
253
+ } else if (operation.targetText) {
254
+ const idx = text.indexOf(operation.targetText);
255
+ if (idx === -1) return;
256
+ start = idx; end = idx + operation.targetText.length;
257
+ } else return;
258
+ dispatchEdit(start, end, operation.replacement);
259
+ if (anchor) anchor.end += operation.replacement.length - (end - start);
260
+ break;
261
+ }
262
+ case "insert": {
263
+ let pos = view.state.selection.main.head;
264
+ if (typeof operation.position === "number") pos = operation.position;
265
+ else if (operation.position === "end") pos = text.length;
266
+ else if (operation.position === "start") pos = 0;
267
+ dispatchEdit(pos, pos, operation.text);
268
+ break;
269
+ }
270
+ case "delete": {
271
+ let start: number, end: number;
272
+ if (operation.range) {
273
+ start = operation.range.start; end = operation.range.end;
274
+ } else if (operation.targetText) {
275
+ const idx = text.indexOf(operation.targetText);
276
+ if (idx === -1) return;
277
+ start = idx; end = idx + operation.targetText.length;
278
+ } else return;
279
+ dispatchEdit(start, end, "");
280
+ if (anchor) anchor.end -= (end - start);
281
+ break;
282
+ }
283
+ case "replace_all":
284
+ view.dispatch({ changes: { from: 0, to: text.length, insert: operation.replacement } });
285
+ segments.clear();
286
+ if (anchor) anchor.end = operation.replacement.length;
287
+ break;
288
+ default:
289
+ console.warn("[codeMirrorBinding] Operation not supported:", operation.type);
290
+ }
291
+ },
292
+ };
293
+ }