From 4574b7f03ccc85c4bebdbfd9475788456086704f Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Thu, 8 Aug 2024 12:27:40 -0400
Subject: many changes to add typing in place of 'any's etc

---
 .../views/collections/CollectionDockingView.tsx    | 68 +++++++++++-----------
 1 file changed, 33 insertions(+), 35 deletions(-)

(limited to 'src/client/views/collections/CollectionDockingView.tsx')

diff --git a/src/client/views/collections/CollectionDockingView.tsx b/src/client/views/collections/CollectionDockingView.tsx
index 5a142cc6e..e0aa79c7b 100644
--- a/src/client/views/collections/CollectionDockingView.tsx
+++ b/src/client/views/collections/CollectionDockingView.tsx
@@ -1,14 +1,15 @@
-/* eslint-disable @typescript-eslint/no-explicit-any */
 import { action, IReactionDisposer, makeObservable, observable, reaction } from 'mobx';
 import { observer } from 'mobx-react';
 import * as React from 'react';
 import * as ReactDOM from 'react-dom/client';
+import ResizeObserver from 'resize-observer-polyfill';
 import { addStyleSheet, addStyleSheetRule, clearStyleSheetRules, DivHeight, DivWidth, incrementTitleCopy, returnTrue, UpdateIcon } from '../../../ClientUtils';
 import { Doc, DocListCast, Field, Opt } from '../../../fields/Doc';
 import { AclAdmin, AclEdit, DocData } from '../../../fields/DocSymbols';
 import { Id } from '../../../fields/FieldSymbols';
 import { InkTool } from '../../../fields/InkField';
 import { List } from '../../../fields/List';
+import { FieldType } from '../../../fields/ObjectField';
 import { ImageCast, NumCast, StrCast } from '../../../fields/Types';
 import { ImageField } from '../../../fields/URLField';
 import { GetEffectiveAcl, inheritParentAcls, SetPropSetterCb } from '../../../fields/util';
@@ -31,17 +32,15 @@ import { UndoStack } from '../UndoStack';
 import './CollectionDockingView.scss';
 import { CollectionSubView, SubCollectionViewProps } from './CollectionSubView';
 
