@noix-dev/brix-vue 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +74 -2
- package/package.json +117 -3
- package/src/components/address-field/AddressField.vue +337 -0
- package/src/components/address-field/index.ts +1 -0
- package/src/components/appearance-picker/AppearancePicker.vue +502 -0
- package/src/components/appearance-picker/AppearanceSegment.vue +175 -0
- package/src/components/appearance-picker/index.ts +44 -0
- package/src/components/autofill/AutoFillDialog.vue +264 -0
- package/src/components/autofill/index.ts +1 -0
- package/src/components/badge/Badge.vue +23 -0
- package/src/components/badge/index.ts +106 -0
- package/src/components/build-stamp/BuildStamp.vue +62 -0
- package/src/components/build-stamp/index.ts +1 -0
- package/src/components/button/Button.vue +31 -0
- package/src/components/button/index.ts +122 -0
- package/src/components/card/Card.vue +29 -0
- package/src/components/card/CardAction.vue +17 -0
- package/src/components/card/CardContent.vue +14 -0
- package/src/components/card/CardDescription.vue +14 -0
- package/src/components/card/CardFooter.vue +22 -0
- package/src/components/card/CardHeader.vue +22 -0
- package/src/components/card/CardTitle.vue +29 -0
- package/src/components/card/index.ts +47 -0
- package/src/components/chart/BarChart.vue +123 -0
- package/src/components/chart/StackedBarChart.vue +149 -0
- package/src/components/chart/appearanceRevision.ts +22 -0
- package/src/components/chart/chartColor.ts +59 -0
- package/src/components/chart/index.ts +6 -0
- package/src/components/checkbox/Checkbox.vue +61 -0
- package/src/components/checkbox/index.ts +1 -0
- package/src/components/code-editor/CodeEditor.vue +352 -0
- package/src/components/code-editor/index.ts +9 -0
- package/src/components/code-editor/syntaxTheme.ts +110 -0
- package/src/components/colour-picker/ColourPicker.vue +421 -0
- package/src/components/colour-picker/index.ts +1 -0
- package/src/components/combobox/Combobox.vue +337 -0
- package/src/components/combobox/index.ts +1 -0
- package/src/components/comments/CommentComposer.vue +302 -0
- package/src/components/comments/CommentMessage.vue +259 -0
- package/src/components/comments/CommentThread.vue +250 -0
- package/src/components/comments/commentBody.ts +185 -0
- package/src/components/comments/commentLevels.ts +139 -0
- package/src/components/comments/commentNode.ts +35 -0
- package/src/components/comments/composer.ts +20 -0
- package/src/components/comments/index.ts +29 -0
- package/src/components/confirm-dialog/ConfirmDialog.vue +153 -0
- package/src/components/confirm-dialog/index.ts +1 -0
- package/src/components/containers/Box.vue +42 -0
- package/src/components/containers/Section.vue +88 -0
- package/src/components/containers/SectionAction.vue +17 -0
- package/src/components/containers/SectionContent.vue +14 -0
- package/src/components/containers/SectionDescription.vue +14 -0
- package/src/components/containers/SectionHeader.vue +22 -0
- package/src/components/containers/SectionTitle.vue +17 -0
- package/src/components/containers/index.ts +9 -0
- package/src/components/data-table/ColumnMenu.vue +241 -0
- package/src/components/data-table/DataTable.vue +447 -0
- package/src/components/data-table/ExpandCaret.vue +34 -0
- package/src/components/data-table/SortableHead.vue +184 -0
- package/src/components/data-table/TableLoadingRow.vue +13 -0
- package/src/components/data-table/TableProgressRow.vue +26 -0
- package/src/components/data-table/columns.ts +13 -0
- package/src/components/data-table/index.ts +3 -0
- package/src/components/data-table/pinned.ts +19 -0
- package/src/components/data-table/rowActivation.ts +14 -0
- package/src/components/data-table/sort.ts +13 -0
- package/src/components/data-table/types.ts +23 -0
- package/src/components/date-picker/CalendarPanel.vue +432 -0
- package/src/components/date-picker/DateField.vue +293 -0
- package/src/components/date-picker/DatePicker.vue +147 -0
- package/src/components/date-picker/UntilDatePicker.vue +96 -0
- package/src/components/date-picker/dateFieldGuard.ts +87 -0
- package/src/components/date-picker/index.ts +4 -0
- package/src/components/date-picker/useDateField.ts +37 -0
- package/src/components/date-range-picker/DateRangePicker.vue +247 -0
- package/src/components/date-range-picker/index.ts +1 -0
- package/src/components/date-time-picker/DateTimePicker.vue +291 -0
- package/src/components/date-time-picker/index.ts +1 -0
- package/src/components/dialog/Dialog.vue +250 -0
- package/src/components/dialog/DialogSurface.vue +214 -0
- package/src/components/dialog/dialogEdges.ts +65 -0
- package/src/components/dialog/dialogSizes.ts +35 -0
- package/src/components/dialog/index.ts +12 -0
- package/src/components/dialog/toastHost.ts +18 -0
- package/src/components/dropdown/Dropdown.vue +101 -0
- package/src/components/dropdown/DropdownItems.vue +154 -0
- package/src/components/dropdown/dropdown.ts +64 -0
- package/src/components/dropdown/index.ts +2 -0
- package/src/components/file-preview/FilePreview.vue +78 -0
- package/src/components/file-preview/index.ts +1 -0
- package/src/components/file-upload/FileUpload.vue +234 -0
- package/src/components/file-upload/index.ts +1 -0
- package/src/components/filter-check-menu/FilterCheckMenu.vue +562 -0
- package/src/components/filter-check-menu/filterCheckOption.ts +10 -0
- package/src/components/filter-check-menu/index.ts +2 -0
- package/src/components/form-field/FormField.vue +74 -0
- package/src/components/form-field/index.ts +2 -0
- package/src/components/form-field/types.ts +5 -0
- package/src/components/frames/FrameCentred.vue +137 -0
- package/src/components/frames/FrameSidebar.vue +113 -0
- package/src/components/frames/FrameSplit.vue +109 -0
- package/src/components/frames/index.ts +3 -0
- package/src/components/icon/BellIcon.vue +27 -0
- package/src/components/icon/BoldIcon.vue +31 -0
- package/src/components/icon/CalendarDaysIcon.vue +33 -0
- package/src/components/icon/CheckIcon.vue +31 -0
- package/src/components/icon/ChevronDownIcon.vue +30 -0
- package/src/components/icon/ChevronLeftIcon.vue +31 -0
- package/src/components/icon/ChevronRightIcon.vue +31 -0
- package/src/components/icon/ChevronUpIcon.vue +30 -0
- package/src/components/icon/CircleAlertIcon.vue +29 -0
- package/src/components/icon/ClockIcon.vue +31 -0
- package/src/components/icon/Columns3Icon.vue +32 -0
- package/src/components/icon/FlipHorizontalIcon.vue +29 -0
- package/src/components/icon/ItalicIcon.vue +28 -0
- package/src/components/icon/LinkIcon.vue +27 -0
- package/src/components/icon/ListIcon.vue +29 -0
- package/src/components/icon/ListOrderedIcon.vue +28 -0
- package/src/components/icon/PenLineIcon.vue +31 -0
- package/src/components/icon/PlusIcon.vue +28 -0
- package/src/components/icon/RotateCcwIcon.vue +28 -0
- package/src/components/icon/RotateCwIcon.vue +28 -0
- package/src/components/icon/SearchIcon.vue +31 -0
- package/src/components/icon/StarIcon.vue +29 -0
- package/src/components/icon/TriangleAlertIcon.vue +29 -0
- package/src/components/icon/UnderlineIcon.vue +27 -0
- package/src/components/icon/Undo2Icon.vue +28 -0
- package/src/components/icon/XIcon.vue +31 -0
- package/src/components/icon/ZoomInIcon.vue +30 -0
- package/src/components/icon/ZoomOutIcon.vue +29 -0
- package/src/components/icon/index.ts +37 -0
- package/src/components/icon-picker/IconPicker.vue +560 -0
- package/src/components/icon-picker/index.ts +1 -0
- package/src/components/image-crop-dialog/CropRatioRow.vue +132 -0
- package/src/components/image-crop-dialog/CropVariantStrip.vue +277 -0
- package/src/components/image-crop-dialog/ImageCropDialog.vue +1541 -0
- package/src/components/image-crop-dialog/index.ts +16 -0
- package/src/components/image-upload/ImageUpload.vue +345 -0
- package/src/components/image-upload/index.ts +2 -0
- package/src/components/input/Input.vue +48 -0
- package/src/components/input/index.ts +1 -0
- package/src/components/keycap/Keycap.vue +49 -0
- package/src/components/keycap/index.ts +1 -0
- package/src/components/label/Label.vue +26 -0
- package/src/components/label/index.ts +1 -0
- package/src/components/menu/MenuCheckboxItem.vue +63 -0
- package/src/components/menu/MenuContent.vue +45 -0
- package/src/components/menu/MenuGroup.vue +22 -0
- package/src/components/menu/MenuItem.vue +34 -0
- package/src/components/menu/MenuLabel.vue +22 -0
- package/src/components/menu/MenuRadioGroup.vue +22 -0
- package/src/components/menu/MenuRadioItem.vue +50 -0
- package/src/components/menu/MenuRoot.vue +25 -0
- package/src/components/menu/MenuSeparator.vue +27 -0
- package/src/components/menu/MenuSub.vue +29 -0
- package/src/components/menu/MenuSubContent.vue +48 -0
- package/src/components/menu/MenuSubTrigger.vue +49 -0
- package/src/components/menu/MenuTrigger.vue +22 -0
- package/src/components/menu/index.ts +20 -0
- package/src/components/menu/menuClasses.ts +74 -0
- package/src/components/morph-select/MorphSelect.vue +150 -0
- package/src/components/morph-select/index.ts +1 -0
- package/src/components/nav/NavFlyout.vue +343 -0
- package/src/components/nav/NavGroup.vue +306 -0
- package/src/components/nav/NavItem.vue +243 -0
- package/src/components/nav/NavNodes.ts +66 -0
- package/src/components/nav/NavSection.vue +105 -0
- package/src/components/nav/NavSidebar.vue +137 -0
- package/src/components/nav/index.ts +5 -0
- package/src/components/nav/navChain.ts +112 -0
- package/src/components/nav/navClasses.ts +229 -0
- package/src/components/nav/navNode.ts +10 -0
- package/src/components/nav/navRegistry.ts +24 -0
- package/src/components/notification-list/NotificationList.vue +314 -0
- package/src/components/notification-list/index.ts +2 -0
- package/src/components/notification-list/notifications.ts +21 -0
- package/src/components/pagination/Pagination.vue +296 -0
- package/src/components/pagination/index.ts +1 -0
- package/src/components/pin-field/PinField.vue +332 -0
- package/src/components/pin-field/index.ts +1 -0
- package/src/components/popover-panel/PopoverPanel.vue +115 -0
- package/src/components/popover-panel/index.ts +5 -0
- package/src/components/rating/Rating.vue +201 -0
- package/src/components/rating/index.ts +1 -0
- package/src/components/record-avatar/RecordAvatar.vue +76 -0
- package/src/components/record-avatar/index.ts +1 -0
- package/src/components/result-window/ResultWindow.vue +399 -0
- package/src/components/result-window/index.ts +2 -0
- package/src/components/result-window/results.ts +75 -0
- package/src/components/rich-editor/RichEditor.vue +370 -0
- package/src/components/rich-editor/RichEditorLinkDialog.vue +133 -0
- package/src/components/rich-editor/index.ts +8 -0
- package/src/components/row-actions/RowActions.vue +262 -0
- package/src/components/row-actions/index.ts +2 -0
- package/src/components/row-actions/rowActions.ts +44 -0
- package/src/components/search-select/SearchSelect.vue +599 -0
- package/src/components/search-select/index.ts +2 -0
- package/src/components/search-select/types.ts +14 -0
- package/src/components/segmented-choice/SegmentedChoice.vue +136 -0
- package/src/components/segmented-choice/index.ts +2 -0
- package/src/components/segmented-choice/segmentedOption.ts +6 -0
- package/src/components/select/Select.vue +18 -0
- package/src/components/select/SelectContent.vue +73 -0
- package/src/components/select/SelectGroup.vue +21 -0
- package/src/components/select/SelectItem.vue +47 -0
- package/src/components/select/SelectItemText.vue +12 -0
- package/src/components/select/SelectLabel.vue +17 -0
- package/src/components/select/SelectScrollDownButton.vue +36 -0
- package/src/components/select/SelectScrollUpButton.vue +36 -0
- package/src/components/select/SelectSeparator.vue +19 -0
- package/src/components/select/SelectTrigger.vue +52 -0
- package/src/components/select/SelectValue.vue +12 -0
- package/src/components/select/index.ts +11 -0
- package/src/components/session/IdleTimer.vue +71 -0
- package/src/components/session/SessionDialogs.vue +177 -0
- package/src/components/session/index.ts +2 -0
- package/src/components/split-button/SplitButton.vue +122 -0
- package/src/components/split-button/index.ts +2 -0
- package/src/components/split-button/splitButtonItem.ts +7 -0
- package/src/components/switch/Switch.vue +60 -0
- package/src/components/switch/index.ts +1 -0
- package/src/components/table/Table.vue +51 -0
- package/src/components/table/TableBody.vue +14 -0
- package/src/components/table/TableCaption.vue +17 -0
- package/src/components/table/TableCell.vue +22 -0
- package/src/components/table/TableEmpty.vue +72 -0
- package/src/components/table/TableFooter.vue +17 -0
- package/src/components/table/TableHead.vue +22 -0
- package/src/components/table/TableHeader.vue +14 -0
- package/src/components/table/TableRow.vue +22 -0
- package/src/components/table/index.ts +9 -0
- package/src/components/tabs/Tabs.vue +173 -0
- package/src/components/tabs/index.ts +2 -0
- package/src/components/tabs/tabItem.ts +12 -0
- package/src/components/textarea/LimitedTextarea.vue +81 -0
- package/src/components/textarea/TextLimitCounter.vue +107 -0
- package/src/components/textarea/Textarea.vue +42 -0
- package/src/components/textarea/index.ts +4 -0
- package/src/components/textarea/textLimit.ts +43 -0
- package/src/components/time-picker/TimePicker.vue +522 -0
- package/src/components/time-picker/index.ts +1 -0
- package/src/components/toast/ToastHost.vue +276 -0
- package/src/components/toast/index.ts +1 -0
- package/src/components/update-notice/UpdateNotice.vue +103 -0
- package/src/components/update-notice/index.ts +1 -0
- package/src/components/user-menu/UserMenu.vue +154 -0
- package/src/components/user-menu/index.ts +1 -0
- package/src/index.ts +182 -0
- package/src/lib/cn.ts +21 -0
- package/src/lib/panelFocus.ts +65 -0
- package/src/lib/sessionMonitor.ts +390 -0
- package/src/lib/slotContent.ts +17 -0
- package/src/lib/updateCheck.ts +133 -0
- package/src/styles.css +82 -0
|
@@ -0,0 +1,1541 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import {
|
|
3
|
+
asQuarter,
|
|
4
|
+
centredRect,
|
|
5
|
+
clampRect,
|
|
6
|
+
fitRectToRatio,
|
|
7
|
+
formatLabel,
|
|
8
|
+
mirrorRect,
|
|
9
|
+
resolveSelection,
|
|
10
|
+
useUiLabels,
|
|
11
|
+
type CropRect,
|
|
12
|
+
type CropResult,
|
|
13
|
+
type CropSelection,
|
|
14
|
+
type CropVariant,
|
|
15
|
+
type Quarter,
|
|
16
|
+
} from '@noix-dev/brix-core';
|
|
17
|
+
import Cropper, { type CropperCanvas, type CropperImage, type CropperSelection } from 'cropperjs';
|
|
18
|
+
import { computed, onBeforeUnmount, ref, useId, watch } from 'vue';
|
|
19
|
+
import { Button } from '../button';
|
|
20
|
+
import { Dialog } from '../dialog';
|
|
21
|
+
import {
|
|
22
|
+
CircleAlertIcon,
|
|
23
|
+
FlipHorizontalIcon,
|
|
24
|
+
RotateCcwIcon,
|
|
25
|
+
RotateCwIcon,
|
|
26
|
+
Undo2Icon,
|
|
27
|
+
ZoomInIcon,
|
|
28
|
+
ZoomOutIcon,
|
|
29
|
+
} from '../icon';
|
|
30
|
+
import CropRatioRow from './CropRatioRow.vue';
|
|
31
|
+
import CropVariantStrip from './CropVariantStrip.vue';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A modal that crops, rotates, flips and zooms an image and hands the result back as a File.
|
|
35
|
+
* Controlled, like `Dialog`: `open` and `src` in, `apply` (with the cropped file) and `cancel`
|
|
36
|
+
* out. Pressing Escape, the close button or Cancel all say `cancel`; it never closes itself.
|
|
37
|
+
*
|
|
38
|
+
* **THE DOOR.** This family carries `cropperjs`, an optional peer dependency, so it is not on
|
|
39
|
+
* the root barrel (ADR 0014): import it from `@noix-dev/brix-vue/image-crop-dialog`, and install
|
|
40
|
+
* `cropperjs` (`^2.3.1`) beside it. A consumer who never renders a cropper installs none.
|
|
41
|
+
*
|
|
42
|
+
* WORDING. The title, the button names and the load-error text come from the labels contract
|
|
43
|
+
* (`cropTitle`, `cropZoomIn`, ..., `cropLoadError`). `title` replaces the heading and `labels`
|
|
44
|
+
* overrides member by member; whatever it omits keeps the contract's wording.
|
|
45
|
+
*
|
|
46
|
+
* CROP VARIANTS. With `variants` the dialog holds one rectangle per named variant and still
|
|
47
|
+
* loads one image into one cropper: the strip right of the stage previews them all, the ratio
|
|
48
|
+
* row above it belongs to the variant that is open, and Reset re-frames that variant alone. An
|
|
49
|
+
* empty array means the same as no variants at all, and then nothing below applies. A rectangle
|
|
50
|
+
* is fractions of the frame as DISPLAYED (turn by `rotate`, mirror if `flip`, then cut), so the
|
|
51
|
+
* turn and the mirror are the image's and shared by the whole set while the rectangle is the
|
|
52
|
+
* variant's: they are read back from the first variant that carries a stored selection and
|
|
53
|
+
* written onto every selection of the set, because a set whose members disagreed about which
|
|
54
|
+
* way is up would export two crops that cannot both be right. Zoom and pan are view-only and
|
|
55
|
+
* change no rectangle; a turn re-fits every rectangle to its own ratio against the frame's new
|
|
56
|
+
* aspect, and a flip mirrors every rectangle exactly. The picture is turned and mirrored in that
|
|
57
|
+
* same order, which is NOT the order the calls go out in: cropperjs post-multiplies, so the
|
|
58
|
+
* restore lays the mirror on before the turn and a mirror pressed under a quarter turn carries its
|
|
59
|
+
* -1 on the other axis (see `buildSelections` and `flipHorizontal`). `variants` supersedes
|
|
60
|
+
* `aspectRatio`, which belongs to the single-crop caller.
|
|
61
|
+
*
|
|
62
|
+
* APPLYING THE SET. Apply exports one file per variant, in the order `variants` was given,
|
|
63
|
+
* named `cropped-<name>.jpg` or `cropped-<name>.png`, and `apply` carries them in a second
|
|
64
|
+
* argument: one `CropResult` each, the variant's `name` and `file` beside the whole selection
|
|
65
|
+
* (`ratio`, `rect`, `rotate`, `flip`, `auto`). That is the format a consumer stores, minus the
|
|
66
|
+
* file, and hands back as the variant's `selection`, so it is the public shape rather than an
|
|
67
|
+
* internal convenience. The first argument, `file`, is **the first variant's** file: the first
|
|
68
|
+
* variant is the primary one. A caller who passes no variants is emitted that one argument and
|
|
69
|
+
* no second one at all, which is why the second is optional rather than an empty list — and
|
|
70
|
+
* the one cropper is stepped through the set while the export runs, which the stage shows under
|
|
71
|
+
* the existing processing state. An export that fails is a whole apply that fails: no event, the
|
|
72
|
+
* dialog stays open, and the set is never handed over with a variant missing from it. The footer
|
|
73
|
+
* counts the variants that are framed and names the ones that are not; its button says
|
|
74
|
+
* `cropApplyAll` only for more than one variant, since `formatLabel` knows no plural and "Apply 1
|
|
75
|
+
* crops" is avoided by not asking the question.
|
|
76
|
+
* Everything the footer says of variants waits for the picture, as the strip does: before the
|
|
77
|
+
* load there are no rectangles to count, and a press then exports the one crop that is framed.
|
|
78
|
+
*
|
|
79
|
+
* ICONS. Every glyph sits behind a named slot with the inline SVG as its default (ADR 0013):
|
|
80
|
+
* `zoom-in-icon`, `zoom-out-icon`, `rotate-left-icon`, `rotate-right-icon`, `flip-icon`,
|
|
81
|
+
* `reset-icon`, `close-icon` (the dialog's X) and `load-error-icon`.
|
|
82
|
+
*
|
|
83
|
+
* COLOUR. The shell is `Dialog`, so its scrim and its panel are that component's. The toolbar
|
|
84
|
+
* buttons are `Button` in its ghost variant on a `--muted` band, and the separators are
|
|
85
|
+
* `--border`: structure, not meaning, so roles. The glyphs are ink on that band and are graphics,
|
|
86
|
+
* so the bar is 3:1: `--muted-foreground` on `--muted` measures 4.34 to 6.56 over the six
|
|
87
|
+
* theme and mode pairs (4.34 is kingfisher light; `--foreground` on `--background`, which the
|
|
88
|
+
* hover paints, is 15.29 or better). The ghost button's own hover ground is `--muted`, which would
|
|
89
|
+
* be invisible on this band, so the toolbar buttons hover onto `--background` instead.
|
|
90
|
+
*
|
|
91
|
+
* The variant strip and the ratio row are role tokens throughout, and the strip adds no second
|
|
92
|
+
* swatch. Two grounds, kept apart: the PREVIEW BOX, which holds a variant's picture, is
|
|
93
|
+
* `--muted` rather than the stage's fixed ground — a thumbnail in a list is structure, and a
|
|
94
|
+
* second fixed-palette site would have to earn its own computed figure for no gain. The TILE
|
|
95
|
+
* around that box is a `--background` button edged in `--border`, `--primary` while it is the
|
|
96
|
+
* open one. So a tile's label and the line under it are read against `--background` and its
|
|
97
|
+
* picture against `--muted`.
|
|
98
|
+
*
|
|
99
|
+
* The panel the strip stands on is `Dialog`'s `--popover`, which is a THIRD ground and not the
|
|
100
|
+
* tile's: the two tokens hold the same colour in all three light themes and part in all three
|
|
101
|
+
* dark ones, where the tile is the darker (graphite `#141618` on `#1b1e21`, noix `#0b1316` on
|
|
102
|
+
* `#121d21`, kingfisher `oklch(0.145 0 0)` on `oklch(0.205 0 0)`). A figure taken over one
|
|
103
|
+
* is therefore no figure for the other, and the footer's status line sits on the panel, not on
|
|
104
|
+
* a tile.
|
|
105
|
+
*
|
|
106
|
+
* The ratio row runs the same `--muted` band as the toolbar but its ink is NOT the toolbar's: a
|
|
107
|
+
* ratio is a word, not a glyph, so it is held to 4.5 rather than 3:1 and takes `--foreground`.
|
|
108
|
+
* The figure above is the one that does not carry over, and `CropRatioRow.vue` has the six.
|
|
109
|
+
*
|
|
110
|
+
* THE STAGE IS A SWATCH, ON PURPOSE. `bg-gray-950` is the one fixed-palette ground in this
|
|
111
|
+
* family, and it is not a role because a photo editor's ground is deliberately independent of
|
|
112
|
+
* the theme: an image is judged against a constant, and a stage that followed the theme would
|
|
113
|
+
* make the same picture look different from one mode to the next. The token contract cannot hold
|
|
114
|
+
* the pair either, since it describes role on role. So the ink that sits on the stage, the load
|
|
115
|
+
* error, is white at three weights, and its figures are computed here, not estimated, with
|
|
116
|
+
* `wash` and `contrastRatio` from `@noix-dev/brix-tokens/contrast`, over the stage's own `#030712`
|
|
117
|
+
* (Tailwind's `gray-950`, `oklch(13% 0.028 261.692)` converted to sRGB):
|
|
118
|
+
*
|
|
119
|
+
* `text-white` (#ffffff) 20.13:1 the headline
|
|
120
|
+
* `text-white/60` (#9a9ca0) 7.32:1 the hint under it
|
|
121
|
+
* `text-white/40` (#686a71) 3.73:1 the glyph, a graphic against its 3:1
|
|
122
|
+
*
|
|
123
|
+
* The error overlay takes the stage's ground with `bg-inherit` rather than naming it twice, so
|
|
124
|
+
* the pair cannot come apart. The picture itself is never read against that ground: the
|
|
125
|
+
* cropper's canvas draws a light checkerboard (white and `#ccc`, in its own stylesheet) behind
|
|
126
|
+
* an image, which is what keeps one with alpha visible. The component asks the canvas for it
|
|
127
|
+
* (`background`) instead of leaning on the library's default template.
|
|
128
|
+
*
|
|
129
|
+
* `Dialog` keeps its own `data-testid="dialog-close"` on the X; the other controls carry
|
|
130
|
+
* `crop-*` ids.
|
|
131
|
+
*/
|
|
132
|
+
|
|
133
|
+
/** Every button and aria string of the dialog except the heading, which stays the `title` prop. */
|
|
134
|
+
export interface ImageCropDialogLabels {
|
|
135
|
+
close: string;
|
|
136
|
+
zoomIn: string;
|
|
137
|
+
zoomOut: string;
|
|
138
|
+
rotateLeft: string;
|
|
139
|
+
rotateRight: string;
|
|
140
|
+
flip: string;
|
|
141
|
+
reset: string;
|
|
142
|
+
cancel: string;
|
|
143
|
+
processing: string;
|
|
144
|
+
apply: string;
|
|
145
|
+
/** Shown instead of the stage when the source image cannot be loaded. */
|
|
146
|
+
loadError: string;
|
|
147
|
+
loadErrorHint: string;
|
|
148
|
+
/** The variant strip's heading, and the marker on a variant nobody has framed. */
|
|
149
|
+
variants: string;
|
|
150
|
+
auto: string;
|
|
151
|
+
/** The ratio row's heading, its chip for one allowed ratio, and its chip for none. */
|
|
152
|
+
aspectRatio: string;
|
|
153
|
+
ratioOnly: string;
|
|
154
|
+
ratioFree: string;
|
|
155
|
+
/**
|
|
156
|
+
* The footer's count of framed variants (`{set}`, `{total}`), the sentence naming the ones
|
|
157
|
+
* that are not (`{names}`), and Apply for a set of more than one (`{count}`).
|
|
158
|
+
*/
|
|
159
|
+
variantsStatus: string;
|
|
160
|
+
variantsPending: string;
|
|
161
|
+
applyAll: string;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const props = withDefaults(
|
|
165
|
+
defineProps<{
|
|
166
|
+
open: boolean;
|
|
167
|
+
src: string | null;
|
|
168
|
+
aspectRatio?: number;
|
|
169
|
+
/**
|
|
170
|
+
* The named shapes this image is cropped into. Absent, or an empty array, is the
|
|
171
|
+
* single-crop dialog, unchanged. A variant whose `ratios` list is empty is free, and a
|
|
172
|
+
* duplicate `name` warns in development with the later variant winning.
|
|
173
|
+
*/
|
|
174
|
+
variants?: CropVariant[];
|
|
175
|
+
/** The heading and the dialog's name; the contract's `cropTitle` when left out. */
|
|
176
|
+
title?: string;
|
|
177
|
+
/** Overrides the button and aria wording member by member; omitted members keep the contract's. */
|
|
178
|
+
labels?: Partial<ImageCropDialogLabels>;
|
|
179
|
+
/**
|
|
180
|
+
* JPEG keeps a full-resolution photo or scan small enough to upload. PNG keeps
|
|
181
|
+
* transparency, for logos.
|
|
182
|
+
*/
|
|
183
|
+
outputType?: 'image/jpeg' | 'image/png';
|
|
184
|
+
}>(),
|
|
185
|
+
{
|
|
186
|
+
title: undefined,
|
|
187
|
+
outputType: 'image/jpeg',
|
|
188
|
+
},
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const emit = defineEmits<{
|
|
192
|
+
/** `file` is the first variant's; `crops` is absent for a caller who passes no variants. */
|
|
193
|
+
(e: 'apply', file: File, crops?: CropResult[]): void;
|
|
194
|
+
(e: 'cancel'): void;
|
|
195
|
+
}>();
|
|
196
|
+
|
|
197
|
+
const contract = useUiLabels();
|
|
198
|
+
|
|
199
|
+
const text = computed<ImageCropDialogLabels>(() => ({
|
|
200
|
+
close: contract.value.close,
|
|
201
|
+
zoomIn: contract.value.cropZoomIn,
|
|
202
|
+
zoomOut: contract.value.cropZoomOut,
|
|
203
|
+
rotateLeft: contract.value.cropRotateLeft,
|
|
204
|
+
rotateRight: contract.value.cropRotateRight,
|
|
205
|
+
flip: contract.value.cropFlip,
|
|
206
|
+
reset: contract.value.reset,
|
|
207
|
+
cancel: contract.value.cancel,
|
|
208
|
+
processing: contract.value.cropProcessing,
|
|
209
|
+
apply: contract.value.cropApply,
|
|
210
|
+
loadError: contract.value.cropLoadError,
|
|
211
|
+
loadErrorHint: contract.value.cropLoadErrorHint,
|
|
212
|
+
variants: contract.value.cropVariants,
|
|
213
|
+
auto: contract.value.cropVariantAuto,
|
|
214
|
+
aspectRatio: contract.value.cropAspectRatio,
|
|
215
|
+
ratioOnly: contract.value.cropRatioOnly,
|
|
216
|
+
ratioFree: contract.value.cropRatioFree,
|
|
217
|
+
variantsStatus: contract.value.cropVariantsStatus,
|
|
218
|
+
variantsPending: contract.value.cropVariantsPending,
|
|
219
|
+
applyAll: contract.value.cropApplyAll,
|
|
220
|
+
...props.labels,
|
|
221
|
+
}));
|
|
222
|
+
|
|
223
|
+
const heading = computed(() => props.title ?? contract.value.cropTitle);
|
|
224
|
+
|
|
225
|
+
const imgRef = ref<HTMLImageElement>();
|
|
226
|
+
const isExporting = ref(false);
|
|
227
|
+
// The source can simply fail to load (an expired link, a preview that is not written
|
|
228
|
+
// yet). cropperjs renders its own broken-image placeholder, which reads as "the document is
|
|
229
|
+
// gone" — so say what happened and block Apply instead.
|
|
230
|
+
const loadFailed = ref(false);
|
|
231
|
+
// What the flag may be cleared by is deliberately narrow: a `load` event, or a new source or
|
|
232
|
+
// element (see the watch below). Rebuilding the cropper is not one of them.
|
|
233
|
+
|
|
234
|
+
// A multiplicative step for the zoom buttons ($scale is cumulative, not absolute).
|
|
235
|
+
const ZOOM_STEP = 1.1;
|
|
236
|
+
// The long edge of the exported crop. A 300 dpi A4 scan is ~3500 px; 3000 px is still sharp
|
|
237
|
+
// in a printed PDF and keeps a JPEG far below an upload limit.
|
|
238
|
+
const MAX_EXPORT_EDGE = 3000;
|
|
239
|
+
const JPEG_QUALITY = 0.92;
|
|
240
|
+
|
|
241
|
+
let cropper: Cropper | null = null;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The picture's own transform, tracked here because the library moves it too; the pair that owns
|
|
245
|
+
* this is `movePicture` and `onPictureTransform`, below. Declared beside the cropper, and not
|
|
246
|
+
* beside them, because the mount watch runs before that point of the module is evaluated.
|
|
247
|
+
*
|
|
248
|
+
* A DEPTH, NOT A FLAG. `$setTransform` fires the library's `transform` event synchronously, so a
|
|
249
|
+
* transform of ours is always inside the handler that refuses it -- the whole reason the bracket
|
|
250
|
+
* exists. Nothing nests two brackets today, and the guard below is what stops it, so no test can
|
|
251
|
+
* reach the case; but a flag would have the inner `finally` clear it while the outer transform
|
|
252
|
+
* was still running, and the library would then be told about our own move as if it were its
|
|
253
|
+
* own, and re-perform it. A counter cannot be wrong that way, and costs two characters.
|
|
254
|
+
*/
|
|
255
|
+
let movingPicture = 0;
|
|
256
|
+
/** The listeners sit on the elements, so taking them off again needs the ones they went on. */
|
|
257
|
+
let trackedImage: CropperImage | null = null;
|
|
258
|
+
let trackedCanvas: CropperCanvas | null = null;
|
|
259
|
+
|
|
260
|
+
function destroyCropper(): void {
|
|
261
|
+
untrackCropper();
|
|
262
|
+
cropper?.destroy();
|
|
263
|
+
cropper = null;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function mountCropper(image: HTMLImageElement): void {
|
|
267
|
+
destroyCropper();
|
|
268
|
+
// A cropper that has just been built has laid nothing out yet, whoever built it — and its
|
|
269
|
+
// picture is unturned and unmirrored, so what was tracked of the previous one is gone with it.
|
|
270
|
+
//
|
|
271
|
+
// THE GENERATION MOVES WITH THE CROPPER, and not only with the picture (`resetVariantState`).
|
|
272
|
+
// Reset before the restore has run takes the single-crop branch and rebuilds over the same
|
|
273
|
+
// `<img>`, which starts a second `awaitFrameLayout` while the first is still outstanding — and
|
|
274
|
+
// the first one still resolves, because `destroy()` only takes the canvas out of the document
|
|
275
|
+
// and the detached image goes on loading. Both would then be current, `buildSelections` has no
|
|
276
|
+
// "already built" guard, and the stored turn would be applied twice with `rotate` reporting
|
|
277
|
+
// one of them.
|
|
278
|
+
pictureGeneration += 1;
|
|
279
|
+
frameLaidOut = false;
|
|
280
|
+
rotate.value = 0;
|
|
281
|
+
flip.value = false;
|
|
282
|
+
cropper = new Cropper(image);
|
|
283
|
+
// The library moves the picture on its own too — a wheel over the stage, a pinch — and those
|
|
284
|
+
// need the same bracket the toolbar has; it also moves the selection itself, which the
|
|
285
|
+
// open variant has to follow. See `onPictureTransform` and `onActionEnd`.
|
|
286
|
+
trackCropper();
|
|
287
|
+
// Asked here and not later: `$ready` has to be asked while the library's own picture is
|
|
288
|
+
// still loading, which it is for exactly as long as this call stack (see
|
|
289
|
+
// `awaitFrameLayout`).
|
|
290
|
+
void awaitFrameLayout();
|
|
291
|
+
// The checkerboard behind the image is what keeps one with alpha visible against the
|
|
292
|
+
// dark stage. The library's default template sets it; asking again means a change of
|
|
293
|
+
// template cannot take it away.
|
|
294
|
+
const canvas = cropper.getCropperCanvas();
|
|
295
|
+
if (canvas) {
|
|
296
|
+
canvas.background = true;
|
|
297
|
+
}
|
|
298
|
+
const selection = cropper.getCropperSelection();
|
|
299
|
+
if (!selection) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
if (props.aspectRatio !== undefined) {
|
|
303
|
+
selection.aspectRatio = props.aspectRatio;
|
|
304
|
+
}
|
|
305
|
+
selection.movable = true;
|
|
306
|
+
selection.resizable = true;
|
|
307
|
+
selection.initialCoverage = 0.85;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// ---------------------------------------------------------------------------------------------
|
|
311
|
+
// The crop variants: one rectangle per variant, one cropper, one loaded image.
|
|
312
|
+
// ---------------------------------------------------------------------------------------------
|
|
313
|
+
|
|
314
|
+
/** The source's own size in pixels, which is 0 until it has loaded. */
|
|
315
|
+
const natural = ref({ width: 0, height: 0 });
|
|
316
|
+
/** One per variant, keyed by `CropVariant.name`. */
|
|
317
|
+
const selections = ref<Record<string, CropSelection>>({});
|
|
318
|
+
const openName = ref('');
|
|
319
|
+
/** The image's turn and mirror, shared by the whole set. */
|
|
320
|
+
const rotate = ref<Quarter>(0);
|
|
321
|
+
const flip = ref(false);
|
|
322
|
+
/** The crop stage, named so that the strip's tiles can say which panel they switch. */
|
|
323
|
+
const stageId = useId();
|
|
324
|
+
|
|
325
|
+
const hasVariants = computed(() => (props.variants?.length ?? 0) > 0);
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* The variants by name. A duplicate name is the later variant, in the first one's place: a `Map`
|
|
329
|
+
* built from the list keeps the position of the first entry and the value of the last, which is
|
|
330
|
+
* also what keying the selections by name does anyway.
|
|
331
|
+
*/
|
|
332
|
+
const variantList = computed<CropVariant[]>(() => [
|
|
333
|
+
...new Map((props.variants ?? []).map((variant) => [variant.name, variant])).values(),
|
|
334
|
+
]);
|
|
335
|
+
|
|
336
|
+
const quarterTurn = computed(() => rotate.value === 90 || rotate.value === 270);
|
|
337
|
+
|
|
338
|
+
/** Width / height of the frame as displayed, which a quarter turn inverts; 0 before the load. */
|
|
339
|
+
const frameAspect = computed(() => {
|
|
340
|
+
const { width, height } = natural.value;
|
|
341
|
+
if (!(width > 0) || !(height > 0)) {
|
|
342
|
+
return 0;
|
|
343
|
+
}
|
|
344
|
+
return quarterTurn.value ? height / width : width / height;
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
const openIndex = computed(() => {
|
|
348
|
+
const at = variantList.value.findIndex((variant) => variant.name === openName.value);
|
|
349
|
+
return at === -1 ? 0 : at;
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
const openVariant = computed(() => variantList.value[openIndex.value]);
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* The name to tell the strip, which is the name of the variant this dialog treats as open rather
|
|
356
|
+
* than `openName` itself. The two differ only where `openName` names nothing in the list, and
|
|
357
|
+
* then `openIndex` falls back to the first variant while the raw name would mark no tile: no
|
|
358
|
+
* `aria-selected`, no `tabindex="0"`, a tablist nothing can reach. `buildSelections` and
|
|
359
|
+
* `syncVariantList` both normalise `openName`, so the divergence is unreachable -- stating the
|
|
360
|
+
* open variant's own name is simply shorter than the argument for why it cannot happen.
|
|
361
|
+
*/
|
|
362
|
+
const openTabName = computed(() => openVariant.value?.name ?? '');
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* The other end of the tab relationship, minted here like the stage's own id: the tiles carry
|
|
366
|
+
* these and the panel names the open one as its label, so both halves agree by construction
|
|
367
|
+
* rather than each side building a name the other has to guess. Positional, because a variant's
|
|
368
|
+
* name is consumer data and may hold anything.
|
|
369
|
+
*/
|
|
370
|
+
const tabIds = computed(() => variantList.value.map((_, at) => `${stageId}-tab-${at}`));
|
|
371
|
+
const openTabId = computed(() => tabIds.value[openIndex.value]);
|
|
372
|
+
|
|
373
|
+
const openSelection = computed(() =>
|
|
374
|
+
openVariant.value ? selections.value[openVariant.value.name] : undefined,
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Everything the strip and the ratio row need is in place: the frame has a size and every
|
|
379
|
+
* variant has a rectangle. The strip draws a tile with no frame for a variant it has no entry
|
|
380
|
+
* for, and this is what keeps that branch unreachable in real use.
|
|
381
|
+
*/
|
|
382
|
+
const variantsReady = computed(
|
|
383
|
+
() =>
|
|
384
|
+
hasVariants.value &&
|
|
385
|
+
frameAspect.value > 0 &&
|
|
386
|
+
variantList.value.every((variant) => !!selections.value[variant.name]),
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* The footer's two lines. `auto` is what says a variant has never been framed, so the count and
|
|
391
|
+
* the list are the two halves of the same question and each is counted from the list itself
|
|
392
|
+
* rather than one being derived from the other: a variant with no selection at all — which
|
|
393
|
+
* `variantsReady` rules out — would then be reported as neither set nor pending, instead of
|
|
394
|
+
* padding whichever figure was taken as the remainder.
|
|
395
|
+
*/
|
|
396
|
+
const pendingVariants = computed(() =>
|
|
397
|
+
variantList.value.filter((variant) => selections.value[variant.name]?.auto === true),
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
const setVariantCount = computed(
|
|
401
|
+
() =>
|
|
402
|
+
variantList.value.filter((variant) => selections.value[variant.name]?.auto === false)
|
|
403
|
+
.length,
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
const variantsStatus = computed(() =>
|
|
407
|
+
formatLabel(text.value.variantsStatus, {
|
|
408
|
+
set: setVariantCount.value,
|
|
409
|
+
total: variantList.value.length,
|
|
410
|
+
}),
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
const variantsPending = computed(() =>
|
|
414
|
+
formatLabel(text.value.variantsPending, {
|
|
415
|
+
names: pendingVariants.value.map((variant) => variant.label).join(', '),
|
|
416
|
+
}),
|
|
417
|
+
);
|
|
418
|
+
|
|
419
|
+
/** `cropApplyAll` only for a set: `formatLabel` has no plural, so one variant is not asked. */
|
|
420
|
+
const applyText = computed(() =>
|
|
421
|
+
variantsReady.value && variantList.value.length > 1
|
|
422
|
+
? formatLabel(text.value.applyAll, { count: variantList.value.length })
|
|
423
|
+
: text.value.apply,
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
/** A variant with no allowed ratios is free, and so is a selection naming a ratio it lost. */
|
|
427
|
+
function ratioValue(variant: CropVariant | undefined, name: string): number | null {
|
|
428
|
+
return variant?.ratios.find((ratio) => ratio.name === name)?.value ?? null;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
interface PixelBox {
|
|
432
|
+
x: number;
|
|
433
|
+
y: number;
|
|
434
|
+
width: number;
|
|
435
|
+
height: number;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* The displayed frame in the pixels the cropper measures its selection in, which are its
|
|
440
|
+
* canvas's. The size is the natural size times the transform's scale rather than the element's
|
|
441
|
+
* measured width: it is exact for every quarter turn, and it is the quantity `exportSize` already
|
|
442
|
+
* derives from the same matrix, so the two cannot drift apart. The corner is measured, because
|
|
443
|
+
* only the document knows where the canvas put the picture.
|
|
444
|
+
*
|
|
445
|
+
* Null before the image has loaded: there is no size then, and a conversion that divided by it
|
|
446
|
+
* would hand every variant a rectangle of NaN.
|
|
447
|
+
*/
|
|
448
|
+
function frameBox(): PixelBox | null {
|
|
449
|
+
const image = cropper?.getCropperImage();
|
|
450
|
+
if (!image) {
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
const [a = 1, b = 0] = image.$getTransform() ?? [];
|
|
454
|
+
const scale = Math.hypot(a, b) || 1;
|
|
455
|
+
const width = (quarterTurn.value ? natural.value.height : natural.value.width) * scale;
|
|
456
|
+
const height = (quarterTurn.value ? natural.value.width : natural.value.height) * scale;
|
|
457
|
+
if (!(width > 0) || !(height > 0)) {
|
|
458
|
+
return null;
|
|
459
|
+
}
|
|
460
|
+
const picture = image.getBoundingClientRect();
|
|
461
|
+
const canvas = cropper?.getCropperCanvas()?.getBoundingClientRect();
|
|
462
|
+
return {
|
|
463
|
+
x: picture.left - (canvas?.left ?? 0),
|
|
464
|
+
y: picture.top - (canvas?.top ?? 0),
|
|
465
|
+
width,
|
|
466
|
+
height,
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// The two directions of one conversion, side by side, so they cannot drift apart.
|
|
471
|
+
|
|
472
|
+
function toPixels(rect: CropRect, frame: PixelBox): PixelBox {
|
|
473
|
+
return {
|
|
474
|
+
x: frame.x + rect.x * frame.width,
|
|
475
|
+
y: frame.y + rect.y * frame.height,
|
|
476
|
+
width: rect.width * frame.width,
|
|
477
|
+
height: rect.height * frame.height,
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function toRect(box: PixelBox, frame: PixelBox): CropRect {
|
|
482
|
+
// Clamped on the way in as well as on the way out: cropperjs lets a box be dragged onto the
|
|
483
|
+
// stage beside the picture, and a fraction outside 0..1 means nothing when it is applied again.
|
|
484
|
+
return clampRect({
|
|
485
|
+
x: (box.x - frame.x) / frame.width,
|
|
486
|
+
y: (box.y - frame.y) / frame.height,
|
|
487
|
+
width: box.width / frame.width,
|
|
488
|
+
height: box.height / frame.height,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* The box the dialog itself last laid on the cropper, re-read from the element so that the
|
|
494
|
+
* library's own rounding and clamping are part of it. A box that still equals this one is not
|
|
495
|
+
* something a person moved, so reading it back would be the dialog overwriting a stored
|
|
496
|
+
* rectangle with its own arithmetic — on a zoom, that would silently re-frame the variant.
|
|
497
|
+
*/
|
|
498
|
+
let written: PixelBox | null = null;
|
|
499
|
+
/** Well under the whole pixel cropperjs rounds its own box to. */
|
|
500
|
+
const BOX_EPSILON = 0.01;
|
|
501
|
+
|
|
502
|
+
function sameBox(one: PixelBox, other: PixelBox): boolean {
|
|
503
|
+
return (['x', 'y', 'width', 'height'] as const).every(
|
|
504
|
+
(edge) => Math.abs(one[edge] - other[edge]) < BOX_EPSILON,
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function selectionBox(element: { x: number; y: number; width: number; height: number }): PixelBox {
|
|
509
|
+
return { x: element.x, y: element.y, width: element.width, height: element.height };
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Lays one variant's rectangle, and the ratio it is held to, on the cropper, and says what the
|
|
514
|
+
* element then reads as — null when there is nothing to lay it on. The export walks the whole
|
|
515
|
+
* set through here, which is why it is not the open variant's own function.
|
|
516
|
+
*/
|
|
517
|
+
function writeSelection(variant: CropVariant, selection: CropSelection): PixelBox | null {
|
|
518
|
+
const element = cropper?.getCropperSelection();
|
|
519
|
+
const frame = frameBox();
|
|
520
|
+
if (!element || !frame) {
|
|
521
|
+
return null;
|
|
522
|
+
}
|
|
523
|
+
// The library reads any aspect ratio that is not a positive number as none at all, which is
|
|
524
|
+
// what a free variant wants.
|
|
525
|
+
element.aspectRatio = ratioValue(variant, selection.ratio) ?? NaN;
|
|
526
|
+
const box = toPixels(selection.rect, frame);
|
|
527
|
+
element.$change(box.x, box.y, box.width, box.height);
|
|
528
|
+
return selectionBox(element);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** Lays the open variant's rectangle on the cropper, and remembers the box that was laid. */
|
|
532
|
+
function writeOpenSelection(): void {
|
|
533
|
+
written = null;
|
|
534
|
+
const variant = openVariant.value;
|
|
535
|
+
const selection = openSelection.value;
|
|
536
|
+
if (!variant || !selection) {
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
written = writeSelection(variant, selection);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** Reads the cropper's box back into the open variant, if a person has moved it. */
|
|
543
|
+
function readOpenSelection(): void {
|
|
544
|
+
const element = cropper?.getCropperSelection();
|
|
545
|
+
const frame = frameBox();
|
|
546
|
+
const variant = openVariant.value;
|
|
547
|
+
const selection = openSelection.value;
|
|
548
|
+
if (!element || !frame || !variant || !selection || !written) {
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
const box = selectionBox(element);
|
|
552
|
+
if (sameBox(box, written)) {
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
// A rectangle somebody moved is no longer a computed one.
|
|
556
|
+
selections.value = {
|
|
557
|
+
...selections.value,
|
|
558
|
+
[variant.name]: { ...selection, rect: toRect(box, frame), auto: false },
|
|
559
|
+
};
|
|
560
|
+
written = box;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** Applies a change to every variant's rectangle, the ones nobody has opened included. */
|
|
564
|
+
function mapSelections(
|
|
565
|
+
change: (selection: CropSelection, variant: CropVariant) => CropSelection,
|
|
566
|
+
): void {
|
|
567
|
+
const next: Record<string, CropSelection> = {};
|
|
568
|
+
for (const variant of variantList.value) {
|
|
569
|
+
const selection = selections.value[variant.name];
|
|
570
|
+
if (selection) {
|
|
571
|
+
next[variant.name] = change(selection, variant);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
selections.value = next;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* One warning per list, not one per restore: the same array is not news twice, and a variant set
|
|
579
|
+
* is read again on every reopen and on every new source. The guard is on the array's identity,
|
|
580
|
+
* not on its contents, so a caller who writes `:variants="[...]"` inline hands over a new array
|
|
581
|
+
* on every render and will be told again each time the list is read — which is the right side to
|
|
582
|
+
* err on, since the second warning says the same true thing about the same mistake.
|
|
583
|
+
*/
|
|
584
|
+
let warnedFor: CropVariant[] | undefined;
|
|
585
|
+
|
|
586
|
+
function warnAboutDuplicateNames(): void {
|
|
587
|
+
if (process.env.NODE_ENV === 'production' || warnedFor === props.variants) {
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
warnedFor = props.variants;
|
|
591
|
+
const names = (props.variants ?? []).map((variant) => variant.name);
|
|
592
|
+
const duplicates = [...new Set(names.filter((name, at) => names.indexOf(name) !== at))];
|
|
593
|
+
if (duplicates.length === 0) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
console.warn(
|
|
597
|
+
`Warning: \`ImageCropDialog\` was given more than one crop variant named ${duplicates
|
|
598
|
+
.map((name) => `\`${name}\``)
|
|
599
|
+
.join(', ')}.\n\n` +
|
|
600
|
+
'A variant name keys its crop, so the last variant of each name is the one that is used.',
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* The restore, run once the picture has loaded AND the cropper has laid it out, so that the frame
|
|
606
|
+
* it converts against is the frame the rectangles are laid on (see `awaitFrameLayout`).
|
|
607
|
+
*
|
|
608
|
+
* It is not only the rectangles: the stored `rotate` and `flip` describe the frame those
|
|
609
|
+
* rectangles were drawn against, so the image is turned and mirrored back first, through the
|
|
610
|
+
* same calls the toolbar makes, and the rectangles are laid on it afterwards. They are the
|
|
611
|
+
* image's and identical across one set, so they are taken from the first variant that carries a
|
|
612
|
+
* selection, every later variant's copy is ignored, and the set is normalised onto them.
|
|
613
|
+
*/
|
|
614
|
+
function buildSelections(): void {
|
|
615
|
+
if (!hasVariants.value || frameAspect.value <= 0) {
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
warnAboutDuplicateNames();
|
|
619
|
+
const carried = variantList.value.find((variant) => variant.selection)?.selection;
|
|
620
|
+
const turn = asQuarter(carried?.rotate);
|
|
621
|
+
const mirrored = carried?.flip === true;
|
|
622
|
+
rotate.value = turn;
|
|
623
|
+
flip.value = mirrored;
|
|
624
|
+
// The picture has not been turned yet, so `layOnOrientation` is exactly what this needs.
|
|
625
|
+
movePicture((image) => {
|
|
626
|
+
layOnOrientation(image, turn, mirrored);
|
|
627
|
+
});
|
|
628
|
+
const next: Record<string, CropSelection> = {};
|
|
629
|
+
for (const variant of variantList.value) {
|
|
630
|
+
next[variant.name] = {
|
|
631
|
+
...resolveSelection(variant, frameAspect.value),
|
|
632
|
+
rotate: turn,
|
|
633
|
+
flip: mirrored,
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
selections.value = next;
|
|
637
|
+
openName.value = variantList.value[0]?.name ?? '';
|
|
638
|
+
writeOpenSelection();
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* A new list while the dialog is open: every variant keeps the rectangle it has, a variant
|
|
643
|
+
* that is new gets its own, a name that is gone is dropped, and an open variant that is gone
|
|
644
|
+
* falls back to the first of the new list rather than to an index past its end. The image's
|
|
645
|
+
* transform is not touched, because turning it back a second time would compound the turn.
|
|
646
|
+
*
|
|
647
|
+
* A STORED `selection` THAT ARRIVES LATER IS NOT READ. "Keeps the rectangle it has" is about the
|
|
648
|
+
* name, not the object: a variant whose name is already known keeps the selection this dialog
|
|
649
|
+
* built, so a consumer who opens with the shapes and then replaces `variants` with the same
|
|
650
|
+
* shapes now carrying stored rectangles -- the shape of an asynchronous load -- gets no restore
|
|
651
|
+
* and no warning. Only a name that is new reads its `selection`, and even then the stored
|
|
652
|
+
* `rotate` and `flip` give way to the picture's current turn, because the picture is shared and
|
|
653
|
+
* turning it under an open dialog would compound the turn above. Taking a late selection would
|
|
654
|
+
* mean deciding which wins once the person has already dragged, so the restore stays at the one
|
|
655
|
+
* point where that question cannot arise: the open.
|
|
656
|
+
*/
|
|
657
|
+
function syncVariantList(): void {
|
|
658
|
+
if (!hasVariants.value || frameAspect.value <= 0) {
|
|
659
|
+
selections.value = {};
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
warnAboutDuplicateNames();
|
|
663
|
+
readOpenSelection();
|
|
664
|
+
const next: Record<string, CropSelection> = {};
|
|
665
|
+
for (const variant of variantList.value) {
|
|
666
|
+
next[variant.name] =
|
|
667
|
+
selections.value[variant.name] ??
|
|
668
|
+
({
|
|
669
|
+
...resolveSelection(variant, frameAspect.value),
|
|
670
|
+
rotate: rotate.value,
|
|
671
|
+
flip: flip.value,
|
|
672
|
+
} satisfies CropSelection);
|
|
673
|
+
}
|
|
674
|
+
selections.value = next;
|
|
675
|
+
if (!next[openName.value]) {
|
|
676
|
+
openName.value = variantList.value[0]?.name ?? '';
|
|
677
|
+
}
|
|
678
|
+
writeOpenSelection();
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Which cropper the stage is on — the picture's identity and the cropper's both, since a rebuild
|
|
683
|
+
* over the same picture leaves the old one's wait outstanding just as a new source does. The
|
|
684
|
+
* restore waits, so it can return to a dialog that has since been given another source, been
|
|
685
|
+
* closed, or had its cropper rebuilt under it: it carries the generation it was started for, and
|
|
686
|
+
* one that is no longer current writes nothing.
|
|
687
|
+
*/
|
|
688
|
+
let pictureGeneration = 0;
|
|
689
|
+
/** Whether the cropper has laid the current picture out; see `awaitFrameLayout`. */
|
|
690
|
+
let frameLaidOut = false;
|
|
691
|
+
|
|
692
|
+
function resetVariantState(): void {
|
|
693
|
+
pictureGeneration += 1;
|
|
694
|
+
frameLaidOut = false;
|
|
695
|
+
natural.value = { width: 0, height: 0 };
|
|
696
|
+
selections.value = {};
|
|
697
|
+
openName.value = '';
|
|
698
|
+
rotate.value = 0;
|
|
699
|
+
flip.value = false;
|
|
700
|
+
written = null;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* THE RESTORE WAITS FOR TWO THINGS and runs on whichever arrives second: the `<img>`'s own `load`,
|
|
705
|
+
* which is the only thing that knows the natural size, and the cropper having laid the picture
|
|
706
|
+
* out, which is what gives the frame its size on screen.
|
|
707
|
+
*
|
|
708
|
+
* Waiting for the second one is the whole point. A rectangle is fractions of the DISPLAYED frame,
|
|
709
|
+
* so converting one needs the frame the rectangle will then be laid on — and the `<img>`'s `load`
|
|
710
|
+
* is not where that is true. cropperjs mounts a second image with the same source inside its
|
|
711
|
+
* canvas and sizes and centres the picture when THAT one is handled; for a source that is already
|
|
712
|
+
* in hand (a `data:` URI) ours wins the race, so `frameBox` still measured the natural size at
|
|
713
|
+
* scale 1 and the first variant's rectangle was converted against it and then laid on a frame
|
|
714
|
+
* about to be fitted: the right shape in the wrong place, on the first thing a person looks at.
|
|
715
|
+
* An object URL loses the race the other way, which is why this never showed through
|
|
716
|
+
* `ImageUpload`, and not because it is rare.
|
|
717
|
+
*/
|
|
718
|
+
function restoreWhenLoadedAndLaidOut(): void {
|
|
719
|
+
if (!frameLaidOut || !(natural.value.width > 0) || !(natural.value.height > 0)) {
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
buildSelections();
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* `cropper-image`'s `$ready` resolves after the library's own `load` handler has run, which is
|
|
727
|
+
* the call that sizes and centres the picture — BUT ONLY IF IT IS ASKED WHILE THAT PICTURE IS
|
|
728
|
+
* STILL LOADING. Measured, not assumed: asked from the stage image's `load` handler it answers at
|
|
729
|
+
* once and with the picture still at scale 1, because `complete` on the library's image turns
|
|
730
|
+
* true before the `load` event that lays it out is dispatched, and `$ready` resolves straight
|
|
731
|
+
* away for an image that is already complete. Asked from `mountCropper` instead, the library's
|
|
732
|
+
* image has had no chance to finish, so `$ready` takes its other path: it waits for that image's
|
|
733
|
+
* `load`, behind the library's own handler for the same event, and then defers a turn on top.
|
|
734
|
+
*
|
|
735
|
+
* Nothing of the set is on the stage while this is outstanding: `variantsReady` stays false until
|
|
736
|
+
* `buildSelections` has run, so the strip, the ratio row and the footer's line are no more
|
|
737
|
+
* present during the wait than they were before the load, and the transform buttons keep the
|
|
738
|
+
* guard they already had.
|
|
739
|
+
*/
|
|
740
|
+
async function awaitFrameLayout(): Promise<void> {
|
|
741
|
+
const generation = pictureGeneration;
|
|
742
|
+
try {
|
|
743
|
+
await cropper?.getCropperImage()?.$ready();
|
|
744
|
+
} catch {
|
|
745
|
+
// `$ready` rejects for a source the library's own image cannot load. The stage image
|
|
746
|
+
// reports that in its own right through `error`, and there is no frame either way.
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
if (generation !== pictureGeneration) {
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
frameLaidOut = true;
|
|
753
|
+
restoreWhenLoadedAndLaidOut();
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/** The natural size, and with it every rectangle, only exists once the picture has loaded. */
|
|
757
|
+
function onImageLoad(): void {
|
|
758
|
+
loadFailed.value = false;
|
|
759
|
+
natural.value = {
|
|
760
|
+
width: imgRef.value?.naturalWidth ?? 0,
|
|
761
|
+
height: imgRef.value?.naturalHeight ?? 0,
|
|
762
|
+
};
|
|
763
|
+
restoreWhenLoadedAndLaidOut();
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
function pickVariant(name: string): void {
|
|
767
|
+
if (name === openName.value) {
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
readOpenSelection();
|
|
771
|
+
openName.value = name;
|
|
772
|
+
writeOpenSelection();
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
function pickRatio(name: string): void {
|
|
776
|
+
const variant = openVariant.value;
|
|
777
|
+
if (!variant || openSelection.value?.ratio === name) {
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
readOpenSelection();
|
|
781
|
+
const selection = openSelection.value;
|
|
782
|
+
if (!selection) {
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
selections.value = {
|
|
786
|
+
...selections.value,
|
|
787
|
+
[variant.name]: {
|
|
788
|
+
...selection,
|
|
789
|
+
ratio: name,
|
|
790
|
+
// The box keeps its centre and its height; the width follows the new ratio.
|
|
791
|
+
rect: fitRectToRatio(selection.rect, ratioValue(variant, name), frameAspect.value),
|
|
792
|
+
auto: false,
|
|
793
|
+
},
|
|
794
|
+
};
|
|
795
|
+
writeOpenSelection();
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
watch(() => props.variants, syncVariantList);
|
|
799
|
+
|
|
800
|
+
// The image lives inside the dialog's content, which is teleported and only appears a tick
|
|
801
|
+
// after `open` turns true, so the cropper is built when the element itself shows up (and
|
|
802
|
+
// again for a new `src`), not at a fixed delay after the prop. `flush: 'post'` makes the
|
|
803
|
+
// `src` attribute current by the time the library reads it.
|
|
804
|
+
watch(
|
|
805
|
+
() => [props.open, props.src, imgRef.value] as const,
|
|
806
|
+
([open, src, image]) => {
|
|
807
|
+
if (open && src && image) {
|
|
808
|
+
// A source or an element that is new reports for itself (`load` or `error`), so
|
|
809
|
+
// the failure is cleared here and nowhere else. `mountCropper` must not clear it:
|
|
810
|
+
// Reset rebuilds the cropper over the same `<img>`, which does not load again, so
|
|
811
|
+
// no second `error` would ever put the flag back and Apply would export a broken
|
|
812
|
+
// picture as the crop.
|
|
813
|
+
loadFailed.value = false;
|
|
814
|
+
// A picture that is new, or gone, takes the frame and the rectangles with it: they
|
|
815
|
+
// describe the previous one, and the window before the new `load` would otherwise be
|
|
816
|
+
// a strip of the old picture's boxes over the new picture's source.
|
|
817
|
+
resetVariantState();
|
|
818
|
+
mountCropper(image);
|
|
819
|
+
} else {
|
|
820
|
+
destroyCropper();
|
|
821
|
+
resetVariantState();
|
|
822
|
+
}
|
|
823
|
+
},
|
|
824
|
+
{ immediate: true, flush: 'post' },
|
|
825
|
+
);
|
|
826
|
+
|
|
827
|
+
watch(
|
|
828
|
+
() => props.aspectRatio,
|
|
829
|
+
(ratio) => {
|
|
830
|
+
// With variants the ratio in force is the open variant's, so this prop is the
|
|
831
|
+
// single-crop caller's alone: a later change of it must not pull the cropper away from
|
|
832
|
+
// the ratio the rectangle on screen was fitted to.
|
|
833
|
+
if (ratio === undefined || hasVariants.value) {
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
const selection = cropper?.getCropperSelection();
|
|
837
|
+
if (selection) {
|
|
838
|
+
selection.aspectRatio = ratio;
|
|
839
|
+
}
|
|
840
|
+
},
|
|
841
|
+
);
|
|
842
|
+
|
|
843
|
+
onBeforeUnmount(destroyCropper);
|
|
844
|
+
|
|
845
|
+
/** A transform of the dialog's own, marked as one. */
|
|
846
|
+
function movePicture(move: (image: CropperImage) => void): void {
|
|
847
|
+
const image = cropper?.getCropperImage();
|
|
848
|
+
if (!image) {
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
movingPicture += 1;
|
|
852
|
+
try {
|
|
853
|
+
move(image);
|
|
854
|
+
} finally {
|
|
855
|
+
movingPicture -= 1;
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
/**
|
|
860
|
+
* THE INVARIANT, COMPOSED IN ONE PLACE: the picture's matrix is always `z · S^flip · R_rotate`,
|
|
861
|
+
* and this lays the `S^flip · R_rotate` half of it onto a picture that carries no turn and no
|
|
862
|
+
* mirror of its own — a cropper just built, or a matrix just set to a bare scale.
|
|
863
|
+
*
|
|
864
|
+
* THE MIRROR GOES ON BEFORE THE TURN, because the library post-multiplies: `$scale` and then
|
|
865
|
+
* `$rotate` composes `S·R`, which is the order the spec fixes and the order the strip's CSS
|
|
866
|
+
* paints. The other way round composes `R·S` — for a quarter turn the same picture 180 degrees
|
|
867
|
+
* from the frame the stored rectangles were drawn against, with every ratio still correct, so it
|
|
868
|
+
* looks right until someone checks. The operand is the plain `(-1, 1)` and not `flipHorizontal`'s
|
|
869
|
+
* conjugate, because the picture this is laid on is unturned when the mirror reaches it; neither
|
|
870
|
+
* call touches the matrix's `e`/`f`, so a translation already set survives both.
|
|
871
|
+
*/
|
|
872
|
+
function layOnOrientation(image: CropperImage, turn: Quarter, mirrored: boolean): void {
|
|
873
|
+
if (mirrored) {
|
|
874
|
+
image.$scale(-1, 1);
|
|
875
|
+
}
|
|
876
|
+
if (turn !== 0) {
|
|
877
|
+
image.$rotate(`${turn}deg`);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
/** Both of the library's own hands on the crop, taken together and let go together. */
|
|
882
|
+
function trackCropper(): void {
|
|
883
|
+
untrackCropper();
|
|
884
|
+
trackedImage = cropper?.getCropperImage() ?? null;
|
|
885
|
+
trackedImage?.addEventListener('transform', onPictureTransform);
|
|
886
|
+
trackedCanvas = cropper?.getCropperCanvas() ?? null;
|
|
887
|
+
trackedCanvas?.addEventListener('actionend', onActionEnd);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
function untrackCropper(): void {
|
|
891
|
+
trackedImage?.removeEventListener('transform', onPictureTransform);
|
|
892
|
+
trackedImage = null;
|
|
893
|
+
trackedCanvas?.removeEventListener('actionend', onActionEnd);
|
|
894
|
+
trackedCanvas = null;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* THE BOX A PERSON DRAGGED IS THE VARIANT'S RECTANGLE, from the moment the gesture ends. cropperjs
|
|
899
|
+
* moves and resizes its own selection and tells nobody, and nothing here listened: the open
|
|
900
|
+
* variant's tile went on previewing the rectangle from before the drag, went on wearing its dashed
|
|
901
|
+
* edge and its `auto` word, and the footer went on counting it among the ones nobody has framed —
|
|
902
|
+
* all of it correcting itself only when the person clicked something else. The spec ties `auto` to
|
|
903
|
+
* a change of the rectangle, and that is the drag, not the next click.
|
|
904
|
+
*
|
|
905
|
+
* `actionend` is the canvas's own end of gesture (`cropper.js:1035`): once per drag or resize, not
|
|
906
|
+
* once per frame, so the strip is not re-rendered while a pointer is moving. `readOpenSelection`
|
|
907
|
+
* is what carries it, so the `written` guard still decides whether anything moved at all — a box
|
|
908
|
+
* the dialog laid on itself is not read back as somebody's framing, and no read can attribute one
|
|
909
|
+
* variant's rectangle to another.
|
|
910
|
+
*/
|
|
911
|
+
function onActionEnd(): void {
|
|
912
|
+
if (!variantsReady.value) {
|
|
913
|
+
return;
|
|
914
|
+
}
|
|
915
|
+
readOpenSelection();
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* A quarter turn, a mirror and a uniform scale each leave one of the matrix's two diagonals at
|
|
920
|
+
* zero. Anything else turns the picture off the axes, which no rectangle here can describe.
|
|
921
|
+
*/
|
|
922
|
+
function axisAligned(matrix: number[]): boolean {
|
|
923
|
+
const [a = 0, b = 0, c = 0, d = 0] = matrix;
|
|
924
|
+
const flat = 1e-9;
|
|
925
|
+
return (Math.abs(b) < flat && Math.abs(c) < flat) || (Math.abs(a) < flat && Math.abs(d) < flat);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* THE PICTURE MOVES BY MORE THAN THE TOOLBAR. cropperjs binds a `wheel` listener on its canvas
|
|
930
|
+
* whenever the canvas is not disabled (`cropper.js:746`) and scales the picture by its own
|
|
931
|
+
* `scaleStep` through `ACTION_SCALE` (`:701`, `:1044`); two pointers on the canvas make an
|
|
932
|
+
* `ACTION_TRANSFORM`, which scales AND turns in one matrix. None of that passes through the
|
|
933
|
+
* toolbar, so none of it was bracketed: the selection kept its own place in the canvas while the
|
|
934
|
+
* picture moved under it, and Apply exported the stored rectangle rather than the framing the
|
|
935
|
+
* stage was showing. It is the defect the zoom buttons were already fixed for, left live on the
|
|
936
|
+
* paths the dialog does not own.
|
|
937
|
+
*
|
|
938
|
+
* `$setTransform` offers the new matrix as a cancelable `transform` event BEFORE it applies it
|
|
939
|
+
* (`cropper.js:1922`), so this listener can own the whole change: it refuses the library's own
|
|
940
|
+
* application and performs the very same matrix itself, inside the bracket. The wheel keeps
|
|
941
|
+
* working as a zoom, and the box follows the picture.
|
|
942
|
+
*
|
|
943
|
+
* A MATRIX THAT IS NOT AXIS-ALIGNED LOSES ITS TURN AND KEEPS ITS SCALE, which is not the same as
|
|
944
|
+
* being dropped. Every rectangle here is fractions of an axis-aligned frame, and `frameBox`
|
|
945
|
+
* measures that frame as the natural size times the matrix's scale — exact for a quarter turn,
|
|
946
|
+
* meaningless for the off-axis turn a twisting pinch produces, which `asQuarter` would refuse
|
|
947
|
+
* afterwards anyway. So the turn goes. But the zoom in the same matrix must not go with it:
|
|
948
|
+
* `cropper-canvas` classifies a two-pointer gesture per frame from measured rates and emits
|
|
949
|
+
* `ACTION_SCALE` only for a frame whose rotate-rate is exactly 0 (`cropper.js:987`), so every
|
|
950
|
+
* frame of a gesture made by human fingers is an `ACTION_TRANSFORM` with the turn and the scale
|
|
951
|
+
* baked into one operand (`:1477`) — and dropping those frames whole is a pinch that does not
|
|
952
|
+
* zoom. The uniform scale comes out as `hypot(a, b)` and is performed on its own.
|
|
953
|
+
*
|
|
954
|
+
* It is performed as the picture's OWN orientation at that scale, because `$setTransform` sets
|
|
955
|
+
* the WHOLE matrix rather than composing a delta onto it: a bare `[z, 0, 0, z]` would throw away
|
|
956
|
+
* every quarter turn and the mirror the picture carries. `layOnOrientation` rebuilds them from
|
|
957
|
+
* the pair this component tracks, which is the one invariant — `z · S^flip · R_rotate` — that
|
|
958
|
+
* the toolbar and the restore keep too. So a pinch zooms the picture and a twist does not turn
|
|
959
|
+
* it: the toolbar's quarter turns are the only turns this dialog can describe.
|
|
960
|
+
*
|
|
961
|
+
* A WINDOW RESIZE IS NOT IN THIS CLASS and needs nothing. The library re-centres and re-fits the
|
|
962
|
+
* picture on its own `load` alone (`$handleLoad` calls `$center`, `cropper.js:1399`); nothing in
|
|
963
|
+
* it listens for a resize except the shade's own render, and both the picture's offset and the
|
|
964
|
+
* selection's box are canvas pixels. A canvas that changes size therefore moves neither of them,
|
|
965
|
+
* and no fraction changes meaning.
|
|
966
|
+
*/
|
|
967
|
+
function onPictureTransform(event: Event): void {
|
|
968
|
+
if (movingPicture > 0 || !variantsReady.value) {
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
const matrix = (event as CustomEvent<{ matrix?: number[] }>).detail?.matrix;
|
|
972
|
+
event.preventDefault();
|
|
973
|
+
if (!matrix) {
|
|
974
|
+
return;
|
|
975
|
+
}
|
|
976
|
+
const offAxis = !axisAligned(matrix);
|
|
977
|
+
readOpenSelection();
|
|
978
|
+
movePicture((image) => {
|
|
979
|
+
if (!offAxis) {
|
|
980
|
+
image.$setTransform(matrix);
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
983
|
+
// The scale and the offset the library wanted, under the turn and the mirror the picture
|
|
984
|
+
// already has rather than the one the gesture asked for. Every matrix the library offers
|
|
985
|
+
// is a similarity — a turn and a uniform scale — so `hypot(a, b)` is its scale and is
|
|
986
|
+
// positive: an all-zero matrix would have read as axis-aligned and never arrive here.
|
|
987
|
+
const [a = 1, b = 0, , , x = 0, y = 0] = matrix;
|
|
988
|
+
const z = Math.hypot(a, b);
|
|
989
|
+
image.$setTransform([z, 0, 0, z, x, y]);
|
|
990
|
+
layOnOrientation(image, rotate.value, flip.value);
|
|
991
|
+
});
|
|
992
|
+
writeOpenSelection();
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
/**
|
|
996
|
+
* Zoom is view-only: it changes no rectangle. The box on screen is another matter — cropperjs
|
|
997
|
+
* renders a selection at its own place inside the canvas and only its viewer follows the image's
|
|
998
|
+
* transform — so the picture would scale under a stationary box and frame something other than
|
|
999
|
+
* the rectangle that is stored, which Apply would then export. The box is therefore read back
|
|
1000
|
+
* before the picture moves and laid on the new frame afterwards. Every other way the picture
|
|
1001
|
+
* moves is held to the same pair, the dialog's own turns and mirrors and the library's wheel
|
|
1002
|
+
* alike; there is no pan, because a drag on the canvas draws a selection rather than moving the
|
|
1003
|
+
* picture (the default template's `action="select"`, `cropper.js:3517`).
|
|
1004
|
+
*/
|
|
1005
|
+
function zoomBy(factor: number): void {
|
|
1006
|
+
if (variantsReady.value) {
|
|
1007
|
+
readOpenSelection();
|
|
1008
|
+
}
|
|
1009
|
+
movePicture((image) => {
|
|
1010
|
+
image.$scale(factor, factor);
|
|
1011
|
+
});
|
|
1012
|
+
if (!variantsReady.value) {
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
writeOpenSelection();
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
function zoomIn(): void {
|
|
1019
|
+
zoomBy(ZOOM_STEP);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
function zoomOut(): void {
|
|
1023
|
+
zoomBy(1 / ZOOM_STEP);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/**
|
|
1027
|
+
* The transform buttons before the picture has loaded. The cropper exists as soon as the element
|
|
1028
|
+
* does, so a click lands in that window — and a turn applied there would not be tracked, because
|
|
1029
|
+
* there is no frame yet to track it against: `rotate` would say 0 while the picture is at 90, the
|
|
1030
|
+
* restore would then add the stored turn on top of it and fit every rectangle of the set against
|
|
1031
|
+
* a transposed frame. Zoom needs no such guard: the frame's scale is read from the image's matrix
|
|
1032
|
+
* when the rectangles are built, so a zoom before the load is simply part of it.
|
|
1033
|
+
*/
|
|
1034
|
+
function transformUntracked(): boolean {
|
|
1035
|
+
return hasVariants.value && !variantsReady.value;
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
/**
|
|
1039
|
+
* A quarter turn. A box drawn at 16:9 is 16:9 in what the person saw, so after the turn the same
|
|
1040
|
+
* fractions read as 9:16 and no longer match their variant: every rectangle is re-fitted to its
|
|
1041
|
+
* own ratio around its own centre, against the frame's new aspect. The box is read back before
|
|
1042
|
+
* the picture moves, because the fractions it is read as are the old frame's. `auto` is left
|
|
1043
|
+
* alone: a turn of the image is not somebody framing a variant.
|
|
1044
|
+
*/
|
|
1045
|
+
function rotateBy(quarter: 90 | -90): void {
|
|
1046
|
+
if (transformUntracked()) {
|
|
1047
|
+
return;
|
|
1048
|
+
}
|
|
1049
|
+
if (variantsReady.value) {
|
|
1050
|
+
readOpenSelection();
|
|
1051
|
+
}
|
|
1052
|
+
movePicture((image) => {
|
|
1053
|
+
image.$rotate(`${quarter}deg`);
|
|
1054
|
+
});
|
|
1055
|
+
// The turn is the picture's, not the set's, so it is tracked whether or not there are
|
|
1056
|
+
// variants — as the mirror is, on the same line of `flipHorizontal`. Both halves of the pair
|
|
1057
|
+
// are read back when the picture has to be re-oriented from them (`onPictureTransform`), and
|
|
1058
|
+
// an orientation cannot be rebuilt from a mirror nobody wrote down; `flipHorizontal` also
|
|
1059
|
+
// reads the turn to know which axis mirrors the frame on screen, and a single-crop caller
|
|
1060
|
+
// turns and mirrors the same picture with the same two buttons.
|
|
1061
|
+
rotate.value = asQuarter((rotate.value + quarter + 360) % 360);
|
|
1062
|
+
if (!variantsReady.value) {
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
mapSelections((selection, variant) => ({
|
|
1066
|
+
...selection,
|
|
1067
|
+
rect: fitRectToRatio(
|
|
1068
|
+
selection.rect,
|
|
1069
|
+
ratioValue(variant, selection.ratio),
|
|
1070
|
+
frameAspect.value,
|
|
1071
|
+
),
|
|
1072
|
+
rotate: rotate.value,
|
|
1073
|
+
}));
|
|
1074
|
+
writeOpenSelection();
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
function rotateLeft(): void {
|
|
1078
|
+
rotateBy(-90);
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
function rotateRight(): void {
|
|
1082
|
+
rotateBy(90);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
function flipHorizontal(): void {
|
|
1086
|
+
if (transformUntracked()) {
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
if (variantsReady.value) {
|
|
1090
|
+
readOpenSelection();
|
|
1091
|
+
}
|
|
1092
|
+
// `$scale` multiplies the image's matrix, it does not set it: -1 on every press mirrors
|
|
1093
|
+
// and unmirrors alternately, and `(1, 1)` would be the identity.
|
|
1094
|
+
//
|
|
1095
|
+
// WHICH AXIS CARRIES THE -1 DEPENDS ON THE TURN, and that is the whole of this branch.
|
|
1096
|
+
// cropperjs post-multiplies — `$transform` is
|
|
1097
|
+
// `$setTransform(multiplyMatrices(this.$matrix, [a, b, c, d, e, f]))` — so the operand is
|
|
1098
|
+
// applied in the picture's own, already turned frame. What `flip` means in the spec, and what
|
|
1099
|
+
// the strip paints with `scaleX(-1) rotate(Ndeg)`, is the mirror of the frame AS DISPLAYED:
|
|
1100
|
+
// the composite `S·R`, not `R·S`. Reaching `S·R` by post-multiplying onto `R` takes the
|
|
1101
|
+
// conjugate `R⁻¹·S_x·R`, which is `(-1, 1)` for 0 and 180 and `(1, -1)` for 90 and 270 — and
|
|
1102
|
+
// the same operand both mirrors and unmirrors, since `S_x` is its own inverse. A plain
|
|
1103
|
+
// `(-1, 1)` under a quarter turn mirrors the HORIZONTAL screen axis instead: 180 degrees from
|
|
1104
|
+
// the stored pair's own meaning and from the tile beside the stage, while `mirrorRect` mirrors
|
|
1105
|
+
// x — so every rectangle would land on content nobody framed. It also let the press order
|
|
1106
|
+
// decide the picture, which left a stored `{rotate, flip}` determining nothing.
|
|
1107
|
+
movePicture((image) => {
|
|
1108
|
+
if (quarterTurn.value) {
|
|
1109
|
+
image.$scale(1, -1);
|
|
1110
|
+
} else {
|
|
1111
|
+
image.$scale(-1, 1);
|
|
1112
|
+
}
|
|
1113
|
+
});
|
|
1114
|
+
// Tracked outside the guard, for the reasons `rotateBy` gives about the turn: the pair is the
|
|
1115
|
+
// picture's and the two halves of it are tracked the same way.
|
|
1116
|
+
flip.value = !flip.value;
|
|
1117
|
+
if (!variantsReady.value) {
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
// A mirror loses nothing, so every rectangle is mirrored exactly rather than re-fitted.
|
|
1121
|
+
mapSelections((selection) => ({
|
|
1122
|
+
...selection,
|
|
1123
|
+
rect: mirrorRect(selection.rect),
|
|
1124
|
+
flip: flip.value,
|
|
1125
|
+
}));
|
|
1126
|
+
writeOpenSelection();
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
function reset(): void {
|
|
1130
|
+
if (variantsReady.value) {
|
|
1131
|
+
resetOpenVariant();
|
|
1132
|
+
return;
|
|
1133
|
+
}
|
|
1134
|
+
// Simplest reliable reset: tear down and rebuild the cropper from scratch.
|
|
1135
|
+
if (imgRef.value) {
|
|
1136
|
+
mountCropper(imgRef.value);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* With variants, Reset re-frames the variant that is open and nothing else: the ratio in force
|
|
1142
|
+
* stays in force, the other variants keep their rectangles, and the image's turn, mirror and
|
|
1143
|
+
* zoom are not touched. Rebuilding the cropper here would reset the transform of the picture the
|
|
1144
|
+
* whole set is framed against, which is a destructive act behind a button that does not promise it.
|
|
1145
|
+
*/
|
|
1146
|
+
function resetOpenVariant(): void {
|
|
1147
|
+
const variant = openVariant.value;
|
|
1148
|
+
const selection = openSelection.value;
|
|
1149
|
+
if (!variant || !selection) {
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
selections.value = {
|
|
1153
|
+
...selections.value,
|
|
1154
|
+
[variant.name]: {
|
|
1155
|
+
...selection,
|
|
1156
|
+
rect: centredRect(ratioValue(variant, selection.ratio), frameAspect.value),
|
|
1157
|
+
auto: true,
|
|
1158
|
+
},
|
|
1159
|
+
};
|
|
1160
|
+
writeOpenSelection();
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
function handleCancel(): void {
|
|
1164
|
+
emit('cancel');
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
// The dialog is controlled and this component says `cancel` for every way out.
|
|
1168
|
+
function onUpdateOpen(value: boolean): void {
|
|
1169
|
+
if (!value) {
|
|
1170
|
+
handleCancel();
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
/**
|
|
1175
|
+
* The crop's size in pixels of the original image. cropperjs exports at the selection's size
|
|
1176
|
+
* on screen unless told otherwise, which turns a scan into a thumbnail. The image's transform
|
|
1177
|
+
* maps original pixels to stage pixels; its scale survives rotation and flipping.
|
|
1178
|
+
*
|
|
1179
|
+
* Only the short edge is passed: cropperjs derives the other from the selection's aspect
|
|
1180
|
+
* ratio. Given both, it fits them into that ratio and truncates, so rounding could shrink the
|
|
1181
|
+
* short edge of a tiny crop to zero, an empty canvas that exports nothing.
|
|
1182
|
+
*/
|
|
1183
|
+
function exportSize(selection: { width: number; height: number }): {
|
|
1184
|
+
width?: number;
|
|
1185
|
+
height?: number;
|
|
1186
|
+
} {
|
|
1187
|
+
const [a = 1, b = 0] = cropper?.getCropperImage()?.$getTransform() ?? [];
|
|
1188
|
+
const toOriginal = 1 / (Math.hypot(a, b) || 1);
|
|
1189
|
+
const long = Math.max(selection.width, selection.height) * toOriginal;
|
|
1190
|
+
const short = Math.min(selection.width, selection.height) * toOriginal;
|
|
1191
|
+
const edge = Math.max(1, Math.round(short * Math.min(1, MAX_EXPORT_EDGE / long)));
|
|
1192
|
+
return selection.width >= selection.height ? { height: edge } : { width: edge };
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
/** `cropped.jpg` for the single crop; a variant's crop is named after the variant. */
|
|
1196
|
+
function fileName(name?: string): string {
|
|
1197
|
+
const extension = props.outputType === 'image/jpeg' ? 'jpg' : 'png';
|
|
1198
|
+
return `cropped${name === undefined ? '' : `-${name}`}.${extension}`;
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
/**
|
|
1202
|
+
* Whatever the cropper frames right now, as a file. This is the export path as it was: the size
|
|
1203
|
+
* from `exportSize`, a white ground for a JPEG, and the library's own `toBlob`. A canvas that
|
|
1204
|
+
* yields no blob yields no file, rather than an empty one.
|
|
1205
|
+
*/
|
|
1206
|
+
async function exportFramed(selection: CropperSelection, name: string): Promise<File | null> {
|
|
1207
|
+
const type = props.outputType;
|
|
1208
|
+
const canvas = await selection.$toCanvas({
|
|
1209
|
+
...exportSize(selection),
|
|
1210
|
+
// JPEG has no transparency: paint what the image does not cover white, not black.
|
|
1211
|
+
beforeDraw:
|
|
1212
|
+
type === 'image/jpeg'
|
|
1213
|
+
? (context: CanvasRenderingContext2D, target: HTMLCanvasElement) => {
|
|
1214
|
+
context.fillStyle = '#ffffff';
|
|
1215
|
+
context.fillRect(0, 0, target.width, target.height);
|
|
1216
|
+
}
|
|
1217
|
+
: undefined,
|
|
1218
|
+
});
|
|
1219
|
+
return new Promise<File | null>((resolve) => {
|
|
1220
|
+
canvas.toBlob(
|
|
1221
|
+
(blob) => resolve(blob ? new File([blob], name, { type }) : null),
|
|
1222
|
+
type,
|
|
1223
|
+
JPEG_QUALITY,
|
|
1224
|
+
);
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
/**
|
|
1229
|
+
* One file per variant, in the order `variants` was given. There is one cropper, so each
|
|
1230
|
+
* variant's rectangle is laid on it in turn and the stage steps through the set while this runs
|
|
1231
|
+
* — feedback under the processing state, not something to hide.
|
|
1232
|
+
*
|
|
1233
|
+
* Two things frame the loop. The box the person may have just dragged is read back first, or
|
|
1234
|
+
* Apply straight after a drag would export the stored rectangle instead of the one on screen.
|
|
1235
|
+
* And the open variant is laid on the cropper again however the loop ends — in a `finally`,
|
|
1236
|
+
* because `$toCanvas` and `toBlob` both reject on a tainted canvas, which is what a remote
|
|
1237
|
+
* source without CORS gives every Apply. An unwinding loop that skipped the restore would leave
|
|
1238
|
+
* the stage framing another variant's rectangle while the strip says the open one is open, and
|
|
1239
|
+
* `written` with it, so the next read-back from any pick, turn, flip, zoom or ratio change would
|
|
1240
|
+
* write that other variant's rectangle into the open variant's selection as a chosen one.
|
|
1241
|
+
*
|
|
1242
|
+
* A failed export is a failed apply: nothing is emitted and the dialog stays open. A set with a
|
|
1243
|
+
* hole in it would be stored by the consumer as the authoritative one, and a first variant that
|
|
1244
|
+
* failed would quietly promote the second to primary — so the set is not laxer than the single
|
|
1245
|
+
* crop it generalises, which treats a blob that never came as no apply at all.
|
|
1246
|
+
*/
|
|
1247
|
+
async function exportVariants(element: CropperSelection): Promise<void> {
|
|
1248
|
+
readOpenSelection();
|
|
1249
|
+
const crops: CropResult[] = [];
|
|
1250
|
+
try {
|
|
1251
|
+
for (const variant of variantList.value) {
|
|
1252
|
+
const selection = selections.value[variant.name];
|
|
1253
|
+
// A box that was not laid on would export the previous variant's picture under
|
|
1254
|
+
// this variant's name, so a variant without one stops the apply as well.
|
|
1255
|
+
if (!selection || !writeSelection(variant, selection)) {
|
|
1256
|
+
return;
|
|
1257
|
+
}
|
|
1258
|
+
const file = await exportFramed(element, fileName(variant.name));
|
|
1259
|
+
if (!file) {
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
crops.push({ ...selection, name: variant.name, file });
|
|
1263
|
+
}
|
|
1264
|
+
} finally {
|
|
1265
|
+
writeOpenSelection();
|
|
1266
|
+
}
|
|
1267
|
+
const [primary] = crops;
|
|
1268
|
+
if (primary) {
|
|
1269
|
+
// The first variant is the primary one: its file is what a caller who reads only the
|
|
1270
|
+
// first argument gets.
|
|
1271
|
+
emit('apply', primary.file, crops);
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
async function handleApply(): Promise<void> {
|
|
1276
|
+
const element = cropper?.getCropperSelection();
|
|
1277
|
+
if (!element) {
|
|
1278
|
+
return;
|
|
1279
|
+
}
|
|
1280
|
+
isExporting.value = true;
|
|
1281
|
+
try {
|
|
1282
|
+
if (variantsReady.value) {
|
|
1283
|
+
await exportVariants(element);
|
|
1284
|
+
return;
|
|
1285
|
+
}
|
|
1286
|
+
// The single-crop caller, unchanged: one file, one argument. A variant caller lands
|
|
1287
|
+
// here too while the picture has not loaded, because there are no rectangles then —
|
|
1288
|
+
// which is what the footer's wording follows as well.
|
|
1289
|
+
const file = await exportFramed(element, fileName());
|
|
1290
|
+
if (file) {
|
|
1291
|
+
emit('apply', file);
|
|
1292
|
+
}
|
|
1293
|
+
} finally {
|
|
1294
|
+
isExporting.value = false;
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
// The ghost variant's hover ground is `--muted`, which is the band's own colour here.
|
|
1299
|
+
const TOOL_CLASS = 'text-muted-foreground hover:bg-background dark:hover:bg-background';
|
|
1300
|
+
</script>
|
|
1301
|
+
|
|
1302
|
+
<template>
|
|
1303
|
+
<Dialog
|
|
1304
|
+
:open="open && !!src"
|
|
1305
|
+
:title="heading"
|
|
1306
|
+
:close-label="text.close"
|
|
1307
|
+
size="xl"
|
|
1308
|
+
@update:open="onUpdateOpen"
|
|
1309
|
+
>
|
|
1310
|
+
<template v-if="$slots['close-icon']" #close-icon>
|
|
1311
|
+
<slot name="close-icon" />
|
|
1312
|
+
</template>
|
|
1313
|
+
|
|
1314
|
+
<!-- The strip stands beside the stage, and the ratio row above it. Both wrappers are
|
|
1315
|
+
`contents` without variants, which lays out no box of its own: the stage and the
|
|
1316
|
+
toolbar stay adjacent block-level siblings in the dialog body's own flow, which is
|
|
1317
|
+
what keeps the stage's rounded top against the toolbar's rounded bottom and adds no
|
|
1318
|
+
gap. A caller who passes no variants therefore sees today's layout exactly.
|
|
1319
|
+
|
|
1320
|
+
THE COLUMNS ARE `hasVariants`, NOT `variantsReady`, and the strip's own content is
|
|
1321
|
+
what waits for the picture. The stage would otherwise be laid out at the dialog's
|
|
1322
|
+
full width while the picture loads, cropperjs would fit and centre the picture to
|
|
1323
|
+
THAT width, and the strip appearing a moment later would take its column off the
|
|
1324
|
+
stage under a picture already centred in the wider one — the picture resting to one
|
|
1325
|
+
side, and the frame a rectangle was converted against no longer the frame it is laid
|
|
1326
|
+
on. One width from the first frame is what keeps the two the same. -->
|
|
1327
|
+
<div :class="hasVariants ? 'flex items-stretch gap-3' : 'contents'">
|
|
1328
|
+
<div :class="hasVariants ? 'flex min-w-0 flex-1 flex-col' : 'contents'">
|
|
1329
|
+
<!-- No `ratios.length` here. A variant that allows none is free, and the row
|
|
1330
|
+
says so rather than vanishing: measured at 1024x768, dropping the row took
|
|
1331
|
+
the left column from 568 to 522 and the dialog from 691 to 679, moving the
|
|
1332
|
+
footer 7px up while the stage stood still at 461. A 12px shuffle every time
|
|
1333
|
+
somebody opens that variant, and nothing anywhere saying it is free. -->
|
|
1334
|
+
<CropRatioRow
|
|
1335
|
+
v-if="variantsReady && openVariant"
|
|
1336
|
+
:ratios="openVariant.ratios"
|
|
1337
|
+
:current="openSelection?.ratio ?? ''"
|
|
1338
|
+
:labels="{
|
|
1339
|
+
aspectRatio: text.aspectRatio,
|
|
1340
|
+
only: text.ratioOnly,
|
|
1341
|
+
free: text.ratioFree,
|
|
1342
|
+
}"
|
|
1343
|
+
@pick="pickRatio"
|
|
1344
|
+
/>
|
|
1345
|
+
|
|
1346
|
+
<!-- A definite height, not flex-1: the stage used to grow with the dialog while
|
|
1347
|
+
its content stayed capped at half the viewport, which is what produced a
|
|
1348
|
+
large dark area around a small crop box. The ground is a fixed swatch, see
|
|
1349
|
+
the note above. The tiles name it as the panel they switch. -->
|
|
1350
|
+
<div
|
|
1351
|
+
:id="variantsReady ? stageId : undefined"
|
|
1352
|
+
data-testid="crop-stage"
|
|
1353
|
+
:role="variantsReady ? 'tabpanel' : undefined"
|
|
1354
|
+
:aria-labelledby="variantsReady ? openTabId : undefined"
|
|
1355
|
+
class="crop-stage bg-gray-950 relative h-[min(60vh,640px)] shrink-0 overflow-hidden rounded-t-lg p-3"
|
|
1356
|
+
>
|
|
1357
|
+
<img
|
|
1358
|
+
ref="imgRef"
|
|
1359
|
+
:src="src ?? undefined"
|
|
1360
|
+
crossorigin="anonymous"
|
|
1361
|
+
alt=""
|
|
1362
|
+
class="mx-auto block max-h-full max-w-full"
|
|
1363
|
+
@load="onImageLoad"
|
|
1364
|
+
@error="loadFailed = true"
|
|
1365
|
+
/>
|
|
1366
|
+
<div
|
|
1367
|
+
v-if="loadFailed"
|
|
1368
|
+
data-testid="crop-load-error"
|
|
1369
|
+
role="alert"
|
|
1370
|
+
class="absolute inset-0 flex flex-col items-center justify-center gap-2 bg-inherit p-6 text-center"
|
|
1371
|
+
>
|
|
1372
|
+
<slot name="load-error-icon">
|
|
1373
|
+
<CircleAlertIcon class="size-8 text-white/40" />
|
|
1374
|
+
</slot>
|
|
1375
|
+
<p class="text-sm font-semibold text-white">{{ text.loadError }}</p>
|
|
1376
|
+
<p class="text-xs text-white/60">{{ text.loadErrorHint }}</p>
|
|
1377
|
+
</div>
|
|
1378
|
+
</div>
|
|
1379
|
+
|
|
1380
|
+
<div
|
|
1381
|
+
data-testid="crop-toolbar"
|
|
1382
|
+
class="border-border bg-muted flex flex-wrap items-center justify-center gap-1 rounded-b-lg border border-t-0 px-4 py-3"
|
|
1383
|
+
>
|
|
1384
|
+
<Button
|
|
1385
|
+
variant="ghost"
|
|
1386
|
+
size="icon-lg"
|
|
1387
|
+
data-testid="crop-zoom-in"
|
|
1388
|
+
:aria-label="text.zoomIn"
|
|
1389
|
+
:title="text.zoomIn"
|
|
1390
|
+
:class="TOOL_CLASS"
|
|
1391
|
+
@click="zoomIn"
|
|
1392
|
+
>
|
|
1393
|
+
<slot name="zoom-in-icon"><ZoomInIcon class="size-5" /></slot>
|
|
1394
|
+
</Button>
|
|
1395
|
+
<Button
|
|
1396
|
+
variant="ghost"
|
|
1397
|
+
size="icon-lg"
|
|
1398
|
+
data-testid="crop-zoom-out"
|
|
1399
|
+
:aria-label="text.zoomOut"
|
|
1400
|
+
:title="text.zoomOut"
|
|
1401
|
+
:class="TOOL_CLASS"
|
|
1402
|
+
@click="zoomOut"
|
|
1403
|
+
>
|
|
1404
|
+
<slot name="zoom-out-icon"><ZoomOutIcon class="size-5" /></slot>
|
|
1405
|
+
</Button>
|
|
1406
|
+
|
|
1407
|
+
<span
|
|
1408
|
+
role="separator"
|
|
1409
|
+
aria-orientation="vertical"
|
|
1410
|
+
class="bg-border mx-1 h-6 w-px"
|
|
1411
|
+
/>
|
|
1412
|
+
|
|
1413
|
+
<Button
|
|
1414
|
+
variant="ghost"
|
|
1415
|
+
size="icon-lg"
|
|
1416
|
+
data-testid="crop-rotate-left"
|
|
1417
|
+
:aria-label="text.rotateLeft"
|
|
1418
|
+
:title="text.rotateLeft"
|
|
1419
|
+
:class="TOOL_CLASS"
|
|
1420
|
+
@click="rotateLeft"
|
|
1421
|
+
>
|
|
1422
|
+
<slot name="rotate-left-icon"><RotateCcwIcon class="size-5" /></slot>
|
|
1423
|
+
</Button>
|
|
1424
|
+
<Button
|
|
1425
|
+
variant="ghost"
|
|
1426
|
+
size="icon-lg"
|
|
1427
|
+
data-testid="crop-rotate-right"
|
|
1428
|
+
:aria-label="text.rotateRight"
|
|
1429
|
+
:title="text.rotateRight"
|
|
1430
|
+
:class="TOOL_CLASS"
|
|
1431
|
+
@click="rotateRight"
|
|
1432
|
+
>
|
|
1433
|
+
<slot name="rotate-right-icon"><RotateCwIcon class="size-5" /></slot>
|
|
1434
|
+
</Button>
|
|
1435
|
+
|
|
1436
|
+
<span
|
|
1437
|
+
role="separator"
|
|
1438
|
+
aria-orientation="vertical"
|
|
1439
|
+
class="bg-border mx-1 h-6 w-px"
|
|
1440
|
+
/>
|
|
1441
|
+
|
|
1442
|
+
<Button
|
|
1443
|
+
variant="ghost"
|
|
1444
|
+
size="icon-lg"
|
|
1445
|
+
data-testid="crop-flip"
|
|
1446
|
+
:aria-label="text.flip"
|
|
1447
|
+
:title="text.flip"
|
|
1448
|
+
:class="TOOL_CLASS"
|
|
1449
|
+
@click="flipHorizontal"
|
|
1450
|
+
>
|
|
1451
|
+
<slot name="flip-icon"><FlipHorizontalIcon class="size-5" /></slot>
|
|
1452
|
+
</Button>
|
|
1453
|
+
|
|
1454
|
+
<span
|
|
1455
|
+
role="separator"
|
|
1456
|
+
aria-orientation="vertical"
|
|
1457
|
+
class="bg-border mx-1 h-6 w-px"
|
|
1458
|
+
/>
|
|
1459
|
+
|
|
1460
|
+
<Button
|
|
1461
|
+
variant="ghost"
|
|
1462
|
+
size="icon-lg"
|
|
1463
|
+
data-testid="crop-reset"
|
|
1464
|
+
:aria-label="text.reset"
|
|
1465
|
+
:title="text.reset"
|
|
1466
|
+
:class="TOOL_CLASS"
|
|
1467
|
+
@click="reset"
|
|
1468
|
+
>
|
|
1469
|
+
<slot name="reset-icon"><Undo2Icon class="size-5" /></slot>
|
|
1470
|
+
</Button>
|
|
1471
|
+
</div>
|
|
1472
|
+
</div>
|
|
1473
|
+
|
|
1474
|
+
<!-- The column owns the width, so it is reserved whether or not the strip is in it
|
|
1475
|
+
yet; the strip fills it. -->
|
|
1476
|
+
<div v-if="hasVariants" data-testid="crop-variant-column" class="w-56 shrink-0">
|
|
1477
|
+
<CropVariantStrip
|
|
1478
|
+
v-if="variantsReady && src"
|
|
1479
|
+
:src="src"
|
|
1480
|
+
:variants="variantList"
|
|
1481
|
+
:selections="selections"
|
|
1482
|
+
:current="openTabName"
|
|
1483
|
+
:frame-aspect="frameAspect"
|
|
1484
|
+
:rotate="rotate"
|
|
1485
|
+
:flip="flip"
|
|
1486
|
+
:aria-controls="stageId"
|
|
1487
|
+
:tab-ids="tabIds"
|
|
1488
|
+
:labels="{ variants: text.variants, auto: text.auto }"
|
|
1489
|
+
@pick="pickVariant"
|
|
1490
|
+
/>
|
|
1491
|
+
</div>
|
|
1492
|
+
</div>
|
|
1493
|
+
|
|
1494
|
+
<template #footer>
|
|
1495
|
+
<!-- The status stands left of the buttons through `mr-auto` in the footer's own
|
|
1496
|
+
flex row, so it needs no wrapper and a narrow dialog still wraps it onto its
|
|
1497
|
+
own line. Two spans rather than two paragraphs: one line, two statements.
|
|
1498
|
+
Their separator is an INTERPOLATED space and not the newline above it: Vue's
|
|
1499
|
+
condense mode drops a whitespace-only text node that holds a newline
|
|
1500
|
+
altogether, so the two statements rendered as one word ("...setNot set: ...").
|
|
1501
|
+
An interpolation is not whitespace to that pass, so it survives. The suite
|
|
1502
|
+
reads the paragraph's own text, which is the only place that join is visible. -->
|
|
1503
|
+
<p
|
|
1504
|
+
v-if="variantsReady"
|
|
1505
|
+
data-testid="crop-variants-line"
|
|
1506
|
+
class="text-muted-foreground mr-auto text-xs"
|
|
1507
|
+
>
|
|
1508
|
+
<span data-testid="crop-variants-status">{{ variantsStatus }}</span>
|
|
1509
|
+
<template v-if="pendingVariants.length > 0">
|
|
1510
|
+
{{ ' ' }}
|
|
1511
|
+
<span data-testid="crop-variants-pending">{{ variantsPending }}</span>
|
|
1512
|
+
</template>
|
|
1513
|
+
</p>
|
|
1514
|
+
|
|
1515
|
+
<Button variant="outline" data-testid="crop-cancel" @click="handleCancel">
|
|
1516
|
+
{{ text.cancel }}
|
|
1517
|
+
</Button>
|
|
1518
|
+
<Button
|
|
1519
|
+
data-testid="crop-apply"
|
|
1520
|
+
:disabled="isExporting || loadFailed"
|
|
1521
|
+
@click="handleApply"
|
|
1522
|
+
>
|
|
1523
|
+
{{ isExporting ? text.processing : applyText }}
|
|
1524
|
+
</Button>
|
|
1525
|
+
</template>
|
|
1526
|
+
</Dialog>
|
|
1527
|
+
</template>
|
|
1528
|
+
|
|
1529
|
+
<style scoped>
|
|
1530
|
+
/*
|
|
1531
|
+
* cropperjs hides the original <img> (display:none) and inserts its own <cropper-canvas>
|
|
1532
|
+
* as a sibling, so sizing the <img> has no effect on the crop area — the canvas is what
|
|
1533
|
+
* has to fill the stage. Without this the canvas keeps its intrinsic size and the crop
|
|
1534
|
+
* box ends up tiny inside a large dark stage.
|
|
1535
|
+
*/
|
|
1536
|
+
.crop-stage :deep(cropper-canvas) {
|
|
1537
|
+
display: block;
|
|
1538
|
+
width: 100%;
|
|
1539
|
+
height: 100%;
|
|
1540
|
+
}
|
|
1541
|
+
</style>
|