@tldraw/editor 5.7.0-internal.5fe8922939ec → 5.7.0-internal.d83eaf8e2d50
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/index.d.ts +9 -48
- package/dist-cjs/index.js +1 -3
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/lib/TldrawEditor.js +2 -10
- package/dist-cjs/lib/TldrawEditor.js.map +2 -2
- package/dist-cjs/lib/config/createTLStore.js +1 -3
- package/dist-cjs/lib/config/createTLStore.js.map +2 -2
- package/dist-cjs/lib/editor/Editor.js +8 -37
- package/dist-cjs/lib/editor/Editor.js.map +2 -2
- package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js +2 -44
- package/dist-cjs/lib/editor/managers/HistoryManager/HistoryManager.js.map +2 -2
- package/dist-cjs/lib/editor/types/emit-types.js.map +1 -1
- package/dist-cjs/lib/license/LicenseManager.js +16 -8
- package/dist-cjs/lib/license/LicenseManager.js.map +2 -2
- package/dist-cjs/version.js +3 -3
- package/dist-cjs/version.js.map +1 -1
- package/dist-esm/index.d.mts +9 -48
- package/dist-esm/index.mjs +1 -3
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/TldrawEditor.mjs +3 -11
- package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
- package/dist-esm/lib/config/createTLStore.mjs +1 -3
- package/dist-esm/lib/config/createTLStore.mjs.map +2 -2
- package/dist-esm/lib/editor/Editor.mjs +8 -38
- package/dist-esm/lib/editor/Editor.mjs.map +2 -2
- package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs +2 -44
- package/dist-esm/lib/editor/managers/HistoryManager/HistoryManager.mjs.map +2 -2
- package/dist-esm/lib/license/LicenseManager.mjs +16 -8
- package/dist-esm/lib/license/LicenseManager.mjs.map +2 -2
- package/dist-esm/version.mjs +3 -3
- package/dist-esm/version.mjs.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +1 -6
- package/src/lib/TldrawEditor.tsx +1 -10
- package/src/lib/config/createTLStore.ts +4 -11
- package/src/lib/editor/Editor.ts +8 -47
- package/src/lib/editor/managers/HistoryManager/HistoryManager.ts +2 -58
- package/src/lib/editor/types/emit-types.ts +2 -14
- package/src/lib/license/LicenseManager.test.ts +62 -21
- package/src/lib/license/LicenseManager.ts +25 -11
- package/src/version.ts +3 -3
- package/dist-cjs/lib/editor/managers/AuthorizeManager/AuthorizeManager.js +0 -94
- package/dist-cjs/lib/editor/managers/AuthorizeManager/AuthorizeManager.js.map +0 -7
- package/dist-cjs/lib/hooks/useStableAuthorize.js +0 -45
- package/dist-cjs/lib/hooks/useStableAuthorize.js.map +0 -7
- package/dist-esm/lib/editor/managers/AuthorizeManager/AuthorizeManager.mjs +0 -76
- package/dist-esm/lib/editor/managers/AuthorizeManager/AuthorizeManager.mjs.map +0 -7
- package/dist-esm/lib/hooks/useStableAuthorize.mjs +0 -25
- package/dist-esm/lib/hooks/useStableAuthorize.mjs.map +0 -7
- package/src/lib/editor/managers/AuthorizeManager/AuthorizeManager.ts +0 -110
- package/src/lib/hooks/useStableAuthorize.ts +0 -31
|
@@ -4,7 +4,7 @@ import { publishDates, version } from '../../version'
|
|
|
4
4
|
import { getDefaultCdnBaseUrl } from '../utils/assets'
|
|
5
5
|
import { importPublicKey, str2ab } from '../utils/licensing'
|
|
6
6
|
|
|
7
|
-
const GRACE_PERIOD_DAYS =
|
|
7
|
+
const GRACE_PERIOD_DAYS = 5
|
|
8
8
|
|
|
9
9
|
export const FLAGS = {
|
|
10
10
|
// -- MUTUALLY EXCLUSIVE FLAGS
|
|
@@ -30,6 +30,8 @@ export const FLAGS = {
|
|
|
30
30
|
// Commenting is the first sub-feature of collaboration.
|
|
31
31
|
FEAT_COMMENTING: 1 << 7,
|
|
32
32
|
TRACKED: 1 << 8,
|
|
33
|
+
// Libraries is a sub-feature of collaboration.
|
|
34
|
+
FEAT_LIBRARIES: 1 << 9,
|
|
33
35
|
}
|
|
34
36
|
const HIGHEST_FLAG = Math.max(...Object.values(FLAGS))
|
|
35
37
|
|
|
@@ -80,19 +82,21 @@ export interface LicenseInfo {
|
|
|
80
82
|
|
|
81
83
|
/**
|
|
82
84
|
* Names of the licensable product features gated by the license. `collaboration` is an umbrella
|
|
83
|
-
* that also grants all of its sub-features (currently
|
|
85
|
+
* that also grants all of its sub-features (currently `commenting` and `libraries`).
|
|
84
86
|
*
|
|
85
87
|
* @internal
|
|
86
88
|
*/
|
|
87
|
-
export type LicenseFeatureName = 'collaboration' | 'commenting'
|
|
89
|
+
export type LicenseFeatureName = 'collaboration' | 'commenting' | 'libraries'
|
|
88
90
|
|
|
89
91
|
const NO_FEATURES: Readonly<Record<LicenseFeatureName, boolean>> = {
|
|
90
92
|
collaboration: false,
|
|
91
93
|
commenting: false,
|
|
94
|
+
libraries: false,
|
|
92
95
|
}
|
|
93
96
|
const ALL_FEATURES: Readonly<Record<LicenseFeatureName, boolean>> = {
|
|
94
97
|
collaboration: true,
|
|
95
98
|
commenting: true,
|
|
99
|
+
libraries: true,
|
|
96
100
|
}
|
|
97
101
|
|
|
98
102
|
/** @internal */
|
|
@@ -102,7 +106,7 @@ export type LicenseState =
|
|
|
102
106
|
| 'licensed-with-watermark' // License is valid but shows watermark (evaluation licenses, WITH_WATERMARK licenses)
|
|
103
107
|
| 'unlicensed' // No valid license found or license is invalid (development)
|
|
104
108
|
| 'unlicensed-production' // No valid license in production deployment (missing, invalid, or wrong domain)
|
|
105
|
-
| 'expired' // License has been expired (
|
|
109
|
+
| 'expired' // License has been expired (+ grace period days past expiration for regular licenses, immediately for evaluation licenses)
|
|
106
110
|
/** @internal */
|
|
107
111
|
export type InvalidLicenseReason =
|
|
108
112
|
| 'invalid-license-key'
|
|
@@ -136,6 +140,7 @@ export interface ValidLicenseKeyResult {
|
|
|
136
140
|
isEvaluationLicenseExpired: boolean
|
|
137
141
|
isCollaborationEnabled: boolean
|
|
138
142
|
isCommentingEnabled: boolean
|
|
143
|
+
isLibrariesEnabled: boolean
|
|
139
144
|
daysSinceExpiry: number
|
|
140
145
|
}
|
|
141
146
|
|
|
@@ -253,7 +258,7 @@ export class LicenseManager {
|
|
|
253
258
|
/**
|
|
254
259
|
* The license tldraw's own domains get without a key. It keeps the watermark — our properties
|
|
255
260
|
* advertise the SDK the same way any other watermarked deployment does — and grants the
|
|
256
|
-
* collaboration features our apps are built on.
|
|
261
|
+
* collaboration and libraries features our apps are built on.
|
|
257
262
|
*
|
|
258
263
|
* Every expiry-derived field is a literal rather than something computed from a date, because
|
|
259
264
|
* there is no key here to expire: the grant lasts as long as the host is ours.
|
|
@@ -264,7 +269,11 @@ export class LicenseManager {
|
|
|
264
269
|
license: {
|
|
265
270
|
id: CANONICAL_HOST_LICENSE_ID,
|
|
266
271
|
hosts: [window.location.hostname.toLowerCase()],
|
|
267
|
-
flags:
|
|
272
|
+
flags:
|
|
273
|
+
FLAGS.WITH_WATERMARK |
|
|
274
|
+
FLAGS.FEAT_COLLABORATION |
|
|
275
|
+
FLAGS.FEAT_COMMENTING |
|
|
276
|
+
FLAGS.FEAT_LIBRARIES,
|
|
268
277
|
expiryDate: CANONICAL_HOST_NEVER_EXPIRES.toISOString(),
|
|
269
278
|
},
|
|
270
279
|
isDevelopment: this.isDevelopment,
|
|
@@ -281,6 +290,7 @@ export class LicenseManager {
|
|
|
281
290
|
isEvaluationLicenseExpired: false,
|
|
282
291
|
isCollaborationEnabled: true,
|
|
283
292
|
isCommentingEnabled: true,
|
|
293
|
+
isLibrariesEnabled: true,
|
|
284
294
|
daysSinceExpiry: 0,
|
|
285
295
|
}
|
|
286
296
|
}
|
|
@@ -450,11 +460,13 @@ export class LicenseManager {
|
|
|
450
460
|
const isPerpetualLicenseExpired =
|
|
451
461
|
isPerpetualLicense && this.isPerpetualLicenseExpired(expiryDate)
|
|
452
462
|
|
|
453
|
-
// The collaboration umbrella grants all of its sub-features, so commenting
|
|
454
|
-
// by
|
|
463
|
+
// The collaboration umbrella grants all of its sub-features, so commenting and libraries
|
|
464
|
+
// are each enabled by their own flag or the collaboration flag.
|
|
455
465
|
const isCollaborationEnabled = this.isFlagEnabled(licenseInfo.flags, FLAGS.FEAT_COLLABORATION)
|
|
456
466
|
const isCommentingEnabled =
|
|
457
467
|
isCollaborationEnabled || this.isFlagEnabled(licenseInfo.flags, FLAGS.FEAT_COMMENTING)
|
|
468
|
+
const isLibrariesEnabled =
|
|
469
|
+
isCollaborationEnabled || this.isFlagEnabled(licenseInfo.flags, FLAGS.FEAT_LIBRARIES)
|
|
458
470
|
|
|
459
471
|
// For perpetual licenses, the calendar expiry date only gates access to future
|
|
460
472
|
// major/minor releases; it does not "expire" the license itself. While the user
|
|
@@ -481,6 +493,7 @@ export class LicenseManager {
|
|
|
481
493
|
isEvaluationLicense && this.isEvaluationLicenseExpired(expiryDate),
|
|
482
494
|
isCollaborationEnabled,
|
|
483
495
|
isCommentingEnabled,
|
|
496
|
+
isLibrariesEnabled,
|
|
484
497
|
daysSinceExpiry,
|
|
485
498
|
}
|
|
486
499
|
this.outputLicenseInfoIfNeeded(result)
|
|
@@ -674,7 +687,7 @@ export function getEnabledFeatures(
|
|
|
674
687
|
return { ...ALL_FEATURES }
|
|
675
688
|
}
|
|
676
689
|
|
|
677
|
-
// Features require an active, valid license. Both the
|
|
690
|
+
// Features require an active, valid license. Both the grace-period 'licensed' state and
|
|
678
691
|
// the watermark state count as valid; unlicensed/expired/pending do not.
|
|
679
692
|
if (
|
|
680
693
|
!result.isLicenseParseable ||
|
|
@@ -693,6 +706,7 @@ export function getEnabledFeatures(
|
|
|
693
706
|
return {
|
|
694
707
|
collaboration: result.isCollaborationEnabled,
|
|
695
708
|
commenting: result.isCommentingEnabled,
|
|
709
|
+
libraries: result.isLibrariesEnabled,
|
|
696
710
|
}
|
|
697
711
|
}
|
|
698
712
|
|
|
@@ -748,7 +762,7 @@ export function getLicenseState(
|
|
|
748
762
|
// Handle expired regular licenses (both annual and perpetual)
|
|
749
763
|
if (result.isPerpetualLicenseExpired || result.isAnnualLicenseExpired) {
|
|
750
764
|
outputMessages([
|
|
751
|
-
|
|
765
|
+
`Your tldraw license has been expired for more than ${GRACE_PERIOD_DAYS} days!`,
|
|
752
766
|
`Please reach out to ${LICENSE_EMAIL} to renew your license.`,
|
|
753
767
|
])
|
|
754
768
|
return 'expired'
|
|
@@ -762,7 +776,7 @@ export function getLicenseState(
|
|
|
762
776
|
`License expired ${daysSinceExpiry} days ago.`,
|
|
763
777
|
`Please reach out to ${LICENSE_EMAIL} to renew your license.`,
|
|
764
778
|
])
|
|
765
|
-
// Within
|
|
779
|
+
// Within grace period: still licensed (no watermark)
|
|
766
780
|
return 'licensed'
|
|
767
781
|
}
|
|
768
782
|
|
package/src/version.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// This file is automatically generated by internal/scripts/refresh-assets.ts.
|
|
2
2
|
// Do not edit manually. Or do, I'm a comment, not a cop.
|
|
3
3
|
|
|
4
|
-
export const version = '5.7.0-internal.
|
|
4
|
+
export const version = '5.7.0-internal.d83eaf8e2d50'
|
|
5
5
|
export const publishDates = {
|
|
6
6
|
major: '2026-05-06T16:28:18.473Z',
|
|
7
|
-
minor: '2026-11-
|
|
8
|
-
patch: '2026-11-
|
|
7
|
+
minor: '2026-11-25T16:42:19.076Z',
|
|
8
|
+
patch: '2026-11-25T16:42:19.076Z',
|
|
9
9
|
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var AuthorizeManager_exports = {};
|
|
20
|
-
__export(AuthorizeManager_exports, {
|
|
21
|
-
AuthorizeManager: () => AuthorizeManager,
|
|
22
|
-
getAuthorizeUserId: () => getAuthorizeUserId,
|
|
23
|
-
judgeWrite: () => judgeWrite
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(AuthorizeManager_exports);
|
|
26
|
-
var import_tlschema = require("@tldraw/tlschema");
|
|
27
|
-
var import_utils = require("@tldraw/utils");
|
|
28
|
-
var import_EditorManager = require("../EditorManager");
|
|
29
|
-
class AuthorizeManager extends import_EditorManager.EditorManager {
|
|
30
|
-
constructor(editor) {
|
|
31
|
-
super(editor);
|
|
32
|
-
const refusals = editor.store.props.collaboration?.refusals;
|
|
33
|
-
if (refusals) {
|
|
34
|
-
this.register(
|
|
35
|
-
refusals.subscribe((refusal) => editor.emit("refused", { ...refusal, source: "server" }))
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
const authorize = editor.store.props.authorize;
|
|
39
|
-
if (!authorize?.write) return;
|
|
40
|
-
this.register(
|
|
41
|
-
editor.store.addCommitCheck((diff) => {
|
|
42
|
-
const refusal = judgeWrite(editor.store, authorize, diff, getAuthorizeUserId(editor));
|
|
43
|
-
if (!refusal) return null;
|
|
44
|
-
return { afterRevert: () => editor.emit("refused", { ...refusal, source: "local" }) };
|
|
45
|
-
})
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function getAuthorizeUserId(editor) {
|
|
50
|
-
const fromRoom = editor.store.props.collaboration?.userId?.get();
|
|
51
|
-
return fromRoom === void 0 ? editor.user.getExternalId() : fromRoom;
|
|
52
|
-
}
|
|
53
|
-
function judgeWrite(store, authorize, diff, userId) {
|
|
54
|
-
const isDocument = (record) => store.scopedTypes.document.has(record.typeName);
|
|
55
|
-
const changes = [];
|
|
56
|
-
for (const next of Object.values(diff.added)) {
|
|
57
|
-
if (isDocument(next)) changes.push({ type: "create", id: next.id, prev: null, next });
|
|
58
|
-
}
|
|
59
|
-
for (const [prev, next] of Object.values(diff.updated)) {
|
|
60
|
-
if (isDocument(next) && !(0, import_utils.isEqual)(prev, next)) {
|
|
61
|
-
changes.push({ type: "update", id: next.id, prev, next });
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
for (const prev of Object.values(diff.removed)) {
|
|
65
|
-
if (isDocument(prev)) changes.push({ type: "delete", id: prev.id, prev, next: null });
|
|
66
|
-
}
|
|
67
|
-
const write = authorize.write;
|
|
68
|
-
if (!write || changes.length === 0) return null;
|
|
69
|
-
const readDocument = (id) => {
|
|
70
|
-
const record = store.get(id);
|
|
71
|
-
return record && isDocument(record) ? record : void 0;
|
|
72
|
-
};
|
|
73
|
-
const after = { get: readDocument };
|
|
74
|
-
const before = {
|
|
75
|
-
get: (id) => {
|
|
76
|
-
if (id in diff.added) return void 0;
|
|
77
|
-
if (id in diff.updated) return diff.updated[id][0];
|
|
78
|
-
if (id in diff.removed) return diff.removed[id];
|
|
79
|
-
return readDocument(id);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
try {
|
|
83
|
-
return write(changes, {
|
|
84
|
-
userId,
|
|
85
|
-
can: (0, import_tlschema.bindAuthorizeCan)(authorize.can, { userId }),
|
|
86
|
-
before,
|
|
87
|
-
after
|
|
88
|
-
});
|
|
89
|
-
} catch (e) {
|
|
90
|
-
console.error("authorize.write threw; refusing the change", e);
|
|
91
|
-
return { reason: "error" };
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
//# sourceMappingURL=AuthorizeManager.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../../src/lib/editor/managers/AuthorizeManager/AuthorizeManager.ts"],
|
|
4
|
-
"sourcesContent": ["import { RecordsDiff } from '@tldraw/store'\nimport {\n\tbindAuthorizeCan,\n\tTLAuthorize,\n\tTLRecord,\n\tTLRecordChange,\n\tTLRecordGetter,\n\tTLStore,\n\tTLWriteRefusal,\n} from '@tldraw/tlschema'\nimport { isEqual } from '@tldraw/utils'\nimport type { Editor } from '../../Editor'\nimport { EditorManager } from '../EditorManager'\n\n/**\n * Runs the store's `authorize.write` on each of this user's operations, as a sync room runs it on\n * each push. The store judges an operation once its side effects have settled, and reverts a\n * refused one before history or listeners see it. Then the editor emits `'refused'`. The room's\n * refusals, from `collaboration.refusals`, go out on the same event.\n *\n * @internal\n */\nexport class AuthorizeManager extends EditorManager {\n\tconstructor(editor: Editor) {\n\t\tsuper(editor)\n\t\tconst refusals = editor.store.props.collaboration?.refusals\n\t\tif (refusals) {\n\t\t\tthis.register(\n\t\t\t\trefusals.subscribe((refusal) => editor.emit('refused', { ...refusal, source: 'server' }))\n\t\t\t)\n\t\t}\n\t\tconst authorize = editor.store.props.authorize\n\t\tif (!authorize?.write) return\n\t\tthis.register(\n\t\t\teditor.store.addCommitCheck((diff) => {\n\t\t\t\tconst refusal = judgeWrite(editor.store, authorize, diff, getAuthorizeUserId(editor))\n\t\t\t\tif (!refusal) return null\n\t\t\t\treturn { afterRevert: () => editor.emit('refused', { ...refusal, source: 'local' }) }\n\t\t\t})\n\t\t)\n\t}\n}\n\n/**\n * The user id the authorize hooks get. Once the room has said which user it judges this session\n * as, the editor uses that one, so that both sides judge the same user (a guest is `null` in the\n * room but has a local id in the editor). Before that, it is the editor user's id.\n *\n * @internal\n */\nexport function getAuthorizeUserId(editor: Editor): string | null {\n\tconst fromRoom = editor.store.props.collaboration?.userId?.get()\n\treturn fromRoom === undefined ? editor.user.getExternalId() : fromRoom\n}\n\n/**\n * Judges one settled operation with the write hook. Only document records count, because those\n * are the only records the room sees.\n *\n * @internal\n */\nexport function judgeWrite(\n\tstore: TLStore,\n\tauthorize: TLAuthorize,\n\tdiff: RecordsDiff<TLRecord>,\n\tuserId: string | null\n): TLWriteRefusal | null {\n\tconst isDocument = (record: TLRecord) => store.scopedTypes.document.has(record.typeName)\n\tconst changes: TLRecordChange[] = []\n\tfor (const next of Object.values(diff.added)) {\n\t\tif (isDocument(next)) changes.push({ type: 'create', id: next.id, prev: null, next })\n\t}\n\tfor (const [prev, next] of Object.values(diff.updated)) {\n\t\tif (isDocument(next) && !isEqual(prev, next)) {\n\t\t\tchanges.push({ type: 'update', id: next.id, prev, next })\n\t\t}\n\t}\n\tfor (const prev of Object.values(diff.removed)) {\n\t\tif (isDocument(prev)) changes.push({ type: 'delete', id: prev.id, prev, next: null })\n\t}\n\tconst write = authorize.write\n\tif (!write || changes.length === 0) return null\n\n\tconst readDocument = (id: TLRecord['id']) => {\n\t\tconst record = store.get(id)\n\t\treturn record && isDocument(record) ? record : undefined\n\t}\n\tconst after = { get: readDocument } as TLRecordGetter\n\tconst before = {\n\t\tget: (id: TLRecord['id']) => {\n\t\t\tif (id in diff.added) return undefined\n\t\t\tif (id in diff.updated) return diff.updated[id][0]\n\t\t\tif (id in diff.removed) return diff.removed[id]\n\t\t\treturn readDocument(id)\n\t\t},\n\t} as TLRecordGetter\n\n\ttry {\n\t\treturn write(changes, {\n\t\t\tuserId,\n\t\t\tcan: bindAuthorizeCan(authorize.can, { userId }),\n\t\t\tbefore,\n\t\t\tafter,\n\t\t})\n\t} catch (e) {\n\t\t// the room refuses when its hook throws, so the editor must too\n\t\tconsole.error('authorize.write threw; refusing the change', e)\n\t\treturn { reason: 'error' }\n\t}\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,sBAQO;AACP,mBAAwB;AAExB,2BAA8B;AAUvB,MAAM,yBAAyB,mCAAc;AAAA,EACnD,YAAY,QAAgB;AAC3B,UAAM,MAAM;AACZ,UAAM,WAAW,OAAO,MAAM,MAAM,eAAe;AACnD,QAAI,UAAU;AACb,WAAK;AAAA,QACJ,SAAS,UAAU,CAAC,YAAY,OAAO,KAAK,WAAW,EAAE,GAAG,SAAS,QAAQ,SAAS,CAAC,CAAC;AAAA,MACzF;AAAA,IACD;AACA,UAAM,YAAY,OAAO,MAAM,MAAM;AACrC,QAAI,CAAC,WAAW,MAAO;AACvB,SAAK;AAAA,MACJ,OAAO,MAAM,eAAe,CAAC,SAAS;AACrC,cAAM,UAAU,WAAW,OAAO,OAAO,WAAW,MAAM,mBAAmB,MAAM,CAAC;AACpF,YAAI,CAAC,QAAS,QAAO;AACrB,eAAO,EAAE,aAAa,MAAM,OAAO,KAAK,WAAW,EAAE,GAAG,SAAS,QAAQ,QAAQ,CAAC,EAAE;AAAA,MACrF,CAAC;AAAA,IACF;AAAA,EACD;AACD;AASO,SAAS,mBAAmB,QAA+B;AACjE,QAAM,WAAW,OAAO,MAAM,MAAM,eAAe,QAAQ,IAAI;AAC/D,SAAO,aAAa,SAAY,OAAO,KAAK,cAAc,IAAI;AAC/D;AAQO,SAAS,WACf,OACA,WACA,MACA,QACwB;AACxB,QAAM,aAAa,CAAC,WAAqB,MAAM,YAAY,SAAS,IAAI,OAAO,QAAQ;AACvF,QAAM,UAA4B,CAAC;AACnC,aAAW,QAAQ,OAAO,OAAO,KAAK,KAAK,GAAG;AAC7C,QAAI,WAAW,IAAI,EAAG,SAAQ,KAAK,EAAE,MAAM,UAAU,IAAI,KAAK,IAAI,MAAM,MAAM,KAAK,CAAC;AAAA,EACrF;AACA,aAAW,CAAC,MAAM,IAAI,KAAK,OAAO,OAAO,KAAK,OAAO,GAAG;AACvD,QAAI,WAAW,IAAI,KAAK,KAAC,sBAAQ,MAAM,IAAI,GAAG;AAC7C,cAAQ,KAAK,EAAE,MAAM,UAAU,IAAI,KAAK,IAAI,MAAM,KAAK,CAAC;AAAA,IACzD;AAAA,EACD;AACA,aAAW,QAAQ,OAAO,OAAO,KAAK,OAAO,GAAG;AAC/C,QAAI,WAAW,IAAI,EAAG,SAAQ,KAAK,EAAE,MAAM,UAAU,IAAI,KAAK,IAAI,MAAM,MAAM,KAAK,CAAC;AAAA,EACrF;AACA,QAAM,QAAQ,UAAU;AACxB,MAAI,CAAC,SAAS,QAAQ,WAAW,EAAG,QAAO;AAE3C,QAAM,eAAe,CAAC,OAAuB;AAC5C,UAAM,SAAS,MAAM,IAAI,EAAE;AAC3B,WAAO,UAAU,WAAW,MAAM,IAAI,SAAS;AAAA,EAChD;AACA,QAAM,QAAQ,EAAE,KAAK,aAAa;AAClC,QAAM,SAAS;AAAA,IACd,KAAK,CAAC,OAAuB;AAC5B,UAAI,MAAM,KAAK,MAAO,QAAO;AAC7B,UAAI,MAAM,KAAK,QAAS,QAAO,KAAK,QAAQ,EAAE,EAAE,CAAC;AACjD,UAAI,MAAM,KAAK,QAAS,QAAO,KAAK,QAAQ,EAAE;AAC9C,aAAO,aAAa,EAAE;AAAA,IACvB;AAAA,EACD;AAEA,MAAI;AACH,WAAO,MAAM,SAAS;AAAA,MACrB;AAAA,MACA,SAAK,kCAAiB,UAAU,KAAK,EAAE,OAAO,CAAC;AAAA,MAC/C;AAAA,MACA;AAAA,IACD,CAAC;AAAA,EACF,SAAS,GAAG;AAEX,YAAQ,MAAM,8CAA8C,CAAC;AAC7D,WAAO,EAAE,QAAQ,QAAQ;AAAA,EAC1B;AACD;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var useStableAuthorize_exports = {};
|
|
20
|
-
__export(useStableAuthorize_exports, {
|
|
21
|
-
useStableAuthorize: () => useStableAuthorize
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(useStableAuthorize_exports);
|
|
24
|
-
var import_state = require("@tldraw/state");
|
|
25
|
-
var import_react = require("react");
|
|
26
|
-
function useStableAuthorize(authorize) {
|
|
27
|
-
const [current] = (0, import_react.useState)(() => (0, import_state.atom)("authorize", authorize));
|
|
28
|
-
(0, import_react.useLayoutEffect)(() => {
|
|
29
|
-
current.set(authorize);
|
|
30
|
-
}, [current, authorize]);
|
|
31
|
-
const hasWrite = !!authorize?.write;
|
|
32
|
-
const hasRead = !!authorize?.read;
|
|
33
|
-
const hasCan = !!authorize?.can;
|
|
34
|
-
return (0, import_react.useMemo)(() => {
|
|
35
|
-
if (!hasWrite && !hasRead && !hasCan) return void 0;
|
|
36
|
-
const stable = {};
|
|
37
|
-
if (hasWrite) stable.write = (changes, ctx) => current.get()?.write?.(changes, ctx) ?? null;
|
|
38
|
-
if (hasRead) stable.read = (record, ctx) => current.get()?.read?.(record, ctx) ?? true;
|
|
39
|
-
if (hasCan) {
|
|
40
|
-
stable.can = (permission, data, ctx) => current.get()?.can?.(permission, data, ctx) ?? true;
|
|
41
|
-
}
|
|
42
|
-
return stable;
|
|
43
|
-
}, [current, hasWrite, hasRead, hasCan]);
|
|
44
|
-
}
|
|
45
|
-
//# sourceMappingURL=useStableAuthorize.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/lib/hooks/useStableAuthorize.ts"],
|
|
4
|
-
"sourcesContent": ["import { atom } from '@tldraw/state'\nimport { TLAuthorize } from '@tldraw/tlschema'\nimport { useLayoutEffect, useMemo, useState } from 'react'\n\n/**\n * Keeps one authorize object for the life of a store, so that an `authorize` written inline (for\n * example `composeAuthorize(...)` in a component) does not make a new store on every render. Each\n * hook calls the latest object, and reads it from a signal, so a reactive `editor.can` check runs\n * again when the object changes. A new store is made only when a hook is added or removed.\n *\n * @internal\n */\nexport function useStableAuthorize(authorize: TLAuthorize | undefined): TLAuthorize | undefined {\n\tconst [current] = useState(() => atom('authorize', authorize))\n\tuseLayoutEffect(() => {\n\t\tcurrent.set(authorize)\n\t}, [current, authorize])\n\tconst hasWrite = !!authorize?.write\n\tconst hasRead = !!authorize?.read\n\tconst hasCan = !!authorize?.can\n\treturn useMemo(() => {\n\t\tif (!hasWrite && !hasRead && !hasCan) return undefined\n\t\tconst stable: TLAuthorize = {}\n\t\tif (hasWrite) stable.write = (changes, ctx) => current.get()?.write?.(changes, ctx) ?? null\n\t\tif (hasRead) stable.read = (record, ctx) => current.get()?.read?.(record, ctx) ?? true\n\t\tif (hasCan) {\n\t\t\tstable.can = (permission, data, ctx) => current.get()?.can?.(permission, data, ctx) ?? true\n\t\t}\n\t\treturn stable\n\t}, [current, hasWrite, hasRead, hasCan])\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAqB;AAErB,mBAAmD;AAU5C,SAAS,mBAAmB,WAA6D;AAC/F,QAAM,CAAC,OAAO,QAAI,uBAAS,UAAM,mBAAK,aAAa,SAAS,CAAC;AAC7D,oCAAgB,MAAM;AACrB,YAAQ,IAAI,SAAS;AAAA,EACtB,GAAG,CAAC,SAAS,SAAS,CAAC;AACvB,QAAM,WAAW,CAAC,CAAC,WAAW;AAC9B,QAAM,UAAU,CAAC,CAAC,WAAW;AAC7B,QAAM,SAAS,CAAC,CAAC,WAAW;AAC5B,aAAO,sBAAQ,MAAM;AACpB,QAAI,CAAC,YAAY,CAAC,WAAW,CAAC,OAAQ,QAAO;AAC7C,UAAM,SAAsB,CAAC;AAC7B,QAAI,SAAU,QAAO,QAAQ,CAAC,SAAS,QAAQ,QAAQ,IAAI,GAAG,QAAQ,SAAS,GAAG,KAAK;AACvF,QAAI,QAAS,QAAO,OAAO,CAAC,QAAQ,QAAQ,QAAQ,IAAI,GAAG,OAAO,QAAQ,GAAG,KAAK;AAClF,QAAI,QAAQ;AACX,aAAO,MAAM,CAAC,YAAY,MAAM,QAAQ,QAAQ,IAAI,GAAG,MAAM,YAAY,MAAM,GAAG,KAAK;AAAA,IACxF;AACA,WAAO;AAAA,EACR,GAAG,CAAC,SAAS,UAAU,SAAS,MAAM,CAAC;AACxC;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
bindAuthorizeCan
|
|
3
|
-
} from "@tldraw/tlschema";
|
|
4
|
-
import { isEqual } from "@tldraw/utils";
|
|
5
|
-
import { EditorManager } from "../EditorManager.mjs";
|
|
6
|
-
class AuthorizeManager extends EditorManager {
|
|
7
|
-
constructor(editor) {
|
|
8
|
-
super(editor);
|
|
9
|
-
const refusals = editor.store.props.collaboration?.refusals;
|
|
10
|
-
if (refusals) {
|
|
11
|
-
this.register(
|
|
12
|
-
refusals.subscribe((refusal) => editor.emit("refused", { ...refusal, source: "server" }))
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
const authorize = editor.store.props.authorize;
|
|
16
|
-
if (!authorize?.write) return;
|
|
17
|
-
this.register(
|
|
18
|
-
editor.store.addCommitCheck((diff) => {
|
|
19
|
-
const refusal = judgeWrite(editor.store, authorize, diff, getAuthorizeUserId(editor));
|
|
20
|
-
if (!refusal) return null;
|
|
21
|
-
return { afterRevert: () => editor.emit("refused", { ...refusal, source: "local" }) };
|
|
22
|
-
})
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function getAuthorizeUserId(editor) {
|
|
27
|
-
const fromRoom = editor.store.props.collaboration?.userId?.get();
|
|
28
|
-
return fromRoom === void 0 ? editor.user.getExternalId() : fromRoom;
|
|
29
|
-
}
|
|
30
|
-
function judgeWrite(store, authorize, diff, userId) {
|
|
31
|
-
const isDocument = (record) => store.scopedTypes.document.has(record.typeName);
|
|
32
|
-
const changes = [];
|
|
33
|
-
for (const next of Object.values(diff.added)) {
|
|
34
|
-
if (isDocument(next)) changes.push({ type: "create", id: next.id, prev: null, next });
|
|
35
|
-
}
|
|
36
|
-
for (const [prev, next] of Object.values(diff.updated)) {
|
|
37
|
-
if (isDocument(next) && !isEqual(prev, next)) {
|
|
38
|
-
changes.push({ type: "update", id: next.id, prev, next });
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
for (const prev of Object.values(diff.removed)) {
|
|
42
|
-
if (isDocument(prev)) changes.push({ type: "delete", id: prev.id, prev, next: null });
|
|
43
|
-
}
|
|
44
|
-
const write = authorize.write;
|
|
45
|
-
if (!write || changes.length === 0) return null;
|
|
46
|
-
const readDocument = (id) => {
|
|
47
|
-
const record = store.get(id);
|
|
48
|
-
return record && isDocument(record) ? record : void 0;
|
|
49
|
-
};
|
|
50
|
-
const after = { get: readDocument };
|
|
51
|
-
const before = {
|
|
52
|
-
get: (id) => {
|
|
53
|
-
if (id in diff.added) return void 0;
|
|
54
|
-
if (id in diff.updated) return diff.updated[id][0];
|
|
55
|
-
if (id in diff.removed) return diff.removed[id];
|
|
56
|
-
return readDocument(id);
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
try {
|
|
60
|
-
return write(changes, {
|
|
61
|
-
userId,
|
|
62
|
-
can: bindAuthorizeCan(authorize.can, { userId }),
|
|
63
|
-
before,
|
|
64
|
-
after
|
|
65
|
-
});
|
|
66
|
-
} catch (e) {
|
|
67
|
-
console.error("authorize.write threw; refusing the change", e);
|
|
68
|
-
return { reason: "error" };
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
export {
|
|
72
|
-
AuthorizeManager,
|
|
73
|
-
getAuthorizeUserId,
|
|
74
|
-
judgeWrite
|
|
75
|
-
};
|
|
76
|
-
//# sourceMappingURL=AuthorizeManager.mjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../../src/lib/editor/managers/AuthorizeManager/AuthorizeManager.ts"],
|
|
4
|
-
"sourcesContent": ["import { RecordsDiff } from '@tldraw/store'\nimport {\n\tbindAuthorizeCan,\n\tTLAuthorize,\n\tTLRecord,\n\tTLRecordChange,\n\tTLRecordGetter,\n\tTLStore,\n\tTLWriteRefusal,\n} from '@tldraw/tlschema'\nimport { isEqual } from '@tldraw/utils'\nimport type { Editor } from '../../Editor'\nimport { EditorManager } from '../EditorManager'\n\n/**\n * Runs the store's `authorize.write` on each of this user's operations, as a sync room runs it on\n * each push. The store judges an operation once its side effects have settled, and reverts a\n * refused one before history or listeners see it. Then the editor emits `'refused'`. The room's\n * refusals, from `collaboration.refusals`, go out on the same event.\n *\n * @internal\n */\nexport class AuthorizeManager extends EditorManager {\n\tconstructor(editor: Editor) {\n\t\tsuper(editor)\n\t\tconst refusals = editor.store.props.collaboration?.refusals\n\t\tif (refusals) {\n\t\t\tthis.register(\n\t\t\t\trefusals.subscribe((refusal) => editor.emit('refused', { ...refusal, source: 'server' }))\n\t\t\t)\n\t\t}\n\t\tconst authorize = editor.store.props.authorize\n\t\tif (!authorize?.write) return\n\t\tthis.register(\n\t\t\teditor.store.addCommitCheck((diff) => {\n\t\t\t\tconst refusal = judgeWrite(editor.store, authorize, diff, getAuthorizeUserId(editor))\n\t\t\t\tif (!refusal) return null\n\t\t\t\treturn { afterRevert: () => editor.emit('refused', { ...refusal, source: 'local' }) }\n\t\t\t})\n\t\t)\n\t}\n}\n\n/**\n * The user id the authorize hooks get. Once the room has said which user it judges this session\n * as, the editor uses that one, so that both sides judge the same user (a guest is `null` in the\n * room but has a local id in the editor). Before that, it is the editor user's id.\n *\n * @internal\n */\nexport function getAuthorizeUserId(editor: Editor): string | null {\n\tconst fromRoom = editor.store.props.collaboration?.userId?.get()\n\treturn fromRoom === undefined ? editor.user.getExternalId() : fromRoom\n}\n\n/**\n * Judges one settled operation with the write hook. Only document records count, because those\n * are the only records the room sees.\n *\n * @internal\n */\nexport function judgeWrite(\n\tstore: TLStore,\n\tauthorize: TLAuthorize,\n\tdiff: RecordsDiff<TLRecord>,\n\tuserId: string | null\n): TLWriteRefusal | null {\n\tconst isDocument = (record: TLRecord) => store.scopedTypes.document.has(record.typeName)\n\tconst changes: TLRecordChange[] = []\n\tfor (const next of Object.values(diff.added)) {\n\t\tif (isDocument(next)) changes.push({ type: 'create', id: next.id, prev: null, next })\n\t}\n\tfor (const [prev, next] of Object.values(diff.updated)) {\n\t\tif (isDocument(next) && !isEqual(prev, next)) {\n\t\t\tchanges.push({ type: 'update', id: next.id, prev, next })\n\t\t}\n\t}\n\tfor (const prev of Object.values(diff.removed)) {\n\t\tif (isDocument(prev)) changes.push({ type: 'delete', id: prev.id, prev, next: null })\n\t}\n\tconst write = authorize.write\n\tif (!write || changes.length === 0) return null\n\n\tconst readDocument = (id: TLRecord['id']) => {\n\t\tconst record = store.get(id)\n\t\treturn record && isDocument(record) ? record : undefined\n\t}\n\tconst after = { get: readDocument } as TLRecordGetter\n\tconst before = {\n\t\tget: (id: TLRecord['id']) => {\n\t\t\tif (id in diff.added) return undefined\n\t\t\tif (id in diff.updated) return diff.updated[id][0]\n\t\t\tif (id in diff.removed) return diff.removed[id]\n\t\t\treturn readDocument(id)\n\t\t},\n\t} as TLRecordGetter\n\n\ttry {\n\t\treturn write(changes, {\n\t\t\tuserId,\n\t\t\tcan: bindAuthorizeCan(authorize.can, { userId }),\n\t\t\tbefore,\n\t\t\tafter,\n\t\t})\n\t} catch (e) {\n\t\t// the room refuses when its hook throws, so the editor must too\n\t\tconsole.error('authorize.write threw; refusing the change', e)\n\t\treturn { reason: 'error' }\n\t}\n}\n"],
|
|
5
|
-
"mappings": "AACA;AAAA,EACC;AAAA,OAOM;AACP,SAAS,eAAe;AAExB,SAAS,qBAAqB;AAUvB,MAAM,yBAAyB,cAAc;AAAA,EACnD,YAAY,QAAgB;AAC3B,UAAM,MAAM;AACZ,UAAM,WAAW,OAAO,MAAM,MAAM,eAAe;AACnD,QAAI,UAAU;AACb,WAAK;AAAA,QACJ,SAAS,UAAU,CAAC,YAAY,OAAO,KAAK,WAAW,EAAE,GAAG,SAAS,QAAQ,SAAS,CAAC,CAAC;AAAA,MACzF;AAAA,IACD;AACA,UAAM,YAAY,OAAO,MAAM,MAAM;AACrC,QAAI,CAAC,WAAW,MAAO;AACvB,SAAK;AAAA,MACJ,OAAO,MAAM,eAAe,CAAC,SAAS;AACrC,cAAM,UAAU,WAAW,OAAO,OAAO,WAAW,MAAM,mBAAmB,MAAM,CAAC;AACpF,YAAI,CAAC,QAAS,QAAO;AACrB,eAAO,EAAE,aAAa,MAAM,OAAO,KAAK,WAAW,EAAE,GAAG,SAAS,QAAQ,QAAQ,CAAC,EAAE;AAAA,MACrF,CAAC;AAAA,IACF;AAAA,EACD;AACD;AASO,SAAS,mBAAmB,QAA+B;AACjE,QAAM,WAAW,OAAO,MAAM,MAAM,eAAe,QAAQ,IAAI;AAC/D,SAAO,aAAa,SAAY,OAAO,KAAK,cAAc,IAAI;AAC/D;AAQO,SAAS,WACf,OACA,WACA,MACA,QACwB;AACxB,QAAM,aAAa,CAAC,WAAqB,MAAM,YAAY,SAAS,IAAI,OAAO,QAAQ;AACvF,QAAM,UAA4B,CAAC;AACnC,aAAW,QAAQ,OAAO,OAAO,KAAK,KAAK,GAAG;AAC7C,QAAI,WAAW,IAAI,EAAG,SAAQ,KAAK,EAAE,MAAM,UAAU,IAAI,KAAK,IAAI,MAAM,MAAM,KAAK,CAAC;AAAA,EACrF;AACA,aAAW,CAAC,MAAM,IAAI,KAAK,OAAO,OAAO,KAAK,OAAO,GAAG;AACvD,QAAI,WAAW,IAAI,KAAK,CAAC,QAAQ,MAAM,IAAI,GAAG;AAC7C,cAAQ,KAAK,EAAE,MAAM,UAAU,IAAI,KAAK,IAAI,MAAM,KAAK,CAAC;AAAA,IACzD;AAAA,EACD;AACA,aAAW,QAAQ,OAAO,OAAO,KAAK,OAAO,GAAG;AAC/C,QAAI,WAAW,IAAI,EAAG,SAAQ,KAAK,EAAE,MAAM,UAAU,IAAI,KAAK,IAAI,MAAM,MAAM,KAAK,CAAC;AAAA,EACrF;AACA,QAAM,QAAQ,UAAU;AACxB,MAAI,CAAC,SAAS,QAAQ,WAAW,EAAG,QAAO;AAE3C,QAAM,eAAe,CAAC,OAAuB;AAC5C,UAAM,SAAS,MAAM,IAAI,EAAE;AAC3B,WAAO,UAAU,WAAW,MAAM,IAAI,SAAS;AAAA,EAChD;AACA,QAAM,QAAQ,EAAE,KAAK,aAAa;AAClC,QAAM,SAAS;AAAA,IACd,KAAK,CAAC,OAAuB;AAC5B,UAAI,MAAM,KAAK,MAAO,QAAO;AAC7B,UAAI,MAAM,KAAK,QAAS,QAAO,KAAK,QAAQ,EAAE,EAAE,CAAC;AACjD,UAAI,MAAM,KAAK,QAAS,QAAO,KAAK,QAAQ,EAAE;AAC9C,aAAO,aAAa,EAAE;AAAA,IACvB;AAAA,EACD;AAEA,MAAI;AACH,WAAO,MAAM,SAAS;AAAA,MACrB;AAAA,MACA,KAAK,iBAAiB,UAAU,KAAK,EAAE,OAAO,CAAC;AAAA,MAC/C;AAAA,MACA;AAAA,IACD,CAAC;AAAA,EACF,SAAS,GAAG;AAEX,YAAQ,MAAM,8CAA8C,CAAC;AAC7D,WAAO,EAAE,QAAQ,QAAQ;AAAA,EAC1B;AACD;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { atom } from "@tldraw/state";
|
|
2
|
-
import { useLayoutEffect, useMemo, useState } from "react";
|
|
3
|
-
function useStableAuthorize(authorize) {
|
|
4
|
-
const [current] = useState(() => atom("authorize", authorize));
|
|
5
|
-
useLayoutEffect(() => {
|
|
6
|
-
current.set(authorize);
|
|
7
|
-
}, [current, authorize]);
|
|
8
|
-
const hasWrite = !!authorize?.write;
|
|
9
|
-
const hasRead = !!authorize?.read;
|
|
10
|
-
const hasCan = !!authorize?.can;
|
|
11
|
-
return useMemo(() => {
|
|
12
|
-
if (!hasWrite && !hasRead && !hasCan) return void 0;
|
|
13
|
-
const stable = {};
|
|
14
|
-
if (hasWrite) stable.write = (changes, ctx) => current.get()?.write?.(changes, ctx) ?? null;
|
|
15
|
-
if (hasRead) stable.read = (record, ctx) => current.get()?.read?.(record, ctx) ?? true;
|
|
16
|
-
if (hasCan) {
|
|
17
|
-
stable.can = (permission, data, ctx) => current.get()?.can?.(permission, data, ctx) ?? true;
|
|
18
|
-
}
|
|
19
|
-
return stable;
|
|
20
|
-
}, [current, hasWrite, hasRead, hasCan]);
|
|
21
|
-
}
|
|
22
|
-
export {
|
|
23
|
-
useStableAuthorize
|
|
24
|
-
};
|
|
25
|
-
//# sourceMappingURL=useStableAuthorize.mjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/lib/hooks/useStableAuthorize.ts"],
|
|
4
|
-
"sourcesContent": ["import { atom } from '@tldraw/state'\nimport { TLAuthorize } from '@tldraw/tlschema'\nimport { useLayoutEffect, useMemo, useState } from 'react'\n\n/**\n * Keeps one authorize object for the life of a store, so that an `authorize` written inline (for\n * example `composeAuthorize(...)` in a component) does not make a new store on every render. Each\n * hook calls the latest object, and reads it from a signal, so a reactive `editor.can` check runs\n * again when the object changes. A new store is made only when a hook is added or removed.\n *\n * @internal\n */\nexport function useStableAuthorize(authorize: TLAuthorize | undefined): TLAuthorize | undefined {\n\tconst [current] = useState(() => atom('authorize', authorize))\n\tuseLayoutEffect(() => {\n\t\tcurrent.set(authorize)\n\t}, [current, authorize])\n\tconst hasWrite = !!authorize?.write\n\tconst hasRead = !!authorize?.read\n\tconst hasCan = !!authorize?.can\n\treturn useMemo(() => {\n\t\tif (!hasWrite && !hasRead && !hasCan) return undefined\n\t\tconst stable: TLAuthorize = {}\n\t\tif (hasWrite) stable.write = (changes, ctx) => current.get()?.write?.(changes, ctx) ?? null\n\t\tif (hasRead) stable.read = (record, ctx) => current.get()?.read?.(record, ctx) ?? true\n\t\tif (hasCan) {\n\t\t\tstable.can = (permission, data, ctx) => current.get()?.can?.(permission, data, ctx) ?? true\n\t\t}\n\t\treturn stable\n\t}, [current, hasWrite, hasRead, hasCan])\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,YAAY;AAErB,SAAS,iBAAiB,SAAS,gBAAgB;AAU5C,SAAS,mBAAmB,WAA6D;AAC/F,QAAM,CAAC,OAAO,IAAI,SAAS,MAAM,KAAK,aAAa,SAAS,CAAC;AAC7D,kBAAgB,MAAM;AACrB,YAAQ,IAAI,SAAS;AAAA,EACtB,GAAG,CAAC,SAAS,SAAS,CAAC;AACvB,QAAM,WAAW,CAAC,CAAC,WAAW;AAC9B,QAAM,UAAU,CAAC,CAAC,WAAW;AAC7B,QAAM,SAAS,CAAC,CAAC,WAAW;AAC5B,SAAO,QAAQ,MAAM;AACpB,QAAI,CAAC,YAAY,CAAC,WAAW,CAAC,OAAQ,QAAO;AAC7C,UAAM,SAAsB,CAAC;AAC7B,QAAI,SAAU,QAAO,QAAQ,CAAC,SAAS,QAAQ,QAAQ,IAAI,GAAG,QAAQ,SAAS,GAAG,KAAK;AACvF,QAAI,QAAS,QAAO,OAAO,CAAC,QAAQ,QAAQ,QAAQ,IAAI,GAAG,OAAO,QAAQ,GAAG,KAAK;AAClF,QAAI,QAAQ;AACX,aAAO,MAAM,CAAC,YAAY,MAAM,QAAQ,QAAQ,IAAI,GAAG,MAAM,YAAY,MAAM,GAAG,KAAK;AAAA,IACxF;AACA,WAAO;AAAA,EACR,GAAG,CAAC,SAAS,UAAU,SAAS,MAAM,CAAC;AACxC;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { RecordsDiff } from '@tldraw/store'
|
|
2
|
-
import {
|
|
3
|
-
bindAuthorizeCan,
|
|
4
|
-
TLAuthorize,
|
|
5
|
-
TLRecord,
|
|
6
|
-
TLRecordChange,
|
|
7
|
-
TLRecordGetter,
|
|
8
|
-
TLStore,
|
|
9
|
-
TLWriteRefusal,
|
|
10
|
-
} from '@tldraw/tlschema'
|
|
11
|
-
import { isEqual } from '@tldraw/utils'
|
|
12
|
-
import type { Editor } from '../../Editor'
|
|
13
|
-
import { EditorManager } from '../EditorManager'
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Runs the store's `authorize.write` on each of this user's operations, as a sync room runs it on
|
|
17
|
-
* each push. The store judges an operation once its side effects have settled, and reverts a
|
|
18
|
-
* refused one before history or listeners see it. Then the editor emits `'refused'`. The room's
|
|
19
|
-
* refusals, from `collaboration.refusals`, go out on the same event.
|
|
20
|
-
*
|
|
21
|
-
* @internal
|
|
22
|
-
*/
|
|
23
|
-
export class AuthorizeManager extends EditorManager {
|
|
24
|
-
constructor(editor: Editor) {
|
|
25
|
-
super(editor)
|
|
26
|
-
const refusals = editor.store.props.collaboration?.refusals
|
|
27
|
-
if (refusals) {
|
|
28
|
-
this.register(
|
|
29
|
-
refusals.subscribe((refusal) => editor.emit('refused', { ...refusal, source: 'server' }))
|
|
30
|
-
)
|
|
31
|
-
}
|
|
32
|
-
const authorize = editor.store.props.authorize
|
|
33
|
-
if (!authorize?.write) return
|
|
34
|
-
this.register(
|
|
35
|
-
editor.store.addCommitCheck((diff) => {
|
|
36
|
-
const refusal = judgeWrite(editor.store, authorize, diff, getAuthorizeUserId(editor))
|
|
37
|
-
if (!refusal) return null
|
|
38
|
-
return { afterRevert: () => editor.emit('refused', { ...refusal, source: 'local' }) }
|
|
39
|
-
})
|
|
40
|
-
)
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* The user id the authorize hooks get. Once the room has said which user it judges this session
|
|
46
|
-
* as, the editor uses that one, so that both sides judge the same user (a guest is `null` in the
|
|
47
|
-
* room but has a local id in the editor). Before that, it is the editor user's id.
|
|
48
|
-
*
|
|
49
|
-
* @internal
|
|
50
|
-
*/
|
|
51
|
-
export function getAuthorizeUserId(editor: Editor): string | null {
|
|
52
|
-
const fromRoom = editor.store.props.collaboration?.userId?.get()
|
|
53
|
-
return fromRoom === undefined ? editor.user.getExternalId() : fromRoom
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Judges one settled operation with the write hook. Only document records count, because those
|
|
58
|
-
* are the only records the room sees.
|
|
59
|
-
*
|
|
60
|
-
* @internal
|
|
61
|
-
*/
|
|
62
|
-
export function judgeWrite(
|
|
63
|
-
store: TLStore,
|
|
64
|
-
authorize: TLAuthorize,
|
|
65
|
-
diff: RecordsDiff<TLRecord>,
|
|
66
|
-
userId: string | null
|
|
67
|
-
): TLWriteRefusal | null {
|
|
68
|
-
const isDocument = (record: TLRecord) => store.scopedTypes.document.has(record.typeName)
|
|
69
|
-
const changes: TLRecordChange[] = []
|
|
70
|
-
for (const next of Object.values(diff.added)) {
|
|
71
|
-
if (isDocument(next)) changes.push({ type: 'create', id: next.id, prev: null, next })
|
|
72
|
-
}
|
|
73
|
-
for (const [prev, next] of Object.values(diff.updated)) {
|
|
74
|
-
if (isDocument(next) && !isEqual(prev, next)) {
|
|
75
|
-
changes.push({ type: 'update', id: next.id, prev, next })
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
for (const prev of Object.values(diff.removed)) {
|
|
79
|
-
if (isDocument(prev)) changes.push({ type: 'delete', id: prev.id, prev, next: null })
|
|
80
|
-
}
|
|
81
|
-
const write = authorize.write
|
|
82
|
-
if (!write || changes.length === 0) return null
|
|
83
|
-
|
|
84
|
-
const readDocument = (id: TLRecord['id']) => {
|
|
85
|
-
const record = store.get(id)
|
|
86
|
-
return record && isDocument(record) ? record : undefined
|
|
87
|
-
}
|
|
88
|
-
const after = { get: readDocument } as TLRecordGetter
|
|
89
|
-
const before = {
|
|
90
|
-
get: (id: TLRecord['id']) => {
|
|
91
|
-
if (id in diff.added) return undefined
|
|
92
|
-
if (id in diff.updated) return diff.updated[id][0]
|
|
93
|
-
if (id in diff.removed) return diff.removed[id]
|
|
94
|
-
return readDocument(id)
|
|
95
|
-
},
|
|
96
|
-
} as TLRecordGetter
|
|
97
|
-
|
|
98
|
-
try {
|
|
99
|
-
return write(changes, {
|
|
100
|
-
userId,
|
|
101
|
-
can: bindAuthorizeCan(authorize.can, { userId }),
|
|
102
|
-
before,
|
|
103
|
-
after,
|
|
104
|
-
})
|
|
105
|
-
} catch (e) {
|
|
106
|
-
// the room refuses when its hook throws, so the editor must too
|
|
107
|
-
console.error('authorize.write threw; refusing the change', e)
|
|
108
|
-
return { reason: 'error' }
|
|
109
|
-
}
|
|
110
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { atom } from '@tldraw/state'
|
|
2
|
-
import { TLAuthorize } from '@tldraw/tlschema'
|
|
3
|
-
import { useLayoutEffect, useMemo, useState } from 'react'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Keeps one authorize object for the life of a store, so that an `authorize` written inline (for
|
|
7
|
-
* example `composeAuthorize(...)` in a component) does not make a new store on every render. Each
|
|
8
|
-
* hook calls the latest object, and reads it from a signal, so a reactive `editor.can` check runs
|
|
9
|
-
* again when the object changes. A new store is made only when a hook is added or removed.
|
|
10
|
-
*
|
|
11
|
-
* @internal
|
|
12
|
-
*/
|
|
13
|
-
export function useStableAuthorize(authorize: TLAuthorize | undefined): TLAuthorize | undefined {
|
|
14
|
-
const [current] = useState(() => atom('authorize', authorize))
|
|
15
|
-
useLayoutEffect(() => {
|
|
16
|
-
current.set(authorize)
|
|
17
|
-
}, [current, authorize])
|
|
18
|
-
const hasWrite = !!authorize?.write
|
|
19
|
-
const hasRead = !!authorize?.read
|
|
20
|
-
const hasCan = !!authorize?.can
|
|
21
|
-
return useMemo(() => {
|
|
22
|
-
if (!hasWrite && !hasRead && !hasCan) return undefined
|
|
23
|
-
const stable: TLAuthorize = {}
|
|
24
|
-
if (hasWrite) stable.write = (changes, ctx) => current.get()?.write?.(changes, ctx) ?? null
|
|
25
|
-
if (hasRead) stable.read = (record, ctx) => current.get()?.read?.(record, ctx) ?? true
|
|
26
|
-
if (hasCan) {
|
|
27
|
-
stable.can = (permission, data, ctx) => current.get()?.can?.(permission, data, ctx) ?? true
|
|
28
|
-
}
|
|
29
|
-
return stable
|
|
30
|
-
}, [current, hasWrite, hasRead, hasCan])
|
|
31
|
-
}
|