-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Add Svelte 5 runes support #59
Open
rtrampox
wants to merge
12
commits into
ValentinH:main
Choose a base branch
from
rtrampox:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+570
−223
Open
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9e864bd
chore: Upgrade svelte version to 5
rtrampox 92d7bb7
Refactor to svelte's runes syntax and add props type
rtrampox a9a1519
Remove unintended pnpm-lock.yaml
rtrampox ce1126f
Export types from the index file
rtrampox 54a8283
chore: Update dependencies to latest versions and update yarn lockfile.
rtrampox debe25f
refactor: update event handling in Cropper component to use svelte ac…
rtrampox 498593d
fix: add missing import
rtrampox aff4f31
fix: add missing prevent default from some events.
rtrampox d282400
chore: remove unused lastZoom state
rtrampox 3629d2d
feat: improve types for cropcomplete event.
rtrampox 6145bf8
fix: add nonpassive to wheel event, and remove from touchstart.
rtrampox caf6c11
chore: remove unused effect
rtrampox 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 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 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 |
---|---|---|
@@ -1,35 +1,36 @@ | ||
<script lang="ts"> | ||
import { createEventDispatcher, onDestroy, onMount } from 'svelte' | ||
import type { HTMLImgAttributes } from 'svelte/elements' | ||
import { onDestroy, onMount } from 'svelte' | ||
import * as helpers from './helpers' | ||
import type { CropShape, DispatchEvents, ImageSize, Point, Size } from './types' | ||
|
||
export let image: string | ||
export let crop: Point = { x: 0, y: 0 } | ||
export let zoom = 1 | ||
export let aspect = 4 / 3 | ||
export let minZoom = 1 | ||
export let maxZoom = 3 | ||
export let cropSize: Size | null = null | ||
export let cropShape: CropShape = 'rect' | ||
export let showGrid = true | ||
export let zoomSpeed = 1 | ||
export let crossOrigin: HTMLImgAttributes['crossorigin'] = null | ||
export let restrictPosition = true | ||
export let tabindex: number | undefined = undefined | ||
|
||
let cropperSize: Size | null = null | ||
let imageSize: ImageSize = { width: 0, height: 0, naturalWidth: 0, naturalHeight: 0 } | ||
let containerEl: HTMLDivElement | null = null | ||
let containerRect: DOMRect | null = null | ||
let imgEl: HTMLImageElement | null = null | ||
let dragStartPosition: Point = { x: 0, y: 0 } | ||
let dragStartCrop: Point = { x: 0, y: 0 } | ||
let lastPinchDistance = 0 | ||
let rafDragTimeout: number | null = null | ||
let rafZoomTimeout: number | null = null | ||
|
||
const dispatch = createEventDispatcher<DispatchEvents>() | ||
import type { ImageSize, Point, CropperProps, Size } from './types' | ||
|
||
|
||
let { | ||
image, | ||
crop = $bindable({ x: 0, y: 0 }), | ||
zoom = $bindable(1), | ||
minZoom = $bindable(1), | ||
maxZoom = $bindable(3), | ||
aspect = 4 / 3, | ||
cropSize = null, | ||
cropShape = 'rect', | ||
showGrid = true, | ||
zoomSpeed = 1, | ||
crossOrigin = null, | ||
restrictPosition = true, | ||
tabindex = undefined, | ||
oncropcomplete, | ||
}: Partial<CropperProps> = $props() | ||
|
||
let cropperSize = $state<Size | null>(null) | ||
let imageSize = $state<ImageSize>({ width: 0, height: 0, naturalWidth: 0, naturalHeight: 0 }) | ||
let containerEl = $state<HTMLDivElement | null>(null) | ||
let containerRect = $state<DOMRect | null>(null) | ||
let imgEl = $state<HTMLImageElement | null>(null) | ||
let dragStartPosition = $state<Point>({ x: 0, y: 0 }) | ||
let dragStartCrop = $state<Point>({ x: 0, y: 0 }) | ||
let lastPinchDistance = $state(0) | ||
let rafDragTimeout = $state<number | null>(null) | ||
let rafZoomTimeout = $state<number | null>(null) | ||
|
||
onMount(() => { | ||
// when rendered via SSR, the image can already be loaded and its onLoad callback will never be called | ||
|
@@ -229,34 +230,55 @@ | |
restrictPosition | ||
) | ||
|
||
dispatch('cropcomplete', { | ||
oncropcomplete?.({ | ||
percent: croppedAreaPercentages, | ||
pixels: croppedAreaPixels, | ||
}) | ||
} | ||
|
||
// ------ Reactive statement ------ | ||
//when aspect changes, we reset the cropperSize | ||
$: if (imgEl) { | ||
cropperSize = cropSize ? cropSize : helpers.getCropSize(imgEl.width, imgEl.height, aspect) | ||
} | ||
$effect(() => { | ||
if (imgEl) { | ||
cropperSize = cropSize ? cropSize : helpers.getCropSize(imgEl.width, imgEl.height, aspect) | ||
} | ||
}) | ||
|
||
// when zoom changes, we recompute the cropped area | ||
$: zoom && emitCropData() | ||
let lastZoom = $state(zoom); | ||
$effect(() => { | ||
if (zoom !== lastZoom) { | ||
lastZoom = zoom; | ||
emitCropData() | ||
} | ||
}) | ||
|
||
$: if(aspect) { | ||
computeSizes() | ||
emitCropData() | ||
} | ||
let lastAspect = $state(aspect); | ||
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. Same here, this state should not be needed. 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. The |
||
$effect(() => { | ||
if (aspect !== lastAspect) { | ||
lastAspect = aspect; | ||
computeSizes() | ||
emitCropData() | ||
} | ||
}) | ||
</script> | ||
|
||
<svelte:window on:resize={computeSizes} /> | ||
<div | ||
class="svelte-easy-crop-container" | ||
bind:this={containerEl} | ||
on:mousedown|preventDefault={onMouseDown} | ||
on:touchstart|nonpassive|preventDefault={onTouchStart} | ||
ValentinH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
on:wheel|nonpassive|preventDefault={onWheel} | ||
onmousedown={(e) => { | ||
e.preventDefault() | ||
onMouseDown(e) | ||
}} | ||
ontouchstart={(e) => { | ||
e.preventDefault() | ||
onTouchStart(e) | ||
}} | ||
onwheel={(e) => { | ||
e.preventDefault(); | ||
onWheel(e) | ||
}} | ||
{tabindex} | ||
role="button" | ||
data-testid="container" | ||
|
@@ -265,7 +287,7 @@ | |
bind:this={imgEl} | ||
class="svelte-easy-crop-image" | ||
src={image} | ||
on:load={onImgLoad} | ||
onload={onImgLoad} | ||
alt="" | ||
style="transform: translate({crop.x}px, {crop.y}px) scale({zoom});" | ||
crossorigin={crossOrigin} | ||
|
@@ -277,7 +299,7 @@ | |
class:svelte-easy-crop-grid={showGrid} | ||
style="width: {cropperSize.width}px; height: {cropperSize.height}px;" | ||
data-testid="cropper" | ||
/> | ||
></div> | ||
{/if} | ||
</div> | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import Cropper from './Cropper.svelte' | ||
|
||
export * from "./types" | ||
export default Cropper |
This file contains 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 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
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.
We should not need to track the last zoom value ourselves, Svelte should do it automatically as long as we access
zoom
in the effect.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.
This one can be removed safely without causing loops. Just added it for testing and forgot to remove