Skip to content

Commit

Permalink
release(cropper): v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Mar 22, 2024
1 parent a5332a0 commit acf43ff
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 16 deletions.
2 changes: 1 addition & 1 deletion index.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"react": false
},
"cropper": {
"version": "0.1.0",
"version": "0.2.0",
"style": true,
"icon": false,
"test": true,
Expand Down
6 changes: 3 additions & 3 deletions src/cropper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ import LunaCropper from 'luna-cropper'
```javascript
const container = document.getElementById('container')
const cropper = new LunaCropper(container, {
url: 'https://res.liriliri.io/luna/wallpaper.jpg',
image: 'https://res.liriliri.io/luna/wallpaper.jpg',
})
console.log(cropper.getData())
```

## Configuration

* preview(null | HTMLElement): Preview dom container.
* url(string): Image url.
* image(string): Image url.
* preview(HTMLElement): Preview dom container.

## Api

Expand Down
33 changes: 25 additions & 8 deletions src/cropper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import throttle from 'licia/throttle'
import clone from 'licia/clone'
import max from 'licia/max'
import contain from 'licia/contain'
import { drag, eventPage } from '../share/util'
import { drag, eventPage, exportCjs } from '../share/util'

const $document = $(document as any)

/** IOptions */
export interface IOptions extends IComponentOptions {
/** Image url. */
url: string
image: string
/** Preview dom container. */
preview: HTMLElement | null
preview?: HTMLElement
}

interface IImageData {
Expand Down Expand Up @@ -51,7 +51,7 @@ interface ICropBoxData {
* @example
* const container = document.getElementById('container')
* const cropper = new LunaCropper(container, {
* url: 'https://res.liriliri.io/luna/wallpaper.jpg',
* image: 'https://res.liriliri.io/luna/wallpaper.jpg',
* })
* console.log(cropper.getData())
*/
Expand Down Expand Up @@ -108,7 +108,7 @@ export default class Cropper extends Component<IOptions> {
this.initPreview(options.preview)
}

this.load(options.url)
this.load(options.image)
}
destroy() {
super.destroy()
Expand Down Expand Up @@ -179,6 +179,22 @@ export default class Cropper extends Component<IOptions> {
private bindEvent() {
this.resizeSensor.addListener(this.onResize)
this.$container.on(drag('start'), this.onCropStart)

this.on('optionChange', (name, val) => {
switch (name) {
case 'preview':
if (val) {
this.initPreview(val)
this.load(this.options.image)
} else {
this.$preview = null
}
break
case 'image':
this.load(val)
break
}
})
}
private onCropStart = (e: any) => {
e = e.origEvent
Expand Down Expand Up @@ -627,8 +643,9 @@ export default class Cropper extends Component<IOptions> {
}
}

module.exports = Cropper
module.exports.default = Cropper

const round = Math.round
const abs = Math.abs

if (typeof module !== 'undefined') {
exportCjs(module, Cropper)
}
2 changes: 1 addition & 1 deletion src/cropper/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "cropper",
"version": "0.1.0",
"version": "0.2.0",
"description": "Image cropper"
}
46 changes: 46 additions & 0 deletions src/cropper/react.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { CSSProperties, FC, useEffect, useRef } from 'react'
import Cropper, { IOptions } from './index'

interface ICropperProps extends IOptions {
style?: CSSProperties
className?: string
onCreate?: (cropper: Cropper) => void
}

const LunaCropper: FC<ICropperProps> = (props) => {
const cropperRef = useRef<HTMLDivElement>(null)
const cropper = useRef<Cropper>()

useEffect(() => {
const { image, preview } = props
cropper.current = new Cropper(cropperRef.current!, {
image,
preview,
})
props.onCreate && props.onCreate(cropper.current)

return () => cropper.current?.destroy()
}, [])

useEffect(() => {
if (cropper.current) {
cropper.current.setOption('image', props.image)
}
}, [props.image])

useEffect(() => {
if (cropper.current) {
cropper.current.setOption('preview', props.preview)
}
}, [props.preview])

return (
<div
className={props.className || ''}
style={props.style}
ref={cropperRef}
></div>
)
}

export default LunaCropper
43 changes: 40 additions & 3 deletions src/cropper/story.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'luna-cropper.css'
import Cropper from 'luna-cropper.js'
import LunaCropper from './react'
import readme from './README.md'
import story from '../share/story'
import h from 'licia/h'
import $ from 'licia/$'
import { text } from '@storybook/addon-knobs'
import { useState } from 'react'

const def = story(
'cropper',
Expand All @@ -27,10 +29,10 @@ const def = story(
wrapper.appendChild(container)
wrapper.appendChild(preview)

const url = text('Url', 'https://res.liriliri.io/luna/wallpaper.jpg')
const { image } = createKnobs()

const cropper = new Cropper(container, {
url,
image,
preview,
})

Expand All @@ -39,9 +41,44 @@ const def = story(
{
readme,
source: __STORY__,
ReactComponent() {
const { image } = createKnobs()
const [ref, setRef] = useState()

return (
<>
<LunaCropper
style={{
width: '100%',
height: 400,
aspectRatio: '2',
}}
onCreate={(cropper) => {
console.log(cropper)
}}
preview={ref}
image={image}
/>
<div
style={{ width: '50%', margin: '1rem 0' }}
ref={(ref) => {
setRef(ref)
}}
/>
</>
)
},
}
)

function createKnobs() {
const image = text('Image', 'https://res.liriliri.io/luna/wallpaper.jpg')

return {
image,
}
}

export default def

export const { cropper } = def
export const { cropper: html, react } = def

0 comments on commit acf43ff

Please sign in to comment.