-
Notifications
You must be signed in to change notification settings - Fork 25
feat: add shift + mouse scroll for zoom out and zoom in in preview app #1248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d452679
feat: add shift + mouse scroll for zoom out and zoom in in preview app
AlexAndBear 9f1c175
Apply suggestion from @Copilot
AlexAndBear 0d8aa32
refactor
AlexAndBear f327608
refactor
AlexAndBear fb89b24
refactor
AlexAndBear d20296c
fix bug while reloading image
AlexAndBear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,15 +5,24 @@ | |
| :src="file.url" | ||
| :alt="file.name" | ||
| :data-id="file.id" | ||
| class="max-w-full max-h-full pt-4 cursor-move object-contain" | ||
| :style="`transform: rotate(${currentImageRotation}deg)`" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed anymore |
||
| class="max-w-full max-h-full pt-4" | ||
| /> | ||
| </template> | ||
| <script lang="ts"> | ||
| import { CachedFile } from '../../helpers/types' | ||
| import { defineComponent, PropType, onMounted, ref, watch, unref, nextTick } from 'vue' | ||
| import { | ||
| defineComponent, | ||
| PropType, | ||
| ref, | ||
| watch, | ||
| unref, | ||
| nextTick, | ||
| onMounted, | ||
| onBeforeUnmount | ||
| } from 'vue' | ||
| import type { PanzoomObject, PanzoomOptions } from '@panzoom/panzoom' | ||
| import Panzoom from '@panzoom/panzoom' | ||
| import { useEventBus } from '@opencloud-eu/web-pkg' | ||
|
|
||
| export default defineComponent({ | ||
| name: 'MediaImage', | ||
|
|
@@ -22,39 +31,68 @@ export default defineComponent({ | |
| type: Object as PropType<CachedFile>, | ||
| required: true | ||
| }, | ||
| currentImageZoom: { | ||
| type: Number, | ||
| required: true | ||
| }, | ||
| currentImageRotation: { | ||
| type: Number, | ||
| required: true | ||
| }, | ||
| currentImagePositionX: { | ||
| type: Number, | ||
| required: true | ||
| }, | ||
| currentImagePositionY: { | ||
| type: Number, | ||
| required: true | ||
| } | ||
| }, | ||
| emits: ['panZoomChange'], | ||
| setup(props, { emit }) { | ||
| setup(props) { | ||
| const eventBus = useEventBus() | ||
|
|
||
| const img = ref<HTMLElement | null>() | ||
| const panzoom = ref<PanzoomObject>() | ||
| const resetEventToken = ref(null) | ||
| const zoomToken = ref(null) | ||
| const shrinkToken = ref(null) | ||
|
|
||
| const onWheelEvent = (e: WheelEvent) => { | ||
| e.preventDefault() | ||
| if (!e.shiftKey) { | ||
| return false | ||
| } | ||
|
|
||
| const onPanZoomChange = (event: Event) => { | ||
| emit('panZoomChange', event) | ||
| if (e.deltaY < 0) { | ||
| unref(panzoom).zoomIn({ step: 0.1 }) | ||
| } else if (e.deltaY > 0) { | ||
| unref(panzoom).zoomOut({ step: 0.1 }) | ||
| } | ||
| } | ||
|
|
||
| const setTransform = ({ scale, x, y }: { scale: number; x: number; y: number }) => { | ||
| let h: number | ||
| let v: number | ||
|
|
||
| switch (props.currentImageRotation) { | ||
| case -270: | ||
| case 90: | ||
| h = y | ||
| v = 0 - x | ||
| break | ||
| case -180: | ||
| case 180: | ||
| h = 0 - x | ||
| v = 0 - y | ||
| break | ||
| case -90: | ||
| case 270: | ||
| h = 0 - y | ||
| v = x | ||
| break | ||
| default: | ||
| h = x | ||
| v = y | ||
| } | ||
|
|
||
| unref(panzoom).setStyle( | ||
| 'transform', | ||
| `rotate(${props.currentImageRotation}deg) scale(${scale}) translate(${h}px, ${v}px)` | ||
| ) | ||
| } | ||
|
|
||
| const initPanzoom = async () => { | ||
| if (unref(panzoom)) { | ||
| await nextTick() | ||
| ;(unref(img) as unknown as HTMLElement).removeEventListener( | ||
| 'panzoomchange', | ||
| onPanZoomChange | ||
| ) | ||
| ;(unref(img) as unknown as HTMLElement).removeEventListener('wheel', onWheelEvent) | ||
| unref(panzoom)?.destroy() | ||
| } | ||
|
|
||
|
|
@@ -65,50 +103,46 @@ export default defineComponent({ | |
| animate: false, | ||
| duration: 300, | ||
| overflow: 'auto', | ||
| minScale: 0.5, | ||
| maxScale: 10, | ||
| setTransform: (_, { scale, x, y }) => { | ||
| let h: number | ||
| let v: number | ||
|
|
||
| switch (props.currentImageRotation) { | ||
| case -270: | ||
| case 90: | ||
| h = y | ||
| v = 0 - x | ||
| break | ||
| case -180: | ||
| case 180: | ||
| h = 0 - x | ||
| v = 0 - y | ||
| break | ||
| case -90: | ||
| case 270: | ||
| h = 0 - y | ||
| v = x | ||
| break | ||
| default: | ||
| h = x | ||
| v = y | ||
| } | ||
|
|
||
| unref(panzoom).setStyle( | ||
| 'transform', | ||
| `rotate(${props.currentImageRotation}deg) scale(${scale}) translate(${h}px, ${v}px)` | ||
| ) | ||
| } | ||
| setTransform: (_, { scale, x, y }) => setTransform({ scale, x, y }) | ||
| } as PanzoomOptions) | ||
| ;(unref(img) as unknown as HTMLElement).addEventListener('panzoomchange', onPanZoomChange) | ||
| ;(unref(img) as unknown as HTMLElement).addEventListener('wheel', onWheelEvent) | ||
| } | ||
|
|
||
| watch(img, initPanzoom) | ||
| onMounted(initPanzoom) | ||
| watch(() => props.file, initPanzoom, { immediate: true, deep: true }) | ||
|
|
||
| watch( | ||
| () => props.currentImageRotation, | ||
| () => { | ||
| if (!unref(panzoom)) { | ||
| return | ||
| } | ||
|
|
||
| setTransform({ | ||
| scale: unref(panzoom).getScale(), | ||
| x: unref(panzoom).getPan().x, | ||
| y: unref(panzoom).getPan().y | ||
| }) | ||
| } | ||
| ) | ||
|
|
||
| watch([() => props.currentImageZoom, () => props.currentImageRotation], () => { | ||
| unref(panzoom).zoom(props.currentImageZoom) | ||
| onMounted(() => { | ||
| resetEventToken.value = eventBus.subscribe('app.preview.media.image.reset', () => | ||
| initPanzoom() | ||
| ) | ||
| zoomToken.value = eventBus.subscribe('app.preview.media.image.zoom', () => | ||
| unref(panzoom)?.zoomIn({ step: 0.1 }) | ||
| ) | ||
| shrinkToken.value = eventBus.subscribe('app.preview.media.image.shrink', () => | ||
| unref(panzoom)?.zoomOut({ step: 0.1 }) | ||
| ) | ||
| }) | ||
|
|
||
| watch([() => props.currentImagePositionX, () => props.currentImagePositionY], () => { | ||
| unref(panzoom).pan(props.currentImagePositionX, props.currentImagePositionY) | ||
| onBeforeUnmount(() => { | ||
| eventBus.unsubscribe('app.preview.media.image.reset', unref(resetEventToken)) | ||
| eventBus.unsubscribe('app.preview.media.image.zoom', unref(zoomToken)) | ||
| eventBus.unsubscribe('app.preview.media.image.shrink', unref(shrinkToken)) | ||
| }) | ||
|
|
||
| return { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cursor-move object-contain are obsolete