/* eslint-disable react/no-unused-class-component-methods */ /* eslint-disable react/no-array-index-key */ import { action, computed, makeObservable, observable } from 'mobx'; import { observer } from 'mobx-react'; import * as React from 'react'; import { Doc } from '../../fields/Doc'; import { Id } from '../../fields/FieldSymbols'; import { OverlayView } from '../views/OverlayView'; import { DocumentView } from '../views/nodes/DocumentView'; import { PresBox } from '../views/nodes/trails'; @observer export class BranchingTrailManager extends React.Component { // eslint-disable-next-line no-use-before-define public static Instance: BranchingTrailManager; // stack of the history @observable private slideHistoryStack: string[] = []; @observable private containsSet: Set = new Set(); // docId to Doc map @observable private docIdToDocMap: Map = new Map(); // prev pres to copmare with @observable private prevPresId: string | null = null; @action setPrevPres = action((newId: string | null) => { this.prevPresId = newId; }); constructor(props: object) { super(props); makeObservable(this); if (!BranchingTrailManager.Instance) { BranchingTrailManager.Instance = this; } } setupUi = () => { OverlayView.Instance.addWindow(, { x: 100, y: 150, width: 1000, title: 'Branching Trail' }); // OverlayView.Instance.forceUpdate(); console.log(OverlayView.Instance); // let hi = Docs.Create.TextDocument("beee", { // x: 100, // y: 100, // }) // hi.overlayX = 100; // hi.overlayY = 100; // Doc.AddToMyOverlay(hi); }; @action setSlideHistoryStack = action((newArr: string[]) => { this.slideHistoryStack = newArr; }); // eslint-disable-next-line react/sort-comp observeDocumentChange = (targetDoc: Doc, pres: PresBox) => { const presId = pres.Document[Id]; if (this.prevPresId === presId) { return; } const targetDocId = targetDoc[Id]; this.docIdToDocMap.set(targetDocId, targetDoc); if (this.prevPresId === null) { this.setupUi(); } if (this.prevPresId === null || this.prevPresId !== presId) { Doc.UserDoc().isBranchingMode = true; this.setPrevPres(presId); // REVERT THE SET const stringified = [presId, targetDocId].toString(); if (this.containsSet.has([presId, targetDocId].toString())) { // remove all the elements after the targetDocId const newStack = this.slideHistoryStack.slice(0, this.slideHistoryStack.indexOf(stringified)); const removed = this.slideHistoryStack.slice(this.slideHistoryStack.indexOf(stringified)); this.setSlideHistoryStack(newStack); removed.forEach(info => this.containsSet.delete(info.toString())); } else { this.setSlideHistoryStack([...this.slideHistoryStack, stringified]); this.containsSet.add(stringified); } } console.log(this.slideHistoryStack.length); if (this.slideHistoryStack.length === 0) { Doc.UserDoc().isBranchingMode = false; } }; clickHandler = (e: React.PointerEvent, targetDocId: string, removeIndex: number) => { const targetDoc = this.docIdToDocMap.get(targetDocId); if (!targetDoc) { return; } const newStack = this.slideHistoryStack.slice(0, removeIndex); const removed = this.slideHistoryStack.slice(removeIndex); this.setSlideHistoryStack(newStack); removed.forEach(info => this.containsSet.delete(info.toString())); DocumentView.showDocument(targetDoc, { willZoomCentered: true }); if (this.slideHistoryStack.length === 0) { Doc.UserDoc().isBranchingMode = false; } // PresBox.NavigateToTarget(targetDoc, targetDoc); }; @computed get trailBreadcrumbs() { return (
{this.slideHistoryStack.map((info, index) => { const [presId, targetDocId] = info.split(','); const doc = this.docIdToDocMap.get(targetDocId); if (!doc) { return null; } return ( -{'>'} ); })}
); } render() { return
{BranchingTrailManager.Instance.trailBreadcrumbs}
; } }