@tldraw/editor 3.16.0-canary.faec5de49906 → 3.16.0-canary.fe4babe9c1ad

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 (87) hide show
  1. package/dist-cjs/index.d.ts +42 -3
  2. package/dist-cjs/index.js +4 -2
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/TldrawEditor.js +1 -1
  5. package/dist-cjs/lib/TldrawEditor.js.map +2 -2
  6. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +11 -1
  7. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  8. package/dist-cjs/lib/editor/Editor.js +6 -2
  9. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  10. package/dist-cjs/lib/editor/managers/FocusManager/FocusManager.js +4 -2
  11. package/dist-cjs/lib/editor/managers/FocusManager/FocusManager.js.map +2 -2
  12. package/dist-cjs/lib/hooks/useCanvasEvents.js +15 -12
  13. package/dist-cjs/lib/hooks/useCanvasEvents.js.map +2 -2
  14. package/dist-cjs/lib/hooks/useDocumentEvents.js +5 -5
  15. package/dist-cjs/lib/hooks/useDocumentEvents.js.map +2 -2
  16. package/dist-cjs/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.js +1 -2
  17. package/dist-cjs/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.js.map +2 -2
  18. package/dist-cjs/lib/hooks/useGestureEvents.js +1 -1
  19. package/dist-cjs/lib/hooks/useGestureEvents.js.map +2 -2
  20. package/dist-cjs/lib/hooks/useHandleEvents.js +3 -3
  21. package/dist-cjs/lib/hooks/useHandleEvents.js.map +2 -2
  22. package/dist-cjs/lib/hooks/useSelectionEvents.js +4 -4
  23. package/dist-cjs/lib/hooks/useSelectionEvents.js.map +2 -2
  24. package/dist-cjs/lib/license/LicenseManager.js +24 -4
  25. package/dist-cjs/lib/license/LicenseManager.js.map +2 -2
  26. package/dist-cjs/lib/license/Watermark.js +2 -2
  27. package/dist-cjs/lib/license/Watermark.js.map +2 -2
  28. package/dist-cjs/lib/utils/dom.js +12 -1
  29. package/dist-cjs/lib/utils/dom.js.map +2 -2
  30. package/dist-cjs/lib/utils/getPointerInfo.js +2 -2
  31. package/dist-cjs/lib/utils/getPointerInfo.js.map +2 -2
  32. package/dist-cjs/version.js +3 -3
  33. package/dist-cjs/version.js.map +1 -1
  34. package/dist-esm/index.d.mts +42 -3
  35. package/dist-esm/index.mjs +7 -3
  36. package/dist-esm/index.mjs.map +2 -2
  37. package/dist-esm/lib/TldrawEditor.mjs +2 -2
  38. package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
  39. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +12 -2
  40. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  41. package/dist-esm/lib/editor/Editor.mjs +6 -2
  42. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  43. package/dist-esm/lib/editor/managers/FocusManager/FocusManager.mjs +4 -2
  44. package/dist-esm/lib/editor/managers/FocusManager/FocusManager.mjs.map +2 -2
  45. package/dist-esm/lib/hooks/useCanvasEvents.mjs +17 -13
  46. package/dist-esm/lib/hooks/useCanvasEvents.mjs.map +2 -2
  47. package/dist-esm/lib/hooks/useDocumentEvents.mjs +11 -6
  48. package/dist-esm/lib/hooks/useDocumentEvents.mjs.map +2 -2
  49. package/dist-esm/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.mjs +2 -3
  50. package/dist-esm/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.mjs.map +2 -2
  51. package/dist-esm/lib/hooks/useGestureEvents.mjs +2 -2
  52. package/dist-esm/lib/hooks/useGestureEvents.mjs.map +2 -2
  53. package/dist-esm/lib/hooks/useHandleEvents.mjs +9 -4
  54. package/dist-esm/lib/hooks/useHandleEvents.mjs.map +2 -2
  55. package/dist-esm/lib/hooks/useSelectionEvents.mjs +6 -5
  56. package/dist-esm/lib/hooks/useSelectionEvents.mjs.map +2 -2
  57. package/dist-esm/lib/license/LicenseManager.mjs +24 -4
  58. package/dist-esm/lib/license/LicenseManager.mjs.map +2 -2
  59. package/dist-esm/lib/license/Watermark.mjs +3 -3
  60. package/dist-esm/lib/license/Watermark.mjs.map +2 -2
  61. package/dist-esm/lib/utils/dom.mjs +12 -1
  62. package/dist-esm/lib/utils/dom.mjs.map +2 -2
  63. package/dist-esm/lib/utils/getPointerInfo.mjs +2 -2
  64. package/dist-esm/lib/utils/getPointerInfo.mjs.map +2 -2
  65. package/dist-esm/version.mjs +3 -3
  66. package/dist-esm/version.mjs.map +1 -1
  67. package/package.json +7 -7
  68. package/src/index.ts +2 -0
  69. package/src/lib/TldrawEditor.tsx +2 -2
  70. package/src/lib/components/default-components/DefaultCanvas.tsx +8 -2
  71. package/src/lib/editor/Editor.test.ts +90 -0
  72. package/src/lib/editor/Editor.ts +12 -2
  73. package/src/lib/editor/managers/FocusManager/FocusManager.ts +6 -2
  74. package/src/lib/hooks/useCanvasEvents.ts +17 -11
  75. package/src/lib/hooks/useDocumentEvents.ts +11 -6
  76. package/src/lib/hooks/useFixSafariDoubleTapZoomPencilEvents.ts +2 -2
  77. package/src/lib/hooks/useGestureEvents.ts +2 -2
  78. package/src/lib/hooks/useHandleEvents.ts +9 -4
  79. package/src/lib/hooks/useSelectionEvents.ts +6 -5
  80. package/src/lib/license/LicenseManager.test.ts +78 -2
  81. package/src/lib/license/LicenseManager.ts +31 -5
  82. package/src/lib/license/Watermark.tsx +3 -3
  83. package/src/lib/test/InFrontOfTheCanvas.test.tsx +187 -0
  84. package/src/lib/utils/dom.test.ts +94 -0
  85. package/src/lib/utils/dom.ts +38 -1
  86. package/src/lib/utils/getPointerInfo.ts +2 -1
  87. package/src/version.ts +3 -3
