start new -> upload new

This commit is contained in:
Qing 2022-06-14 21:33:42 +08:00
parent 335685d581
commit 19e7e816af
3 changed files with 40 additions and 16 deletions

View File

@ -287,20 +287,30 @@ export default function Editor(props: EditorProps) {
// Zoom reset // Zoom reset
const resetZoom = useCallback(() => { const resetZoom = useCallback(() => {
if (!minScale || !original || !windowSize) { if (!minScale) {
return return
} }
const viewport = viewportRef.current const viewport = viewportRef.current
if (!viewport) { if (!viewport) {
throw new Error('no viewport') return
} }
const offsetX = (windowSize.width - original.width * minScale) / 2 const offsetX = (windowSize.width - original.width * minScale) / 2
const offsetY = (windowSize.height - original.height * minScale) / 2 const offsetY = (windowSize.height - original.height * minScale) / 2
viewport.setTransform(offsetX, offsetY, minScale, 200, 'easeOutQuad') viewport.setTransform(offsetX, offsetY, minScale, 0, 'easeOutQuad')
viewport.state.scale = minScale viewport.state.scale = minScale
setScale(minScale) setScale(minScale)
setPanned(false) setPanned(false)
}, [viewportRef, minScale, original, windowSize]) }, [viewportRef, windowSize, original.width, windowSize.height, minScale])
useEffect(() => {
setLineGroups([])
setCurLineGroup([])
setRenders([])
resetZoom()
const imageSizeLimit = Math.max(original.width, original.height)
setSizeLimit(imageSizeLimit)
}, [resetZoom, file, original])
useEffect(() => { useEffect(() => {
window.addEventListener('resize', () => { window.addEventListener('resize', () => {

View File

@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react' import React, { useCallback, useEffect, useState } from 'react'
import Selector from '../shared/Selector' import Selector from '../shared/Selector'
const sizes = ['720', '1080', '2000', 'Original'] const sizes = ['720', '1080', '2000', 'Original']
@ -14,6 +14,10 @@ export default function SizeSelector(props: SizeSelectorProps) {
const [activeSize, setActiveSize] = useState<string>('Original') const [activeSize, setActiveSize] = useState<string>('Original')
const longSide: number = Math.max(originalWidth, originalHeight) const longSide: number = Math.max(originalWidth, originalHeight)
useEffect(() => {
setActiveSize('Original')
}, [originalHeight, originalWidth])
const getSizeShowName = useCallback( const getSizeShowName = useCallback(
(size: string) => { (size: string) => {
if (size === 'Original') { if (size === 'Original') {

View File

@ -1,5 +1,5 @@
import { ArrowLeftIcon } from '@heroicons/react/outline' import { ArrowLeftIcon, UploadIcon } from '@heroicons/react/outline'
import React from 'react' import React, { useState } from 'react'
import { useRecoilState } from 'recoil' import { useRecoilState } from 'recoil'
import { fileState } from '../../store/Atoms' import { fileState } from '../../store/Atoms'
import Button from '../shared/Button' import Button from '../shared/Button'
@ -11,20 +11,30 @@ import SettingIcon from '../Settings/SettingIcon'
const Header = () => { const Header = () => {
const [file, setFile] = useRecoilState(fileState) const [file, setFile] = useRecoilState(fileState)
const resolution = useResolution() const resolution = useResolution()
const [uploadElemId] = useState(`file-upload-${Math.random().toString()}`)
const renderHeader = () => { const renderHeader = () => {
return ( return (
<header> <header>
<div style={{ visibility: file ? 'visible' : 'hidden' }}> <div style={{ visibility: file ? 'visible' : 'hidden' }}>
<Button <label htmlFor={uploadElemId}>
icon={<ArrowLeftIcon />} <Button icon={<UploadIcon />} style={{ border: 0 }}>
onClick={() => { <input
setFile(undefined) style={{ display: 'none' }}
}} id={uploadElemId}
style={{ border: 0 }} name={uploadElemId}
> type="file"
{resolution === 'desktop' ? 'Start New' : undefined} onChange={ev => {
</Button> const newFile = ev.currentTarget.files?.[0]
if (newFile) {
setFile(newFile)
}
}}
accept="image/png, image/jpeg"
/>
{resolution === 'desktop' ? 'Upload New' : undefined}
</Button>
</label>
</div> </div>
<div className="header-icons-wrapper"> <div className="header-icons-wrapper">