1 line
21 KiB
Plaintext
1 line
21 KiB
Plaintext
|
|
{"version":3,"file":"croppingHandlers.mjs","names":[],"sources":["../../extensions/cropping_controls/croppingHandlers.ts"],"sourcesContent":["import type {\n TModificationEvents,\n TransformActionHandler,\n FabricImage,\n ObjectEvents,\n Control,\n TMat2D,\n} from 'fabric';\nimport { controlsUtils, Point, util } from 'fabric';\n\nconst { wrapWithFixedAnchor, wrapWithFireEvent } = controlsUtils;\n\n/**\n * Wraps a handler to swap behavior based on flip state.\n */\nexport const withFlip = (\n handler: TransformActionHandler,\n flippedHandler: TransformActionHandler,\n axis: 'flipX' | 'flipY',\n): TransformActionHandler => {\n return (eventData, transform, x, y) => {\n if (transform.target[axis]) {\n return flippedHandler(eventData, transform, x, y);\n }\n return handler(eventData, transform, x, y);\n };\n};\n\n/**\n * Wraps corner handlers to swap both X and Y behavior based on flip state.\n */\nexport const withCornerFlip = (\n xHandler: TransformActionHandler,\n xFlippedHandler: TransformActionHandler,\n yHandler: TransformActionHandler,\n yFlippedHandler: TransformActionHandler,\n): TransformActionHandler => {\n return (eventData, transform, x, y) => {\n const target = transform.target as FabricImage;\n const xResult = (target.flipX ? xFlippedHandler : xHandler)(\n eventData,\n transform,\n x,\n y,\n );\n const yResult = (target.flipY ? yFlippedHandler : yHandler)(\n eventData,\n transform,\n x,\n y,\n );\n return xResult || yResult;\n };\n};\n\n/**\n * Wrap controlsUtils.changeObjectWidth with image constrains\n */\nexport const changeImageWidth: TransformActionHandler = (\n eventData,\n transform,\n x,\n y,\n) => {\n const { target } = transform;\n const { width } = target;\n const image = target as FabricImage;\n const modified = controlsUtils.changeObjectWidth(eventData, transform, x, y);\n const availableWidth = image._element.width - image.cropX;\n if (modified) {\n if (image.width > availableWidth) {\n image.width = availableWidth;\n }\n if (image.width < 1) {\n image.width = 1;\n }\n }\n return width !== image.width;\n};\n\nexport const changeCropWidth = wrapWithFireEvent(\n 'CROPPING' as TModificationEvents,\n wrapWithFixedAnchor(changeImageWidth),\n);\n\n/**\n * Wrap controlsUtils.changeObjectHeight with image constrains\n */\nexport const changeImageHeight: TransformActionHandler = (\n eventData,\n transform,\n x,\n y,\n) => {\n const { target } = transform;\n const { height } = target;\n const image = target as FabricImage;\n const modified = controlsUtils.changeObjectHeight(eventData, transform, x, y);\n const availableHeight = image._element.height - image.cropY;\n if (modified) {\n if (image.height > availableHeight) {\n image.height = availableHeight;\n }\n if (image.height < 1) {\n image.height = 1;\n }\n }\n return height !== image.height;\n};\n\nexport const changeCropHeight = wrapWithFireEvent(\n 'CROPPING' as TModificationEvents,\n wrapWithFixedAnchor(changeImageHeight),\n);\n\nexport const changeImageCropX: TransformActionHandler = (\n eventData,\n transform,\n x,\n y,\n) => {\n const { target } = transform;\n const image = target as FabricImage;\n const { width, cropX } = image;\n const modified = controlsUtils.changeObjectWidth(eventData, transform, x, y);\n let newCropX = cropX + width - image.width;\n image.width = width;\n if (modified) {\n if (newCropX < 0) {\n newCropX = 0;\n }\n image.cropX = newCropX;\n // calculate new width on the base of how much crop we have now\n image.width += cropX - newCropX;\n }\n return newCropX !== cropX;\n};\n\nexport const changeImageCropY: TransformActionHandler = (\n eventData,\n transform,\n x,\n y,\n) => {\n const { target } = transform;\n const image = target as FabricImage;\n const { height, cropY } = image;\n const modified = controlsUtils.changeObjectHeight(eventData, transform, x, y);\n let newCropY = cropY + height - image.height;\n image.height =
|