@sprawlify/primitives 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/anatomy.cjs +3 -0
- package/dist/anatomy.d.cts +2 -0
- package/dist/anatomy.d.mts +2 -0
- package/dist/anatomy.mjs +3 -0
- package/dist/aria-hidden-DqYx0f_y.mjs +177 -0
- package/dist/aria-hidden-DtLU6hSU.cjs +182 -0
- package/dist/aria-hidden.cjs +4 -0
- package/dist/aria-hidden.d.cts +16 -0
- package/dist/aria-hidden.d.mts +16 -0
- package/dist/aria-hidden.mjs +4 -0
- package/dist/auto-resize-D3qHtFy1.mjs +114 -0
- package/dist/auto-resize-xzlPFDyD.cjs +125 -0
- package/dist/auto-resize.cjs +5 -0
- package/dist/auto-resize.d.cts +17 -0
- package/dist/auto-resize.d.mts +17 -0
- package/dist/auto-resize.mjs +4 -0
- package/dist/chunk-CbDLau6x.cjs +34 -0
- package/dist/collection-8XqaNHs_.mjs +1279 -0
- package/dist/collection-DIoIDISa.cjs +1326 -0
- package/dist/collection.cjs +11 -0
- package/dist/collection.d.cts +5 -0
- package/dist/collection.d.mts +5 -0
- package/dist/collection.mjs +4 -0
- package/dist/color-BkAP1Tkd.d.mts +60 -0
- package/dist/color-VKewNv2e.d.cts +60 -0
- package/dist/color-utils-Cc5GVXZh.mjs +657 -0
- package/dist/color-utils-LER_6Zwu.cjs +680 -0
- package/dist/color-utils.cjs +7 -0
- package/dist/color-utils.d.cts +19 -0
- package/dist/color-utils.d.mts +19 -0
- package/dist/color-utils.mjs +4 -0
- package/dist/core-30pJjyC2.d.mts +49 -0
- package/dist/core-CQFbMdTl.cjs +176 -0
- package/dist/core-CefBjbst.d.cts +49 -0
- package/dist/core-DCFNMTZF.mjs +129 -0
- package/dist/core.cjs +12 -0
- package/dist/core.d.cts +3 -0
- package/dist/core.d.mts +3 -0
- package/dist/core.mjs +5 -0
- package/dist/create-anatomy-B3XYc-qG.d.mts +70 -0
- package/dist/create-anatomy-BGQ8pv8J.d.cts +70 -0
- package/dist/create-anatomy-BbDZbHpC.mjs +39 -0
- package/dist/create-anatomy-CDsIFUXg.cjs +45 -0
- package/dist/create-props-BnF_vl-E.mjs +5 -0
- package/dist/create-props-YKy7JIKj.cjs +11 -0
- package/dist/data-url-C3ITPVRX.d.cts +10 -0
- package/dist/data-url-DLwZ9M77.d.mts +10 -0
- package/dist/date-utils-D2xPHa_I.cjs +738 -0
- package/dist/date-utils-wEa1hhpc.mjs +474 -0
- package/dist/date-utils.cjs +46 -0
- package/dist/date-utils.d.cts +101 -0
- package/dist/date-utils.d.mts +2 -0
- package/dist/date-utils.mjs +3 -0
- package/dist/defineProperty-BoUOT9Ie.cjs +49 -0
- package/dist/defineProperty-DrXfyjd4.mjs +43 -0
- package/dist/dismissable-CTfKZ4OG.cjs +282 -0
- package/dist/dismissable-DDrx0xnH.mjs +271 -0
- package/dist/dismissable-layer-C7pZU2wi.d.cts +39 -0
- package/dist/dismissable-layer-t_b0fk-p.d.mts +39 -0
- package/dist/dismissable.cjs +7 -0
- package/dist/dismissable.d.cts +3 -0
- package/dist/dismissable.d.mts +3 -0
- package/dist/dismissable.mjs +6 -0
- package/dist/dom-query-CWZdWUGp.cjs +2141 -0
- package/dist/dom-query-DFCRhyj1.mjs +1374 -0
- package/dist/dom-query.cjs +130 -0
- package/dist/dom-query.d.cts +307 -0
- package/dist/dom-query.d.mts +307 -0
- package/dist/dom-query.mjs +3 -0
- package/dist/file-utils.cjs +220 -0
- package/dist/file-utils.d.cts +51 -0
- package/dist/file-utils.d.mts +51 -0
- package/dist/file-utils.mjs +210 -0
- package/dist/focus-trap-D-bypQEJ.mjs +485 -0
- package/dist/focus-trap-D4i52S9A.cjs +496 -0
- package/dist/focus-trap.cjs +5 -0
- package/dist/focus-trap.d.cts +88 -0
- package/dist/focus-trap.d.mts +88 -0
- package/dist/focus-trap.mjs +4 -0
- package/dist/focus-visible-DYTWdWpa.cjs +210 -0
- package/dist/focus-visible-irppYatv.mjs +175 -0
- package/dist/focus-visible.cjs +9 -0
- package/dist/focus-visible.d.cts +31 -0
- package/dist/focus-visible.d.mts +31 -0
- package/dist/focus-visible.mjs +4 -0
- package/dist/functions-BOkTDEFM.d.cts +15 -0
- package/dist/functions-IZtWcC8Q.d.mts +15 -0
- package/dist/highlight-word.cjs +100 -0
- package/dist/highlight-word.d.cts +24 -0
- package/dist/highlight-word.d.mts +24 -0
- package/dist/highlight-word.mjs +99 -0
- package/dist/hotkeys.cjs +758 -0
- package/dist/hotkeys.d.cts +130 -0
- package/dist/hotkeys.d.mts +130 -0
- package/dist/hotkeys.mjs +754 -0
- package/dist/i18n-utils.cjs +450 -0
- package/dist/i18n-utils.d.cts +61 -0
- package/dist/i18n-utils.d.mts +61 -0
- package/dist/i18n-utils.mjs +440 -0
- package/dist/index-BkzYij2j.d.mts +25 -0
- package/dist/index-Bqw3r34-.d.cts +1383 -0
- package/dist/index-C0MKXQmE.d.mts +1383 -0
- package/dist/index-C9-feK5c.d.cts +25 -0
- package/dist/index-CD5FN7kY.d.mts +114 -0
- package/dist/index-CJji6wHl.d.cts +15 -0
- package/dist/index-CNi5FkPe.d.mts +15 -0
- package/dist/index-D96rs8B0.d.mts +115 -0
- package/dist/index.cjs +0 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +1 -0
- package/dist/interact-outside-BplxR9cR.mjs +200 -0
- package/dist/interact-outside-C8-oFBpB.cjs +205 -0
- package/dist/interact-outside.cjs +5 -0
- package/dist/interact-outside.d.cts +2 -0
- package/dist/interact-outside.d.mts +2 -0
- package/dist/interact-outside.mjs +5 -0
- package/dist/json-tree-utils.cjs +1512 -0
- package/dist/json-tree-utils.d.cts +117 -0
- package/dist/json-tree-utils.d.mts +117 -0
- package/dist/json-tree-utils.mjs +1463 -0
- package/dist/list-collection-BIMJGQjn.d.cts +61 -0
- package/dist/list-collection-Db3eixRU.d.mts +61 -0
- package/dist/live-region-CcZPGo89.cjs +52 -0
- package/dist/live-region-CnPh21WH.mjs +46 -0
- package/dist/live-region.cjs +3 -0
- package/dist/live-region.d.cts +2 -0
- package/dist/live-region.d.mts +2 -0
- package/dist/live-region.mjs +3 -0
- package/dist/machines/accordion/index.cjs +328 -0
- package/dist/machines/accordion/index.d.cts +83 -0
- package/dist/machines/accordion/index.d.mts +83 -0
- package/dist/machines/accordion/index.mjs +322 -0
- package/dist/machines/async-list/index.cjs +311 -0
- package/dist/machines/async-list/index.d.cts +89 -0
- package/dist/machines/async-list/index.d.mts +89 -0
- package/dist/machines/async-list/index.mjs +310 -0
- package/dist/machines/avatar/index.cjs +169 -0
- package/dist/machines/avatar/index.d.cts +63 -0
- package/dist/machines/avatar/index.d.mts +63 -0
- package/dist/machines/avatar/index.mjs +165 -0
- package/dist/machines/bottom-sheet/index.cjs +676 -0
- package/dist/machines/bottom-sheet/index.d.cts +239 -0
- package/dist/machines/bottom-sheet/index.d.mts +239 -0
- package/dist/machines/bottom-sheet/index.mjs +672 -0
- package/dist/machines/carousel/index.cjs +859 -0
- package/dist/machines/carousel/index.d.cts +146 -0
- package/dist/machines/carousel/index.d.mts +146 -0
- package/dist/machines/carousel/index.mjs +851 -0
- package/dist/machines/checkbox/index.cjs +316 -0
- package/dist/machines/checkbox/index.d.cts +81 -0
- package/dist/machines/checkbox/index.d.mts +81 -0
- package/dist/machines/checkbox/index.mjs +312 -0
- package/dist/machines/clipboard/index.cjs +225 -0
- package/dist/machines/clipboard/index.d.cts +68 -0
- package/dist/machines/clipboard/index.d.mts +68 -0
- package/dist/machines/clipboard/index.mjs +219 -0
- package/dist/machines/collapsible/index.cjs +362 -0
- package/dist/machines/collapsible/index.d.cts +82 -0
- package/dist/machines/collapsible/index.d.mts +82 -0
- package/dist/machines/collapsible/index.mjs +358 -0
- package/dist/machines/color-picker/index.cjs +1529 -0
- package/dist/machines/color-picker/index.d.cts +205 -0
- package/dist/machines/color-picker/index.d.mts +205 -0
- package/dist/machines/color-picker/index.mjs +1514 -0
- package/dist/machines/combobox/index.cjs +1593 -0
- package/dist/machines/combobox/index.d.cts +214 -0
- package/dist/machines/combobox/index.d.mts +214 -0
- package/dist/machines/combobox/index.mjs +1582 -0
- package/dist/machines/date-picker/index.cjs +2177 -0
- package/dist/machines/date-picker/index.d.cts +350 -0
- package/dist/machines/date-picker/index.d.mts +350 -0
- package/dist/machines/date-picker/index.mjs +2161 -0
- package/dist/machines/dialog/index.cjs +325 -0
- package/dist/machines/dialog/index.d.cts +80 -0
- package/dist/machines/dialog/index.d.mts +80 -0
- package/dist/machines/dialog/index.mjs +321 -0
- package/dist/machines/editable/index.cjs +551 -0
- package/dist/machines/editable/index.d.cts +114 -0
- package/dist/machines/editable/index.d.mts +114 -0
- package/dist/machines/editable/index.mjs +547 -0
- package/dist/machines/floating-panel/index.cjs +995 -0
- package/dist/machines/floating-panel/index.d.cts +147 -0
- package/dist/machines/floating-panel/index.d.mts +147 -0
- package/dist/machines/floating-panel/index.mjs +988 -0
- package/dist/machines/hover-card/index.cjs +383 -0
- package/dist/machines/hover-card/index.d.cts +69 -0
- package/dist/machines/hover-card/index.d.mts +69 -0
- package/dist/machines/hover-card/index.mjs +379 -0
- package/dist/machines/image-cropper/index.cjs +1995 -0
- package/dist/machines/image-cropper/index.d.cts +182 -0
- package/dist/machines/image-cropper/index.d.mts +182 -0
- package/dist/machines/image-cropper/index.mjs +1990 -0
- package/dist/machines/listbox/index.cjs +794 -0
- package/dist/machines/listbox/index.d.cts +168 -0
- package/dist/machines/listbox/index.d.mts +168 -0
- package/dist/machines/listbox/index.mjs +782 -0
- package/dist/machines/marquee/index.cjs +395 -0
- package/dist/machines/marquee/index.d.cts +102 -0
- package/dist/machines/marquee/index.d.mts +102 -0
- package/dist/machines/marquee/index.mjs +391 -0
- package/dist/machines/menu/index.cjs +1298 -0
- package/dist/machines/menu/index.d.cts +175 -0
- package/dist/machines/menu/index.d.mts +175 -0
- package/dist/machines/menu/index.mjs +1286 -0
- package/dist/machines/navigation-menu/index.cjs +861 -0
- package/dist/machines/navigation-menu/index.d.cts +127 -0
- package/dist/machines/navigation-menu/index.d.mts +127 -0
- package/dist/machines/navigation-menu/index.mjs +857 -0
- package/dist/machines/number-input/index.cjs +996 -0
- package/dist/machines/number-input/index.d.cts +134 -0
- package/dist/machines/number-input/index.d.mts +134 -0
- package/dist/machines/number-input/index.mjs +991 -0
- package/dist/machines/pagination/index.cjs +419 -0
- package/dist/machines/pagination/index.d.cts +139 -0
- package/dist/machines/pagination/index.d.mts +139 -0
- package/dist/machines/pagination/index.mjs +411 -0
- package/dist/machines/password-input/index.cjs +228 -0
- package/dist/machines/password-input/index.d.cts +71 -0
- package/dist/machines/password-input/index.d.mts +71 -0
- package/dist/machines/password-input/index.mjs +224 -0
- package/dist/machines/pin-input/index.cjs +523 -0
- package/dist/machines/pin-input/index.d.cts +105 -0
- package/dist/machines/pin-input/index.d.mts +105 -0
- package/dist/machines/pin-input/index.mjs +519 -0
- package/dist/machines/popover/index.cjs +430 -0
- package/dist/machines/popover/index.d.cts +88 -0
- package/dist/machines/popover/index.d.mts +88 -0
- package/dist/machines/popover/index.mjs +426 -0
- package/dist/machines/presence/index.cjs +192 -0
- package/dist/machines/presence/index.d.cts +44 -0
- package/dist/machines/presence/index.d.mts +44 -0
- package/dist/machines/presence/index.mjs +190 -0
- package/dist/machines/progress/index.cjs +288 -0
- package/dist/machines/progress/index.d.cts +96 -0
- package/dist/machines/progress/index.d.mts +96 -0
- package/dist/machines/progress/index.mjs +284 -0
- package/dist/machines/qr-code/index.cjs +172 -0
- package/dist/machines/qr-code/index.d.cts +70 -0
- package/dist/machines/qr-code/index.d.mts +70 -0
- package/dist/machines/qr-code/index.mjs +167 -0
- package/dist/machines/radio-group/index.cjs +436 -0
- package/dist/machines/radio-group/index.d.cts +106 -0
- package/dist/machines/radio-group/index.d.mts +106 -0
- package/dist/machines/radio-group/index.mjs +430 -0
- package/dist/machines/rating-group/index.cjs +405 -0
- package/dist/machines/rating-group/index.d.cts +101 -0
- package/dist/machines/rating-group/index.d.mts +101 -0
- package/dist/machines/rating-group/index.mjs +399 -0
- package/dist/machines/scroll-area/index.cjs +857 -0
- package/dist/machines/scroll-area/index.d.cts +159 -0
- package/dist/machines/scroll-area/index.d.mts +159 -0
- package/dist/machines/scroll-area/index.mjs +853 -0
- package/dist/machines/select/index.cjs +1165 -0
- package/dist/machines/select/index.d.cts +183 -0
- package/dist/machines/select/index.d.mts +183 -0
- package/dist/machines/select/index.mjs +1154 -0
- package/dist/machines/signature-pad/index.cjs +334 -0
- package/dist/machines/signature-pad/index.d.cts +108 -0
- package/dist/machines/signature-pad/index.d.mts +108 -0
- package/dist/machines/signature-pad/index.mjs +328 -0
- package/dist/machines/slider/index.cjs +957 -0
- package/dist/machines/slider/index.d.cts +149 -0
- package/dist/machines/slider/index.d.mts +149 -0
- package/dist/machines/slider/index.mjs +949 -0
- package/dist/machines/splitter/index.cjs +1112 -0
- package/dist/machines/splitter/index.d.cts +155 -0
- package/dist/machines/splitter/index.d.mts +155 -0
- package/dist/machines/splitter/index.mjs +1103 -0
- package/dist/machines/steps/index.cjs +289 -0
- package/dist/machines/steps/index.d.cts +97 -0
- package/dist/machines/steps/index.d.mts +97 -0
- package/dist/machines/steps/index.mjs +285 -0
- package/dist/machines/switch/index.cjs +305 -0
- package/dist/machines/switch/index.d.cts +80 -0
- package/dist/machines/switch/index.d.mts +80 -0
- package/dist/machines/switch/index.mjs +301 -0
- package/dist/machines/tabs/index.cjs +508 -0
- package/dist/machines/tabs/index.d.cts +111 -0
- package/dist/machines/tabs/index.d.mts +111 -0
- package/dist/machines/tabs/index.mjs +500 -0
- package/dist/machines/tags-input/index.cjs +1127 -0
- package/dist/machines/tags-input/index.d.cts +179 -0
- package/dist/machines/tags-input/index.d.mts +179 -0
- package/dist/machines/tags-input/index.mjs +1121 -0
- package/dist/machines/timer/index.cjs +329 -0
- package/dist/machines/timer/index.d.cts +98 -0
- package/dist/machines/timer/index.d.mts +98 -0
- package/dist/machines/timer/index.mjs +324 -0
- package/dist/machines/toast/index.cjs +1155 -0
- package/dist/machines/toast/index.d.cts +195 -0
- package/dist/machines/toast/index.d.mts +195 -0
- package/dist/machines/toast/index.mjs +1151 -0
- package/dist/machines/toggle/index.cjs +103 -0
- package/dist/machines/toggle/index.d.cts +46 -0
- package/dist/machines/toggle/index.d.mts +46 -0
- package/dist/machines/toggle/index.mjs +99 -0
- package/dist/machines/toggle-group/index.cjs +328 -0
- package/dist/machines/toggle-group/index.d.cts +81 -0
- package/dist/machines/toggle-group/index.d.mts +81 -0
- package/dist/machines/toggle-group/index.mjs +322 -0
- package/dist/machines/tooltip/index.cjs +516 -0
- package/dist/machines/tooltip/index.d.cts +71 -0
- package/dist/machines/tooltip/index.d.mts +71 -0
- package/dist/machines/tooltip/index.mjs +512 -0
- package/dist/machines/tour/index.cjs +1108 -0
- package/dist/machines/tour/index.d.cts +206 -0
- package/dist/machines/tour/index.d.mts +206 -0
- package/dist/machines/tour/index.mjs +1101 -0
- package/dist/machines/tree-view/index.cjs +1271 -0
- package/dist/machines/tree-view/index.d.cts +215 -0
- package/dist/machines/tree-view/index.d.mts +215 -0
- package/dist/machines/tree-view/index.mjs +1263 -0
- package/dist/node-BctU8GXk.d.mts +24 -0
- package/dist/node-CSsuPZVZ.d.cts +24 -0
- package/dist/popper-BPJeAtcA.mjs +349 -0
- package/dist/popper-D0FAW_p7.cjs +373 -0
- package/dist/popper.cjs +8 -0
- package/dist/popper.d.cts +74 -0
- package/dist/popper.d.mts +2 -0
- package/dist/popper.mjs +5 -0
- package/dist/raf-BnlYUlDi.d.mts +15 -0
- package/dist/raf-Cd2FGy0z.d.cts +15 -0
- package/dist/rect-utils-0ellN0a_.mjs +854 -0
- package/dist/rect-utils-DKLRhP8G.cjs +1147 -0
- package/dist/rect-utils.cjs +51 -0
- package/dist/rect-utils.d.cts +198 -0
- package/dist/rect-utils.d.mts +198 -0
- package/dist/rect-utils.mjs +3 -0
- package/dist/remove-scroll-Lrfv79X_.mjs +58 -0
- package/dist/remove-scroll-Uvzg1L9r.cjs +63 -0
- package/dist/remove-scroll.cjs +0 -0
- package/dist/remove-scroll.d.cts +1 -0
- package/dist/remove-scroll.d.mts +1 -0
- package/dist/remove-scroll.mjs +1 -0
- package/dist/scroll-snap-CrTmQRzA.mjs +152 -0
- package/dist/scroll-snap-zyiZLv7W.cjs +175 -0
- package/dist/scroll-snap.cjs +7 -0
- package/dist/scroll-snap.d.cts +14 -0
- package/dist/scroll-snap.d.mts +14 -0
- package/dist/scroll-snap.mjs +4 -0
- package/dist/selection-BIhSzkF7.d.mts +50 -0
- package/dist/selection-CS1GBp8e.d.cts +50 -0
- package/dist/store-BZcXv49B.cjs +305 -0
- package/dist/store-DEojWy9H.mjs +263 -0
- package/dist/store.cjs +9 -0
- package/dist/store.d.cts +26 -0
- package/dist/store.d.mts +26 -0
- package/dist/store.mjs +3 -0
- package/dist/stringify-state.cjs +49 -0
- package/dist/stringify-state.d.cts +8 -0
- package/dist/stringify-state.d.mts +8 -0
- package/dist/stringify-state.mjs +46 -0
- package/dist/tree-collection-BwU5WhGQ.d.mts +78 -0
- package/dist/tree-collection-DP_eznnI.d.cts +78 -0
- package/dist/typeahead-BdNwVP09.d.cts +45 -0
- package/dist/typeahead-XN6lZ7G9.d.mts +45 -0
- package/dist/types-BivBkYg9.d.cts +57 -0
- package/dist/types-Bl_6JtPQ.d.mts +57 -0
- package/dist/types-CPUMVt7c.d.mts +54 -0
- package/dist/types-CT_W6HWr.d.cts +54 -0
- package/dist/types-DDTcG99l.d.mts +201 -0
- package/dist/types-DejIu60O.d.cts +201 -0
- package/dist/types-DiIdKZ3K.d.cts +45 -0
- package/dist/types-GxLIgJib.d.cts +17 -0
- package/dist/types.cjs +15 -0
- package/dist/types.d.cts +2 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +14 -0
- package/dist/utils-BZyrxWWR.mjs +525 -0
- package/dist/utils-_6frwjgJ.cjs +1040 -0
- package/dist/utils.cjs +88 -0
- package/dist/utils.d.cts +144 -0
- package/dist/utils.d.mts +144 -0
- package/dist/utils.mjs +3 -0
- package/dist/wait-for-CMjPsqWk.d.cts +10 -0
- package/dist/wait-for-D2nkdD4z.d.mts +10 -0
- package/package.json +399 -0
|
@@ -0,0 +1,1990 @@
|
|
|
1
|
+
import { t as createAnatomy } from "../../create-anatomy-BbDZbHpC.mjs";
|
|
2
|
+
import { Xt as contains, ft as getEventTarget, lt as getEventKey, p as resizeObserverBorderBox, st as addDomEvent, ut as getEventPoint, vn as dataAttr } from "../../dom-query-DFCRhyj1.mjs";
|
|
3
|
+
import { H as toPx, W as callAll, g as clampValue, nt as isBoolean, u as createSplitProps } from "../../utils-BZyrxWWR.mjs";
|
|
4
|
+
import { a as createMachine } from "../../core-DCFNMTZF.mjs";
|
|
5
|
+
import { t as createProps } from "../../create-props-BnF_vl-E.mjs";
|
|
6
|
+
|
|
7
|
+
//#region src/machines/image-cropper/image-cropper.anatomy.ts
|
|
8
|
+
const anatomy = createAnatomy("image-cropper").parts("root", "viewport", "image", "selection", "handle", "grid");
|
|
9
|
+
const parts = anatomy.build();
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/machines/image-cropper/get-resize-axis-style.ts
|
|
13
|
+
function getHandlePositionStyles(handlePosition) {
|
|
14
|
+
switch (handlePosition) {
|
|
15
|
+
case "n": return {
|
|
16
|
+
position: "absolute",
|
|
17
|
+
cursor: "n-resize",
|
|
18
|
+
width: "96%",
|
|
19
|
+
top: 0,
|
|
20
|
+
left: "50%",
|
|
21
|
+
translate: "-50% -50%"
|
|
22
|
+
};
|
|
23
|
+
case "e": return {
|
|
24
|
+
position: "absolute",
|
|
25
|
+
cursor: "e-resize",
|
|
26
|
+
height: "96%",
|
|
27
|
+
right: 0,
|
|
28
|
+
top: "50%",
|
|
29
|
+
translate: "50% -50%"
|
|
30
|
+
};
|
|
31
|
+
case "s": return {
|
|
32
|
+
position: "absolute",
|
|
33
|
+
cursor: "s-resize",
|
|
34
|
+
width: "96%",
|
|
35
|
+
bottom: 0,
|
|
36
|
+
left: "50%",
|
|
37
|
+
translate: "-50% 50%"
|
|
38
|
+
};
|
|
39
|
+
case "w": return {
|
|
40
|
+
position: "absolute",
|
|
41
|
+
cursor: "w-resize",
|
|
42
|
+
height: "96%",
|
|
43
|
+
left: 0,
|
|
44
|
+
top: "50%",
|
|
45
|
+
translate: "-50% -50%"
|
|
46
|
+
};
|
|
47
|
+
case "se": return {
|
|
48
|
+
position: "absolute",
|
|
49
|
+
cursor: "se-resize",
|
|
50
|
+
bottom: 0,
|
|
51
|
+
right: 0,
|
|
52
|
+
translate: "50% 50%"
|
|
53
|
+
};
|
|
54
|
+
case "sw": return {
|
|
55
|
+
position: "absolute",
|
|
56
|
+
cursor: "sw-resize",
|
|
57
|
+
bottom: 0,
|
|
58
|
+
left: 0,
|
|
59
|
+
translate: "-50% 50%"
|
|
60
|
+
};
|
|
61
|
+
case "ne": return {
|
|
62
|
+
position: "absolute",
|
|
63
|
+
cursor: "ne-resize",
|
|
64
|
+
top: 0,
|
|
65
|
+
right: 0,
|
|
66
|
+
translate: "50% -50%"
|
|
67
|
+
};
|
|
68
|
+
case "nw": return {
|
|
69
|
+
position: "absolute",
|
|
70
|
+
cursor: "nw-resize",
|
|
71
|
+
top: 0,
|
|
72
|
+
left: 0,
|
|
73
|
+
translate: "-50% -50%"
|
|
74
|
+
};
|
|
75
|
+
default: throw new Error(`Invalid handlePosition: ${handlePosition}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/machines/image-cropper/image-cropper.dom.ts
|
|
81
|
+
const getRootId = (ctx) => ctx.ids?.root ?? `image-cropper:${ctx.id}`;
|
|
82
|
+
const getViewportId = (ctx) => ctx.ids?.viewport ?? `image-cropper:${ctx.id}:viewport`;
|
|
83
|
+
const getImageId = (ctx) => ctx.ids?.image ?? `image-cropper:${ctx.id}:image`;
|
|
84
|
+
const getSelectionId = (ctx) => ctx.ids?.selection ?? `image-cropper:${ctx.id}:selection`;
|
|
85
|
+
const getHandleId = (ctx, position) => ctx.ids?.handle?.(position) ?? `image-cropper:${ctx.id}:handle:${position}`;
|
|
86
|
+
const getRootEl = (ctx) => ctx.getById(getRootId(ctx));
|
|
87
|
+
const getViewportEl = (ctx) => ctx.getById(getViewportId(ctx));
|
|
88
|
+
const getImageEl = (ctx) => ctx.getById(getImageId(ctx));
|
|
89
|
+
const getSelectionEl = (ctx) => ctx.getById(getSelectionId(ctx));
|
|
90
|
+
function drawCroppedImageToCanvas(params) {
|
|
91
|
+
const { context, scope } = params;
|
|
92
|
+
const imageEl = getImageEl(scope);
|
|
93
|
+
if (!imageEl || !imageEl.complete) return null;
|
|
94
|
+
const doc = imageEl.ownerDocument;
|
|
95
|
+
const crop = context.get("crop");
|
|
96
|
+
const zoom = context.get("zoom");
|
|
97
|
+
const rotation = context.get("rotation");
|
|
98
|
+
const flip = context.get("flip");
|
|
99
|
+
const viewportRect = context.get("viewportRect");
|
|
100
|
+
const naturalSize = context.get("naturalSize");
|
|
101
|
+
const offset = context.get("offset");
|
|
102
|
+
const canvas = doc.createElement("canvas");
|
|
103
|
+
canvas.width = crop.width;
|
|
104
|
+
canvas.height = crop.height;
|
|
105
|
+
const ctx = canvas.getContext("2d");
|
|
106
|
+
if (!ctx) return null;
|
|
107
|
+
ctx.save();
|
|
108
|
+
ctx.translate(canvas.width / 2, canvas.height / 2);
|
|
109
|
+
ctx.rotate(rotation * Math.PI / 180);
|
|
110
|
+
const scaleX = flip.horizontal ? -1 : 1;
|
|
111
|
+
const scaleY = flip.vertical ? -1 : 1;
|
|
112
|
+
ctx.scale(scaleX, scaleY);
|
|
113
|
+
const viewportCenterX = viewportRect.width / 2;
|
|
114
|
+
const viewportCenterY = viewportRect.height / 2;
|
|
115
|
+
const cropCenterX = crop.x + crop.width / 2;
|
|
116
|
+
const cropCenterY = crop.y + crop.height / 2;
|
|
117
|
+
const deltaX = cropCenterX - viewportCenterX;
|
|
118
|
+
const deltaY = cropCenterY - viewportCenterY;
|
|
119
|
+
const imageCenterX = naturalSize.width / 2;
|
|
120
|
+
const imageCenterY = naturalSize.height / 2;
|
|
121
|
+
const sourceX = imageCenterX + (deltaX - offset.x) / zoom;
|
|
122
|
+
const sourceY = imageCenterY + (deltaY - offset.y) / zoom;
|
|
123
|
+
const sourceWidth = crop.width / zoom;
|
|
124
|
+
const sourceHeight = crop.height / zoom;
|
|
125
|
+
ctx.drawImage(imageEl, sourceX - sourceWidth / 2, sourceY - sourceHeight / 2, sourceWidth, sourceHeight, -canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height);
|
|
126
|
+
ctx.restore();
|
|
127
|
+
return canvas;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/machines/image-cropper/image-cropper.utils.ts
|
|
132
|
+
const { min, max, abs, round, hypot, PI, cos, sin } = Math;
|
|
133
|
+
const isLeftHandle = (handlePosition) => handlePosition === "w" || handlePosition === "nw" || handlePosition === "sw";
|
|
134
|
+
const isRightHandle = (handlePosition) => handlePosition === "e" || handlePosition === "ne" || handlePosition === "se";
|
|
135
|
+
const isTopHandle = (handlePosition) => handlePosition === "n" || handlePosition === "nw" || handlePosition === "ne";
|
|
136
|
+
const isBottomHandle = (handlePosition) => handlePosition === "s" || handlePosition === "sw" || handlePosition === "se";
|
|
137
|
+
const isCornerHandle = (handlePosition) => (isLeftHandle(handlePosition) || isRightHandle(handlePosition)) && (isTopHandle(handlePosition) || isBottomHandle(handlePosition));
|
|
138
|
+
const isHorizontalEdgeHandle = (handlePosition) => (isLeftHandle(handlePosition) || isRightHandle(handlePosition)) && !(isTopHandle(handlePosition) || isBottomHandle(handlePosition));
|
|
139
|
+
const isVerticalEdgeHandle = (handlePosition) => (isTopHandle(handlePosition) || isBottomHandle(handlePosition)) && !(isLeftHandle(handlePosition) || isRightHandle(handlePosition));
|
|
140
|
+
const hasAspectRatio = (value) => typeof value === "number" && value > 0;
|
|
141
|
+
const resolveSizeLimits = (options) => {
|
|
142
|
+
const { minSize, maxSize, viewportSize, aspectRatio } = options;
|
|
143
|
+
let minWidth = min(minSize.width, viewportSize.width);
|
|
144
|
+
let minHeight = min(minSize.height, viewportSize.height);
|
|
145
|
+
let maxWidth = maxSize?.width ?? viewportSize.width;
|
|
146
|
+
if (!Number.isFinite(maxWidth)) maxWidth = viewportSize.width;
|
|
147
|
+
maxWidth = min(maxWidth, viewportSize.width);
|
|
148
|
+
let maxHeight = maxSize?.height ?? viewportSize.height;
|
|
149
|
+
if (!Number.isFinite(maxHeight)) maxHeight = viewportSize.height;
|
|
150
|
+
maxHeight = min(maxHeight, viewportSize.height);
|
|
151
|
+
maxWidth = max(minWidth, maxWidth);
|
|
152
|
+
maxHeight = max(minHeight, maxHeight);
|
|
153
|
+
const hasAspect = hasAspectRatio(aspectRatio);
|
|
154
|
+
if (hasAspect) {
|
|
155
|
+
const minWidthWithAspect = max(minWidth, minHeight * aspectRatio);
|
|
156
|
+
const minHeightWithAspect = minWidthWithAspect / aspectRatio;
|
|
157
|
+
minWidth = min(minWidthWithAspect, viewportSize.width);
|
|
158
|
+
minHeight = min(minHeightWithAspect, viewportSize.height);
|
|
159
|
+
let constrainedMaxWidth = min(maxWidth, maxHeight * aspectRatio, viewportSize.width);
|
|
160
|
+
let constrainedMaxHeight = constrainedMaxWidth / aspectRatio;
|
|
161
|
+
if (constrainedMaxHeight > maxHeight || constrainedMaxHeight > viewportSize.height) {
|
|
162
|
+
constrainedMaxHeight = min(maxHeight, viewportSize.height);
|
|
163
|
+
constrainedMaxWidth = constrainedMaxHeight * aspectRatio;
|
|
164
|
+
}
|
|
165
|
+
maxWidth = max(minWidth, min(constrainedMaxWidth, viewportSize.width));
|
|
166
|
+
maxHeight = max(minHeight, min(constrainedMaxHeight, viewportSize.height));
|
|
167
|
+
} else {
|
|
168
|
+
maxWidth = max(minWidth, min(maxWidth, viewportSize.width));
|
|
169
|
+
maxHeight = max(minHeight, min(maxHeight, viewportSize.height));
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
minWidth,
|
|
173
|
+
minHeight,
|
|
174
|
+
maxWidth,
|
|
175
|
+
maxHeight,
|
|
176
|
+
hasAspect
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
const clampAspectSize = (params) => {
|
|
180
|
+
const { widthValue, heightValue, limits, viewportRect, aspectRatio } = params;
|
|
181
|
+
const { minWidth, minHeight, maxWidth, maxHeight } = limits;
|
|
182
|
+
const constrainWidthFromHeight = (height) => {
|
|
183
|
+
let width = clampValue(height * aspectRatio, minWidth, maxWidth);
|
|
184
|
+
width = min(width, viewportRect.width);
|
|
185
|
+
return {
|
|
186
|
+
width,
|
|
187
|
+
height: width / aspectRatio
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
const clampByWidth = (value) => {
|
|
191
|
+
let width = clampValue(value, minWidth, maxWidth);
|
|
192
|
+
width = min(width, viewportRect.width);
|
|
193
|
+
let height = width / aspectRatio;
|
|
194
|
+
if (height < minHeight) {
|
|
195
|
+
const constrained = constrainWidthFromHeight(minHeight);
|
|
196
|
+
width = constrained.width;
|
|
197
|
+
height = constrained.height;
|
|
198
|
+
}
|
|
199
|
+
if (height > maxHeight) {
|
|
200
|
+
const constrained = constrainWidthFromHeight(min(maxHeight, viewportRect.height));
|
|
201
|
+
width = constrained.width;
|
|
202
|
+
height = constrained.height;
|
|
203
|
+
}
|
|
204
|
+
if (height > viewportRect.height) {
|
|
205
|
+
const constrained = constrainWidthFromHeight(viewportRect.height);
|
|
206
|
+
width = constrained.width;
|
|
207
|
+
height = constrained.height;
|
|
208
|
+
if (height < minHeight) {
|
|
209
|
+
const reconstrainted = constrainWidthFromHeight(minHeight);
|
|
210
|
+
width = reconstrainted.width;
|
|
211
|
+
height = reconstrainted.height;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
width,
|
|
216
|
+
height
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
const clampByHeight = (value) => {
|
|
220
|
+
let height = clampValue(value, minHeight, maxHeight);
|
|
221
|
+
height = min(height, viewportRect.height);
|
|
222
|
+
let width = height * aspectRatio;
|
|
223
|
+
width = clampValue(width, minWidth, maxWidth);
|
|
224
|
+
width = min(width, viewportRect.width);
|
|
225
|
+
let adjustedHeight = width / aspectRatio;
|
|
226
|
+
if (adjustedHeight < minHeight) {
|
|
227
|
+
const constrained = constrainWidthFromHeight(minHeight);
|
|
228
|
+
width = constrained.width;
|
|
229
|
+
adjustedHeight = constrained.height;
|
|
230
|
+
}
|
|
231
|
+
if (adjustedHeight > maxHeight) {
|
|
232
|
+
const constrained = constrainWidthFromHeight(min(maxHeight, viewportRect.height));
|
|
233
|
+
width = constrained.width;
|
|
234
|
+
adjustedHeight = constrained.height;
|
|
235
|
+
}
|
|
236
|
+
if (width > viewportRect.width) {
|
|
237
|
+
width = viewportRect.width;
|
|
238
|
+
adjustedHeight = width / aspectRatio;
|
|
239
|
+
if (adjustedHeight > maxHeight) {
|
|
240
|
+
const constrained = constrainWidthFromHeight(min(maxHeight, viewportRect.height));
|
|
241
|
+
width = constrained.width;
|
|
242
|
+
adjustedHeight = constrained.height;
|
|
243
|
+
}
|
|
244
|
+
if (adjustedHeight < minHeight) {
|
|
245
|
+
const constrained = constrainWidthFromHeight(minHeight);
|
|
246
|
+
width = constrained.width;
|
|
247
|
+
adjustedHeight = constrained.height;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
width,
|
|
252
|
+
height: adjustedHeight
|
|
253
|
+
};
|
|
254
|
+
};
|
|
255
|
+
const byWidth = clampByWidth(widthValue);
|
|
256
|
+
const byHeight = clampByHeight(heightValue);
|
|
257
|
+
const deltaWidth = abs(byWidth.width - widthValue) + abs(byWidth.height - heightValue);
|
|
258
|
+
return abs(byHeight.width - widthValue) + abs(byHeight.height - heightValue) < deltaWidth ? byHeight : byWidth;
|
|
259
|
+
};
|
|
260
|
+
const applyDeltaToEdges = (params) => {
|
|
261
|
+
const { bounds, delta, handlePosition, viewportRect, minSize, maxSize } = params;
|
|
262
|
+
let { left, top, right, bottom } = bounds;
|
|
263
|
+
if (isLeftHandle(handlePosition)) {
|
|
264
|
+
const minLeft = max(0, right - maxSize.width);
|
|
265
|
+
const maxLeft = right - minSize.width;
|
|
266
|
+
left = clampValue(left + delta.x, minLeft, maxLeft);
|
|
267
|
+
}
|
|
268
|
+
if (isRightHandle(handlePosition)) {
|
|
269
|
+
const minRight = left + minSize.width;
|
|
270
|
+
const maxRight = min(viewportRect.width, left + maxSize.width);
|
|
271
|
+
right = clampValue(right + delta.x, minRight, maxRight);
|
|
272
|
+
}
|
|
273
|
+
if (isTopHandle(handlePosition)) {
|
|
274
|
+
const minTop = max(0, bottom - maxSize.height);
|
|
275
|
+
const maxTop = bottom - minSize.height;
|
|
276
|
+
top = clampValue(top + delta.y, minTop, maxTop);
|
|
277
|
+
}
|
|
278
|
+
if (isBottomHandle(handlePosition)) {
|
|
279
|
+
const minBottom = top + minSize.height;
|
|
280
|
+
const maxBottom = min(viewportRect.height, top + maxSize.height);
|
|
281
|
+
bottom = clampValue(bottom + delta.y, minBottom, maxBottom);
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
left,
|
|
285
|
+
top,
|
|
286
|
+
right,
|
|
287
|
+
bottom
|
|
288
|
+
};
|
|
289
|
+
};
|
|
290
|
+
const applyAspectToHorizontalResize = (params) => {
|
|
291
|
+
const { bounds, limits, viewportRect, aspectRatio, handlePosition } = params;
|
|
292
|
+
const { left, top, right, bottom } = bounds;
|
|
293
|
+
const centerY = (top + bottom) / 2;
|
|
294
|
+
let nextWidth = right - left;
|
|
295
|
+
let nextHeight = nextWidth / aspectRatio;
|
|
296
|
+
const constrained = clampAspectSize({
|
|
297
|
+
widthValue: nextWidth,
|
|
298
|
+
heightValue: nextHeight,
|
|
299
|
+
limits,
|
|
300
|
+
viewportRect,
|
|
301
|
+
aspectRatio
|
|
302
|
+
});
|
|
303
|
+
nextWidth = constrained.width;
|
|
304
|
+
nextHeight = constrained.height;
|
|
305
|
+
const halfH = nextHeight / 2;
|
|
306
|
+
let newTop = centerY - halfH;
|
|
307
|
+
let newBottom = centerY + halfH;
|
|
308
|
+
if (newTop < 0) {
|
|
309
|
+
newTop = 0;
|
|
310
|
+
newBottom = nextHeight;
|
|
311
|
+
}
|
|
312
|
+
if (newBottom > viewportRect.height) {
|
|
313
|
+
newBottom = viewportRect.height;
|
|
314
|
+
newTop = newBottom - nextHeight;
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
left: isRightHandle(handlePosition) ? left : right - nextWidth,
|
|
318
|
+
top: newTop,
|
|
319
|
+
right: isRightHandle(handlePosition) ? left + nextWidth : right,
|
|
320
|
+
bottom: newBottom
|
|
321
|
+
};
|
|
322
|
+
};
|
|
323
|
+
const applyAspectToVerticalResize = (params) => {
|
|
324
|
+
const { bounds, limits, viewportRect, aspectRatio, handlePosition } = params;
|
|
325
|
+
const { left, top, right, bottom } = bounds;
|
|
326
|
+
const centerX = (left + right) / 2;
|
|
327
|
+
let nextHeight = bottom - top;
|
|
328
|
+
let nextWidth = nextHeight * aspectRatio;
|
|
329
|
+
const constrained = clampAspectSize({
|
|
330
|
+
widthValue: nextWidth,
|
|
331
|
+
heightValue: nextHeight,
|
|
332
|
+
limits,
|
|
333
|
+
viewportRect,
|
|
334
|
+
aspectRatio
|
|
335
|
+
});
|
|
336
|
+
nextWidth = constrained.width;
|
|
337
|
+
nextHeight = constrained.height;
|
|
338
|
+
const halfW = nextWidth / 2;
|
|
339
|
+
let newLeft = centerX - halfW;
|
|
340
|
+
let newRight = centerX + halfW;
|
|
341
|
+
if (newLeft < 0) {
|
|
342
|
+
newLeft = 0;
|
|
343
|
+
newRight = nextWidth;
|
|
344
|
+
}
|
|
345
|
+
if (newRight > viewportRect.width) {
|
|
346
|
+
newRight = viewportRect.width;
|
|
347
|
+
newLeft = newRight - nextWidth;
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
left: newLeft,
|
|
351
|
+
top: isBottomHandle(handlePosition) ? top : bottom - nextHeight,
|
|
352
|
+
right: newRight,
|
|
353
|
+
bottom: isBottomHandle(handlePosition) ? top + nextHeight : bottom
|
|
354
|
+
};
|
|
355
|
+
};
|
|
356
|
+
const applyCornerResize = (params) => {
|
|
357
|
+
const { bounds, width, height, handlePosition } = params;
|
|
358
|
+
const { left, top, right, bottom } = bounds;
|
|
359
|
+
if (isRightHandle(handlePosition) && isBottomHandle(handlePosition)) return {
|
|
360
|
+
left,
|
|
361
|
+
top,
|
|
362
|
+
right: left + width,
|
|
363
|
+
bottom: top + height
|
|
364
|
+
};
|
|
365
|
+
else if (isRightHandle(handlePosition) && isTopHandle(handlePosition)) return {
|
|
366
|
+
left,
|
|
367
|
+
top: bottom - height,
|
|
368
|
+
right: left + width,
|
|
369
|
+
bottom
|
|
370
|
+
};
|
|
371
|
+
else if (isBottomHandle(handlePosition)) return {
|
|
372
|
+
left: right - width,
|
|
373
|
+
top,
|
|
374
|
+
right,
|
|
375
|
+
bottom: top + height
|
|
376
|
+
};
|
|
377
|
+
else return {
|
|
378
|
+
left: right - width,
|
|
379
|
+
top: bottom - height,
|
|
380
|
+
right,
|
|
381
|
+
bottom
|
|
382
|
+
};
|
|
383
|
+
};
|
|
384
|
+
function computeResizeCrop(options) {
|
|
385
|
+
const { cropStart, handlePosition, delta, viewportRect, minSize, maxSize, aspectRatio } = options;
|
|
386
|
+
let { x, y, width, height } = cropStart;
|
|
387
|
+
let left = x;
|
|
388
|
+
let top = y;
|
|
389
|
+
let right = x + width;
|
|
390
|
+
let bottom = y + height;
|
|
391
|
+
const { minWidth, minHeight, maxWidth, maxHeight, hasAspect } = resolveSizeLimits({
|
|
392
|
+
minSize,
|
|
393
|
+
maxSize,
|
|
394
|
+
viewportSize: viewportRect,
|
|
395
|
+
aspectRatio
|
|
396
|
+
});
|
|
397
|
+
const edgesAfterDelta = applyDeltaToEdges({
|
|
398
|
+
bounds: {
|
|
399
|
+
left,
|
|
400
|
+
top,
|
|
401
|
+
right,
|
|
402
|
+
bottom
|
|
403
|
+
},
|
|
404
|
+
delta,
|
|
405
|
+
handlePosition,
|
|
406
|
+
viewportRect,
|
|
407
|
+
minSize,
|
|
408
|
+
maxSize
|
|
409
|
+
});
|
|
410
|
+
left = edgesAfterDelta.left;
|
|
411
|
+
top = edgesAfterDelta.top;
|
|
412
|
+
right = edgesAfterDelta.right;
|
|
413
|
+
bottom = edgesAfterDelta.bottom;
|
|
414
|
+
if (hasAspect) {
|
|
415
|
+
const limits = {
|
|
416
|
+
minWidth,
|
|
417
|
+
minHeight,
|
|
418
|
+
maxWidth,
|
|
419
|
+
maxHeight,
|
|
420
|
+
hasAspect
|
|
421
|
+
};
|
|
422
|
+
if (isCornerHandle(handlePosition)) {
|
|
423
|
+
let tempW = right - left;
|
|
424
|
+
let tempH = tempW / aspectRatio;
|
|
425
|
+
if (tempH > bottom - top || top + tempH > viewportRect.height || left + tempW > viewportRect.width) {
|
|
426
|
+
tempH = bottom - top;
|
|
427
|
+
tempW = tempH * aspectRatio;
|
|
428
|
+
}
|
|
429
|
+
const constrained = clampAspectSize({
|
|
430
|
+
widthValue: tempW,
|
|
431
|
+
heightValue: tempH,
|
|
432
|
+
limits,
|
|
433
|
+
viewportRect,
|
|
434
|
+
aspectRatio
|
|
435
|
+
});
|
|
436
|
+
const result = applyCornerResize({
|
|
437
|
+
bounds: {
|
|
438
|
+
left,
|
|
439
|
+
top,
|
|
440
|
+
right,
|
|
441
|
+
bottom
|
|
442
|
+
},
|
|
443
|
+
width: constrained.width,
|
|
444
|
+
height: constrained.height,
|
|
445
|
+
handlePosition
|
|
446
|
+
});
|
|
447
|
+
left = result.left;
|
|
448
|
+
top = result.top;
|
|
449
|
+
right = result.right;
|
|
450
|
+
bottom = result.bottom;
|
|
451
|
+
} else if (isHorizontalEdgeHandle(handlePosition)) {
|
|
452
|
+
const result = applyAspectToHorizontalResize({
|
|
453
|
+
bounds: {
|
|
454
|
+
left,
|
|
455
|
+
top,
|
|
456
|
+
right,
|
|
457
|
+
bottom
|
|
458
|
+
},
|
|
459
|
+
limits,
|
|
460
|
+
viewportRect,
|
|
461
|
+
aspectRatio,
|
|
462
|
+
handlePosition
|
|
463
|
+
});
|
|
464
|
+
left = result.left;
|
|
465
|
+
top = result.top;
|
|
466
|
+
right = result.right;
|
|
467
|
+
bottom = result.bottom;
|
|
468
|
+
} else if (isVerticalEdgeHandle(handlePosition)) {
|
|
469
|
+
const result = applyAspectToVerticalResize({
|
|
470
|
+
bounds: {
|
|
471
|
+
left,
|
|
472
|
+
top,
|
|
473
|
+
right,
|
|
474
|
+
bottom
|
|
475
|
+
},
|
|
476
|
+
limits,
|
|
477
|
+
viewportRect,
|
|
478
|
+
aspectRatio,
|
|
479
|
+
handlePosition
|
|
480
|
+
});
|
|
481
|
+
left = result.left;
|
|
482
|
+
top = result.top;
|
|
483
|
+
right = result.right;
|
|
484
|
+
bottom = result.bottom;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
const maxLeft = max(0, viewportRect.width - minWidth);
|
|
488
|
+
const maxTop = max(0, viewportRect.height - minHeight);
|
|
489
|
+
left = clampValue(left, 0, maxLeft);
|
|
490
|
+
top = clampValue(top, 0, maxTop);
|
|
491
|
+
const maxRight = min(viewportRect.width, left + maxWidth);
|
|
492
|
+
const maxBottom = min(viewportRect.height, top + maxHeight);
|
|
493
|
+
right = clampValue(right, left + minWidth, maxRight);
|
|
494
|
+
bottom = clampValue(bottom, top + minHeight, maxBottom);
|
|
495
|
+
return {
|
|
496
|
+
x: left,
|
|
497
|
+
y: top,
|
|
498
|
+
width: right - left,
|
|
499
|
+
height: bottom - top
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
function computeMoveCrop(cropStart, delta, viewportRect) {
|
|
503
|
+
return {
|
|
504
|
+
x: clampValue(cropStart.x + delta.x, 0, viewportRect.width - cropStart.width),
|
|
505
|
+
y: clampValue(cropStart.y + delta.y, 0, viewportRect.height - cropStart.height),
|
|
506
|
+
width: cropStart.width,
|
|
507
|
+
height: cropStart.height
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
function clampOffset(params) {
|
|
511
|
+
const { zoom, rotation, viewportSize, offset, fixedCropArea, crop, naturalSize } = params;
|
|
512
|
+
const { cos: cos$1, sin: sin$1 } = getRotationTransform(rotation);
|
|
513
|
+
if (fixedCropArea && crop && naturalSize) {
|
|
514
|
+
const aabb$1 = computeAABB(naturalSize, zoom, cos$1, sin$1);
|
|
515
|
+
const center = getViewportCenter(viewportSize);
|
|
516
|
+
const cropRight = crop.x + crop.width;
|
|
517
|
+
const cropBottom = crop.y + crop.height;
|
|
518
|
+
return clampPoint(offset, {
|
|
519
|
+
x: cropRight - center.x - aabb$1.width / 2,
|
|
520
|
+
y: cropBottom - center.y - aabb$1.height / 2
|
|
521
|
+
}, {
|
|
522
|
+
x: crop.x - center.x + aabb$1.width / 2,
|
|
523
|
+
y: crop.y - center.y + aabb$1.height / 2
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
const aabb = computeAABB(viewportSize, zoom, cos$1, sin$1);
|
|
527
|
+
const extraWidth = max(0, aabb.width - viewportSize.width);
|
|
528
|
+
const extraHeight = max(0, aabb.height - viewportSize.height);
|
|
529
|
+
return clampPoint(offset, {
|
|
530
|
+
x: -extraWidth / 2,
|
|
531
|
+
y: -extraHeight / 2
|
|
532
|
+
}, {
|
|
533
|
+
x: extraWidth / 2,
|
|
534
|
+
y: extraHeight / 2
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
const expandLeft = (crop, step, maxWidth) => {
|
|
538
|
+
const newX = max(0, crop.x - step);
|
|
539
|
+
const newWidth = crop.width + (crop.x - newX);
|
|
540
|
+
if (newWidth <= maxWidth) return {
|
|
541
|
+
x: newX,
|
|
542
|
+
width: newWidth
|
|
543
|
+
};
|
|
544
|
+
return {
|
|
545
|
+
x: crop.x + crop.width - maxWidth,
|
|
546
|
+
width: maxWidth
|
|
547
|
+
};
|
|
548
|
+
};
|
|
549
|
+
const expandTop = (crop, step, maxHeight) => {
|
|
550
|
+
const newY = max(0, crop.y - step);
|
|
551
|
+
const newHeight = crop.height + (crop.y - newY);
|
|
552
|
+
if (newHeight <= maxHeight) return {
|
|
553
|
+
y: newY,
|
|
554
|
+
height: newHeight
|
|
555
|
+
};
|
|
556
|
+
return {
|
|
557
|
+
y: crop.y + crop.height - maxHeight,
|
|
558
|
+
height: maxHeight
|
|
559
|
+
};
|
|
560
|
+
};
|
|
561
|
+
const shrinkFromLeft = (crop, step, minWidth) => {
|
|
562
|
+
const newX = min(crop.x + step, crop.x + crop.width - minWidth);
|
|
563
|
+
return {
|
|
564
|
+
x: newX,
|
|
565
|
+
width: crop.width - (newX - crop.x)
|
|
566
|
+
};
|
|
567
|
+
};
|
|
568
|
+
const shrinkFromTop = (crop, step, minHeight) => {
|
|
569
|
+
const newY = min(crop.y + step, crop.y + crop.height - minHeight);
|
|
570
|
+
return {
|
|
571
|
+
y: newY,
|
|
572
|
+
height: crop.height - (newY - crop.y)
|
|
573
|
+
};
|
|
574
|
+
};
|
|
575
|
+
function computeKeyboardCrop(key, handlePosition, step, crop, viewportRect, minSize, maxSize) {
|
|
576
|
+
const nextCrop = { ...crop };
|
|
577
|
+
const { minWidth, minHeight, maxWidth, maxHeight } = resolveSizeLimits({
|
|
578
|
+
minSize,
|
|
579
|
+
maxSize,
|
|
580
|
+
viewportSize: viewportRect
|
|
581
|
+
});
|
|
582
|
+
const isCorner = isCornerHandle(handlePosition);
|
|
583
|
+
if (key === "ArrowLeft") {
|
|
584
|
+
if (isLeftHandle(handlePosition)) {
|
|
585
|
+
const expanded = expandLeft(crop, step, maxWidth);
|
|
586
|
+
nextCrop.x = expanded.x;
|
|
587
|
+
nextCrop.width = expanded.width;
|
|
588
|
+
if (isCorner && isTopHandle(handlePosition)) {
|
|
589
|
+
const expandedY = expandTop(crop, step, maxHeight);
|
|
590
|
+
nextCrop.y = expandedY.y;
|
|
591
|
+
nextCrop.height = expandedY.height;
|
|
592
|
+
} else if (isCorner && isBottomHandle(handlePosition)) {
|
|
593
|
+
const newHeight = nextCrop.height + step;
|
|
594
|
+
nextCrop.height = min(viewportRect.height - nextCrop.y, min(maxHeight, newHeight));
|
|
595
|
+
}
|
|
596
|
+
} else if (isRightHandle(handlePosition)) {
|
|
597
|
+
nextCrop.width = max(minWidth, nextCrop.width - step);
|
|
598
|
+
if (isCorner && isTopHandle(handlePosition)) {
|
|
599
|
+
const shrunk = shrinkFromTop(crop, step, minHeight);
|
|
600
|
+
nextCrop.y = shrunk.y;
|
|
601
|
+
nextCrop.height = shrunk.height;
|
|
602
|
+
} else if (isCorner && isBottomHandle(handlePosition)) nextCrop.height = max(minHeight, nextCrop.height - step);
|
|
603
|
+
}
|
|
604
|
+
} else if (key === "ArrowRight") {
|
|
605
|
+
if (isLeftHandle(handlePosition)) {
|
|
606
|
+
const shrunk = shrinkFromLeft(crop, step, minWidth);
|
|
607
|
+
nextCrop.x = shrunk.x;
|
|
608
|
+
nextCrop.width = shrunk.width;
|
|
609
|
+
if (isCorner && isTopHandle(handlePosition)) {
|
|
610
|
+
const shrunkY = shrinkFromTop(crop, step, minHeight);
|
|
611
|
+
nextCrop.y = shrunkY.y;
|
|
612
|
+
nextCrop.height = shrunkY.height;
|
|
613
|
+
} else if (isCorner && isBottomHandle(handlePosition)) nextCrop.height = max(minHeight, nextCrop.height - step);
|
|
614
|
+
} else if (isRightHandle(handlePosition)) {
|
|
615
|
+
const newWidth = nextCrop.width + step;
|
|
616
|
+
nextCrop.width = min(viewportRect.width - nextCrop.x, min(maxWidth, newWidth));
|
|
617
|
+
if (isCorner && isTopHandle(handlePosition)) {
|
|
618
|
+
const expanded = expandTop(crop, step, maxHeight);
|
|
619
|
+
nextCrop.y = expanded.y;
|
|
620
|
+
nextCrop.height = expanded.height;
|
|
621
|
+
} else if (isCorner && isBottomHandle(handlePosition)) {
|
|
622
|
+
const newHeight = nextCrop.height + step;
|
|
623
|
+
nextCrop.height = min(viewportRect.height - nextCrop.y, min(maxHeight, newHeight));
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
if (key === "ArrowUp") {
|
|
628
|
+
if (isTopHandle(handlePosition)) {
|
|
629
|
+
const expanded = expandTop(crop, step, maxHeight);
|
|
630
|
+
nextCrop.y = expanded.y;
|
|
631
|
+
nextCrop.height = expanded.height;
|
|
632
|
+
if (isCorner && isLeftHandle(handlePosition)) {
|
|
633
|
+
const expandedX = expandLeft(crop, step, maxWidth);
|
|
634
|
+
nextCrop.x = expandedX.x;
|
|
635
|
+
nextCrop.width = expandedX.width;
|
|
636
|
+
} else if (isCorner && isRightHandle(handlePosition)) {
|
|
637
|
+
const newWidth = nextCrop.width + step;
|
|
638
|
+
nextCrop.width = min(viewportRect.width - nextCrop.x, min(maxWidth, newWidth));
|
|
639
|
+
}
|
|
640
|
+
} else if (isBottomHandle(handlePosition)) {
|
|
641
|
+
nextCrop.height = max(minHeight, nextCrop.height - step);
|
|
642
|
+
if (isCorner && isLeftHandle(handlePosition)) {
|
|
643
|
+
const shrunk = shrinkFromLeft(crop, step, minWidth);
|
|
644
|
+
nextCrop.x = shrunk.x;
|
|
645
|
+
nextCrop.width = shrunk.width;
|
|
646
|
+
} else if (isCorner && isRightHandle(handlePosition)) nextCrop.width = max(minWidth, nextCrop.width - step);
|
|
647
|
+
}
|
|
648
|
+
} else if (key === "ArrowDown") {
|
|
649
|
+
if (isTopHandle(handlePosition)) {
|
|
650
|
+
const shrunk = shrinkFromTop(crop, step, minHeight);
|
|
651
|
+
nextCrop.y = shrunk.y;
|
|
652
|
+
nextCrop.height = shrunk.height;
|
|
653
|
+
if (isCorner && isLeftHandle(handlePosition)) {
|
|
654
|
+
const shrunkX = shrinkFromLeft(crop, step, minWidth);
|
|
655
|
+
nextCrop.x = shrunkX.x;
|
|
656
|
+
nextCrop.width = shrunkX.width;
|
|
657
|
+
} else if (isCorner && isRightHandle(handlePosition)) nextCrop.width = max(minWidth, nextCrop.width - step);
|
|
658
|
+
} else if (isBottomHandle(handlePosition)) {
|
|
659
|
+
const newHeight = nextCrop.height + step;
|
|
660
|
+
nextCrop.height = min(viewportRect.height - nextCrop.y, min(maxHeight, newHeight));
|
|
661
|
+
if (isCorner && isLeftHandle(handlePosition)) {
|
|
662
|
+
const expanded = expandLeft(crop, step, maxWidth);
|
|
663
|
+
nextCrop.x = expanded.x;
|
|
664
|
+
nextCrop.width = expanded.width;
|
|
665
|
+
} else if (isCorner && isRightHandle(handlePosition)) {
|
|
666
|
+
const newWidth = nextCrop.width + step;
|
|
667
|
+
nextCrop.width = min(viewportRect.width - nextCrop.x, min(maxWidth, newWidth));
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
return nextCrop;
|
|
672
|
+
}
|
|
673
|
+
function getKeyboardMoveDelta(key, step) {
|
|
674
|
+
switch (key) {
|
|
675
|
+
case "ArrowLeft": return {
|
|
676
|
+
x: -step,
|
|
677
|
+
y: 0
|
|
678
|
+
};
|
|
679
|
+
case "ArrowRight": return {
|
|
680
|
+
x: step,
|
|
681
|
+
y: 0
|
|
682
|
+
};
|
|
683
|
+
case "ArrowUp": return {
|
|
684
|
+
x: 0,
|
|
685
|
+
y: -step
|
|
686
|
+
};
|
|
687
|
+
case "ArrowDown": return {
|
|
688
|
+
x: 0,
|
|
689
|
+
y: step
|
|
690
|
+
};
|
|
691
|
+
default: return ZERO_POINT;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
const resolveCropAspectRatio = (shape, aspectRatio) => shape === "circle" ? 1 : aspectRatio;
|
|
695
|
+
const getCropSizeLimits = (prop) => ({
|
|
696
|
+
minSize: {
|
|
697
|
+
width: prop("minWidth"),
|
|
698
|
+
height: prop("minHeight")
|
|
699
|
+
},
|
|
700
|
+
maxSize: {
|
|
701
|
+
width: prop("maxWidth"),
|
|
702
|
+
height: prop("maxHeight")
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
const getNudgeStep = (prop, modifiers) => {
|
|
706
|
+
if (modifiers.ctrlKey || modifiers.metaKey) return prop("nudgeStepCtrl");
|
|
707
|
+
if (modifiers.shiftKey) return prop("nudgeStepShift");
|
|
708
|
+
return prop("nudgeStep");
|
|
709
|
+
};
|
|
710
|
+
const DEFAULT_VIEWPORT_FILL = .8;
|
|
711
|
+
const computeDefaultCropDimensions = (viewportRect, aspectRatio, fixedCropArea) => {
|
|
712
|
+
const targetWidth = viewportRect.width * DEFAULT_VIEWPORT_FILL;
|
|
713
|
+
const targetHeight = viewportRect.height * DEFAULT_VIEWPORT_FILL;
|
|
714
|
+
if (typeof aspectRatio === "number" && aspectRatio > 0) {
|
|
715
|
+
if (fixedCropArea) {
|
|
716
|
+
let height$1 = viewportRect.height;
|
|
717
|
+
let width = height$1 * aspectRatio;
|
|
718
|
+
if (width > viewportRect.width) {
|
|
719
|
+
width = viewportRect.width;
|
|
720
|
+
height$1 = width / aspectRatio;
|
|
721
|
+
}
|
|
722
|
+
return {
|
|
723
|
+
width,
|
|
724
|
+
height: height$1
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
if (aspectRatio > targetWidth / targetHeight) {
|
|
728
|
+
const width = targetWidth;
|
|
729
|
+
return {
|
|
730
|
+
width,
|
|
731
|
+
height: width / aspectRatio
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
const height = targetHeight;
|
|
735
|
+
return {
|
|
736
|
+
width: height * aspectRatio,
|
|
737
|
+
height
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
if (fixedCropArea) {
|
|
741
|
+
const size = min(viewportRect.width, viewportRect.height);
|
|
742
|
+
return {
|
|
743
|
+
width: size,
|
|
744
|
+
height: size
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
return {
|
|
748
|
+
width: targetWidth,
|
|
749
|
+
height: targetHeight
|
|
750
|
+
};
|
|
751
|
+
};
|
|
752
|
+
const normalizeFlipState = (nextFlip, currentFlip) => {
|
|
753
|
+
if (!nextFlip) return currentFlip;
|
|
754
|
+
return {
|
|
755
|
+
horizontal: isBoolean(nextFlip.horizontal) ? nextFlip.horizontal : currentFlip.horizontal,
|
|
756
|
+
vertical: isBoolean(nextFlip.vertical) ? nextFlip.vertical : currentFlip.vertical
|
|
757
|
+
};
|
|
758
|
+
};
|
|
759
|
+
const isEqualFlip = (a, b) => {
|
|
760
|
+
return a.horizontal === b.horizontal && a.vertical === b.vertical;
|
|
761
|
+
};
|
|
762
|
+
const isVisibleRect = (rect) => rect.width > 0 && rect.height > 0;
|
|
763
|
+
const getCenterPoint = (rect) => ({
|
|
764
|
+
x: rect.x + rect.width / 2,
|
|
765
|
+
y: rect.y + rect.height / 2
|
|
766
|
+
});
|
|
767
|
+
const getViewportCenter = (size) => ({
|
|
768
|
+
x: size.width / 2,
|
|
769
|
+
y: size.height / 2
|
|
770
|
+
});
|
|
771
|
+
const centerRect = (size, viewport) => ({
|
|
772
|
+
x: max(0, (viewport.width - size.width) / 2),
|
|
773
|
+
y: max(0, (viewport.height - size.height) / 2)
|
|
774
|
+
});
|
|
775
|
+
const getMidpoint = (p1, p2, offset = ZERO_POINT) => ({
|
|
776
|
+
x: (p1.x + p2.x) / 2 - offset.x,
|
|
777
|
+
y: (p1.y + p2.y) / 2 - offset.y
|
|
778
|
+
});
|
|
779
|
+
const getMaxBounds = (cropSize, viewportSize) => ({
|
|
780
|
+
x: max(0, viewportSize.width - cropSize.width),
|
|
781
|
+
y: max(0, viewportSize.height - cropSize.height)
|
|
782
|
+
});
|
|
783
|
+
const isSameSize = (a, b) => {
|
|
784
|
+
return a.width === b.width && a.height === b.height;
|
|
785
|
+
};
|
|
786
|
+
const ZERO_POINT = {
|
|
787
|
+
x: 0,
|
|
788
|
+
y: 0
|
|
789
|
+
};
|
|
790
|
+
const getTouchDistance = (p1, p2) => {
|
|
791
|
+
return hypot(p1.x - p2.x, p1.y - p2.y);
|
|
792
|
+
};
|
|
793
|
+
const clampPoint = (point, min$1, max$1) => ({
|
|
794
|
+
x: clampValue(point.x, min$1.x, max$1.x),
|
|
795
|
+
y: clampValue(point.y, min$1.y, max$1.y)
|
|
796
|
+
});
|
|
797
|
+
const subtractPoints = (a, b) => ({
|
|
798
|
+
x: a.x - b.x,
|
|
799
|
+
y: a.y - b.y
|
|
800
|
+
});
|
|
801
|
+
const addPoints = (a, b) => ({
|
|
802
|
+
x: a.x + b.x,
|
|
803
|
+
y: a.y + b.y
|
|
804
|
+
});
|
|
805
|
+
const roundRect = (rect) => ({
|
|
806
|
+
x: round(rect.x),
|
|
807
|
+
y: round(rect.y),
|
|
808
|
+
width: round(rect.width),
|
|
809
|
+
height: round(rect.height)
|
|
810
|
+
});
|
|
811
|
+
const scaleRect = (rect, scale) => ({
|
|
812
|
+
x: rect.x * scale.x,
|
|
813
|
+
y: rect.y * scale.y,
|
|
814
|
+
width: rect.width * scale.x,
|
|
815
|
+
height: rect.height * scale.y
|
|
816
|
+
});
|
|
817
|
+
const getRotationTransform = (rotation) => {
|
|
818
|
+
const theta = rotation % 360 * PI / 180;
|
|
819
|
+
return {
|
|
820
|
+
cos: abs(cos(theta)),
|
|
821
|
+
sin: abs(sin(theta))
|
|
822
|
+
};
|
|
823
|
+
};
|
|
824
|
+
const computeAABB = (size, zoom, cos$1, sin$1) => {
|
|
825
|
+
const w = size.width * zoom;
|
|
826
|
+
const h = size.height * zoom;
|
|
827
|
+
return {
|
|
828
|
+
width: w * cos$1 + h * sin$1,
|
|
829
|
+
height: w * sin$1 + h * cos$1
|
|
830
|
+
};
|
|
831
|
+
};
|
|
832
|
+
const scaleSize = (size, scale) => ({
|
|
833
|
+
width: size.width * scale,
|
|
834
|
+
height: size.height * scale
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
//#endregion
|
|
838
|
+
//#region src/machines/image-cropper/image-cropper.connect.ts
|
|
839
|
+
function connect(service, normalize) {
|
|
840
|
+
const { scope, send, context, prop, state, computed } = service;
|
|
841
|
+
const dragging = state.matches("dragging");
|
|
842
|
+
const panning = state.matches("panning");
|
|
843
|
+
const translations = prop("translations");
|
|
844
|
+
const fixedCropArea = prop("fixedCropArea");
|
|
845
|
+
const cropShape = prop("cropShape");
|
|
846
|
+
const zoom = context.get("zoom");
|
|
847
|
+
const rotation = context.get("rotation");
|
|
848
|
+
const flip = context.get("flip");
|
|
849
|
+
const crop = context.get("crop");
|
|
850
|
+
const offset = context.get("offset");
|
|
851
|
+
const naturalSize = context.get("naturalSize");
|
|
852
|
+
const viewportRect = context.get("viewportRect");
|
|
853
|
+
const isImageReady = computed("isImageReady");
|
|
854
|
+
const isMeasured = computed("isMeasured");
|
|
855
|
+
const roundedCrop = roundRect(crop);
|
|
856
|
+
const shouldIgnoreTouchPointer = (event) => {
|
|
857
|
+
if (event.pointerType !== "touch") return false;
|
|
858
|
+
const isSecondaryTouch = event.isPrimary === false;
|
|
859
|
+
const pinchActive = context.get("pinchDistance") != null;
|
|
860
|
+
return isSecondaryTouch || pinchActive;
|
|
861
|
+
};
|
|
862
|
+
return {
|
|
863
|
+
zoom,
|
|
864
|
+
rotation,
|
|
865
|
+
flip,
|
|
866
|
+
crop,
|
|
867
|
+
offset,
|
|
868
|
+
naturalSize,
|
|
869
|
+
viewportRect,
|
|
870
|
+
dragging,
|
|
871
|
+
panning,
|
|
872
|
+
setZoom(value) {
|
|
873
|
+
send({
|
|
874
|
+
type: "SET_ZOOM",
|
|
875
|
+
zoom: value
|
|
876
|
+
});
|
|
877
|
+
},
|
|
878
|
+
zoomBy(delta) {
|
|
879
|
+
send({
|
|
880
|
+
type: "SET_ZOOM",
|
|
881
|
+
zoom: zoom + delta
|
|
882
|
+
});
|
|
883
|
+
},
|
|
884
|
+
setRotation(value) {
|
|
885
|
+
send({
|
|
886
|
+
type: "SET_ROTATION",
|
|
887
|
+
rotation: value
|
|
888
|
+
});
|
|
889
|
+
},
|
|
890
|
+
rotateBy(degrees) {
|
|
891
|
+
send({
|
|
892
|
+
type: "SET_ROTATION",
|
|
893
|
+
rotation: rotation + degrees
|
|
894
|
+
});
|
|
895
|
+
},
|
|
896
|
+
setFlip(nextFlip) {
|
|
897
|
+
if (!nextFlip) return;
|
|
898
|
+
const normalized = normalizeFlipState(nextFlip, flip);
|
|
899
|
+
if (isEqualFlip(normalized, flip)) return;
|
|
900
|
+
send({
|
|
901
|
+
type: "SET_FLIP",
|
|
902
|
+
flip: normalized
|
|
903
|
+
});
|
|
904
|
+
},
|
|
905
|
+
flipHorizontally(value) {
|
|
906
|
+
const nextValue = typeof value === "boolean" ? value : !flip.horizontal;
|
|
907
|
+
if (nextValue === flip.horizontal) return;
|
|
908
|
+
send({
|
|
909
|
+
type: "SET_FLIP",
|
|
910
|
+
flip: { horizontal: nextValue }
|
|
911
|
+
});
|
|
912
|
+
},
|
|
913
|
+
flipVertically(value) {
|
|
914
|
+
const nextValue = typeof value === "boolean" ? value : !flip.vertical;
|
|
915
|
+
if (nextValue === flip.vertical) return;
|
|
916
|
+
send({
|
|
917
|
+
type: "SET_FLIP",
|
|
918
|
+
flip: { vertical: nextValue }
|
|
919
|
+
});
|
|
920
|
+
},
|
|
921
|
+
resize(handlePosition, delta) {
|
|
922
|
+
if (!handlePosition) return;
|
|
923
|
+
if (fixedCropArea) return;
|
|
924
|
+
let deltaX = 0;
|
|
925
|
+
let deltaY = 0;
|
|
926
|
+
if (isLeftHandle(handlePosition)) deltaX = -delta;
|
|
927
|
+
else if (isRightHandle(handlePosition)) deltaX = delta;
|
|
928
|
+
if (isTopHandle(handlePosition)) deltaY = -delta;
|
|
929
|
+
else if (isBottomHandle(handlePosition)) deltaY = delta;
|
|
930
|
+
send({
|
|
931
|
+
type: "RESIZE_CROP",
|
|
932
|
+
handlePosition,
|
|
933
|
+
delta: {
|
|
934
|
+
x: deltaX,
|
|
935
|
+
y: deltaY
|
|
936
|
+
}
|
|
937
|
+
});
|
|
938
|
+
},
|
|
939
|
+
reset() {
|
|
940
|
+
send({ type: "RESET" });
|
|
941
|
+
},
|
|
942
|
+
getCropData() {
|
|
943
|
+
const scale = naturalSize.width / viewportRect.width;
|
|
944
|
+
const naturalX = (crop.x - offset.x) * scale;
|
|
945
|
+
const naturalY = (crop.y - offset.y) * scale;
|
|
946
|
+
const naturalWidth = crop.width * scale;
|
|
947
|
+
const naturalHeight = crop.height * scale;
|
|
948
|
+
return {
|
|
949
|
+
x: Math.round(naturalX),
|
|
950
|
+
y: Math.round(naturalY),
|
|
951
|
+
width: Math.round(naturalWidth),
|
|
952
|
+
height: Math.round(naturalHeight),
|
|
953
|
+
rotate: rotation,
|
|
954
|
+
flipX: flip.horizontal,
|
|
955
|
+
flipY: flip.vertical
|
|
956
|
+
};
|
|
957
|
+
},
|
|
958
|
+
async getCroppedImage(options = {}) {
|
|
959
|
+
const { type = "image/png", quality = 1, output = "blob" } = options;
|
|
960
|
+
if (!isVisibleRect(naturalSize)) return null;
|
|
961
|
+
const canvas = drawCroppedImageToCanvas(service);
|
|
962
|
+
if (!canvas) return null;
|
|
963
|
+
if (output === "dataUrl") return canvas.toDataURL(type, quality);
|
|
964
|
+
return new Promise((resolve) => {
|
|
965
|
+
canvas.toBlob((blob) => {
|
|
966
|
+
resolve(blob);
|
|
967
|
+
}, type, quality);
|
|
968
|
+
});
|
|
969
|
+
},
|
|
970
|
+
getRootProps() {
|
|
971
|
+
return normalize.element({
|
|
972
|
+
...parts.root.attrs,
|
|
973
|
+
id: getRootId(scope),
|
|
974
|
+
dir: prop("dir"),
|
|
975
|
+
role: "group",
|
|
976
|
+
"aria-roledescription": translations.rootRoleDescription,
|
|
977
|
+
"aria-label": translations.rootLabel,
|
|
978
|
+
"aria-description": isImageReady ? translations.previewDescription({
|
|
979
|
+
crop: roundedCrop,
|
|
980
|
+
zoom: Number.isFinite(zoom) ? zoom : null,
|
|
981
|
+
rotation: Number.isFinite(rotation) ? rotation : null
|
|
982
|
+
}) : translations.previewLoading,
|
|
983
|
+
"aria-live": "polite",
|
|
984
|
+
"aria-controls": `${getViewportId(scope)} ${getSelectionId(scope)}`,
|
|
985
|
+
"aria-busy": isImageReady ? void 0 : "true",
|
|
986
|
+
"data-fixed": dataAttr(fixedCropArea),
|
|
987
|
+
"data-shape": cropShape,
|
|
988
|
+
"data-pinch": dataAttr(context.get("pinchDistance") != null),
|
|
989
|
+
"data-dragging": dataAttr(dragging),
|
|
990
|
+
"data-panning": dataAttr(panning),
|
|
991
|
+
style: {
|
|
992
|
+
"--crop-width": toPx(crop.width),
|
|
993
|
+
"--crop-height": toPx(crop.height),
|
|
994
|
+
"--crop-x": toPx(crop.x),
|
|
995
|
+
"--crop-y": toPx(crop.y),
|
|
996
|
+
"--image-zoom": zoom,
|
|
997
|
+
"--image-rotation": rotation,
|
|
998
|
+
"--image-offset-x": toPx(offset.x),
|
|
999
|
+
"--image-offset-y": toPx(offset.y)
|
|
1000
|
+
}
|
|
1001
|
+
});
|
|
1002
|
+
},
|
|
1003
|
+
getViewportProps() {
|
|
1004
|
+
const viewportId = getViewportId(scope);
|
|
1005
|
+
return normalize.element({
|
|
1006
|
+
...parts.viewport.attrs,
|
|
1007
|
+
id: viewportId,
|
|
1008
|
+
role: "presentation",
|
|
1009
|
+
"data-ownedby": getRootId(scope),
|
|
1010
|
+
"data-disabled": dataAttr(!!fixedCropArea),
|
|
1011
|
+
style: {
|
|
1012
|
+
position: "relative",
|
|
1013
|
+
overflow: "hidden",
|
|
1014
|
+
touchAction: "none",
|
|
1015
|
+
userSelect: "none"
|
|
1016
|
+
},
|
|
1017
|
+
onPointerDown(event) {
|
|
1018
|
+
if (event.pointerType === "mouse" && event.button !== 0) return;
|
|
1019
|
+
if (shouldIgnoreTouchPointer(event)) return;
|
|
1020
|
+
const target = getEventTarget(event);
|
|
1021
|
+
const rootEl = getRootEl(scope);
|
|
1022
|
+
if (!target || !rootEl || !contains(rootEl, target)) return;
|
|
1023
|
+
const selectionEl = getSelectionEl(scope);
|
|
1024
|
+
if (!fixedCropArea && contains(selectionEl, target)) return;
|
|
1025
|
+
const handleEl = target.closest("[data-scope=\"image-cropper\"][data-part=\"handle\"]");
|
|
1026
|
+
if (handleEl && contains(rootEl, handleEl)) return;
|
|
1027
|
+
send({
|
|
1028
|
+
type: "PAN_POINTER_DOWN",
|
|
1029
|
+
point: getEventPoint(event)
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
},
|
|
1034
|
+
getImageProps() {
|
|
1035
|
+
const flipHorizontal = flip.horizontal;
|
|
1036
|
+
const flipVertical = flip.vertical;
|
|
1037
|
+
const translate = `translate(${toPx(offset.x)}, ${toPx(offset.y)})`;
|
|
1038
|
+
const rotate = `rotate(${rotation}deg)`;
|
|
1039
|
+
const scale = `scale(${zoom * (flipHorizontal ? -1 : 1)}, ${zoom * (flipVertical ? -1 : 1)})`;
|
|
1040
|
+
return normalize.element({
|
|
1041
|
+
...parts.image.attrs,
|
|
1042
|
+
id: getImageId(scope),
|
|
1043
|
+
draggable: false,
|
|
1044
|
+
role: "presentation",
|
|
1045
|
+
alt: "",
|
|
1046
|
+
"aria-hidden": true,
|
|
1047
|
+
"data-ownedby": getViewportId(scope),
|
|
1048
|
+
"data-ready": dataAttr(isImageReady),
|
|
1049
|
+
"data-flip-horizontal": dataAttr(flipHorizontal),
|
|
1050
|
+
"data-flip-vertical": dataAttr(flipVertical),
|
|
1051
|
+
onLoad(event) {
|
|
1052
|
+
const imageEl = event.currentTarget;
|
|
1053
|
+
if (!imageEl?.complete) return;
|
|
1054
|
+
const { naturalWidth: width, naturalHeight: height } = imageEl;
|
|
1055
|
+
send({
|
|
1056
|
+
type: "SET_NATURAL_SIZE",
|
|
1057
|
+
src: "element",
|
|
1058
|
+
size: {
|
|
1059
|
+
width,
|
|
1060
|
+
height
|
|
1061
|
+
}
|
|
1062
|
+
});
|
|
1063
|
+
},
|
|
1064
|
+
style: {
|
|
1065
|
+
pointerEvents: "none",
|
|
1066
|
+
userSelect: "none",
|
|
1067
|
+
transform: `${translate} ${rotate} ${scale}`,
|
|
1068
|
+
willChange: "transform"
|
|
1069
|
+
}
|
|
1070
|
+
});
|
|
1071
|
+
},
|
|
1072
|
+
getSelectionProps() {
|
|
1073
|
+
const disabled = !!fixedCropArea;
|
|
1074
|
+
return normalize.element({
|
|
1075
|
+
...parts.selection.attrs,
|
|
1076
|
+
id: getSelectionId(scope),
|
|
1077
|
+
tabIndex: disabled ? void 0 : 0,
|
|
1078
|
+
role: "slider",
|
|
1079
|
+
"aria-label": translations.selectionLabel({ shape: cropShape }),
|
|
1080
|
+
"aria-roledescription": translations.selectionRoleDescription,
|
|
1081
|
+
"aria-disabled": disabled ? "true" : void 0,
|
|
1082
|
+
"aria-valuemin": 0,
|
|
1083
|
+
"aria-valuemax": isVisibleRect(viewportRect) ? Math.max(0, Math.round(viewportRect.width - crop.width)) : Math.max(roundedCrop.x, 0),
|
|
1084
|
+
"aria-valuenow": roundedCrop.x,
|
|
1085
|
+
"aria-valuetext": translations.selectionValueText({
|
|
1086
|
+
shape: cropShape,
|
|
1087
|
+
...roundedCrop
|
|
1088
|
+
}),
|
|
1089
|
+
"aria-description": translations.selectionInstructions,
|
|
1090
|
+
"data-disabled": dataAttr(disabled),
|
|
1091
|
+
"data-shape": cropShape,
|
|
1092
|
+
"data-measured": dataAttr(isMeasured),
|
|
1093
|
+
"data-dragging": dataAttr(dragging),
|
|
1094
|
+
"data-panning": dataAttr(panning),
|
|
1095
|
+
style: {
|
|
1096
|
+
position: "absolute",
|
|
1097
|
+
top: "var(--crop-y)",
|
|
1098
|
+
left: "var(--crop-x)",
|
|
1099
|
+
width: "var(--crop-width)",
|
|
1100
|
+
height: "var(--crop-height)",
|
|
1101
|
+
touchAction: "none",
|
|
1102
|
+
visibility: isMeasured ? void 0 : "hidden"
|
|
1103
|
+
},
|
|
1104
|
+
onPointerDown(event) {
|
|
1105
|
+
if (disabled) {
|
|
1106
|
+
event.preventDefault();
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
if (shouldIgnoreTouchPointer(event)) return;
|
|
1110
|
+
send({
|
|
1111
|
+
type: "POINTER_DOWN",
|
|
1112
|
+
point: getEventPoint(event)
|
|
1113
|
+
});
|
|
1114
|
+
},
|
|
1115
|
+
onKeyDown(event) {
|
|
1116
|
+
if (disabled) {
|
|
1117
|
+
event.preventDefault();
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
if (event.defaultPrevented) return;
|
|
1121
|
+
const src = "selection";
|
|
1122
|
+
const { shiftKey, ctrlKey, metaKey, altKey } = event;
|
|
1123
|
+
const key = getEventKey(event, { dir: prop("dir") });
|
|
1124
|
+
const isZoomInKey = key === "+" || key === "=";
|
|
1125
|
+
if (isZoomInKey || key === "-" || key === "_") {
|
|
1126
|
+
send({
|
|
1127
|
+
type: "ZOOM",
|
|
1128
|
+
trigger: "keyboard",
|
|
1129
|
+
delta: isZoomInKey ? -1 : 1
|
|
1130
|
+
});
|
|
1131
|
+
event.preventDefault();
|
|
1132
|
+
return;
|
|
1133
|
+
}
|
|
1134
|
+
if (altKey && (key === "ArrowUp" || key === "ArrowDown" || key === "ArrowLeft" || key === "ArrowRight")) {
|
|
1135
|
+
send({
|
|
1136
|
+
type: "NUDGE_RESIZE_CROP",
|
|
1137
|
+
handlePosition: key === "ArrowUp" || key === "ArrowDown" ? "s" : "e",
|
|
1138
|
+
key,
|
|
1139
|
+
src,
|
|
1140
|
+
shiftKey,
|
|
1141
|
+
ctrlKey,
|
|
1142
|
+
metaKey
|
|
1143
|
+
});
|
|
1144
|
+
event.preventDefault();
|
|
1145
|
+
return;
|
|
1146
|
+
}
|
|
1147
|
+
const exec = {
|
|
1148
|
+
ArrowUp() {
|
|
1149
|
+
send({
|
|
1150
|
+
type: "NUDGE_MOVE_CROP",
|
|
1151
|
+
key: "ArrowUp",
|
|
1152
|
+
src,
|
|
1153
|
+
shiftKey,
|
|
1154
|
+
ctrlKey,
|
|
1155
|
+
metaKey
|
|
1156
|
+
});
|
|
1157
|
+
},
|
|
1158
|
+
ArrowDown() {
|
|
1159
|
+
send({
|
|
1160
|
+
type: "NUDGE_MOVE_CROP",
|
|
1161
|
+
key: "ArrowDown",
|
|
1162
|
+
src,
|
|
1163
|
+
shiftKey,
|
|
1164
|
+
ctrlKey,
|
|
1165
|
+
metaKey
|
|
1166
|
+
});
|
|
1167
|
+
},
|
|
1168
|
+
ArrowLeft() {
|
|
1169
|
+
send({
|
|
1170
|
+
type: "NUDGE_MOVE_CROP",
|
|
1171
|
+
key: "ArrowLeft",
|
|
1172
|
+
src,
|
|
1173
|
+
shiftKey,
|
|
1174
|
+
ctrlKey,
|
|
1175
|
+
metaKey
|
|
1176
|
+
});
|
|
1177
|
+
},
|
|
1178
|
+
ArrowRight() {
|
|
1179
|
+
send({
|
|
1180
|
+
type: "NUDGE_MOVE_CROP",
|
|
1181
|
+
key: "ArrowRight",
|
|
1182
|
+
src,
|
|
1183
|
+
shiftKey,
|
|
1184
|
+
ctrlKey,
|
|
1185
|
+
metaKey
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
}[key];
|
|
1189
|
+
if (exec) {
|
|
1190
|
+
exec(event);
|
|
1191
|
+
event.preventDefault();
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
});
|
|
1195
|
+
},
|
|
1196
|
+
getHandleProps(props$1) {
|
|
1197
|
+
const handlePosition = props$1.position;
|
|
1198
|
+
const disabled = !!fixedCropArea;
|
|
1199
|
+
return normalize.element({
|
|
1200
|
+
...parts.handle.attrs,
|
|
1201
|
+
id: getHandleId(scope, handlePosition),
|
|
1202
|
+
"data-position": handlePosition,
|
|
1203
|
+
"aria-hidden": "true",
|
|
1204
|
+
role: "presentation",
|
|
1205
|
+
"data-disabled": dataAttr(disabled),
|
|
1206
|
+
style: getHandlePositionStyles(handlePosition),
|
|
1207
|
+
onPointerDown(event) {
|
|
1208
|
+
if (disabled) {
|
|
1209
|
+
event.preventDefault();
|
|
1210
|
+
return;
|
|
1211
|
+
}
|
|
1212
|
+
if (shouldIgnoreTouchPointer(event)) return;
|
|
1213
|
+
send({
|
|
1214
|
+
type: "POINTER_DOWN",
|
|
1215
|
+
point: getEventPoint(event),
|
|
1216
|
+
handlePosition
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
1219
|
+
});
|
|
1220
|
+
},
|
|
1221
|
+
getGridProps(props$1) {
|
|
1222
|
+
const axis = props$1.axis;
|
|
1223
|
+
const isMeasured$1 = computed("isMeasured");
|
|
1224
|
+
return normalize.element({
|
|
1225
|
+
...parts.grid.attrs,
|
|
1226
|
+
"aria-hidden": "true",
|
|
1227
|
+
"data-axis": axis,
|
|
1228
|
+
"data-dragging": dataAttr(dragging),
|
|
1229
|
+
"data-panning": dataAttr(panning),
|
|
1230
|
+
style: {
|
|
1231
|
+
position: "absolute",
|
|
1232
|
+
inset: axis === "horizontal" ? "33.33% 0" : "0 33.33%",
|
|
1233
|
+
pointerEvents: "none",
|
|
1234
|
+
visibility: isMeasured$1 ? void 0 : "hidden"
|
|
1235
|
+
}
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
};
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
//#endregion
|
|
1242
|
+
//#region src/machines/image-cropper/image-cropper.machine.ts
|
|
1243
|
+
const machine = createMachine({
|
|
1244
|
+
props({ props: props$1 }) {
|
|
1245
|
+
return {
|
|
1246
|
+
minWidth: 40,
|
|
1247
|
+
minHeight: 40,
|
|
1248
|
+
maxWidth: Number.POSITIVE_INFINITY,
|
|
1249
|
+
maxHeight: Number.POSITIVE_INFINITY,
|
|
1250
|
+
defaultZoom: 1,
|
|
1251
|
+
zoomStep: .1,
|
|
1252
|
+
zoomSensitivity: 2,
|
|
1253
|
+
minZoom: 1,
|
|
1254
|
+
maxZoom: 5,
|
|
1255
|
+
defaultRotation: 0,
|
|
1256
|
+
defaultFlip: {
|
|
1257
|
+
horizontal: false,
|
|
1258
|
+
vertical: false
|
|
1259
|
+
},
|
|
1260
|
+
fixedCropArea: false,
|
|
1261
|
+
cropShape: "rectangle",
|
|
1262
|
+
nudgeStep: 1,
|
|
1263
|
+
nudgeStepShift: 10,
|
|
1264
|
+
nudgeStepCtrl: 50,
|
|
1265
|
+
...props$1,
|
|
1266
|
+
translations: {
|
|
1267
|
+
rootLabel: "Image cropper",
|
|
1268
|
+
rootRoleDescription: "Image cropper",
|
|
1269
|
+
previewLoading: "Image cropper preview loading",
|
|
1270
|
+
previewDescription({ crop, zoom, rotation }) {
|
|
1271
|
+
return `Image cropper preview, ${zoom != null && Number.isFinite(zoom) ? `${zoom.toFixed(2)}x zoom` : "default zoom"}, ${rotation != null && Number.isFinite(rotation) ? `${Math.round(rotation)} degrees rotation` : "0 degrees rotation"}. Crop positioned at ${crop.x}px from the left and ${crop.y}px from the top with a size of ${crop.width}px by ${crop.height}px.`;
|
|
1272
|
+
},
|
|
1273
|
+
selectionLabel: ({ shape }) => `Crop selection area (${shape === "circle" ? "circle" : "rectangle"})`,
|
|
1274
|
+
selectionRoleDescription: "2d slider",
|
|
1275
|
+
selectionInstructions: "Use arrow keys to move the crop. Hold Alt with arrow keys to resize width or height. Press plus or minus to zoom.",
|
|
1276
|
+
selectionValueText({ shape, x, y, width, height }) {
|
|
1277
|
+
if (shape === "circle") return `Position X ${x}px, Y ${y}px. Diameter ${width}px.`;
|
|
1278
|
+
return `Position X ${x}px, Y ${y}px. Size ${width}px by ${height}px.`;
|
|
1279
|
+
},
|
|
1280
|
+
...props$1.translations
|
|
1281
|
+
}
|
|
1282
|
+
};
|
|
1283
|
+
},
|
|
1284
|
+
context({ bindable, prop }) {
|
|
1285
|
+
return {
|
|
1286
|
+
naturalSize: bindable(() => ({ defaultValue: {
|
|
1287
|
+
width: 0,
|
|
1288
|
+
height: 0
|
|
1289
|
+
} })),
|
|
1290
|
+
crop: bindable(() => ({
|
|
1291
|
+
defaultValue: {
|
|
1292
|
+
x: 0,
|
|
1293
|
+
y: 0,
|
|
1294
|
+
width: 0,
|
|
1295
|
+
height: 0
|
|
1296
|
+
},
|
|
1297
|
+
onChange(crop) {
|
|
1298
|
+
prop("onCropChange")?.({ crop });
|
|
1299
|
+
}
|
|
1300
|
+
})),
|
|
1301
|
+
pointerStart: bindable(() => ({ defaultValue: null })),
|
|
1302
|
+
cropStart: bindable(() => ({ defaultValue: null })),
|
|
1303
|
+
handlePosition: bindable(() => ({ defaultValue: null })),
|
|
1304
|
+
shiftLockRatio: bindable(() => ({ defaultValue: null })),
|
|
1305
|
+
pinchDistance: bindable(() => ({ defaultValue: null })),
|
|
1306
|
+
pinchMidpoint: bindable(() => ({ defaultValue: null })),
|
|
1307
|
+
zoom: bindable(() => ({
|
|
1308
|
+
defaultValue: prop("zoom") ?? prop("defaultZoom"),
|
|
1309
|
+
onChange(zoom) {
|
|
1310
|
+
prop("onZoomChange")?.({ zoom });
|
|
1311
|
+
}
|
|
1312
|
+
})),
|
|
1313
|
+
rotation: bindable(() => ({
|
|
1314
|
+
defaultValue: prop("defaultRotation"),
|
|
1315
|
+
value: prop("rotation"),
|
|
1316
|
+
onChange(rotation) {
|
|
1317
|
+
prop("onRotationChange")?.({ rotation });
|
|
1318
|
+
}
|
|
1319
|
+
})),
|
|
1320
|
+
flip: bindable(() => {
|
|
1321
|
+
return {
|
|
1322
|
+
defaultValue: { ...prop("defaultFlip") },
|
|
1323
|
+
value: prop("flip"),
|
|
1324
|
+
onChange(flip) {
|
|
1325
|
+
prop("onFlipChange")?.({ flip });
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
}),
|
|
1329
|
+
offset: bindable(() => ({ defaultValue: ZERO_POINT })),
|
|
1330
|
+
offsetStart: bindable(() => ({ defaultValue: null })),
|
|
1331
|
+
viewportRect: bindable(() => ({ defaultValue: {
|
|
1332
|
+
width: 0,
|
|
1333
|
+
height: 0,
|
|
1334
|
+
top: 0,
|
|
1335
|
+
left: 0,
|
|
1336
|
+
right: 0,
|
|
1337
|
+
bottom: 0
|
|
1338
|
+
} }))
|
|
1339
|
+
};
|
|
1340
|
+
},
|
|
1341
|
+
initialState() {
|
|
1342
|
+
return "idle";
|
|
1343
|
+
},
|
|
1344
|
+
on: {
|
|
1345
|
+
PINCH_START: { actions: ["setPinchDistance"] },
|
|
1346
|
+
PINCH_MOVE: { actions: ["handlePinchMove"] },
|
|
1347
|
+
PINCH_END: { actions: ["clearPinchDistance"] },
|
|
1348
|
+
SET_ZOOM: { actions: ["updateZoom"] },
|
|
1349
|
+
SET_ROTATION: { actions: ["setRotation"] },
|
|
1350
|
+
SET_FLIP: { actions: ["setFlip"] },
|
|
1351
|
+
RESIZE_CROP: {
|
|
1352
|
+
guard: "canResizeCrop",
|
|
1353
|
+
actions: ["resizeCrop"]
|
|
1354
|
+
},
|
|
1355
|
+
VIEWPORT_RESIZE: { actions: ["resizeViewport"] },
|
|
1356
|
+
RESET: { actions: ["resetToInitialState"] }
|
|
1357
|
+
},
|
|
1358
|
+
computed: {
|
|
1359
|
+
isMeasured: ({ context }) => isVisibleRect(context.get("viewportRect")) && isVisibleRect(context.get("crop")),
|
|
1360
|
+
isImageReady: ({ context }) => isVisibleRect(context.get("naturalSize"))
|
|
1361
|
+
},
|
|
1362
|
+
watch({ track, context, prop, send }) {
|
|
1363
|
+
track([() => prop("zoom")], () => {
|
|
1364
|
+
const propZoom = prop("zoom");
|
|
1365
|
+
if (propZoom === void 0) return;
|
|
1366
|
+
if (propZoom === context.get("zoom")) return;
|
|
1367
|
+
send({
|
|
1368
|
+
type: "SET_ZOOM",
|
|
1369
|
+
zoom: propZoom,
|
|
1370
|
+
src: "prop"
|
|
1371
|
+
});
|
|
1372
|
+
});
|
|
1373
|
+
},
|
|
1374
|
+
states: {
|
|
1375
|
+
idle: {
|
|
1376
|
+
entry: ["checkImageStatus"],
|
|
1377
|
+
effects: [
|
|
1378
|
+
"trackViewportResize",
|
|
1379
|
+
"trackWheelEvent",
|
|
1380
|
+
"trackTouchEvents"
|
|
1381
|
+
],
|
|
1382
|
+
on: {
|
|
1383
|
+
SET_NATURAL_SIZE: { actions: ["setNaturalSize"] },
|
|
1384
|
+
SET_DEFAULT_CROP: { actions: ["setDefaultCrop"] },
|
|
1385
|
+
POINTER_DOWN: {
|
|
1386
|
+
guard: "canDragSelection",
|
|
1387
|
+
target: "dragging",
|
|
1388
|
+
actions: [
|
|
1389
|
+
"setPointerStart",
|
|
1390
|
+
"setCropStart",
|
|
1391
|
+
"setHandlePosition"
|
|
1392
|
+
]
|
|
1393
|
+
},
|
|
1394
|
+
PAN_POINTER_DOWN: {
|
|
1395
|
+
guard: "canPan",
|
|
1396
|
+
target: "panning",
|
|
1397
|
+
actions: ["setPointerStart", "setOffsetStart"]
|
|
1398
|
+
},
|
|
1399
|
+
ZOOM: {
|
|
1400
|
+
guard: "hasViewportRect",
|
|
1401
|
+
actions: ["updateZoom"]
|
|
1402
|
+
},
|
|
1403
|
+
NUDGE_RESIZE_CROP: {
|
|
1404
|
+
guard: "hasViewportRect",
|
|
1405
|
+
actions: ["nudgeResizeCrop"]
|
|
1406
|
+
},
|
|
1407
|
+
NUDGE_MOVE_CROP: {
|
|
1408
|
+
guard: "hasViewportRect",
|
|
1409
|
+
actions: ["nudgeMoveCrop"]
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
},
|
|
1413
|
+
dragging: {
|
|
1414
|
+
effects: ["trackPointerMove"],
|
|
1415
|
+
on: {
|
|
1416
|
+
POINTER_MOVE: { actions: ["updateCrop"] },
|
|
1417
|
+
POINTER_UP: {
|
|
1418
|
+
target: "idle",
|
|
1419
|
+
actions: [
|
|
1420
|
+
"clearPointerStart",
|
|
1421
|
+
"clearCropStart",
|
|
1422
|
+
"clearHandlePosition",
|
|
1423
|
+
"clearOffsetStart",
|
|
1424
|
+
"clearShiftRatio"
|
|
1425
|
+
]
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
},
|
|
1429
|
+
panning: {
|
|
1430
|
+
effects: ["trackPointerMove"],
|
|
1431
|
+
on: {
|
|
1432
|
+
POINTER_MOVE: { actions: ["updatePanOffset"] },
|
|
1433
|
+
POINTER_UP: {
|
|
1434
|
+
target: "idle",
|
|
1435
|
+
actions: ["clearPointerStart", "clearOffsetStart"]
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
},
|
|
1440
|
+
implementations: {
|
|
1441
|
+
guards: {
|
|
1442
|
+
hasViewportRect({ context }) {
|
|
1443
|
+
return isVisibleRect(context.get("viewportRect"));
|
|
1444
|
+
},
|
|
1445
|
+
canResizeCrop({ context, prop }) {
|
|
1446
|
+
return !prop("fixedCropArea") && isVisibleRect(context.get("viewportRect"));
|
|
1447
|
+
},
|
|
1448
|
+
canPan({ context }) {
|
|
1449
|
+
return isVisibleRect(context.get("naturalSize")) && isVisibleRect(context.get("viewportRect"));
|
|
1450
|
+
},
|
|
1451
|
+
canDragSelection({ context, prop }) {
|
|
1452
|
+
return isVisibleRect(context.get("viewportRect")) && !prop("fixedCropArea");
|
|
1453
|
+
}
|
|
1454
|
+
},
|
|
1455
|
+
actions: {
|
|
1456
|
+
checkImageStatus({ send, scope, context }) {
|
|
1457
|
+
const naturalSize = context.get("naturalSize");
|
|
1458
|
+
const imageEl = getImageEl(scope);
|
|
1459
|
+
if (!imageEl?.complete) return;
|
|
1460
|
+
const { naturalWidth: width, naturalHeight: height } = imageEl;
|
|
1461
|
+
if (isVisibleRect({
|
|
1462
|
+
width,
|
|
1463
|
+
height
|
|
1464
|
+
}) && !isVisibleRect(naturalSize)) send({
|
|
1465
|
+
type: "SET_NATURAL_SIZE",
|
|
1466
|
+
src: "ssr",
|
|
1467
|
+
size: {
|
|
1468
|
+
width,
|
|
1469
|
+
height
|
|
1470
|
+
}
|
|
1471
|
+
});
|
|
1472
|
+
},
|
|
1473
|
+
setNaturalSize({ event, context, send }) {
|
|
1474
|
+
context.set("naturalSize", event.size);
|
|
1475
|
+
send({
|
|
1476
|
+
type: "SET_DEFAULT_CROP",
|
|
1477
|
+
src: "init"
|
|
1478
|
+
});
|
|
1479
|
+
},
|
|
1480
|
+
setDefaultCrop({ context, prop, scope }) {
|
|
1481
|
+
const viewportEl = getViewportEl(scope);
|
|
1482
|
+
if (!viewportEl) return;
|
|
1483
|
+
const viewportRect = getBoundingRect(viewportEl);
|
|
1484
|
+
if (!isVisibleRect(viewportRect)) return;
|
|
1485
|
+
const aspectRatio = resolveCropAspectRatio(prop("cropShape"), prop("aspectRatio"));
|
|
1486
|
+
const { minSize, maxSize } = getCropSizeLimits(prop);
|
|
1487
|
+
const clampSize = (rect) => {
|
|
1488
|
+
const result = computeResizeCrop({
|
|
1489
|
+
cropStart: rect,
|
|
1490
|
+
handlePosition: "se",
|
|
1491
|
+
delta: ZERO_POINT,
|
|
1492
|
+
viewportRect,
|
|
1493
|
+
minSize,
|
|
1494
|
+
maxSize,
|
|
1495
|
+
aspectRatio
|
|
1496
|
+
});
|
|
1497
|
+
return {
|
|
1498
|
+
width: result.width,
|
|
1499
|
+
height: result.height
|
|
1500
|
+
};
|
|
1501
|
+
};
|
|
1502
|
+
const initialCrop = prop("initialCrop");
|
|
1503
|
+
if (initialCrop) {
|
|
1504
|
+
const { width: width$1, height: height$1 } = clampSize({
|
|
1505
|
+
x: 0,
|
|
1506
|
+
y: 0,
|
|
1507
|
+
width: initialCrop.width,
|
|
1508
|
+
height: initialCrop.height
|
|
1509
|
+
});
|
|
1510
|
+
const { x: x$1, y: y$1 } = clampPoint(initialCrop, ZERO_POINT, getMaxBounds({
|
|
1511
|
+
width: width$1,
|
|
1512
|
+
height: height$1
|
|
1513
|
+
}, viewportRect));
|
|
1514
|
+
context.set("crop", {
|
|
1515
|
+
x: x$1,
|
|
1516
|
+
y: y$1,
|
|
1517
|
+
width: width$1,
|
|
1518
|
+
height: height$1
|
|
1519
|
+
});
|
|
1520
|
+
return;
|
|
1521
|
+
}
|
|
1522
|
+
const defaultSize = computeDefaultCropDimensions(viewportRect, aspectRatio, prop("fixedCropArea"));
|
|
1523
|
+
const constrainedSize = clampSize({
|
|
1524
|
+
x: 0,
|
|
1525
|
+
y: 0,
|
|
1526
|
+
width: defaultSize.width,
|
|
1527
|
+
height: defaultSize.height
|
|
1528
|
+
});
|
|
1529
|
+
const width = constrainedSize.width;
|
|
1530
|
+
const height = constrainedSize.height;
|
|
1531
|
+
const { x, y } = centerRect({
|
|
1532
|
+
width,
|
|
1533
|
+
height
|
|
1534
|
+
}, viewportRect);
|
|
1535
|
+
context.set("crop", {
|
|
1536
|
+
x,
|
|
1537
|
+
y,
|
|
1538
|
+
width,
|
|
1539
|
+
height
|
|
1540
|
+
});
|
|
1541
|
+
context.set("viewportRect", viewportRect);
|
|
1542
|
+
},
|
|
1543
|
+
setPointerStart({ event, context }) {
|
|
1544
|
+
const point = event.point;
|
|
1545
|
+
if (!point) return;
|
|
1546
|
+
context.set("pointerStart", point);
|
|
1547
|
+
},
|
|
1548
|
+
setOffsetStart({ context }) {
|
|
1549
|
+
const offset = context.get("offset");
|
|
1550
|
+
context.set("offsetStart", { ...offset });
|
|
1551
|
+
},
|
|
1552
|
+
setCropStart({ context }) {
|
|
1553
|
+
const crop = context.get("crop");
|
|
1554
|
+
context.set("cropStart", crop);
|
|
1555
|
+
},
|
|
1556
|
+
updateCrop({ context, event, prop }) {
|
|
1557
|
+
const handlePosition = context.get("handlePosition");
|
|
1558
|
+
const pointerStart = context.get("pointerStart");
|
|
1559
|
+
const cropStart = context.get("cropStart");
|
|
1560
|
+
const viewportRect = context.get("viewportRect");
|
|
1561
|
+
const cropShape = prop("cropShape");
|
|
1562
|
+
const aspectRatioProp = prop("aspectRatio");
|
|
1563
|
+
let aspectRatio = resolveCropAspectRatio(cropShape, aspectRatioProp);
|
|
1564
|
+
const { minSize, maxSize } = getCropSizeLimits(prop);
|
|
1565
|
+
if (!pointerStart || !cropStart) return;
|
|
1566
|
+
const currentPoint = event.point;
|
|
1567
|
+
const delta = subtractPoints(currentPoint, pointerStart);
|
|
1568
|
+
let nextCrop;
|
|
1569
|
+
if (handlePosition) {
|
|
1570
|
+
if (typeof aspectRatioProp === "undefined" && cropShape !== "circle") if (event.shiftKey) {
|
|
1571
|
+
const currentCrop = context.get("crop");
|
|
1572
|
+
const w = currentCrop.width;
|
|
1573
|
+
const h = currentCrop.height;
|
|
1574
|
+
if (w > 0 && h > 0) {
|
|
1575
|
+
const ratio = w / h;
|
|
1576
|
+
if (ratio > 0) context.set("shiftLockRatio", ratio);
|
|
1577
|
+
}
|
|
1578
|
+
const lockRatio = context.get("shiftLockRatio");
|
|
1579
|
+
if (lockRatio !== null && lockRatio > 0) aspectRatio = lockRatio;
|
|
1580
|
+
} else context.set("shiftLockRatio", null);
|
|
1581
|
+
else context.set("shiftLockRatio", null);
|
|
1582
|
+
nextCrop = computeResizeCrop({
|
|
1583
|
+
cropStart,
|
|
1584
|
+
handlePosition,
|
|
1585
|
+
delta,
|
|
1586
|
+
viewportRect,
|
|
1587
|
+
minSize,
|
|
1588
|
+
maxSize,
|
|
1589
|
+
aspectRatio
|
|
1590
|
+
});
|
|
1591
|
+
} else nextCrop = computeMoveCrop(cropStart, delta, viewportRect);
|
|
1592
|
+
context.set("crop", nextCrop);
|
|
1593
|
+
},
|
|
1594
|
+
updatePanOffset({ context, event, prop }) {
|
|
1595
|
+
const point = event.point;
|
|
1596
|
+
const pointerStart = context.get("pointerStart");
|
|
1597
|
+
const offsetStart = context.get("offsetStart");
|
|
1598
|
+
if (!point || !pointerStart || !offsetStart) return;
|
|
1599
|
+
const nextOffset = clampOffset({
|
|
1600
|
+
zoom: context.get("zoom"),
|
|
1601
|
+
rotation: context.get("rotation"),
|
|
1602
|
+
viewportSize: context.get("viewportRect"),
|
|
1603
|
+
offset: addPoints(offsetStart, subtractPoints(point, pointerStart)),
|
|
1604
|
+
fixedCropArea: prop("fixedCropArea"),
|
|
1605
|
+
crop: context.get("crop"),
|
|
1606
|
+
naturalSize: context.get("naturalSize")
|
|
1607
|
+
});
|
|
1608
|
+
context.set("offset", nextOffset);
|
|
1609
|
+
},
|
|
1610
|
+
setHandlePosition({ event, context }) {
|
|
1611
|
+
const position = event.handlePosition;
|
|
1612
|
+
if (!position) return;
|
|
1613
|
+
context.set("handlePosition", position);
|
|
1614
|
+
},
|
|
1615
|
+
setRotation({ context, event }) {
|
|
1616
|
+
const rotation = event.rotation;
|
|
1617
|
+
const nextRotation = clampValue(rotation, 0, 360);
|
|
1618
|
+
context.set("rotation", nextRotation);
|
|
1619
|
+
},
|
|
1620
|
+
setFlip({ context, event }) {
|
|
1621
|
+
const nextFlip = event.flip;
|
|
1622
|
+
if (!nextFlip) return;
|
|
1623
|
+
const currentFlip = context.get("flip");
|
|
1624
|
+
const normalized = normalizeFlipState(nextFlip, currentFlip);
|
|
1625
|
+
if (isEqualFlip(normalized, currentFlip)) return;
|
|
1626
|
+
context.set("flip", normalized);
|
|
1627
|
+
},
|
|
1628
|
+
resizeCrop({ context, event, prop }) {
|
|
1629
|
+
const { handlePosition, delta } = event;
|
|
1630
|
+
if (!handlePosition) return;
|
|
1631
|
+
const viewportRect = context.get("viewportRect");
|
|
1632
|
+
if (!isVisibleRect(viewportRect)) return;
|
|
1633
|
+
const aspectRatio = resolveCropAspectRatio(prop("cropShape"), prop("aspectRatio"));
|
|
1634
|
+
const { minSize, maxSize } = getCropSizeLimits(prop);
|
|
1635
|
+
const nextCrop = computeResizeCrop({
|
|
1636
|
+
cropStart: context.get("crop"),
|
|
1637
|
+
handlePosition,
|
|
1638
|
+
delta,
|
|
1639
|
+
viewportRect,
|
|
1640
|
+
minSize,
|
|
1641
|
+
maxSize,
|
|
1642
|
+
aspectRatio
|
|
1643
|
+
});
|
|
1644
|
+
context.set("crop", nextCrop);
|
|
1645
|
+
},
|
|
1646
|
+
clearPointerStart({ context }) {
|
|
1647
|
+
context.set("pointerStart", null);
|
|
1648
|
+
},
|
|
1649
|
+
clearCropStart({ context }) {
|
|
1650
|
+
context.set("cropStart", null);
|
|
1651
|
+
},
|
|
1652
|
+
clearHandlePosition({ context }) {
|
|
1653
|
+
context.set("handlePosition", null);
|
|
1654
|
+
},
|
|
1655
|
+
clearOffsetStart({ context }) {
|
|
1656
|
+
context.set("offsetStart", null);
|
|
1657
|
+
},
|
|
1658
|
+
clearShiftRatio({ context }) {
|
|
1659
|
+
context.set("shiftLockRatio", null);
|
|
1660
|
+
},
|
|
1661
|
+
updateZoom({ context, event, prop }) {
|
|
1662
|
+
let { delta, point, zoom: targetZoom, scale, panDelta } = event;
|
|
1663
|
+
const crop = context.get("crop");
|
|
1664
|
+
const currentZoom = context.get("zoom");
|
|
1665
|
+
const currentOffset = context.get("offset");
|
|
1666
|
+
const rotation = context.get("rotation");
|
|
1667
|
+
const viewportRect = context.get("viewportRect");
|
|
1668
|
+
const naturalSize = context.get("naturalSize");
|
|
1669
|
+
const fixedCropArea = prop("fixedCropArea");
|
|
1670
|
+
if (!point) point = getCenterPoint(crop);
|
|
1671
|
+
const step = Math.abs(prop("zoomStep"));
|
|
1672
|
+
const sensitivity = Math.max(0, prop("zoomSensitivity"));
|
|
1673
|
+
const [minZoom, maxZoom] = [prop("minZoom"), prop("maxZoom")];
|
|
1674
|
+
const calculateNextZoom = () => {
|
|
1675
|
+
if (typeof targetZoom === "number") return clampValue(targetZoom, minZoom, maxZoom);
|
|
1676
|
+
if (event.trigger === "touch" && typeof scale === "number") {
|
|
1677
|
+
const clampedScale = clampValue(scale, .5, 2);
|
|
1678
|
+
return clampValue(currentZoom * (sensitivity > 0 ? Math.pow(clampedScale, sensitivity) : clampedScale), minZoom, maxZoom);
|
|
1679
|
+
}
|
|
1680
|
+
if (typeof delta === "number") return clampValue(currentZoom + step * (Math.sign(delta) < 0 ? 1 : -1), minZoom, maxZoom);
|
|
1681
|
+
return null;
|
|
1682
|
+
};
|
|
1683
|
+
const applyClampedOffset = (zoom, offset) => {
|
|
1684
|
+
return clampOffset({
|
|
1685
|
+
zoom,
|
|
1686
|
+
rotation,
|
|
1687
|
+
viewportSize: viewportRect,
|
|
1688
|
+
offset,
|
|
1689
|
+
fixedCropArea,
|
|
1690
|
+
crop,
|
|
1691
|
+
naturalSize
|
|
1692
|
+
});
|
|
1693
|
+
};
|
|
1694
|
+
const nextZoom = calculateNextZoom();
|
|
1695
|
+
if (nextZoom === null) return;
|
|
1696
|
+
if (nextZoom === currentZoom && panDelta) {
|
|
1697
|
+
const nextOffset$1 = applyClampedOffset(currentZoom, addPoints(currentOffset, panDelta));
|
|
1698
|
+
context.set("offset", nextOffset$1);
|
|
1699
|
+
return;
|
|
1700
|
+
}
|
|
1701
|
+
if (nextZoom === currentZoom) return;
|
|
1702
|
+
const { width: viewportWidth, height: viewportHeight } = viewportRect;
|
|
1703
|
+
const { x: centerX, y: centerY } = getViewportCenter(viewportRect);
|
|
1704
|
+
const zoomRatio = nextZoom / currentZoom;
|
|
1705
|
+
let nextOffset = {
|
|
1706
|
+
x: (1 - zoomRatio) * (point.x - centerX) + zoomRatio * currentOffset.x,
|
|
1707
|
+
y: (1 - zoomRatio) * (point.y - centerY) + zoomRatio * currentOffset.y
|
|
1708
|
+
};
|
|
1709
|
+
if (panDelta) nextOffset = applyClampedOffset(nextZoom, addPoints(nextOffset, panDelta));
|
|
1710
|
+
else if (nextZoom < currentZoom) if (fixedCropArea) nextOffset = applyClampedOffset(nextZoom, nextOffset);
|
|
1711
|
+
else {
|
|
1712
|
+
const { width: scaledImageWidth, height: scaledImageHeight } = scaleSize(viewportRect, nextZoom);
|
|
1713
|
+
if (scaledImageWidth <= viewportWidth) nextOffset.x = 0;
|
|
1714
|
+
else {
|
|
1715
|
+
const minX = viewportWidth - centerX - scaledImageWidth / 2;
|
|
1716
|
+
const maxX = scaledImageWidth / 2 - centerX;
|
|
1717
|
+
nextOffset.x = Math.max(minX, Math.min(maxX, nextOffset.x));
|
|
1718
|
+
}
|
|
1719
|
+
if (scaledImageHeight <= viewportHeight) nextOffset.y = 0;
|
|
1720
|
+
else {
|
|
1721
|
+
const minY = viewportHeight - centerY - scaledImageHeight / 2;
|
|
1722
|
+
const maxY = scaledImageHeight / 2 - centerY;
|
|
1723
|
+
nextOffset.y = Math.max(minY, Math.min(maxY, nextOffset.y));
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
context.set("zoom", nextZoom);
|
|
1727
|
+
context.set("offset", nextOffset);
|
|
1728
|
+
},
|
|
1729
|
+
setPinchDistance({ context, event, send }) {
|
|
1730
|
+
const touches = Array.isArray(event.touches) ? event.touches : [];
|
|
1731
|
+
if (touches.length < 2) return;
|
|
1732
|
+
if (context.get("pointerStart") !== null) send({
|
|
1733
|
+
type: "POINTER_UP",
|
|
1734
|
+
src: "pinch"
|
|
1735
|
+
});
|
|
1736
|
+
const [first, second] = touches;
|
|
1737
|
+
const distance = getTouchDistance(first, second);
|
|
1738
|
+
const viewportRect = context.get("viewportRect");
|
|
1739
|
+
const midpoint = getMidpoint(first, second, {
|
|
1740
|
+
x: viewportRect.left,
|
|
1741
|
+
y: viewportRect.top
|
|
1742
|
+
});
|
|
1743
|
+
context.set("pinchDistance", distance);
|
|
1744
|
+
context.set("pinchMidpoint", midpoint);
|
|
1745
|
+
},
|
|
1746
|
+
handlePinchMove({ context, event, send }) {
|
|
1747
|
+
const touches = Array.isArray(event.touches) ? event.touches : [];
|
|
1748
|
+
if (touches.length < 2) return;
|
|
1749
|
+
const [first, second] = touches;
|
|
1750
|
+
const distance = getTouchDistance(first, second);
|
|
1751
|
+
const lastDistance = context.get("pinchDistance");
|
|
1752
|
+
const lastMidpoint = context.get("pinchMidpoint");
|
|
1753
|
+
const viewportRect = context.get("viewportRect");
|
|
1754
|
+
const midpoint = getMidpoint(first, second, {
|
|
1755
|
+
x: viewportRect.left,
|
|
1756
|
+
y: viewportRect.top
|
|
1757
|
+
});
|
|
1758
|
+
if (lastDistance != null && lastDistance > 0 && lastMidpoint != null) {
|
|
1759
|
+
const delta = lastDistance - distance;
|
|
1760
|
+
const scale = distance / lastDistance;
|
|
1761
|
+
const hasSignificantZoom = Math.abs(delta) > 1;
|
|
1762
|
+
const panDelta = subtractPoints(midpoint, lastMidpoint);
|
|
1763
|
+
send({
|
|
1764
|
+
type: "ZOOM",
|
|
1765
|
+
trigger: "touch",
|
|
1766
|
+
delta,
|
|
1767
|
+
scale: hasSignificantZoom ? scale : 1,
|
|
1768
|
+
point: midpoint,
|
|
1769
|
+
panDelta
|
|
1770
|
+
});
|
|
1771
|
+
}
|
|
1772
|
+
context.set("pinchDistance", distance);
|
|
1773
|
+
context.set("pinchMidpoint", midpoint);
|
|
1774
|
+
},
|
|
1775
|
+
clearPinchDistance({ context }) {
|
|
1776
|
+
context.set("pinchDistance", null);
|
|
1777
|
+
context.set("pinchMidpoint", null);
|
|
1778
|
+
},
|
|
1779
|
+
nudgeResizeCrop({ context, event, prop }) {
|
|
1780
|
+
const { key, handlePosition, shiftKey, ctrlKey, metaKey } = event;
|
|
1781
|
+
const crop = context.get("crop");
|
|
1782
|
+
const viewportRect = context.get("viewportRect");
|
|
1783
|
+
const step = getNudgeStep(prop, {
|
|
1784
|
+
shiftKey,
|
|
1785
|
+
ctrlKey,
|
|
1786
|
+
metaKey
|
|
1787
|
+
});
|
|
1788
|
+
const { minSize, maxSize } = getCropSizeLimits(prop);
|
|
1789
|
+
const nextCrop = computeKeyboardCrop(key, handlePosition, step, crop, viewportRect, minSize, maxSize);
|
|
1790
|
+
context.set("crop", nextCrop);
|
|
1791
|
+
},
|
|
1792
|
+
nudgeMoveCrop({ context, event, prop }) {
|
|
1793
|
+
const { key, shiftKey, ctrlKey, metaKey } = event;
|
|
1794
|
+
const crop = context.get("crop");
|
|
1795
|
+
const viewportRect = context.get("viewportRect");
|
|
1796
|
+
const nextCrop = computeMoveCrop(crop, getKeyboardMoveDelta(key, getNudgeStep(prop, {
|
|
1797
|
+
shiftKey,
|
|
1798
|
+
ctrlKey,
|
|
1799
|
+
metaKey
|
|
1800
|
+
})), viewportRect);
|
|
1801
|
+
context.set("crop", nextCrop);
|
|
1802
|
+
},
|
|
1803
|
+
resizeViewport({ context, prop, scope, send }) {
|
|
1804
|
+
const viewportEl = getViewportEl(scope);
|
|
1805
|
+
if (!viewportEl) return;
|
|
1806
|
+
const newViewportRect = getBoundingRect(viewportEl);
|
|
1807
|
+
if (!isVisibleRect(newViewportRect)) return;
|
|
1808
|
+
const oldViewportRect = context.get("viewportRect");
|
|
1809
|
+
if (isSameSize(oldViewportRect, newViewportRect)) return;
|
|
1810
|
+
context.set("viewportRect", newViewportRect);
|
|
1811
|
+
const oldCrop = context.get("crop");
|
|
1812
|
+
if (!isVisibleRect(oldViewportRect)) {
|
|
1813
|
+
if (!isVisibleRect(oldCrop)) {
|
|
1814
|
+
send({
|
|
1815
|
+
type: "SET_DEFAULT_CROP",
|
|
1816
|
+
src: "viewport-resize"
|
|
1817
|
+
});
|
|
1818
|
+
return;
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
const aspectRatio = resolveCropAspectRatio(prop("cropShape"), prop("aspectRatio"));
|
|
1822
|
+
const { minSize, maxSize } = getCropSizeLimits(prop);
|
|
1823
|
+
const constrainedCrop = computeResizeCrop({
|
|
1824
|
+
cropStart: scaleRect(oldCrop, {
|
|
1825
|
+
x: newViewportRect.width / oldViewportRect.width,
|
|
1826
|
+
y: newViewportRect.height / oldViewportRect.height
|
|
1827
|
+
}),
|
|
1828
|
+
handlePosition: "se",
|
|
1829
|
+
delta: ZERO_POINT,
|
|
1830
|
+
viewportRect: newViewportRect,
|
|
1831
|
+
minSize,
|
|
1832
|
+
maxSize,
|
|
1833
|
+
aspectRatio
|
|
1834
|
+
});
|
|
1835
|
+
const { x, y } = clampPoint(constrainedCrop, ZERO_POINT, getMaxBounds(constrainedCrop, newViewportRect));
|
|
1836
|
+
context.set("crop", {
|
|
1837
|
+
x,
|
|
1838
|
+
y,
|
|
1839
|
+
width: constrainedCrop.width,
|
|
1840
|
+
height: constrainedCrop.height
|
|
1841
|
+
});
|
|
1842
|
+
},
|
|
1843
|
+
resetToInitialState({ context }) {
|
|
1844
|
+
context.set("zoom", context.initial("zoom"));
|
|
1845
|
+
context.set("rotation", context.initial("rotation"));
|
|
1846
|
+
context.set("flip", context.initial("flip"));
|
|
1847
|
+
context.set("offset", ZERO_POINT);
|
|
1848
|
+
context.set("crop", context.initial("crop"));
|
|
1849
|
+
}
|
|
1850
|
+
},
|
|
1851
|
+
effects: {
|
|
1852
|
+
trackPointerMove({ scope, send }) {
|
|
1853
|
+
function onPointerMove(event) {
|
|
1854
|
+
send({
|
|
1855
|
+
type: "POINTER_MOVE",
|
|
1856
|
+
point: getEventPoint(event),
|
|
1857
|
+
target: getEventTarget(event),
|
|
1858
|
+
shiftKey: event.shiftKey
|
|
1859
|
+
});
|
|
1860
|
+
}
|
|
1861
|
+
function onPointerUp() {
|
|
1862
|
+
send({ type: "POINTER_UP" });
|
|
1863
|
+
}
|
|
1864
|
+
return callAll(addDomEvent(scope.getDoc(), "pointermove", onPointerMove), addDomEvent(scope.getDoc(), "pointerup", onPointerUp));
|
|
1865
|
+
},
|
|
1866
|
+
trackViewportResize({ scope, send }) {
|
|
1867
|
+
const viewportEl = getViewportEl(scope);
|
|
1868
|
+
if (!viewportEl) return;
|
|
1869
|
+
return resizeObserverBorderBox.observe(viewportEl, () => {
|
|
1870
|
+
send({
|
|
1871
|
+
type: "VIEWPORT_RESIZE",
|
|
1872
|
+
src: "resize"
|
|
1873
|
+
});
|
|
1874
|
+
});
|
|
1875
|
+
},
|
|
1876
|
+
trackWheelEvent({ scope, send }) {
|
|
1877
|
+
const viewportEl = getViewportEl(scope);
|
|
1878
|
+
if (!viewportEl) return;
|
|
1879
|
+
function onWheel(event) {
|
|
1880
|
+
event.preventDefault();
|
|
1881
|
+
if (!viewportEl) return;
|
|
1882
|
+
const rect = viewportEl.getBoundingClientRect();
|
|
1883
|
+
const point = {
|
|
1884
|
+
x: event.clientX - rect.left,
|
|
1885
|
+
y: event.clientY - rect.top
|
|
1886
|
+
};
|
|
1887
|
+
send({
|
|
1888
|
+
type: "ZOOM",
|
|
1889
|
+
trigger: "wheel",
|
|
1890
|
+
delta: event.deltaY,
|
|
1891
|
+
point
|
|
1892
|
+
});
|
|
1893
|
+
}
|
|
1894
|
+
return addDomEvent(viewportEl, "wheel", onWheel, { passive: false });
|
|
1895
|
+
},
|
|
1896
|
+
trackTouchEvents({ scope, send }) {
|
|
1897
|
+
const viewportEl = getViewportEl(scope);
|
|
1898
|
+
if (!viewportEl) return;
|
|
1899
|
+
function onTouchStart(event) {
|
|
1900
|
+
if (event.touches.length >= 2) {
|
|
1901
|
+
event.preventDefault();
|
|
1902
|
+
send({
|
|
1903
|
+
type: "PINCH_START",
|
|
1904
|
+
touches: Array.from(event.touches).map((touch) => ({
|
|
1905
|
+
x: touch.clientX,
|
|
1906
|
+
y: touch.clientY
|
|
1907
|
+
}))
|
|
1908
|
+
});
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
function onTouchMove(event) {
|
|
1912
|
+
if (event.touches.length >= 2) {
|
|
1913
|
+
event.preventDefault();
|
|
1914
|
+
send({
|
|
1915
|
+
type: "PINCH_MOVE",
|
|
1916
|
+
touches: Array.from(event.touches).map((touch) => ({
|
|
1917
|
+
x: touch.clientX,
|
|
1918
|
+
y: touch.clientY
|
|
1919
|
+
}))
|
|
1920
|
+
});
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
function onTouchEnd(event) {
|
|
1924
|
+
if (event.touches.length < 2) send({ type: "PINCH_END" });
|
|
1925
|
+
}
|
|
1926
|
+
return callAll(addDomEvent(viewportEl, "touchstart", onTouchStart, { passive: false }), addDomEvent(viewportEl, "touchmove", onTouchMove, { passive: false }), addDomEvent(viewportEl, "touchend", onTouchEnd));
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
});
|
|
1931
|
+
const getBoundingRect = (el) => {
|
|
1932
|
+
const rect = el.getBoundingClientRect();
|
|
1933
|
+
return {
|
|
1934
|
+
width: rect.width,
|
|
1935
|
+
height: rect.height,
|
|
1936
|
+
top: rect.top,
|
|
1937
|
+
left: rect.left,
|
|
1938
|
+
right: rect.right,
|
|
1939
|
+
bottom: rect.bottom
|
|
1940
|
+
};
|
|
1941
|
+
};
|
|
1942
|
+
|
|
1943
|
+
//#endregion
|
|
1944
|
+
//#region src/machines/image-cropper/image-cropper.props.ts
|
|
1945
|
+
const props = createProps()([
|
|
1946
|
+
"id",
|
|
1947
|
+
"ids",
|
|
1948
|
+
"dir",
|
|
1949
|
+
"getRootNode",
|
|
1950
|
+
"initialCrop",
|
|
1951
|
+
"minWidth",
|
|
1952
|
+
"minHeight",
|
|
1953
|
+
"maxWidth",
|
|
1954
|
+
"maxHeight",
|
|
1955
|
+
"aspectRatio",
|
|
1956
|
+
"cropShape",
|
|
1957
|
+
"zoom",
|
|
1958
|
+
"rotation",
|
|
1959
|
+
"flip",
|
|
1960
|
+
"defaultZoom",
|
|
1961
|
+
"defaultRotation",
|
|
1962
|
+
"defaultFlip",
|
|
1963
|
+
"zoomStep",
|
|
1964
|
+
"zoomSensitivity",
|
|
1965
|
+
"minZoom",
|
|
1966
|
+
"maxZoom",
|
|
1967
|
+
"onZoomChange",
|
|
1968
|
+
"onRotationChange",
|
|
1969
|
+
"onFlipChange",
|
|
1970
|
+
"onCropChange",
|
|
1971
|
+
"fixedCropArea",
|
|
1972
|
+
"nudgeStep",
|
|
1973
|
+
"nudgeStepShift",
|
|
1974
|
+
"nudgeStepCtrl",
|
|
1975
|
+
"translations"
|
|
1976
|
+
]);
|
|
1977
|
+
const splitProps = createSplitProps(props);
|
|
1978
|
+
const handles = [
|
|
1979
|
+
"nw",
|
|
1980
|
+
"n",
|
|
1981
|
+
"ne",
|
|
1982
|
+
"e",
|
|
1983
|
+
"se",
|
|
1984
|
+
"s",
|
|
1985
|
+
"sw",
|
|
1986
|
+
"w"
|
|
1987
|
+
];
|
|
1988
|
+
|
|
1989
|
+
//#endregion
|
|
1990
|
+
export { anatomy, connect, handles, machine, props, splitProps };
|