|
|
@@ -67,7 +67,7 @@ const defaultDocument: TLDrawDocument = {
|
|
67
|
67
|
},
|
|
68
|
68
|
}
|
|
69
|
69
|
|
|
70
|
|
-const initialData: Data = {
|
|
|
70
|
+const defaultState: Data = {
|
|
71
|
71
|
settings: {
|
|
72
|
72
|
isPenMode: false,
|
|
73
|
73
|
isDarkMode: false,
|
|
|
@@ -120,11 +120,11 @@ export class TLDrawState extends StateManager<Data> {
|
|
120
|
120
|
selectedGroupId?: string
|
|
121
|
121
|
|
|
122
|
122
|
constructor(
|
|
123
|
|
- id = Utils.uniqueId(),
|
|
|
123
|
+ id?: string,
|
|
124
|
124
|
onChange?: (tlstate: TLDrawState, data: Data, reason: string) => void,
|
|
125
|
125
|
onMount?: (tlstate: TLDrawState) => void
|
|
126
|
126
|
) {
|
|
127
|
|
- super(initialData, id, 2, (prev, next, prevVersion) => {
|
|
|
127
|
+ super(defaultState, id, 2, (prev, next, prevVersion) => {
|
|
128
|
128
|
const state = { ...prev }
|
|
129
|
129
|
if (prevVersion === 1)
|
|
130
|
130
|
state.settings = {
|
|
|
@@ -478,56 +478,79 @@ export class TLDrawState extends StateManager<Data> {
|
|
478
|
478
|
}
|
|
479
|
479
|
|
|
480
|
480
|
/**
|
|
481
|
|
- * Load a new document.
|
|
482
|
|
- * @param document The document to load
|
|
|
481
|
+ * Update the current document.
|
|
|
482
|
+ * @param document
|
|
483
|
483
|
*/
|
|
484
|
|
- loadDocument = (document: TLDrawDocument): this => {
|
|
485
|
|
- this.deselectAll()
|
|
486
|
|
- this.resetHistory()
|
|
487
|
|
- this.clearSelectHistory()
|
|
488
|
|
- this.session = undefined
|
|
489
|
|
- this.selectedGroupId = undefined
|
|
|
484
|
+ updateDocument = (document: TLDrawDocument, reason = 'updated_document'): this => {
|
|
|
485
|
+ console.log(reason)
|
|
|
486
|
+
|
|
|
487
|
+ const state = this.state
|
|
490
|
488
|
|
|
491
|
|
- return this.replaceState(
|
|
|
489
|
+ const currentPageId = document.pages[this.currentPageId]
|
|
|
490
|
+ ? this.currentPageId
|
|
|
491
|
+ : Object.keys(document.pages)[0]
|
|
|
492
|
+
|
|
|
493
|
+ this.replaceState(
|
|
492
|
494
|
{
|
|
493
|
495
|
...this.state,
|
|
494
|
496
|
appState: {
|
|
495
|
497
|
...this.appState,
|
|
496
|
|
- currentPageId: Object.keys(document.pages)[0],
|
|
|
498
|
+ currentPageId,
|
|
497
|
499
|
},
|
|
498
|
500
|
document: {
|
|
499
|
501
|
...document,
|
|
500
|
502
|
pages: Object.fromEntries(
|
|
501
|
503
|
Object.entries(document.pages)
|
|
502
|
504
|
.sort((a, b) => (a[1].childIndex || 0) - (b[1].childIndex || 0))
|
|
503
|
|
- .map(([id, page], i) => {
|
|
504
|
|
- return [
|
|
505
|
|
- id,
|
|
506
|
|
- {
|
|
507
|
|
- ...page,
|
|
508
|
|
- name: page.name ? page.name : `Page ${i++}`,
|
|
509
|
|
- },
|
|
510
|
|
- ]
|
|
|
505
|
+ .map(([pageId, page], i) => {
|
|
|
506
|
+ const nextPage = { ...page }
|
|
|
507
|
+ if (!nextPage.name) nextPage.name = `Page ${i + 1}`
|
|
|
508
|
+ return [pageId, nextPage]
|
|
511
|
509
|
})
|
|
512
|
510
|
),
|
|
513
|
511
|
pageStates: Object.fromEntries(
|
|
514
|
|
- Object.entries(document.pageStates).map(([id, pageState]) => {
|
|
515
|
|
- return [
|
|
516
|
|
- id,
|
|
517
|
|
- {
|
|
518
|
|
- ...pageState,
|
|
519
|
|
- bindingId: undefined,
|
|
520
|
|
- editingId: undefined,
|
|
521
|
|
- hoveredId: undefined,
|
|
522
|
|
- pointedId: undefined,
|
|
523
|
|
- },
|
|
524
|
|
- ]
|
|
|
512
|
+ Object.entries(document.pageStates).map(([pageId, pageState]) => {
|
|
|
513
|
+ const page = document.pages[pageId]
|
|
|
514
|
+ const nextPageState = { ...pageState }
|
|
|
515
|
+ const keysToCheck = ['bindingId', 'editingId', 'hoveredId', 'pointedId'] as const
|
|
|
516
|
+
|
|
|
517
|
+ for (const key of keysToCheck) {
|
|
|
518
|
+ if (!page.shapes[key]) {
|
|
|
519
|
+ nextPageState[key] = undefined
|
|
|
520
|
+ }
|
|
|
521
|
+ }
|
|
|
522
|
+
|
|
|
523
|
+ nextPageState.selectedIds = pageState.selectedIds.filter(
|
|
|
524
|
+ (id) => !!document.pages[pageId].shapes[id]
|
|
|
525
|
+ )
|
|
|
526
|
+
|
|
|
527
|
+ return [pageId, nextPageState]
|
|
525
|
528
|
})
|
|
526
|
529
|
),
|
|
527
|
530
|
},
|
|
528
|
531
|
},
|
|
529
|
|
- `loaded_document:${document.id}`
|
|
|
532
|
+ `${reason}:${document.id}`
|
|
530
|
533
|
)
|
|
|
534
|
+
|
|
|
535
|
+ console.log(
|
|
|
536
|
+ 'did page change?',
|
|
|
537
|
+ this.state.document.pages['page1'] !== state.document.pages['page1']
|
|
|
538
|
+ )
|
|
|
539
|
+
|
|
|
540
|
+ return this
|
|
|
541
|
+ }
|
|
|
542
|
+
|
|
|
543
|
+ /**
|
|
|
544
|
+ * Load a new document.
|
|
|
545
|
+ * @param document The document to load
|
|
|
546
|
+ */
|
|
|
547
|
+ loadDocument = (document: TLDrawDocument): this => {
|
|
|
548
|
+ this.deselectAll()
|
|
|
549
|
+ this.resetHistory()
|
|
|
550
|
+ this.clearSelectHistory()
|
|
|
551
|
+ this.session = undefined
|
|
|
552
|
+ this.selectedGroupId = undefined
|
|
|
553
|
+ return this.updateDocument(document, 'loaded_document')
|
|
531
|
554
|
}
|
|
532
|
555
|
|
|
533
|
556
|
/**
|