kritzel-stencil 0.1.76 → 0.1.77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/cjs/index.cjs.js +2 -330
  2. package/dist/cjs/kritzel-active-users_42.cjs.entry.js +36 -18
  3. package/dist/cjs/{workspace.migrations-DkmVO6dE.js → workspace.migrations-OULs44dI.js} +331 -2
  4. package/dist/collection/classes/objects/selection-group.class.js +2 -0
  5. package/dist/collection/components/core/kritzel-awareness-cursors/kritzel-awareness-cursors.css +6 -1
  6. package/dist/collection/components/core/kritzel-awareness-cursors/kritzel-awareness-cursors.js +5 -2
  7. package/dist/collection/components/core/kritzel-editor/kritzel-editor.css +2 -0
  8. package/dist/collection/components/core/kritzel-editor/kritzel-editor.js +12 -6
  9. package/dist/collection/components/core/kritzel-engine/kritzel-engine.js +18 -8
  10. package/dist/collection/components/ui/kritzel-utility-panel/kritzel-utility-panel.css +1 -0
  11. package/dist/collection/constants/version.js +1 -1
  12. package/dist/components/index.js +1 -1
  13. package/dist/components/kritzel-awareness-cursors.js +1 -1
  14. package/dist/components/kritzel-controls.js +1 -1
  15. package/dist/components/kritzel-editor.js +1 -1
  16. package/dist/components/kritzel-engine.js +1 -1
  17. package/dist/components/kritzel-settings.js +1 -1
  18. package/dist/components/kritzel-tool-config.js +1 -1
  19. package/dist/components/kritzel-utility-panel.js +1 -1
  20. package/dist/components/{p-DvIEvoZu.js → p-BK1hLBTd.js} +1 -1
  21. package/dist/components/{p-jdYmu4SA.js → p-BdGcOXa5.js} +2 -2
  22. package/dist/components/{p-CsoDfhD5.js → p-C_X8stam.js} +1 -1
  23. package/dist/components/p-WotNmY5q.js +1 -0
  24. package/dist/components/{p-31FVoNWR.js → p-XS5J5W5_.js} +1 -1
  25. package/dist/components/{p-CJ2eHeoV.js → p-_CqLIbO6.js} +1 -1
  26. package/dist/components/{p-2OYw6GJ7.js → p-_LbtY-TA.js} +1 -1
  27. package/dist/esm/index.js +2 -331
  28. package/dist/esm/kritzel-active-users_42.entry.js +36 -18
  29. package/dist/esm/{workspace.migrations-D48_Bqvh.js → workspace.migrations-D6whgl7G.js} +331 -1
  30. package/dist/stencil/index.esm.js +1 -1
  31. package/dist/stencil/p-8fe1ec39.entry.js +9 -0
  32. package/dist/stencil/p-D6whgl7G.js +1 -0
  33. package/dist/stencil/stencil.esm.js +1 -1
  34. package/dist/types/classes/objects/selection-group.class.d.ts +1 -0
  35. package/dist/types/components.d.ts +8 -2
  36. package/dist/types/constants/version.d.ts +1 -1
  37. package/package.json +1 -1
  38. package/dist/components/p-xNwOWoiT.js +0 -1
  39. package/dist/stencil/p-775a7246.entry.js +0 -9
  40. package/dist/stencil/p-D48_Bqvh.js +0 -1
