nicecode-v2/packages/meta/lib/ImageEditor/cropper/render.js
2024-01-10 15:15:12 +08:00

165 lines
5.7 KiB
JavaScript

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);
// src/ImageEditor/cropper/render.ts
var render_exports = {};
__export(render_exports, {
default: () => render_default
});
module.exports = __toCommonJS(render_exports);
var import_css = require("rc-util/lib/Dom/css");
var import_class = require("rc-util/lib/Dom/class");
var import_utils = require("../utils");
var import_constants = require("./constants");
var render_default = {
cropBoxData: null,
viewBoxImage: null,
render() {
this.initContainer();
this.initCropBox();
this.limitCropBox(true, true);
},
initContainer() {
const { element, container } = this;
const containerData = {
width: (0, import_css.getOuterWidth)(element),
height: (0, import_css.getOuterHeight)(element)
};
this.containerData = containerData;
(0, import_css.set)(container, containerData);
},
initCropBox() {
const { containerData, options } = this;
const cropBoxData = {
// width: containerData.width,
// height: containerData.height,
width: 0,
height: 0,
left: 0,
top: 0,
minWidth: 0,
minHeight: 0,
maxWidth: containerData.width,
maxHeight: containerData.height,
minLeft: 0,
maxLeft: containerData.width,
minTop: 0,
maxTop: containerData.height,
oldLeft: 0,
oldTop: 0
};
this.cropBoxData = cropBoxData;
this.limitCropBox(true, true);
},
renderCropBox() {
const { cropBoxData } = this;
if (cropBoxData.width > cropBoxData.maxWidth || cropBoxData.width < cropBoxData.minWidth) {
cropBoxData.left = cropBoxData.oldLeft;
}
if (cropBoxData.height > cropBoxData.maxHeight || cropBoxData.height < cropBoxData.minHeight) {
cropBoxData.top = cropBoxData.oldTop;
}
cropBoxData.width = Math.min(
Math.max(cropBoxData.width, cropBoxData.minWidth),
cropBoxData.maxWidth
);
cropBoxData.height = Math.min(
Math.max(cropBoxData.height, cropBoxData.minHeight),
cropBoxData.maxHeight
);
this.limitCropBox(false, true);
cropBoxData.left = Math.min(
Math.max(cropBoxData.left, cropBoxData.minLeft),
cropBoxData.maxLeft
);
cropBoxData.top = Math.min(Math.max(cropBoxData.top, cropBoxData.minTop), cropBoxData.maxTop);
cropBoxData.oldLeft = cropBoxData.left;
cropBoxData.oldTop = cropBoxData.top;
(0, import_css.set)(
this.cropBox,
Object.assign(
{
width: cropBoxData.width,
height: cropBoxData.height
},
(0, import_utils.getTransforms)({
translateX: cropBoxData.left,
translateY: cropBoxData.top
})
)
);
if (this.options.showMask) {
this.renderPreview();
}
},
clearCropBox() {
if (this.cropped && !this.disabled) {
Object.assign(this.cropBoxData, {
left: 0,
top: 0,
width: 0,
height: 0
});
this.cropped = false;
this.renderCropBox();
(0, import_class.removeClass)(this.dragBox, import_constants.CLASS_MASK);
(0, import_class.addClass)(this.cropBox, import_constants.CLASS_HIDDEN);
}
return this;
},
limitCropBox(sizeLimited, positionLimited) {
const { options, containerData, cropBoxData, limited } = this;
const { cropBoxLimited = {} } = options;
if (sizeLimited) {
let minCropBoxWidth = Number(options.minCropBoxWidth) || 0;
let minCropBoxHeight = Number(options.minCropBoxHeight) || 0;
const maxCropBoxWidth = limited ? Math.min(
containerData.width,
cropBoxLimited.width,
cropBoxLimited.width + cropBoxLimited.left,
containerData.width - cropBoxLimited.left
) : containerData.width;
const maxCropBoxHeight = limited ? Math.min(
containerData.height,
cropBoxLimited.height,
cropBoxLimited.height + cropBoxLimited.top,
containerData.height - cropBoxLimited.top
) : containerData.height;
minCropBoxWidth = Math.min(minCropBoxWidth, containerData.width);
minCropBoxHeight = Math.min(minCropBoxHeight, containerData.height);
cropBoxData.minWidth = Math.min(minCropBoxWidth, maxCropBoxWidth);
cropBoxData.minHeight = Math.min(minCropBoxHeight, maxCropBoxHeight);
cropBoxData.maxWidth = maxCropBoxWidth;
cropBoxData.maxHeight = maxCropBoxHeight;
}
if (positionLimited) {
if (limited) {
cropBoxData.minLeft = Math.max(0, cropBoxLimited.left);
cropBoxData.minTop = Math.max(0, cropBoxLimited.top);
cropBoxData.maxLeft = Math.min(containerData.width, cropBoxLimited.left + cropBoxLimited.width) - cropBoxData.width;
cropBoxData.maxTop = Math.min(containerData.height, cropBoxLimited.top + cropBoxLimited.height) - cropBoxData.height;
} else {
cropBoxData.minLeft = 0;
cropBoxData.minTop = 0;
cropBoxData.maxLeft = containerData.width - cropBoxData.width;
cropBoxData.maxTop = containerData.height - cropBoxData.height;
}
}
}
};