kritzel-stencil 0.0.158 → 0.0.160
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/dist/cjs/{default-text-tool.config-BySzvIox.js → default-text-tool.config-D10FksvZ.js} +139 -50
- package/dist/cjs/default-text-tool.config-D10FksvZ.js.map +1 -0
- package/dist/cjs/index.cjs.js +1 -1
- package/dist/cjs/kritzel-color_22.cjs.entry.js +4 -21
- package/dist/collection/classes/handlers/resize.handler.js +46 -32
- package/dist/collection/classes/handlers/resize.handler.js.map +1 -1
- package/dist/collection/classes/objects/selection-group.class.js +91 -16
- package/dist/collection/classes/objects/selection-group.class.js.map +1 -1
- package/dist/collection/components/core/kritzel-engine/kritzel-engine.js +3 -20
- package/dist/collection/components/core/kritzel-engine/kritzel-engine.js.map +1 -1
- package/dist/components/index.js +2 -2
- package/dist/components/kritzel-editor.js +1 -1
- package/dist/components/kritzel-engine.js +1 -1
- package/dist/components/{p-DjTEcPMZ.js → p-DTHqEUDc.js} +142 -70
- package/dist/components/p-DTHqEUDc.js.map +1 -0
- package/dist/esm/{default-text-tool.config-2YFQA3SF.js → default-text-tool.config-DzqpOikl.js} +139 -50
- package/dist/esm/default-text-tool.config-DzqpOikl.js.map +1 -0
- package/dist/esm/index.js +2 -2
- package/dist/esm/kritzel-color_22.entry.js +4 -21
- package/dist/stencil/index.esm.js +1 -1
- package/dist/stencil/{p-14102a0c.entry.js → p-5475442e.entry.js} +3 -3
- package/dist/stencil/p-5475442e.entry.js.map +1 -0
- package/dist/stencil/{p-2YFQA3SF.js → p-DzqpOikl.js} +2 -2
- package/dist/stencil/p-DzqpOikl.js.map +1 -0
- package/dist/stencil/stencil.esm.js +1 -1
- package/dist/types/classes/objects/selection-group.class.d.ts +1 -0
- package/dist/types/components/core/kritzel-engine/kritzel-engine.d.ts +0 -1
- package/package.json +1 -1
- package/dist/cjs/default-text-tool.config-BySzvIox.js.map +0 -1
- package/dist/components/p-DjTEcPMZ.js.map +0 -1
- package/dist/esm/default-text-tool.config-2YFQA3SF.js.map +0 -1
- package/dist/stencil/p-14102a0c.entry.js.map +0 -1
- package/dist/stencil/p-2YFQA3SF.js.map +0 -1
package/dist/esm/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { w as writeVarUint, a as writeVarUint8Array, t as toUint8Array, r as readVarUint, b as readVarUint8Array, e as encodeStateAsUpdate, c as applyUpdate, d as encodeStateVector, f as createEncoder, g as createDecoder, s as setIfUndefined, h as create, i as fromBase64, v as varStorage, j as toBase64, o as onChange, k as createUint8ArrayFromArrayBuffer, l as offChange, m as readVarString, O as Observable, n as floor, p as getUnixTime, q as equalityDeep, u as writeVarString, x as map, y as ObservableV2, z as length, A as isNode, B as min, C as pow, H as HocuspocusProvider, D as HocuspocusProviderWebsocket } from './default-text-tool.config-
|
|
2
|
-
export { R as DEFAULT_BRUSH_CONFIG, S as DEFAULT_TEXT_CONFIG, N as IndexedDBSyncProvider, P as KritzelAppStateMap, G as KritzelBrushTool, I as KritzelEraserTool, F as KritzelImage, J as KritzelImageTool, E as KritzelPath, M as KritzelSelectionTool, K as KritzelText, L as KritzelTextTool, Q as KritzelWorkspace } from './default-text-tool.config-
|
|
1
|
+
import { w as writeVarUint, a as writeVarUint8Array, t as toUint8Array, r as readVarUint, b as readVarUint8Array, e as encodeStateAsUpdate, c as applyUpdate, d as encodeStateVector, f as createEncoder, g as createDecoder, s as setIfUndefined, h as create, i as fromBase64, v as varStorage, j as toBase64, o as onChange, k as createUint8ArrayFromArrayBuffer, l as offChange, m as readVarString, O as Observable, n as floor, p as getUnixTime, q as equalityDeep, u as writeVarString, x as map, y as ObservableV2, z as length, A as isNode, B as min, C as pow, H as HocuspocusProvider, D as HocuspocusProviderWebsocket } from './default-text-tool.config-DzqpOikl.js';
|
|
2
|
+
export { R as DEFAULT_BRUSH_CONFIG, S as DEFAULT_TEXT_CONFIG, N as IndexedDBSyncProvider, P as KritzelAppStateMap, G as KritzelBrushTool, I as KritzelEraserTool, F as KritzelImage, J as KritzelImageTool, E as KritzelPath, M as KritzelSelectionTool, K as KritzelText, L as KritzelTextTool, Q as KritzelWorkspace } from './default-text-tool.config-DzqpOikl.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* BroadcastChannel sync provider for cross-tab synchronization
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { r as registerInstance, h, H as Host, c as createEvent, g as getElement } from './index-SGde3HXB.js';
|
|
2
|
-
import { T as KritzelDevicesHelper, G as KritzelBrushTool, L as KritzelTextTool, U as KritzelMouseButton, M as KritzelSelectionTool, R as DEFAULT_BRUSH_CONFIG, I as KritzelEraserTool, S as DEFAULT_TEXT_CONFIG, J as KritzelImageTool, Q as KritzelWorkspace, V as KritzelKeyboardHelper, W as KritzelBaseHandler, X as KritzelToolRegistry, Y as KritzelSelectionBox, Z as KritzelSelectionGroup, _ as KritzelBaseObject, F as KritzelImage, K as KritzelText, E as KritzelPath, $ as Doc, a0 as DEFAULT_SYNC_CONFIG, a1 as UndoManager, P as KritzelAppStateMap, a2 as ObjectHelper, a3 as KritzelEventHelper, a4 as KritzelBaseTool } from './default-text-tool.config-
|
|
2
|
+
import { T as KritzelDevicesHelper, G as KritzelBrushTool, L as KritzelTextTool, U as KritzelMouseButton, M as KritzelSelectionTool, R as DEFAULT_BRUSH_CONFIG, I as KritzelEraserTool, S as DEFAULT_TEXT_CONFIG, J as KritzelImageTool, Q as KritzelWorkspace, V as KritzelKeyboardHelper, W as KritzelBaseHandler, X as KritzelToolRegistry, Y as KritzelSelectionBox, Z as KritzelSelectionGroup, _ as KritzelBaseObject, F as KritzelImage, K as KritzelText, E as KritzelPath, $ as Doc, a0 as DEFAULT_SYNC_CONFIG, a1 as UndoManager, P as KritzelAppStateMap, a2 as ObjectHelper, a3 as KritzelEventHelper, a4 as KritzelBaseTool } from './default-text-tool.config-DzqpOikl.js';
|
|
3
3
|
|
|
4
4
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
5
5
|
|
|
@@ -19782,7 +19782,6 @@ const KritzelEngine = class {
|
|
|
19782
19782
|
contextMenuHandler;
|
|
19783
19783
|
keyHandler;
|
|
19784
19784
|
contextMenuElement = null;
|
|
19785
|
-
visibleObjectsTimings = [];
|
|
19786
19785
|
get isSelecting() {
|
|
19787
19786
|
return this.core.store.state.activeTool instanceof KritzelSelectionTool && this.core.store.state.isSelecting;
|
|
19788
19787
|
}
|
|
@@ -19859,7 +19858,6 @@ const KritzelEngine = class {
|
|
|
19859
19858
|
const baseHandleTouchSize = baseHandleSize * 2 < 14 ? 14 : baseHandleSize;
|
|
19860
19859
|
const viewportCenterX = this.core.store.state.viewportWidth / 2 + this.core.store.state.translateX;
|
|
19861
19860
|
const viewportCenterY = this.core.store.state.viewportHeight / 2 + this.core.store.state.translateY;
|
|
19862
|
-
const startTime = performance.now();
|
|
19863
19861
|
const viewportBounds = {
|
|
19864
19862
|
x: -this.core.store.state.translateX / this.core.store.state.scale,
|
|
19865
19863
|
y: -this.core.store.state.translateY / this.core.store.state.scale,
|
|
@@ -19869,22 +19867,7 @@ const KritzelEngine = class {
|
|
|
19869
19867
|
depth: 100,
|
|
19870
19868
|
};
|
|
19871
19869
|
const visibleObjects = this.core.store.state.objects.query(viewportBounds);
|
|
19872
|
-
|
|
19873
|
-
const elapsed = endTime - startTime;
|
|
19874
|
-
this.visibleObjectsTimings.push(elapsed);
|
|
19875
|
-
if (this.visibleObjectsTimings.length > 25) {
|
|
19876
|
-
this.visibleObjectsTimings.shift();
|
|
19877
|
-
}
|
|
19878
|
-
if (this.visibleObjectsTimings.length === 25) {
|
|
19879
|
-
const avg = this.visibleObjectsTimings.reduce((a, b) => a + b, 0) / 25;
|
|
19880
|
-
const min = Math.min(...this.visibleObjectsTimings);
|
|
19881
|
-
const max = Math.max(...this.visibleObjectsTimings);
|
|
19882
|
-
console.log(`[Performance] Visible objects over 25 calls - Avg: ${avg.toFixed(2)}ms, Min: ${min.toFixed(2)}ms, Max: ${max.toFixed(2)}ms, Current: ${elapsed.toFixed(2)}ms (${visibleObjects.length} objects)`);
|
|
19883
|
-
}
|
|
19884
|
-
else {
|
|
19885
|
-
console.log(`[Performance] Getting visible objects took ${elapsed.toFixed(2)}ms (${visibleObjects.length} objects visible) [${this.visibleObjectsTimings.length}/25 samples]`);
|
|
19886
|
-
}
|
|
19887
|
-
return (h(Host, { key: 'f59e309c44cf9d056c17df5bae809227b0da6014' }, this.core.store.state.debugInfo.showViewportInfo && (h("div", { key: '2e22b7eb72f8eea9b7aafb5b54d82039931fb1dd', class: "debug-panel" }, h("div", { key: 'd003aff63c05fa5889ae73448c603e4461bdcb68' }, "ActiveWorkspaceId: ", this.core.store.state?.activeWorkspace?.id), h("div", { key: '62c6c30e81f284904e159b8abbc643527cd760ee' }, "ActiveWorkspaceName: ", this.core.store.state?.activeWorkspace?.name), h("div", { key: 'e6e80882c725104d52fab8da9a8b1f9e52c28202' }, "TranslateX: ", this.core.store.state?.translateX), h("div", { key: 'c88da5777f20f6b93f22cd78d624bd3673fa3fbd' }, "TranslateY: ", this.core.store.state?.translateY), h("div", { key: '19d293c9e537961dc0dd47ff138608eccc84b9c0' }, "ViewportWidth: ", this.core.store.state?.viewportWidth), h("div", { key: '747c1d0b46a918aafa8da3bff094d08b321ab638' }, "ViewportHeight: ", this.core.store.state?.viewportHeight), h("div", { key: 'a0a89524a87a8a5a438077c77c53c63bfbc02358' }, "PointerCount: ", this.core.store.state.pointers.size), h("div", { key: 'b4b385704c802a78cd959998cff0bf61dad6a54c' }, "Scale: ", this.core.store.state?.scale), h("div", { key: '8933716cedc4d45092e6810adb8fbad1a1c80af3' }, "ActiveTool: ", this.core.store.state?.activeTool?.name), h("div", { key: '407c5a3ca4b73e13f2f11e36b20792231e512e43' }, "HasViewportChanged: ", this.core.store.state?.hasViewportChanged ? 'true' : 'false'), h("div", { key: '8c81eccee3d1b13ded8cb3e47a275df585cfb276' }, "IsEnabled: ", this.core.store.state?.isEnabled ? 'true' : 'false'), h("div", { key: '8af988839b1712024a692d554021c9a6b5c95ea1' }, "IsScaling: ", this.core.store.state?.isScaling ? 'true' : 'false'), h("div", { key: '6b29e356911a7f6a8b7ec349323974c0160c99a9' }, "IsPanning: ", this.core.store.state?.isPanning ? 'true' : 'false'), h("div", { key: 'd47293ba022498e1e91d3b44d668bb5b09120cc0' }, "IsSelecting: ", this.isSelecting ? 'true' : 'false'), h("div", { key: '135e1930f98d56018367fb2653a4564b4b01ea5a' }, "IsSelectionActive: ", this.isSelectionActive ? 'true' : 'false'), h("div", { key: 'bccac5527134ed4eb2308c96a056bad7ad47b479' }, "IsResizeHandleSelected: ", this.core.store.state.isResizeHandleSelected ? 'true' : 'false'), h("div", { key: '8a18e170e35f1a7cf889b6c9fcb44a2ea9b4b73e' }, "IsRotationHandleSelected: ", this.core.store.state.isRotationHandleSelected ? 'true' : 'false'), h("div", { key: '88e357566ef307d1c274a75994de95f1682497b4' }, "IsDrawing: ", this.core.store.state.isDrawing ? 'true' : 'false'), h("div", { key: '48cefe71eb0c4676f9a708a6857500b68f5e5e6c' }, "IsWriting: ", this.core.store.state.isWriting ? 'true' : 'false'), h("div", { key: '0646a93553cb9ca21f27c876314104efec7f7b4d' }, "PointerX: ", this.core.store.state?.pointerX), h("div", { key: '18cf01af3a33b87f26215921f8d67cae13743f68' }, "PointerY: ", this.core.store.state?.pointerY), h("div", { key: '3a250d4b06489eeea5a8765675bab0f1bad358ce' }, "SelectedObjects: ", this.core.store.selectionGroup?.objects.length || 0), h("div", { key: 'c5f9bc07eb57de8de922efcf2646ace75fb48a7e' }, "ViewportCenter: (", viewportCenterX.toFixed(2), ", ", viewportCenterY.toFixed(2), ")"))), h("div", { key: '25d849d5d92785d0545fcd54d0d94bbf5713b6b4', id: "origin", class: "origin", style: {
|
|
19870
|
+
return (h(Host, { key: '1a76bcddde5f1aa68dd20a73af61f6f89ab08c78' }, this.core.store.state.debugInfo.showViewportInfo && (h("div", { key: '9be109e44187d7d2072e115533b3c58a36351993', class: "debug-panel" }, h("div", { key: 'de16ca51bc04f851ef65a9a937829916041abd1c' }, "ActiveWorkspaceId: ", this.core.store.state?.activeWorkspace?.id), h("div", { key: '299b05d4091d6c883ee637207e242e3fdb93f19c' }, "ActiveWorkspaceName: ", this.core.store.state?.activeWorkspace?.name), h("div", { key: '9ef3f04b7fcb42ec87d8b7e6b9f5e914918fd780' }, "TranslateX: ", this.core.store.state?.translateX), h("div", { key: 'cd920e869be5112c7dbe7d494f9fa06902c3d30a' }, "TranslateY: ", this.core.store.state?.translateY), h("div", { key: 'd8d28322f8cc2b5cf9a3da2c97c91e3115f53ddc' }, "ViewportWidth: ", this.core.store.state?.viewportWidth), h("div", { key: 'e8d920e10b020b359533682452f76489bc8e9f2d' }, "ViewportHeight: ", this.core.store.state?.viewportHeight), h("div", { key: '768280f7bf01703c9f6ea22b06b89fe7130006a4' }, "PointerCount: ", this.core.store.state.pointers.size), h("div", { key: 'ff0f5e860a9987abaf8c81b95d1476af19d9b186' }, "Scale: ", this.core.store.state?.scale), h("div", { key: 'a672fbf63576a1776f4d3960d9f36493a4308a79' }, "ActiveTool: ", this.core.store.state?.activeTool?.name), h("div", { key: 'd562bff5d582e3df38e0a62229c8c7b0700b9fa0' }, "HasViewportChanged: ", this.core.store.state?.hasViewportChanged ? 'true' : 'false'), h("div", { key: 'd6b2646e7fd0abb5d39928c51ad340ccf306db01' }, "IsEnabled: ", this.core.store.state?.isEnabled ? 'true' : 'false'), h("div", { key: '453e3d3691aad984895241734c70de61cbcd60ec' }, "IsScaling: ", this.core.store.state?.isScaling ? 'true' : 'false'), h("div", { key: '156668df87ea5001c7348c1c141f1c8601fe23da' }, "IsPanning: ", this.core.store.state?.isPanning ? 'true' : 'false'), h("div", { key: '86f956f35a84961c17685ebfd7cb73e92f04cd45' }, "IsSelecting: ", this.isSelecting ? 'true' : 'false'), h("div", { key: '73581c5791434983308d49e5c41f82836729a75d' }, "IsSelectionActive: ", this.isSelectionActive ? 'true' : 'false'), h("div", { key: '23f3eef374052545b7e99b0cb58a00011b421188' }, "IsResizeHandleSelected: ", this.core.store.state.isResizeHandleSelected ? 'true' : 'false'), h("div", { key: '51842e9f6e95413e6defbe6f81f43c1c7559be3e' }, "IsRotationHandleSelected: ", this.core.store.state.isRotationHandleSelected ? 'true' : 'false'), h("div", { key: 'b835c25cb75bc6f6d29a71ba25b18735f60319f8' }, "IsDrawing: ", this.core.store.state.isDrawing ? 'true' : 'false'), h("div", { key: '9a9740f44f06d57f2f9a1fce94ca2330a9c14e29' }, "IsWriting: ", this.core.store.state.isWriting ? 'true' : 'false'), h("div", { key: '9b97322077152d4fbcb98bc650ee7d0555f8d06d' }, "PointerX: ", this.core.store.state?.pointerX), h("div", { key: '9eddc79647458ca7b72e3241709fc36276587159' }, "PointerY: ", this.core.store.state?.pointerY), h("div", { key: '426ac2bf30669cc79beda5c2e5136eba5c29c578' }, "SelectedObjects: ", this.core.store.selectionGroup?.objects.length || 0), h("div", { key: '8544b6e8aaeacee60fe9597ec13af11c938eaacf' }, "ViewportCenter: (", viewportCenterX.toFixed(2), ", ", viewportCenterY.toFixed(2), ")"))), h("div", { key: '5101eecff40329a93b5ac6eb09830f1067ebaa90', id: "origin", class: "origin", style: {
|
|
19888
19871
|
transform: `matrix(${this.core.store.state?.scale}, 0, 0, ${this.core.store.state?.scale}, ${this.core.store.state?.translateX}, ${this.core.store.state?.translateY})`,
|
|
19889
19872
|
} }, visibleObjects?.map(object => {
|
|
19890
19873
|
return (h("div", { key: object.id, style: {
|
|
@@ -20011,7 +19994,7 @@ const KritzelEngine = class {
|
|
|
20011
19994
|
cursor: 'grab',
|
|
20012
19995
|
paintOrder: 'fill',
|
|
20013
19996
|
} }))))));
|
|
20014
|
-
})), this.core.store.state.isContextMenuVisible && (h("kritzel-context-menu", { key: '
|
|
19997
|
+
})), this.core.store.state.isContextMenuVisible && (h("kritzel-context-menu", { key: '8270193062df51aa5e358bd31c0851f54e4644d1', class: "context-menu", ref: el => (this.contextMenuElement = el), items: this.core.store.state.contextMenuItems, objects: this.core.store.selectionGroup?.objects || [], style: {
|
|
20015
19998
|
position: 'fixed',
|
|
20016
19999
|
left: `${this.core.store.state.contextMenuX}px`,
|
|
20017
20000
|
top: `${this.core.store.state.contextMenuY}px`,
|
|
@@ -20022,7 +20005,7 @@ const KritzelEngine = class {
|
|
|
20022
20005
|
y: (-this.core.store.state.translateY + this.core.store.state.contextMenuY) / this.core.store.state.scale,
|
|
20023
20006
|
}, this.core.store.selectionGroup?.objects);
|
|
20024
20007
|
this.hideContextMenu();
|
|
20025
|
-
}, onClose: () => this.hideContextMenu() })), this.core.store.state?.activeTool instanceof KritzelEraserTool && !this.core.store.state.isScaling && h("kritzel-cursor-trail", { key: '
|
|
20008
|
+
}, onClose: () => this.hideContextMenu() })), this.core.store.state?.activeTool instanceof KritzelEraserTool && !this.core.store.state.isScaling && h("kritzel-cursor-trail", { key: '7d199fcf652dee0cd12fff8a558b455c361e019f', core: this.core })));
|
|
20026
20009
|
}
|
|
20027
20010
|
static get watchers() { return {
|
|
20028
20011
|
"workspace": ["onWorkspaceChange"],
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{w as s,a as t,t as e,r as i,b as n,e as o,c,d as a,f as h,g as r,s as l,h as d,i as u,v as f,j as p,o as m,k as y,l as w,m as k,O as b,n as v,p as T,q as g,u as W,x as $,y as x,z,A as B,B as C,C as U,H,D as A}from"./p-2YFQA3SF.js";export{R as DEFAULT_BRUSH_CONFIG,S as DEFAULT_TEXT_CONFIG,N as IndexedDBSyncProvider,P as KritzelAppStateMap,G as KritzelBrushTool,I as KritzelEraserTool,F as KritzelImage,J as KritzelImageTool,E as KritzelPath,M as KritzelSelectionTool,K as KritzelText,L as KritzelTextTool,Q as KritzelWorkspace}from"./p-2YFQA3SF.js";class D{doc;channel;_synced=false;constructor(s,t,e){this.doc=t;this.channel=new BroadcastChannel(s);this.channel.onmessage=s=>{this.handleMessage(s.data)};this.doc.on("update",this.handleDocUpdate);this.broadcastSync();setTimeout((()=>{this._synced=true}),100);console.info(`BroadcastChannel Provider initialized: ${s}`)}handleDocUpdate=(i,n)=>{if(n!==this){const n=h();s(n,0);t(n,i);this.channel.postMessage(e(n))}};handleMessage(a){const l=r(new Uint8Array(a));const d=i(l);switch(d){case 0:const i=n(l);c(this.doc,i,this);break;case 1:this.broadcastSync();break;case 2:const a=n(l);const r=o(this.doc,a);if(r.length>0){const i=h();s(i,0);t(i,r);this.channel.postMessage(e(i))}break}}broadcastSync(){const i=h();s(i,2);t(i,a(this.doc));this.channel.postMessage(e(i))}async connect(){if(this._synced){return}return new Promise((s=>{const t=()=>{if(this._synced){s()}else{setTimeout(t,50)}};t()}))}disconnect(){}destroy(){this.doc.off("update",this.handleDocUpdate);this.channel.close()}}const O=new Map;class _{constructor(s){this.room=s;this.onmessage=null;this._onChange=t=>t.key===s&&this.onmessage!==null&&this.onmessage({data:u(t.newValue||"")});m(this._onChange)}postMessage(s){f.setItem(this.room,p(y(s)))}close(){w(this._onChange)}}const j=typeof BroadcastChannel==="undefined"?_:BroadcastChannel;const Y=s=>l(O,s,(()=>{const t=d();const e=new j(s);e.onmessage=s=>t.forEach((t=>t(s.data,"broadcastchannel")));return{bc:e,subs:t}}));const q=(s,t)=>{Y(s).subs.add(t);return t};const X=(s,t)=>{const e=Y(s);const i=e.subs.delete(t);if(i&&e.subs.size===0){e.bc.close();O.delete(s)}return i};const V=(s,t,e=null)=>{const i=Y(s);i.bc.postMessage(t);i.subs.forEach((s=>s(t,e)))};const Z=0;const ss=1;const ts=2;const es=(e,i)=>{s(e,Z);const n=a(i);t(e,n)};const is=(e,i,n)=>{s(e,ss);t(e,o(i,n))};const ns=(s,t,e)=>is(t,e,n(s));const os=(s,t,e)=>{try{c(t,n(s),e)}catch(s){console.error("Caught error while handling a Yjs update",s)}};const cs=(e,i)=>{s(e,ts);t(e,i)};const as=os;const hs=(s,t,e,n)=>{const o=i(s);switch(o){case Z:ns(s,t,e);break;case ss:os(s,e,n);break;case ts:as(s,e,n);break;default:throw new Error("Unknown message type")}return o};const rs=0;const ls=(s,t,e)=>{switch(i(s)){case rs:e(t,k(s))}};const ds=3e4;class us extends b{constructor(s){super();this.doc=s;this.clientID=s.clientID;this.states=new Map;this.meta=new Map;this._checkInterval=setInterval((()=>{const s=T();if(this.getLocalState()!==null&&ds/2<=s-this.meta.get(this.clientID).lastUpdated){this.setLocalState(this.getLocalState())}const t=[];this.meta.forEach(((e,i)=>{if(i!==this.clientID&&ds<=s-e.lastUpdated&&this.states.has(i)){t.push(i)}}));if(t.length>0){fs(this,t,"timeout")}}),v(ds/10));s.on("destroy",(()=>{this.destroy()}));this.setLocalState({})}destroy(){this.emit("destroy",[this]);this.setLocalState(null);super.destroy();clearInterval(this._checkInterval)}getLocalState(){return this.states.get(this.clientID)||null}setLocalState(s){const t=this.clientID;const e=this.meta.get(t);const i=e===undefined?0:e.clock+1;const n=this.states.get(t);if(s===null){this.states.delete(t)}else{this.states.set(t,s)}this.meta.set(t,{clock:i,lastUpdated:T()});const o=[];const c=[];const a=[];const h=[];if(s===null){h.push(t)}else if(n==null){if(s!=null){o.push(t)}}else{c.push(t);if(!g(n,s)){a.push(t)}}if(o.length>0||a.length>0||h.length>0){this.emit("change",[{added:o,updated:a,removed:h},"local"])}this.emit("update",[{added:o,updated:c,removed:h},"local"])}setLocalStateField(s,t){const e=this.getLocalState();if(e!==null){this.setLocalState({...e,[s]:t})}}getStates(){return this.states}}const fs=(s,t,e)=>{const i=[];for(let e=0;e<t.length;e++){const n=t[e];if(s.states.has(n)){s.states.delete(n);if(n===s.clientID){const t=s.meta.get(n);s.meta.set(n,{clock:t.clock+1,lastUpdated:T()})}i.push(n)}}if(i.length>0){s.emit("change",[{added:[],updated:[],removed:i},e]);s.emit("update",[{added:[],updated:[],removed:i},e])}};const ps=(t,i,n=t.states)=>{const o=i.length;const c=h();s(c,o);for(let e=0;e<o;e++){const o=i[e];const a=n.get(o)||null;const h=t.meta.get(o).clock;s(c,o);s(c,h);W(c,JSON.stringify(a))}return e(c)};const ms=(s,t,e)=>{const n=r(t);const o=T();const c=[];const a=[];const h=[];const l=[];const d=i(n);for(let t=0;t<d;t++){const t=i(n);let e=i(n);const r=JSON.parse(k(n));const d=s.meta.get(t);const u=s.states.get(t);const f=d===undefined?0:d.clock;if(f<e||f===e&&r===null&&s.states.has(t)){if(r===null){if(t===s.clientID&&s.getLocalState()!=null){e++}else{s.states.delete(t)}}else{s.states.set(t,r)}s.meta.set(t,{clock:e,lastUpdated:o});if(d===undefined&&r!==null){c.push(t)}else if(d!==undefined&&r===null){l.push(t)}else if(r!==null){if(!g(r,u)){h.push(t)}a.push(t)}}}if(c.length>0||h.length>0||l.length>0){s.emit("change",[{added:c,updated:h,removed:l},e])}if(c.length>0||a.length>0||l.length>0){s.emit("update",[{added:c,updated:a,removed:l},e])}};const ys=s=>$(s,((s,t)=>`${encodeURIComponent(t)}=${encodeURIComponent(s)}`)).join("&");const Ss=0;const ws=3;const ks=1;const bs=2;const vs=[];vs[Ss]=(t,e,i,n,o)=>{s(t,Ss);const c=hs(e,t,i.doc,i);if(n&&c===ss&&!i.synced){i.synced=true}};vs[ws]=(e,i,n,o,c)=>{s(e,ks);t(e,ps(n.awareness,Array.from(n.awareness.getStates().keys())))};vs[ks]=(s,t,e,i,o)=>{ms(e.awareness,n(t),e)};vs[bs]=(s,t,e,i,n)=>{ls(t,e.doc,((s,t)=>gs(e,t)))};const Ts=3e4;const gs=(s,t)=>console.warn(`Permission denied to access ${s.url}.\n${t}`);const Ws=(s,t,e)=>{const n=r(t);const o=h();const c=i(n);const a=s.messageHandlers[c];if(a){a(o,n,s,e,c)}else{console.error("Unable to compute message")}return o};const $s=(s,t,e)=>{if(t===s.ws){s.emit("connection-close",[e,s]);s.ws=null;t.close();s.wsconnecting=false;if(s.wsconnected){s.wsconnected=false;s.synced=false;fs(s.awareness,Array.from(s.awareness.getStates().keys()).filter((t=>t!==s.doc.clientID)),s);s.emit("status",[{status:"disconnected"}])}else{s.wsUnsuccessfulReconnects++}setTimeout(xs,C(U(2,s.wsUnsuccessfulReconnects)*100,s.maxBackoffTime),s)}};const xs=i=>{if(i.shouldConnect&&i.ws===null){const n=new i._WS(i.url,i.protocols);n.binaryType="arraybuffer";i.ws=n;i.wsconnecting=true;i.wsconnected=false;i.synced=false;n.onmessage=s=>{i.wsLastMessageReceived=T();const t=Ws(i,new Uint8Array(s.data),true);if(z(t)>1){n.send(e(t))}};n.onerror=s=>{i.emit("connection-error",[s,i])};n.onclose=s=>{$s(i,n,s)};n.onopen=()=>{i.wsLastMessageReceived=T();i.wsconnecting=false;i.wsconnected=true;i.wsUnsuccessfulReconnects=0;i.emit("status",[{status:"connected"}]);const o=h();s(o,Ss);es(o,i.doc);n.send(e(o));if(i.awareness.getLocalState()!==null){const o=h();s(o,ks);t(o,ps(i.awareness,[i.doc.clientID]));n.send(e(o))}};i.emit("status",[{status:"connecting"}])}};const zs=(s,t)=>{const e=s.ws;if(s.wsconnected&&e&&e.readyState===e.OPEN){e.send(t)}if(s.bcconnected){V(s.bcChannel,t,s)}};class Is extends x{constructor(i,n,o,{connect:c=true,awareness:a=new us(o),params:r={},protocols:l=[],WebSocketPolyfill:d=WebSocket,resyncInterval:u=-1,maxBackoffTime:f=2500,disableBc:p=false}={}){super();while(i[i.length-1]==="/"){i=i.slice(0,i.length-1)}this.serverUrl=i;this.bcChannel=i+"/"+n;this.maxBackoffTime=f;this.params=r;this.protocols=l;this.roomname=n;this.doc=o;this._WS=d;this.awareness=a;this.wsconnected=false;this.wsconnecting=false;this.bcconnected=false;this.disableBc=p;this.wsUnsuccessfulReconnects=0;this.messageHandlers=vs.slice();this._synced=false;this.ws=null;this.wsLastMessageReceived=0;this.shouldConnect=c;this._resyncInterval=0;if(u>0){this._resyncInterval=setInterval((()=>{if(this.ws&&this.ws.readyState===WebSocket.OPEN){const t=h();s(t,Ss);es(t,o);this.ws.send(e(t))}}),u)}this._bcSubscriber=(s,t)=>{if(t!==this){const t=Ws(this,new Uint8Array(s),false);if(z(t)>1){V(this.bcChannel,e(t),this)}}};this._updateHandler=(t,i)=>{if(i!==this){const i=h();s(i,Ss);cs(i,t);zs(this,e(i))}};this.doc.on("update",this._updateHandler);this._awarenessUpdateHandler=({added:i,updated:n,removed:o},c)=>{const r=i.concat(n).concat(o);const l=h();s(l,ks);t(l,ps(a,r));zs(this,e(l))};this._exitHandler=()=>{fs(this.awareness,[o.clientID],"app closed")};if(B&&typeof process!=="undefined"){process.on("exit",this._exitHandler)}a.on("update",this._awarenessUpdateHandler);this._checkInterval=setInterval((()=>{if(this.wsconnected&&Ts<T()-this.wsLastMessageReceived){$s(this,this.ws,null)}}),Ts/10);if(c){this.connect()}}get url(){const s=ys(this.params);return this.serverUrl+"/"+this.roomname+(s.length===0?"":"?"+s)}get synced(){return this._synced}set synced(s){if(this._synced!==s){this._synced=s;this.emit("synced",[s]);this.emit("sync",[s])}}destroy(){if(this._resyncInterval!==0){clearInterval(this._resyncInterval)}clearInterval(this._checkInterval);this.disconnect();if(B&&typeof process!=="undefined"){process.off("exit",this._exitHandler)}this.awareness.off("update",this._awarenessUpdateHandler);this.doc.off("update",this._updateHandler);super.destroy()}connectBc(){if(this.disableBc){return}if(!this.bcconnected){q(this.bcChannel,this._bcSubscriber);this.bcconnected=true}const i=h();s(i,Ss);es(i,this.doc);V(this.bcChannel,e(i),this);const n=h();s(n,Ss);is(n,this.doc);V(this.bcChannel,e(n),this);const o=h();s(o,ws);V(this.bcChannel,e(o),this);const c=h();s(c,ks);t(c,ps(this.awareness,[this.doc.clientID]));V(this.bcChannel,e(c),this)}disconnectBc(){const i=h();s(i,ks);t(i,ps(this.awareness,[this.doc.clientID],new Map));zs(this,e(i));if(this.bcconnected){X(this.bcChannel,this._bcSubscriber);this.bcconnected=false}}disconnect(){this.shouldConnect=false;this.disconnectBc();if(this.ws!==null){$s(this,this.ws,null)}}connect(){this.shouldConnect=true;if(!this.wsconnected&&this.ws===null){xs(this);this.connectBc()}}}class Ps{provider;isConnected=false;constructor(s,t,e){const i=e?.url||"ws://localhost:1234";const n=e?.roomName||s;this.provider=new Is(i,n,t,{params:e?.params,protocols:e?.protocols,WebSocketPolyfill:e?.WebSocketPolyfill,awareness:e?.awareness,maxBackoffTime:e?.maxBackoffTime,disableBc:true});this.setupEventListeners();console.info(`WebSocket Provider initialized: ${i}/${n}`)}static with(s){return{create:(t,e)=>new Ps(t,e,s)}}setupEventListeners(){this.provider.on("status",(({status:s})=>{if(s==="connected"){this.isConnected=true;console.info("WebSocket connected")}else if(s==="disconnected"){this.isConnected=false;console.info("WebSocket disconnected")}}));this.provider.on("sync",(s=>{if(s){console.info("WebSocket synced")}}))}async connect(){if(this.isConnected){return}return new Promise(((s,t)=>{const e=setTimeout((()=>{t(new Error("WebSocket connection timeout"))}),1e4);const i=({status:t})=>{if(t==="connected"){clearTimeout(e);this.provider.off("status",i);this.isConnected=true;s()}};this.provider.on("status",i);if(this.provider.wsconnected){clearTimeout(e);this.provider.off("status",i);this.isConnected=true;s()}}))}disconnect(){if(this.provider){this.provider.disconnect()}this.isConnected=false}destroy(){if(this.provider){this.provider.destroy()}this.isConnected=false}}class Bs{provider;isConnected=false;isSynced=false;usesSharedSocket=false;static sharedWebSocketProvider=null;constructor(s,t,e){const i=e?.name||s;const n=e?.url||"ws://localhost:1234";const o=e?.websocketProvider||Bs.sharedWebSocketProvider;if(o){this.usesSharedSocket=true;const s={websocketProvider:o,name:i,document:t,token:e?.token||null,onConnect:()=>{this.isConnected=true;if(!e?.quiet){console.info(`Hocuspocus connected: ${i}`)}if(e?.onConnect){e.onConnect()}},onDisconnect:()=>{this.isConnected=false;this.isSynced=false;if(!e?.quiet){console.info(`Hocuspocus disconnected: ${i}`)}if(e?.onDisconnect){e.onDisconnect()}},onSynced:()=>{this.isSynced=true;if(!e?.quiet){console.info(`Hocuspocus synced: ${i}`)}if(e?.onSynced){e.onSynced()}}};if(e?.forceSyncInterval!==undefined){s.forceSyncInterval=e.forceSyncInterval}if(e?.onAuthenticationFailed){s.onAuthenticationFailed=e.onAuthenticationFailed}if(e?.onStatus){s.onStatus=e.onStatus}this.provider=new H(s);this.provider.attach();if(!e?.quiet){console.info(`Hocuspocus Provider initialized (multiplexed): ${i}`)}}else{this.usesSharedSocket=false;const s={url:n,name:i,document:t,token:e?.token||null,onConnect:()=>{this.isConnected=true;if(!e?.quiet){console.info(`Hocuspocus connected: ${i}`)}if(e?.onConnect){e.onConnect()}},onDisconnect:()=>{this.isConnected=false;this.isSynced=false;if(!e?.quiet){console.info(`Hocuspocus disconnected: ${i}`)}if(e?.onDisconnect){e.onDisconnect()}},onSynced:()=>{this.isSynced=true;if(!e?.quiet){console.info(`Hocuspocus synced: ${i}`)}if(e?.onSynced){e.onSynced()}}};if(e?.forceSyncInterval!==undefined){s.forceSyncInterval=e.forceSyncInterval}if(e?.onAuthenticationFailed){s.onAuthenticationFailed=e.onAuthenticationFailed}if(e?.onStatus){s.onStatus=e.onStatus}if(e?.WebSocketPolyfill){s.WebSocketPolyfill=e.WebSocketPolyfill}this.provider=new H(s);if(!e?.quiet){console.info(`Hocuspocus Provider initialized: ${n}/${i}`)}}}static createSharedWebSocket(s){if(Bs.sharedWebSocketProvider){console.warn("Shared WebSocket already exists. Returning existing instance.");return Bs.sharedWebSocketProvider}const t={url:s.url};if(s.WebSocketPolyfill){t.WebSocketPolyfill=s.WebSocketPolyfill}if(s.onConnect){t.onConnect=s.onConnect}if(s.onDisconnect){t.onDisconnect=s.onDisconnect}if(s.onStatus){t.onStatus=s.onStatus}Bs.sharedWebSocketProvider=new A(t);console.info(`Shared Hocuspocus WebSocket created: ${s.url}`);return Bs.sharedWebSocketProvider}static destroySharedWebSocket(){if(Bs.sharedWebSocketProvider){Bs.sharedWebSocketProvider.destroy();Bs.sharedWebSocketProvider=null;console.info("Shared Hocuspocus WebSocket destroyed")}}static getSharedWebSocket(){return Bs.sharedWebSocketProvider}static with(s){return{create:(t,e)=>new Bs(t,e,s)}}async connect(){if(this.isSynced){return}return new Promise(((s,t)=>{const e=setTimeout((()=>{t(new Error("Hocuspocus connection timeout"))}),1e4);const i=()=>{clearTimeout(e);this.provider.off("synced",i);s()};this.provider.on("synced",i);if(this.provider.isSynced){clearTimeout(e);this.provider.off("synced",i);s();return}if(!this.isConnected&&!this.usesSharedSocket){this.provider.connect()}}))}disconnect(){if(this.provider){if(this.usesSharedSocket){this.provider.detach()}else{this.provider.disconnect()}}this.isConnected=false;this.isSynced=false}destroy(){if(this.provider){this.provider.destroy()}this.isConnected=false;this.isSynced=false}}export{D as BroadcastSyncProvider,Bs as HocuspocusSyncProvider,Ps as WebSocketSyncProvider};
|
|
1
|
+
import{w as s,a as t,t as e,r as i,b as n,e as o,c,d as a,f as h,g as r,s as l,h as d,i as u,v as f,j as p,o as m,k as y,l as w,m as k,O as b,n as v,p as T,q as g,u as z,x as W,y as $,z as x,A as B,B as C,C as U,H,D as A}from"./p-DzqpOikl.js";export{R as DEFAULT_BRUSH_CONFIG,S as DEFAULT_TEXT_CONFIG,N as IndexedDBSyncProvider,P as KritzelAppStateMap,G as KritzelBrushTool,I as KritzelEraserTool,F as KritzelImage,J as KritzelImageTool,E as KritzelPath,M as KritzelSelectionTool,K as KritzelText,L as KritzelTextTool,Q as KritzelWorkspace}from"./p-DzqpOikl.js";class D{doc;channel;_synced=false;constructor(s,t,e){this.doc=t;this.channel=new BroadcastChannel(s);this.channel.onmessage=s=>{this.handleMessage(s.data)};this.doc.on("update",this.handleDocUpdate);this.broadcastSync();setTimeout((()=>{this._synced=true}),100);console.info(`BroadcastChannel Provider initialized: ${s}`)}handleDocUpdate=(i,n)=>{if(n!==this){const n=h();s(n,0);t(n,i);this.channel.postMessage(e(n))}};handleMessage(a){const l=r(new Uint8Array(a));const d=i(l);switch(d){case 0:const i=n(l);c(this.doc,i,this);break;case 1:this.broadcastSync();break;case 2:const a=n(l);const r=o(this.doc,a);if(r.length>0){const i=h();s(i,0);t(i,r);this.channel.postMessage(e(i))}break}}broadcastSync(){const i=h();s(i,2);t(i,a(this.doc));this.channel.postMessage(e(i))}async connect(){if(this._synced){return}return new Promise((s=>{const t=()=>{if(this._synced){s()}else{setTimeout(t,50)}};t()}))}disconnect(){}destroy(){this.doc.off("update",this.handleDocUpdate);this.channel.close()}}const O=new Map;class _{constructor(s){this.room=s;this.onmessage=null;this._onChange=t=>t.key===s&&this.onmessage!==null&&this.onmessage({data:u(t.newValue||"")});m(this._onChange)}postMessage(s){f.setItem(this.room,p(y(s)))}close(){w(this._onChange)}}const j=typeof BroadcastChannel==="undefined"?_:BroadcastChannel;const q=s=>l(O,s,(()=>{const t=d();const e=new j(s);e.onmessage=s=>t.forEach((t=>t(s.data,"broadcastchannel")));return{bc:e,subs:t}}));const X=(s,t)=>{q(s).subs.add(t);return t};const Y=(s,t)=>{const e=q(s);const i=e.subs.delete(t);if(i&&e.subs.size===0){e.bc.close();O.delete(s)}return i};const V=(s,t,e=null)=>{const i=q(s);i.bc.postMessage(t);i.subs.forEach((s=>s(t,e)))};const Z=0;const ss=1;const ts=2;const es=(e,i)=>{s(e,Z);const n=a(i);t(e,n)};const is=(e,i,n)=>{s(e,ss);t(e,o(i,n))};const ns=(s,t,e)=>is(t,e,n(s));const os=(s,t,e)=>{try{c(t,n(s),e)}catch(s){console.error("Caught error while handling a Yjs update",s)}};const cs=(e,i)=>{s(e,ts);t(e,i)};const as=os;const hs=(s,t,e,n)=>{const o=i(s);switch(o){case Z:ns(s,t,e);break;case ss:os(s,e,n);break;case ts:as(s,e,n);break;default:throw new Error("Unknown message type")}return o};const rs=0;const ls=(s,t,e)=>{switch(i(s)){case rs:e(t,k(s))}};const ds=3e4;class us extends b{constructor(s){super();this.doc=s;this.clientID=s.clientID;this.states=new Map;this.meta=new Map;this._checkInterval=setInterval((()=>{const s=T();if(this.getLocalState()!==null&&ds/2<=s-this.meta.get(this.clientID).lastUpdated){this.setLocalState(this.getLocalState())}const t=[];this.meta.forEach(((e,i)=>{if(i!==this.clientID&&ds<=s-e.lastUpdated&&this.states.has(i)){t.push(i)}}));if(t.length>0){fs(this,t,"timeout")}}),v(ds/10));s.on("destroy",(()=>{this.destroy()}));this.setLocalState({})}destroy(){this.emit("destroy",[this]);this.setLocalState(null);super.destroy();clearInterval(this._checkInterval)}getLocalState(){return this.states.get(this.clientID)||null}setLocalState(s){const t=this.clientID;const e=this.meta.get(t);const i=e===undefined?0:e.clock+1;const n=this.states.get(t);if(s===null){this.states.delete(t)}else{this.states.set(t,s)}this.meta.set(t,{clock:i,lastUpdated:T()});const o=[];const c=[];const a=[];const h=[];if(s===null){h.push(t)}else if(n==null){if(s!=null){o.push(t)}}else{c.push(t);if(!g(n,s)){a.push(t)}}if(o.length>0||a.length>0||h.length>0){this.emit("change",[{added:o,updated:a,removed:h},"local"])}this.emit("update",[{added:o,updated:c,removed:h},"local"])}setLocalStateField(s,t){const e=this.getLocalState();if(e!==null){this.setLocalState({...e,[s]:t})}}getStates(){return this.states}}const fs=(s,t,e)=>{const i=[];for(let e=0;e<t.length;e++){const n=t[e];if(s.states.has(n)){s.states.delete(n);if(n===s.clientID){const t=s.meta.get(n);s.meta.set(n,{clock:t.clock+1,lastUpdated:T()})}i.push(n)}}if(i.length>0){s.emit("change",[{added:[],updated:[],removed:i},e]);s.emit("update",[{added:[],updated:[],removed:i},e])}};const ps=(t,i,n=t.states)=>{const o=i.length;const c=h();s(c,o);for(let e=0;e<o;e++){const o=i[e];const a=n.get(o)||null;const h=t.meta.get(o).clock;s(c,o);s(c,h);z(c,JSON.stringify(a))}return e(c)};const ms=(s,t,e)=>{const n=r(t);const o=T();const c=[];const a=[];const h=[];const l=[];const d=i(n);for(let t=0;t<d;t++){const t=i(n);let e=i(n);const r=JSON.parse(k(n));const d=s.meta.get(t);const u=s.states.get(t);const f=d===undefined?0:d.clock;if(f<e||f===e&&r===null&&s.states.has(t)){if(r===null){if(t===s.clientID&&s.getLocalState()!=null){e++}else{s.states.delete(t)}}else{s.states.set(t,r)}s.meta.set(t,{clock:e,lastUpdated:o});if(d===undefined&&r!==null){c.push(t)}else if(d!==undefined&&r===null){l.push(t)}else if(r!==null){if(!g(r,u)){h.push(t)}a.push(t)}}}if(c.length>0||h.length>0||l.length>0){s.emit("change",[{added:c,updated:h,removed:l},e])}if(c.length>0||a.length>0||l.length>0){s.emit("update",[{added:c,updated:a,removed:l},e])}};const ys=s=>W(s,((s,t)=>`${encodeURIComponent(t)}=${encodeURIComponent(s)}`)).join("&");const Ss=0;const ws=3;const ks=1;const bs=2;const vs=[];vs[Ss]=(t,e,i,n,o)=>{s(t,Ss);const c=hs(e,t,i.doc,i);if(n&&c===ss&&!i.synced){i.synced=true}};vs[ws]=(e,i,n,o,c)=>{s(e,ks);t(e,ps(n.awareness,Array.from(n.awareness.getStates().keys())))};vs[ks]=(s,t,e,i,o)=>{ms(e.awareness,n(t),e)};vs[bs]=(s,t,e,i,n)=>{ls(t,e.doc,((s,t)=>gs(e,t)))};const Ts=3e4;const gs=(s,t)=>console.warn(`Permission denied to access ${s.url}.\n${t}`);const zs=(s,t,e)=>{const n=r(t);const o=h();const c=i(n);const a=s.messageHandlers[c];if(a){a(o,n,s,e,c)}else{console.error("Unable to compute message")}return o};const Ws=(s,t,e)=>{if(t===s.ws){s.emit("connection-close",[e,s]);s.ws=null;t.close();s.wsconnecting=false;if(s.wsconnected){s.wsconnected=false;s.synced=false;fs(s.awareness,Array.from(s.awareness.getStates().keys()).filter((t=>t!==s.doc.clientID)),s);s.emit("status",[{status:"disconnected"}])}else{s.wsUnsuccessfulReconnects++}setTimeout($s,C(U(2,s.wsUnsuccessfulReconnects)*100,s.maxBackoffTime),s)}};const $s=i=>{if(i.shouldConnect&&i.ws===null){const n=new i._WS(i.url,i.protocols);n.binaryType="arraybuffer";i.ws=n;i.wsconnecting=true;i.wsconnected=false;i.synced=false;n.onmessage=s=>{i.wsLastMessageReceived=T();const t=zs(i,new Uint8Array(s.data),true);if(x(t)>1){n.send(e(t))}};n.onerror=s=>{i.emit("connection-error",[s,i])};n.onclose=s=>{Ws(i,n,s)};n.onopen=()=>{i.wsLastMessageReceived=T();i.wsconnecting=false;i.wsconnected=true;i.wsUnsuccessfulReconnects=0;i.emit("status",[{status:"connected"}]);const o=h();s(o,Ss);es(o,i.doc);n.send(e(o));if(i.awareness.getLocalState()!==null){const o=h();s(o,ks);t(o,ps(i.awareness,[i.doc.clientID]));n.send(e(o))}};i.emit("status",[{status:"connecting"}])}};const xs=(s,t)=>{const e=s.ws;if(s.wsconnected&&e&&e.readyState===e.OPEN){e.send(t)}if(s.bcconnected){V(s.bcChannel,t,s)}};class Is extends ${constructor(i,n,o,{connect:c=true,awareness:a=new us(o),params:r={},protocols:l=[],WebSocketPolyfill:d=WebSocket,resyncInterval:u=-1,maxBackoffTime:f=2500,disableBc:p=false}={}){super();while(i[i.length-1]==="/"){i=i.slice(0,i.length-1)}this.serverUrl=i;this.bcChannel=i+"/"+n;this.maxBackoffTime=f;this.params=r;this.protocols=l;this.roomname=n;this.doc=o;this._WS=d;this.awareness=a;this.wsconnected=false;this.wsconnecting=false;this.bcconnected=false;this.disableBc=p;this.wsUnsuccessfulReconnects=0;this.messageHandlers=vs.slice();this._synced=false;this.ws=null;this.wsLastMessageReceived=0;this.shouldConnect=c;this._resyncInterval=0;if(u>0){this._resyncInterval=setInterval((()=>{if(this.ws&&this.ws.readyState===WebSocket.OPEN){const t=h();s(t,Ss);es(t,o);this.ws.send(e(t))}}),u)}this._bcSubscriber=(s,t)=>{if(t!==this){const t=zs(this,new Uint8Array(s),false);if(x(t)>1){V(this.bcChannel,e(t),this)}}};this._updateHandler=(t,i)=>{if(i!==this){const i=h();s(i,Ss);cs(i,t);xs(this,e(i))}};this.doc.on("update",this._updateHandler);this._awarenessUpdateHandler=({added:i,updated:n,removed:o},c)=>{const r=i.concat(n).concat(o);const l=h();s(l,ks);t(l,ps(a,r));xs(this,e(l))};this._exitHandler=()=>{fs(this.awareness,[o.clientID],"app closed")};if(B&&typeof process!=="undefined"){process.on("exit",this._exitHandler)}a.on("update",this._awarenessUpdateHandler);this._checkInterval=setInterval((()=>{if(this.wsconnected&&Ts<T()-this.wsLastMessageReceived){Ws(this,this.ws,null)}}),Ts/10);if(c){this.connect()}}get url(){const s=ys(this.params);return this.serverUrl+"/"+this.roomname+(s.length===0?"":"?"+s)}get synced(){return this._synced}set synced(s){if(this._synced!==s){this._synced=s;this.emit("synced",[s]);this.emit("sync",[s])}}destroy(){if(this._resyncInterval!==0){clearInterval(this._resyncInterval)}clearInterval(this._checkInterval);this.disconnect();if(B&&typeof process!=="undefined"){process.off("exit",this._exitHandler)}this.awareness.off("update",this._awarenessUpdateHandler);this.doc.off("update",this._updateHandler);super.destroy()}connectBc(){if(this.disableBc){return}if(!this.bcconnected){X(this.bcChannel,this._bcSubscriber);this.bcconnected=true}const i=h();s(i,Ss);es(i,this.doc);V(this.bcChannel,e(i),this);const n=h();s(n,Ss);is(n,this.doc);V(this.bcChannel,e(n),this);const o=h();s(o,ws);V(this.bcChannel,e(o),this);const c=h();s(c,ks);t(c,ps(this.awareness,[this.doc.clientID]));V(this.bcChannel,e(c),this)}disconnectBc(){const i=h();s(i,ks);t(i,ps(this.awareness,[this.doc.clientID],new Map));xs(this,e(i));if(this.bcconnected){Y(this.bcChannel,this._bcSubscriber);this.bcconnected=false}}disconnect(){this.shouldConnect=false;this.disconnectBc();if(this.ws!==null){Ws(this,this.ws,null)}}connect(){this.shouldConnect=true;if(!this.wsconnected&&this.ws===null){$s(this);this.connectBc()}}}class Ps{provider;isConnected=false;constructor(s,t,e){const i=e?.url||"ws://localhost:1234";const n=e?.roomName||s;this.provider=new Is(i,n,t,{params:e?.params,protocols:e?.protocols,WebSocketPolyfill:e?.WebSocketPolyfill,awareness:e?.awareness,maxBackoffTime:e?.maxBackoffTime,disableBc:true});this.setupEventListeners();console.info(`WebSocket Provider initialized: ${i}/${n}`)}static with(s){return{create:(t,e)=>new Ps(t,e,s)}}setupEventListeners(){this.provider.on("status",(({status:s})=>{if(s==="connected"){this.isConnected=true;console.info("WebSocket connected")}else if(s==="disconnected"){this.isConnected=false;console.info("WebSocket disconnected")}}));this.provider.on("sync",(s=>{if(s){console.info("WebSocket synced")}}))}async connect(){if(this.isConnected){return}return new Promise(((s,t)=>{const e=setTimeout((()=>{t(new Error("WebSocket connection timeout"))}),1e4);const i=({status:t})=>{if(t==="connected"){clearTimeout(e);this.provider.off("status",i);this.isConnected=true;s()}};this.provider.on("status",i);if(this.provider.wsconnected){clearTimeout(e);this.provider.off("status",i);this.isConnected=true;s()}}))}disconnect(){if(this.provider){this.provider.disconnect()}this.isConnected=false}destroy(){if(this.provider){this.provider.destroy()}this.isConnected=false}}class Bs{provider;isConnected=false;isSynced=false;usesSharedSocket=false;static sharedWebSocketProvider=null;constructor(s,t,e){const i=e?.name||s;const n=e?.url||"ws://localhost:1234";const o=e?.websocketProvider||Bs.sharedWebSocketProvider;if(o){this.usesSharedSocket=true;const s={websocketProvider:o,name:i,document:t,token:e?.token||null,onConnect:()=>{this.isConnected=true;if(!e?.quiet){console.info(`Hocuspocus connected: ${i}`)}if(e?.onConnect){e.onConnect()}},onDisconnect:()=>{this.isConnected=false;this.isSynced=false;if(!e?.quiet){console.info(`Hocuspocus disconnected: ${i}`)}if(e?.onDisconnect){e.onDisconnect()}},onSynced:()=>{this.isSynced=true;if(!e?.quiet){console.info(`Hocuspocus synced: ${i}`)}if(e?.onSynced){e.onSynced()}}};if(e?.forceSyncInterval!==undefined){s.forceSyncInterval=e.forceSyncInterval}if(e?.onAuthenticationFailed){s.onAuthenticationFailed=e.onAuthenticationFailed}if(e?.onStatus){s.onStatus=e.onStatus}this.provider=new H(s);this.provider.attach();if(!e?.quiet){console.info(`Hocuspocus Provider initialized (multiplexed): ${i}`)}}else{this.usesSharedSocket=false;const s={url:n,name:i,document:t,token:e?.token||null,onConnect:()=>{this.isConnected=true;if(!e?.quiet){console.info(`Hocuspocus connected: ${i}`)}if(e?.onConnect){e.onConnect()}},onDisconnect:()=>{this.isConnected=false;this.isSynced=false;if(!e?.quiet){console.info(`Hocuspocus disconnected: ${i}`)}if(e?.onDisconnect){e.onDisconnect()}},onSynced:()=>{this.isSynced=true;if(!e?.quiet){console.info(`Hocuspocus synced: ${i}`)}if(e?.onSynced){e.onSynced()}}};if(e?.forceSyncInterval!==undefined){s.forceSyncInterval=e.forceSyncInterval}if(e?.onAuthenticationFailed){s.onAuthenticationFailed=e.onAuthenticationFailed}if(e?.onStatus){s.onStatus=e.onStatus}if(e?.WebSocketPolyfill){s.WebSocketPolyfill=e.WebSocketPolyfill}this.provider=new H(s);if(!e?.quiet){console.info(`Hocuspocus Provider initialized: ${n}/${i}`)}}}static createSharedWebSocket(s){if(Bs.sharedWebSocketProvider){console.warn("Shared WebSocket already exists. Returning existing instance.");return Bs.sharedWebSocketProvider}const t={url:s.url};if(s.WebSocketPolyfill){t.WebSocketPolyfill=s.WebSocketPolyfill}if(s.onConnect){t.onConnect=s.onConnect}if(s.onDisconnect){t.onDisconnect=s.onDisconnect}if(s.onStatus){t.onStatus=s.onStatus}Bs.sharedWebSocketProvider=new A(t);console.info(`Shared Hocuspocus WebSocket created: ${s.url}`);return Bs.sharedWebSocketProvider}static destroySharedWebSocket(){if(Bs.sharedWebSocketProvider){Bs.sharedWebSocketProvider.destroy();Bs.sharedWebSocketProvider=null;console.info("Shared Hocuspocus WebSocket destroyed")}}static getSharedWebSocket(){return Bs.sharedWebSocketProvider}static with(s){return{create:(t,e)=>new Bs(t,e,s)}}async connect(){if(this.isSynced){return}return new Promise(((s,t)=>{const e=setTimeout((()=>{t(new Error("Hocuspocus connection timeout"))}),1e4);const i=()=>{clearTimeout(e);this.provider.off("synced",i);s()};this.provider.on("synced",i);if(this.provider.isSynced){clearTimeout(e);this.provider.off("synced",i);s();return}if(!this.isConnected&&!this.usesSharedSocket){this.provider.connect()}}))}disconnect(){if(this.provider){if(this.usesSharedSocket){this.provider.detach()}else{this.provider.disconnect()}}this.isConnected=false;this.isSynced=false}destroy(){if(this.provider){this.provider.destroy()}this.isConnected=false;this.isSynced=false}}export{D as BroadcastSyncProvider,Bs as HocuspocusSyncProvider,Ps as WebSocketSyncProvider};
|
|
2
2
|
//# sourceMappingURL=index.esm.js.map
|