@@ -18470,6 +18470,7 @@ class KritzelSelectionGroup extends KritzelBaseObject {
18470
18470
  snapshotHeight = 0;
18471
18471
  snapshotTranslateX = 0;
18472
18472
  snapshotTranslateY = 0;
18473
+ clientId;
18473
18474
  minX;
18474
18475
  maxX;
18475
18476
  minY;
@@ -18542,6 +18543,7 @@ class KritzelSelectionGroup extends KritzelBaseObject {
18542
18543
  object.id = object.generateId();
18543
18544
  object.workspaceId = core.store.state.activeWorkspace.id;
18544
18545
  object.userId = core.user?.id;
18546
+ object.clientId = core.store.state.objects?.localClientId ?? undefined;
18545
18547
  object.scale = core.store.state.scale;
18546
18548
  object.zIndex = 99999;
18547
18549
  // Initialize styling with theme-aware defaults
@@ -25202,6 +25204,334 @@ class HocuspocusProvider extends EventEmitter {
25202
25204
  }
25203
25205
  }
25204
25206
 
25207
+ /**
25208
+ * Hocuspocus sync provider for real-time collaboration
25209
+ * Supports multiplexing - multiple documents can share the same WebSocket connection
25210
+ */
25211
+ class HocuspocusSyncProvider {
25212
+ type = 'network';
25213
+ provider;
25214
+ isConnected = false;
25215
+ isSynced = false;
25216
+ usesSharedSocket = false;
25217
+ isDestroyed = false;
25218
+ connectTimeout = null;
25219
+ pendingConnectReject = null;
25220
+ connectionTimeoutMs;
25221
+ _connectionStatus = 'disconnected';
25222
+ visibilityHandler = null;
25223
+ onlineHandler = null;
25224
+ get awareness() {
25225
+ return this.provider.awareness;
25226
+ }
25227
+ get connectionStatus() {
25228
+ return this._connectionStatus;
25229
+ }
25230
+ // Static shared WebSocket instance for multiplexing
25231
+ static sharedWebSocketProvider = null;
25232
+ constructor(docName, doc, options) {
25233
+ const name = options?.name || docName;
25234
+ const url = options?.url || 'ws://localhost:1234';
25235
+ this.connectionTimeoutMs = options?.connectionTimeout ?? 10000;
25236
+ // Use provided websocketProvider or the static shared one
25237
+ const websocketProvider = options?.websocketProvider || HocuspocusSyncProvider.sharedWebSocketProvider;
25238
+ // Build reconnect config from options
25239
+ const reconnectConfig = {};
25240
+ if (options?.delay !== undefined)
25241
+ reconnectConfig.delay = options.delay;
25242
+ if (options?.factor !== undefined)
25243
+ reconnectConfig.factor = options.factor;
25244
+ if (options?.maxAttempts !== undefined)
25245
+ reconnectConfig.maxAttempts = options.maxAttempts;
25246
+ if (options?.minDelay !== undefined)
25247
+ reconnectConfig.minDelay = options.minDelay;
25248
+ if (options?.maxDelay !== undefined)
25249
+ reconnectConfig.maxDelay = options.maxDelay;
25250
+ const onConnect = () => {
25251
+ if (this.isDestroyed) {
25252
+ return;
25253
+ }
25254
+ this.isConnected = true;
25255
+ this._connectionStatus = 'connected';
25256
+ if (!options?.quiet) {
25257
+ console.info(`Hocuspocus connected: ${name}`);
25258
+ }
25259
+ if (options?.onConnect) {
25260
+ options.onConnect();
25261
+ }
25262
+ };
25263
+ const onDisconnect = () => {
25264
+ if (this.isDestroyed) {
25265
+ return;
25266
+ }
25267
+ this.isConnected = false;
25268
+ this.isSynced = false;
25269
+ this._connectionStatus = 'disconnected';
25270
+ if (!options?.quiet) {
25271
+ console.info(`Hocuspocus disconnected: ${name}`);
25272
+ }
25273
+ if (options?.onDisconnect) {
25274
+ options.onDisconnect();
25275
+ }
25276
+ };
25277
+ const onSynced = () => {
25278
+ if (this.isDestroyed) {
25279
+ return;
25280
+ }
25281
+ this.isSynced = true;
25282
+ this._connectionStatus = 'synced';
25283
+ if (!options?.quiet) {
25284
+ console.info(`Hocuspocus synced: ${name}`);
25285
+ }
25286
+ if (options?.onSynced) {
25287
+ options.onSynced();
25288
+ }
25289
+ };
25290
+ const onStatus = (data) => {
25291
+ if (this.isDestroyed) {
25292
+ return;
25293
+ }
25294
+ if (data.status === 'connecting') {
25295
+ this._connectionStatus = 'connecting';
25296
+ }
25297
+ if (options?.onStatus) {
25298
+ options.onStatus(data);
25299
+ }
25300
+ };
25301
+ if (websocketProvider) {
25302
+ // Multiplexing mode - use shared WebSocket connection
25303
+ this.usesSharedSocket = true;
25304
+ const config = {
25305
+ websocketProvider,
25306
+ name,
25307
+ document: doc,
25308
+ token: options?.token || null,
25309
+ onStatus,
25310
+ onConnect,
25311
+ onDisconnect,
25312
+ onSynced,
25313
+ ...reconnectConfig,
25314
+ };
25315
+ // Add optional settings
25316
+ if (options?.forceSyncInterval !== undefined) {
25317
+ config.forceSyncInterval = options.forceSyncInterval;
25318
+ }
25319
+ if (options?.onAuthenticationFailed) {
25320
+ config.onAuthenticationFailed = options.onAuthenticationFailed;
25321
+ }
25322
+ this.provider = new HocuspocusProvider(config);
25323
+ // Must call attach() explicitly when using shared socket
25324
+ this.provider.attach();
25325
+ if (!options?.quiet) {
25326
+ console.info(`Hocuspocus Provider initialized (multiplexed): ${name}`);
25327
+ }
25328
+ }
25329
+ else {
25330
+ // Standalone mode - create own WebSocket connection
25331
+ this.usesSharedSocket = false;
25332
+ const config = {
25333
+ url,
25334
+ name,
25335
+ document: doc,
25336
+ token: options?.token || null,
25337
+ autoConnect: false,
25338
+ onStatus,
25339
+ onConnect,
25340
+ onDisconnect,
25341
+ onSynced,
25342
+ ...reconnectConfig,
25343
+ };
25344
+ // Add optional settings
25345
+ if (options?.forceSyncInterval !== undefined) {
25346
+ config.forceSyncInterval = options.forceSyncInterval;
25347
+ }
25348
+ if (options?.onAuthenticationFailed) {
25349
+ config.onAuthenticationFailed = options.onAuthenticationFailed;
25350
+ }
25351
+ if (options?.WebSocketPolyfill) {
25352
+ config.WebSocketPolyfill = options.WebSocketPolyfill;
25353
+ }
25354
+ this.provider = new HocuspocusProvider(config);
25355
+ if (!options?.quiet) {
25356
+ console.info(`Hocuspocus Provider initialized: ${url}/${name}`);
25357
+ }
25358
+ }
25359
+ this.setupBrowserEventListeners();
25360
+ }
25361
+ setupBrowserEventListeners() {
25362
+ if (typeof document !== 'undefined') {
25363
+ this.visibilityHandler = () => {
25364
+ if (document.visibilityState === 'visible' && !this.isConnected && !this.isDestroyed) {
25365
+ this.provider.connect();
25366
+ }
25367
+ };
25368
+ document.addEventListener('visibilitychange', this.visibilityHandler);
25369
+ }
25370
+ if (typeof window !== 'undefined') {
25371
+ this.onlineHandler = () => {
25372
+ if (!this.isConnected && !this.isDestroyed) {
25373
+ this.provider.connect();
25374
+ }
25375
+ };
25376
+ window.addEventListener('online', this.onlineHandler);
25377
+ }
25378
+ }
25379
+ removeBrowserEventListeners() {
25380
+ if (this.visibilityHandler && typeof document !== 'undefined') {
25381
+ document.removeEventListener('visibilitychange', this.visibilityHandler);
25382
+ this.visibilityHandler = null;
25383
+ }
25384
+ if (this.onlineHandler && typeof window !== 'undefined') {
25385
+ window.removeEventListener('online', this.onlineHandler);
25386
+ this.onlineHandler = null;
25387
+ }
25388
+ }
25389
+ /**
25390
+ * Create a shared WebSocket connection for multiplexing
25391
+ * Call this once to create a shared connection that multiple providers can use
25392
+ */
25393
+ static createSharedWebSocket(options) {
25394
+ if (HocuspocusSyncProvider.sharedWebSocketProvider) {
25395
+ console.warn('Shared WebSocket already exists. Returning existing instance.');
25396
+ return HocuspocusSyncProvider.sharedWebSocketProvider;
25397
+ }
25398
+ const config = {
25399
+ url: options.url,
25400
+ };
25401
+ if (options.WebSocketPolyfill) {
25402
+ config.WebSocketPolyfill = options.WebSocketPolyfill;
25403
+ }
25404
+ if (options.onConnect) {
25405
+ config.onConnect = options.onConnect;
25406
+ }
25407
+ if (options.onDisconnect) {
25408
+ config.onDisconnect = options.onDisconnect;
25409
+ }
25410
+ if (options.onStatus) {
25411
+ config.onStatus = options.onStatus;
25412
+ }
25413
+ HocuspocusSyncProvider.sharedWebSocketProvider = new HocuspocusProviderWebsocket(config);
25414
+ console.info(`Shared Hocuspocus WebSocket created: ${options.url}`);
25415
+ return HocuspocusSyncProvider.sharedWebSocketProvider;
25416
+ }
25417
+ /**
25418
+ * Destroy the shared WebSocket connection
25419
+ * Call this when you're done with all multiplexed providers
25420
+ */
25421
+ static destroySharedWebSocket() {
25422
+ if (HocuspocusSyncProvider.sharedWebSocketProvider) {
25423
+ HocuspocusSyncProvider.sharedWebSocketProvider.destroy();
25424
+ HocuspocusSyncProvider.sharedWebSocketProvider = null;
25425
+ console.info('Shared Hocuspocus WebSocket destroyed');
25426
+ }
25427
+ }
25428
+ /**
25429
+ * Get the shared WebSocket provider instance (if it exists)
25430
+ */
25431
+ static getSharedWebSocket() {
25432
+ return HocuspocusSyncProvider.sharedWebSocketProvider;
25433
+ }
25434
+ /**
25435
+ * Static factory method for creating HocuspocusSyncProvider with configuration options
25436
+ * Returns a ProviderFactory that can be used in sync configuration
25437
+ */
25438
+ static with(options) {
25439
+ return {
25440
+ create: (docName, doc, runtimeOptions) => {
25441
+ const mergedOptions = runtimeOptions ? { ...options, ...runtimeOptions } : options;
25442
+ return new HocuspocusSyncProvider(docName, doc, mergedOptions);
25443
+ },
25444
+ };
25445
+ }
25446
+ async connect() {
25447
+ if (this.isSynced || this.isDestroyed) {
25448
+ return;
25449
+ }
25450
+ this._connectionStatus = 'connecting';
25451
+ return new Promise((resolve, reject) => {
25452
+ // Store reject function so we can cancel the connection if destroyed
25453
+ this.pendingConnectReject = reject;
25454
+ this.connectTimeout = setTimeout(() => {
25455
+ this.pendingConnectReject = null;
25456
+ this.connectTimeout = null;
25457
+ reject(new Error('Hocuspocus connection timeout'));
25458
+ }, this.connectionTimeoutMs);
25459
+ const syncHandler = () => {
25460
+ if (this.connectTimeout) {
25461
+ clearTimeout(this.connectTimeout);
25462
+ this.connectTimeout = null;
25463
+ }
25464
+ this.pendingConnectReject = null;
25465
+ this.provider.off('synced', syncHandler);
25466
+ if (!this.isDestroyed) {
25467
+ resolve();
25468
+ }
25469
+ };
25470
+ this.provider.on('synced', syncHandler);
25471
+ // If already synced, resolve immediately
25472
+ if (this.provider.isSynced) {
25473
+ if (this.connectTimeout) {
25474
+ clearTimeout(this.connectTimeout);
25475
+ this.connectTimeout = null;
25476
+ }
25477
+ this.pendingConnectReject = null;
25478
+ this.provider.off('synced', syncHandler);
25479
+ resolve();
25480
+ return;
25481
+ }
25482
+ // Connect if not already connected (standalone mode only)
25483
+ if (!this.isConnected && !this.usesSharedSocket) {
25484
+ this.provider.connect();
25485
+ }
25486
+ });
25487
+ }
25488
+ async reconnect() {
25489
+ this.disconnect();
25490
+ return this.connect();
25491
+ }
25492
+ disconnect() {
25493
+ // Cancel any pending connection attempt
25494
+ if (this.connectTimeout) {
25495
+ clearTimeout(this.connectTimeout);
25496
+ this.connectTimeout = null;
25497
+ }
25498
+ if (this.pendingConnectReject) {
25499
+ this.pendingConnectReject = null; // Don't reject, just abandon the promise
25500
+ }
25501
+ if (this.provider) {
25502
+ if (this.usesSharedSocket) {
25503
+ // Detach from shared socket instead of disconnecting
25504
+ this.provider.detach();
25505
+ }
25506
+ else {
25507
+ this.provider.disconnect();
25508
+ }
25509
+ }
25510
+ this.isConnected = false;
25511
+ this.isSynced = false;
25512
+ this._connectionStatus = 'disconnected';
25513
+ }
25514
+ destroy() {
25515
+ // Mark as destroyed first to prevent any callbacks from doing work
25516
+ this.isDestroyed = true;
25517
+ // Cancel any pending connection attempt
25518
+ if (this.connectTimeout) {
25519
+ clearTimeout(this.connectTimeout);
25520
+ this.connectTimeout = null;
25521
+ }
25522
+ if (this.pendingConnectReject) {
25523
+ this.pendingConnectReject = null; // Don't reject, just abandon the promise
25524
+ }
25525
+ this.removeBrowserEventListeners();
25526
+ if (this.provider) {
25527
+ this.provider.destroy();
25528
+ }
25529
+ this.isConnected = false;
25530
+ this.isSynced = false;
25531
+ this._connectionStatus = 'disconnected';
25532
+ }
25533
+ }
25534
+
25205
25535
  /** Current version of the workspace export format */
25206
25536
  const WORKSPACE_EXPORT_VERSION = '1.2.0';
25207
25537
  /**
@@ -26853,4 +27183,4 @@ const WORKSPACE_MIGRATIONS = [
26853
27183
  },
26854
27184
  ];
26855
27185
 
26856
- export { APP_STATE_MIGRATIONS as A, KritzelDevicesHelper as B, CURRENT_APP_STATE_SCHEMA_VERSION as C, DEFAULT_BRUSH_CONFIG as D, KritzelMouseButton as E, DEFAULT_COLOR_PALETTE as F, KritzelSelectionGroup as G, HocuspocusProvider as H, IndexedDBSyncProvider as I, KritzelSelectionBox as J, KritzelText as K, KritzelIconRegistry as L, KritzelKeyboardHelper as M, KritzelBaseHandler as N, KritzelToolRegistry as O, KritzelBaseObject as P, ObjectHelper as Q, KritzelClassHelper as R, ShapeType as S, KritzelEventHelper as T, KritzelBaseTool as U, WORKSPACE_EXPORT_VERSION as W, HocuspocusProviderWebsocket as a, KritzelPath as b, KritzelImage as c, KritzelLine as d, KritzelGroup as e, KritzelShape as f, KritzelBrushTool as g, KritzelLineTool as h, KritzelEraserTool as i, KritzelImageTool as j, KritzelTextTool as k, KritzelShapeTool as l, KritzelCursorHelper as m, KritzelSelectionTool as n, KritzelWorkspace as o, KritzelAnchorManager as p, KritzelThemeManager as q, DEFAULT_TEXT_CONFIG as r, DEFAULT_LINE_TOOL_CONFIG as s, lightTheme as t, darkTheme as u, KritzelAlignment as v, CURRENT_WORKSPACE_SCHEMA_VERSION as w, runMigrations as x, WORKSPACE_MIGRATIONS as y, KritzelColorHelper as z };
27186
+ export { APP_STATE_MIGRATIONS as A, KritzelMouseButton as B, CURRENT_APP_STATE_SCHEMA_VERSION as C, DEFAULT_BRUSH_CONFIG as D, DEFAULT_COLOR_PALETTE as E, KritzelSelectionGroup as F, KritzelSelectionBox as G, HocuspocusSyncProvider as H, IndexedDBSyncProvider as I, KritzelIconRegistry as J, KritzelText as K, KritzelKeyboardHelper as L, KritzelBaseHandler as M, KritzelToolRegistry as N, KritzelBaseObject as O, ObjectHelper as P, KritzelClassHelper as Q, KritzelEventHelper as R, ShapeType as S, KritzelBaseTool as T, WORKSPACE_EXPORT_VERSION as W, KritzelPath as a, KritzelImage as b, KritzelLine as c, KritzelGroup as d, KritzelShape as e, KritzelBrushTool as f, KritzelLineTool as g, KritzelEraserTool as h, KritzelImageTool as i, KritzelTextTool as j, KritzelShapeTool as k, KritzelCursorHelper as l, KritzelSelectionTool as m, KritzelWorkspace as n, KritzelAnchorManager as o, KritzelThemeManager as p, DEFAULT_TEXT_CONFIG as q, DEFAULT_LINE_TOOL_CONFIG as r, lightTheme as s, darkTheme as t, KritzelAlignment as u, CURRENT_WORKSPACE_SCHEMA_VERSION as v, runMigrations as w, WORKSPACE_MIGRATIONS as x, KritzelColorHelper as y, KritzelDevicesHelper as z };
@@ -1 +1 @@
1
- import{H as a,a as T}from"./p-D48_Bqvh.js";export{A as APP_STATE_MIGRATIONS,C as CURRENT_APP_STATE_SCHEMA_VERSION,w as CURRENT_WORKSPACE_SCHEMA_VERSION,D as DEFAULT_BRUSH_CONFIG,s as DEFAULT_LINE_TOOL_CONFIG,r as DEFAULT_TEXT_CONFIG,I as IndexedDBSyncProvider,v as KritzelAlignment,p as KritzelAnchorManager,g as KritzelBrushTool,m as KritzelCursorHelper,i as KritzelEraserTool,e as KritzelGroup,c as KritzelImage,j as KritzelImageTool,d as KritzelLine,h as KritzelLineTool,b as KritzelPath,n as KritzelSelectionTool,f as KritzelShape,l as KritzelShapeTool,K as KritzelText,k as KritzelTextTool,q as KritzelThemeManager,o as KritzelWorkspace,S as ShapeType,W as WORKSPACE_EXPORT_VERSION,y as WORKSPACE_MIGRATIONS,u as darkTheme,t as lightTheme,x as runMigrations}from"./p-D48_Bqvh.js";import*as E from"yjs";import{WebsocketProvider as _}from"y-websocket";import"y-indexeddb";const z=Math.floor,P=127,R=Number.MAX_SAFE_INTEGER;class H{constructor(){this.cpos=0,this.cbuf=new Uint8Array(100),this.bufs=[]}}const U=()=>new H,O=t=>{const e=new Uint8Array((t=>{let e=t.cpos;for(let s=0;s<t.bufs.length;s++)e+=t.bufs[s].length;return e})(t));let s=0;for(let i=0;i<t.bufs.length;i++){const n=t.bufs[i];e.set(n,s),s+=n.length}return e.set(new Uint8Array(t.cbuf.buffer,0,t.cpos),s),e},N=(t,e)=>{const s=t.cbuf.length;t.cpos===s&&(t.bufs.push(t.cbuf),t.cbuf=new Uint8Array(2*s),t.cpos=0),t.cbuf[t.cpos++]=e},B=(t,e)=>{for(;e>P;)N(t,128|P&e),e=z(e/128);N(t,P&e)},L=(t,e)=>{B(t,e.byteLength),((t,e)=>{const s=t.cbuf.length,i=t.cpos,n=((t,e)=>t<e?t:e)(s-i,e.length),o=e.length-n;t.cbuf.set(e.subarray(0,n),i),t.cpos+=n,o>0&&(t.bufs.push(t.cbuf),t.cbuf=new Uint8Array(((t,e)=>t>e?t:e)(2*s,o)),t.cbuf.set(e.subarray(n)),t.cpos=o)})(t,e)},M=t=>new Error(t),$=M("Unexpected end of array"),F=M("Integer out of Range");class G{constructor(t){this.arr=t,this.pos=0}}const V=t=>((t,e)=>{const s=new Uint8Array(t.arr.buffer,t.pos+t.arr.byteOffset,e);return t.pos+=e,s})(t,X(t)),X=t=>{let e=0,s=1;const i=t.arr.length;for(;t.pos<i;){const i=t.arr[t.pos++];if(e+=(i&P)*s,s*=128,i<128)return e;if(e>R)throw F}throw $};class J{type="local";doc;channel;_synced=!1;constructor(t,e,s){this.doc=e,this.channel=new BroadcastChannel(t),this.channel.onmessage=t=>{this.handleMessage(t.data)},this.doc.on("update",this.handleDocUpdate),this.broadcastSync(),setTimeout((()=>{this._synced=!0}),100),s?.quiet||console.info(`BroadcastChannel Provider initialized: ${t}`)}handleDocUpdate=(t,e)=>{if(e!==this){const e=U();B(e,0),L(e,t),this.channel.postMessage(O(e))}};handleMessage(t){const e=(s=new Uint8Array(t),new G(s));var s;switch(X(e)){case 0:const t=V(e);E.applyUpdate(this.doc,t,this);break;case 1:this.broadcastSync();break;case 2:const s=V(e),i=E.encodeStateAsUpdate(this.doc,s);if(i.length>0){const t=U();B(t,0),L(t,i),this.channel.postMessage(O(t))}}}broadcastSync(){const t=U();B(t,2),L(t,E.encodeStateVector(this.doc)),this.channel.postMessage(O(t))}async connect(){if(!this._synced)return new Promise((t=>{const e=()=>{this._synced?t():setTimeout(e,50)};e()}))}disconnect(){}async reconnect(){return this.disconnect(),this.connect()}destroy(){this.doc.off("update",this.handleDocUpdate),this.channel.close()}}class Q{type="network";provider;isConnected=!1;_quiet=!1;get awareness(){return this.provider.awareness}constructor(t,e,s){const i=s?.url||"ws://localhost:1234",n=s?.roomName||t;this.provider=new _(i,n,e,{params:s?.params,protocols:s?.protocols,WebSocketPolyfill:s?.WebSocketPolyfill,awareness:s?.awareness,maxBackoffTime:s?.maxBackoffTime,disableBc:!0}),this._quiet=s?.quiet??!1,this.setupEventListeners(),this._quiet||console.info(`WebSocket Provider initialized: ${i}/${n}`)}static with(t){return{create:(e,s,i)=>{const n=i?{...t,...i}:t;return new Q(e,s,n)}}}setupEventListeners(){this.provider.on("status",(({status:t})=>{"connected"===t?(this.isConnected=!0,this._quiet||console.info("WebSocket connected")):"disconnected"===t&&(this.isConnected=!1,this._quiet||console.info("WebSocket disconnected"))})),this.provider.on("sync",(t=>{t&&!this._quiet&&console.info("WebSocket synced")}))}async connect(){if(!this.isConnected)return new Promise(((t,e)=>{const s=setTimeout((()=>{e(new Error("WebSocket connection timeout"))}),1e4),i=({status:e})=>{"connected"===e&&(clearTimeout(s),this.provider.off("status",i),this.isConnected=!0,t())};this.provider.on("status",i),this.provider.wsconnected&&(clearTimeout(s),this.provider.off("status",i),this.isConnected=!0,t())}))}disconnect(){this.provider&&this.provider.disconnect(),this.isConnected=!1}async reconnect(){return this.disconnect(),this.connect()}destroy(){this.provider&&this.provider.destroy(),this.isConnected=!1}}class Y{type="network";provider;isConnected=!1;isSynced=!1;usesSharedSocket=!1;isDestroyed=!1;connectTimeout=null;pendingConnectReject=null;connectionTimeoutMs;_connectionStatus="disconnected";visibilityHandler=null;onlineHandler=null;get awareness(){return this.provider.awareness}get connectionStatus(){return this._connectionStatus}static sharedWebSocketProvider=null;constructor(t,e,s){const i=s?.name||t,n=s?.url||"ws://localhost:1234";this.connectionTimeoutMs=s?.connectionTimeout??1e4;const o=s?.websocketProvider||Y.sharedWebSocketProvider,c={};void 0!==s?.delay&&(c.delay=s.delay),void 0!==s?.factor&&(c.factor=s.factor),void 0!==s?.maxAttempts&&(c.maxAttempts=s.maxAttempts),void 0!==s?.minDelay&&(c.minDelay=s.minDelay),void 0!==s?.maxDelay&&(c.maxDelay=s.maxDelay);const r=()=>{this.isDestroyed||(this.isConnected=!0,this._connectionStatus="connected",s?.quiet||console.info(`Hocuspocus connected: ${i}`),s?.onConnect&&s.onConnect())},h=()=>{this.isDestroyed||(this.isConnected=!1,this.isSynced=!1,this._connectionStatus="disconnected",s?.quiet||console.info(`Hocuspocus disconnected: ${i}`),s?.onDisconnect&&s.onDisconnect())},l=()=>{this.isDestroyed||(this.isSynced=!0,this._connectionStatus="synced",s?.quiet||console.info(`Hocuspocus synced: ${i}`),s?.onSynced&&s.onSynced())},u=t=>{this.isDestroyed||("connecting"===t.status&&(this._connectionStatus="connecting"),s?.onStatus&&s.onStatus(t))};if(o){this.usesSharedSocket=!0;const t={websocketProvider:o,name:i,document:e,token:s?.token||null,onStatus:u,onConnect:r,onDisconnect:h,onSynced:l,...c};void 0!==s?.forceSyncInterval&&(t.forceSyncInterval=s.forceSyncInterval),s?.onAuthenticationFailed&&(t.onAuthenticationFailed=s.onAuthenticationFailed),this.provider=new a(t),this.provider.attach(),s?.quiet||console.info(`Hocuspocus Provider initialized (multiplexed): ${i}`)}else{this.usesSharedSocket=!1;const t={url:n,name:i,document:e,token:s?.token||null,autoConnect:!1,onStatus:u,onConnect:r,onDisconnect:h,onSynced:l,...c};void 0!==s?.forceSyncInterval&&(t.forceSyncInterval=s.forceSyncInterval),s?.onAuthenticationFailed&&(t.onAuthenticationFailed=s.onAuthenticationFailed),s?.WebSocketPolyfill&&(t.WebSocketPolyfill=s.WebSocketPolyfill),this.provider=new a(t),s?.quiet||console.info(`Hocuspocus Provider initialized: ${n}/${i}`)}this.setupBrowserEventListeners()}setupBrowserEventListeners(){"undefined"!=typeof document&&(this.visibilityHandler=()=>{"visible"!==document.visibilityState||this.isConnected||this.isDestroyed||this.provider.connect()},document.addEventListener("visibilitychange",this.visibilityHandler)),"undefined"!=typeof window&&(this.onlineHandler=()=>{this.isConnected||this.isDestroyed||this.provider.connect()},window.addEventListener("online",this.onlineHandler))}removeBrowserEventListeners(){this.visibilityHandler&&"undefined"!=typeof document&&(document.removeEventListener("visibilitychange",this.visibilityHandler),this.visibilityHandler=null),this.onlineHandler&&"undefined"!=typeof window&&(window.removeEventListener("online",this.onlineHandler),this.onlineHandler=null)}static createSharedWebSocket(t){if(Y.sharedWebSocketProvider)return console.warn("Shared WebSocket already exists. Returning existing instance."),Y.sharedWebSocketProvider;const e={url:t.url};return t.WebSocketPolyfill&&(e.WebSocketPolyfill=t.WebSocketPolyfill),t.onConnect&&(e.onConnect=t.onConnect),t.onDisconnect&&(e.onDisconnect=t.onDisconnect),t.onStatus&&(e.onStatus=t.onStatus),Y.sharedWebSocketProvider=new T(e),console.info(`Shared Hocuspocus WebSocket created: ${t.url}`),Y.sharedWebSocketProvider}static destroySharedWebSocket(){Y.sharedWebSocketProvider&&(Y.sharedWebSocketProvider.destroy(),Y.sharedWebSocketProvider=null,console.info("Shared Hocuspocus WebSocket destroyed"))}static getSharedWebSocket(){return Y.sharedWebSocketProvider}static with(t){return{create:(e,s,i)=>{const n=i?{...t,...i}:t;return new Y(e,s,n)}}}async connect(){if(!this.isSynced&&!this.isDestroyed)return this._connectionStatus="connecting",new Promise(((t,e)=>{this.pendingConnectReject=e,this.connectTimeout=setTimeout((()=>{this.pendingConnectReject=null,this.connectTimeout=null,e(new Error("Hocuspocus connection timeout"))}),this.connectionTimeoutMs);const s=()=>{this.connectTimeout&&(clearTimeout(this.connectTimeout),this.connectTimeout=null),this.pendingConnectReject=null,this.provider.off("synced",s),this.isDestroyed||t()};if(this.provider.on("synced",s),this.provider.isSynced)return this.connectTimeout&&(clearTimeout(this.connectTimeout),this.connectTimeout=null),this.pendingConnectReject=null,this.provider.off("synced",s),void t();this.isConnected||this.usesSharedSocket||this.provider.connect()}))}async reconnect(){return this.disconnect(),this.connect()}disconnect(){this.connectTimeout&&(clearTimeout(this.connectTimeout),this.connectTimeout=null),this.pendingConnectReject&&(this.pendingConnectReject=null),this.provider&&(this.usesSharedSocket?this.provider.detach():this.provider.disconnect()),this.isConnected=!1,this.isSynced=!1,this._connectionStatus="disconnected"}destroy(){this.isDestroyed=!0,this.connectTimeout&&(clearTimeout(this.connectTimeout),this.connectTimeout=null),this.pendingConnectReject&&(this.pendingConnectReject=null),this.removeBrowserEventListeners(),this.provider&&this.provider.destroy(),this.isConnected=!1,this.isSynced=!1,this._connectionStatus="disconnected"}}export{J as BroadcastSyncProvider,Y as HocuspocusSyncProvider,Q as WebSocketSyncProvider}
1
+ export{A as APP_STATE_MIGRATIONS,C as CURRENT_APP_STATE_SCHEMA_VERSION,v as CURRENT_WORKSPACE_SCHEMA_VERSION,D as DEFAULT_BRUSH_CONFIG,r as DEFAULT_LINE_TOOL_CONFIG,q as DEFAULT_TEXT_CONFIG,H as HocuspocusSyncProvider,I as IndexedDBSyncProvider,u as KritzelAlignment,o as KritzelAnchorManager,f as KritzelBrushTool,l as KritzelCursorHelper,h as KritzelEraserTool,d as KritzelGroup,b as KritzelImage,i as KritzelImageTool,c as KritzelLine,g as KritzelLineTool,a as KritzelPath,m as KritzelSelectionTool,e as KritzelShape,k as KritzelShapeTool,K as KritzelText,j as KritzelTextTool,p as KritzelThemeManager,n as KritzelWorkspace,S as ShapeType,W as WORKSPACE_EXPORT_VERSION,x as WORKSPACE_MIGRATIONS,t as darkTheme,s as lightTheme,w as runMigrations}from"./p-D6whgl7G.js";import*as T from"yjs";import{WebsocketProvider as y}from"y-websocket";import"y-indexeddb";const E=Math.floor,_=127,z=Number.MAX_SAFE_INTEGER;class P{constructor(){this.cpos=0,this.cbuf=new Uint8Array(100),this.bufs=[]}}const R=()=>new P,U=t=>{const s=new Uint8Array((t=>{let s=t.cpos;for(let e=0;e<t.bufs.length;e++)s+=t.bufs[e].length;return s})(t));let e=0;for(let i=0;i<t.bufs.length;i++){const n=t.bufs[i];s.set(n,e),e+=n.length}return s.set(new Uint8Array(t.cbuf.buffer,0,t.cpos),e),s},O=(t,s)=>{const e=t.cbuf.length;t.cpos===e&&(t.bufs.push(t.cbuf),t.cbuf=new Uint8Array(2*e),t.cpos=0),t.cbuf[t.cpos++]=s},N=(t,s)=>{for(;s>_;)O(t,128|_&s),s=E(s/128);O(t,_&s)},M=(t,s)=>{N(t,s.byteLength),((t,s)=>{const e=t.cbuf.length,i=t.cpos,n=((t,s)=>t<s?t:s)(e-i,s.length),r=s.length-n;t.cbuf.set(s.subarray(0,n),i),t.cpos+=n,r>0&&(t.bufs.push(t.cbuf),t.cbuf=new Uint8Array(((t,s)=>t>s?t:s)(2*e,r)),t.cbuf.set(s.subarray(n)),t.cpos=r)})(t,s)},L=t=>new Error(t),B=L("Unexpected end of array"),G=L("Integer out of Range");class F{constructor(t){this.arr=t,this.pos=0}}const V=t=>((t,s)=>{const e=new Uint8Array(t.arr.buffer,t.pos+t.arr.byteOffset,s);return t.pos+=s,e})(t,$(t)),$=t=>{let s=0,e=1;const i=t.arr.length;for(;t.pos<i;){const i=t.arr[t.pos++];if(s+=(i&_)*e,e*=128,i<128)return s;if(s>z)throw G}throw B};class X{type="local";doc;channel;_synced=!1;constructor(t,s,e){this.doc=s,this.channel=new BroadcastChannel(t),this.channel.onmessage=t=>{this.handleMessage(t.data)},this.doc.on("update",this.handleDocUpdate),this.broadcastSync(),setTimeout((()=>{this._synced=!0}),100),e?.quiet||console.info(`BroadcastChannel Provider initialized: ${t}`)}handleDocUpdate=(t,s)=>{if(s!==this){const s=R();N(s,0),M(s,t),this.channel.postMessage(U(s))}};handleMessage(t){const s=(e=new Uint8Array(t),new F(e));var e;switch($(s)){case 0:const t=V(s);T.applyUpdate(this.doc,t,this);break;case 1:this.broadcastSync();break;case 2:const e=V(s),i=T.encodeStateAsUpdate(this.doc,e);if(i.length>0){const t=R();N(t,0),M(t,i),this.channel.postMessage(U(t))}}}broadcastSync(){const t=R();N(t,2),M(t,T.encodeStateVector(this.doc)),this.channel.postMessage(U(t))}async connect(){if(!this._synced)return new Promise((t=>{const s=()=>{this._synced?t():setTimeout(s,50)};s()}))}disconnect(){}async reconnect(){return this.disconnect(),this.connect()}destroy(){this.doc.off("update",this.handleDocUpdate),this.channel.close()}}class J{type="network";provider;isConnected=!1;_quiet=!1;get awareness(){return this.provider.awareness}constructor(t,s,e){const i=e?.url||"ws://localhost:1234",n=e?.roomName||t;this.provider=new y(i,n,s,{params:e?.params,protocols:e?.protocols,WebSocketPolyfill:e?.WebSocketPolyfill,awareness:e?.awareness,maxBackoffTime:e?.maxBackoffTime,disableBc:!0}),this._quiet=e?.quiet??!1,this.setupEventListeners(),this._quiet||console.info(`WebSocket Provider initialized: ${i}/${n}`)}static with(t){return{create:(s,e,i)=>{const n=i?{...t,...i}:t;return new J(s,e,n)}}}setupEventListeners(){this.provider.on("status",(({status:t})=>{"connected"===t?(this.isConnected=!0,this._quiet||console.info("WebSocket connected")):"disconnected"===t&&(this.isConnected=!1,this._quiet||console.info("WebSocket disconnected"))})),this.provider.on("sync",(t=>{t&&!this._quiet&&console.info("WebSocket synced")}))}async connect(){if(!this.isConnected)return new Promise(((t,s)=>{const e=setTimeout((()=>{s(new Error("WebSocket connection timeout"))}),1e4),i=({status:s})=>{"connected"===s&&(clearTimeout(e),this.provider.off("status",i),this.isConnected=!0,t())};this.provider.on("status",i),this.provider.wsconnected&&(clearTimeout(e),this.provider.off("status",i),this.isConnected=!0,t())}))}disconnect(){this.provider&&this.provider.disconnect(),this.isConnected=!1}async reconnect(){return this.disconnect(),this.connect()}destroy(){this.provider&&this.provider.destroy(),this.isConnected=!1}}export{X as BroadcastSyncProvider,J as WebSocketSyncProvider}