From 139600ab7e8a82a31744cd3798247236cd5616fc Mon Sep 17 00:00:00 2001 From: bobzel Date: Fri, 3 May 2024 22:08:56 -0400 Subject: fixed pdf rendering. fixed cropping images. fixed zooming out of an image that is fitWidth to set scroll bar properly. --- .../collectionFreeForm/CollectionFreeFormView.tsx | 87 ++++++++-------------- 1 file changed, 31 insertions(+), 56 deletions(-) (limited to 'src/client/views/collections/collectionFreeForm') diff --git a/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx b/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx index a36c36261..21a069bd8 100644 --- a/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx +++ b/src/client/views/collections/collectionFreeForm/CollectionFreeFormView.tsx @@ -22,7 +22,7 @@ import { ImageField } from '../../../../fields/URLField'; import { TraceMobx } from '../../../../fields/util'; import { Gestures, PointData } from '../../../../pen-gestures/GestureTypes'; import { GestureUtils } from '../../../../pen-gestures/GestureUtils'; -import { aggregateBounds, emptyFunction, intersectRect, Utils } from '../../../../Utils'; +import { aggregateBounds, clamp, emptyFunction, intersectRect, Utils } from '../../../../Utils'; import { Docs } from '../../../documents/Documents'; import { CollectionViewType, DocumentType } from '../../../documents/DocumentTypes'; import { DocUtils } from '../../../documents/DocUtils'; @@ -377,7 +377,7 @@ export class CollectionFreeFormView extends CollectionSubView pair.layout) .slice() @@ -411,8 +410,7 @@ export class CollectionFreeFormView extends CollectionSubView { const layoutDoc = Doc.Layout(d); const delta = Utils.rotPt(x - dropPos[0], y - dropPos[1], fromScreenXf.Rotate); if (this.Document._currentFrame !== undefined) { @@ -431,7 +429,7 @@ export class CollectionFreeFormView extends CollectionSubView { SnappingManager.TriggerUserPanned(); - this.setPan(NumCast(this.Document[this.panXFieldKey]) - e.deltaX, NumCast(this.Document[this.panYFieldKey]) - e.deltaY, 0, true); + this.setPan(NumCast(this.Document[this.panXFieldKey]) - e.deltaX, NumCast(this.Document[this.panYFieldKey]) - e.deltaY, 0); }; @action pan = (e: PointerEvent): void => { - const ctrlKey = e.ctrlKey && !e.shiftKey; - const shiftKey = e.shiftKey && !e.ctrlKey; + const [ctrlKey, shiftKey] = [e.ctrlKey && !e.shiftKey, e.shiftKey && !e.ctrlKey]; SnappingManager.TriggerUserPanned(); this.DocumentView?.().clearViewTransition(); const [dxi, dyi] = this.screenToFreeformContentsXf.transformDirection(e.clientX - this._lastX, e.clientY - this._lastY); const { x: dx, y: dy } = Utils.rotPt(dxi, dyi, this.ScreenToLocalBoxXf().Rotate); - this.setPan(NumCast(this.Document[this.panXFieldKey]) - (ctrlKey ? 0 : dx), NumCast(this.Document[this.panYFieldKey]) - (shiftKey ? 0 : dy), 0, true); + this.setPan(NumCast(this.Document[this.panXFieldKey]) - (ctrlKey ? 0 : dx), NumCast(this.Document[this.panYFieldKey]) - (shiftKey ? 0 : dy), 0); this._lastX = e.clientX; this._lastY = e.clientY; }; @@ -795,18 +791,15 @@ export class CollectionFreeFormView extends CollectionSubView NumCast(this.Document[this.scaleFieldKey + '_max'], Number.MAX_VALUE)) { - deltaScale = NumCast(this.Document[this.scaleFieldKey + '_max'], 1) / invTransform.Scale; - } - if (deltaScale * invTransform.Scale < NumCast(this.Document[this.scaleFieldKey + '_min'], this.isAnnotationOverlay ? 1 : 0)) { - deltaScale = NumCast(this.Document[this.scaleFieldKey + '_min'], 1) / invTransform.Scale; - } - + const minScale = NumCast(this.Document[this.scaleFieldKey + '_min'], this.isAnnotationOverlay ? 1 : 0); + const maxScale = NumCast(this.Document[this.scaleFieldKey + '_max'], Number.MAX_VALUE); + deltaScale = clamp(deltaScale, minScale / invTransform.Scale, maxScale / invTransform.Scale); const localTransform = invTransform.scaleAbout(deltaScale, x, y); if (localTransform.Scale >= 0.05 || localTransform.Scale > this.zoomScaling()) { const safeScale = Math.min(Math.max(0.05, localTransform.Scale), 20); + const allowScroll = this.Document[this.scaleFieldKey] !== minScale && Math.abs(safeScale) === minScale; this.Document[this.scaleFieldKey] = Math.abs(safeScale); - this.setPan(-localTransform.TranslateX / safeScale, (this._props.originTopLeft ? undefined : NumCast(this.Document.layout_scrollTop) * safeScale) || -localTransform.TranslateY / safeScale); + this.setPan(-localTransform.TranslateX / safeScale, (this._props.originTopLeft ? undefined : NumCast(this.Document.layout_scrollTop) * safeScale) || -localTransform.TranslateY / safeScale, undefined, allowScroll); } }; @@ -843,35 +836,18 @@ export class CollectionFreeFormView extends CollectionSubView will talk with Bob about using mobx to do this to remove this line of code. if (Doc.UserDoc()?.presentationMode === 'watching') ReplayMovements.Instance.pauseFromInteraction(); - if (!this.isAnnotationOverlay && clamp) { + if (!this.isAnnotationOverlay && this.childDocs.length) { // this section wraps the pan position, horizontally and/or vertically whenever the content is panned out of the viewing bounds - const docs = this.childLayoutPairs.map(pair => pair.layout).filter(doc => doc instanceof Doc && doc.type !== DocumentType.LINK); - const measuredDocs = docs.map(doc => ({ x: NumCast(doc.x), y: NumCast(doc.y), width: NumCast(doc._width), height: NumCast(doc._height) })); - if (measuredDocs.length) { - const { originTopLeft } = this._props; - // const xrangeMin = Math.min(...measuredDocs.map(doc => doc.x), originTopLeft ? 0 : Number.MAX_VALUE); - // const yrangeMin = Math.min(...measuredDocs.map(doc => doc.y), originTopLeft ? 0 : Number.MAX_VALUE); - // const xrangeMax = Math.max(...measuredDocs.map(doc => doc.x + doc.width)); - // const yrangeMax = Math.max(...measuredDocs.map(doc => doc.y + doc.height)); - const { bounds: { x: xrangeMin, y: yrangeMin, r: xrangeMax, b: yrangeMax } } = this.contentBounds(); // prettier-ignore - const nativeScaling = this._props.NativeDimScaling?.() || 1; - const scaling = this.zoomScaling() * nativeScaling; - const [widScaling, hgtScaling] = [this._props.PanelWidth() / scaling, this._props.PanelHeight() / scaling]; - const panelWidMax = widScaling * (originTopLeft ? 2 / nativeScaling : 1); - const panelHgtMax = hgtScaling * (originTopLeft ? 2 / nativeScaling : 1); - const panelWidMin = widScaling * (originTopLeft ? 0 : 1); - const panelHgtMin = hgtScaling * (originTopLeft ? 0 : 1); - if (xrangeMin >= panX + panelWidMax / 2) panX = xrangeMax + (originTopLeft ? 0 : panelWidMax / 2); - else if (xrangeMax <= panX - panelWidMin / 2) panX = xrangeMin - (originTopLeft ? panelWidMax / 2 : panelWidMin / 2); // prettier-ignore - if (yrangeMin >= panY + panelHgtMax / 2) panY = yrangeMax + (originTopLeft ? 0 : panelHgtMax / 2); - else if (yrangeMax <= panY - panelHgtMin / 2) panY = yrangeMin - (originTopLeft ? panelHgtMax / 2 : panelHgtMin / 2); // prettier-ignore - } + const { bounds: { x: xrangeMin, y: yrangeMin, r: xrangeMax, b: yrangeMax } } = this.contentBounds(); // prettier-ignore + const scaling = this.zoomScaling() * (this._props.NativeDimScaling?.() || 1); + const [widScaling, hgtScaling] = [this._props.PanelWidth() / scaling, this._props.PanelHeight() / scaling]; + panX = clamp(panX, xrangeMin - widScaling / 2, xrangeMax + widScaling / 2); + panY = clamp(panY, yrangeMin - hgtScaling / 2, yrangeMax + hgtScaling / 2); } if (!this.layoutDoc._lockedTransform || LightboxView.LightboxDoc) { this.setPanZoomTransition(panTime); @@ -880,22 +856,22 @@ export class CollectionFreeFormView extends CollectionSubView 2 && this.layoutDoc.layout_scrollTop === undefined && NumCast(this.layoutDoc._freeform_scale, minScale) === minScale) { - const maxPanScrollY = minPanY + fitYscroll; - const relTop = (panY - minPanY) / (maxPanScrollY - minPanY); + const newPanY = clamp(panY, minPanY, maxPanY); + // this mess fixes a problem when zooming to the default on an image that is fit width and can scroll. + // Without this, the scroll always goes to the top, instead of matching the pan position. + if (fitYscroll > 2 && allowScroll && NumCast(this.layoutDoc._freeform_scale, minScale) === minScale) { setTimeout(() => { + const relTop = (clamp(panY, minPanY, fitYscroll) - minPanY) / fitYscroll; this.layoutDoc.layout_scrollTop = relTop * maxScrollTop; }, 10); - newPanY = minPanY; } !this.Document._verticalScroll && (this.Document[this.panXFieldKey] = this.isAnnotationOverlay ? newPanX : panX); !this.Document._horizontalScroll && (this.Document[this.panYFieldKey] = this.isAnnotationOverlay ? newPanY : panY); @@ -909,8 +885,7 @@ export class CollectionFreeFormView extends CollectionSubView