From cdbe62da5c62f809629f4e8b01524b223f38abd4 Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Fri, 26 Jan 2024 10:38:02 -0500
Subject: cleaning up doubleClick documentview handling.  fixed pan mode to
 drag collections when not active.

---
 src/client/views/nodes/DocumentView.tsx | 116 +++++++++++---------------------
 1 file changed, 38 insertions(+), 78 deletions(-)

(limited to 'src/client/views/nodes/DocumentView.tsx')

diff --git a/src/client/views/nodes/DocumentView.tsx b/src/client/views/nodes/DocumentView.tsx
index 8eb354e1e..a82580ddb 100644
--- a/src/client/views/nodes/DocumentView.tsx
+++ b/src/client/views/nodes/DocumentView.tsx
@@ -236,16 +236,15 @@ export class DocumentViewInternal extends DocComponent<FieldViewProps & Document
         runInAction(() => (this._mounted = true));
         this.setupHandlers();
         this._disposers.contentActive = reaction(
-            () => {
+            () =>
                 //  true  - if the document has been activated directly or indirectly (by having its children selected)
                 //  false - if its pointer events are explicitly turned off or if it's container tells it that it's inactive
                 // undefined - it is not active, but it should be responsive to actions that might activate it or its contents (eg clicking)
-                return this._props.isContentActive() === false || this._props.pointerEvents?.() === 'none'
+                this._props.isContentActive() === false || this._props.pointerEvents?.() === 'none'
                     ? false
                     : Doc.ActiveTool !== InkTool.None || SnappingManager.CanEmbed || this.rootSelected() || this.Document.forceActive || this._componentView?.isAnyChildContentActive?.() || this._props.isContentActive()
                       ? true
-                      : undefined;
-            },
+                      : undefined,
             active => (this._isContentActive = active),
             { fireImmediately: true }
         );
@@ -312,20 +311,23 @@ export class DocumentViewInternal extends DocComponent<FieldViewProps & Document
             let stopPropagate = true;
             let preventDefault = true;
             !this.layoutDoc._keepZWhenDragged && this._props.bringToFront?.(this.Document);
