arethesis 0.1.5 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -0
- package/README.md +5 -4
- package/dist/arethesis.js +2 -147
- package/dist/binding/data-binder.d.ts +8 -0
- package/dist/binding/data-binder.d.ts.map +1 -1
- package/dist/binding/data-binder.js +605 -0
- package/dist/binding/data-binder.js.map +1 -0
- package/dist/binding/types.d.ts +20 -0
- package/dist/binding/types.d.ts.map +1 -1
- package/dist/binding/validation-engine.js +160 -0
- package/dist/binding/validation-engine.js.map +1 -0
- package/dist/cdn/arethesis.min.js +2 -0
- package/dist/cdn/arethesis.min.js.map +1 -0
- package/dist/cdn/chunks/CdTL-MEv.min.js +3 -0
- package/dist/cdn/chunks/CdTL-MEv.min.js.map +1 -0
- package/dist/cdn/components.min.js +81 -0
- package/dist/cdn/components.min.js.map +1 -0
- package/dist/collab/index.d.ts +22 -0
- package/dist/collab/index.d.ts.map +1 -0
- package/dist/collab/index.js +23 -0
- package/dist/collab/index.js.map +1 -0
- package/dist/components/component.d.ts +17 -0
- package/dist/components/component.d.ts.map +1 -1
- package/dist/components/component.js +765 -0
- package/dist/components/component.js.map +1 -0
- package/dist/components/decorators.js +27 -0
- package/dist/components/decorators.js.map +1 -0
- package/dist/components/form-associated.d.ts +55 -0
- package/dist/components/form-associated.d.ts.map +1 -0
- package/dist/components/form-associated.js +180 -0
- package/dist/components/form-associated.js.map +1 -0
- package/dist/components/runtime.d.ts +2 -0
- package/dist/components/runtime.d.ts.map +1 -1
- package/dist/components/runtime.js +18 -0
- package/dist/components/runtime.js.map +1 -0
- package/dist/components/template-loader.js +90 -0
- package/dist/components/template-loader.js.map +1 -0
- package/dist/components.js +2 -606
- package/dist/core/arethesis.d.ts +13 -9
- package/dist/core/arethesis.d.ts.map +1 -1
- package/dist/core/arethesis.js +249 -0
- package/dist/core/arethesis.js.map +1 -0
- package/dist/core/hooks.d.ts +19 -0
- package/dist/core/hooks.d.ts.map +1 -0
- package/dist/core/hooks.js +23 -0
- package/dist/core/hooks.js.map +1 -0
- package/dist/core/index.d.ts +57 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +25 -0
- package/dist/data/list-view-registry.js +300 -0
- package/dist/data/list-view-registry.js.map +1 -0
- package/dist/data/object-store.js +280 -0
- package/dist/data/object-store.js.map +1 -0
- package/dist/data/optimistic-updates.js +168 -0
- package/dist/data/optimistic-updates.js.map +1 -0
- package/dist/data/persistence-manager.js +233 -0
- package/dist/data/persistence-manager.js.map +1 -0
- package/dist/defaults.d.ts +1 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +17 -0
- package/dist/defaults.js.map +1 -0
- package/dist/http/auth-manager.js +260 -0
- package/dist/http/auth-manager.js.map +1 -0
- package/dist/http/http-client.d.ts +7 -0
- package/dist/http/http-client.d.ts.map +1 -1
- package/dist/http/http-client.js +501 -0
- package/dist/http/http-client.js.map +1 -0
- package/dist/http/http-error.js +53 -0
- package/dist/http/http-error.js.map +1 -0
- package/dist/http/mapping.js +64 -0
- package/dist/http/mapping.js.map +1 -0
- package/dist/index.d.ts +3 -52
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +55 -0
- package/dist/kit/calendar.d.ts +43 -0
- package/dist/kit/calendar.d.ts.map +1 -0
- package/dist/kit/calendar.js +423 -0
- package/dist/kit/calendar.js.map +1 -0
- package/dist/kit/cell-editor.d.ts +47 -0
- package/dist/kit/cell-editor.d.ts.map +1 -0
- package/dist/kit/cell-editor.js +191 -0
- package/dist/kit/cell-editor.js.map +1 -0
- package/dist/kit/chart.d.ts +57 -0
- package/dist/kit/chart.d.ts.map +1 -0
- package/dist/kit/chart.js +541 -0
- package/dist/kit/chart.js.map +1 -0
- package/dist/kit/combobox.d.ts +57 -0
- package/dist/kit/combobox.d.ts.map +1 -0
- package/dist/kit/combobox.js +379 -0
- package/dist/kit/combobox.js.map +1 -0
- package/dist/kit/command-palette.d.ts +54 -0
- package/dist/kit/command-palette.d.ts.map +1 -0
- package/dist/kit/command-palette.js +315 -0
- package/dist/kit/command-palette.js.map +1 -0
- package/dist/kit/data-table.d.ts +17 -1
- package/dist/kit/data-table.d.ts.map +1 -1
- package/dist/kit/data-table.js +373 -0
- package/dist/kit/data-table.js.map +1 -0
- package/dist/kit/dialog.d.ts +31 -0
- package/dist/kit/dialog.d.ts.map +1 -0
- package/dist/kit/dialog.js +157 -0
- package/dist/kit/dialog.js.map +1 -0
- package/dist/kit/elements/calendar.d.ts +3 -0
- package/dist/kit/elements/calendar.d.ts.map +1 -0
- package/dist/kit/elements/calendar.js +11 -0
- package/dist/kit/elements/calendar.js.map +1 -0
- package/dist/kit/elements/chart.d.ts +3 -0
- package/dist/kit/elements/chart.d.ts.map +1 -0
- package/dist/kit/elements/chart.js +11 -0
- package/dist/kit/elements/chart.js.map +1 -0
- package/dist/kit/elements/combobox.d.ts +3 -0
- package/dist/kit/elements/combobox.d.ts.map +1 -0
- package/dist/kit/elements/combobox.js +11 -0
- package/dist/kit/elements/combobox.js.map +1 -0
- package/dist/kit/elements/command-palette.d.ts +3 -0
- package/dist/kit/elements/command-palette.d.ts.map +1 -0
- package/dist/kit/elements/command-palette.js +11 -0
- package/dist/kit/elements/command-palette.js.map +1 -0
- package/dist/kit/elements/data-table.d.ts +3 -0
- package/dist/kit/elements/data-table.d.ts.map +1 -0
- package/dist/kit/elements/data-table.js +11 -0
- package/dist/kit/elements/data-table.js.map +1 -0
- package/dist/kit/elements/dialog.d.ts +4 -0
- package/dist/kit/elements/dialog.d.ts.map +1 -0
- package/dist/kit/elements/dialog.js +11 -0
- package/dist/kit/elements/dialog.js.map +1 -0
- package/dist/kit/elements/flex-view.d.ts +3 -0
- package/dist/kit/elements/flex-view.d.ts.map +1 -0
- package/dist/kit/elements/flex-view.js +11 -0
- package/dist/kit/elements/flex-view.js.map +1 -0
- package/dist/kit/elements/form.d.ts +3 -0
- package/dist/kit/elements/form.d.ts.map +1 -0
- package/dist/kit/elements/form.js +11 -0
- package/dist/kit/elements/form.js.map +1 -0
- package/dist/kit/elements/kanban.d.ts +3 -0
- package/dist/kit/elements/kanban.d.ts.map +1 -0
- package/dist/kit/elements/kanban.js +11 -0
- package/dist/kit/elements/kanban.js.map +1 -0
- package/dist/kit/elements/list.d.ts +3 -0
- package/dist/kit/elements/list.d.ts.map +1 -0
- package/dist/kit/elements/list.js +11 -0
- package/dist/kit/elements/list.js.map +1 -0
- package/dist/kit/elements/markdown-editor.d.ts +3 -0
- package/dist/kit/elements/markdown-editor.d.ts.map +1 -0
- package/dist/kit/elements/markdown-editor.js +11 -0
- package/dist/kit/elements/markdown-editor.js.map +1 -0
- package/dist/kit/elements/presence.d.ts +3 -0
- package/dist/kit/elements/presence.d.ts.map +1 -0
- package/dist/kit/elements/presence.js +11 -0
- package/dist/kit/elements/presence.js.map +1 -0
- package/dist/kit/elements/sheet.d.ts +3 -0
- package/dist/kit/elements/sheet.d.ts.map +1 -0
- package/dist/kit/elements/sheet.js +11 -0
- package/dist/kit/elements/sheet.js.map +1 -0
- package/dist/kit/elements/tabs.d.ts +3 -0
- package/dist/kit/elements/tabs.d.ts.map +1 -0
- package/dist/kit/elements/tabs.js +11 -0
- package/dist/kit/elements/tabs.js.map +1 -0
- package/dist/kit/elements/toast.d.ts +4 -0
- package/dist/kit/elements/toast.d.ts.map +1 -0
- package/dist/kit/elements/toast.js +11 -0
- package/dist/kit/elements/toast.js.map +1 -0
- package/dist/kit/elements/tree.d.ts +3 -0
- package/dist/kit/elements/tree.d.ts.map +1 -0
- package/dist/kit/elements/tree.js +11 -0
- package/dist/kit/elements/tree.js.map +1 -0
- package/dist/kit/flex-view.d.ts +2 -0
- package/dist/kit/flex-view.d.ts.map +1 -1
- package/dist/kit/flex-view.js +346 -0
- package/dist/kit/flex-view.js.map +1 -0
- package/dist/kit/form.d.ts +2 -0
- package/dist/kit/form.d.ts.map +1 -1
- package/dist/kit/form.js +211 -0
- package/dist/kit/form.js.map +1 -0
- package/dist/kit/index.d.ts +33 -16
- package/dist/kit/index.d.ts.map +1 -1
- package/dist/kit/index.js +40 -0
- package/dist/kit/internal/modal.d.ts +26 -0
- package/dist/kit/internal/modal.d.ts.map +1 -0
- package/dist/kit/internal/modal.js +59 -0
- package/dist/kit/internal/modal.js.map +1 -0
- package/dist/kit/kanban.d.ts +73 -0
- package/dist/kit/kanban.d.ts.map +1 -0
- package/dist/kit/kanban.js +404 -0
- package/dist/kit/kanban.js.map +1 -0
- package/dist/kit/list.d.ts +4 -0
- package/dist/kit/list.d.ts.map +1 -1
- package/dist/kit/list.js +106 -0
- package/dist/kit/list.js.map +1 -0
- package/dist/kit/markdown/highlight.d.ts +29 -0
- package/dist/kit/markdown/highlight.d.ts.map +1 -0
- package/dist/kit/markdown/highlight.js +106 -0
- package/dist/kit/markdown/highlight.js.map +1 -0
- package/dist/kit/markdown/render.d.ts +31 -0
- package/dist/kit/markdown/render.d.ts.map +1 -0
- package/dist/kit/markdown/render.js +260 -0
- package/dist/kit/markdown/render.js.map +1 -0
- package/dist/kit/markdown-editor.d.ts +54 -0
- package/dist/kit/markdown-editor.d.ts.map +1 -0
- package/dist/kit/markdown-editor.js +797 -0
- package/dist/kit/markdown-editor.js.map +1 -0
- package/dist/kit/presence.d.ts +36 -0
- package/dist/kit/presence.d.ts.map +1 -0
- package/dist/kit/presence.js +262 -0
- package/dist/kit/presence.js.map +1 -0
- package/dist/kit/sheet/formula.d.ts +106 -0
- package/dist/kit/sheet/formula.d.ts.map +1 -0
- package/dist/kit/sheet/formula.js +364 -0
- package/dist/kit/sheet/formula.js.map +1 -0
- package/dist/kit/sheet/model.d.ts +28 -0
- package/dist/kit/sheet/model.d.ts.map +1 -0
- package/dist/kit/sheet/model.js +136 -0
- package/dist/kit/sheet/model.js.map +1 -0
- package/dist/kit/sheet/sheet.d.ts +47 -0
- package/dist/kit/sheet/sheet.d.ts.map +1 -0
- package/dist/kit/sheet/sheet.js +676 -0
- package/dist/kit/sheet/sheet.js.map +1 -0
- package/dist/kit/tabs.d.ts +17 -0
- package/dist/kit/tabs.d.ts.map +1 -0
- package/dist/kit/tabs.js +192 -0
- package/dist/kit/tabs.js.map +1 -0
- package/dist/kit/theme-boot.generated.d.ts +2 -0
- package/dist/kit/theme-boot.generated.d.ts.map +1 -1
- package/dist/kit/theme-boot.generated.js +8 -0
- package/dist/kit/theme-boot.generated.js.map +1 -0
- package/dist/kit/theme.d.ts +5 -3
- package/dist/kit/theme.d.ts.map +1 -1
- package/dist/kit/theme.js +89 -0
- package/dist/kit/theme.js.map +1 -0
- package/dist/kit/toast.d.ts +46 -0
- package/dist/kit/toast.d.ts.map +1 -0
- package/dist/kit/toast.js +154 -0
- package/dist/kit/toast.js.map +1 -0
- package/dist/kit/tree.d.ts +41 -0
- package/dist/kit/tree.d.ts.map +1 -0
- package/dist/kit/tree.js +422 -0
- package/dist/kit/tree.js.map +1 -0
- package/dist/mixins/context-menu.d.ts.map +1 -1
- package/dist/mixins/context-menu.js +183 -0
- package/dist/mixins/context-menu.js.map +1 -0
- package/dist/mixins/draggable.d.ts.map +1 -1
- package/dist/mixins/draggable.js +140 -0
- package/dist/mixins/draggable.js.map +1 -0
- package/dist/mixins/dropdown.d.ts.map +1 -1
- package/dist/mixins/dropdown.js +216 -0
- package/dist/mixins/dropdown.js.map +1 -0
- package/dist/mixins/editable.d.ts.map +1 -1
- package/dist/mixins/editable.js +175 -0
- package/dist/mixins/editable.js.map +1 -0
- package/dist/mixins/grid-navigation.d.ts.map +1 -1
- package/dist/mixins/grid-navigation.js +194 -0
- package/dist/mixins/grid-navigation.js.map +1 -0
- package/dist/mixins/index.d.ts +8 -2
- package/dist/mixins/index.d.ts.map +1 -1
- package/dist/mixins/index.js +38 -0
- package/dist/mixins/index.js.map +1 -0
- package/dist/mixins/infinite-scroll.d.ts.map +1 -1
- package/dist/mixins/infinite-scroll.js +83 -0
- package/dist/mixins/infinite-scroll.js.map +1 -0
- package/dist/mixins/listbox.d.ts +19 -0
- package/dist/mixins/listbox.d.ts.map +1 -0
- package/dist/mixins/listbox.js +208 -0
- package/dist/mixins/listbox.js.map +1 -0
- package/dist/mixins/popup.d.ts +36 -0
- package/dist/mixins/popup.d.ts.map +1 -0
- package/dist/mixins/popup.js +113 -0
- package/dist/mixins/popup.js.map +1 -0
- package/dist/mixins/registry.d.ts +6 -0
- package/dist/mixins/registry.d.ts.map +1 -1
- package/dist/mixins/registry.js +167 -0
- package/dist/mixins/registry.js.map +1 -0
- package/dist/mixins/resizable.d.ts.map +1 -1
- package/dist/mixins/resizable.js +128 -0
- package/dist/mixins/resizable.js.map +1 -0
- package/dist/mixins/sortable.d.ts.map +1 -1
- package/dist/mixins/sortable.js +511 -0
- package/dist/mixins/sortable.js.map +1 -0
- package/dist/mixins/tooltip.d.ts.map +1 -1
- package/dist/mixins/tooltip.js +80 -0
- package/dist/mixins/tooltip.js.map +1 -0
- package/dist/mixins/utils.js +105 -0
- package/dist/mixins/utils.js.map +1 -0
- package/dist/plugins/async-validators.js +118 -0
- package/dist/plugins/async-validators.js.map +1 -0
- package/dist/plugins/builtins.d.ts +1 -1
- package/dist/plugins/builtins.d.ts.map +1 -1
- package/dist/plugins/builtins.js +20 -0
- package/dist/plugins/builtins.js.map +1 -0
- package/dist/plugins/date-utils.js +128 -0
- package/dist/plugins/date-utils.js.map +1 -0
- package/dist/plugins/plugin-manager.d.ts.map +1 -1
- package/dist/plugins/plugin-manager.js +99 -0
- package/dist/plugins/plugin-manager.js.map +1 -0
- package/dist/plugins/rich-text.js +365 -0
- package/dist/plugins/rich-text.js.map +1 -0
- package/dist/realtime/azure-pubsub-manager.js +273 -0
- package/dist/realtime/azure-pubsub-manager.js.map +1 -0
- package/dist/realtime/collab/anchors.d.ts +10 -0
- package/dist/realtime/collab/anchors.d.ts.map +1 -0
- package/dist/realtime/collab/anchors.js +19 -0
- package/dist/realtime/collab/anchors.js.map +1 -0
- package/dist/realtime/collab/collab-session.d.ts +46 -1
- package/dist/realtime/collab/collab-session.d.ts.map +1 -1
- package/dist/realtime/collab/collab-session.js +396 -0
- package/dist/realtime/collab/collab-session.js.map +1 -0
- package/dist/realtime/collab/cursor-overlay.d.ts +10 -0
- package/dist/realtime/collab/cursor-overlay.d.ts.map +1 -1
- package/dist/realtime/collab/cursor-overlay.js +187 -0
- package/dist/realtime/collab/cursor-overlay.js.map +1 -0
- package/dist/realtime/collab/textarea-adapter.d.ts +13 -0
- package/dist/realtime/collab/textarea-adapter.d.ts.map +1 -1
- package/dist/realtime/collab/textarea-adapter.js +125 -0
- package/dist/realtime/collab/textarea-adapter.js.map +1 -0
- package/dist/realtime/collab/undo-manager.d.ts +44 -0
- package/dist/realtime/collab/undo-manager.d.ts.map +1 -0
- package/dist/realtime/collab/undo-manager.js +148 -0
- package/dist/realtime/collab/undo-manager.js.map +1 -0
- package/dist/realtime/index.d.ts +11 -0
- package/dist/realtime/index.d.ts.map +1 -0
- package/dist/realtime/index.js +22 -0
- package/dist/realtime/index.js.map +1 -0
- package/dist/realtime/ot/client.d.ts +1 -1
- package/dist/realtime/ot/client.d.ts.map +1 -1
- package/dist/realtime/ot/client.js +74 -0
- package/dist/realtime/ot/client.js.map +1 -0
- package/dist/realtime/ot/server.d.ts +13 -1
- package/dist/realtime/ot/server.d.ts.map +1 -1
- package/dist/realtime/ot/server.js +292 -0
- package/dist/realtime/ot/server.js.map +1 -0
- package/dist/realtime/ot/text-operation.js +282 -0
- package/dist/realtime/ot/text-operation.js.map +1 -0
- package/dist/realtime/reconnect.js +58 -0
- package/dist/realtime/reconnect.js.map +1 -0
- package/dist/realtime/sse-manager.js +172 -0
- package/dist/realtime/sse-manager.js.map +1 -0
- package/dist/realtime/types.d.ts +29 -1
- package/dist/realtime/types.d.ts.map +1 -1
- package/dist/realtime/websocket-manager.js +180 -0
- package/dist/realtime/websocket-manager.js.map +1 -0
- package/dist/theme-polychrome-boot.js +17 -0
- package/dist/theme-polychrome.css +310 -0
- package/dist/theme.css +490 -1
- package/dist/tokens/base.tokens.json +18 -0
- package/dist/tokens/dark-high-contrast.tokens.json +11 -0
- package/dist/tokens/dark.tokens.json +81 -0
- package/dist/tokens/light-high-contrast.tokens.json +11 -0
- package/dist/tokens/light.tokens.json +81 -0
- package/dist/tokens/polychrome.base.tokens.json +45 -0
- package/dist/tokens/polychrome.dark-high-contrast.tokens.json +183 -0
- package/dist/tokens/polychrome.dark.tokens.json +321 -0
- package/dist/tokens/polychrome.light-high-contrast.tokens.json +115 -0
- package/dist/tokens/polychrome.light.tokens.json +418 -0
- package/dist/tokens/polychrome.resolver.json +78 -0
- package/dist/utils/backoff.js +30 -0
- package/dist/utils/backoff.js.map +1 -0
- package/dist/utils/dates.js +67 -0
- package/dist/utils/dates.js.map +1 -0
- package/dist/utils/dom.js +30 -0
- package/dist/utils/dom.js.map +1 -0
- package/dist/utils/emitter.js +40 -0
- package/dist/utils/emitter.js.map +1 -0
- package/dist/utils/fuzzy.d.ts +23 -0
- package/dist/utils/fuzzy.d.ts.map +1 -0
- package/dist/utils/fuzzy.js +95 -0
- package/dist/utils/fuzzy.js.map +1 -0
- package/dist/utils/path.js +99 -0
- package/dist/utils/path.js.map +1 -0
- package/dist/utils/sanitize.js +161 -0
- package/dist/utils/sanitize.js.map +1 -0
- package/dist/utils/url.js +44 -0
- package/dist/utils/url.js.map +1 -0
- package/dist/utils/virtual-window.d.ts +25 -0
- package/dist/utils/virtual-window.d.ts.map +1 -0
- package/dist/utils/virtual-window.js +19 -0
- package/dist/utils/virtual-window.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +6 -0
- package/dist/version.js.map +1 -0
- package/package.json +63 -11
- package/dist/arethesis-jPH9QdQE.js +0 -5345
- package/dist/arethesis-jPH9QdQE.js.map +0 -1
- package/dist/arethesis.js.map +0 -1
- package/dist/components.js.map +0 -1
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { OTClient } from "../ot/client.js";
|
|
2
|
+
import { TextOperation } from "../ot/text-operation.js";
|
|
3
|
+
import { Emitter } from "../../utils/emitter.js";
|
|
4
|
+
import { TextareaAdapter } from "./textarea-adapter.js";
|
|
5
|
+
import { CursorOverlay } from "./cursor-overlay.js";
|
|
6
|
+
import { UndoManager, caretAfter } from "./undo-manager.js";
|
|
7
|
+
import { mapRange, transformRange } from "./anchors.js";
|
|
8
|
+
// src/realtime/collab/collab-session.ts
|
|
9
|
+
/**
|
|
10
|
+
* CollabSession: real-time collaborative editing of one document over the
|
|
11
|
+
* WebSocketManager, using operational transformation.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const session = app.collab.open('doc-42', textarea, { user: { name: 'Alice', color: '#e11d48' } });
|
|
15
|
+
* session.on('presence', cursors => renderAvatars(cursors));
|
|
16
|
+
* // later
|
|
17
|
+
* session.close();
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* The server side is `CollabServer` (see `realtime/ot/server`).
|
|
21
|
+
*
|
|
22
|
+
* @module realtime/collab/collab-session
|
|
23
|
+
*/
|
|
24
|
+
var CollabSession = class extends Emitter {
|
|
25
|
+
ws;
|
|
26
|
+
options;
|
|
27
|
+
docId;
|
|
28
|
+
channel;
|
|
29
|
+
client = null;
|
|
30
|
+
adapter;
|
|
31
|
+
overlay = null;
|
|
32
|
+
cursors = /* @__PURE__ */ new Map();
|
|
33
|
+
unsubscribe = null;
|
|
34
|
+
offOpen = null;
|
|
35
|
+
offStatus = null;
|
|
36
|
+
outstandingOpId = null;
|
|
37
|
+
opCounter = 0;
|
|
38
|
+
presenceTimer = null;
|
|
39
|
+
_status = "connecting";
|
|
40
|
+
/** The textarea's own readOnly, restored once the document has loaded. */
|
|
41
|
+
wasReadOnly = null;
|
|
42
|
+
history = null;
|
|
43
|
+
/** The content as of the last change, for computing undo steps. */
|
|
44
|
+
lastContent = "";
|
|
45
|
+
annotationMap = /* @__PURE__ */ new Map();
|
|
46
|
+
/** Annotation changes made while our own edits are in flight, sent once synchronized. */
|
|
47
|
+
pendingAnnotations = [];
|
|
48
|
+
clientId = null;
|
|
49
|
+
constructor(ws, docId, target, options = {}) {
|
|
50
|
+
super();
|
|
51
|
+
this.ws = ws;
|
|
52
|
+
this.options = options;
|
|
53
|
+
this.docId = docId;
|
|
54
|
+
this.channel = `doc:${docId}`;
|
|
55
|
+
this.adapter = target instanceof HTMLElement ? new TextareaAdapter(target) : target;
|
|
56
|
+
if (target instanceof HTMLTextAreaElement && options.cursors !== false) this.overlay = new CursorOverlay(target);
|
|
57
|
+
if (target instanceof HTMLElement) {
|
|
58
|
+
target.setAttribute("aria-busy", "true");
|
|
59
|
+
if (target instanceof HTMLTextAreaElement || target instanceof HTMLInputElement) {
|
|
60
|
+
this.wasReadOnly = target.readOnly;
|
|
61
|
+
target.readOnly = true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
this.adapter.attach((op) => this.handleLocalEdit(op), (selection) => this.queuePresence(selection));
|
|
65
|
+
if (options.undo) {
|
|
66
|
+
this.history = new UndoManager();
|
|
67
|
+
this.adapter.attachHistory?.(() => this.undo(), () => this.redo());
|
|
68
|
+
}
|
|
69
|
+
this.unsubscribe = ws.subscribe(this.channel, (message) => this.handleMessage(message));
|
|
70
|
+
this.offOpen = ws.onOpen(() => this.join());
|
|
71
|
+
this.offStatus = ws.onStatusChange((status) => {
|
|
72
|
+
if (status !== "open") this.setStatus("offline");
|
|
73
|
+
});
|
|
74
|
+
if (ws.isConnected) this.join();
|
|
75
|
+
else ws.connect();
|
|
76
|
+
}
|
|
77
|
+
get status() {
|
|
78
|
+
return this._status;
|
|
79
|
+
}
|
|
80
|
+
get content() {
|
|
81
|
+
return this.adapter.getValue();
|
|
82
|
+
}
|
|
83
|
+
get revision() {
|
|
84
|
+
return this.client?.revision ?? 0;
|
|
85
|
+
}
|
|
86
|
+
/** Remote collaborators. */
|
|
87
|
+
get peers() {
|
|
88
|
+
return Array.from(this.cursors.values());
|
|
89
|
+
}
|
|
90
|
+
/** Annotations (comments), in document order, with positions in the local document. */
|
|
91
|
+
get annotations() {
|
|
92
|
+
return [...this.annotationMap.values()].sort((a, b) => a.from - b.from || a.to - b.to);
|
|
93
|
+
}
|
|
94
|
+
/** Whether `undo()` / `redo()` would do anything (with `{ undo: true }`). */
|
|
95
|
+
get canUndo() {
|
|
96
|
+
return this.history?.canUndo ?? false;
|
|
97
|
+
}
|
|
98
|
+
get canRedo() {
|
|
99
|
+
return this.history?.canRedo ?? false;
|
|
100
|
+
}
|
|
101
|
+
/** Undo your last edit (needs `{ undo: true }`). Returns whether anything changed. */
|
|
102
|
+
undo() {
|
|
103
|
+
return this.applyHistory(this.history?.undo(this.adapter.getValue()) ?? null);
|
|
104
|
+
}
|
|
105
|
+
/** Redo your last undone edit (needs `{ undo: true }`). */
|
|
106
|
+
redo() {
|
|
107
|
+
return this.applyHistory(this.history?.redo(this.adapter.getValue()) ?? null);
|
|
108
|
+
}
|
|
109
|
+
applyHistory(op) {
|
|
110
|
+
if (!op || !this.client || !this.adapter.applyLocal) return false;
|
|
111
|
+
this.adapter.applyLocal(op, caretAfter(op));
|
|
112
|
+
this.commitLocal(op);
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Comment on the text from `from` to `to` (local positions). The comment
|
|
117
|
+
* appears (in the `annotations` event) once the server has stored it.
|
|
118
|
+
* Returns its id.
|
|
119
|
+
*/
|
|
120
|
+
annotate(from, to, body, id = `a-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`) {
|
|
121
|
+
this.sendAnnotation({
|
|
122
|
+
action: "add",
|
|
123
|
+
annotation: {
|
|
124
|
+
id,
|
|
125
|
+
from: Math.min(from, to),
|
|
126
|
+
to: Math.max(from, to),
|
|
127
|
+
body,
|
|
128
|
+
user: this.options.user,
|
|
129
|
+
createdAt: Date.now()
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return id;
|
|
133
|
+
}
|
|
134
|
+
/** Change an annotation: its body, `resolved`, `replies`, or its range (local positions). */
|
|
135
|
+
updateAnnotation(id, changes) {
|
|
136
|
+
this.sendAnnotation({
|
|
137
|
+
action: "update",
|
|
138
|
+
annotation: {
|
|
139
|
+
...changes,
|
|
140
|
+
id
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
removeAnnotation(id) {
|
|
145
|
+
this.sendAnnotation({
|
|
146
|
+
action: "remove",
|
|
147
|
+
annotation: { id }
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
sendAnnotation(payload) {
|
|
151
|
+
this.pendingAnnotations.push(payload);
|
|
152
|
+
this.flushAnnotations();
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Positions only mean something at a revision. While our own edits are in
|
|
156
|
+
* flight, the local document is ahead of every server revision, so wait
|
|
157
|
+
* until they're acknowledged.
|
|
158
|
+
*/
|
|
159
|
+
flushAnnotations() {
|
|
160
|
+
if (!this.client || this.client.state !== "synchronized" || !this.ws.isConnected) return;
|
|
161
|
+
for (const payload of this.pendingAnnotations.splice(0)) this.ws.send({
|
|
162
|
+
type: "collab:annotate",
|
|
163
|
+
channel: this.channel,
|
|
164
|
+
payload: {
|
|
165
|
+
...payload,
|
|
166
|
+
revision: this.client.revision
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
toLocalRange(a) {
|
|
171
|
+
const client = this.client;
|
|
172
|
+
return client ? mapRange(a, (i, stickLeft) => client.transformIndex(i, stickLeft)) : a;
|
|
173
|
+
}
|
|
174
|
+
emitAnnotations() {
|
|
175
|
+
this.emit("annotations", this.annotations);
|
|
176
|
+
}
|
|
177
|
+
setStatus(status) {
|
|
178
|
+
if (status === this._status) return;
|
|
179
|
+
this._status = status;
|
|
180
|
+
this.emit("status", status);
|
|
181
|
+
}
|
|
182
|
+
join() {
|
|
183
|
+
this.setStatus("connecting");
|
|
184
|
+
this.ws.send({
|
|
185
|
+
type: "collab:join",
|
|
186
|
+
channel: this.channel,
|
|
187
|
+
payload: {
|
|
188
|
+
user: this.options.user,
|
|
189
|
+
revision: this.client?.revision
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
handleMessage(message) {
|
|
194
|
+
const payload = message.payload ?? {};
|
|
195
|
+
switch (message.type) {
|
|
196
|
+
case "collab:init":
|
|
197
|
+
this.handleInit(payload);
|
|
198
|
+
break;
|
|
199
|
+
case "collab:op":
|
|
200
|
+
if (!this.client || payload.clientId === this.clientId) return;
|
|
201
|
+
this.client.applyServer(TextOperation.fromJSON(payload.op));
|
|
202
|
+
this.updateStatus();
|
|
203
|
+
break;
|
|
204
|
+
case "collab:ack":
|
|
205
|
+
if (!this.client || payload.opId && payload.opId !== this.outstandingOpId) return;
|
|
206
|
+
this.outstandingOpId = null;
|
|
207
|
+
this.client.serverAck();
|
|
208
|
+
this.updateStatus();
|
|
209
|
+
break;
|
|
210
|
+
case "collab:presence": {
|
|
211
|
+
const id = String(payload.clientId ?? "");
|
|
212
|
+
if (!id || id === this.clientId) return;
|
|
213
|
+
const existing = this.cursors.get(id);
|
|
214
|
+
const selection = payload.selection;
|
|
215
|
+
this.cursors.set(id, {
|
|
216
|
+
clientId: id,
|
|
217
|
+
user: payload.user ?? existing?.user,
|
|
218
|
+
selection: selection ? this.toLocal(selection) : existing?.selection
|
|
219
|
+
});
|
|
220
|
+
this.emitPresence();
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
case "collab:leave":
|
|
224
|
+
if (this.cursors.delete(String(payload.clientId))) this.emitPresence();
|
|
225
|
+
break;
|
|
226
|
+
case "collab:annotate": {
|
|
227
|
+
const { action, annotation } = payload;
|
|
228
|
+
if (!annotation?.id) return;
|
|
229
|
+
if (action === "remove") this.annotationMap.delete(annotation.id);
|
|
230
|
+
else this.annotationMap.set(annotation.id, this.toLocalRange(annotation));
|
|
231
|
+
this.emitAnnotations();
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
case "collab:error": this.emit("error", { message: String(payload.message ?? "Collaboration error") });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
handleInit(init) {
|
|
238
|
+
this.clientId = init.clientId;
|
|
239
|
+
if (!this.client || init.since === null || init.since === void 0 || !init.missed) {
|
|
240
|
+
this.client = new OTClient(init.revision, {
|
|
241
|
+
send: (revision, op) => this.sendOp(revision, op),
|
|
242
|
+
apply: (op) => this.applyRemote(op)
|
|
243
|
+
});
|
|
244
|
+
this.outstandingOpId = null;
|
|
245
|
+
this.lastContent = init.content;
|
|
246
|
+
this.history?.clear();
|
|
247
|
+
this.adapter.setValue(init.content);
|
|
248
|
+
this.emit("ready", {
|
|
249
|
+
content: init.content,
|
|
250
|
+
revision: init.revision
|
|
251
|
+
});
|
|
252
|
+
this.emit("change", {
|
|
253
|
+
content: init.content,
|
|
254
|
+
remote: true
|
|
255
|
+
});
|
|
256
|
+
} else {
|
|
257
|
+
for (const entry of init.missed) if (entry.opId && entry.opId === this.outstandingOpId) {
|
|
258
|
+
this.outstandingOpId = null;
|
|
259
|
+
this.client.serverAck();
|
|
260
|
+
} else this.client.applyServer(TextOperation.fromJSON(entry.op));
|
|
261
|
+
this.client.resend();
|
|
262
|
+
}
|
|
263
|
+
this.cursors.clear();
|
|
264
|
+
for (const peer of init.peers ?? []) this.cursors.set(peer.clientId, peer);
|
|
265
|
+
this.emitPresence();
|
|
266
|
+
this.annotationMap.clear();
|
|
267
|
+
for (const a of init.annotations ?? []) this.annotationMap.set(a.id, this.toLocalRange(a));
|
|
268
|
+
this.emitAnnotations();
|
|
269
|
+
if (this.adapter instanceof TextareaAdapter) {
|
|
270
|
+
this.adapter.element.removeAttribute("aria-busy");
|
|
271
|
+
if (this.wasReadOnly !== null) this.adapter.element.readOnly = this.wasReadOnly;
|
|
272
|
+
this.wasReadOnly = null;
|
|
273
|
+
}
|
|
274
|
+
this.updateStatus();
|
|
275
|
+
}
|
|
276
|
+
sendOp(revision, op) {
|
|
277
|
+
this.outstandingOpId = `${this.clientId ?? "c"}-${++this.opCounter}`;
|
|
278
|
+
this.ws.send({
|
|
279
|
+
type: "collab:op",
|
|
280
|
+
channel: this.channel,
|
|
281
|
+
payload: {
|
|
282
|
+
revision,
|
|
283
|
+
op: op.toJSON(),
|
|
284
|
+
opId: this.outstandingOpId
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
applyRemote(op) {
|
|
289
|
+
this.adapter.applyOperation(op);
|
|
290
|
+
this.lastContent = this.adapter.getValue();
|
|
291
|
+
this.history?.transform(op);
|
|
292
|
+
this.moveAnnotations(op);
|
|
293
|
+
for (const cursor of this.cursors.values()) if (cursor.selection) cursor.selection = {
|
|
294
|
+
anchor: op.transformIndex(cursor.selection.anchor),
|
|
295
|
+
head: op.transformIndex(cursor.selection.head)
|
|
296
|
+
};
|
|
297
|
+
this.emitPresence();
|
|
298
|
+
this.emit("change", {
|
|
299
|
+
content: this.adapter.getValue(),
|
|
300
|
+
remote: true
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
handleLocalEdit(op) {
|
|
304
|
+
if (!this.client) return;
|
|
305
|
+
this.history?.record(op, this.lastContent);
|
|
306
|
+
this.commitLocal(op);
|
|
307
|
+
}
|
|
308
|
+
/** A local edit (typed, or an undo/redo) that is already in the editor. */
|
|
309
|
+
commitLocal(op) {
|
|
310
|
+
if (!this.client) return;
|
|
311
|
+
this.lastContent = this.adapter.getValue();
|
|
312
|
+
this.client.applyClient(op);
|
|
313
|
+
for (const cursor of this.cursors.values()) if (cursor.selection) cursor.selection = {
|
|
314
|
+
anchor: op.transformIndex(cursor.selection.anchor),
|
|
315
|
+
head: op.transformIndex(cursor.selection.head)
|
|
316
|
+
};
|
|
317
|
+
this.moveAnnotations(op);
|
|
318
|
+
this.emitPresence();
|
|
319
|
+
this.updateStatus();
|
|
320
|
+
this.emit("change", {
|
|
321
|
+
content: this.adapter.getValue(),
|
|
322
|
+
remote: false
|
|
323
|
+
});
|
|
324
|
+
this.queuePresence(this.adapter.getSelection());
|
|
325
|
+
}
|
|
326
|
+
moveAnnotations(op) {
|
|
327
|
+
if (!this.annotationMap.size) return;
|
|
328
|
+
for (const [id, a] of this.annotationMap) this.annotationMap.set(id, transformRange(a, op));
|
|
329
|
+
this.emitAnnotations();
|
|
330
|
+
}
|
|
331
|
+
toLocal(selection) {
|
|
332
|
+
if (!this.client) return selection;
|
|
333
|
+
return {
|
|
334
|
+
anchor: this.client.transformIndex(selection.anchor),
|
|
335
|
+
head: this.client.transformIndex(selection.head)
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
queuePresence(selection) {
|
|
339
|
+
if (this.presenceTimer || !this.client) return;
|
|
340
|
+
this.presenceTimer = setTimeout(() => {
|
|
341
|
+
this.presenceTimer = null;
|
|
342
|
+
this.ws.send({
|
|
343
|
+
type: "collab:presence",
|
|
344
|
+
channel: this.channel,
|
|
345
|
+
payload: {
|
|
346
|
+
selection: this.adapter.getSelection() ?? selection,
|
|
347
|
+
user: this.options.user
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}, this.options.presenceInterval ?? 50);
|
|
351
|
+
}
|
|
352
|
+
emitPresence() {
|
|
353
|
+
const peers = this.peers;
|
|
354
|
+
this.overlay?.update(peers);
|
|
355
|
+
this.emit("presence", peers);
|
|
356
|
+
}
|
|
357
|
+
updateStatus() {
|
|
358
|
+
if (!this.ws.isConnected) this.setStatus("offline");
|
|
359
|
+
else this.setStatus(this.client?.state === "synchronized" ? "synced" : "syncing");
|
|
360
|
+
this.flushAnnotations();
|
|
361
|
+
}
|
|
362
|
+
/** Leave the document and clean up. */
|
|
363
|
+
close() {
|
|
364
|
+
if (this.presenceTimer) clearTimeout(this.presenceTimer);
|
|
365
|
+
if (this.ws.isConnected) this.ws.send({
|
|
366
|
+
type: "collab:leave",
|
|
367
|
+
channel: this.channel
|
|
368
|
+
});
|
|
369
|
+
this.unsubscribe?.();
|
|
370
|
+
this.offOpen?.();
|
|
371
|
+
this.offStatus?.();
|
|
372
|
+
if (this.adapter instanceof TextareaAdapter && this.wasReadOnly !== null) {
|
|
373
|
+
this.adapter.element.readOnly = this.wasReadOnly;
|
|
374
|
+
this.adapter.element.removeAttribute("aria-busy");
|
|
375
|
+
}
|
|
376
|
+
this.adapter.destroy();
|
|
377
|
+
this.overlay?.destroy();
|
|
378
|
+
this.clear();
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
/** Opens collaboration sessions on a WebSocketManager. */
|
|
382
|
+
var CollabManager = class {
|
|
383
|
+
getWs;
|
|
384
|
+
constructor(getWs) {
|
|
385
|
+
this.getWs = getWs;
|
|
386
|
+
}
|
|
387
|
+
/** Start collaborating on `docId` in a textarea/input (or a custom adapter). */
|
|
388
|
+
open(docId, target, options = {}) {
|
|
389
|
+
const ws = this.getWs();
|
|
390
|
+
if (!ws) throw new Error("Collaboration needs a WebSocket: pass `websocket` to Arethesis.init().");
|
|
391
|
+
return new CollabSession(ws, docId, target, options);
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
export { CollabManager, CollabSession };
|
|
395
|
+
|
|
396
|
+
//# sourceMappingURL=collab-session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collab-session.js","names":[],"sources":["../../../src/realtime/collab/collab-session.ts"],"sourcesContent":["/**\n * CollabSession: real-time collaborative editing of one document over the\n * WebSocketManager, using operational transformation.\n *\n * ```ts\n * const session = app.collab.open('doc-42', textarea, { user: { name: 'Alice', color: '#e11d48' } });\n * session.on('presence', cursors => renderAvatars(cursors));\n * // later\n * session.close();\n * ```\n *\n * The server side is `CollabServer` (see `realtime/ot/server`).\n *\n * @module realtime/collab/collab-session\n */\n\nimport { OTClient } from '../ot/client';\nimport { TextOperation } from '../ot/text-operation';\nimport type { WebSocketManager } from '../websocket-manager';\nimport type { AnnotatePayload, CollabAnnotation, CollabMessage, CollabSelection, CollabUser, RemoteCursor } from '../types';\nimport { Emitter } from '../../utils/emitter';\nimport { TextareaAdapter, type EditorAdapter } from './textarea-adapter';\nimport { CursorOverlay } from './cursor-overlay';\nimport { caretAfter, UndoManager } from './undo-manager';\nimport { mapRange, transformRange } from './anchors';\n\nexport type CollabStatus = 'connecting' | 'synced' | 'syncing' | 'offline';\n\nexport interface CollabEvents extends Record<string, unknown> {\n /** The document content changed (locally or remotely). */\n change: { content: string; remote: boolean };\n /** Remote collaborators and their selections. */\n presence: RemoteCursor[];\n status: CollabStatus;\n error: { message: string };\n /** Initial content arrived. */\n ready: { content: string; revision: number };\n /** Annotations (comments) changed: added, edited, removed, or moved by edits. */\n annotations: CollabAnnotation[];\n}\n\nexport interface CollabOptions {\n user?: CollabUser;\n /** Draw remote cursors over a textarea. @default true */\n cursors?: boolean;\n /** Presence update throttle in ms. @default 50 */\n presenceInterval?: number;\n /**\n * Per-user undo and redo: Ctrl/Cmd+Z (and Shift+Z, or Y) revert only your\n * own edits, even after other people's. Replaces the browser's undo, which\n * doesn't know about remote edits. @default false\n */\n undo?: boolean;\n}\n\ninterface InitPayload {\n content: string;\n revision: number;\n clientId: string;\n peers?: RemoteCursor[];\n since?: number | null;\n missed?: Array<{ op: unknown; opId?: string }>;\n annotations?: CollabAnnotation[];\n}\n\nexport class CollabSession extends Emitter<CollabEvents> {\n readonly docId: string;\n readonly channel: string;\n private client: OTClient | null = null;\n private adapter: EditorAdapter;\n private overlay: CursorOverlay | null = null;\n private cursors = new Map<string, RemoteCursor>();\n private unsubscribe: (() => void) | null = null;\n private offOpen: (() => void) | null = null;\n private offStatus: (() => void) | null = null;\n private outstandingOpId: string | null = null;\n private opCounter = 0;\n private presenceTimer: ReturnType<typeof setTimeout> | null = null;\n private _status: CollabStatus = 'connecting';\n /** The textarea's own readOnly, restored once the document has loaded. */\n private wasReadOnly: boolean | null = null;\n private history: UndoManager | null = null;\n /** The content as of the last change, for computing undo steps. */\n private lastContent = '';\n private annotationMap = new Map<string, CollabAnnotation>();\n /** Annotation changes made while our own edits are in flight, sent once synchronized. */\n private pendingAnnotations: AnnotatePayload[] = [];\n clientId: string | null = null;\n\n constructor(\n private ws: WebSocketManager,\n docId: string,\n target: HTMLTextAreaElement | HTMLInputElement | EditorAdapter,\n private options: CollabOptions = {}\n ) {\n super();\n this.docId = docId;\n this.channel = `doc:${docId}`;\n this.adapter = target instanceof HTMLElement ? new TextareaAdapter(target) : target;\n if (target instanceof HTMLTextAreaElement && options.cursors !== false) this.overlay = new CursorOverlay(target);\n if (target instanceof HTMLElement) {\n target.setAttribute('aria-busy', 'true');\n // Typing before the document has loaded would be overwritten by it: wait.\n if (target instanceof HTMLTextAreaElement || target instanceof HTMLInputElement) {\n this.wasReadOnly = target.readOnly;\n target.readOnly = true;\n }\n }\n\n this.adapter.attach(\n op => this.handleLocalEdit(op),\n selection => this.queuePresence(selection),\n );\n if (options.undo) {\n this.history = new UndoManager();\n this.adapter.attachHistory?.(() => this.undo(), () => this.redo());\n }\n this.unsubscribe = ws.subscribe<CollabMessage>(this.channel, message => this.handleMessage(message));\n this.offOpen = ws.onOpen(() => this.join());\n this.offStatus = ws.onStatusChange(status => {\n if (status !== 'open') this.setStatus('offline');\n });\n if (ws.isConnected) this.join();\n else void ws.connect();\n }\n\n get status(): CollabStatus {\n return this._status;\n }\n\n get content(): string {\n return this.adapter.getValue();\n }\n\n get revision(): number {\n return this.client?.revision ?? 0;\n }\n\n /** Remote collaborators. */\n get peers(): RemoteCursor[] {\n return Array.from(this.cursors.values());\n }\n\n /** Annotations (comments), in document order, with positions in the local document. */\n get annotations(): CollabAnnotation[] {\n return [...this.annotationMap.values()].sort((a, b) => a.from - b.from || a.to - b.to);\n }\n\n /** Whether `undo()` / `redo()` would do anything (with `{ undo: true }`). */\n get canUndo(): boolean {\n return this.history?.canUndo ?? false;\n }\n\n get canRedo(): boolean {\n return this.history?.canRedo ?? false;\n }\n\n /** Undo your last edit (needs `{ undo: true }`). Returns whether anything changed. */\n undo(): boolean {\n return this.applyHistory(this.history?.undo(this.adapter.getValue()) ?? null);\n }\n\n /** Redo your last undone edit (needs `{ undo: true }`). */\n redo(): boolean {\n return this.applyHistory(this.history?.redo(this.adapter.getValue()) ?? null);\n }\n\n private applyHistory(op: TextOperation | null): boolean {\n if (!op || !this.client || !this.adapter.applyLocal) return false;\n this.adapter.applyLocal(op, caretAfter(op));\n this.commitLocal(op);\n return true;\n }\n\n /**\n * Comment on the text from `from` to `to` (local positions). The comment\n * appears (in the `annotations` event) once the server has stored it.\n * Returns its id.\n */\n annotate(from: number, to: number, body: string, id = `a-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`): string {\n this.sendAnnotation({ action: 'add', annotation: { id, from: Math.min(from, to), to: Math.max(from, to), body, user: this.options.user, createdAt: Date.now() } });\n return id;\n }\n\n /** Change an annotation: its body, `resolved`, `replies`, or its range (local positions). */\n updateAnnotation(id: string, changes: Partial<Omit<CollabAnnotation, 'id'>>): void {\n this.sendAnnotation({ action: 'update', annotation: { ...changes, id } });\n }\n\n removeAnnotation(id: string): void {\n this.sendAnnotation({ action: 'remove', annotation: { id } });\n }\n\n private sendAnnotation(payload: AnnotatePayload): void {\n this.pendingAnnotations.push(payload);\n this.flushAnnotations();\n }\n\n /**\n * Positions only mean something at a revision. While our own edits are in\n * flight, the local document is ahead of every server revision, so wait\n * until they're acknowledged.\n */\n private flushAnnotations(): void {\n if (!this.client || this.client.state !== 'synchronized' || !this.ws.isConnected) return;\n for (const payload of this.pendingAnnotations.splice(0)) {\n this.ws.send({ type: 'collab:annotate', channel: this.channel, payload: { ...payload, revision: this.client.revision } });\n }\n }\n\n private toLocalRange(a: CollabAnnotation): CollabAnnotation {\n const client = this.client;\n return client ? mapRange(a, (i, stickLeft) => client.transformIndex(i, stickLeft)) : a;\n }\n\n private emitAnnotations(): void {\n this.emit('annotations', this.annotations);\n }\n\n private setStatus(status: CollabStatus): void {\n if (status === this._status) return;\n this._status = status;\n this.emit('status', status);\n }\n\n private join(): void {\n this.setStatus('connecting');\n this.ws.send({ type: 'collab:join', channel: this.channel, payload: { user: this.options.user, revision: this.client?.revision } });\n }\n\n private handleMessage(message: CollabMessage): void {\n const payload = (message.payload ?? {}) as Record<string, unknown>;\n switch (message.type) {\n case 'collab:init':\n this.handleInit(payload as unknown as InitPayload);\n break;\n case 'collab:op':\n if (!this.client || payload.clientId === this.clientId) return;\n this.client.applyServer(TextOperation.fromJSON(payload.op));\n this.updateStatus();\n break;\n case 'collab:ack':\n if (!this.client || (payload.opId && payload.opId !== this.outstandingOpId)) return;\n this.outstandingOpId = null;\n this.client.serverAck();\n this.updateStatus();\n break;\n case 'collab:presence': {\n const id = String(payload.clientId ?? '');\n if (!id || id === this.clientId) return;\n const existing = this.cursors.get(id);\n const selection = payload.selection as CollabSelection | undefined;\n this.cursors.set(id, {\n clientId: id,\n user: (payload.user as CollabUser | undefined) ?? existing?.user,\n selection: selection ? this.toLocal(selection) : existing?.selection,\n });\n this.emitPresence();\n break;\n }\n case 'collab:leave':\n if (this.cursors.delete(String(payload.clientId))) this.emitPresence();\n break;\n case 'collab:annotate': {\n const { action, annotation } = payload as unknown as AnnotatePayload;\n if (!annotation?.id) return;\n if (action === 'remove') this.annotationMap.delete(annotation.id);\n else this.annotationMap.set(annotation.id, this.toLocalRange(annotation as CollabAnnotation));\n this.emitAnnotations();\n break;\n }\n case 'collab:error':\n this.emit('error', { message: String(payload.message ?? 'Collaboration error') });\n break;\n }\n }\n\n private handleInit(init: InitPayload): void {\n this.clientId = init.clientId;\n if (!this.client || init.since === null || init.since === undefined || !init.missed) {\n // First sync (or the server could not replay): take the server's document.\n this.client = new OTClient(init.revision, {\n send: (revision, op) => this.sendOp(revision, op),\n apply: op => this.applyRemote(op),\n });\n this.outstandingOpId = null;\n this.lastContent = init.content;\n this.history?.clear();\n this.adapter.setValue(init.content);\n this.emit('ready', { content: init.content, revision: init.revision });\n this.emit('change', { content: init.content, remote: true });\n } else {\n // Reconnected: replay what we missed; our own op in the list counts as an ack.\n for (const entry of init.missed) {\n if (entry.opId && entry.opId === this.outstandingOpId) {\n this.outstandingOpId = null;\n this.client.serverAck();\n } else {\n this.client.applyServer(TextOperation.fromJSON(entry.op));\n }\n }\n this.client.resend();\n }\n this.cursors.clear();\n for (const peer of init.peers ?? []) this.cursors.set(peer.clientId, peer);\n this.emitPresence();\n this.annotationMap.clear();\n for (const a of init.annotations ?? []) this.annotationMap.set(a.id, this.toLocalRange(a));\n this.emitAnnotations();\n if (this.adapter instanceof TextareaAdapter) {\n this.adapter.element.removeAttribute('aria-busy');\n if (this.wasReadOnly !== null) this.adapter.element.readOnly = this.wasReadOnly;\n this.wasReadOnly = null;\n }\n this.updateStatus();\n }\n\n private sendOp(revision: number, op: TextOperation): void {\n this.outstandingOpId = `${this.clientId ?? 'c'}-${++this.opCounter}`;\n this.ws.send({ type: 'collab:op', channel: this.channel, payload: { revision, op: op.toJSON(), opId: this.outstandingOpId } });\n }\n\n private applyRemote(op: TextOperation): void {\n this.adapter.applyOperation(op);\n this.lastContent = this.adapter.getValue();\n this.history?.transform(op);\n this.moveAnnotations(op);\n for (const cursor of this.cursors.values()) {\n if (cursor.selection) {\n cursor.selection = { anchor: op.transformIndex(cursor.selection.anchor), head: op.transformIndex(cursor.selection.head) };\n }\n }\n this.emitPresence();\n this.emit('change', { content: this.adapter.getValue(), remote: true });\n }\n\n private handleLocalEdit(op: TextOperation): void {\n if (!this.client) return; // not synced yet; the init will overwrite\n this.history?.record(op, this.lastContent);\n this.commitLocal(op);\n }\n\n /** A local edit (typed, or an undo/redo) that is already in the editor. */\n private commitLocal(op: TextOperation): void {\n if (!this.client) return;\n this.lastContent = this.adapter.getValue();\n this.client.applyClient(op);\n for (const cursor of this.cursors.values()) {\n if (cursor.selection) {\n cursor.selection = { anchor: op.transformIndex(cursor.selection.anchor), head: op.transformIndex(cursor.selection.head) };\n }\n }\n this.moveAnnotations(op);\n this.emitPresence();\n this.updateStatus();\n this.emit('change', { content: this.adapter.getValue(), remote: false });\n this.queuePresence(this.adapter.getSelection());\n }\n\n private moveAnnotations(op: TextOperation): void {\n if (!this.annotationMap.size) return;\n for (const [id, a] of this.annotationMap) this.annotationMap.set(id, transformRange(a, op));\n this.emitAnnotations();\n }\n\n private toLocal(selection: CollabSelection): CollabSelection {\n if (!this.client) return selection;\n return { anchor: this.client.transformIndex(selection.anchor), head: this.client.transformIndex(selection.head) };\n }\n\n private queuePresence(selection: CollabSelection): void {\n if (this.presenceTimer || !this.client) return;\n this.presenceTimer = setTimeout(() => {\n this.presenceTimer = null;\n this.ws.send({ type: 'collab:presence', channel: this.channel, payload: { selection: this.adapter.getSelection() ?? selection, user: this.options.user } });\n }, this.options.presenceInterval ?? 50);\n }\n\n private emitPresence(): void {\n const peers = this.peers;\n this.overlay?.update(peers);\n this.emit('presence', peers);\n }\n\n private updateStatus(): void {\n if (!this.ws.isConnected) this.setStatus('offline');\n else this.setStatus(this.client?.state === 'synchronized' ? 'synced' : 'syncing');\n this.flushAnnotations();\n }\n\n /** Leave the document and clean up. */\n close(): void {\n if (this.presenceTimer) clearTimeout(this.presenceTimer);\n if (this.ws.isConnected) this.ws.send({ type: 'collab:leave', channel: this.channel });\n this.unsubscribe?.();\n this.offOpen?.();\n this.offStatus?.();\n if (this.adapter instanceof TextareaAdapter && this.wasReadOnly !== null) {\n this.adapter.element.readOnly = this.wasReadOnly;\n this.adapter.element.removeAttribute('aria-busy');\n }\n this.adapter.destroy();\n this.overlay?.destroy();\n this.clear();\n }\n}\n\n/** Opens collaboration sessions on a WebSocketManager. */\nexport class CollabManager {\n constructor(private getWs: () => WebSocketManager | null) {}\n\n /** Start collaborating on `docId` in a textarea/input (or a custom adapter). */\n open(docId: string, target: HTMLTextAreaElement | HTMLInputElement | EditorAdapter, options: CollabOptions = {}): CollabSession {\n const ws = this.getWs();\n if (!ws) throw new Error('Collaboration needs a WebSocket: pass `websocket` to Arethesis.init().');\n return new CollabSession(ws, docId, target, options);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiEA,IAAa,gBAAb,cAAmC,QAAsB;CAyB7C;CAGA;CA3BV;CACA;CACA,SAAkC;CAClC;CACA,UAAw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,KAAK,KAAK,MAAM;EACtB,IAAI,CAAC,IAAI,MAAM,IAAI,MAAM,wEAAwE;EACjG,OAAO,IAAI,cAAc,IAAI,OAAO,QAAQ,OAAO;CACrD;AACF"}
|
|
@@ -5,6 +5,16 @@ export declare function caretCoordinates(textarea: HTMLTextAreaElement | HTMLInp
|
|
|
5
5
|
left: number;
|
|
6
6
|
height: number;
|
|
7
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* The rectangles (one per visual line) covering the text from `from` to `to`
|
|
10
|
+
* in a textarea, relative to its border box, ignoring scrolling.
|
|
11
|
+
*/
|
|
12
|
+
export declare function rangeRects(textarea: HTMLTextAreaElement | HTMLInputElement, from: number, to: number): Array<{
|
|
13
|
+
top: number;
|
|
14
|
+
left: number;
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
}>;
|
|
8
18
|
export declare class CursorOverlay {
|
|
9
19
|
private textarea;
|
|
10
20
|
private layer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cursor-overlay.d.ts","sourceRoot":"","sources":["../../../src/realtime/collab/cursor-overlay.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"cursor-overlay.d.ts","sourceRoot":"","sources":["../../../src/realtime/collab/cursor-overlay.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAiB7C,0FAA0F;AAC1F,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,mBAAmB,GAAG,gBAAgB,EAAE,KAAK,EAAE,MAAM,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAe/I;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,mBAAmB,GAAG,gBAAgB,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,KAAK,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAgB1K;AAED,qBAAa,aAAa;IAMZ,OAAO,CAAC,QAAQ;IAL5B,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,UAAU,CAAyB;IAC3C,OAAO,CAAC,KAAK,CAAK;gBAEE,QAAQ,EAAE,mBAAmB,GAAG,gBAAgB;IAcpE,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,IAAI;IAKrC,OAAO,CAAC,QAAQ;IAQhB,OAAO,CAAC,MAAM;IAkCd,OAAO,IAAI,IAAI;CAKhB"}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// src/realtime/collab/cursor-overlay.ts
|
|
2
|
+
var MIRRORED = [
|
|
3
|
+
"boxSizing",
|
|
4
|
+
"width",
|
|
5
|
+
"height",
|
|
6
|
+
"overflowX",
|
|
7
|
+
"overflowY",
|
|
8
|
+
"borderTopWidth",
|
|
9
|
+
"borderRightWidth",
|
|
10
|
+
"borderBottomWidth",
|
|
11
|
+
"borderLeftWidth",
|
|
12
|
+
"paddingTop",
|
|
13
|
+
"paddingRight",
|
|
14
|
+
"paddingBottom",
|
|
15
|
+
"paddingLeft",
|
|
16
|
+
"fontStyle",
|
|
17
|
+
"fontVariant",
|
|
18
|
+
"fontWeight",
|
|
19
|
+
"fontStretch",
|
|
20
|
+
"fontSize",
|
|
21
|
+
"lineHeight",
|
|
22
|
+
"fontFamily",
|
|
23
|
+
"textAlign",
|
|
24
|
+
"textTransform",
|
|
25
|
+
"textIndent",
|
|
26
|
+
"letterSpacing",
|
|
27
|
+
"wordSpacing",
|
|
28
|
+
"tabSize",
|
|
29
|
+
"whiteSpace",
|
|
30
|
+
"wordBreak",
|
|
31
|
+
"overflowWrap"
|
|
32
|
+
];
|
|
33
|
+
function mirrorOf(textarea) {
|
|
34
|
+
const mirror = document.createElement("div");
|
|
35
|
+
const style = getComputedStyle(textarea);
|
|
36
|
+
for (const prop of MIRRORED) mirror.style[prop] = style[prop];
|
|
37
|
+
Object.assign(mirror.style, {
|
|
38
|
+
position: "absolute",
|
|
39
|
+
visibility: "hidden",
|
|
40
|
+
top: "0",
|
|
41
|
+
left: "-9999px",
|
|
42
|
+
whiteSpace: textarea instanceof HTMLInputElement ? "pre" : "pre-wrap",
|
|
43
|
+
wordWrap: "break-word",
|
|
44
|
+
overflow: "hidden"
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
mirror,
|
|
48
|
+
style
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Pixel position of a character index inside a textarea, relative to its padding box. */
|
|
52
|
+
function caretCoordinates(textarea, index) {
|
|
53
|
+
const { mirror, style } = mirrorOf(textarea);
|
|
54
|
+
mirror.textContent = textarea.value.slice(0, index);
|
|
55
|
+
const marker = document.createElement("span");
|
|
56
|
+
marker.textContent = textarea.value.slice(index) || ".";
|
|
57
|
+
mirror.appendChild(marker);
|
|
58
|
+
document.body.appendChild(mirror);
|
|
59
|
+
const lineHeight = parseFloat(style.lineHeight) || parseFloat(style.fontSize) * 1.2 || 16;
|
|
60
|
+
const result = {
|
|
61
|
+
top: marker.offsetTop + (parseFloat(style.borderTopWidth) || 0),
|
|
62
|
+
left: marker.offsetLeft + (parseFloat(style.borderLeftWidth) || 0),
|
|
63
|
+
height: lineHeight
|
|
64
|
+
};
|
|
65
|
+
mirror.remove();
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The rectangles (one per visual line) covering the text from `from` to `to`
|
|
70
|
+
* in a textarea, relative to its border box, ignoring scrolling.
|
|
71
|
+
*/
|
|
72
|
+
function rangeRects(textarea, from, to) {
|
|
73
|
+
const start = Math.max(0, Math.min(from, to));
|
|
74
|
+
const end = Math.min(textarea.value.length, Math.max(from, to));
|
|
75
|
+
if (start === end) return [];
|
|
76
|
+
const { mirror, style } = mirrorOf(textarea);
|
|
77
|
+
const span = document.createElement("span");
|
|
78
|
+
span.textContent = textarea.value.slice(start, end);
|
|
79
|
+
mirror.append(textarea.value.slice(0, start), span, textarea.value.slice(end) || ".");
|
|
80
|
+
document.body.appendChild(mirror);
|
|
81
|
+
const box = mirror.getBoundingClientRect();
|
|
82
|
+
const lineHeight = parseFloat(style.lineHeight) || parseFloat(style.fontSize) * 1.2 || 16;
|
|
83
|
+
const rects = Array.from(span.getClientRects()).filter((r) => r.width > .5).map((r) => ({
|
|
84
|
+
top: r.top - box.top + (r.height - lineHeight) / 2,
|
|
85
|
+
left: r.left - box.left,
|
|
86
|
+
width: r.width,
|
|
87
|
+
height: lineHeight
|
|
88
|
+
}));
|
|
89
|
+
mirror.remove();
|
|
90
|
+
return rects;
|
|
91
|
+
}
|
|
92
|
+
var CursorOverlay = class {
|
|
93
|
+
textarea;
|
|
94
|
+
layer;
|
|
95
|
+
cursors = [];
|
|
96
|
+
controller = new AbortController();
|
|
97
|
+
frame = 0;
|
|
98
|
+
constructor(textarea) {
|
|
99
|
+
this.textarea = textarea;
|
|
100
|
+
const parent = textarea.parentElement ?? document.body;
|
|
101
|
+
if (getComputedStyle(parent).position === "static") parent.style.position = "relative";
|
|
102
|
+
this.layer = document.createElement("div");
|
|
103
|
+
this.layer.className = "collab-cursors";
|
|
104
|
+
this.layer.setAttribute("aria-hidden", "true");
|
|
105
|
+
Object.assign(this.layer.style, {
|
|
106
|
+
position: "absolute",
|
|
107
|
+
pointerEvents: "none",
|
|
108
|
+
overflow: "hidden",
|
|
109
|
+
inset: "0"
|
|
110
|
+
});
|
|
111
|
+
parent.appendChild(this.layer);
|
|
112
|
+
const { signal } = this.controller;
|
|
113
|
+
textarea.addEventListener("scroll", () => this.schedule(), {
|
|
114
|
+
signal,
|
|
115
|
+
passive: true
|
|
116
|
+
});
|
|
117
|
+
textarea.addEventListener("input", () => this.schedule(), { signal });
|
|
118
|
+
window.addEventListener("resize", () => this.schedule(), { signal });
|
|
119
|
+
}
|
|
120
|
+
update(cursors) {
|
|
121
|
+
this.cursors = cursors;
|
|
122
|
+
this.schedule();
|
|
123
|
+
}
|
|
124
|
+
schedule() {
|
|
125
|
+
if (this.frame) return;
|
|
126
|
+
this.frame = requestAnimationFrame(() => {
|
|
127
|
+
this.frame = 0;
|
|
128
|
+
this.render();
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
render() {
|
|
132
|
+
const ta = this.textarea;
|
|
133
|
+
const offsetTop = ta.offsetTop - ta.scrollTop;
|
|
134
|
+
const offsetLeft = ta.offsetLeft - ta.scrollLeft;
|
|
135
|
+
this.layer.replaceChildren();
|
|
136
|
+
for (const cursor of this.cursors) {
|
|
137
|
+
if (!cursor.selection) continue;
|
|
138
|
+
const color = cursor.user?.color ?? "#7c3aed";
|
|
139
|
+
for (const r of rangeRects(ta, cursor.selection.anchor, cursor.selection.head)) {
|
|
140
|
+
const highlight = document.createElement("div");
|
|
141
|
+
highlight.className = "collab-selection";
|
|
142
|
+
Object.assign(highlight.style, {
|
|
143
|
+
position: "absolute",
|
|
144
|
+
top: `${offsetTop + r.top}px`,
|
|
145
|
+
left: `${offsetLeft + r.left}px`,
|
|
146
|
+
width: `${r.width}px`,
|
|
147
|
+
height: `${r.height}px`,
|
|
148
|
+
background: color,
|
|
149
|
+
opacity: "0.22"
|
|
150
|
+
});
|
|
151
|
+
this.layer.appendChild(highlight);
|
|
152
|
+
}
|
|
153
|
+
const pos = caretCoordinates(ta, Math.min(cursor.selection.head, ta.value.length));
|
|
154
|
+
const caret = document.createElement("div");
|
|
155
|
+
caret.className = "collab-cursor";
|
|
156
|
+
Object.assign(caret.style, {
|
|
157
|
+
position: "absolute",
|
|
158
|
+
top: `${offsetTop + pos.top}px`,
|
|
159
|
+
left: `${offsetLeft + pos.left}px`,
|
|
160
|
+
height: `${pos.height}px`,
|
|
161
|
+
width: "2px",
|
|
162
|
+
background: color
|
|
163
|
+
});
|
|
164
|
+
const label = document.createElement("span");
|
|
165
|
+
label.className = "collab-cursor__label";
|
|
166
|
+
label.textContent = cursor.user?.name ?? "Guest";
|
|
167
|
+
Object.assign(label.style, {
|
|
168
|
+
position: "absolute",
|
|
169
|
+
bottom: "100%",
|
|
170
|
+
left: "0",
|
|
171
|
+
background: color,
|
|
172
|
+
color: "#fff",
|
|
173
|
+
whiteSpace: "nowrap"
|
|
174
|
+
});
|
|
175
|
+
caret.appendChild(label);
|
|
176
|
+
this.layer.appendChild(caret);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
destroy() {
|
|
180
|
+
cancelAnimationFrame(this.frame);
|
|
181
|
+
this.controller.abort();
|
|
182
|
+
this.layer.remove();
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
export { CursorOverlay, caretCoordinates, rangeRects };
|
|
186
|
+
|
|
187
|
+
//# sourceMappingURL=cursor-overlay.js.map
|