-const _global = (window /* browser */ || global) /* node */ as any;
-
 @observer
 export class CollectionDockingView extends CollectionSubView() {
-    static tabClass: JSX.Element | null = null;
+    static tabClass: unknown = null;
     /**
      * Initialize by assigning the add split method to DocumentView and by
      * configuring golden layout to render its documents using the specified React component
      * @param ele - typically would be set to TabDocView
      */
-    public static Init(ele: JSX.Element | null) {
+    public static Init(ele: unknown) {
         this.tabClass = ele;
         DocumentView.addSplit = CollectionDockingView.AddSplit;
     }
@@ -54,11 +53,13 @@ export class CollectionDockingView extends CollectionSubView() {
     private _flush: UndoManager.Batch | undefined;
     private _unmounting = false;
     private _ignoreStateChange = '';
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
+    private _goldenLayout: any = null;
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
     public tabMap: Set<any> = new Set();
     public get HasFullScreen() {
         return this._goldenLayout._maximisedItem !== null;
     }
-    private _goldenLayout: any = null;
     static _highlightStyleSheet = addStyleSheet();
 
     constructor(props: SubCollectionViewProps) {
@@ -66,8 +67,8 @@ export class CollectionDockingView extends CollectionSubView() {
         makeObservable(this);
         if (this._props.renderDepth < 0) CollectionDockingView.Instance = this;
         // Why is this here?
-        (window as any).React = React;
-        (window as any).ReactDOM = ReactDOM;
+        (window as unknown as { React: unknown }).React = React;
+        (window as unknown as { ReactDOM: unknown }).ReactDOM = ReactDOM;
         DragManager.StartWindowDrag = this.StartOtherDrag;
         this.Document.myTrails; // this is equivalent to having a prefetchProxy for myTrails which is needed for the My Trails button in the UI which assumes that Doc.ActiveDashboard.myTrails is legit...
     }
@@ -89,10 +90,11 @@ export class CollectionDockingView extends CollectionSubView() {
     };
 
     tabItemDropped = () => DragManager.CompleteWindowDrag?.(false);
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
     tabDragStart = (proxy: any, finishDrag?: (aborted: boolean) => void) => {
         this._flush = this._flush ?? UndoManager.StartBatch('tab move');
-        const dashDoc = proxy?._contentItem?.tab?.DashDoc as Doc;
-        dashDoc && (DragManager.DocDragData = new DragManager.DocumentDragData([proxy._contentItem.tab.DashDoc]));
+        //const dashDoc = proxy?._contentItem?.tab?.DashDoc as Doc;
+        //dashDoc && (DragManager.DocDragData = new DragManager.DocumentDragData([proxy._contentItem.tab.DashDoc]));
         DragManager.CompleteWindowDrag = (aborted: boolean) => {
             if (aborted) {
                 proxy._dragListener.AbortDrag();
@@ -130,12 +132,13 @@ export class CollectionDockingView extends CollectionSubView() {
     }
 
     @undoBatch
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
     public static ReplaceTab(document: Doc, mods: OpenWhereMod, stack: any, panelName: string, addToSplit?: boolean, keyValue?: boolean): boolean {
         const instance = CollectionDockingView.Instance;
         if (!instance) return false;
         const newConfig = DashboardView.makeDocumentConfig(document, panelName, undefined, keyValue);
         if (!panelName && stack) {
-            const activeContentItemIndex = stack.contentItems.findIndex((item: any) => item.config === stack._activeContentItem.config);
+            const activeContentItemIndex = stack.contentItems.findIndex((item: { config: unknown }) => item.config === stack._activeContentItem.config);
             const newContentItem = stack.layoutManager.createContentItem(newConfig, instance._goldenLayout);
             stack.addChild(newContentItem.contentItems[0], undefined);
             stack.contentItems[activeContentItemIndex].remove();
@@ -155,6 +158,7 @@ export class CollectionDockingView extends CollectionSubView() {
     }
 
     @undoBatch
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
     public static ToggleSplit(doc: Doc, location: OpenWhereMod, stack?: any, panelName?: string, keyValue?: boolean) {
         return Array.from(CollectionDockingView.Instance?.tabMap.keys() ?? []).findIndex(tab => tab.DashDoc === doc) !== -1 ? CollectionDockingView.CloseSplit(doc) : CollectionDockingView.AddSplit(doc, location, stack, panelName, keyValue);
     }
@@ -163,6 +167,7 @@ export class CollectionDockingView extends CollectionSubView() {
     //  Creates a split on any side of the docking view based on the passed input pullSide and then adds the Document to the requested side
     //
     @action
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
     public static AddSplit(document: Doc, pullSide: OpenWhereMod, stack?: any, panelName?: string, keyValue?: boolean) {
         if (document?._type_collection === CollectionViewType.Docking && !keyValue) return DashboardView.openDashboard(document);
         if (!CollectionDockingView.Instance) return false;
@@ -321,7 +326,7 @@ export class CollectionDockingView extends CollectionSubView() {
      * @param target
      * @param title
      */
-    titleChanged = (target: any, value: any) => {
+    titleChanged = (target: Doc, value: FieldType) => {
         const title = Field.toString(value);
         if (title.startsWith('@') && !title.substring(1).match(/[()[\]@]/) && title.length > 1) {
             const embedding = DocListCast(target.proto_embeddings).lastElement();
@@ -340,7 +345,7 @@ export class CollectionDockingView extends CollectionSubView() {
                 () => DocumentView.LightboxDoc(),
                 doc => setTimeout(() => !doc && this.onResize())
             );
-            new _global.ResizeObserver(this.onResize).observe(this._containerRef.current);
+            new ResizeObserver(this.onResize).observe(this._containerRef.current);
             this._reactionDisposer = reaction(
                 () => StrCast(this.Document.dockingConfig),
                 config => {
@@ -429,7 +434,7 @@ export class CollectionDockingView extends CollectionSubView() {
     @action
     onPointerDown = (e: React.PointerEvent): void => {
         let hitFlyout = false;
-        for (let par = e.target as any; !hitFlyout && par; par = par.parentElement) {
+        for (let par = e.target as HTMLElement | null; !hitFlyout && par; par = par.parentElement) {
             hitFlyout = par.className === 'dockingViewButtonSelector';
         }
         if (!hitFlyout) {
@@ -514,6 +519,7 @@ export class CollectionDockingView extends CollectionSubView() {
         return changesMade;
     };
 
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
     tabDestroyed = (tab: any) => {
         this._flush = this._flush ?? UndoManager.StartBatch('tab movement');
         const dashDoc = tab.DashDoc;
@@ -531,18 +537,21 @@ export class CollectionDockingView extends CollectionSubView() {
             const { fieldKey } = CollectionDockingView.Instance.props;
             Doc.RemoveDocFromList(dview, fieldKey, dashDoc);
             this.tabMap.delete(tab);
-            tab._disposers && Object.values(tab._disposers).forEach((disposer: any) => disposer?.());
+            tab._disposers && Object.values(tab._disposers).forEach(disposer => (disposer as () => void)());
             this.stateChanged();
         }
     };
-    tabCreated = (tab: any) => {
+    tabCreated = (tab: { contentItem: { element: HTMLElement[] } }) => {
         this.tabMap.add(tab);
-        tab.contentItem.element[0]?.firstChild?.firstChild?.InitTab?.(tab); // have to explicitly initialize tabs that reuse contents from previous tabs (ie, when dragging a tab around a new tab is created for the old content)
+        // InitTab is added to the tab's HTMLElement in TabDocView
+        const tabdocviewContent = tab.contentItem.element[0]?.firstChild?.firstChild as unknown as { InitTab?: (tab: object) => void };
+        tabdocviewContent?.InitTab?.(tab); // have to explicitly initialize tabs that reuse contents from previous tabs (ie, when dragging a tab around a new tab is created for the old content)
     };
 
+    // eslint-disable-next-line @typescript-eslint/no-explicit-any
     stackCreated = (stackIn: any) => {
         const stack = stackIn.header ? stackIn : stackIn.origin;
-        stack.header?.element.on('mousedown', (e: any) => {
+        stack.header?.element.on('mousedown', (e: MouseEvent) => {
             const dashboard = Doc.ActiveDashboard;
             if (dashboard && e.target === stack.header?.element[0] && e.button === 2) {
                 dashboard.pane_count = NumCast(dashboard.pane_count) + 1;
@@ -595,7 +604,7 @@ export class CollectionDockingView extends CollectionSubView() {
                 })
             );
 
-        stack.element.click((e: any) => {
+        stack.element.click((e: { originalEvent: MouseEvent }) => {
             if (stack.contentItems.length === 0 && Array.from(document.elementsFromPoint(e.originalEvent.x, e.originalEvent.y)).some(ele => ele?.className === 'empty-tabs-message')) {
                 addNewDoc();
             }
@@ -633,7 +642,7 @@ export class CollectionDockingView extends CollectionSubView() {
 
 ScriptingGlobals.add(
     // eslint-disable-next-line prefer-arrow-callback
-    function openInLightbox(doc: any) {
+    function openInLightbox(doc: Doc) {
         CollectionDockingView.Instance?._props.addDocTab(doc, OpenWhere.lightboxAlways);
     },
     'opens up document in a lightbox',
@@ -641,33 +650,22 @@ ScriptingGlobals.add(
 );
 ScriptingGlobals.add(
     // eslint-disable-next-line prefer-arrow-callback
-    function openDoc(doc: any, where: OpenWhere) {
+    function openDoc(doc: Doc | string, where: OpenWhere) {
         switch (where) {
             case OpenWhere.addRight:
-                return CollectionDockingView.AddSplit(doc, OpenWhereMod.right);
+                return doc instanceof Doc && CollectionDockingView.AddSplit(doc, OpenWhereMod.right);
             case OpenWhere.overlay:
             default:
-                // prettier-ignore
                 switch (doc) {
                     case '<ScriptingRepl />': return OverlayView.Instance.addWindow(<ScriptingRepl />, { x: 300, y: 100, width: 200, height: 200, title: 'Scripting REPL' });
                     case "<UndoStack />": return OverlayView.Instance.addWindow(<UndoStack />, { x: 300, y: 100, width: 200, height: 200, title: 'Undo stack' });
-                    default: 
-                }
-                Doc.AddToMyOverlay(doc);
-                return true;
+                    default: return doc instanceof Doc && Doc.AddToMyOverlay(doc);
+                } // prettier-ignore
         }
     },
     'opens up document in location specified',
     '(doc: any)'
 );
-ScriptingGlobals.add(
-    // eslint-disable-next-line prefer-arrow-callback
-    function openRepl() {
-        return 'openRepl';
-    },
-    'opens up document in screen overlay layer',
-    '(doc: any)'
-);
 // eslint-disable-next-line prefer-arrow-callback
 ScriptingGlobals.add(async function snapshotDashboard() {
     const batch = UndoManager.StartBatch('snapshot');
-- 
cgit v1.2.3-70-g09d2

