refactor: decouple slider
This commit is contained in:
parent
adaf4f78c4
commit
22870aff1b
|
@ -52,8 +52,7 @@
|
||||||
"@pixi/text": "^5.3.9",
|
"@pixi/text": "^5.3.9",
|
||||||
"image-size": "^0.9.3",
|
"image-size": "^0.9.3",
|
||||||
"kefir": "^3.8.8",
|
"kefir": "^3.8.8",
|
||||||
"lru-cache": "^6.0.0",
|
"lru-cache": "^6.0.0"
|
||||||
"rc-slider": "^9.7.1"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@flecks/fleck": "^1.4.1"
|
"@flecks/fleck": "^1.4.1"
|
||||||
|
|
|
@ -8,28 +8,18 @@ import {
|
||||||
useEffect,
|
useEffect,
|
||||||
useImperativeHandle,
|
useImperativeHandle,
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
|
||||||
} from '@flecks/react';
|
} from '@flecks/react';
|
||||||
import K from 'kefir';
|
import K from 'kefir';
|
||||||
import Slider from 'rc-slider';
|
|
||||||
|
|
||||||
const marks = {
|
|
||||||
1: '1x',
|
|
||||||
2: '2x',
|
|
||||||
4: '4x',
|
|
||||||
8: '8x',
|
|
||||||
};
|
|
||||||
|
|
||||||
const Stage = forwardRef(({
|
const Stage = forwardRef(({
|
||||||
centered,
|
centered,
|
||||||
renderable,
|
renderable,
|
||||||
renderer,
|
renderer,
|
||||||
scalable,
|
scale,
|
||||||
size,
|
size,
|
||||||
styles,
|
styles,
|
||||||
ticker,
|
ticker,
|
||||||
}, fref) => {
|
}, fref) => {
|
||||||
const [scale, setScale] = useState(1);
|
|
||||||
const hostRef = useRef();
|
const hostRef = useRef();
|
||||||
useImperativeHandle(fref, () => ({
|
useImperativeHandle(fref, () => ({
|
||||||
events: () => K.merge(
|
events: () => K.merge(
|
||||||
|
@ -50,14 +40,14 @@ const Stage = forwardRef(({
|
||||||
event,
|
event,
|
||||||
// Normalize touch events.
|
// Normalize touch events.
|
||||||
(event) => {
|
(event) => {
|
||||||
|
/* eslint-disable no-param-reassign */
|
||||||
if (-1 !== ['touchstart', 'touchmove'].indexOf(event.type)) {
|
if (-1 !== ['touchstart', 'touchmove'].indexOf(event.type)) {
|
||||||
/* eslint-disable no-param-reassign */
|
|
||||||
event.clientX = event.touches[0].clientX;
|
event.clientX = event.touches[0].clientX;
|
||||||
event.deltaX = event.touches[0].deltaX;
|
event.deltaX = event.touches[0].deltaX;
|
||||||
event.clientY = event.touches[0].clientY;
|
event.clientY = event.touches[0].clientY;
|
||||||
event.deltaY = event.touches[0].deltaY;
|
event.deltaY = event.touches[0].deltaY;
|
||||||
/* eslint-enable no-param-reassign */
|
|
||||||
}
|
}
|
||||||
|
/* eslint-enable no-param-reassign */
|
||||||
return event;
|
return event;
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -92,24 +82,10 @@ const Stage = forwardRef(({
|
||||||
current.removeChild(renderer.element);
|
current.removeChild(renderer.element);
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
if (scalable) {
|
|
||||||
const wheelEvents = K.fromEvents(renderer.element, 'wheel');
|
|
||||||
const onWheel = (event) => {
|
|
||||||
if (event.ctrlKey) {
|
|
||||||
event.preventDefault();
|
|
||||||
const increment = event.deltaY > 0 ? 0.5 : 2;
|
|
||||||
setScale(Math.min(8, Math.max(1, scale * increment)));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
wheelEvents.onValue(onWheel);
|
|
||||||
cleanups.push(() => {
|
|
||||||
wheelEvents.offValue(onWheel);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return () => {
|
return () => {
|
||||||
cleanups.forEach((fn) => fn());
|
cleanups.forEach((fn) => fn());
|
||||||
};
|
};
|
||||||
}, [hostRef, renderer, scalable, scale, setScale]);
|
}, [hostRef, renderer]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!renderable || !renderer) {
|
if (!renderable || !renderer) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -136,18 +112,6 @@ const Stage = forwardRef(({
|
||||||
}, [renderable, renderer, ticker]);
|
}, [renderable, renderer, ticker]);
|
||||||
return (
|
return (
|
||||||
<div className={styles.stage}>
|
<div className={styles.stage}>
|
||||||
{scalable && (
|
|
||||||
<Slider
|
|
||||||
min={1}
|
|
||||||
max={8}
|
|
||||||
marks={marks}
|
|
||||||
step={null}
|
|
||||||
onChange={(scale) => {
|
|
||||||
setScale(scale);
|
|
||||||
}}
|
|
||||||
value={scale}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
<div className={styles.canvas} ref={hostRef} />
|
<div className={styles.canvas} ref={hostRef} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
@ -156,7 +120,7 @@ const Stage = forwardRef(({
|
||||||
Stage.defaultProps = {
|
Stage.defaultProps = {
|
||||||
centered: true,
|
centered: true,
|
||||||
renderer: new Renderer([0, 0]),
|
renderer: new Renderer([0, 0]),
|
||||||
scalable: true,
|
scale: 1,
|
||||||
styles: {
|
styles: {
|
||||||
canvas: '',
|
canvas: '',
|
||||||
stage: '',
|
stage: '',
|
||||||
|
@ -177,7 +141,7 @@ Stage.propTypes = {
|
||||||
render: PropTypes.func,
|
render: PropTypes.func,
|
||||||
resize: PropTypes.func,
|
resize: PropTypes.func,
|
||||||
}),
|
}),
|
||||||
scalable: PropTypes.bool,
|
scale: PropTypes.number,
|
||||||
size: PropTypes.arrayOf(PropTypes.number).isRequired,
|
size: PropTypes.arrayOf(PropTypes.number).isRequired,
|
||||||
styles: PropTypes.shape({
|
styles: PropTypes.shape({
|
||||||
canvas: PropTypes.string,
|
canvas: PropTypes.string,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user