@m3ui-vue/m3ui-vue 1.2.2 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/API.md +3010 -376
- package/dist/MButton-Bl_BSUep.js +172 -0
- package/dist/MButton-Bl_BSUep.js.map +1 -0
- package/dist/MDialog-D_Yik8ou.js +173 -0
- package/dist/MDialog-D_Yik8ou.js.map +1 -0
- package/dist/MIconButton-DoNotQ9v.js +282 -0
- package/dist/MIconButton-DoNotQ9v.js.map +1 -0
- package/dist/MListItem-BxifHJdU.js +824 -0
- package/dist/MListItem-BxifHJdU.js.map +1 -0
- package/dist/MLoadingIndicator-BxbFhSX6.js +115 -0
- package/dist/MLoadingIndicator-BxbFhSX6.js.map +1 -0
- package/dist/MMenuItem-Bih0h4-n.js +604 -0
- package/dist/MMenuItem-Bih0h4-n.js.map +1 -0
- package/dist/MProgressBar-BCoruxA-.js +184 -0
- package/dist/MProgressBar-BCoruxA-.js.map +1 -0
- package/dist/MSeekBar-BeN9QcB3.js +435 -0
- package/dist/MSeekBar-BeN9QcB3.js.map +1 -0
- package/dist/MSegmentedButton-DL9JtJFX.js +65 -0
- package/dist/MSegmentedButton-DL9JtJFX.js.map +1 -0
- package/dist/MSlider-BVSbJlGS.js +691 -0
- package/dist/MSlider-BVSbJlGS.js.map +1 -0
- package/dist/MSpinner-C2SUnWk4.js +65 -0
- package/dist/MSpinner-C2SUnWk4.js.map +1 -0
- package/dist/MTextField-CEkWSZ2v.js +296 -0
- package/dist/MTextField-CEkWSZ2v.js.map +1 -0
- package/dist/MTooltip-CodJGEO-.js +756 -0
- package/dist/MTooltip-CodJGEO-.js.map +1 -0
- package/dist/_storageKeys-CYXsUBwK.js +18 -0
- package/dist/_storageKeys-CYXsUBwK.js.map +1 -0
- package/dist/_useEscapeStack-CjndQhqg.js +19 -0
- package/dist/_useEscapeStack-CjndQhqg.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/chart.js +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MAudioPlayer.vue.d.ts +115 -0
- package/dist/components/MBadge.vue.d.ts +1 -1
- package/dist/components/MBottomSheetScaffold.vue.d.ts +1 -1
- package/dist/components/MBox.vue.d.ts +1 -1
- package/dist/components/MButton.vue.d.ts +1 -1
- package/dist/components/MConfirmDialog.vue.d.ts +1 -1
- package/dist/components/MCopyButton.vue.d.ts +2 -2
- package/dist/components/MDataTable.vue.d.ts +2 -2
- package/dist/components/MDialog.vue.d.ts +2 -2
- package/dist/components/MDivider.vue.d.ts +1 -1
- package/dist/components/MDragHandle.vue.d.ts +1 -1
- package/dist/components/MDrawCanvas.vue.d.ts +150 -0
- package/dist/components/MDrawLayers.vue.d.ts +3 -0
- package/dist/components/MDrawToolbar.vue.d.ts +34 -0
- package/dist/components/MExportButton.vue.d.ts +1 -1
- package/dist/components/MFab.vue.d.ts +2 -2
- package/dist/components/MFabMenu.vue.d.ts +2 -2
- package/dist/components/MFixed.vue.d.ts +1 -1
- package/dist/components/MFlex.vue.d.ts +2 -2
- package/dist/components/MFloorPlan.vue.d.ts +248 -0
- package/dist/components/MFloorPlanInspector.vue.d.ts +56 -0
- package/dist/components/MFloorPlanToolbar.vue.d.ts +51 -0
- package/dist/components/MFlow.vue.d.ts +318 -0
- package/dist/components/MFlowControls.vue.d.ts +33 -0
- package/dist/components/MFlowHandle.vue.d.ts +16 -0
- package/dist/components/MFlowMinimap.vue.d.ts +22 -0
- package/dist/components/MFlowNode.vue.d.ts +35 -0
- package/dist/components/MGantt.vue.d.ts +141 -0
- package/dist/components/MGanttControls.vue.d.ts +32 -0
- package/dist/components/MIconButton.vue.d.ts +3 -1
- package/dist/components/MImageCropper.vue.d.ts +110 -0
- package/dist/components/MInfiniteScroll.vue.d.ts +1 -1
- package/dist/components/MLightbox.vue.d.ts +2 -2
- package/dist/components/MList.vue.d.ts +1 -1
- package/dist/components/MLoadingOverlay.vue.d.ts +1 -1
- package/dist/components/MMapControl.vue.d.ts +1 -1
- package/dist/components/MMapSearch.vue.d.ts +1 -1
- package/dist/components/MMaskField.vue.d.ts +12 -1
- package/dist/components/MMenu.vue.d.ts +1 -1
- package/dist/components/MNavigationDrawer.vue.d.ts +1 -1
- package/dist/components/MNumberField.vue.d.ts +11 -2
- package/dist/components/MPdfViewer.vue.d.ts +174 -0
- package/dist/components/MPullToRefresh.vue.d.ts +1 -1
- package/dist/components/MResourceTimeline.vue.d.ts +149 -0
- package/dist/components/MScheduler.vue.d.ts +3 -3
- package/dist/components/MScheme.vue.d.ts +1 -1
- package/dist/components/MSearchField.vue.d.ts +1 -1
- package/dist/components/MSeekBar.vue.d.ts +1 -1
- package/dist/components/MSimpleCarousel.vue.d.ts +1 -1
- package/dist/components/MSkeleton.vue.d.ts +1 -1
- package/dist/components/MSlider.vue.d.ts +2 -2
- package/dist/components/MSparkline.vue.d.ts +1 -1
- package/dist/components/MSplitter.vue.d.ts +1 -1
- package/dist/components/MStack.vue.d.ts +2 -2
- package/dist/components/MSwipeToDismiss.vue.d.ts +1 -1
- package/dist/components/MSwitch.vue.d.ts +1 -1
- package/dist/components/MTable.vue.d.ts +1 -1
- package/dist/components/MTagInput.vue.d.ts +12 -1
- package/dist/components/MTextField.vue.d.ts +13 -2
- package/dist/components/MToolbar.vue.d.ts +1 -1
- package/dist/components/MTree.vue.d.ts +2 -2
- package/dist/components/MVideoPlayer.vue.d.ts +253 -0
- package/dist/components/MVirtualTable.vue.d.ts +1 -1
- package/dist/components/MWindow.vue.d.ts +3 -3
- package/dist/components/_MFlowEdge.vue.d.ts +9 -0
- package/dist/components/_MFlowEdgeLabel.vue.d.ts +9 -0
- package/dist/components/_MFlowNodeShell.vue.d.ts +9 -0
- package/dist/components/_MPdfPage.vue.d.ts +76 -0
- package/dist/components/_MTimeBackground.vue.d.ts +14 -0
- package/dist/components/_MTimeHeader.vue.d.ts +20 -0
- package/dist/composables/_flowPanel.d.ts +2 -0
- package/dist/composables/_flowSlot.d.ts +5 -0
- package/dist/composables/_ganttTime.d.ts +41 -0
- package/dist/composables/_useTimeAxis.d.ts +90 -0
- package/dist/composables/drawGeometry.d.ts +119 -0
- package/dist/composables/flowFormats.d.ts +79 -0
- package/dist/composables/flowLayout.d.ts +45 -0
- package/dist/composables/flowPaths.d.ts +53 -0
- package/dist/composables/ganttFormats.d.ts +52 -0
- package/dist/composables/ganttSchedule.d.ts +99 -0
- package/dist/composables/useDrawing.d.ts +44 -0
- package/dist/composables/useFloorPlan.d.ts +58 -0
- package/dist/composables/useFlow.d.ts +266 -0
- package/dist/composables/useGantt.d.ts +87 -0
- package/dist/composables/useLocale.d.ts +204 -0
- package/dist/composables/useMediaPlayer.d.ts +101 -0
- package/dist/composables/usePdf.d.ts +119 -0
- package/dist/composables/useResourceTimeline.d.ts +70 -0
- package/dist/draw.d.ts +7 -0
- package/dist/draw.js +1702 -0
- package/dist/draw.js.map +1 -0
- package/dist/flow.d.ts +14 -0
- package/dist/flow.js +3547 -0
- package/dist/flow.js.map +1 -0
- package/dist/flowPaths-CUc6X373.js +161 -0
- package/dist/flowPaths-CUc6X373.js.map +1 -0
- package/dist/gantt.d.ts +12 -0
- package/dist/gantt.js +3575 -0
- package/dist/gantt.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/locales/index.js +1435 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +8248 -7637
- package/dist/m3ui.js.map +1 -1
- package/dist/map.js +113 -108
- package/dist/map.js.map +1 -1
- package/dist/media.d.ts +5 -0
- package/dist/media.js +1311 -0
- package/dist/media.js.map +1 -0
- package/dist/pdf.d.ts +4 -0
- package/dist/pdf.js +1871 -0
- package/dist/pdf.js.map +1 -0
- package/dist/rich-text-editor.js +28 -26
- package/dist/rich-text-editor.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/{useLocale-JYxDDeXC.js → useLocale-CaWwi-b6.js} +206 -2
- package/dist/useLocale-CaWwi-b6.js.map +1 -0
- package/dist/{useThemeTokens-y0HZpyhL.js → useThemeTokens-DQQcaWbE.js} +40 -54
- package/dist/useThemeTokens-DQQcaWbE.js.map +1 -0
- package/package.json +29 -5
- package/src/__tests__/MAudioPlayer.test.ts +75 -0
- package/src/__tests__/MDrawCanvas.test.ts +118 -0
- package/src/__tests__/MFloorPlan.test.ts +299 -0
- package/src/__tests__/MFlow.test.ts +590 -0
- package/src/__tests__/MGantt.test.ts +130 -0
- package/src/__tests__/MImageCropper.test.ts +89 -0
- package/src/__tests__/MPdfViewer.test.ts +235 -0
- package/src/__tests__/MResourceTimeline.test.ts +102 -0
- package/src/__tests__/MVideoPlayer.test.ts +201 -0
- package/src/__tests__/drawGeometry.test.ts +58 -0
- package/src/__tests__/fieldExpose.test.ts +30 -0
- package/src/__tests__/flowFormats.test.ts +185 -0
- package/src/__tests__/flowLayout.test.ts +113 -0
- package/src/__tests__/ganttFormats.test.ts +76 -0
- package/src/__tests__/ganttSchedule.test.ts +136 -0
- package/src/__tests__/pdfMock.ts +83 -0
- package/src/__tests__/useMediaPlayer.test.ts +182 -0
- package/src/__tests__/usePdf.test.ts +102 -0
- package/src/components/MAudioPlayer.vue +319 -0
- package/src/components/MDrawCanvas.vue +819 -0
- package/src/components/MDrawLayers.vue +114 -0
- package/src/components/MDrawToolbar.vue +171 -0
- package/src/components/MFloorPlan.vue +1080 -0
- package/src/components/MFloorPlanInspector.vue +169 -0
- package/src/components/MFloorPlanToolbar.vue +128 -0
- package/src/components/MFlow.vue +2060 -0
- package/src/components/MFlowControls.vue +110 -0
- package/src/components/MFlowHandle.vue +60 -0
- package/src/components/MFlowMinimap.vue +134 -0
- package/src/components/MFlowNode.vue +98 -0
- package/src/components/MGantt.vue +1609 -0
- package/src/components/MGanttControls.vue +77 -0
- package/src/components/MIconButton.vue +3 -1
- package/src/components/MImageCropper.vue +507 -0
- package/src/components/MMaskField.vue +13 -0
- package/src/components/MNumberField.vue +13 -0
- package/src/components/MPdfViewer.vue +1270 -0
- package/src/components/MResourceTimeline.vue +1020 -0
- package/src/components/MTagInput.vue +13 -0
- package/src/components/MTextField.vue +16 -0
- package/src/components/MVideoPlayer.vue +839 -0
- package/src/components/_MFlowEdge.vue +100 -0
- package/src/components/_MFlowEdgeLabel.vue +56 -0
- package/src/components/_MFlowNodeShell.vue +111 -0
- package/src/components/_MPdfPage.vue +456 -0
- package/src/components/_MTimeBackground.vue +16 -0
- package/src/components/_MTimeHeader.vue +31 -0
- package/src/composables/_flowPanel.ts +13 -0
- package/src/composables/_flowSlot.ts +6 -0
- package/src/composables/_ganttTime.ts +200 -0
- package/src/composables/_useTimeAxis.ts +224 -0
- package/src/composables/drawGeometry.ts +405 -0
- package/src/composables/flowFormats.ts +916 -0
- package/src/composables/flowLayout.ts +297 -0
- package/src/composables/flowPaths.ts +239 -0
- package/src/composables/ganttFormats.ts +366 -0
- package/src/composables/ganttSchedule.ts +403 -0
- package/src/composables/useDrawing.ts +53 -0
- package/src/composables/useFloorPlan.ts +73 -0
- package/src/composables/useFlow.ts +250 -0
- package/src/composables/useGantt.ts +92 -0
- package/src/composables/useLocale.ts +410 -0
- package/src/composables/useMediaPlayer.ts +406 -0
- package/src/composables/usePdf.ts +456 -0
- package/src/composables/useResourceTimeline.ts +76 -0
- package/src/draw.ts +25 -0
- package/src/flow.ts +26 -0
- package/src/gantt.ts +12 -0
- package/src/index.ts +9 -0
- package/src/locales/de.ts +204 -0
- package/src/locales/es.ts +204 -0
- package/src/locales/fr.ts +204 -0
- package/src/locales/ja.ts +204 -0
- package/src/locales/ko.ts +204 -0
- package/src/locales/pt.ts +204 -0
- package/src/locales/zh.ts +204 -0
- package/src/media.ts +5 -0
- package/src/pdf.ts +15 -0
- package/dist/MMenuItem-D4Gx_l3F.js +0 -929
- package/dist/MMenuItem-D4Gx_l3F.js.map +0 -1
- package/dist/MSlider-Dx_S3vJ9.js +0 -1506
- package/dist/MSlider-Dx_S3vJ9.js.map +0 -1
- package/dist/MTextField-Bz8JIsnl.js +0 -633
- package/dist/MTextField-Bz8JIsnl.js.map +0 -1
- package/dist/useLocale-JYxDDeXC.js.map +0 -1
- package/dist/useThemeTokens-y0HZpyhL.js.map +0 -1
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* A toolbar for an MGantt or an MResourceTimeline (put it in their default slot): the zoom as named scales, zoom in
|
|
4
|
+
* and out, today, expand and collapse all, undo and redo. Not an M3 spec component.
|
|
5
|
+
*
|
|
6
|
+
* @slot default More controls at the start
|
|
7
|
+
* @slot end More controls at the end
|
|
8
|
+
*/
|
|
9
|
+
import { computed, inject } from 'vue'
|
|
10
|
+
import { TIMELINE_CONTROLS_KEY, type GanttScale } from '../composables/useGantt'
|
|
11
|
+
import { useLocale } from '../composables/useLocale'
|
|
12
|
+
import MIconButton from './MIconButton.vue'
|
|
13
|
+
import MSegmentedButton from './MSegmentedButton.vue'
|
|
14
|
+
|
|
15
|
+
const props = withDefaults(defineProps<{
|
|
16
|
+
/** The scales offered as a segmented button (`[]` hides it) */
|
|
17
|
+
scales?: GanttScale[]
|
|
18
|
+
zoom?: boolean
|
|
19
|
+
today?: boolean
|
|
20
|
+
/** Expand all / collapse all */
|
|
21
|
+
fold?: boolean
|
|
22
|
+
/** Undo and redo */
|
|
23
|
+
history?: boolean
|
|
24
|
+
}>(), {
|
|
25
|
+
scales: () => ['day', 'week', 'month', 'quarter'],
|
|
26
|
+
zoom: true,
|
|
27
|
+
today: true,
|
|
28
|
+
fold: true,
|
|
29
|
+
history: true,
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const injected = inject(TIMELINE_CONTROLS_KEY, null)
|
|
33
|
+
if (!injected) throw new Error('MGanttControls must be inside <MGantt> or <MResourceTimeline>')
|
|
34
|
+
const gantt = injected
|
|
35
|
+
const locale = useLocale()
|
|
36
|
+
const NAMES: Record<GanttScale, keyof typeof locale> = {
|
|
37
|
+
hour: 'ganttScaleHour',
|
|
38
|
+
day: 'ganttScaleDay',
|
|
39
|
+
week: 'ganttScaleWeek',
|
|
40
|
+
month: 'ganttScaleMonth',
|
|
41
|
+
quarter: 'ganttScaleQuarter',
|
|
42
|
+
year: 'ganttScaleYear',
|
|
43
|
+
}
|
|
44
|
+
const options = computed(() => props.scales.map((s) => ({ value: s, label: locale[NAMES[s]] as string })))
|
|
45
|
+
</script>
|
|
46
|
+
|
|
47
|
+
<template>
|
|
48
|
+
<div class="m3-gantt-controls flex flex-wrap items-center gap-2 border-b border-outline-variant px-3 py-2" role="toolbar">
|
|
49
|
+
<slot />
|
|
50
|
+
<MSegmentedButton
|
|
51
|
+
v-if="options.length"
|
|
52
|
+
:model-value="gantt.scale.value"
|
|
53
|
+
:options="options"
|
|
54
|
+
density="compact"
|
|
55
|
+
@update:model-value="gantt.setScale($event as GanttScale)"
|
|
56
|
+
/>
|
|
57
|
+
<div class="flex items-center">
|
|
58
|
+
<template v-if="zoom">
|
|
59
|
+
<MIconButton icon="zoom_out" :label="locale.ganttZoomOut" @click="gantt.zoomOut()" />
|
|
60
|
+
<MIconButton icon="zoom_in" :label="locale.ganttZoomIn" @click="gantt.zoomIn()" />
|
|
61
|
+
</template>
|
|
62
|
+
<MIconButton v-if="today" icon="today" :label="locale.ganttToday" @click="gantt.scrollToDate()" />
|
|
63
|
+
<MIconButton
|
|
64
|
+
v-if="fold && gantt.foldable.value"
|
|
65
|
+
:icon="gantt.folded.value ? 'unfold_more' : 'unfold_less'"
|
|
66
|
+
:label="gantt.folded.value ? locale.ganttExpandAll : locale.ganttCollapseAll"
|
|
67
|
+
@click="gantt.folded.value ? gantt.expandAll() : gantt.collapseAll()"
|
|
68
|
+
/>
|
|
69
|
+
<template v-if="history && !gantt.readonly.value">
|
|
70
|
+
<MIconButton icon="undo" :label="locale.ganttUndo" :disabled="!gantt.canUndo.value" @click="gantt.undo()" />
|
|
71
|
+
<MIconButton icon="redo" :label="locale.ganttRedo" :disabled="!gantt.canRedo.value" @click="gantt.redo()" />
|
|
72
|
+
</template>
|
|
73
|
+
</div>
|
|
74
|
+
<div class="flex-1" />
|
|
75
|
+
<slot name="end" />
|
|
76
|
+
</div>
|
|
77
|
+
</template>
|
|
@@ -37,6 +37,8 @@ const props = withDefaults(
|
|
|
37
37
|
shape?: 'rounded' | 'squared'
|
|
38
38
|
/** M3's 5-tier icon button sizing (xs:32, sm:40, md:56, lg:96, xl:136), or a custom pixel value */
|
|
39
39
|
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | number
|
|
40
|
+
/** Icon size in px. Default the size tier's (or 55% of a custom pixel `size`) */
|
|
41
|
+
iconSize?: number
|
|
40
42
|
/** Disables interaction */
|
|
41
43
|
disabled?: boolean
|
|
42
44
|
/** Vue Router destination — renders as RouterLink instead of button */
|
|
@@ -240,6 +242,6 @@ const variantClasses = computed(() => {
|
|
|
240
242
|
@pointercancel="onPointerUp"
|
|
241
243
|
@click="onClick"
|
|
242
244
|
>
|
|
243
|
-
<MIcon :name="checked && checkedIcon ? checkedIcon : icon" :size="resolved.icon" :filled="!!checked || iconFilled" />
|
|
245
|
+
<MIcon :name="checked && checkedIcon ? checkedIcon : icon" :size="iconSize ?? resolved.icon" :filled="!!checked || iconFilled" />
|
|
244
246
|
</component>
|
|
245
247
|
</template>
|
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* An image cropper: the picture fitted in its box with a crop frame you drag and resize by its corners and edges
|
|
4
|
+
* (a rule-of-thirds grid while you do), locked to an `aspect` or free, square or round (`shape="circle"` for
|
|
5
|
+
* avatars). Rotate a quarter turn at a time and flip; the frame follows. Not an M3 spec component: M3 parts after
|
|
6
|
+
* Google Photos' crop tool.
|
|
7
|
+
*
|
|
8
|
+
* `v-model:crop` is the frame in the rotated picture's own pixels (`{ x, y, width, height }`), `v-model:rotation`
|
|
9
|
+
* (0, 90, 180, 270, applied after the mirror) and `v-model:flipped` (the picture mirrored left to right before it turns). The template ref's `toCanvas()` / `toBlob()` draw the result, at the
|
|
10
|
+
* crop's size or one you give (`width`, `height`, `maxSize`), round with a transparent outside for `circle`.
|
|
11
|
+
* `src` takes a URL, a `File` or a `Blob` (e.g. from MFileUpload); a picture from another origin must allow it
|
|
12
|
+
* (CORS) to be exported, and gets `crossorigin="anonymous"`.
|
|
13
|
+
*
|
|
14
|
+
* Keyboard (on the frame): arrows move it, Shift + arrows resize it (10 px steps with Alt).
|
|
15
|
+
*
|
|
16
|
+
* @slot controls Instead of the toolbar under the picture. Props: `{ rotate, flip, reset, aspect, setAspect }`
|
|
17
|
+
* @event load The picture loaded: `{ width, height }` (its natural size)
|
|
18
|
+
* @event error The picture didn't load
|
|
19
|
+
* @event change A drag or a key changed the crop (after `update:crop`): the crop
|
|
20
|
+
*/
|
|
21
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
22
|
+
import { useLocale } from '../composables/useLocale'
|
|
23
|
+
import { prefersReducedMotion } from '../composables/_springAnimation'
|
|
24
|
+
import MIconButton from './MIconButton.vue'
|
|
25
|
+
import MChip from './MChip.vue'
|
|
26
|
+
|
|
27
|
+
export interface CropRect { x: number; y: number; width: number; height: number }
|
|
28
|
+
export interface CropAspect { label: string; value: number | null }
|
|
29
|
+
export interface CropOutputOptions {
|
|
30
|
+
/** The result's size in px; one alone keeps the crop's proportions (default the crop's own size) */
|
|
31
|
+
width?: number
|
|
32
|
+
height?: number
|
|
33
|
+
/** The longest side at most, px */
|
|
34
|
+
maxSize?: number
|
|
35
|
+
/** Round, transparent outside (default true for `shape="circle"`) */
|
|
36
|
+
circle?: boolean
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const props = withDefaults(defineProps<{
|
|
40
|
+
/** The picture: a URL, a File or a Blob */
|
|
41
|
+
src?: string | Blob | null
|
|
42
|
+
/** The frame, in the rotated picture's pixels (`v-model:crop`) */
|
|
43
|
+
crop?: CropRect | null
|
|
44
|
+
/** Width over height (1 square, 16 / 9…); null is free */
|
|
45
|
+
aspect?: number | null
|
|
46
|
+
/** Aspects offered in the toolbar as chips (`v-model:aspect` follows them) */
|
|
47
|
+
aspects?: CropAspect[]
|
|
48
|
+
/** Quarter turns clockwise in degrees (`v-model:rotation`) */
|
|
49
|
+
rotation?: 0 | 90 | 180 | 270
|
|
50
|
+
/** Mirrored left to right (`v-model:flipped`) */
|
|
51
|
+
flipped?: boolean
|
|
52
|
+
shape?: 'rect' | 'circle'
|
|
53
|
+
/** The frame's smallest side, in the picture's px */
|
|
54
|
+
minSize?: number
|
|
55
|
+
/** The rule-of-thirds grid: while dragging (default), always or never */
|
|
56
|
+
guides?: 'drag' | 'always' | 'never'
|
|
57
|
+
/** The toolbar under the picture */
|
|
58
|
+
controls?: boolean
|
|
59
|
+
/** Accessible name of the picture */
|
|
60
|
+
alt?: string
|
|
61
|
+
}>(), {
|
|
62
|
+
src: null,
|
|
63
|
+
crop: null,
|
|
64
|
+
aspect: null,
|
|
65
|
+
rotation: 0,
|
|
66
|
+
flipped: false,
|
|
67
|
+
shape: 'rect',
|
|
68
|
+
minSize: 24,
|
|
69
|
+
guides: 'drag',
|
|
70
|
+
controls: true,
|
|
71
|
+
alt: '',
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
const emit = defineEmits<{
|
|
75
|
+
'update:crop': [CropRect]
|
|
76
|
+
'update:aspect': [number | null]
|
|
77
|
+
'update:rotation': [0 | 90 | 180 | 270]
|
|
78
|
+
'update:flipped': [boolean]
|
|
79
|
+
load: [{ width: number; height: number }]
|
|
80
|
+
error: [Event]
|
|
81
|
+
change: [CropRect]
|
|
82
|
+
}>()
|
|
83
|
+
|
|
84
|
+
const locale = useLocale()
|
|
85
|
+
|
|
86
|
+
// ── The picture ───────────────────────────────────────────────────────────────────────────────────────────
|
|
87
|
+
const url = ref<string | null>(null)
|
|
88
|
+
const natural = ref<{ w: number; h: number } | null>(null)
|
|
89
|
+
let objectUrl: string | null = null
|
|
90
|
+
watch(() => props.src, (src) => {
|
|
91
|
+
if (objectUrl) URL.revokeObjectURL(objectUrl)
|
|
92
|
+
objectUrl = null
|
|
93
|
+
natural.value = null
|
|
94
|
+
if (src && typeof src !== 'string') url.value = objectUrl = URL.createObjectURL(src)
|
|
95
|
+
else url.value = src || null
|
|
96
|
+
}, { immediate: true })
|
|
97
|
+
const img = ref<HTMLImageElement>()
|
|
98
|
+
const crossOrigin = computed(() => (typeof url.value === 'string' && /^https?:/.test(url.value) && !url.value.startsWith(location.origin) ? 'anonymous' : undefined))
|
|
99
|
+
|
|
100
|
+
const rot = ref(props.rotation)
|
|
101
|
+
watch(() => props.rotation, (r) => { if (r !== rot.value) applyRotation(r) })
|
|
102
|
+
const flip = ref(props.flipped)
|
|
103
|
+
watch(() => props.flipped, (f) => { if (f !== flip.value) flipX() })
|
|
104
|
+
const ratio = ref<number | null>(props.aspect)
|
|
105
|
+
watch(() => props.aspect, (a) => { if (a !== ratio.value) setAspect(a) })
|
|
106
|
+
|
|
107
|
+
// The rotated picture's size: what the crop is measured in
|
|
108
|
+
const size = computed(() => {
|
|
109
|
+
const n = natural.value
|
|
110
|
+
if (!n) return null
|
|
111
|
+
return rot.value % 180 ? { w: n.h, h: n.w } : { w: n.w, h: n.h }
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
// ── Layout ────────────────────────────────────────────────────────────────────────────────────────────────
|
|
115
|
+
const stage = ref<HTMLElement>()
|
|
116
|
+
const box = ref({ w: 0, h: 0 })
|
|
117
|
+
const PAD = 24
|
|
118
|
+
const fit = computed(() => {
|
|
119
|
+
const s = size.value
|
|
120
|
+
if (!s || !box.value.w) return null
|
|
121
|
+
const scale = Math.min((box.value.w - PAD * 2) / s.w, (box.value.h - PAD * 2) / s.h)
|
|
122
|
+
return { scale, x: (box.value.w - s.w * scale) / 2, y: (box.value.h - s.h * scale) / 2 }
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
// ── The frame ─────────────────────────────────────────────────────────────────────────────────────────────
|
|
126
|
+
const rect = ref<CropRect>({ x: 0, y: 0, width: 0, height: 0 })
|
|
127
|
+
// What's drawn: springs to `rect` when it changes by a button, follows it as is while dragging
|
|
128
|
+
const shown = ref<CropRect>({ x: 0, y: 0, width: 0, height: 0 })
|
|
129
|
+
watch(() => props.crop, (c) => {
|
|
130
|
+
if (!c || !size.value || sameRect(c, rect.value)) return
|
|
131
|
+
rect.value = clampRect({ ...c })
|
|
132
|
+
animateTo(rect.value, false)
|
|
133
|
+
})
|
|
134
|
+
const sameRect = (a: CropRect, b: CropRect) => a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height
|
|
135
|
+
const round = (r: CropRect): CropRect => ({ x: Math.round(r.x), y: Math.round(r.y), width: Math.round(r.width), height: Math.round(r.height) })
|
|
136
|
+
|
|
137
|
+
// The largest frame of the aspect, centered (or the whole picture when free)
|
|
138
|
+
function fullRect(a = ratio.value): CropRect {
|
|
139
|
+
const s = size.value!
|
|
140
|
+
if (!a) return { x: 0, y: 0, width: s.w, height: s.h }
|
|
141
|
+
let w = s.w
|
|
142
|
+
let h = w / a
|
|
143
|
+
if (h > s.h) { h = s.h; w = h * a }
|
|
144
|
+
return { x: (s.w - w) / 2, y: (s.h - h) / 2, width: w, height: h }
|
|
145
|
+
}
|
|
146
|
+
function clampRect(r: CropRect): CropRect {
|
|
147
|
+
const s = size.value!
|
|
148
|
+
const min = Math.min(props.minSize, s.w, s.h)
|
|
149
|
+
let { x, y, width, height } = r
|
|
150
|
+
width = Math.min(Math.max(width, min), s.w)
|
|
151
|
+
height = Math.min(Math.max(height, min), s.h)
|
|
152
|
+
if (ratio.value) {
|
|
153
|
+
// Shrink to fit the aspect inside the picture
|
|
154
|
+
if (width / height > ratio.value) width = height * ratio.value
|
|
155
|
+
else height = width / ratio.value
|
|
156
|
+
}
|
|
157
|
+
x = Math.min(Math.max(x, 0), s.w - width)
|
|
158
|
+
y = Math.min(Math.max(y, 0), s.h - height)
|
|
159
|
+
return { x, y, width, height }
|
|
160
|
+
}
|
|
161
|
+
function commit(r: CropRect, animate: boolean, done = false) {
|
|
162
|
+
rect.value = r
|
|
163
|
+
const out = round(r)
|
|
164
|
+
emit('update:crop', out)
|
|
165
|
+
if (done) emit('change', out)
|
|
166
|
+
animateTo(r, animate)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Standard DefaultSpatial, on the four values together
|
|
170
|
+
let raf = 0
|
|
171
|
+
let vel = [0, 0, 0, 0]
|
|
172
|
+
function animateTo(target: CropRect, animate: boolean) {
|
|
173
|
+
if (!animate || prefersReducedMotion()) {
|
|
174
|
+
cancelAnimationFrame(raf)
|
|
175
|
+
raf = 0
|
|
176
|
+
vel = [0, 0, 0, 0]
|
|
177
|
+
shown.value = { ...target }
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
if (raf) return
|
|
181
|
+
let last = performance.now()
|
|
182
|
+
const step = (now: number) => {
|
|
183
|
+
let dt = Math.min((now - last) / 1000, 1 / 30)
|
|
184
|
+
last = now
|
|
185
|
+
const cur = [shown.value.x, shown.value.y, shown.value.width, shown.value.height]
|
|
186
|
+
const to = [rect.value.x, rect.value.y, rect.value.width, rect.value.height]
|
|
187
|
+
while (dt > 0) {
|
|
188
|
+
const h = Math.min(dt, 0.004)
|
|
189
|
+
for (let i = 0; i < 4; i++) {
|
|
190
|
+
vel[i]! += (-700 * (cur[i]! - to[i]!) - 2 * 0.9 * Math.sqrt(700) * vel[i]!) * h
|
|
191
|
+
cur[i]! += vel[i]! * h
|
|
192
|
+
}
|
|
193
|
+
dt -= h
|
|
194
|
+
}
|
|
195
|
+
const done = cur.every((v, i) => Math.abs(v - to[i]!) < 0.5) && vel.every((v) => Math.abs(v) < 5)
|
|
196
|
+
shown.value = done ? { ...rect.value } : { x: cur[0]!, y: cur[1]!, width: cur[2]!, height: cur[3]! }
|
|
197
|
+
if (done) { raf = 0; vel = [0, 0, 0, 0]; return }
|
|
198
|
+
raf = requestAnimationFrame(step)
|
|
199
|
+
}
|
|
200
|
+
raf = requestAnimationFrame(step)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function onLoad() {
|
|
204
|
+
const el = img.value!
|
|
205
|
+
natural.value = { w: el.naturalWidth, h: el.naturalHeight }
|
|
206
|
+
emit('load', { width: el.naturalWidth, height: el.naturalHeight })
|
|
207
|
+
const r = props.crop ? clampRect({ ...props.crop }) : fullRect()
|
|
208
|
+
commit(r, false)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// ── Rotate, flip, aspect, reset ───────────────────────────────────────────────────────────────────────────
|
|
212
|
+
function applyRotation(to: number) {
|
|
213
|
+
const steps = ((to - rot.value) / 90 + 4) % 4
|
|
214
|
+
for (let i = 0; i < steps; i++) turn(1)
|
|
215
|
+
}
|
|
216
|
+
// A quarter turn: the frame turns with the picture
|
|
217
|
+
function turn(dir: 1 | -1) {
|
|
218
|
+
if (!size.value) {
|
|
219
|
+
rot.value = ((rot.value + dir * 90 + 360) % 360) as 0 | 90 | 180 | 270
|
|
220
|
+
return
|
|
221
|
+
}
|
|
222
|
+
const { w: W, h: H } = size.value
|
|
223
|
+
const r = rect.value
|
|
224
|
+
const next = dir === 1
|
|
225
|
+
? { x: H - (r.y + r.height), y: r.x, width: r.height, height: r.width }
|
|
226
|
+
: { x: r.y, y: W - (r.x + r.width), width: r.height, height: r.width }
|
|
227
|
+
rot.value = ((rot.value + dir * 90 + 360) % 360) as 0 | 90 | 180 | 270
|
|
228
|
+
emit('update:rotation', rot.value)
|
|
229
|
+
// A locked aspect stays the frame's shape: refit what turned
|
|
230
|
+
commit(ratio.value && ratio.value !== 1 ? clampRect({ ...next, ...fitAspect(next) }) : clampRect(next), false)
|
|
231
|
+
}
|
|
232
|
+
function fitAspect(r: CropRect): CropRect {
|
|
233
|
+
const a = ratio.value!
|
|
234
|
+
const cx = r.x + r.width / 2
|
|
235
|
+
const cy = r.y + r.height / 2
|
|
236
|
+
let w = r.width
|
|
237
|
+
let h = w / a
|
|
238
|
+
if (h > r.height) { h = r.height; w = h * a }
|
|
239
|
+
return { x: cx - w / 2, y: cy - h / 2, width: w, height: h }
|
|
240
|
+
}
|
|
241
|
+
// Mirrored left to right as seen. The mirror is kept in the picture's own frame (drawn as rotate · mirror), so a
|
|
242
|
+
// turn after it still turns the whole picture: mirroring as seen is the opposite turn with the mirror toggled
|
|
243
|
+
function flipX() {
|
|
244
|
+
flip.value = !flip.value
|
|
245
|
+
emit('update:flipped', flip.value)
|
|
246
|
+
if (rot.value % 180) {
|
|
247
|
+
rot.value = (360 - rot.value) as 90 | 270
|
|
248
|
+
emit('update:rotation', rot.value)
|
|
249
|
+
}
|
|
250
|
+
if (!size.value) return
|
|
251
|
+
const r = rect.value
|
|
252
|
+
commit({ ...r, x: size.value.w - (r.x + r.width) }, false)
|
|
253
|
+
}
|
|
254
|
+
function setAspect(a: number | null) {
|
|
255
|
+
ratio.value = a
|
|
256
|
+
emit('update:aspect', a)
|
|
257
|
+
if (!size.value) return
|
|
258
|
+
// The largest frame of the new shape around the current one's center
|
|
259
|
+
if (!a) return
|
|
260
|
+
const r = rect.value
|
|
261
|
+
const s = size.value
|
|
262
|
+
const cx = r.x + r.width / 2
|
|
263
|
+
const cy = r.y + r.height / 2
|
|
264
|
+
let w = Math.min(s.w, Math.max(r.width, r.height * a))
|
|
265
|
+
let h = w / a
|
|
266
|
+
if (h > s.h) { h = s.h; w = h * a }
|
|
267
|
+
commit(clampRect({ x: cx - w / 2, y: cy - h / 2, width: w, height: h }), true, true)
|
|
268
|
+
}
|
|
269
|
+
function reset() {
|
|
270
|
+
if (rot.value || flip.value) {
|
|
271
|
+
rot.value = 0
|
|
272
|
+
flip.value = false
|
|
273
|
+
emit('update:rotation', 0)
|
|
274
|
+
emit('update:flipped', false)
|
|
275
|
+
}
|
|
276
|
+
if (size.value) commit(fullRect(), true, true)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// ── Dragging ──────────────────────────────────────────────────────────────────────────────────────────────
|
|
280
|
+
type Handle = 'move' | 'n' | 's' | 'e' | 'w' | 'ne' | 'nw' | 'se' | 'sw'
|
|
281
|
+
const HANDLES: Exclude<Handle, 'move'>[] = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w']
|
|
282
|
+
let drag: { id: number; handle: Handle; sx: number; sy: number; start: CropRect } | null = null
|
|
283
|
+
const dragging = ref(false)
|
|
284
|
+
|
|
285
|
+
function onDown(e: PointerEvent, handle: Handle) {
|
|
286
|
+
if (e.button !== 0 || !fit.value) return
|
|
287
|
+
e.preventDefault()
|
|
288
|
+
e.stopPropagation()
|
|
289
|
+
;(e.currentTarget as HTMLElement).setPointerCapture?.(e.pointerId)
|
|
290
|
+
drag = { id: e.pointerId, handle, sx: e.clientX, sy: e.clientY, start: { ...rect.value } }
|
|
291
|
+
dragging.value = true
|
|
292
|
+
}
|
|
293
|
+
function onMove(e: PointerEvent) {
|
|
294
|
+
if (!drag || e.pointerId !== drag.id || !fit.value) return
|
|
295
|
+
const dx = (e.clientX - drag.sx) / fit.value.scale
|
|
296
|
+
const dy = (e.clientY - drag.sy) / fit.value.scale
|
|
297
|
+
commit(dragRect(drag.start, drag.handle, dx, dy), false)
|
|
298
|
+
}
|
|
299
|
+
function onUp(e: PointerEvent) {
|
|
300
|
+
if (!drag || e.pointerId !== drag.id) return
|
|
301
|
+
drag = null
|
|
302
|
+
dragging.value = false
|
|
303
|
+
emit('change', round(rect.value))
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// The frame after pulling `handle` by dx, dy (picture px), kept inside the picture and to its aspect
|
|
307
|
+
function dragRect(r: CropRect, handle: Handle, dx: number, dy: number): CropRect {
|
|
308
|
+
const s = size.value!
|
|
309
|
+
if (handle === 'move') return clampRect({ ...r, x: r.x + dx, y: r.y + dy })
|
|
310
|
+
const min = Math.min(props.minSize, s.w, s.h)
|
|
311
|
+
let left = r.x
|
|
312
|
+
let top = r.y
|
|
313
|
+
let right = r.x + r.width
|
|
314
|
+
let bottom = r.y + r.height
|
|
315
|
+
if (handle.includes('w')) left = Math.min(Math.max(0, r.x + dx), right - min)
|
|
316
|
+
if (handle.includes('e')) right = Math.max(Math.min(s.w, right + dx), left + min)
|
|
317
|
+
if (handle.includes('n')) top = Math.min(Math.max(0, r.y + dy), bottom - min)
|
|
318
|
+
if (handle.includes('s')) bottom = Math.max(Math.min(s.h, bottom + dy), top + min)
|
|
319
|
+
const a = ratio.value
|
|
320
|
+
if (!a) return { x: left, y: top, width: right - left, height: bottom - top }
|
|
321
|
+
// Locked: the opposite corner (or edge's middle) stays put, and the frame stays inside
|
|
322
|
+
const corner = handle.length === 2
|
|
323
|
+
const ax = handle.includes('w') ? r.x + r.width : handle.includes('e') ? r.x : r.x + r.width / 2
|
|
324
|
+
const ay = handle.includes('n') ? r.y + r.height : handle.includes('s') ? r.y : r.y + r.height / 2
|
|
325
|
+
let w = right - left
|
|
326
|
+
let h = bottom - top
|
|
327
|
+
if (corner) {
|
|
328
|
+
// The bigger pull decides (heights counted in widths, through the aspect)
|
|
329
|
+
if (Math.abs(w - r.width) >= Math.abs(h - r.height) * a) h = w / a
|
|
330
|
+
else w = h * a
|
|
331
|
+
} else if (handle === 'n' || handle === 's') w = h * a
|
|
332
|
+
else h = w / a
|
|
333
|
+
const maxW = handle.includes('w') ? ax : handle.includes('e') ? s.w - ax : 2 * Math.min(ax, s.w - ax)
|
|
334
|
+
const maxH = handle.includes('n') ? ay : handle.includes('s') ? s.h - ay : 2 * Math.min(ay, s.h - ay)
|
|
335
|
+
w = Math.max(Math.min(w, maxW, maxH * a), min)
|
|
336
|
+
h = w / a
|
|
337
|
+
const x = handle.includes('w') ? ax - w : handle.includes('e') ? ax : ax - w / 2
|
|
338
|
+
const y = handle.includes('n') ? ay - h : handle.includes('s') ? ay : ay - h / 2
|
|
339
|
+
return clampRect({ x, y, width: w, height: h })
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function onKey(e: KeyboardEvent) {
|
|
343
|
+
const keys: Record<string, [number, number]> = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }
|
|
344
|
+
const k = keys[e.key]
|
|
345
|
+
if (!k || !size.value) return
|
|
346
|
+
e.preventDefault()
|
|
347
|
+
const step = (e.altKey ? 10 : 1) / (fit.value?.scale ?? 1)
|
|
348
|
+
const r = e.shiftKey ? dragRect(rect.value, k[0] ? 'e' : 's', k[0] * step, k[1] * step) : dragRect(rect.value, 'move', k[0] * step, k[1] * step)
|
|
349
|
+
commit(r, false, true)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// ── Output ────────────────────────────────────────────────────────────────────────────────────────────────
|
|
353
|
+
function toCanvas(options: CropOutputOptions = {}): HTMLCanvasElement | null {
|
|
354
|
+
const el = img.value
|
|
355
|
+
const n = natural.value
|
|
356
|
+
const s = size.value
|
|
357
|
+
if (!el || !n || !s) return null
|
|
358
|
+
const r = round(rect.value)
|
|
359
|
+
let w = options.width ?? (options.height ? (options.height * r.width) / r.height : r.width)
|
|
360
|
+
let h = options.height ?? (options.width ? (options.width * r.height) / r.width : r.height)
|
|
361
|
+
if (options.maxSize && Math.max(w, h) > options.maxSize) {
|
|
362
|
+
const k = options.maxSize / Math.max(w, h)
|
|
363
|
+
w *= k
|
|
364
|
+
h *= k
|
|
365
|
+
}
|
|
366
|
+
const canvas = document.createElement('canvas')
|
|
367
|
+
canvas.width = Math.max(1, Math.round(w))
|
|
368
|
+
canvas.height = Math.max(1, Math.round(h))
|
|
369
|
+
const ctx = canvas.getContext('2d')
|
|
370
|
+
if (!ctx) return null
|
|
371
|
+
ctx.imageSmoothingQuality = 'high'
|
|
372
|
+
if (options.circle ?? props.shape === 'circle') {
|
|
373
|
+
ctx.beginPath()
|
|
374
|
+
ctx.ellipse(canvas.width / 2, canvas.height / 2, canvas.width / 2, canvas.height / 2, 0, 0, Math.PI * 2)
|
|
375
|
+
ctx.clip()
|
|
376
|
+
}
|
|
377
|
+
// Crop space → the rotated, flipped picture → its natural pixels
|
|
378
|
+
ctx.scale(canvas.width / r.width, canvas.height / r.height)
|
|
379
|
+
ctx.translate(-r.x, -r.y)
|
|
380
|
+
ctx.translate(s.w / 2, s.h / 2)
|
|
381
|
+
ctx.rotate((rot.value * Math.PI) / 180)
|
|
382
|
+
if (flip.value) ctx.scale(-1, 1)
|
|
383
|
+
ctx.drawImage(el, -n.w / 2, -n.h / 2)
|
|
384
|
+
return canvas
|
|
385
|
+
}
|
|
386
|
+
function toBlob(options: CropOutputOptions & { type?: string; quality?: number } = {}): Promise<Blob | null> {
|
|
387
|
+
const canvas = toCanvas(options)
|
|
388
|
+
if (!canvas) return Promise.resolve(null)
|
|
389
|
+
const type = options.type ?? ((options.circle ?? props.shape === 'circle') ? 'image/png' : 'image/jpeg')
|
|
390
|
+
return new Promise((resolve) => canvas.toBlob(resolve, type, options.quality ?? 0.92))
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
let ro: ResizeObserver | undefined
|
|
394
|
+
onMounted(() => {
|
|
395
|
+
const el = stage.value!
|
|
396
|
+
const measure = () => { box.value = { w: el.clientWidth, h: el.clientHeight } }
|
|
397
|
+
measure()
|
|
398
|
+
ro = new ResizeObserver(measure)
|
|
399
|
+
ro.observe(el)
|
|
400
|
+
// Already loaded from the cache before the listener
|
|
401
|
+
nextTick(() => { if (img.value?.complete && img.value.naturalWidth && !natural.value) onLoad() })
|
|
402
|
+
})
|
|
403
|
+
onBeforeUnmount(() => {
|
|
404
|
+
ro?.disconnect()
|
|
405
|
+
cancelAnimationFrame(raf)
|
|
406
|
+
if (objectUrl) URL.revokeObjectURL(objectUrl)
|
|
407
|
+
})
|
|
408
|
+
|
|
409
|
+
// ── Drawing ───────────────────────────────────────────────────────────────────────────────────────────────
|
|
410
|
+
const imgStyle = computed(() => {
|
|
411
|
+
const f = fit.value
|
|
412
|
+
const n = natural.value
|
|
413
|
+
if (!f || !n) return { visibility: 'hidden' as const }
|
|
414
|
+
const w = n.w * f.scale
|
|
415
|
+
const h = n.h * f.scale
|
|
416
|
+
const mirror = flip.value ? 'scaleX(-1)' : ''
|
|
417
|
+
return {
|
|
418
|
+
width: `${w}px`,
|
|
419
|
+
height: `${h}px`,
|
|
420
|
+
left: `${box.value.w / 2 - w / 2}px`,
|
|
421
|
+
top: `${box.value.h / 2 - h / 2}px`,
|
|
422
|
+
transform: `rotate(${rot.value}deg) ${mirror}`,
|
|
423
|
+
}
|
|
424
|
+
})
|
|
425
|
+
const frame = computed(() => {
|
|
426
|
+
const f = fit.value
|
|
427
|
+
if (!f) return null
|
|
428
|
+
const r = shown.value
|
|
429
|
+
return { left: f.x + r.x * f.scale, top: f.y + r.y * f.scale, width: r.width * f.scale, height: r.height * f.scale }
|
|
430
|
+
})
|
|
431
|
+
const HANDLE_POS: Record<Exclude<Handle, 'move'>, string> = {
|
|
432
|
+
nw: 'top-0 left-0 cursor-nwse-resize', n: 'top-0 left-1/2 cursor-ns-resize', ne: 'top-0 left-full cursor-nesw-resize', e: 'top-1/2 left-full cursor-ew-resize',
|
|
433
|
+
se: 'top-full left-full cursor-nwse-resize', s: 'top-full left-1/2 cursor-ns-resize', sw: 'top-full left-0 cursor-nesw-resize', w: 'top-1/2 left-0 cursor-ew-resize',
|
|
434
|
+
}
|
|
435
|
+
const showGuides = computed(() => props.guides === 'always' || (props.guides === 'drag' && dragging.value))
|
|
436
|
+
|
|
437
|
+
defineExpose({ toCanvas, toBlob, rotate: turn, flip: flipX, reset, setAspect })
|
|
438
|
+
</script>
|
|
439
|
+
|
|
440
|
+
<template>
|
|
441
|
+
<div class="m3-image-cropper flex flex-col gap-3">
|
|
442
|
+
<div ref="stage" class="relative min-h-0 flex-1 touch-none overflow-hidden rounded-md bg-surface-container-highest select-none">
|
|
443
|
+
<img
|
|
444
|
+
v-if="url"
|
|
445
|
+
ref="img"
|
|
446
|
+
:src="url"
|
|
447
|
+
:alt="alt"
|
|
448
|
+
:crossorigin="crossOrigin"
|
|
449
|
+
draggable="false"
|
|
450
|
+
class="pointer-events-none absolute max-w-none"
|
|
451
|
+
:style="imgStyle"
|
|
452
|
+
@load="onLoad"
|
|
453
|
+
@error="emit('error', $event)"
|
|
454
|
+
>
|
|
455
|
+
<div
|
|
456
|
+
v-if="frame && natural"
|
|
457
|
+
role="slider"
|
|
458
|
+
tabindex="0"
|
|
459
|
+
:aria-label="locale.cropArea"
|
|
460
|
+
:aria-valuetext="`${Math.round(rect.width)} × ${Math.round(rect.height)}`"
|
|
461
|
+
class="absolute cursor-move outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-secondary"
|
|
462
|
+
:class="shape === 'circle' ? 'rounded-full' : ''"
|
|
463
|
+
:style="{ left: `${frame.left}px`, top: `${frame.top}px`, width: `${frame.width}px`, height: `${frame.height}px`, boxShadow: '0 0 0 9999px rgb(0 0 0 / 0.55)' }"
|
|
464
|
+
@pointerdown="onDown($event, 'move')"
|
|
465
|
+
@pointermove="onMove"
|
|
466
|
+
@pointerup="onUp"
|
|
467
|
+
@pointercancel="onUp"
|
|
468
|
+
@keydown="onKey"
|
|
469
|
+
>
|
|
470
|
+
<!-- The frame's line, and the rule of thirds while dragging -->
|
|
471
|
+
<div class="pointer-events-none absolute inset-0 border-2 border-white/90" :class="shape === 'circle' ? 'rounded-full' : ''" />
|
|
472
|
+
<div class="pointer-events-none absolute inset-0 transition-opacity duration-200" :class="showGuides ? 'opacity-100' : 'opacity-0'">
|
|
473
|
+
<div class="absolute inset-y-0 left-1/3 w-px bg-white/60" />
|
|
474
|
+
<div class="absolute inset-y-0 left-2/3 w-px bg-white/60" />
|
|
475
|
+
<div class="absolute inset-x-0 top-1/3 h-px bg-white/60" />
|
|
476
|
+
<div class="absolute inset-x-0 top-2/3 h-px bg-white/60" />
|
|
477
|
+
</div>
|
|
478
|
+
<div
|
|
479
|
+
v-for="h in HANDLES"
|
|
480
|
+
:key="h"
|
|
481
|
+
class="absolute flex size-11 -translate-1/2 items-center justify-center"
|
|
482
|
+
:class="HANDLE_POS[h]"
|
|
483
|
+
@pointerdown="onDown($event, h)"
|
|
484
|
+
@pointermove="onMove"
|
|
485
|
+
@pointerup="onUp"
|
|
486
|
+
@pointercancel="onUp"
|
|
487
|
+
>
|
|
488
|
+
<span class="block bg-white shadow-elevation-1" :class="h.length === 2 ? 'size-4 rounded-full' : h === 'n' || h === 's' ? 'h-1.5 w-6 rounded-full' : 'h-6 w-1.5 rounded-full'" />
|
|
489
|
+
</div>
|
|
490
|
+
</div>
|
|
491
|
+
</div>
|
|
492
|
+
|
|
493
|
+
<slot v-if="controls" name="controls" :rotate="turn" :flip="flipX" :reset="reset" :aspect="ratio" :set-aspect="setAspect">
|
|
494
|
+
<div class="flex flex-wrap items-center gap-1">
|
|
495
|
+
<MIconButton icon="rotate_left" :label="locale.cropRotateLeft" @click="turn(-1)" />
|
|
496
|
+
<MIconButton icon="rotate_right" :label="locale.cropRotateRight" @click="turn(1)" />
|
|
497
|
+
<MIconButton icon="flip" :label="locale.cropFlip" :checked="flip" @click="flipX" />
|
|
498
|
+
<MIconButton icon="restart_alt" :label="locale.cropReset" @click="reset" />
|
|
499
|
+
<div v-if="aspects?.length" class="ml-2 flex flex-wrap gap-2">
|
|
500
|
+
<MChip v-for="a in aspects" :key="a.label" type="filter" :selected="ratio === a.value" @click="setAspect(a.value)">
|
|
501
|
+
{{ a.label === '' ? locale.cropFree : a.label }}
|
|
502
|
+
</MChip>
|
|
503
|
+
</div>
|
|
504
|
+
</div>
|
|
505
|
+
</slot>
|
|
506
|
+
</div>
|
|
507
|
+
</template>
|
|
@@ -181,6 +181,19 @@ const labelClasses = computed(() => {
|
|
|
181
181
|
'peer-disabled:text-on-surface/38',
|
|
182
182
|
].join(' ')
|
|
183
183
|
})
|
|
184
|
+
|
|
185
|
+
defineExpose({
|
|
186
|
+
/** The `<input>` (or `<textarea>`) element */
|
|
187
|
+
inputEl: inputEl,
|
|
188
|
+
/** Focuses the field */
|
|
189
|
+
focus: (options?: FocusOptions) => inputEl.value?.focus(options),
|
|
190
|
+
/** Takes focus away from the field */
|
|
191
|
+
blur: () => inputEl.value?.blur(),
|
|
192
|
+
/** Selects all its text */
|
|
193
|
+
select: () => inputEl.value?.select(),
|
|
194
|
+
/** Selects part of its text (e.g. a file name without its extension) */
|
|
195
|
+
setSelectionRange: (start: number, end: number, direction?: 'forward' | 'backward' | 'none') => inputEl.value?.setSelectionRange(start, end, direction),
|
|
196
|
+
})
|
|
184
197
|
</script>
|
|
185
198
|
|
|
186
199
|
<template>
|
|
@@ -60,6 +60,7 @@ const emit = defineEmits<{ 'update:modelValue': [number | null]; debounced: [num
|
|
|
60
60
|
|
|
61
61
|
const id = useId()
|
|
62
62
|
const fieldBgEl = ref<HTMLElement | null>(null)
|
|
63
|
+
const inputEl = ref<HTMLInputElement | null>(null)
|
|
63
64
|
const { resolvedFieldBg } = useFieldBg(fieldBgEl, () => props.fieldBg)
|
|
64
65
|
|
|
65
66
|
const { debounced: emitDebounced } = useDebounce((v: number | null) => emit('debounced', v), props.debounce)
|
|
@@ -161,6 +162,17 @@ const labelClasses = computed(() => {
|
|
|
161
162
|
'peer-disabled:text-on-surface/38',
|
|
162
163
|
].join(' ')
|
|
163
164
|
})
|
|
165
|
+
|
|
166
|
+
defineExpose({
|
|
167
|
+
/** The `<input>` (or `<textarea>`) element */
|
|
168
|
+
inputEl: inputEl,
|
|
169
|
+
/** Focuses the field */
|
|
170
|
+
focus: (options?: FocusOptions) => inputEl.value?.focus(options),
|
|
171
|
+
/** Takes focus away from the field */
|
|
172
|
+
blur: () => inputEl.value?.blur(),
|
|
173
|
+
/** Selects all its text */
|
|
174
|
+
select: () => inputEl.value?.select(),
|
|
175
|
+
})
|
|
164
176
|
</script>
|
|
165
177
|
|
|
166
178
|
<template>
|
|
@@ -183,6 +195,7 @@ const labelClasses = computed(() => {
|
|
|
183
195
|
</div>
|
|
184
196
|
|
|
185
197
|
<input
|
|
198
|
+
ref="inputEl"
|
|
186
199
|
:id="id"
|
|
187
200
|
type="number"
|
|
188
201
|
:value="modelValue ?? ''"
|