tldraw
Version:
A tiny little drawing editor.
145 lines (144 loc) • 4.81 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Cropping_exports = {};
__export(Cropping_exports, {
Cropping: () => Cropping
});
module.exports = __toCommonJS(Cropping_exports);
var import_editor = require("@tldraw/editor");
var import_crop = require("../../../../../shapes/shared/crop");
var import_PointingResizeHandle = require("../../PointingResizeHandle");
class Cropping extends import_editor.StateNode {
static id = "cropping";
info = {};
markId = "";
snapshot = {};
onEnter(info) {
this.info = info;
if (typeof info.onInteractionEnd === "string") {
this.parent.setCurrentToolIdMask(info.onInteractionEnd);
}
this.markId = this.editor.markHistoryStoppingPoint("cropping");
this.snapshot = this.createSnapshot();
this.updateShapes();
}
onPointerMove() {
this.updateShapes();
}
onKeyDown() {
this.updateShapes();
}
onKeyUp() {
this.updateShapes();
}
onPointerUp() {
this.complete();
}
onComplete() {
this.complete();
}
onCancel() {
this.cancel();
}
onExit() {
this.parent.setCurrentToolIdMask(void 0);
}
updateCursor() {
const selectedShape = this.editor.getSelectedShapes()[0];
if (!selectedShape) return;
const cursorType = import_PointingResizeHandle.CursorTypeMap[this.info.handle];
this.editor.setCursor({ type: cursorType, rotation: this.editor.getSelectionRotation() });
}
updateShapes() {
const { shape, cursorHandleOffset } = this.snapshot;
if (!shape) return;
const util = this.editor.getShapeUtil(shape.type);
if (!util) return;
const shiftKey = this.editor.inputs.getShiftKey();
const currentPagePoint = this.editor.inputs.getCurrentPagePoint().clone().sub(cursorHandleOffset);
const originPagePoint = this.editor.inputs.getOriginPagePoint().clone().sub(cursorHandleOffset);
const change = currentPagePoint.clone().sub(originPagePoint).rot(-shape.rotation);
const crop = shape.props.crop ?? (0, import_crop.getDefaultCrop)();
const uncroppedSize = (0, import_crop.getUncroppedSize)(shape.props, crop);
const cropFn = util.onCrop?.bind(util) ?? import_crop.getCropBox;
const partial = cropFn(shape, {
handle: this.info.handle,
change,
crop,
uncroppedSize,
initialShape: this.snapshot.shape,
aspectRatioLocked: shiftKey
});
if (!partial) return;
this.editor.updateShapes([
{
id: shape.id,
type: shape.type,
...partial
}
]);
this.updateCursor();
}
complete() {
this.updateShapes();
(0, import_editor.kickoutOccludedShapes)(this.editor, [this.snapshot.shape.id]);
const { onInteractionEnd } = this.info;
if (onInteractionEnd) {
if (typeof onInteractionEnd === "string") {
this.editor.setCurrentTool(onInteractionEnd, this.info);
} else {
onInteractionEnd();
}
} else {
this.editor.setCroppingShape(null);
this.editor.setCurrentTool("select.idle");
}
}
cancel() {
this.editor.bailToMark(this.markId);
const { onInteractionEnd } = this.info;
if (onInteractionEnd) {
if (typeof onInteractionEnd === "string") {
this.editor.setCurrentTool(onInteractionEnd, this.info);
} else {
onInteractionEnd();
}
} else {
this.editor.setCroppingShape(null);
this.editor.setCurrentTool("select.idle");
}
}
createSnapshot() {
const selectionRotation = this.editor.getSelectionRotation();
const originPagePoint = this.editor.inputs.getOriginPagePoint();
const shape = this.editor.getOnlySelectedShape();
const selectionBounds = this.editor.getSelectionRotatedPageBounds();
const dragHandlePoint = import_editor.Vec.RotWith(
selectionBounds.getHandlePoint(this.info.handle),
selectionBounds.point,
selectionRotation
);
const cursorHandleOffset = import_editor.Vec.Sub(originPagePoint, dragHandlePoint);
return {
shape,
cursorHandleOffset
};
}
}
//# sourceMappingURL=Cropping.js.map