瀏覽代碼

Fix bad state

main
Steve Ruiz 4 年之前
父節點
當前提交
e38b9cd600
共有 2 個檔案被更改,包括 35 行新增22 行删除
  1. 0
    1
      packages/core/src/components/shape/shape.tsx
  2. 35
    21
      packages/tldraw/src/state/tlstate.ts

+ 0
- 1
packages/core/src/components/shape/shape.tsx 查看文件

@@ -29,7 +29,6 @@ export const Shape = React.memo(
29 29
         transform={transform}
30 30
         {...events}
31 31
       >
32
-        <text>{shape.childIndex}</text>
33 32
         {isEditing && utils.isEditableText ? (
34 33
           <EditingTextShape
35 34
             shape={shape}

+ 35
- 21
packages/tldraw/src/state/tlstate.ts 查看文件

@@ -128,25 +128,6 @@ export class TLDrawState extends StateManager<Data> {
128 128
 
129 129
     this.session = undefined
130 130
     this.pointedId = undefined
131
-
132
-    this.patchState({
133
-      appState: {
134
-        status: {
135
-          current: TLDrawStatus.Idle,
136
-          previous: TLDrawStatus.Idle,
137
-        },
138
-      },
139
-      document: {
140
-        pageStates: {
141
-          [this.currentPageId]: {
142
-            bindingId: undefined,
143
-            editingId: undefined,
144
-            hoveredId: undefined,
145
-            pointedId: undefined,
146
-          },
147
-        },
148
-      },
149
-    })
150 131
   }
151 132
   /* -------------------- Internal -------------------- */
152 133
 
@@ -243,6 +224,7 @@ export class TLDrawState extends StateManager<Data> {
243 224
           })
244 225
 
245 226
           groupsToUpdate.forEach((group) => {
227
+            if (!group) throw Error('no group!')
246 228
             const children = group.children.filter((id) => page.shapes[id] !== undefined)
247 229
 
248 230
             const commonBounds = Utils.getCommonBounds(
@@ -341,6 +323,7 @@ export class TLDrawState extends StateManager<Data> {
341 323
    * @returns this
342 324
    */
343 325
   togglePenMode = (): this => {
326
+    if (this.session) return this
344 327
     return this.patchState(
345 328
       {
346 329
         settings: {
@@ -356,6 +339,7 @@ export class TLDrawState extends StateManager<Data> {
356 339
    * @returns this
357 340
    */
358 341
   toggleDarkMode = (): this => {
342
+    if (this.session) return this
359 343
     this.patchState(
360 344
       { settings: { isDarkMode: !this.state.settings.isDarkMode } },
361 345
       `settings:toggled_dark_mode`
@@ -369,6 +353,7 @@ export class TLDrawState extends StateManager<Data> {
369 353
    * @returns this
370 354
    */
371 355
   toggleZoomSnap = () => {
356
+    if (this.session) return this
372 357
     this.patchState(
373 358
       { settings: { isZoomSnap: !this.state.settings.isZoomSnap } },
374 359
       `settings:toggled_zoom_snap`
@@ -382,6 +367,7 @@ export class TLDrawState extends StateManager<Data> {
382 367
    * @returns this
383 368
    */
384 369
   toggleDebugMode = () => {
370
+    if (this.session) return this
385 371
     this.patchState(
386 372
       { settings: { isDebugMode: !this.state.settings.isDebugMode } },
387 373
       `settings:toggled_debug`
@@ -395,6 +381,7 @@ export class TLDrawState extends StateManager<Data> {
395 381
    * @returns this
396 382
    */
397 383
   toggleStylePanel = (): this => {
384
+    if (this.session) return this
398 385
     this.patchState(
399 386
       { appState: { isStyleOpen: !this.appState.isStyleOpen } },
400 387
       'ui:toggled_style_panel'
@@ -409,6 +396,7 @@ export class TLDrawState extends StateManager<Data> {
409 396
    * @returns this
410 397
    */
411 398
   selectTool = (tool: TLDrawShapeType | 'select'): this => {
399
+    if (this.session) return this
412 400
     return this.patchState(
413 401
       {
414 402
         appState: {
@@ -428,6 +416,7 @@ export class TLDrawState extends StateManager<Data> {
428 416
    * @returns this
429 417
    */
430 418
   toggleToolLock = (): this => {
419
+    if (this.session) return this
431 420
     return this.patchState(
432 421
       {
433 422
         appState: {
@@ -443,8 +432,31 @@ export class TLDrawState extends StateManager<Data> {
443 432
   /* -------------------------------------------------- */
444 433
 
445 434
   resetDocument = (): this => {
446
-    this.loadDocument(defaultDocument)
447
-    this.persist()
435
+    if (this.session) return this
436
+    this.session = undefined
437
+    this.selectedGroupId = undefined
438
+    this.resetHistory()
439
+      .clearSelectHistory()
440
+      .loadDocument(defaultDocument)
441
+      .patchState({
442
+        appState: {
443
+          status: {
444
+            current: TLDrawStatus.Idle,
445
+            previous: TLDrawStatus.Idle,
446
+          },
447
+        },
448
+        document: {
449
+          pageStates: {
450
+            [this.currentPageId]: {
451
+              bindingId: undefined,
452
+              editingId: undefined,
453
+              hoveredId: undefined,
454
+              pointedId: undefined,
455
+            },
456
+          },
457
+        },
458
+      })
459
+      .persist()
448 460
     return this
449 461
   }
450 462
 
@@ -459,6 +471,8 @@ export class TLDrawState extends StateManager<Data> {
459 471
     this.deselectAll()
460 472
     this.resetHistory()
461 473
     this.clearSelectHistory()
474
+    this.session = undefined
475
+    this.selectedGroupId = undefined
462 476
 
463 477
     return this.replaceState({
464 478
       ...this.state,

Loading…
取消
儲存