|
|
@@ -62,6 +62,10 @@ const initialData: Data = {
|
|
62
|
62
|
isToolLocked: false,
|
|
63
|
63
|
isStyleOpen: false,
|
|
64
|
64
|
isEmptyCanvas: false,
|
|
|
65
|
+ status: {
|
|
|
66
|
+ current: TLDrawStatus.Idle,
|
|
|
67
|
+ previous: TLDrawStatus.Idle,
|
|
|
68
|
+ },
|
|
65
|
69
|
},
|
|
66
|
70
|
page: {
|
|
67
|
71
|
id: 'page',
|
|
|
@@ -87,10 +91,6 @@ export class TLDrawState implements TLCallbacks {
|
|
87
|
91
|
}
|
|
88
|
92
|
clipboard?: TLDrawShape[]
|
|
89
|
93
|
session?: Session
|
|
90
|
|
- status: { current: TLDrawStatus; previous: TLDrawStatus } = {
|
|
91
|
|
- current: 'idle',
|
|
92
|
|
- previous: 'idle',
|
|
93
|
|
- }
|
|
94
|
94
|
pointedId?: string
|
|
95
|
95
|
pointedHandle?: string
|
|
96
|
96
|
editingId?: string
|
|
|
@@ -99,12 +99,28 @@ export class TLDrawState implements TLCallbacks {
|
|
99
|
99
|
currentPageId = 'page'
|
|
100
|
100
|
pages: Record<string, TLPage<TLDrawShape, TLDrawBinding>> = { page: initialData.page }
|
|
101
|
101
|
pageStates: Record<string, TLPageState> = { page: initialData.pageState }
|
|
|
102
|
+ isCreating = false
|
|
102
|
103
|
_onChange?: (state: TLDrawState, reason: string) => void
|
|
103
|
104
|
|
|
104
|
105
|
// Low API
|
|
105
|
|
- getState = this.store.getState
|
|
|
106
|
+ private getState = this.store.getState
|
|
106
|
107
|
|
|
107
|
|
- setState = <T extends keyof Data>(data: Partial<Data> | ((data: Data) => Partial<Data>)) => {
|
|
|
108
|
+ private setStatus(status: TLDrawStatus) {
|
|
|
109
|
+ this.store.setState((state) => ({
|
|
|
110
|
+ appState: {
|
|
|
111
|
+ ...state.appState,
|
|
|
112
|
+ status: {
|
|
|
113
|
+ current: status,
|
|
|
114
|
+ previous: state.appState.status.current,
|
|
|
115
|
+ },
|
|
|
116
|
+ },
|
|
|
117
|
+ }))
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
|
120
|
+ private setState = <T extends keyof Data>(
|
|
|
121
|
+ data: Partial<Data> | ((data: Data) => Partial<Data>),
|
|
|
122
|
+ status?: TLDrawStatus
|
|
|
123
|
+ ) => {
|
|
108
|
124
|
const current = this.getState()
|
|
109
|
125
|
|
|
110
|
126
|
// Apply incoming change
|
|
|
@@ -190,6 +206,16 @@ export class TLDrawState implements TLCallbacks {
|
|
190
|
206
|
}
|
|
191
|
207
|
}
|
|
192
|
208
|
|
|
|
209
|
+ if (status) {
|
|
|
210
|
+ next.appState = {
|
|
|
211
|
+ ...next.appState,
|
|
|
212
|
+ status: {
|
|
|
213
|
+ current: status,
|
|
|
214
|
+ previous: next.appState.status.current,
|
|
|
215
|
+ },
|
|
|
216
|
+ }
|
|
|
217
|
+ }
|
|
|
218
|
+
|
|
193
|
219
|
this.store.setState(next as PartialState<Data, T, T, T>)
|
|
194
|
220
|
this.pages[next.page.id] = next.page
|
|
195
|
221
|
this.pageStates[next.page.id] = next.pageState
|
|
|
@@ -249,12 +275,12 @@ export class TLDrawState implements TLCallbacks {
|
|
249
|
275
|
return this
|
|
250
|
276
|
}
|
|
251
|
277
|
/* --------------------- Status --------------------- */
|
|
252
|
|
- setStatus(status: TLDrawStatus) {
|
|
253
|
|
- this.status.previous = this.status.current
|
|
254
|
|
- this.status.current = status
|
|
255
|
|
- // console.log(this.status.previous, ' -> ', this.status.current)
|
|
256
|
|
- return this
|
|
257
|
|
- }
|
|
|
278
|
+ // setStatus(status: TLDrawStatus) {
|
|
|
279
|
+ // this.status.previous = this.status.current
|
|
|
280
|
+ // this.status.current = status
|
|
|
281
|
+ // // console.log(this.status.previous, ' -> ', this.status.current)
|
|
|
282
|
+ // return this
|
|
|
283
|
+ // }
|
|
258
|
284
|
/* -------------------- App State ------------------- */
|
|
259
|
285
|
reset = () => {
|
|
260
|
286
|
this.setState((data) => ({
|
|
|
@@ -525,7 +551,7 @@ export class TLDrawState implements TLCallbacks {
|
|
525
|
551
|
/* -------------------- Sessions -------------------- */
|
|
526
|
552
|
startSession<T extends Session>(session: T, ...args: ParametersExceptFirst<T['start']>) {
|
|
527
|
553
|
this.session = session
|
|
528
|
|
- this.setState((data) => session.start(data, ...args))
|
|
|
554
|
+ this.setState((data) => session.start(data, ...args), session.status)
|
|
529
|
555
|
this._onChange?.(this, `session:start_${session.id}`)
|
|
530
|
556
|
return this
|
|
531
|
557
|
}
|
|
|
@@ -542,10 +568,13 @@ export class TLDrawState implements TLCallbacks {
|
|
542
|
568
|
const { session } = this
|
|
543
|
569
|
if (!session) return this
|
|
544
|
570
|
|
|
545
|
|
- this.setState((data) => session.cancel(data, ...args))
|
|
546
|
|
- this.setStatus('idle')
|
|
|
571
|
+ this.setState((data) => session.cancel(data, ...args), TLDrawStatus.Idle)
|
|
|
572
|
+
|
|
547
|
573
|
this.session = undefined
|
|
|
574
|
+ this.isCreating = false
|
|
|
575
|
+
|
|
548
|
576
|
this._onChange?.(this, `session:cancel:${session.id}`)
|
|
|
577
|
+
|
|
549
|
578
|
return this
|
|
550
|
579
|
}
|
|
551
|
580
|
|
|
|
@@ -553,16 +582,9 @@ export class TLDrawState implements TLCallbacks {
|
|
553
|
582
|
const { session } = this
|
|
554
|
583
|
if (!session) return this
|
|
555
|
584
|
|
|
556
|
|
- this.setStatus('idle')
|
|
557
|
|
-
|
|
558
|
|
- const result = session.complete(this.store.getState(), ...args)
|
|
|
585
|
+ const current = this.getState()
|
|
559
|
586
|
|
|
560
|
|
- if ('after' in result) {
|
|
561
|
|
- this.do(result)
|
|
562
|
|
- } else {
|
|
563
|
|
- this.setState((data) => Utils.deepMerge<Data>(data, result))
|
|
564
|
|
- this._onChange?.(this, `session:complete:${session.id}`)
|
|
565
|
|
- }
|
|
|
587
|
+ const result = session.complete(current, ...args)
|
|
566
|
588
|
|
|
567
|
589
|
const { isToolLocked, activeTool } = this.appState
|
|
568
|
590
|
|
|
|
@@ -571,6 +593,35 @@ export class TLDrawState implements TLCallbacks {
|
|
571
|
593
|
}
|
|
572
|
594
|
|
|
573
|
595
|
this.session = undefined
|
|
|
596
|
+
|
|
|
597
|
+ if ('after' in result) {
|
|
|
598
|
+ // Session ended with a command
|
|
|
599
|
+
|
|
|
600
|
+ if (this.isCreating) {
|
|
|
601
|
+ // We're currently creating a shape. Override the command's
|
|
|
602
|
+ // before state so that when we undo the command, we remove
|
|
|
603
|
+ // the shape we just created.
|
|
|
604
|
+ result.before = {
|
|
|
605
|
+ page: {
|
|
|
606
|
+ shapes: Object.fromEntries(current.pageState.selectedIds.map((id) => [id, undefined])),
|
|
|
607
|
+ },
|
|
|
608
|
+ pageState: {
|
|
|
609
|
+ selectedIds: [],
|
|
|
610
|
+ editingId: undefined,
|
|
|
611
|
+ bindingId: undefined,
|
|
|
612
|
+ hoveredId: undefined,
|
|
|
613
|
+ },
|
|
|
614
|
+ }
|
|
|
615
|
+ }
|
|
|
616
|
+
|
|
|
617
|
+ this.isCreating = false
|
|
|
618
|
+
|
|
|
619
|
+ this.do(result)
|
|
|
620
|
+ } else {
|
|
|
621
|
+ this.setState((data) => Utils.deepMerge<Data>(data, result), TLDrawStatus.Idle)
|
|
|
622
|
+ this._onChange?.(this, `session:complete:${session.id}`)
|
|
|
623
|
+ }
|
|
|
624
|
+
|
|
574
|
625
|
return this
|
|
575
|
626
|
}
|
|
576
|
627
|
|
|
|
@@ -586,12 +637,14 @@ export class TLDrawState implements TLCallbacks {
|
|
586
|
637
|
|
|
587
|
638
|
history.pointer = history.stack.length - 1
|
|
588
|
639
|
|
|
589
|
|
- this.setState((data) =>
|
|
590
|
|
- Object.fromEntries(
|
|
591
|
|
- Object.entries(command.after).map(([key, partial]) => {
|
|
592
|
|
- return [key, Utils.deepMerge(data[key as keyof Data], partial)]
|
|
593
|
|
- })
|
|
594
|
|
- )
|
|
|
640
|
+ this.setState(
|
|
|
641
|
+ (data) =>
|
|
|
642
|
+ Object.fromEntries(
|
|
|
643
|
+ Object.entries(command.after).map(([key, partial]) => {
|
|
|
644
|
+ return [key, Utils.deepMerge(data[key as keyof Data], partial)]
|
|
|
645
|
+ })
|
|
|
646
|
+ ),
|
|
|
647
|
+ TLDrawStatus.Idle
|
|
595
|
648
|
)
|
|
596
|
649
|
|
|
597
|
650
|
this._onChange?.(this, `command:${command.id}`)
|
|
|
@@ -606,12 +659,14 @@ export class TLDrawState implements TLCallbacks {
|
|
606
|
659
|
|
|
607
|
660
|
const command = history.stack[history.pointer]
|
|
608
|
661
|
|
|
609
|
|
- this.setState((data) =>
|
|
610
|
|
- Object.fromEntries(
|
|
611
|
|
- Object.entries(command.before).map(([key, partial]) => {
|
|
612
|
|
- return [key, Utils.deepMerge(data[key as keyof Data], partial)]
|
|
613
|
|
- })
|
|
614
|
|
- )
|
|
|
662
|
+ this.setState(
|
|
|
663
|
+ (data) =>
|
|
|
664
|
+ Object.fromEntries(
|
|
|
665
|
+ Object.entries(command.before).map(([key, partial]) => {
|
|
|
666
|
+ return [key, Utils.deepMerge(data[key as keyof Data], partial)]
|
|
|
667
|
+ })
|
|
|
668
|
+ ),
|
|
|
669
|
+ TLDrawStatus.Idle
|
|
615
|
670
|
)
|
|
616
|
671
|
|
|
617
|
672
|
history.pointer--
|
|
|
@@ -630,13 +685,16 @@ export class TLDrawState implements TLCallbacks {
|
|
630
|
685
|
|
|
631
|
686
|
const command = history.stack[history.pointer]
|
|
632
|
687
|
|
|
633
|
|
- this.setState((data) =>
|
|
634
|
|
- Object.fromEntries(
|
|
635
|
|
- Object.entries(command.after).map(([key, partial]) => {
|
|
636
|
|
- return [key, Utils.deepMerge(data[key as keyof Data], partial)]
|
|
637
|
|
- })
|
|
638
|
|
- )
|
|
|
688
|
+ this.setState(
|
|
|
689
|
+ (data) =>
|
|
|
690
|
+ Object.fromEntries(
|
|
|
691
|
+ Object.entries(command.after).map(([key, partial]) => {
|
|
|
692
|
+ return [key, Utils.deepMerge(data[key as keyof Data], partial)]
|
|
|
693
|
+ })
|
|
|
694
|
+ ),
|
|
|
695
|
+ TLDrawStatus.Idle
|
|
639
|
696
|
)
|
|
|
697
|
+
|
|
640
|
698
|
this._onChange?.(this, `redo:${command.id}`)
|
|
641
|
699
|
|
|
642
|
700
|
return this
|
|
|
@@ -834,7 +892,14 @@ export class TLDrawState implements TLCallbacks {
|
|
834
|
892
|
}
|
|
835
|
893
|
|
|
836
|
894
|
cancel = () => {
|
|
837
|
|
- switch (this.status.current) {
|
|
|
895
|
+ if (this.isCreating) {
|
|
|
896
|
+ this.cancelSession()
|
|
|
897
|
+ this.delete()
|
|
|
898
|
+ this.isCreating = false
|
|
|
899
|
+ return
|
|
|
900
|
+ }
|
|
|
901
|
+
|
|
|
902
|
+ switch (this.appState.status.current) {
|
|
838
|
903
|
case 'idle': {
|
|
839
|
904
|
this.deselectAll()
|
|
840
|
905
|
this.selectTool('select')
|
|
|
@@ -857,11 +922,6 @@ export class TLDrawState implements TLCallbacks {
|
|
857
|
922
|
this.cancelSession()
|
|
858
|
923
|
break
|
|
859
|
924
|
}
|
|
860
|
|
- case 'creating': {
|
|
861
|
|
- this.cancelSession()
|
|
862
|
|
- this.delete()
|
|
863
|
|
- break
|
|
864
|
|
- }
|
|
865
|
925
|
}
|
|
866
|
926
|
|
|
867
|
927
|
return this
|
|
|
@@ -968,7 +1028,6 @@ export class TLDrawState implements TLCallbacks {
|
|
968
|
1028
|
}
|
|
969
|
1029
|
/* -------------------- Sessions -------------------- */
|
|
970
|
1030
|
startBrushSession = (point: number[]) => {
|
|
971
|
|
- this.setStatus('brushing')
|
|
972
|
1031
|
this.startSession(new Sessions.BrushSession(this.store.getState(), point))
|
|
973
|
1032
|
return this
|
|
974
|
1033
|
}
|
|
|
@@ -979,7 +1038,6 @@ export class TLDrawState implements TLCallbacks {
|
|
979
|
1038
|
}
|
|
980
|
1039
|
|
|
981
|
1040
|
startTranslateSession = (point: number[]) => {
|
|
982
|
|
- this.setStatus('translating')
|
|
983
|
1041
|
this.startSession(new Sessions.TranslateSession(this.store.getState(), point))
|
|
984
|
1042
|
return this
|
|
985
|
1043
|
}
|
|
|
@@ -998,8 +1056,6 @@ export class TLDrawState implements TLCallbacks {
|
|
998
|
1056
|
|
|
999
|
1057
|
if (selectedIds.length === 0) return this
|
|
1000
|
1058
|
|
|
1001
|
|
- this.setStatus('transforming')
|
|
1002
|
|
-
|
|
1003
|
1059
|
this.pointedBoundsHandle = handle
|
|
1004
|
1060
|
|
|
1005
|
1061
|
if (this.pointedBoundsHandle === 'rotate') {
|
|
|
@@ -1032,7 +1088,6 @@ export class TLDrawState implements TLCallbacks {
|
|
1032
|
1088
|
|
|
1033
|
1089
|
startTextSession = (id?: string) => {
|
|
1034
|
1090
|
this.editingId = id
|
|
1035
|
|
- this.setStatus('editing-text')
|
|
1036
|
1091
|
this.startSession(new Sessions.TextSession(this.store.getState(), id))
|
|
1037
|
1092
|
return this
|
|
1038
|
1093
|
}
|
|
|
@@ -1043,7 +1098,6 @@ export class TLDrawState implements TLCallbacks {
|
|
1043
|
1098
|
}
|
|
1044
|
1099
|
|
|
1045
|
1100
|
startDrawSession = (id: string, point: number[]) => {
|
|
1046
|
|
- this.setStatus('creating')
|
|
1047
|
1101
|
this.startSession(new Sessions.DrawSession(this.store.getState(), id, point))
|
|
1048
|
1102
|
return this
|
|
1049
|
1103
|
}
|
|
|
@@ -1077,44 +1131,41 @@ export class TLDrawState implements TLCallbacks {
|
|
1077
|
1131
|
return this
|
|
1078
|
1132
|
}
|
|
1079
|
1133
|
|
|
1080
|
|
- updatenPointerMove: TLPointerEventHandler = (info) => {
|
|
|
1134
|
+ updateOnPointerMove: TLPointerEventHandler = (info) => {
|
|
1081
|
1135
|
switch (this.status.current) {
|
|
1082
|
|
- case 'pointingBoundsHandle': {
|
|
|
1136
|
+ case TLDrawStatus.PointingBoundsHandle: {
|
|
1083
|
1137
|
if (!this.pointedBoundsHandle) throw Error('No pointed bounds handle')
|
|
1084
|
1138
|
if (Vec.dist(info.origin, info.point) > 4) {
|
|
1085
|
|
- this.setStatus('transforming')
|
|
1086
|
1139
|
this.startTransformSession(this.getPagePoint(info.origin), this.pointedBoundsHandle)
|
|
1087
|
1140
|
}
|
|
1088
|
1141
|
break
|
|
1089
|
1142
|
}
|
|
1090
|
|
- case 'pointingHandle': {
|
|
|
1143
|
+ case TLDrawStatus.PointingHandle: {
|
|
1091
|
1144
|
if (!this.pointedHandle) throw Error('No pointed handle')
|
|
1092
|
1145
|
if (Vec.dist(info.origin, info.point) > 4) {
|
|
1093
|
|
- this.setStatus('translatingHandle')
|
|
1094
|
1146
|
this.startHandleSession(this.getPagePoint(info.origin), this.pointedHandle)
|
|
1095
|
1147
|
}
|
|
1096
|
1148
|
break
|
|
1097
|
1149
|
}
|
|
1098
|
|
- case 'pointingBounds': {
|
|
|
1150
|
+ case TLDrawStatus.PointingBounds: {
|
|
1099
|
1151
|
if (Vec.dist(info.origin, info.point) > 4) {
|
|
1100
|
|
- this.setStatus('translating')
|
|
1101
|
1152
|
this.startTranslateSession(this.getPagePoint(info.origin))
|
|
1102
|
1153
|
}
|
|
1103
|
1154
|
break
|
|
1104
|
1155
|
}
|
|
1105
|
|
- case 'brushing': {
|
|
|
1156
|
+ case TLDrawStatus.Brushing: {
|
|
1106
|
1157
|
this.updateBrushSession(this.getPagePoint(info.point), info.metaKey)
|
|
1107
|
1158
|
break
|
|
1108
|
1159
|
}
|
|
1109
|
|
- case 'translating': {
|
|
|
1160
|
+ case TLDrawStatus.Translating: {
|
|
1110
|
1161
|
this.updateTranslateSession(this.getPagePoint(info.point), info.shiftKey, info.altKey)
|
|
1111
|
1162
|
break
|
|
1112
|
1163
|
}
|
|
1113
|
|
- case 'transforming': {
|
|
|
1164
|
+ case TLDrawStatus.Transforming: {
|
|
1114
|
1165
|
this.updateTransformSession(this.getPagePoint(info.point), info.shiftKey, info.altKey)
|
|
1115
|
1166
|
break
|
|
1116
|
1167
|
}
|
|
1117
|
|
- case 'translatingHandle': {
|
|
|
1168
|
+ case TLDrawStatus.TranslatingHandle: {
|
|
1118
|
1169
|
this.updateHandleSession(
|
|
1119
|
1170
|
this.getPagePoint(info.point),
|
|
1120
|
1171
|
info.shiftKey,
|
|
|
@@ -1123,7 +1174,7 @@ export class TLDrawState implements TLCallbacks {
|
|
1123
|
1174
|
)
|
|
1124
|
1175
|
break
|
|
1125
|
1176
|
}
|
|
1126
|
|
- case 'creating': {
|
|
|
1177
|
+ case TLDrawStatus.Creating: {
|
|
1127
|
1178
|
switch (this.appState.activeToolType) {
|
|
1128
|
1179
|
case 'draw': {
|
|
1129
|
1180
|
this.updateDrawSession(this.getPagePoint(info.point), info.pressure, info.shiftKey)
|
|
|
@@ -1193,7 +1244,9 @@ export class TLDrawState implements TLCallbacks {
|
|
1193
|
1244
|
selectedIds: [id],
|
|
1194
|
1245
|
},
|
|
1195
|
1246
|
}
|
|
1196
|
|
- })
|
|
|
1247
|
+ }, TLDrawStatus.Creating)
|
|
|
1248
|
+
|
|
|
1249
|
+ this.isCreating = true
|
|
1197
|
1250
|
|
|
1198
|
1251
|
const { activeTool, activeToolType } = this.getAppState()
|
|
1199
|
1252
|
|
|
|
@@ -1231,10 +1284,10 @@ export class TLDrawState implements TLCallbacks {
|
|
1231
|
1284
|
}
|
|
1232
|
1285
|
|
|
1233
|
1286
|
switch (this.status.current) {
|
|
1234
|
|
- case 'idle': {
|
|
|
1287
|
+ case TLDrawStatus.Idle: {
|
|
1235
|
1288
|
break
|
|
1236
|
1289
|
}
|
|
1237
|
|
- case 'brushing': {
|
|
|
1290
|
+ case TLDrawStatus.Brushing: {
|
|
1238
|
1291
|
if (key === 'Meta' || key === 'Control') {
|
|
1239
|
1292
|
this.updateBrushSession(this.getPagePoint(info.point), info.metaKey)
|
|
1240
|
1293
|
return
|
|
|
@@ -1242,7 +1295,7 @@ export class TLDrawState implements TLCallbacks {
|
|
1242
|
1295
|
|
|
1243
|
1296
|
break
|
|
1244
|
1297
|
}
|
|
1245
|
|
- case 'translating': {
|
|
|
1298
|
+ case TLDrawStatus.Translating: {
|
|
1246
|
1299
|
if (key === 'Escape') {
|
|
1247
|
1300
|
this.cancelSession(this.getPagePoint(info.point))
|
|
1248
|
1301
|
}
|
|
|
@@ -1252,7 +1305,7 @@ export class TLDrawState implements TLCallbacks {
|
|
1252
|
1305
|
}
|
|
1253
|
1306
|
break
|
|
1254
|
1307
|
}
|
|
1255
|
|
- case 'transforming': {
|
|
|
1308
|
+ case TLDrawStatus.Transforming: {
|
|
1256
|
1309
|
if (key === 'Escape') {
|
|
1257
|
1310
|
this.cancelSession(this.getPagePoint(info.point))
|
|
1258
|
1311
|
}
|
|
|
@@ -1262,7 +1315,7 @@ export class TLDrawState implements TLCallbacks {
|
|
1262
|
1315
|
}
|
|
1263
|
1316
|
break
|
|
1264
|
1317
|
}
|
|
1265
|
|
- case 'translatingHandle': {
|
|
|
1318
|
+ case TLDrawStatus.TranslatingHandle: {
|
|
1266
|
1319
|
if (key === 'Escape') {
|
|
1267
|
1320
|
this.cancelSession(this.getPagePoint(info.point))
|
|
1268
|
1321
|
}
|
|
|
@@ -1282,25 +1335,25 @@ export class TLDrawState implements TLCallbacks {
|
|
1282
|
1335
|
|
|
1283
|
1336
|
onKeyUp = (key: string, info: TLKeyboardInfo) => {
|
|
1284
|
1337
|
switch (this.status.current) {
|
|
1285
|
|
- case 'brushing': {
|
|
|
1338
|
+ case TLDrawStatus.Brushing: {
|
|
1286
|
1339
|
if (key === 'Meta' || key === 'Control') {
|
|
1287
|
1340
|
this.updateBrushSession(this.getPagePoint(info.point), info.metaKey)
|
|
1288
|
1341
|
}
|
|
1289
|
1342
|
break
|
|
1290
|
1343
|
}
|
|
1291
|
|
- case 'transforming': {
|
|
|
1344
|
+ case TLDrawStatus.Transforming: {
|
|
1292
|
1345
|
if (key === 'Shift' || key === 'Alt') {
|
|
1293
|
1346
|
this.updateTransformSession(this.getPagePoint(info.point), info.shiftKey, info.altKey)
|
|
1294
|
1347
|
}
|
|
1295
|
1348
|
break
|
|
1296
|
1349
|
}
|
|
1297
|
|
- case 'translating': {
|
|
|
1350
|
+ case TLDrawStatus.Translating: {
|
|
1298
|
1351
|
if (key === 'Shift' || key === 'Alt') {
|
|
1299
|
1352
|
this.updateTransformSession(this.getPagePoint(info.point), info.shiftKey, info.altKey)
|
|
1300
|
1353
|
}
|
|
1301
|
1354
|
break
|
|
1302
|
1355
|
}
|
|
1303
|
|
- case 'translatingHandle': {
|
|
|
1356
|
+ case TLDrawStatus.TranslatingHandle: {
|
|
1304
|
1357
|
if (key === 'Escape') {
|
|
1305
|
1358
|
this.cancelSession(this.getPagePoint(info.point))
|
|
1306
|
1359
|
}
|
|
|
@@ -1320,7 +1373,7 @@ export class TLDrawState implements TLCallbacks {
|
|
1320
|
1373
|
|
|
1321
|
1374
|
/* ------------- Renderer Event Handlers ------------ */
|
|
1322
|
1375
|
onPinchStart: TLPinchEventHandler = () => {
|
|
1323
|
|
- this.setStatus('pinching')
|
|
|
1376
|
+ this.setStatus(TLDrawStatus.Pinching)
|
|
1324
|
1377
|
}
|
|
1325
|
1378
|
|
|
1326
|
1379
|
onPinchEnd: TLPinchEventHandler = () => {
|
|
|
@@ -1328,14 +1381,14 @@ export class TLDrawState implements TLCallbacks {
|
|
1328
|
1381
|
}
|
|
1329
|
1382
|
|
|
1330
|
1383
|
onPinch: TLPinchEventHandler = (info, e) => {
|
|
1331
|
|
- if (this.status.current !== 'pinching') return
|
|
|
1384
|
+ if (this.status.current !== TLDrawStatus.Pinching) return
|
|
1332
|
1385
|
|
|
1333
|
1386
|
this.pinchZoom(info.origin, info.delta, info.delta[2] / 350)
|
|
1334
|
|
- this.updatenPointerMove(info, e as any)
|
|
|
1387
|
+ this.updateOnPointerMove(info, e as any)
|
|
1335
|
1388
|
}
|
|
1336
|
1389
|
|
|
1337
|
1390
|
onPan: TLWheelEventHandler = (info, e) => {
|
|
1338
|
|
- if (this.status.current === 'pinching') return
|
|
|
1391
|
+ if (this.status.current === TLDrawStatus.Pinching) return
|
|
1339
|
1392
|
// TODO: Pan and pinchzoom are firing at the same time. Considering turning one of them off!
|
|
1340
|
1393
|
|
|
1341
|
1394
|
const delta = Vec.div(info.delta, this.getPageState().camera.zoom)
|
|
|
@@ -1345,36 +1398,32 @@ export class TLDrawState implements TLCallbacks {
|
|
1345
|
1398
|
if (Vec.isEqual(next, prev)) return
|
|
1346
|
1399
|
|
|
1347
|
1400
|
this.pan(delta)
|
|
1348
|
|
- this.updatenPointerMove(info, e as any)
|
|
|
1401
|
+ this.updateOnPointerMove(info, e as any)
|
|
1349
|
1402
|
}
|
|
1350
|
1403
|
|
|
1351
|
1404
|
onZoom: TLWheelEventHandler = (info, e) => {
|
|
1352
|
1405
|
this.zoom(info.delta[2] / 100)
|
|
1353
|
|
- this.updatenPointerMove(info, e as any)
|
|
|
1406
|
+ this.updateOnPointerMove(info, e as any)
|
|
1354
|
1407
|
}
|
|
1355
|
1408
|
|
|
1356
|
1409
|
// Pointer Events
|
|
1357
|
1410
|
onPointerDown: TLPointerEventHandler = (info) => {
|
|
1358
|
1411
|
switch (this.status.current) {
|
|
1359
|
|
- case 'idle': {
|
|
|
1412
|
+ case TLDrawStatus.Idle: {
|
|
1360
|
1413
|
switch (this.appState.activeTool) {
|
|
1361
|
1414
|
case 'draw': {
|
|
1362
|
|
- this.setStatus('creating')
|
|
1363
|
1415
|
this.createActiveToolShape(info.point)
|
|
1364
|
1416
|
break
|
|
1365
|
1417
|
}
|
|
1366
|
1418
|
case 'rectangle': {
|
|
1367
|
|
- this.setStatus('creating')
|
|
1368
|
1419
|
this.createActiveToolShape(info.point)
|
|
1369
|
1420
|
break
|
|
1370
|
1421
|
}
|
|
1371
|
1422
|
case 'ellipse': {
|
|
1372
|
|
- this.setStatus('creating')
|
|
1373
|
1423
|
this.createActiveToolShape(info.point)
|
|
1374
|
1424
|
break
|
|
1375
|
1425
|
}
|
|
1376
|
1426
|
case 'arrow': {
|
|
1377
|
|
- this.setStatus('creating')
|
|
1378
|
1427
|
this.createActiveToolShape(info.point)
|
|
1379
|
1428
|
break
|
|
1380
|
1429
|
}
|
|
|
@@ -1384,14 +1433,14 @@ export class TLDrawState implements TLCallbacks {
|
|
1384
|
1433
|
}
|
|
1385
|
1434
|
|
|
1386
|
1435
|
onPointerMove: TLPointerEventHandler = (info, e) => {
|
|
1387
|
|
- this.updatenPointerMove(info, e)
|
|
|
1436
|
+ this.updateOnPointerMove(info, e)
|
|
1388
|
1437
|
}
|
|
1389
|
1438
|
|
|
1390
|
1439
|
onPointerUp: TLPointerEventHandler = (info) => {
|
|
1391
|
1440
|
const data = this.getState()
|
|
1392
|
1441
|
|
|
1393
|
1442
|
switch (this.status.current) {
|
|
1394
|
|
- case 'pointingBounds': {
|
|
|
1443
|
+ case TLDrawStatus.PointingBounds: {
|
|
1395
|
1444
|
if (info.target === 'bounds') {
|
|
1396
|
1445
|
// If we just clicked the selecting bounds's background, clear the selection
|
|
1397
|
1446
|
this.deselectAll()
|
|
|
@@ -1412,41 +1461,41 @@ export class TLDrawState implements TLCallbacks {
|
|
1412
|
1461
|
this.pointedId = undefined
|
|
1413
|
1462
|
}
|
|
1414
|
1463
|
|
|
1415
|
|
- this.setStatus('idle')
|
|
|
1464
|
+ this.setStatus(TLDrawStatus.Idle)
|
|
1416
|
1465
|
this.pointedId = undefined
|
|
1417
|
1466
|
break
|
|
1418
|
1467
|
}
|
|
1419
|
|
- case 'pointingBoundsHandle': {
|
|
1420
|
|
- this.setStatus('idle')
|
|
|
1468
|
+ case TLDrawStatus.PointingBoundsHandle: {
|
|
|
1469
|
+ this.setStatus(TLDrawStatus.Idle)
|
|
1421
|
1470
|
this.pointedBoundsHandle = undefined
|
|
1422
|
1471
|
break
|
|
1423
|
1472
|
}
|
|
1424
|
|
- case 'pointingHandle': {
|
|
1425
|
|
- this.setStatus('idle')
|
|
|
1473
|
+ case TLDrawStatus.PointingHandle: {
|
|
|
1474
|
+ this.setStatus(TLDrawStatus.Idle)
|
|
1426
|
1475
|
this.pointedHandle = undefined
|
|
1427
|
1476
|
break
|
|
1428
|
1477
|
}
|
|
1429
|
|
- case 'translatingHandle': {
|
|
|
1478
|
+ case TLDrawStatus.TranslatingHandle: {
|
|
1430
|
1479
|
this.completeSession<Sessions.HandleSession>()
|
|
1431
|
1480
|
this.pointedHandle = undefined
|
|
1432
|
1481
|
break
|
|
1433
|
1482
|
}
|
|
1434
|
|
- case 'brushing': {
|
|
|
1483
|
+ case TLDrawStatus.Brushing: {
|
|
1435
|
1484
|
this.completeSession<Sessions.BrushSession>()
|
|
1436
|
1485
|
brushUpdater.clear()
|
|
1437
|
1486
|
break
|
|
1438
|
1487
|
}
|
|
1439
|
|
- case 'translating': {
|
|
1440
|
|
- this.completeSession(this.getPagePoint(info.point))
|
|
|
1488
|
+ case TLDrawStatus.Translating: {
|
|
|
1489
|
+ this.completeSession<Sessions.TranslateSession>()
|
|
1441
|
1490
|
this.pointedId = undefined
|
|
1442
|
1491
|
break
|
|
1443
|
1492
|
}
|
|
1444
|
|
- case 'transforming': {
|
|
1445
|
|
- this.completeSession(this.getPagePoint(info.point))
|
|
|
1493
|
+ case TLDrawStatus.Transforming: {
|
|
|
1494
|
+ this.completeSession<Sessions.TransformSession>()
|
|
1446
|
1495
|
this.pointedBoundsHandle = undefined
|
|
1447
|
1496
|
break
|
|
1448
|
1497
|
}
|
|
1449
|
|
- case 'creating': {
|
|
|
1498
|
+ case TLDrawStatus.Creating: {
|
|
1450
|
1499
|
this.completeSession(this.getPagePoint(info.point))
|
|
1451
|
1500
|
this.pointedHandle = undefined
|
|
1452
|
1501
|
}
|
|
|
@@ -1485,7 +1534,6 @@ export class TLDrawState implements TLCallbacks {
|
|
1485
|
1534
|
switch (this.appState.activeTool) {
|
|
1486
|
1535
|
case 'text': {
|
|
1487
|
1536
|
// Create a text shape
|
|
1488
|
|
- this.setStatus('creating')
|
|
1489
|
1537
|
this.createActiveToolShape(info.point)
|
|
1490
|
1538
|
break
|
|
1491
|
1539
|
}
|
|
|
@@ -1530,7 +1578,7 @@ export class TLDrawState implements TLCallbacks {
|
|
1530
|
1578
|
this.setSelectedIds([info.target], info.shiftKey)
|
|
1531
|
1579
|
}
|
|
1532
|
1580
|
|
|
1533
|
|
- this.setStatus('pointingBounds')
|
|
|
1581
|
+ this.setStatus(TLDrawStatus.PointingBounds)
|
|
1534
|
1582
|
break
|
|
1535
|
1583
|
}
|
|
1536
|
1584
|
}
|
|
|
@@ -1581,7 +1629,7 @@ export class TLDrawState implements TLCallbacks {
|
|
1581
|
1629
|
|
|
1582
|
1630
|
// Bounds (bounding box background)
|
|
1583
|
1631
|
onPointBounds: TLBoundsEventHandler = () => {
|
|
1584
|
|
- this.setStatus('pointingBounds')
|
|
|
1632
|
+ this.setStatus(TLDrawStatus.PointingBounds)
|
|
1585
|
1633
|
}
|
|
1586
|
1634
|
|
|
1587
|
1635
|
onDoubleClickBounds: TLBoundsEventHandler = () => {
|
|
|
@@ -1606,11 +1654,11 @@ export class TLDrawState implements TLCallbacks {
|
|
1606
|
1654
|
|
|
1607
|
1655
|
onReleaseBounds: TLBoundsEventHandler = (info) => {
|
|
1608
|
1656
|
switch (this.status.current) {
|
|
1609
|
|
- case 'translating': {
|
|
|
1657
|
+ case TLDrawStatus.Translating: {
|
|
1610
|
1658
|
this.completeSession(this.getPagePoint(info.point))
|
|
1611
|
1659
|
break
|
|
1612
|
1660
|
}
|
|
1613
|
|
- case 'brushing': {
|
|
|
1661
|
+ case TLDrawStatus.Brushing: {
|
|
1614
|
1662
|
this.completeSession<Sessions.BrushSession>()
|
|
1615
|
1663
|
brushUpdater.clear()
|
|
1616
|
1664
|
break
|
|
|
@@ -1621,7 +1669,7 @@ export class TLDrawState implements TLCallbacks {
|
|
1621
|
1669
|
// Bounds handles (corners, edges)
|
|
1622
|
1670
|
onPointBoundsHandle: TLBoundsHandleEventHandler = (info) => {
|
|
1623
|
1671
|
this.pointedBoundsHandle = info.target
|
|
1624
|
|
- this.setStatus('pointingBoundsHandle')
|
|
|
1672
|
+ this.setStatus(TLDrawStatus.PointingBoundsHandle)
|
|
1625
|
1673
|
}
|
|
1626
|
1674
|
|
|
1627
|
1675
|
onDoubleClickBoundsHandle: TLBoundsHandleEventHandler = () => {
|
|
|
@@ -1651,7 +1699,7 @@ export class TLDrawState implements TLCallbacks {
|
|
1651
|
1699
|
// Handles (ie the handles of a selected arrow)
|
|
1652
|
1700
|
onPointHandle: TLPointerEventHandler = (info) => {
|
|
1653
|
1701
|
this.pointedHandle = info.target
|
|
1654
|
|
- this.setStatus('pointingHandle')
|
|
|
1702
|
+ this.setStatus(TLDrawStatus.PointingHandle)
|
|
1655
|
1703
|
}
|
|
1656
|
1704
|
|
|
1657
|
1705
|
onDoubleClickHandle: TLPointerEventHandler = (info) => {
|
|
|
@@ -1766,4 +1814,8 @@ export class TLDrawState implements TLCallbacks {
|
|
1766
|
1814
|
get appState() {
|
|
1767
|
1815
|
return this.data.appState
|
|
1768
|
1816
|
}
|
|
|
1817
|
+
|
|
|
1818
|
+ get status() {
|
|
|
1819
|
+ return this.appState.status
|
|
|
1820
|
+ }
|
|
1769
|
1821
|
}
|