@tldraw/editor 3.16.0-canary.0ff72188cd53 → 3.16.0-canary.13ee933ed97f
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 +57 -3
- package/dist-cjs/index.js +4 -2
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/lib/TldrawEditor.js +2 -2
- package/dist-cjs/lib/TldrawEditor.js.map +2 -2
- package/dist-cjs/lib/components/default-components/DefaultCanvas.js +11 -1
- package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
- package/dist-cjs/lib/editor/Editor.js +9 -4
- package/dist-cjs/lib/editor/Editor.js.map +2 -2
- package/dist-cjs/lib/editor/derivations/notVisibleShapes.js +4 -0
- package/dist-cjs/lib/editor/derivations/notVisibleShapes.js.map +2 -2
- package/dist-cjs/lib/editor/shapes/ShapeUtil.js +10 -0
- package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
- package/dist-cjs/lib/hooks/useCanvasEvents.js +15 -12
- package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
- package/dist-cjs/lib/hooks/useDocumentEvents.js +5 -5
- package/dist-cjs/lib/hooks/useDocumentEvents.js.map +2 -2
- package/dist-cjs/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.js +1 -2
- package/dist-cjs/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.js.map +2 -2
- package/dist-cjs/lib/hooks/useGestureEvents.js +1 -1
- package/dist-cjs/lib/hooks/useGestureEvents.js.map +2 -2
- package/dist-cjs/lib/hooks/useHandleEvents.js +3 -3
- package/dist-cjs/lib/hooks/useHandleEvents.js.map +2 -2
- package/dist-cjs/lib/hooks/useSelectionEvents.js +4 -4
- package/dist-cjs/lib/hooks/useSelectionEvents.js.map +2 -2
- package/dist-cjs/lib/license/LicenseManager.js +21 -4
- package/dist-cjs/lib/license/LicenseManager.js.map +2 -2
- package/dist-cjs/lib/license/LicenseProvider.js +17 -1
- package/dist-cjs/lib/license/LicenseProvider.js.map +2 -2
- package/dist-cjs/lib/license/Watermark.js +2 -2
- package/dist-cjs/lib/license/Watermark.js.map +2 -2
- package/dist-cjs/lib/primitives/geometry/Geometry2d.js +24 -2
- package/dist-cjs/lib/primitives/geometry/Geometry2d.js.map +2 -2
- package/dist-cjs/lib/primitives/geometry/Group2d.js +5 -1
- package/dist-cjs/lib/primitives/geometry/Group2d.js.map +2 -2
- package/dist-cjs/lib/utils/dom.js +12 -1
- package/dist-cjs/lib/utils/dom.js.map +2 -2
- package/dist-cjs/lib/utils/getPointerInfo.js +2 -2
- package/dist-cjs/lib/utils/getPointerInfo.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 +57 -3
- package/dist-esm/index.mjs +7 -3
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/TldrawEditor.mjs +3 -3
- package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
- package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +12 -2
- package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
- package/dist-esm/lib/editor/Editor.mjs +9 -4
- package/dist-esm/lib/editor/Editor.mjs.map +2 -2
- package/dist-esm/lib/editor/derivations/notVisibleShapes.mjs +4 -0
- package/dist-esm/lib/editor/derivations/notVisibleShapes.mjs.map +2 -2
- package/dist-esm/lib/editor/shapes/ShapeUtil.mjs +10 -0
- package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
- package/dist-esm/lib/hooks/useCanvasEvents.mjs +17 -13
- package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
- package/dist-esm/lib/hooks/useDocumentEvents.mjs +11 -6
- package/dist-esm/lib/hooks/useDocumentEvents.mjs.map +2 -2
- package/dist-esm/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.mjs +2 -3
- package/dist-esm/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.mjs.map +2 -2
- package/dist-esm/lib/hooks/useGestureEvents.mjs +2 -2
- package/dist-esm/lib/hooks/useGestureEvents.mjs.map +2 -2
- package/dist-esm/lib/hooks/useHandleEvents.mjs +9 -4
- package/dist-esm/lib/hooks/useHandleEvents.mjs.map +2 -2
- package/dist-esm/lib/hooks/useSelectionEvents.mjs +6 -5
- package/dist-esm/lib/hooks/useSelectionEvents.mjs.map +2 -2
- package/dist-esm/lib/license/LicenseManager.mjs +21 -4
- package/dist-esm/lib/license/LicenseManager.mjs.map +2 -2
- package/dist-esm/lib/license/LicenseProvider.mjs +16 -1
- package/dist-esm/lib/license/LicenseProvider.mjs.map +2 -2
- package/dist-esm/lib/license/Watermark.mjs +3 -3
- package/dist-esm/lib/license/Watermark.mjs.map +2 -2
- package/dist-esm/lib/primitives/geometry/Geometry2d.mjs +24 -2
- package/dist-esm/lib/primitives/geometry/Geometry2d.mjs.map +2 -2
- package/dist-esm/lib/primitives/geometry/Group2d.mjs +5 -1
- package/dist-esm/lib/primitives/geometry/Group2d.mjs.map +2 -2
- package/dist-esm/lib/utils/dom.mjs +12 -1
- package/dist-esm/lib/utils/dom.mjs.map +2 -2
- package/dist-esm/lib/utils/getPointerInfo.mjs +2 -2
- package/dist-esm/lib/utils/getPointerInfo.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 +2 -0
- package/src/lib/TldrawEditor.tsx +3 -4
- package/src/lib/components/default-components/DefaultCanvas.tsx +8 -2
- package/src/lib/editor/Editor.test.ts +90 -0
- package/src/lib/editor/Editor.ts +16 -4
- package/src/lib/editor/derivations/notVisibleShapes.ts +6 -0
- package/src/lib/editor/shapes/ShapeUtil.ts +11 -0
- package/src/lib/hooks/useCanvasEvents.ts +17 -11
- package/src/lib/hooks/useDocumentEvents.ts +11 -6
- package/src/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.ts +2 -2
- package/src/lib/hooks/useGestureEvents.ts +2 -2
- package/src/lib/hooks/useHandleEvents.ts +9 -4
- package/src/lib/hooks/useSelectionEvents.ts +6 -5
- package/src/lib/license/LicenseManager.test.ts +78 -2
- package/src/lib/license/LicenseManager.ts +28 -5
- package/src/lib/license/LicenseProvider.tsx +40 -1
- package/src/lib/license/Watermark.tsx +3 -3
- package/src/lib/primitives/geometry/Geometry2d.test.ts +420 -0
- package/src/lib/primitives/geometry/Geometry2d.ts +29 -2
- package/src/lib/primitives/geometry/Group2d.ts +6 -1
- package/src/lib/test/InFrontOfTheCanvas.test.tsx +187 -0
- package/src/lib/utils/dom.test.ts +94 -0
- package/src/lib/utils/dom.ts +38 -1
- package/src/lib/utils/getPointerInfo.ts +2 -1
- package/src/version.ts +3 -3
|
@@ -7,6 +7,12 @@ function fromScratch(editor: Editor): Set<TLShapeId> {
|
|
|
7
7
|
const viewportPageBounds = editor.getViewportPageBounds()
|
|
8
8
|
const notVisibleShapes = new Set<TLShapeId>()
|
|
9
9
|
shapesIds.forEach((id) => {
|
|
10
|
+
const shape = editor.getShape(id)
|
|
11
|
+
if (!shape) return
|
|
12
|
+
|
|
13
|
+
const canCull = editor.getShapeUtil(shape.type).canCull(shape)
|
|
14
|
+
if (!canCull) return
|
|
15
|
+
|
|
10
16
|
// If the shape is fully outside of the viewport page bounds, add it to the set.
|
|
11
17
|
// We'll ignore masks here, since they're more expensive to compute and the overhead is not worth it.
|
|
12
18
|
const pageBounds = editor.getShapePageBounds(id)
|
|
@@ -283,6 +283,17 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
|
|
|
283
283
|
return true
|
|
284
284
|
}
|
|
285
285
|
|
|
286
|
+
/**
|
|
287
|
+
* Whether this shape can be culled. By default, shapes are culled for
|
|
288
|
+
* performance reasons when they are outside of the viewport. Culled shapes are still rendered
|
|
289
|
+
* to the DOM, but have their `display` property set to `none`.
|
|
290
|
+
*
|
|
291
|
+
* @param shape - The shape.
|
|
292
|
+
*/
|
|
293
|
+
canCull(_shape: Shape): boolean {
|
|
294
|
+
return true
|
|
295
|
+
}
|
|
296
|
+
|
|
286
297
|
/**
|
|
287
298
|
* Does this shape provide a background for its children? If this is true,
|
|
288
299
|
* then any children with a `renderBackground` method will have their
|
|
@@ -2,10 +2,11 @@ import { useValue } from '@tldraw/state-react'
|
|
|
2
2
|
import React, { useEffect, useMemo } from 'react'
|
|
3
3
|
import { RIGHT_MOUSE_BUTTON } from '../constants'
|
|
4
4
|
import {
|
|
5
|
+
markEventAsHandled,
|
|
5
6
|
preventDefault,
|
|
6
7
|
releasePointerCapture,
|
|
7
8
|
setPointerCapture,
|
|
8
|
-
|
|
9
|
+
wasEventAlreadyHandled,
|
|
9
10
|
} from '../utils/dom'
|
|
10
11
|
import { getPointerInfo } from '../utils/getPointerInfo'
|
|
11
12
|
import { useEditor } from './useEditor'
|
|
@@ -17,7 +18,7 @@ export function useCanvasEvents() {
|
|
|
17
18
|
const events = useMemo(
|
|
18
19
|
function canvasEvents() {
|
|
19
20
|
function onPointerDown(e: React.PointerEvent) {
|
|
20
|
-
if ((e
|
|
21
|
+
if (wasEventAlreadyHandled(e)) return
|
|
21
22
|
|
|
22
23
|
if (e.button === RIGHT_MOUSE_BUTTON) {
|
|
23
24
|
editor.dispatch({
|
|
@@ -42,7 +43,7 @@ export function useCanvasEvents() {
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
function onPointerUp(e: React.PointerEvent) {
|
|
45
|
-
if ((e
|
|
46
|
+
if (wasEventAlreadyHandled(e)) return
|
|
46
47
|
if (e.button !== 0 && e.button !== 1 && e.button !== 2 && e.button !== 5) return
|
|
47
48
|
|
|
48
49
|
releasePointerCapture(e.currentTarget, e)
|
|
@@ -56,26 +57,28 @@ export function useCanvasEvents() {
|
|
|
56
57
|
}
|
|
57
58
|
|
|
58
59
|
function onPointerEnter(e: React.PointerEvent) {
|
|
59
|
-
if ((e
|
|
60
|
+
if (wasEventAlreadyHandled(e)) return
|
|
60
61
|
if (editor.getInstanceState().isPenMode && e.pointerType !== 'pen') return
|
|
61
62
|
const canHover = e.pointerType === 'mouse' || e.pointerType === 'pen'
|
|
62
63
|
editor.updateInstanceState({ isHoveringCanvas: canHover ? true : null })
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
function onPointerLeave(e: React.PointerEvent) {
|
|
66
|
-
if ((e
|
|
67
|
+
if (wasEventAlreadyHandled(e)) return
|
|
67
68
|
if (editor.getInstanceState().isPenMode && e.pointerType !== 'pen') return
|
|
68
69
|
const canHover = e.pointerType === 'mouse' || e.pointerType === 'pen'
|
|
69
70
|
editor.updateInstanceState({ isHoveringCanvas: canHover ? false : null })
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
function onTouchStart(e: React.TouchEvent) {
|
|
73
|
-
|
|
74
|
+
if (wasEventAlreadyHandled(e)) return
|
|
75
|
+
markEventAsHandled(e)
|
|
74
76
|
preventDefault(e)
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
function onTouchEnd(e: React.TouchEvent) {
|
|
78
|
-
|
|
80
|
+
if (wasEventAlreadyHandled(e)) return
|
|
81
|
+
markEventAsHandled(e)
|
|
79
82
|
// check that e.target is an HTMLElement
|
|
80
83
|
if (!(e.target instanceof HTMLElement)) return
|
|
81
84
|
|
|
@@ -94,12 +97,14 @@ export function useCanvasEvents() {
|
|
|
94
97
|
}
|
|
95
98
|
|
|
96
99
|
function onDragOver(e: React.DragEvent<Element>) {
|
|
100
|
+
if (wasEventAlreadyHandled(e)) return
|
|
97
101
|
preventDefault(e)
|
|
98
102
|
}
|
|
99
103
|
|
|
100
104
|
async function onDrop(e: React.DragEvent<Element>) {
|
|
105
|
+
if (wasEventAlreadyHandled(e)) return
|
|
101
106
|
preventDefault(e)
|
|
102
|
-
|
|
107
|
+
e.stopPropagation()
|
|
103
108
|
|
|
104
109
|
if (e.dataTransfer?.files?.length) {
|
|
105
110
|
const files = Array.from(e.dataTransfer.files)
|
|
@@ -124,7 +129,8 @@ export function useCanvasEvents() {
|
|
|
124
129
|
}
|
|
125
130
|
|
|
126
131
|
function onClick(e: React.MouseEvent) {
|
|
127
|
-
|
|
132
|
+
if (wasEventAlreadyHandled(e)) return
|
|
133
|
+
e.stopPropagation()
|
|
128
134
|
}
|
|
129
135
|
|
|
130
136
|
return {
|
|
@@ -151,8 +157,8 @@ export function useCanvasEvents() {
|
|
|
151
157
|
let lastX: number, lastY: number
|
|
152
158
|
|
|
153
159
|
function onPointerMove(e: PointerEvent) {
|
|
154
|
-
if ((e
|
|
155
|
-
|
|
160
|
+
if (wasEventAlreadyHandled(e)) return
|
|
161
|
+
markEventAsHandled(e)
|
|
156
162
|
|
|
157
163
|
if (e.clientX === lastX && e.clientY === lastY) return
|
|
158
164
|
lastX = e.clientX
|
|
@@ -2,7 +2,12 @@ import { useValue } from '@tldraw/state-react'
|
|
|
2
2
|
import { useEffect } from 'react'
|
|
3
3
|
import { Editor } from '../editor/Editor'
|
|
4
4
|
import { TLKeyboardEventInfo } from '../editor/types/event-types'
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
activeElementShouldCaptureKeys,
|
|
7
|
+
markEventAsHandled,
|
|
8
|
+
preventDefault,
|
|
9
|
+
wasEventAlreadyHandled,
|
|
10
|
+
} from '../utils/dom'
|
|
6
11
|
import { isAccelKey } from '../utils/keyboard'
|
|
7
12
|
import { useContainer } from './useContainer'
|
|
8
13
|
import { useEditor } from './useEditor'
|
|
@@ -29,7 +34,7 @@ export function useDocumentEvents() {
|
|
|
29
34
|
// re-dispatched, which would lead to an infinite loop.
|
|
30
35
|
if ((e as any).isSpecialRedispatchedEvent) return
|
|
31
36
|
preventDefault(e)
|
|
32
|
-
|
|
37
|
+
e.stopPropagation()
|
|
33
38
|
const cvs = container.querySelector('.tl-canvas')
|
|
34
39
|
if (!cvs) return
|
|
35
40
|
const newEvent = new DragEvent(e.type, e)
|
|
@@ -103,8 +108,8 @@ export function useDocumentEvents() {
|
|
|
103
108
|
preventDefault(e)
|
|
104
109
|
}
|
|
105
110
|
|
|
106
|
-
if ((e
|
|
107
|
-
|
|
111
|
+
if (wasEventAlreadyHandled(e)) return
|
|
112
|
+
markEventAsHandled(e)
|
|
108
113
|
const hasSelectedShapes = !!editor.getSelectedShapeIds().length
|
|
109
114
|
|
|
110
115
|
switch (e.key) {
|
|
@@ -211,8 +216,8 @@ export function useDocumentEvents() {
|
|
|
211
216
|
}
|
|
212
217
|
|
|
213
218
|
const handleKeyUp = (e: KeyboardEvent) => {
|
|
214
|
-
if ((e
|
|
215
|
-
|
|
219
|
+
if (wasEventAlreadyHandled(e)) return
|
|
220
|
+
markEventAsHandled(e)
|
|
216
221
|
|
|
217
222
|
if (areShortcutsDisabled(editor)) {
|
|
218
223
|
return
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect } from 'react'
|
|
2
|
-
import { preventDefault } from '../utils/dom'
|
|
2
|
+
import { markEventAsHandled, preventDefault } from '../utils/dom'
|
|
3
3
|
import { useEditor } from './useEditor'
|
|
4
4
|
|
|
5
5
|
const IGNORED_TAGS = ['textarea', 'input']
|
|
@@ -19,7 +19,7 @@ export function useFixSafariDoubleTapZoomPencilEvents(ref: React.RefObject<HTMLE
|
|
|
19
19
|
|
|
20
20
|
const handleEvent = (e: PointerEvent | TouchEvent) => {
|
|
21
21
|
if (e instanceof PointerEvent && e.pointerType === 'pen') {
|
|
22
|
-
|
|
22
|
+
markEventAsHandled(e)
|
|
23
23
|
const { target } = e
|
|
24
24
|
|
|
25
25
|
// Allow events to propagate if the app is editing a shape, or if the event is occurring in a text area or input
|
|
@@ -3,7 +3,7 @@ import { createUseGesture, pinchAction, wheelAction } from '@use-gesture/react'
|
|
|
3
3
|
import * as React from 'react'
|
|
4
4
|
import { TLWheelEventInfo } from '../editor/types/event-types'
|
|
5
5
|
import { Vec } from '../primitives/Vec'
|
|
6
|
-
import { preventDefault
|
|
6
|
+
import { preventDefault } from '../utils/dom'
|
|
7
7
|
import { isAccelKey } from '../utils/keyboard'
|
|
8
8
|
import { normalizeWheel } from '../utils/normalizeWheel'
|
|
9
9
|
import { useEditor } from './useEditor'
|
|
@@ -113,7 +113,7 @@ export function useGestureEvents(ref: React.RefObject<HTMLDivElement>) {
|
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
preventDefault(event)
|
|
116
|
-
|
|
116
|
+
event.stopPropagation()
|
|
117
117
|
const delta = normalizeWheel(event)
|
|
118
118
|
|
|
119
119
|
if (delta.x === 0 && delta.y === 0) return
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { TLArrowShape, TLLineShape, TLShapeId } from '@tldraw/tlschema'
|
|
2
2
|
import * as React from 'react'
|
|
3
3
|
import { Editor } from '../editor/Editor'
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
loopToHtmlElement,
|
|
6
|
+
releasePointerCapture,
|
|
7
|
+
setPointerCapture,
|
|
8
|
+
wasEventAlreadyHandled,
|
|
9
|
+
} from '../utils/dom'
|
|
5
10
|
import { getPointerInfo } from '../utils/getPointerInfo'
|
|
6
11
|
import { useEditor } from './useEditor'
|
|
7
12
|
|
|
@@ -16,7 +21,7 @@ export function useHandleEvents(id: TLShapeId, handleId: string) {
|
|
|
16
21
|
|
|
17
22
|
return React.useMemo(() => {
|
|
18
23
|
const onPointerDown = (e: React.PointerEvent) => {
|
|
19
|
-
if ((e
|
|
24
|
+
if (wasEventAlreadyHandled(e)) return
|
|
20
25
|
|
|
21
26
|
// Must set pointer capture on an HTML element!
|
|
22
27
|
const target = loopToHtmlElement(e.currentTarget)
|
|
@@ -40,7 +45,7 @@ export function useHandleEvents(id: TLShapeId, handleId: string) {
|
|
|
40
45
|
let lastX: number, lastY: number
|
|
41
46
|
|
|
42
47
|
const onPointerMove = (e: React.PointerEvent) => {
|
|
43
|
-
if ((e
|
|
48
|
+
if (wasEventAlreadyHandled(e)) return
|
|
44
49
|
if (e.clientX === lastX && e.clientY === lastY) return
|
|
45
50
|
lastX = e.clientX
|
|
46
51
|
lastY = e.clientY
|
|
@@ -60,7 +65,7 @@ export function useHandleEvents(id: TLShapeId, handleId: string) {
|
|
|
60
65
|
}
|
|
61
66
|
|
|
62
67
|
const onPointerUp = (e: React.PointerEvent) => {
|
|
63
|
-
if ((e
|
|
68
|
+
if (wasEventAlreadyHandled(e)) return
|
|
64
69
|
|
|
65
70
|
const target = loopToHtmlElement(e.currentTarget)
|
|
66
71
|
releasePointerCapture(target, e)
|
|
@@ -3,9 +3,10 @@ import { RIGHT_MOUSE_BUTTON } from '../constants'
|
|
|
3
3
|
import { TLSelectionHandle } from '../editor/types/selection-types'
|
|
4
4
|
import {
|
|
5
5
|
loopToHtmlElement,
|
|
6
|
+
markEventAsHandled,
|
|
6
7
|
releasePointerCapture,
|
|
7
8
|
setPointerCapture,
|
|
8
|
-
|
|
9
|
+
wasEventAlreadyHandled,
|
|
9
10
|
} from '../utils/dom'
|
|
10
11
|
import { getPointerInfo } from '../utils/getPointerInfo'
|
|
11
12
|
import { useEditor } from './useEditor'
|
|
@@ -17,7 +18,7 @@ export function useSelectionEvents(handle: TLSelectionHandle) {
|
|
|
17
18
|
const events = useMemo(
|
|
18
19
|
function selectionEvents() {
|
|
19
20
|
const onPointerDown: React.PointerEventHandler = (e) => {
|
|
20
|
-
if ((e
|
|
21
|
+
if (wasEventAlreadyHandled(e)) return
|
|
21
22
|
|
|
22
23
|
if (e.button === RIGHT_MOUSE_BUTTON) {
|
|
23
24
|
editor.dispatch({
|
|
@@ -54,14 +55,14 @@ export function useSelectionEvents(handle: TLSelectionHandle) {
|
|
|
54
55
|
handle,
|
|
55
56
|
...getPointerInfo(e),
|
|
56
57
|
})
|
|
57
|
-
|
|
58
|
+
markEventAsHandled(e)
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
// Track the last screen point
|
|
61
62
|
let lastX: number, lastY: number
|
|
62
63
|
|
|
63
64
|
function onPointerMove(e: React.PointerEvent) {
|
|
64
|
-
if ((e
|
|
65
|
+
if (wasEventAlreadyHandled(e)) return
|
|
65
66
|
if (e.button !== 0) return
|
|
66
67
|
if (e.clientX === lastX && e.clientY === lastY) return
|
|
67
68
|
lastX = e.clientX
|
|
@@ -77,7 +78,7 @@ export function useSelectionEvents(handle: TLSelectionHandle) {
|
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
const onPointerUp: React.PointerEventHandler = (e) => {
|
|
80
|
-
if ((e
|
|
81
|
+
if (wasEventAlreadyHandled(e)) return
|
|
81
82
|
if (e.button !== 0) return
|
|
82
83
|
|
|
83
84
|
editor.dispatch({
|
|
@@ -266,7 +266,7 @@ describe('LicenseManager', () => {
|
|
|
266
266
|
delete window.location
|
|
267
267
|
// @ts-ignore
|
|
268
268
|
window.location = new URL(
|
|
269
|
-
'vscode-webview
|
|
269
|
+
'vscode-webview://1ipd8pun8ud7nd7hv9d112g7evi7m10vak9vviuvia66ou6aibp3/index.html?id=6ec2dc7a-afe9-45d9-bd71-1749f9568d28&origin=955b256f-37e1-4a72-a2f4-ad633e88239c&swVersion=4&extensionId=tldraw-org.tldraw-vscode&platform=electron&vscode-resource-base-authority=vscode-resource.vscode-cdn.net&parentOrigin=vscode-file%3A%2F%2Fvscode-app'
|
|
270
270
|
)
|
|
271
271
|
|
|
272
272
|
const permissiveHostsInfo = JSON.parse(STANDARD_LICENSE_INFO)
|
|
@@ -286,7 +286,7 @@ describe('LicenseManager', () => {
|
|
|
286
286
|
delete window.location
|
|
287
287
|
// @ts-ignore
|
|
288
288
|
window.location = new URL(
|
|
289
|
-
'vscode-webview
|
|
289
|
+
'vscode-webview://1ipd8pun8ud7nd7hv9d112g7evi7m10vak9vviuvia66ou6aibp3/index.html?id=6ec2dc7a-afe9-45d9-bd71-1749f9568d28&origin=955b256f-37e1-4a72-a2f4-ad633e88239c&swVersion=4&extensionId=tldraw-org.tldraw-vscode&platform=electron&vscode-resource-base-authority=vscode-resource.vscode-cdn.net&parentOrigin=vscode-file%3A%2F%2Fvscode-app'
|
|
290
290
|
)
|
|
291
291
|
|
|
292
292
|
const permissiveHostsInfo = JSON.parse(STANDARD_LICENSE_INFO)
|
|
@@ -300,6 +300,70 @@ describe('LicenseManager', () => {
|
|
|
300
300
|
)) as ValidLicenseKeyResult
|
|
301
301
|
expect(result.isDomainValid).toBe(false)
|
|
302
302
|
})
|
|
303
|
+
|
|
304
|
+
it('Succeeds if it is a native app', async () => {
|
|
305
|
+
// @ts-ignore
|
|
306
|
+
delete window.location
|
|
307
|
+
// @ts-ignore
|
|
308
|
+
window.location = new URL('app-bundle://app/index.html')
|
|
309
|
+
|
|
310
|
+
const nativeLicenseInfo = JSON.parse(STANDARD_LICENSE_INFO)
|
|
311
|
+
nativeLicenseInfo[PROPERTIES.FLAGS] = FLAGS.NATIVE_LICENSE
|
|
312
|
+
nativeLicenseInfo[PROPERTIES.HOSTS] = ['app-bundle:']
|
|
313
|
+
const nativeLicenseKey = await generateLicenseKey(JSON.stringify(nativeLicenseInfo), keyPair)
|
|
314
|
+
const result = (await licenseManager.getLicenseFromKey(
|
|
315
|
+
nativeLicenseKey
|
|
316
|
+
)) as ValidLicenseKeyResult
|
|
317
|
+
expect(result.isDomainValid).toBe(true)
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
it('Succeeds if it is a native app with a wildcard', async () => {
|
|
321
|
+
// @ts-ignore
|
|
322
|
+
delete window.location
|
|
323
|
+
// @ts-ignore
|
|
324
|
+
window.location = new URL('app-bundle://unique-id-123/index.html')
|
|
325
|
+
|
|
326
|
+
const nativeLicenseInfo = JSON.parse(STANDARD_LICENSE_INFO)
|
|
327
|
+
nativeLicenseInfo[PROPERTIES.FLAGS] = FLAGS.NATIVE_LICENSE
|
|
328
|
+
nativeLicenseInfo[PROPERTIES.HOSTS] = ['^app-bundle://unique-id-123.*']
|
|
329
|
+
const nativeLicenseKey = await generateLicenseKey(JSON.stringify(nativeLicenseInfo), keyPair)
|
|
330
|
+
const result = (await licenseManager.getLicenseFromKey(
|
|
331
|
+
nativeLicenseKey
|
|
332
|
+
)) as ValidLicenseKeyResult
|
|
333
|
+
expect(result.isDomainValid).toBe(true)
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
it('Succeeds if it is a native app with a wildcard and search param', async () => {
|
|
337
|
+
// @ts-ignore
|
|
338
|
+
delete window.location
|
|
339
|
+
// @ts-ignore
|
|
340
|
+
window.location = new URL('app-bundle://app/index.html?unique-id-123')
|
|
341
|
+
|
|
342
|
+
const nativeLicenseInfo = JSON.parse(STANDARD_LICENSE_INFO)
|
|
343
|
+
nativeLicenseInfo[PROPERTIES.FLAGS] = FLAGS.NATIVE_LICENSE
|
|
344
|
+
nativeLicenseInfo[PROPERTIES.HOSTS] = ['^app-bundle://app.*unique-id-123.*']
|
|
345
|
+
const nativeLicenseKey = await generateLicenseKey(JSON.stringify(nativeLicenseInfo), keyPair)
|
|
346
|
+
const result = (await licenseManager.getLicenseFromKey(
|
|
347
|
+
nativeLicenseKey
|
|
348
|
+
)) as ValidLicenseKeyResult
|
|
349
|
+
expect(result.isDomainValid).toBe(true)
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
it('Fails if it is a native app with the wrong protocol', async () => {
|
|
353
|
+
// @ts-ignore
|
|
354
|
+
delete window.location
|
|
355
|
+
// @ts-ignore
|
|
356
|
+
window.location = new URL('blah-blundle://app/index.html')
|
|
357
|
+
|
|
358
|
+
const nativeLicenseInfo = JSON.parse(STANDARD_LICENSE_INFO)
|
|
359
|
+
nativeLicenseInfo[PROPERTIES.FLAGS] = FLAGS.NATIVE_LICENSE
|
|
360
|
+
nativeLicenseInfo[PROPERTIES.HOSTS] = ['app-bundle:']
|
|
361
|
+
const nativeLicenseKey = await generateLicenseKey(JSON.stringify(nativeLicenseInfo), keyPair)
|
|
362
|
+
const result = (await licenseManager.getLicenseFromKey(
|
|
363
|
+
nativeLicenseKey
|
|
364
|
+
)) as ValidLicenseKeyResult
|
|
365
|
+
expect(result.isDomainValid).toBe(false)
|
|
366
|
+
})
|
|
303
367
|
})
|
|
304
368
|
|
|
305
369
|
describe('License types and flags', () => {
|
|
@@ -316,6 +380,17 @@ describe('LicenseManager', () => {
|
|
|
316
380
|
expect(result.isInternalLicense).toBe(true)
|
|
317
381
|
})
|
|
318
382
|
|
|
383
|
+
it('Checks for native license', async () => {
|
|
384
|
+
const nativeLicenseInfo = JSON.parse(STANDARD_LICENSE_INFO)
|
|
385
|
+
nativeLicenseInfo[PROPERTIES.FLAGS] = FLAGS.NATIVE_LICENSE
|
|
386
|
+
const nativeLicenseKey = await generateLicenseKey(JSON.stringify(nativeLicenseInfo), keyPair)
|
|
387
|
+
|
|
388
|
+
const result = (await licenseManager.getLicenseFromKey(
|
|
389
|
+
nativeLicenseKey
|
|
390
|
+
)) as ValidLicenseKeyResult
|
|
391
|
+
expect(result.isNativeLicense).toBe(true)
|
|
392
|
+
})
|
|
393
|
+
|
|
319
394
|
it('Checks for license with watermark', async () => {
|
|
320
395
|
const withWatermarkLicenseInfo = JSON.parse(STANDARD_LICENSE_INFO)
|
|
321
396
|
withWatermarkLicenseInfo[PROPERTIES.FLAGS] |= FLAGS.WITH_WATERMARK
|
|
@@ -553,6 +628,7 @@ function getDefaultLicenseResult(overrides: Partial<ValidLicenseKeyResult>): Val
|
|
|
553
628
|
isAnnualLicense: true,
|
|
554
629
|
isAnnualLicenseExpired: false,
|
|
555
630
|
isInternalLicense: false,
|
|
631
|
+
isNativeLicense: false,
|
|
556
632
|
isDevelopment: false,
|
|
557
633
|
isDomainValid: true,
|
|
558
634
|
isPerpetualLicense: false,
|
|
@@ -6,11 +6,22 @@ import { importPublicKey, str2ab } from '../utils/licensing'
|
|
|
6
6
|
const GRACE_PERIOD_DAYS = 30
|
|
7
7
|
|
|
8
8
|
export const FLAGS = {
|
|
9
|
+
// -- MUTUALLY EXCLUSIVE FLAGS --
|
|
10
|
+
// Annual means the license expires after a time period, usually 1 year.
|
|
9
11
|
ANNUAL_LICENSE: 1,
|
|
12
|
+
// Perpetual means the license never expires up to the max supported version.
|
|
10
13
|
PERPETUAL_LICENSE: 1 << 1,
|
|
14
|
+
|
|
15
|
+
// -- ADDITIVE FLAGS --
|
|
16
|
+
// Internal means the license is for internal use only.
|
|
11
17
|
INTERNAL_LICENSE: 1 << 2,
|
|
18
|
+
// Watermark means the product is watermarked.
|
|
12
19
|
WITH_WATERMARK: 1 << 3,
|
|
20
|
+
// Evaluation means the license is for evaluation purposes only.
|
|
13
21
|
EVALUATION_LICENSE: 1 << 4,
|
|
22
|
+
// Native means the license is for native apps which switches
|
|
23
|
+
// on special-case logic.
|
|
24
|
+
NATIVE_LICENSE: 1 << 5,
|
|
14
25
|
}
|
|
15
26
|
const HIGHEST_FLAG = Math.max(...Object.values(FLAGS))
|
|
16
27
|
|
|
@@ -69,6 +80,7 @@ export interface ValidLicenseKeyResult {
|
|
|
69
80
|
isPerpetualLicense: boolean
|
|
70
81
|
isPerpetualLicenseExpired: boolean
|
|
71
82
|
isInternalLicense: boolean
|
|
83
|
+
isNativeLicense: boolean
|
|
72
84
|
isLicensedWithWatermark: boolean
|
|
73
85
|
isEvaluationLicense: boolean
|
|
74
86
|
isEvaluationLicenseExpired: boolean
|
|
@@ -271,6 +283,7 @@ export class LicenseManager {
|
|
|
271
283
|
isPerpetualLicense,
|
|
272
284
|
isPerpetualLicenseExpired: isPerpetualLicense && this.isPerpetualLicenseExpired(expiryDate),
|
|
273
285
|
isInternalLicense: this.isFlagEnabled(licenseInfo.flags, FLAGS.INTERNAL_LICENSE),
|
|
286
|
+
isNativeLicense: this.isNativeLicense(licenseInfo),
|
|
274
287
|
isLicensedWithWatermark: this.isFlagEnabled(licenseInfo.flags, FLAGS.WITH_WATERMARK),
|
|
275
288
|
isEvaluationLicense,
|
|
276
289
|
isEvaluationLicenseExpired:
|
|
@@ -291,13 +304,13 @@ export class LicenseManager {
|
|
|
291
304
|
const currentHostname = window.location.hostname.toLowerCase()
|
|
292
305
|
|
|
293
306
|
return licenseInfo.hosts.some((host) => {
|
|
294
|
-
const
|
|
307
|
+
const normalizedHostOrUrlRegex = host.toLowerCase().trim()
|
|
295
308
|
|
|
296
309
|
// Allow the domain if listed and www variations, 'example.com' allows 'example.com' and 'www.example.com'
|
|
297
310
|
if (
|
|
298
|
-
|
|
299
|
-
`www.${
|
|
300
|
-
|
|
311
|
+
normalizedHostOrUrlRegex === currentHostname ||
|
|
312
|
+
`www.${normalizedHostOrUrlRegex}` === currentHostname ||
|
|
313
|
+
normalizedHostOrUrlRegex === `www.${currentHostname}`
|
|
301
314
|
) {
|
|
302
315
|
return true
|
|
303
316
|
}
|
|
@@ -308,6 +321,12 @@ export class LicenseManager {
|
|
|
308
321
|
return true
|
|
309
322
|
}
|
|
310
323
|
|
|
324
|
+
// Native license support
|
|
325
|
+
// In this case, `normalizedHost` is actually a protocol, e.g. `app-bundle:`
|
|
326
|
+
if (this.isNativeLicense(licenseInfo)) {
|
|
327
|
+
return new RegExp(normalizedHostOrUrlRegex).test(window.location.href)
|
|
328
|
+
}
|
|
329
|
+
|
|
311
330
|
// Glob testing, we only support '*.somedomain.com' right now.
|
|
312
331
|
if (host.includes('*')) {
|
|
313
332
|
const globToRegex = new RegExp(host.replace(/\*/g, '.*?'))
|
|
@@ -318,7 +337,7 @@ export class LicenseManager {
|
|
|
318
337
|
if (window.location.protocol === 'vscode-webview:') {
|
|
319
338
|
const currentUrl = new URL(window.location.href)
|
|
320
339
|
const extensionId = currentUrl.searchParams.get('extensionId')
|
|
321
|
-
if (
|
|
340
|
+
if (normalizedHostOrUrlRegex === extensionId) {
|
|
322
341
|
return true
|
|
323
342
|
}
|
|
324
343
|
}
|
|
@@ -327,6 +346,10 @@ export class LicenseManager {
|
|
|
327
346
|
})
|
|
328
347
|
}
|
|
329
348
|
|
|
349
|
+
private isNativeLicense(licenseInfo: LicenseInfo) {
|
|
350
|
+
return this.isFlagEnabled(licenseInfo.flags, FLAGS.NATIVE_LICENSE)
|
|
351
|
+
}
|
|
352
|
+
|
|
330
353
|
private getExpirationDateWithoutGracePeriod(expiryDate: Date) {
|
|
331
354
|
return new Date(expiryDate.getFullYear(), expiryDate.getMonth(), expiryDate.getDate())
|
|
332
355
|
}
|
|
@@ -17,7 +17,7 @@ export const LICENSE_TIMEOUT = 5000
|
|
|
17
17
|
|
|
18
18
|
/** @internal */
|
|
19
19
|
export function LicenseProvider({
|
|
20
|
-
licenseKey,
|
|
20
|
+
licenseKey = getLicenseKeyFromEnv() ?? undefined,
|
|
21
21
|
children,
|
|
22
22
|
}: {
|
|
23
23
|
licenseKey?: string
|
|
@@ -51,3 +51,42 @@ export function LicenseProvider({
|
|
|
51
51
|
function LicenseGate() {
|
|
52
52
|
return <div data-testid="tl-license-expired" style={{ display: 'none' }} />
|
|
53
53
|
}
|
|
54
|
+
|
|
55
|
+
let envLicenseKey: string | undefined | null = undefined
|
|
56
|
+
function getLicenseKeyFromEnv() {
|
|
57
|
+
if (envLicenseKey !== undefined) {
|
|
58
|
+
return envLicenseKey
|
|
59
|
+
}
|
|
60
|
+
// it's important here that we write out the full process.env.WHATEVER expression instead of
|
|
61
|
+
// doing something like process.env[someVariable]. This is because most bundlers do something
|
|
62
|
+
// like a find-replace inject environment variables, and so won't pick up on dynamic ones. It
|
|
63
|
+
// also means we can't do checks like `process.env && process.env.WHATEVER`, which is why we use
|
|
64
|
+
// the `getEnv` try/catch approach.
|
|
65
|
+
|
|
66
|
+
// framework-specific prefixes borrowed from the ones vercel uses, but trimmed down to just the
|
|
67
|
+
// react-y ones: https://vercel.com/docs/environment-variables/framework-environment-variables
|
|
68
|
+
envLicenseKey =
|
|
69
|
+
getEnv(() => process.env.TLDRAW_LICENSE_KEY) ||
|
|
70
|
+
getEnv(() => process.env.NEXT_PUBLIC_TLDRAW_LICENSE_KEY) ||
|
|
71
|
+
getEnv(() => process.env.REACT_APP_TLDRAW_LICENSE_KEY) ||
|
|
72
|
+
getEnv(() => process.env.GATSBY_TLDRAW_LICENSE_KEY) ||
|
|
73
|
+
getEnv(() => process.env.VITE_TLDRAW_LICENSE_KEY) ||
|
|
74
|
+
getEnv(() => process.env.PUBLIC_TLDRAW_LICENSE_KEY) ||
|
|
75
|
+
getEnv(() => (import.meta as any).env.TLDRAW_LICENSE_KEY) ||
|
|
76
|
+
getEnv(() => (import.meta as any).env.NEXT_PUBLIC_TLDRAW_LICENSE_KEY) ||
|
|
77
|
+
getEnv(() => (import.meta as any).env.REACT_APP_TLDRAW_LICENSE_KEY) ||
|
|
78
|
+
getEnv(() => (import.meta as any).env.GATSBY_TLDRAW_LICENSE_KEY) ||
|
|
79
|
+
getEnv(() => (import.meta as any).env.VITE_TLDRAW_LICENSE_KEY) ||
|
|
80
|
+
getEnv(() => (import.meta as any).env.PUBLIC_TLDRAW_LICENSE_KEY) ||
|
|
81
|
+
null
|
|
82
|
+
|
|
83
|
+
return envLicenseKey
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function getEnv(cb: () => string | undefined) {
|
|
87
|
+
try {
|
|
88
|
+
return cb()
|
|
89
|
+
} catch {
|
|
90
|
+
return undefined
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -3,7 +3,7 @@ import { memo, useRef } from 'react'
|
|
|
3
3
|
import { useCanvasEvents } from '../hooks/useCanvasEvents'
|
|
4
4
|
import { useEditor } from '../hooks/useEditor'
|
|
5
5
|
import { usePassThroughWheelEvents } from '../hooks/usePassThroughWheelEvents'
|
|
6
|
-
import {
|
|
6
|
+
import { markEventAsHandled, preventDefault } from '../utils/dom'
|
|
7
7
|
import { runtime } from '../utils/runtime'
|
|
8
8
|
import { watermarkDesktopSvg, watermarkMobileSvg } from '../watermarks'
|
|
9
9
|
import { LicenseManager } from './LicenseManager'
|
|
@@ -64,7 +64,7 @@ const UnlicensedWatermark = memo(function UnlicensedWatermark({
|
|
|
64
64
|
draggable={false}
|
|
65
65
|
role="button"
|
|
66
66
|
onPointerDown={(e) => {
|
|
67
|
-
|
|
67
|
+
markEventAsHandled(e)
|
|
68
68
|
preventDefault(e)
|
|
69
69
|
}}
|
|
70
70
|
title="Unlicensed - click to get a license"
|
|
@@ -127,7 +127,7 @@ const WatermarkInner = memo(function WatermarkInner({
|
|
|
127
127
|
draggable={false}
|
|
128
128
|
role="button"
|
|
129
129
|
onPointerDown={(e) => {
|
|
130
|
-
|
|
130
|
+
markEventAsHandled(e)
|
|
131
131
|
preventDefault(e)
|
|
132
132
|
}}
|
|
133
133
|
title="made with tldraw"
|