Skip to content

Commit

Permalink
Media editor minor improvements
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Perez <60019601+josh-signal@users.noreply.github.com>
  • Loading branch information
automated-signal and josh-signal committed Dec 3, 2021
1 parent b2a9bcd commit 4e5bab7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
11 changes: 11 additions & 0 deletions ts/components/MediaEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ export const MediaEditor = ({

const [canRedo, setCanRedo] = useState(false);
const [canUndo, setCanUndo] = useState(false);
const [canCrop, setCanCrop] = useState(false);
const [cropAspectRatioLock, setcropAspectRatioLock] = useState(false);
const [drawTool, setDrawTool] = useState<DrawTool>(DrawTool.Pen);
const [drawWidth, setDrawWidth] = useState<DrawWidth>(DrawWidth.Regular);
Expand Down Expand Up @@ -572,6 +573,13 @@ export const MediaEditor = ({
width,
});

rect.on('modified', () => {
const { height: currHeight, width: currWidth } =
rect.getBoundingRect(true);

setCanCrop(currHeight < height || currWidth < width);
});

rect.on('deselected', () => {
setEditMode(undefined);
});
Expand All @@ -589,6 +597,8 @@ export const MediaEditor = ({
}
});
}

setCanCrop(false);
}, [editMode, fabricCanvas, imageState.height, imageState.width, zoom]);

useEffect(() => {
Expand Down Expand Up @@ -857,6 +867,7 @@ export const MediaEditor = ({
<button
aria-label={i18n('MediaEditor__crop--crop')}
className="MediaEditor__crop-toolbar--button MediaEditor__crop-toolbar--crop"
disabled={!canCrop}
onClick={() => {
if (!fabricCanvas) {
return;
Expand Down
4 changes: 4 additions & 0 deletions ts/mediaEditor/MediaEditorFabricCropRect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ MediaEditorFabricCropRect.prototype.controls = {
x: -0.5,
y: -0.5,
actionHandler: fabric.controlsUtils.scalingEqually,
cursorStyle: 'nwse-resize',
render: (
ctx: CanvasRenderingContext2D,
left: number,
Expand All @@ -111,6 +112,7 @@ MediaEditorFabricCropRect.prototype.controls = {
x: 0.5,
y: -0.5,
actionHandler: fabric.controlsUtils.scalingEqually,
cursorStyle: 'nesw-resize',
render: (
ctx: CanvasRenderingContext2D,
left: number,
Expand All @@ -137,6 +139,7 @@ MediaEditorFabricCropRect.prototype.controls = {
x: -0.5,
y: 0.5,
actionHandler: fabric.controlsUtils.scalingEqually,
cursorStyle: 'nesw-resize',
render: (
ctx: CanvasRenderingContext2D,
left: number,
Expand All @@ -163,6 +166,7 @@ MediaEditorFabricCropRect.prototype.controls = {
x: 0.5,
y: 0.5,
actionHandler: fabric.controlsUtils.scalingEqually,
cursorStyle: 'nwse-resize',
render: (
ctx: CanvasRenderingContext2D,
left: number,
Expand Down
11 changes: 8 additions & 3 deletions ts/mediaEditor/MediaEditorFabricPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
// SPDX-License-Identifier: AGPL-3.0-only

import { fabric } from 'fabric';
import { customFabricObjectControls } from './util/customFabricObjectControls';

export class MediaEditorFabricPath extends fabric.Path {
constructor(
path?: string | Array<fabric.Point>,
options?: fabric.IPathOptions
) {
super(path, { fill: undefined, lockScalingFlip: true, ...(options || {}) });
super(path, {
evented: false,
fill: undefined,
hasControls: false,
lockScalingFlip: true,
selectable: false,
...(options || {}),
});
}

static override fromObject(
Expand All @@ -26,4 +32,3 @@ export class MediaEditorFabricPath extends fabric.Path {

MediaEditorFabricPath.prototype.type = 'MediaEditorFabricPath';
MediaEditorFabricPath.prototype.borderColor = '#ffffff';
MediaEditorFabricPath.prototype.controls = customFabricObjectControls;
8 changes: 4 additions & 4 deletions ts/mediaEditor/util/customFabricObjectControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const resizeControl = new fabric.Control({
cursorStyleHandler: () => 'se-resize',
render: (ctx: CanvasRenderingContext2D, left: number, top: number) => {
// circle
const size = 9;
const size = 12;
ctx.save();
ctx.fillStyle = '#fff';
ctx.strokeStyle = '#fff';
Expand All @@ -18,7 +18,7 @@ const resizeControl = new fabric.Control({
ctx.fill();

// arrows NW & SE
const arrowSize = 4;
const arrowSize = 5;
ctx.fillStyle = '#3b3b3b';
ctx.strokeStyle = '#3b3b3b';
ctx.beginPath();
Expand Down Expand Up @@ -96,7 +96,7 @@ const deleteControl = new fabric.Control({
},
render: (ctx: CanvasRenderingContext2D, left: number, top: number) => {
// circle
const size = 9;
const size = 12;
ctx.save();
ctx.fillStyle = '#000';
ctx.strokeStyle = '#000';
Expand All @@ -106,7 +106,7 @@ const deleteControl = new fabric.Control({
ctx.fill();

// x
const xSize = 3;
const xSize = 4;
ctx.fillStyle = '#fff';
ctx.strokeStyle = '#fff';
ctx.beginPath();
Expand Down

0 comments on commit 4e5bab7

Please sign in to comment.