From 10935eb271443bd390d6b27b616bfcfef4ad01b2 Mon Sep 17 00:00:00 2001 From: bobzel Date: Sun, 25 Oct 2020 11:03:39 -0400 Subject: fixed updating pres.box instance and selectedArray to trigger mobx updates properluy --- src/client/views/PropertiesView.tsx | 6 +-- src/client/views/nodes/PresBox.tsx | 45 ++++++++++++---------- .../views/presentationview/PresElementBox.tsx | 12 +++--- 3 files changed, 34 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/client/views/PropertiesView.tsx b/src/client/views/PropertiesView.tsx index e93173c9e..36c4648cf 100644 --- a/src/client/views/PropertiesView.tsx +++ b/src/client/views/PropertiesView.tsx @@ -46,7 +46,7 @@ export class PropertiesView extends React.Component { @computed get selectedDoc() { return SelectionManager.SelectedSchemaDoc() || this.selectedDocumentView?.rootDoc; } @computed get selectedDocumentView() { if (SelectionManager.SelectedDocuments().length) return SelectionManager.SelectedDocuments()[0]; - if (PresBox.Instance && PresBox.Instance._selectedArray) return DocumentManager.Instance.getDocumentView(PresBox.Instance.rootDoc); + if (PresBox.Instance?._selectedArray) return DocumentManager.Instance.getDocumentView(PresBox.Instance.rootDoc); return undefined; } @computed get isPres(): boolean { @@ -982,7 +982,7 @@ export class PropertiesView extends React.Component { ; } if (this.isPres) { - const selectedItem: boolean = PresBox.Instance?._selectedArray.length > 0; + const selectedItem: boolean = PresBox.Instance?._selectedArray.size > 0; const type = PresBox.Instance.activeItem?.type; return
@@ -991,7 +991,7 @@ export class PropertiesView extends React.Component {
{this.editableTitle}
- {PresBox.Instance?._selectedArray.length} selected + {PresBox.Instance?._selectedArray.size} selected
{PresBox.Instance?.listOfSelected}
diff --git a/src/client/views/nodes/PresBox.tsx b/src/client/views/nodes/PresBox.tsx index e46e68e85..0f686c7bc 100644 --- a/src/client/views/nodes/PresBox.tsx +++ b/src/client/views/nodes/PresBox.tsx @@ -1,7 +1,7 @@ import React = require("react"); import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { Tooltip } from "@material-ui/core"; -import { action, computed, observable, runInAction } from "mobx"; +import { action, computed, observable, runInAction, ObservableMap } from "mobx"; import { observer } from "mobx-react"; import { ColorState, SketchPicker } from "react-color"; import { Doc, DocCastAsync, DocListCast, DocListCastAsync } from "../../../fields/Doc"; @@ -63,14 +63,14 @@ const PresBoxDocument = makeInterface(documentSchema); export class PresBox extends ViewBoxBaseComponent(PresBoxDocument) { public static LayoutString(fieldKey: string) { return FieldView.LayoutString(PresBox, fieldKey); } - public static Instance: PresBox; + @observable public static Instance: PresBox; @observable _isChildActive = false; @observable _moveOnFromAudio: boolean = true; @observable _presTimer!: NodeJS.Timeout; @observable _presKeyEventsActive: boolean = false; - @observable _selectedArray: Doc[] = []; + @observable _selectedArray: ObservableMap = new ObservableMap(); @observable _eleArray: HTMLElement[] = []; @observable _dragArray: HTMLElement[] = []; @observable _pathBoolean: boolean = false; @@ -97,7 +97,7 @@ export class PresBox extends ViewBoxBaseComponent @computed get presElement() { return Cast(Doc.UserDoc().presElement, Doc, null); } constructor(props: any) { super(props); - if (Doc.UserDoc().activePresentation = this.rootDoc) PresBox.Instance = this; + if (Doc.UserDoc().activePresentation = this.rootDoc) runInAction(() => PresBox.Instance = this); if (!this.presElement) { // create exactly one presElmentBox template to use by any and all presentations. Doc.UserDoc().presElement = new PrefetchProxy(Docs.Create.PresElementBoxDocument({ title: "pres element template", backgroundColor: "transparent", _xMargin: 0, isTemplateDoc: true, isTemplateForField: "data" @@ -149,6 +149,7 @@ export class PresBox extends ViewBoxBaseComponent !pres?.includes(this.rootDoc) && Doc.AddDocToList(Doc.UserDoc().myPresentations as Doc, "data", this.rootDoc)); } + @action updateCurrentPresentation = () => { Doc.UserDoc().activePresentation = this.rootDoc; document.addEventListener("keydown", this.keyEvents, true); @@ -260,7 +261,8 @@ export class PresBox extends ViewBoxBaseComponent if (presTargetDoc?.lastFrame !== undefined) { presTargetDoc._currentFrame = 0; } - this._selectedArray.splice(0, this._selectedArray.length, this.childDocs[index]); //Update selected array + this._selectedArray.clear(); + this.childDocs[index] && this._selectedArray.set(this.childDocs[index], undefined); //Update selected array //Handles movement to element if (this.layoutDoc._viewType === "stacking") this.navigateToElement(this.childDocs[index]); this.onHideDocument(); //Handles hide after/before @@ -297,7 +299,7 @@ export class PresBox extends ViewBoxBaseComponent const docToJump = curDoc; const willZoom = false; const presStatus = this.rootDoc.presStatus; - const selViewCache = Array.from(this._selectedArray); + const selViewCache = Array.from(this._selectedArray.keys()); const dragViewCache = Array.from(this._dragArray); const eleViewCache = Array.from(this._eleArray); const self = this; @@ -305,7 +307,8 @@ export class PresBox extends ViewBoxBaseComponent const presDocView = DocumentManager.Instance.getDocumentView(self.rootDoc); if (presDocView) SelectionManager.SelectDoc(presDocView, false); self.rootDoc.presStatus = presStatus; - self._selectedArray.splice(0, self._selectedArray.length, ...selViewCache); + self._selectedArray.clear(); + selViewCache.forEach(doc => self._selectedArray.set(doc, undefined)); self._dragArray.splice(0, self._dragArray.length, ...dragViewCache); self._eleArray.splice(0, self._eleArray.length, ...eleViewCache); }); @@ -621,14 +624,14 @@ export class PresBox extends ViewBoxBaseComponent */ @action sortArray = (): Doc[] => { - return this.childDocs.filter(doc => this._selectedArray.includes(doc)); + return this.childDocs.filter(doc => this._selectedArray.has(doc)); } /** * Method to get the list of selected items in the order in which they have been selected */ @computed get listOfSelected() { - const list = this._selectedArray.map((doc: Doc, index: any) => { + const list = Array.from(this._selectedArray.keys()).map((doc: Doc, index: any) => { const curDoc = Cast(doc, Doc, null); const tagDoc = Cast(curDoc.presentationTargetDoc!, Doc, null); if (tagDoc) return
{index + 1}. {curDoc.title}
; @@ -654,8 +657,8 @@ export class PresBox extends ViewBoxBaseComponent //Command click @action multiSelect = (doc: Doc, ref: HTMLElement, drag: HTMLElement) => { - if (!this._selectedArray.includes(doc)) { - this._selectedArray.push(doc); + if (!this._selectedArray.has(doc)) { + this._selectedArray.set(doc, undefined); this._eleArray.push(ref); this._dragArray.push(drag); } @@ -665,11 +668,11 @@ export class PresBox extends ViewBoxBaseComponent //Shift click @action shiftSelect = (doc: Doc, ref: HTMLElement, drag: HTMLElement) => { - this._selectedArray.length = 0; + this._selectedArray.clear(); // const activeItem = Cast(this.childDocs[this.itemIndex], Doc, null); if (this.activeItem) { for (let i = Math.min(this.itemIndex, this.childDocs.indexOf(doc)); i <= Math.max(this.itemIndex, this.childDocs.indexOf(doc)); i++) { - this._selectedArray.push(this.childDocs[i]); + this._selectedArray.set(this.childDocs[i], undefined); this._eleArray.push(ref); this._dragArray.push(drag); } @@ -680,7 +683,8 @@ export class PresBox extends ViewBoxBaseComponent //regular click @action regularSelect = (doc: Doc, ref: HTMLElement, drag: HTMLElement, focus: boolean) => { - this._selectedArray.splice(0, this._selectedArray.length, doc); + this._selectedArray.clear(); + this._selectedArray.set(doc, undefined); this._eleArray.splice(0, this._eleArray.length, ref); this._dragArray.splice(0, this._dragArray.length, drag); focus && this.selectElement(doc); @@ -704,10 +708,10 @@ export class PresBox extends ViewBoxBaseComponent case "Backspace": if (this.layoutDoc.presStatus === "edit") { undoBatch(action(() => { - for (const doc of this._selectedArray) { + for (const doc of Array.from(this._selectedArray.keys())) { this.removeDocument(doc); } - this._selectedArray.length = 0; + this._selectedArray.clear(); this._eleArray.length = 0; this._dragArray.length = 0; }))(); @@ -716,7 +720,7 @@ export class PresBox extends ViewBoxBaseComponent break; case "Escape": if (CurrentUserUtils.OverlayDocs.includes(this.layoutDoc)) { this.updateMinimize(); } - else if (this.layoutDoc.presStatus === "edit") { this._selectedArray.length = this._eleArray.length = this._dragArray.length = 0; } + else if (this.layoutDoc.presStatus === "edit") { this._selectedArray.clear(); this._eleArray.length = this._dragArray.length = 0; } else this.layoutDoc.presStatus = "edit"; if (this._presTimer) clearTimeout(this._presTimer); handled = true; @@ -725,7 +729,7 @@ export class PresBox extends ViewBoxBaseComponent case "Right": case "ArrowRight": if (e.shiftKey) { // TODO: update to work properly this.rootDoc._itemIndex = NumCast(this.rootDoc._itemIndex) + 1; - this._selectedArray.push(this.childDocs[this.rootDoc._itemIndex]); + this._selectedArray.set(this.childDocs[this.rootDoc._itemIndex], undefined); } else { this.next(); if (this._presTimer) clearTimeout(this._presTimer); @@ -736,7 +740,7 @@ export class PresBox extends ViewBoxBaseComponent case "Left": case "ArrowLeft": if (e.shiftKey) { // TODO: update to work properly this.rootDoc._itemIndex = NumCast(this.rootDoc._itemIndex) - 1; - this._selectedArray.push(this.childDocs[this.rootDoc._itemIndex]); + this._selectedArray.set(this.childDocs[this.rootDoc._itemIndex], undefined); } else { this.back(); if (this._presTimer) clearTimeout(this._presTimer); @@ -750,7 +754,8 @@ export class PresBox extends ViewBoxBaseComponent break; case "a": if ((e.metaKey || e.altKey) && this.layoutDoc.presStatus === "edit") { - this._selectedArray.splice(0, this._selectedArray.length, ...this.childDocs); + this._selectedArray.clear(); + this.childDocs.forEach(doc => this._selectedArray.set(doc, undefined)); handled = true; } default: diff --git a/src/client/views/presentationview/PresElementBox.tsx b/src/client/views/presentationview/PresElementBox.tsx index 0dc8a2148..2d7a768cc 100644 --- a/src/client/views/presentationview/PresElementBox.tsx +++ b/src/client/views/presentationview/PresElementBox.tsx @@ -141,8 +141,8 @@ export class PresElementBox extends ViewBoxBaseComponent { @@ -171,7 +171,7 @@ export class PresElementBox extends ViewBoxBaseComponent= 1) { const doc = document.createElement('div'); doc.className = "presItem-multiDrag"; - doc.innerText = "Move " + PresBox.Instance._selectedArray.length + " slides"; + doc.innerText = "Move " + PresBox.Instance._selectedArray.size + " slides"; doc.style.position = 'absolute'; doc.style.top = (e.clientY) + 'px'; doc.style.left = (e.clientX - 50) + 'px'; @@ -226,8 +226,8 @@ export class PresElementBox extends ViewBoxBaseComponent { this.props.removeDocument?.(this.rootDoc); - if (PresBox.Instance._selectedArray.includes(this.rootDoc)) { - PresBox.Instance._selectedArray.splice(PresBox.Instance._selectedArray.indexOf(this.rootDoc), 1); + if (PresBox.Instance._selectedArray.has(this.rootDoc)) { + PresBox.Instance._selectedArray.delete(this.rootDoc); } e.stopPropagation(); }); @@ -275,7 +275,7 @@ export class PresElementBox extends ViewBoxBaseComponent= 300; const miniView: boolean = this.toolbarWidth <= 100; -- cgit v1.2.3-70-g09d2