@@ -58,8 +58,12 @@ export class FocusManager {
58
58
 
59
59
  private handleKeyDown(keyEvent: KeyboardEvent) {
60
60
  const container = this.editor.getContainer()
61
- if (this.editor.isIn('select.editing_shape')) return
62
- if (document.activeElement === container && this.editor.getSelectedShapeIds().length > 0) return
61
+ const activeEl = document.activeElement
62
+ // Edit mode should remove the focus ring, however if the active element's
63
+ // parent is the contextual toolbar, then allow it.
64
+ if (this.editor.isIn('select.editing_shape') && !activeEl?.closest('.tlui-contextual-toolbar'))
65
+ return
66
+ if (activeEl === container && this.editor.getSelectedShapeIds().length > 0) return
63
67
  if (['Tab', 'ArrowUp', 'ArrowDown'].includes(keyEvent.key)) {
64
68
  container.classList.remove('tl-container__no-focus-ring')
65
69
  }
@@ -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
- stopEventPropagation,
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 as any).isKilled) return
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 as any).isKilled) return
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 as any).isKilled) return
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 as any).isKilled) return
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
- ;(e as any).isKilled = true
74
+ if (wasEventAlreadyHandled(e)) return
75
+ markEventAsHandled(e)
74
76
  preventDefault(e)
75
77
  }
76
78
 
