tldraw
Version:
A tiny little drawing editor.
165 lines (164 loc) • 5.65 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 Rotating_exports = {};
__export(Rotating_exports, {
Rotating: () => Rotating
});
module.exports = __toCommonJS(Rotating_exports);
var import_editor = require("@tldraw/editor");
var import_PointingResizeHandle = require("./PointingResizeHandle");
const ONE_DEGREE = Math.PI / 180;
class Rotating extends import_editor.StateNode {
static id = "rotating";
snapshot = {};
info = {};
markId = "";
onEnter(info) {
this.info = info;
if (typeof info.onInteractionEnd === "string") {
this.parent.setCurrentToolIdMask(info.onInteractionEnd);
}
this.markId = this.editor.markHistoryStoppingPoint("rotate start");
const snapshot = (0, import_editor.getRotationSnapshot)({
editor: this.editor,
ids: this.editor.getSelectedShapeIds()
});
if (!snapshot) {
this.parent.transition("idle", this.info);
return;
}
this.snapshot = snapshot;
const newSelectionRotation = this._getRotationFromPointerPosition({
snapToNearestDegree: false
});
(0, import_editor.applyRotationToSnapshotShapes)({
editor: this.editor,
delta: this._getRotationFromPointerPosition({ snapToNearestDegree: false }),
snapshot: this.snapshot,
stage: "start"
});
this.editor.setCursor({
type: import_PointingResizeHandle.CursorTypeMap[this.info.handle],
rotation: newSelectionRotation + this.snapshot.initialShapesRotation
});
}
onExit() {
this.editor.setCursor({ type: "default", rotation: 0 });
this.parent.setCurrentToolIdMask(void 0);
this.snapshot = {};
}
onPointerMove() {
this.update();
}
onKeyDown() {
this.update();
}
onKeyUp() {
this.update();
}
onPointerUp() {
this.complete();
}
onComplete() {
this.complete();
}
onCancel() {
this.cancel();
}
// ---
update() {
const newSelectionRotation = this._getRotationFromPointerPosition({
snapToNearestDegree: false
});
(0, import_editor.applyRotationToSnapshotShapes)({
editor: this.editor,
delta: newSelectionRotation,
snapshot: this.snapshot,
stage: "update"
});
this.editor.setCursor({
type: import_PointingResizeHandle.CursorTypeMap[this.info.handle],
rotation: newSelectionRotation + this.snapshot.initialShapesRotation
});
}
cancel() {
const { shapeSnapshots } = this.snapshot;
shapeSnapshots.forEach(({ shape }) => {
const current = this.editor.getShape(shape.id);
if (current) {
const util = this.editor.getShapeUtil(shape);
util.onRotateCancel?.(shape, current);
}
});
this.editor.bailToMark(this.markId);
const { onInteractionEnd } = this.info;
if (onInteractionEnd) {
if (typeof onInteractionEnd === "string") {
this.editor.setCurrentTool(onInteractionEnd, this.info);
} else {
onInteractionEnd();
}
return;
}
this.parent.transition("idle", this.info);
}
complete() {
(0, import_editor.applyRotationToSnapshotShapes)({
editor: this.editor,
delta: this._getRotationFromPointerPosition({ snapToNearestDegree: true }),
snapshot: this.snapshot,
stage: "end"
});
(0, import_editor.kickoutOccludedShapes)(
this.editor,
this.snapshot.shapeSnapshots.map((s) => s.shape.id)
);
const { onInteractionEnd } = this.info;
if (onInteractionEnd) {
if (typeof onInteractionEnd === "string") {
this.editor.setCurrentTool(onInteractionEnd, this.info);
} else {
onInteractionEnd();
}
return;
}
this.parent.transition("idle", this.info);
}
_getRotationFromPointerPosition({ snapToNearestDegree }) {
const shiftKey = this.editor.inputs.getShiftKey();
const currentPagePoint = this.editor.inputs.getCurrentPagePoint();
const { initialCursorAngle, initialShapesRotation, initialPageCenter } = this.snapshot;
const preSnapRotationDelta = initialPageCenter.angle(currentPagePoint) - initialCursorAngle;
let newSelectionRotation = initialShapesRotation + preSnapRotationDelta;
if (shiftKey) {
newSelectionRotation = (0, import_editor.snapAngle)(newSelectionRotation, 24);
} else if (snapToNearestDegree) {
newSelectionRotation = Math.round(newSelectionRotation / ONE_DEGREE) * ONE_DEGREE;
if (this.editor.getInstanceState().isCoarsePointer) {
const snappedToRightAngle = (0, import_editor.snapAngle)(newSelectionRotation, 4);
const angleToRightAngle = (0, import_editor.shortAngleDist)(newSelectionRotation, snappedToRightAngle);
if (Math.abs(angleToRightAngle) < (0, import_editor.degreesToRadians)(5)) {
newSelectionRotation = snappedToRightAngle;
}
}
}
return newSelectionRotation - initialShapesRotation;
}
}
//# sourceMappingURL=Rotating.js.map