+            const scriptProps = {
+                this: this.Document,
+                _readOnly_: false,
+                scriptContext: this._props.scriptContext,
+                documentView,
+                clientX: e.clientX,
+                clientY: e.clientY,
+                shiftKey: e.shiftKey,
+                altKey: e.altKey,
+                metaKey: e.metaKey,
+                value: undefined,
+            };
             if (this._doubleTap) {
                 const defaultDblclick = this._props.defaultDoubleClick?.() || this.Document.defaultDoubleClick;
                 if (this.onDoubleClickHandler?.script) {
-                    const { clientX, clientY, shiftKey, altKey, ctrlKey } = e; // or we could call e.persist() to capture variables
-                    // prettier-ignore
-                    const func = () => this.onDoubleClickHandler.script.run( {
-                            this: this.Document,
-                            scriptContext: this._props.scriptContext,
-                            documentView,
-                            clientX, clientY,  altKey,  shiftKey,  ctrlKey,
-                            value: undefined,
-                        }, console.log  );
-                    UndoManager.RunInBatch(() => (func().result?.select === true ? this._props.select(false) : ''), 'on double click');
-                } else if (!Doc.IsSystem(this.Document) && (defaultDblclick === undefined || defaultDblclick === 'default')) {
+                    UndoManager.RunInBatch(() => this.onDoubleClickHandler.script.run(scriptProps, console.log).result?.select && this._props.select(false), 'on double click: ' + this.Document.title);
+                } else if (!Doc.IsSystem(this.Document) && defaultDblclick !== 'ignore') {
                     UndoManager.RunInBatch(() => LightboxView.Instance.AddDocTab(this.Document, OpenWhere.lightbox), 'double tap');
                     SelectionManager.DeselectAll();
                     Doc.UnBrushDoc(this.Document);
@@ -338,33 +340,14 @@ export class DocumentViewInternal extends DocComponent<FieldViewProps & Document
             } else {
                 let clickFunc: undefined | (() => any);
                 if (!this.disableClickScriptFunc && this.onClickHandler?.script) {
-                    const { clientX, clientY, shiftKey, altKey, metaKey } = e;
-                    const func = () => {
-                        // replace default add doc func with this view's add doc func.
-                        // to allow override behaviors for how to display links to undisplayed documents.
-                        // e.g., if this document is part of a labeled 'lightbox' container, then documents will be shown in place
-                        // instead of in the global lightbox
+                    clickFunc = undoable(() => {
+                        // use this view's add doc func to override method for following links to undisplayed documents.
+                        // e.g., if this document is part of a labeled 'lightbox' container, then documents will be shown in this container of in the global lightbox
                         const oldFunc = DocumentViewInternal.addDocTabFunc;
                         DocumentViewInternal.addDocTabFunc = this._props.addDocTab;
-                        this.onClickHandler?.script.run(
-                            {
-                                this: this.Document,
-                                _readOnly_: false,
-                                scriptContext: this._props.scriptContext,
-                                documentView,
-                                clientX,
-                                clientY,
-                                shiftKey,
-                                altKey,
-                                metaKey,
-                            },
-                            console.log
-                        ).result?.select === true
-                            ? this._props.select(false)
-                            : '';
+                        this.onClickHandler?.script.run(scriptProps, console.log).result?.select && this._props.select(false);
                         DocumentViewInternal.addDocTabFunc = oldFunc;
-                    };
-                    clickFunc = () => UndoManager.RunInBatch(func, 'click ' + this.Document.title);
+                    }, 'click ' + this.Document.title);
                 } else {
                     // onDragStart implies a button doc that we don't want to select when clicking. RootDocument & isTemplateForField implies we're clicking on part of a template instance and we want to select the whole template, not the part
                     if ((this.layoutDoc.onDragStart || this._props.TemplateDataDocument) && !(e.ctrlKey || e.button > 0)) {
@@ -412,10 +395,9 @@ export class DocumentViewInternal extends DocComponent<FieldViewProps & Document
                 !Doc.IsInMyOverlay(this.layoutDoc)
             ) {
                 e.stopPropagation();
-                // don't preventDefault anymore.  Goldenlayout, PDF text selection and RTF text selection all need it to go though
-                //if (this._props.isSelected(true) && this.Document.type !== DocumentType.PDF && this.layoutDoc._type_collection !== CollectionViewType.Docking) e.preventDefault();
+                // don't preventDefault.  Goldenlayout, PDF text selection and RTF text selection all need it to go though
 
-                // listen to move events if document content isn't active or document is draggable
+                // listen to move events when document content isn't active or document is always draggable
                 if (!this.layoutDoc._lockedPosition && (!this.isContentActive() || BoolCast(this.layoutDoc._dragWhenActive, this._props.dragWhenActive))) {
                     document.addEventListener('pointermove', this.onPointerMove);
                 }
@@ -489,26 +471,24 @@ export class DocumentViewInternal extends DocComponent<FieldViewProps & Document
         if (this.Document === Doc.ActiveDashboard) {
             e.stopPropagation();
             e.preventDefault();
-            alert(
-                (e.target as any)?.closest?.('*.lm_content')
-                    ? "You can't perform this move most likely because you didn't drag the document's title bar to enable embedding in a different document."
-                    : 'Linking to document tabs not yet supported. Drop link on document content.'
-            );
+            alert((e.target as any)?.closest?.('*.lm_content') ? "You can't perform this move most likely because you didn't drag the document's title bar to enable embedding in a different document." : 'Linking to document tabs not yet supported.');
             return true;
         }
-        const linkdrag = de.complete.annoDragData ?? de.complete.linkDragData;
+        const annoData = de.complete.annoDragData;
+        const linkdrag = annoData ?? de.complete.linkDragData;
         if (linkdrag) {
             linkdrag.linkSourceDoc = linkdrag.linkSourceGetAnchor();
             if (linkdrag.linkSourceDoc && linkdrag.linkSourceDoc !== this.Document) {
-                if (de.complete.annoDragData && !de.complete.annoDragData.dropDocument) {
-                    de.complete.annoDragData.dropDocument = de.complete.annoDragData.dropDocCreator(undefined);
+                if (annoData && !annoData.dropDocument) {
+                    annoData.dropDocument = annoData.dropDocCreator(undefined);
                 }
-                if (de.complete.annoDragData || this.Document !== linkdrag.linkSourceDoc.embedContainer) {
-                    const dropDoc = de.complete.annoDragData?.dropDocument ?? this._componentView?.getAnchor?.(true) ?? this.Document;
-                    de.complete.linkDocument = DocUtils.MakeLink(linkdrag.linkSourceDoc, dropDoc, {}, undefined, [de.x, de.y - 50]);
-                    if (de.complete.linkDocument) {
-                        de.complete.linkDocument.layout_isSvg = true;
-                        this._docView?.CollectionFreeFormView?.addDocument(de.complete.linkDocument);
+                if (annoData || this.Document !== linkdrag.linkSourceDoc.embedContainer) {
+                    const dropDoc = annoData?.dropDocument ?? this._componentView?.getAnchor?.(true) ?? this.Document;
+                    const linkDoc = DocUtils.MakeLink(linkdrag.linkSourceDoc, dropDoc, {}, undefined, [de.x, de.y - 50]);
+                    if (linkDoc) {
+                        de.complete.linkDocument = linkDoc;
+                        linkDoc.layout_isSvg = true;
+                        this._docView?.CollectionFreeFormView?.addDocument(linkDoc);
                     }
                 }
                 e.stopPropagation();
@@ -518,25 +498,6 @@ export class DocumentViewInternal extends DocComponent<FieldViewProps & Document
         return false;
     }, 'drop doc');
 
-    makeIntoPortal = undoable(() => {
-        const portalLink = this._allLinks.find(d => d.link_anchor_1 === this.Document && d.link_relationship === 'portal to:portal from');
-        if (!portalLink) {
-            DocUtils.MakeLink(
-                this.Document,
-                Docs.Create.FreeformDocument([], {
-                    _width: NumCast(this.layoutDoc._width) + 10,
-                    _height: Math.max(NumCast(this.layoutDoc._height), NumCast(this.layoutDoc._width) + 10),
-                    _isLightbox: true,
-                    _layout_fitWidth: true,
-                    title: StrCast(this.Document.title) + ' [Portal]',
-                }),
-                { link_relationship: 'portal to:portal from' }
-            );
-        }
-        this.Document.followLinkLocation = OpenWhere.lightbox;
-        this.Document.onClick = FollowLinkScript();
-    }, 'make into portal');
-
     importDocument = () => {
         const input = document.createElement('input');
         input.type = 'file';
@@ -627,7 +588,7 @@ export class DocumentViewInternal extends DocComponent<FieldViewProps & Document
                 const existingOnClick = cm.findByDescription('OnClick...');
                 const onClicks: ContextMenuProps[] = existingOnClick && 'subitems' in existingOnClick ? existingOnClick.subitems : [];
 
-                onClicks.push({ description: 'Enter Portal', event: this.makeIntoPortal, icon: 'window-restore' });
+                onClicks.push({ description: 'Enter Portal', event: undoable(e => DocUtils.makeIntoPortal(this.Document, this.layoutDoc, this._allLinks), 'make into portal'), icon: 'window-restore' });
                 !Doc.noviceMode && onClicks.push({ description: 'Toggle Detail', event: this.setToggleDetail, icon: 'concierge-bell' });
 
                 if (!this.Document.annotationOn) {
@@ -818,7 +779,7 @@ export class DocumentViewInternal extends DocComponent<FieldViewProps & Document
                     rootSelected={this.rootSelected}
                     onClickScript={this.onClickFunc}
                     setTitleFocus={this.setTitleFocus}
-                    hideClickBehaviors={BoolCast(this.layoutDoc.hideClickBehaviors)}
+                    hideClickBehaviors={BoolCast(this.Document.hideClickBehaviors)}
                 />
                 {this.layoutDoc.layout_hideAllLinks ? null : this.allLinkEndpoints()}
             </div>
@@ -1254,7 +1215,6 @@ export class DocumentView extends DocComponent<DocumentViewProps>() {
         this.layoutDoc._viewTransition = undefined;
     };
     public noOnClick = () => this._docViewInternal?.noOnClick();
-    public makeIntoPortal = () => this._docViewInternal?.makeIntoPortal();
     public toggleFollowLink = (zoom?: boolean, setTargetToggle?: boolean): void => this._docViewInternal?.toggleFollowLink(zoom, setTargetToggle);
     public setToggleDetail = () => this._docViewInternal?.setToggleDetail();
     public onContextMenu = (e?: React.MouseEvent, pageX?: number, pageY?: number) => this._docViewInternal?.onContextMenu?.(e, pageX, pageY);
-- 
cgit v1.2.3-70-g09d2