77
79
  function onTouchEnd(e: React.TouchEvent) {
78
- ;(e as any).isKilled = true
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
- stopEventPropagation(e)
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
- stopEventPropagation(e)
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 as any).isKilled) return
155
- ;(e as any).isKilled = true
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 { activeElementShouldCaptureKeys, preventDefault, stopEventPropagation } from '../utils/dom'
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
- stopEventPropagation(e)
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 as any).isKilled) return
107
- ;(e as any).isKilled = true
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 as any).isKilled) return
215
- ;(e as any).isKilled = true
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
- ;(e as any).isKilled = true
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, stopEventPropagation } from '../utils/dom'
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
- stopEventPropagation(event)
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 { loopToHtmlElement, releasePointerCapture, setPointerCapture } from '../utils/dom'
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 as any).isKilled) return
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 as any).isKilled) return
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 as any).isKilled) return
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
- stopEventPropagation,
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 as any).isKilled) return
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
- stopEventPropagation(e)
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 as any).isKilled) return
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 as any).isKilled) return
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: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'
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: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'
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
@@ -166,6 +178,9 @@ export class LicenseManager {
166
178
  const url = new URL(WATERMARK_TRACK_SRC)
167
179
  url.searchParams.set('version', version)
168
180
  url.searchParams.set('license_type', trackType)
181
+ if ('license' in result) {
182
+ url.searchParams.set('license_id', result.license.id)
183
+ }
169
184
 
170
185
  // eslint-disable-next-line no-restricted-globals
171
186
  fetch(url.toString())
@@ -271,6 +286,7 @@ export class LicenseManager {
271
286
  isPerpetualLicense,
272
287
  isPerpetualLicenseExpired: isPerpetualLicense && this.isPerpetualLicenseExpired(expiryDate),
273
288
  isInternalLicense: this.isFlagEnabled(licenseInfo.flags, FLAGS.INTERNAL_LICENSE),
289
+ isNativeLicense: this.isNativeLicense(licenseInfo),
274
290
  isLicensedWithWatermark: this.isFlagEnabled(licenseInfo.flags, FLAGS.WITH_WATERMARK),
275
291
  isEvaluationLicense,
276
292
  isEvaluationLicenseExpired:
@@ -291,13 +307,13 @@ export class LicenseManager {
291
307
  const currentHostname = window.location.hostname.toLowerCase()
292
308
 
293
309
  return licenseInfo.hosts.some((host) => {
294
- const normalizedHost = host.toLowerCase().trim()
310
+ const normalizedHostOrUrlRegex = host.toLowerCase().trim()
295
311
 
296
312
  // Allow the domain if listed and www variations, 'example.com' allows 'example.com' and 'www.example.com'
297
313
  if (
298
- normalizedHost === currentHostname ||
299
- `www.${normalizedHost}` === currentHostname ||
300
- normalizedHost === `www.${currentHostname}`
314
+ normalizedHostOrUrlRegex === currentHostname ||
315
+ `www.${normalizedHostOrUrlRegex}` === currentHostname ||
316
+ normalizedHostOrUrlRegex === `www.${currentHostname}`
301
317
  ) {
302
318
  return true
303
319
  }
@@ -308,6 +324,12 @@ export class LicenseManager {
308
324
  return true
309
325
  }
310
326
 
327
+ // Native license support
328
+ // In this case, `normalizedHost` is actually a protocol, e.g. `app-bundle:`
329
+ if (this.isNativeLicense(licenseInfo)) {
330
+ return new RegExp(normalizedHostOrUrlRegex).test(window.location.href)
331
+ }
332
+
311
333
  // Glob testing, we only support '*.somedomain.com' right now.
312
334
  if (host.includes('*')) {
313
335
  const globToRegex = new RegExp(host.replace(/\*/g, '.*?'))
@@ -318,7 +340,7 @@ export class LicenseManager {
318
340
  if (window.location.protocol === 'vscode-webview:') {
319
341
  const currentUrl = new URL(window.location.href)
320
342
  const extensionId = currentUrl.searchParams.get('extensionId')
321
- if (normalizedHost === extensionId) {
343
+ if (normalizedHostOrUrlRegex === extensionId) {
322
344
  return true
323
345
  }
324
346
  }
@@ -327,6 +349,10 @@ export class LicenseManager {
327
349
  })
328
350
  }
329
351
 
352
+ private isNativeLicense(licenseInfo: LicenseInfo) {
353
+ return this.isFlagEnabled(licenseInfo.flags, FLAGS.NATIVE_LICENSE)
354
+ }
355
+
330
356
  private getExpirationDateWithoutGracePeriod(expiryDate: Date) {
331
357
  return new Date(expiryDate.getFullYear(), expiryDate.getMonth(), expiryDate.getDate())
332
358
  }
@@ -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 { preventDefault, stopEventPropagation } from '../utils/dom'
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
- stopEventPropagation(e)
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
- stopEventPropagation(e)
130
+ markEventAsHandled(e)
131
131
  preventDefault(e)
132
132
  }}
133
133
  title="made with tldraw"