Suggestions from code review

This commit is contained in:
Jason Miller
2021-06-07 14:59:26 -04:00
parent 543c2e73fb
commit 7918938d8a
6 changed files with 8 additions and 85 deletions

View File

@@ -1,5 +1,5 @@
import { h, Component, createRef } from 'preact'; import { h, Component, createRef } from 'preact';
import { drawDataToCanvas } from '../util'; import { drawDataToCanvas } from '../util/canvas';
export interface CanvasImageProps export interface CanvasImageProps
extends h.JSX.HTMLAttributes<HTMLCanvasElement> { extends h.JSX.HTMLAttributes<HTMLCanvasElement> {

View File

@@ -67,7 +67,6 @@ export default class Cropper extends Component<Props, State> {
componentWillReceiveProps({ crop }: Props, nextState: State) { componentWillReceiveProps({ crop }: Props, nextState: State) {
const current = nextState.crop || this.state.crop; const current = nextState.crop || this.state.crop;
if (crop !== this.props.crop && !shallowEqual(crop, current)) { if (crop !== this.props.crop && !shallowEqual(crop, current)) {
// this.setState({ crop: nextProps.crop });
this.setCrop(crop); this.setCrop(crop);
} }
} }
@@ -257,8 +256,8 @@ export default class Cropper extends Component<Props, State> {
} }
componentWillUnmount() { componentWillUnmount() {
addEventListener('keydown', this.onKeyDown); removeEventListener('keydown', this.onKeyDown);
addEventListener('keyup', this.onKeyUp); removeEventListener('keyup', this.onKeyUp);
} }
render({ size }: Props, { crop, pan }: State) { render({ size }: Props, { crop, pan }: State) {

View File

@@ -35,15 +35,7 @@
/* Accept pointer input even though this is unpainted transparent */ /* Accept pointer input even though this is unpainted transparent */
pointer-events: all; pointer-events: all;
cursor: move; cursor: move;
/* animation: ants 1s linear forwards infinite; */
} }
/*
@keyframes ants {
0% { stroke-dashoffset: 0; }
100% { stroke-dashoffset: -12; }
}
*/
.edge { .edge {
fill: #aaa; fill: #aaa;

View File

@@ -395,7 +395,6 @@ export default class Transform extends Component<Props, State> {
onChange={this.onPinchZoomChange} onChange={this.onPinchZoomChange}
ref={this.pinchZoom} ref={this.pinchZoom}
> >
{/* <Backdrop width={displayWidth} height={displayHeight} /> */}
<div <div
class={style.wrap} class={style.wrap}
style={{ style={{
@@ -569,49 +568,3 @@ const SaveButton = ({ onClick }: { onClick: () => void }) => (
<CheckmarkIcon class={style.icon} /> <CheckmarkIcon class={style.icon} />
</button> </button>
); );
interface BackdropProps {
width: number;
height: number;
}
/** @TODO this could at least use clip-path. It's too expensive this way. */
class Backdrop extends Component<BackdropProps> {
shouldComponentUpdate({ width, height }: BackdropProps) {
return width !== this.props.width || height !== this.props.height;
}
render({ width, height }: BackdropProps) {
const transform =
`transform-origin: 50% 50%; transform: translate(var(--x), var(--y)) ` +
`translate(-${width / 2}px, -${height / 2}px) ` +
`scale(calc(var(--scale, 1) * 0.99999));`;
return (
<svg
class={style.backdrop}
preserveAspectRatio="xMidYMid meet"
width="100%"
height="100%"
shape-rendering="optimizeSpeed"
>
<mask id="bgmask">
<rect width="100%" height="100%" fill="white" />
<rect
style={transform}
width={width}
height={height}
x="50%"
y="50%"
fill="black"
/>
</mask>
<rect
class={style.backdropArea}
width="100%"
height="100%"
mask="url(#bgmask)"
/>
</svg>
);
}
}

View File

@@ -14,25 +14,6 @@
will-change: initial !important; will-change: initial !important;
} }
.backdrop {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
transform: none !important;
will-change: initial !important;
contain: strict;
& * {
contain: strict;
}
}
.backdropArea {
fill: rgba(0, 0, 0, 0.25);
}
.pinch-zoom { .pinch-zoom {
composes: abs-fill from global; composes: abs-fill from global;
outline: none; outline: none;

View File

@@ -138,7 +138,7 @@ async function preprocessImage(
let processedData = data; let processedData = data;
const { rotate, flip, crop } = preprocessorState; const { rotate, flip, crop } = preprocessorState;
if (flip && (flip.horizontal || flip.vertical)) { if (flip.horizontal || flip.vertical) {
processedData = await workerBridge.flip(signal, processedData, flip); processedData = await workerBridge.flip(signal, processedData, flip);
} }
@@ -146,7 +146,7 @@ async function preprocessImage(
processedData = await workerBridge.rotate(signal, processedData, rotate); processedData = await workerBridge.rotate(signal, processedData, rotate);
} }
if (crop && (crop.left || crop.top || crop.right || crop.bottom)) { if (crop.left || crop.top || crop.right || crop.bottom) {
processedData = await workerBridge.crop(signal, processedData, crop); processedData = await workerBridge.crop(signal, processedData, crop);
} }
@@ -292,8 +292,6 @@ export default class Compress extends Component<Props, State> {
state: State = { state: State = {
source: undefined, source: undefined,
loading: false, loading: false,
/** @TODO remove this */
// transform: true,
transform: false, transform: false,
preprocessorState: defaultPreprocessorState, preprocessorState: defaultPreprocessorState,
sides: [ sides: [
@@ -393,7 +391,7 @@ export default class Compress extends Component<Props, State> {
this.setState({ transform: true }); this.setState({ transform: true });
}; };
private onTransformUpdated = ({ private onTransformCommit = ({
preprocessorState, preprocessorState,
}: { preprocessorState?: PreprocessorState } = {}) => { }: { preprocessorState?: PreprocessorState } = {}) => {
if (preprocessorState) { if (preprocessorState) {
@@ -1000,8 +998,8 @@ export default class Compress extends Component<Props, State> {
mobileView={mobileView} mobileView={mobileView}
source={source!} source={source!}
preprocessorState={preprocessorState!} preprocessorState={preprocessorState!}
onSave={this.onTransformUpdated} onSave={this.onTransformCommit}
onCancel={this.onTransformUpdated} onCancel={this.onTransformCommit}
/> />
)} )}
</div> </div>