@salmonui/editors 0.0.0-stage → 0.3.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 +39 -2
- package/dist/image-editor/ImageEditor.d.ts +59 -0
- package/dist/image-editor/ImageEditor.d.ts.map +1 -0
- package/dist/image-editor/ImageEditor.js +493 -0
- package/dist/image-editor/ImageEditor.js.map +1 -0
- package/dist/image-editor/adjust.d.ts +17 -0
- package/dist/image-editor/adjust.d.ts.map +1 -0
- package/dist/image-editor/adjust.js +53 -0
- package/dist/image-editor/adjust.js.map +1 -0
- package/dist/image-editor/annotations.d.ts +40 -0
- package/dist/image-editor/annotations.d.ts.map +1 -0
- package/dist/image-editor/annotations.js +184 -0
- package/dist/image-editor/annotations.js.map +1 -0
- package/dist/image-editor/draw.d.ts +25 -0
- package/dist/image-editor/draw.d.ts.map +1 -0
- package/dist/image-editor/draw.js +248 -0
- package/dist/image-editor/draw.js.map +1 -0
- package/dist/image-editor/engine.d.ts +116 -0
- package/dist/image-editor/engine.d.ts.map +1 -0
- package/dist/image-editor/engine.js +367 -0
- package/dist/image-editor/engine.js.map +1 -0
- package/dist/image-editor/geometry.d.ts +41 -0
- package/dist/image-editor/geometry.d.ts.map +1 -0
- package/dist/image-editor/geometry.js +62 -0
- package/dist/image-editor/geometry.js.map +1 -0
- package/dist/image-editor/history.d.ts +24 -0
- package/dist/image-editor/history.d.ts.map +1 -0
- package/dist/image-editor/history.js +31 -0
- package/dist/image-editor/history.js.map +1 -0
- package/dist/image-editor/interact.d.ts +73 -0
- package/dist/image-editor/interact.d.ts.map +1 -0
- package/dist/image-editor/interact.js +686 -0
- package/dist/image-editor/interact.js.map +1 -0
- package/dist/image-editor/items.d.ts +9 -0
- package/dist/image-editor/items.d.ts.map +1 -0
- package/dist/image-editor/items.js +179 -0
- package/dist/image-editor/items.js.map +1 -0
- package/dist/image-editor/ops.d.ts +35 -0
- package/dist/image-editor/ops.d.ts.map +1 -0
- package/dist/image-editor/ops.js +332 -0
- package/dist/image-editor/ops.js.map +1 -0
- package/dist/image-editor/panels.d.ts +24 -0
- package/dist/image-editor/panels.d.ts.map +1 -0
- package/dist/image-editor/panels.js +185 -0
- package/dist/image-editor/panels.js.map +1 -0
- package/dist/image-editor/pixels.d.ts +10 -0
- package/dist/image-editor/pixels.d.ts.map +1 -0
- package/dist/image-editor/pixels.js +172 -0
- package/dist/image-editor/pixels.js.map +1 -0
- package/dist/image-editor/transform.d.ts +33 -0
- package/dist/image-editor/transform.d.ts.map +1 -0
- package/dist/image-editor/transform.js +142 -0
- package/dist/image-editor/transform.js.map +1 -0
- package/dist/image-editor/types.d.ts +450 -0
- package/dist/image-editor/types.d.ts.map +1 -0
- package/dist/image-editor/types.js +6 -0
- package/dist/image-editor/types.js.map +1 -0
- package/dist/image-editor.css +1228 -0
- package/dist/image-editor.d.ts +5 -0
- package/dist/image-editor.d.ts.map +1 -0
- package/dist/image-editor.js +13 -0
- package/dist/image-editor.js.map +1 -0
- package/dist/inline-edit/InlineEdit.d.ts +6 -0
- package/dist/inline-edit/InlineEdit.d.ts.map +1 -0
- package/dist/inline-edit/InlineEdit.js +340 -0
- package/dist/inline-edit/InlineEdit.js.map +1 -0
- package/dist/inline-edit/types.d.ts +177 -0
- package/dist/inline-edit/types.d.ts.map +1 -0
- package/dist/inline-edit/types.js +2 -0
- package/dist/inline-edit/types.js.map +1 -0
- package/dist/inline-edit/validate.d.ts +17 -0
- package/dist/inline-edit/validate.d.ts.map +1 -0
- package/dist/inline-edit/validate.js +34 -0
- package/dist/inline-edit/validate.js.map +1 -0
- package/dist/inline-edit.css +621 -0
- package/dist/inline-edit.d.ts +3 -0
- package/dist/inline-edit.d.ts.map +1 -0
- package/dist/inline-edit.js +9 -0
- package/dist/inline-edit.js.map +1 -0
- package/dist/markdown-editor/MarkdownEditor.d.ts +6 -0
- package/dist/markdown-editor/MarkdownEditor.d.ts.map +1 -0
- package/dist/markdown-editor/MarkdownEditor.js +423 -0
- package/dist/markdown-editor/MarkdownEditor.js.map +1 -0
- package/dist/markdown-editor/commands.d.ts +69 -0
- package/dist/markdown-editor/commands.d.ts.map +1 -0
- package/dist/markdown-editor/commands.js +207 -0
- package/dist/markdown-editor/commands.js.map +1 -0
- package/dist/markdown-editor/dialog.d.ts +19 -0
- package/dist/markdown-editor/dialog.d.ts.map +1 -0
- package/dist/markdown-editor/dialog.js +88 -0
- package/dist/markdown-editor/dialog.js.map +1 -0
- package/dist/markdown-editor/items.d.ts +12 -0
- package/dist/markdown-editor/items.d.ts.map +1 -0
- package/dist/markdown-editor/items.js +110 -0
- package/dist/markdown-editor/items.js.map +1 -0
- package/dist/markdown-editor/menu-popup.d.ts +14 -0
- package/dist/markdown-editor/menu-popup.d.ts.map +1 -0
- package/dist/markdown-editor/menu-popup.js +13 -0
- package/dist/markdown-editor/menu-popup.js.map +1 -0
- package/dist/markdown-editor/types.d.ts +158 -0
- package/dist/markdown-editor/types.d.ts.map +1 -0
- package/dist/markdown-editor/types.js +2 -0
- package/dist/markdown-editor/types.js.map +1 -0
- package/dist/markdown-editor.css +1020 -0
- package/dist/markdown-editor.d.ts +6 -0
- package/dist/markdown-editor.d.ts.map +1 -0
- package/dist/markdown-editor.js +12 -0
- package/dist/markdown-editor.js.map +1 -0
- package/dist/styles.css +1699 -0
- package/package.json +53 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NuLL
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @salmonui/editors
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
InlineEdit, MarkdownEditor and ImageEditor for Salmon UI.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @salmonui/editors @salmonui/core react react-dom
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
`react` and `react-dom` `>=19` are peer dependencies. ESM only.
|
|
10
|
+
Every `@salmonui/*` package of an application must resolve to the same
|
|
11
|
+
version (they are released together and exact-pinned to each other);
|
|
12
|
+
`@salmonui/core` warns in development when a second copy loads.
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { InlineEdit } from '@salmonui/editors/inline-edit';
|
|
16
|
+
import { MarkdownEditor } from '@salmonui/editors/markdown-editor';
|
|
17
|
+
import { ImageEditor } from '@salmonui/editors/image-editor';
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Import the theme stylesheet once per application, then the
|
|
21
|
+
stylesheet of each family you use:
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import '@salmonui/core/styles.css'; // the theme, once per application
|
|
25
|
+
import '@salmonui/editors/inline-edit/styles.css';
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Each family has its own `@salmonui/editors/<family>/styles.css`.
|
|
29
|
+
A family stylesheet carries its own rules and those of every family and shared
|
|
30
|
+
module it renders, so it is the only stylesheet that family needs.
|
|
31
|
+
|
|
32
|
+
Public entry points:
|
|
33
|
+
|
|
34
|
+
- `@salmonui/editors/inline-edit`
|
|
35
|
+
- `@salmonui/editors/markdown-editor`
|
|
36
|
+
- `@salmonui/editors/image-editor`
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
MIT
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type ReactElement } from 'react';
|
|
2
|
+
import { ImageEditorEngine } from './engine.js';
|
|
3
|
+
import { type Point, type Size } from './geometry.js';
|
|
4
|
+
import type { StyleDefaults } from './panels.js';
|
|
5
|
+
import type { ImageEditorAdjustments, ImageEditorAnnotation, ImageEditorCropRatio, ImageEditorEditState, ImageEditorFilter, ImageEditorFrame, ImageEditorInputSource, ImageEditorLocaleText, ImageEditorOperation, ImageEditorProps, ImageEditorTool, ImageEditorTransform } from './types.js';
|
|
6
|
+
export declare const IMAGE_EDITOR_DEFAULT_CROP_RATIOS: readonly ImageEditorCropRatio[];
|
|
7
|
+
export type AnnotationAction = 'add' | 'update' | 'delete' | 'clone' | 'reorder';
|
|
8
|
+
type FilePurpose = {
|
|
9
|
+
kind: 'open';
|
|
10
|
+
} | {
|
|
11
|
+
kind: 'image';
|
|
12
|
+
at: Point | null;
|
|
13
|
+
};
|
|
14
|
+
/** What the lazy chunks read from the latest render. Every function is safe to call from an event handler. */
|
|
15
|
+
export interface Ctx {
|
|
16
|
+
readonly text: ImageEditorLocaleText;
|
|
17
|
+
readonly engine: ImageEditorEngine;
|
|
18
|
+
readonly current: ImageEditorEditState;
|
|
19
|
+
readonly transform: ImageEditorTransform;
|
|
20
|
+
readonly adjustments: ImageEditorAdjustments;
|
|
21
|
+
readonly filter: ImageEditorFilter;
|
|
22
|
+
readonly frame: ImageEditorFrame;
|
|
23
|
+
readonly annotations: readonly ImageEditorAnnotation[];
|
|
24
|
+
readonly tool: ImageEditorTool;
|
|
25
|
+
readonly selected: string | null;
|
|
26
|
+
readonly editing: string | null;
|
|
27
|
+
readonly natural: Size | null;
|
|
28
|
+
readonly zoom: number;
|
|
29
|
+
readonly imageSmoothing: boolean;
|
|
30
|
+
readonly cropRatios: readonly ImageEditorCropRatio[];
|
|
31
|
+
readonly styleDefaults: StyleDefaults;
|
|
32
|
+
readonly cropTick: number;
|
|
33
|
+
readonly onBeforeExport: ImageEditorProps['onBeforeExport'];
|
|
34
|
+
readonly onSave: ImageEditorProps['onSave'];
|
|
35
|
+
latest(): Ctx;
|
|
36
|
+
edit<T>(op: ImageEditorOperation, previous: T, value: T, next: ImageEditorEditState, source: ImageEditorInputSource, opts?: {
|
|
37
|
+
action?: AnnotationAction;
|
|
38
|
+
key?: string;
|
|
39
|
+
detail?: string;
|
|
40
|
+
}): boolean;
|
|
41
|
+
changeAnnotations(next: readonly ImageEditorAnnotation[], action: AnnotationAction, source: ImageEditorInputSource, select?: string | null, key?: string): boolean;
|
|
42
|
+
select(id: string | null, source: ImageEditorInputSource): boolean;
|
|
43
|
+
setTool(tool: ImageEditorTool, source: ImageEditorInputSource): void;
|
|
44
|
+
setEditing(id: string | null): void;
|
|
45
|
+
setStyleDefaults(patch: Partial<StyleDefaults>): void;
|
|
46
|
+
pickFile(purpose: FilePurpose): void;
|
|
47
|
+
say(op: ImageEditorOperation, detail?: string): void;
|
|
48
|
+
canvas(): HTMLCanvasElement | null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* ImageEditor: crop, rotate, straighten, flip, resize, zoom and pan, finetune
|
|
52
|
+
* and filter presets, frames, shape / text / freehand / image annotations,
|
|
53
|
+
* redaction, undo and redo, and export through `actionsRef`.
|
|
54
|
+
* `.salmon-image-editor` with `data-status` and `data-tool`; `ref` is the
|
|
55
|
+
* canvas (`role="img"`).
|
|
56
|
+
*/
|
|
57
|
+
export declare function ImageEditor(props: ImageEditorProps): ReactElement;
|
|
58
|
+
export {};
|
|
59
|
+
//# sourceMappingURL=ImageEditor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ImageEditor.d.ts","sourceRoot":"","sources":["../../src/image-editor/ImageEditor.tsx"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
// ImageEditor. Canvas 2D, zero dependencies. React owns the committed edit
|
|
4
|
+
// (controlled triads, history, tool, selection); the engine (`engine.ts`)
|
|
5
|
+
// owns the canvas and the requestAnimationFrame render loop, and its lazily
|
|
6
|
+
// loaded interaction layer (`interact.ts`) every pointer, wheel, pinch and
|
|
7
|
+
// drag transient. Edit operations (`ops.ts`), panels and the quick-access
|
|
8
|
+
// toolbar (`panels.tsx`), the annotation model and drawing, frames and the
|
|
9
|
+
// pixel fallback are lazy chunks. Render reads no browser global, so the
|
|
10
|
+
// server renders the same shell.
|
|
11
|
+
import { lazy, Suspense, useEffect, useId, useImperativeHandle, useLayoutEffect, useRef, useState } from 'react';
|
|
12
|
+
import { Button } from '@salmonui/buttons/button';
|
|
13
|
+
import { Toolbar, ToolbarItem, ToolbarSeparator } from '@salmonui/navigation/toolbar';
|
|
14
|
+
import { useControllableState } from '@salmonui/core/internal/controllable';
|
|
15
|
+
import { useLocaleText } from '@salmonui/core/internal/locale';
|
|
16
|
+
import { announce, liveRegionProps } from '@salmonui/core/internal/live-region';
|
|
17
|
+
import { pressSource } from '@salmonui/core/internal/press';
|
|
18
|
+
import { filterOps } from './adjust.js';
|
|
19
|
+
import { ImageEditorEngine } from './engine.js';
|
|
20
|
+
import { clamp, outputSize } from './geometry.js';
|
|
21
|
+
import { emptyHistory, pushHistory, redoHistory, undoHistory } from './history.js';
|
|
22
|
+
import { IMAGE_EDITOR_DEFAULT_LOCALE_TEXT, IMAGE_EDITOR_DEFAULT_QUICK_TOOLBAR, IMAGE_EDITOR_DEFAULT_TOOLBAR, itemIcon, TOOL_ITEMS } from './items.js';
|
|
23
|
+
const Panels = lazy(() => import('./panels.js'));
|
|
24
|
+
const loadOps = () => import('./ops.js');
|
|
25
|
+
const EMPTY_TRANSFORM = Object.freeze({});
|
|
26
|
+
const EMPTY_ADJUSTMENTS = Object.freeze({});
|
|
27
|
+
const NO_FRAME = Object.freeze({ type: 'none' });
|
|
28
|
+
const NO_ANNOTATIONS = Object.freeze([]);
|
|
29
|
+
const FRESH = Object.freeze({ transform: EMPTY_TRANSFORM, adjustments: EMPTY_ADJUSTMENTS, filter: 'default', frame: NO_FRAME, annotations: NO_ANNOTATIONS });
|
|
30
|
+
export const IMAGE_EDITOR_DEFAULT_CROP_RATIOS = Object.freeze(['free', 'square', 'circle', '3:2', '4:3', '16:9']);
|
|
31
|
+
const PANEL_TOOLS = new Set(['crop', 'straighten', 'resize', 'adjust', 'filter', 'frame', 'rectangle', 'ellipse', 'line', 'arrow', 'path', 'freehand', 'text', 'redact']);
|
|
32
|
+
const DRAWING = new Set(['rectangle', 'ellipse', 'line', 'arrow', 'path', 'freehand', 'text', 'image', 'redact']);
|
|
33
|
+
/** Before / after callback names per operation: `onBefore<Name>` / `on<Name>`. */
|
|
34
|
+
const EVENTS = {
|
|
35
|
+
crop: 'Crop',
|
|
36
|
+
rotate: 'Rotate',
|
|
37
|
+
flip: 'Flip',
|
|
38
|
+
resize: 'Resize',
|
|
39
|
+
zoom: 'Zoom',
|
|
40
|
+
pan: 'Pan',
|
|
41
|
+
annotation: 'AnnotationChange',
|
|
42
|
+
filter: 'Filter',
|
|
43
|
+
adjustment: 'Adjust',
|
|
44
|
+
frame: 'Frame',
|
|
45
|
+
selection: 'Select',
|
|
46
|
+
};
|
|
47
|
+
const stripZoom = (t) => {
|
|
48
|
+
if (t.zoom === undefined)
|
|
49
|
+
return t;
|
|
50
|
+
const { zoom: _zoom, ...rest } = t;
|
|
51
|
+
return rest;
|
|
52
|
+
};
|
|
53
|
+
const cx = (...names) => names.filter(Boolean).join(' ');
|
|
54
|
+
const freeze = Object.freeze;
|
|
55
|
+
/**
|
|
56
|
+
* ImageEditor: crop, rotate, straighten, flip, resize, zoom and pan, finetune
|
|
57
|
+
* and filter presets, frames, shape / text / freehand / image annotations,
|
|
58
|
+
* redaction, undo and redo, and export through `actionsRef`.
|
|
59
|
+
* `.salmon-image-editor` with `data-status` and `data-tool`; `ref` is the
|
|
60
|
+
* canvas (`role="img"`).
|
|
61
|
+
*/
|
|
62
|
+
export function ImageEditor(props) {
|
|
63
|
+
const { src, crossOrigin, onLoad, onLoadError, accept = 'image/*', transform: transformProp, defaultTransform, onTransformChange, adjustments: adjustmentsProp, defaultAdjustments, onAdjustmentsChange, filter: filterProp, defaultFilter, onFilterChange, frame: frameProp, defaultFrame, onFrameChange, annotations: annotationsProp, defaultAnnotations, onAnnotationsChange, tool: toolProp, defaultTool, onToolChange, cropRatios = IMAGE_EDITOR_DEFAULT_CROP_RATIOS, minZoom = 1, maxZoom = 10, undoLimit = 50, imageSmoothing = true, annotationDefaults, toolbar = IMAGE_EDITOR_DEFAULT_TOOLBAR, renderToolbar, onToolbarItemClick, quickAccessToolbar = IMAGE_EDITOR_DEFAULT_QUICK_TOOLBAR, onHistoryChange, onEditComplete, onBeforeExport, onSave, localeText: ownText, classNames, styles, actionsRef, ref, className, onKeyDown, ...others } = props;
|
|
64
|
+
// The before / after callbacks stay off the root element.
|
|
65
|
+
const rest = { ...others };
|
|
66
|
+
for (const name of Object.values(EVENTS))
|
|
67
|
+
delete rest[`onBefore${name}`], delete rest[`on${name}`];
|
|
68
|
+
const localeText = useLocaleText('imageEditor', ownText);
|
|
69
|
+
const text = localeText ? { ...IMAGE_EDITOR_DEFAULT_LOCALE_TEXT, ...localeText } : IMAGE_EDITOR_DEFAULT_LOCALE_TEXT;
|
|
70
|
+
const [transform, setTransform] = useControllableState(transformProp, defaultTransform ?? EMPTY_TRANSFORM, onTransformChange, 'ImageEditor transform');
|
|
71
|
+
const [adjustments, setAdjustments] = useControllableState(adjustmentsProp, defaultAdjustments ?? EMPTY_ADJUSTMENTS, onAdjustmentsChange, 'ImageEditor adjustments');
|
|
72
|
+
const [filter, setFilter] = useControllableState(filterProp, defaultFilter ?? 'default', onFilterChange, 'ImageEditor filter');
|
|
73
|
+
const [frame, setFrame] = useControllableState(frameProp, defaultFrame ?? NO_FRAME, onFrameChange, 'ImageEditor frame');
|
|
74
|
+
const [annotations, setAnnotations] = useControllableState(annotationsProp, defaultAnnotations ?? NO_ANNOTATIONS, onAnnotationsChange, 'ImageEditor annotations');
|
|
75
|
+
const [tool, setToolState] = useControllableState(toolProp, defaultTool ?? 'select', onToolChange, 'ImageEditor tool');
|
|
76
|
+
const [selected, setSelected] = useState(null);
|
|
77
|
+
const [editing, setEditing] = useState(null);
|
|
78
|
+
const [history, setHistoryState] = useState(emptyHistory);
|
|
79
|
+
const [opened, setOpened] = useState(null);
|
|
80
|
+
const source = opened ?? src ?? null;
|
|
81
|
+
const [status, setStatus] = useState(source ? 'loading' : 'empty');
|
|
82
|
+
const [natural, setNatural] = useState(null);
|
|
83
|
+
const [cropTick, setCropTick] = useState(0);
|
|
84
|
+
const [styleDefaults, setStyleDefaults] = useState(() => {
|
|
85
|
+
const d = annotationDefaults ?? {};
|
|
86
|
+
return { stroke: d.stroke ?? '#e5484d', fill: d.fill ?? '', strokeWidth: d.strokeWidth ?? 4, fontSize: d.fontSize ?? 32, fontFamily: d.fontFamily ?? 'sans-serif', color: d.color ?? '#e5484d', redactMode: 'blur', strength: 12 };
|
|
87
|
+
});
|
|
88
|
+
const [initial, setInitial] = useState(() => ({ transform: stripZoom(transformProp ?? defaultTransform ?? EMPTY_TRANSFORM), adjustments: adjustmentsProp ?? defaultAdjustments ?? EMPTY_ADJUSTMENTS, filter: filterProp ?? defaultFilter ?? 'default', frame: frameProp ?? defaultFrame ?? NO_FRAME, annotations: annotationsProp ?? defaultAnnotations ?? NO_ANNOTATIONS }));
|
|
89
|
+
// Two commits in one event compose: handlers read the latest history here.
|
|
90
|
+
const historyRef = useRef(history);
|
|
91
|
+
const canvasRef = useRef(null);
|
|
92
|
+
const liveRef = useRef(null);
|
|
93
|
+
const fileRef = useRef(null);
|
|
94
|
+
const textRef = useRef(null);
|
|
95
|
+
const filePurpose = useRef({ kind: 'open' });
|
|
96
|
+
const openedByUser = useRef(false);
|
|
97
|
+
const panFrom = useRef({ x: 0, y: 0 });
|
|
98
|
+
const ops = useRef(null);
|
|
99
|
+
const self = useRef(null);
|
|
100
|
+
// The engine's zoom and pan proposals, and the history actions, bound to the latest render.
|
|
101
|
+
const viewRef = useRef(null);
|
|
102
|
+
const historyActions = useRef(null);
|
|
103
|
+
const hintId = useId();
|
|
104
|
+
const zoom = clamp(transform.zoom ?? 1, minZoom, maxZoom);
|
|
105
|
+
const current = { transform: stripZoom(transform), adjustments, filter, frame, annotations };
|
|
106
|
+
const [engine] = useState(() => new ImageEditorEngine({
|
|
107
|
+
annotations: (next, action, source, select) => void self.current?.changeAnnotations(next, action, source, select),
|
|
108
|
+
select: (id, source) => void self.current?.select(id, source),
|
|
109
|
+
zoom: (z, source) => viewRef.current?.('zoom', z, source) ?? false,
|
|
110
|
+
panStart: (source) => viewRef.current?.('panStart', null, source) ?? false,
|
|
111
|
+
panEnd: (pan, source) => void viewRef.current?.('panEnd', pan, source),
|
|
112
|
+
editText: (id) => self.current?.setEditing(id),
|
|
113
|
+
pickImage: (at) => self.current?.pickFile({ kind: 'image', at }),
|
|
114
|
+
cropDraft: () => setCropTick((n) => n + 1),
|
|
115
|
+
create: (kind, at, list, sized) => (self.current && ops.current ? ops.current.create(self.current, kind, at, list, sized) : null),
|
|
116
|
+
}));
|
|
117
|
+
const say = (op, detail) => announce(liveRef.current, text.announce(op, detail));
|
|
118
|
+
/** Before / after pair around `apply`; `false` when the before-callback cancelled. */
|
|
119
|
+
const run = (op, previous, value, source, apply, action) => {
|
|
120
|
+
const name = EVENTS[op];
|
|
121
|
+
const p = props;
|
|
122
|
+
const base = { operation: op, previous, value, source, action: action === 'clone' ? 'add' : action };
|
|
123
|
+
const before = name ? p[`onBefore${name}`] : undefined;
|
|
124
|
+
if (before) {
|
|
125
|
+
let prevented = false;
|
|
126
|
+
const event = {
|
|
127
|
+
...base,
|
|
128
|
+
get defaultPrevented() {
|
|
129
|
+
return prevented;
|
|
130
|
+
},
|
|
131
|
+
preventDefault() {
|
|
132
|
+
prevented = true;
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
before(event);
|
|
136
|
+
if (prevented)
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
if (!apply)
|
|
140
|
+
return true;
|
|
141
|
+
apply();
|
|
142
|
+
if (name)
|
|
143
|
+
p[`on${name}`]?.(freeze(base));
|
|
144
|
+
return true;
|
|
145
|
+
};
|
|
146
|
+
const setHistory = (next) => {
|
|
147
|
+
historyRef.current = next;
|
|
148
|
+
setHistoryState(next);
|
|
149
|
+
onHistoryChange?.(freeze({ canUndo: next.past.length > 0, canRedo: next.future.length > 0, undoCount: next.past.length, redoCount: next.future.length }));
|
|
150
|
+
};
|
|
151
|
+
/** Proposes a new edit state: records history, calls the changed parts' callbacks, reports completion. */
|
|
152
|
+
const commit = (op, next, source, reason, key, record = true, detail, complete = true) => {
|
|
153
|
+
const ev = freeze({ source, reason });
|
|
154
|
+
if (record)
|
|
155
|
+
setHistory(pushHistory(historyRef.current, current, undoLimit, key, key ? Date.now() : 0));
|
|
156
|
+
if (next.transform !== current.transform)
|
|
157
|
+
setTransform((prev) => (prev.zoom === undefined ? next.transform : { ...next.transform, zoom: prev.zoom }), ev);
|
|
158
|
+
if (next.adjustments !== current.adjustments)
|
|
159
|
+
setAdjustments(next.adjustments, ev);
|
|
160
|
+
if (next.filter !== current.filter)
|
|
161
|
+
setFilter(next.filter, ev);
|
|
162
|
+
if (next.frame !== current.frame)
|
|
163
|
+
setFrame(next.frame, ev);
|
|
164
|
+
if (next.annotations !== current.annotations)
|
|
165
|
+
setAnnotations(next.annotations, ev);
|
|
166
|
+
if (complete)
|
|
167
|
+
done(op, source, next, detail);
|
|
168
|
+
};
|
|
169
|
+
const done = (op, source, state, detail) => {
|
|
170
|
+
onEditComplete?.(freeze({ operation: op, source, state }));
|
|
171
|
+
say(op, detail);
|
|
172
|
+
};
|
|
173
|
+
// onEditComplete follows the after-callback.
|
|
174
|
+
const edit = (op, previous, value, next, source, opts = {}) => run(op, previous, value, source, () => commit(op, next, source, op === 'annotation' ? (opts.action ?? 'update') : op, opts.key, true, opts.detail, false), opts.action) && (done(op, source, next, opts.detail), true);
|
|
175
|
+
const setTool = (next, source) => {
|
|
176
|
+
if (next === tool)
|
|
177
|
+
return;
|
|
178
|
+
if (tool === 'path')
|
|
179
|
+
engine.ui?.finishPath(source);
|
|
180
|
+
setToolState(next, freeze({ source, reason: 'selection' }));
|
|
181
|
+
engine.preview = null;
|
|
182
|
+
};
|
|
183
|
+
const select = (id, source) => id === selected ||
|
|
184
|
+
run('selection', selected, id, source, () => {
|
|
185
|
+
setSelected(id);
|
|
186
|
+
if (editing && editing !== id)
|
|
187
|
+
setEditing(null);
|
|
188
|
+
});
|
|
189
|
+
const changeAnnotations = (next, action, source, selectId, key) => {
|
|
190
|
+
if (next === annotations)
|
|
191
|
+
return false;
|
|
192
|
+
const ok = edit('annotation', annotations, next, { ...current, annotations: next }, source, { action, detail: action, ...(key ? { key } : {}) });
|
|
193
|
+
if (ok && selectId !== undefined && selectId !== selected)
|
|
194
|
+
run('selection', selected, selectId, source, () => setSelected(selectId));
|
|
195
|
+
return ok;
|
|
196
|
+
};
|
|
197
|
+
const proposeZoom = (z, source) => {
|
|
198
|
+
const next = clamp(z, minZoom, maxZoom);
|
|
199
|
+
return run('zoom', zoom, next, source, () => {
|
|
200
|
+
setTransform((prev) => ({ ...prev, zoom: next }), freeze({ source, reason: 'zoom' }));
|
|
201
|
+
say('zoom', `${Math.round(next * 100)}%`);
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
const pickFile = (purpose) => {
|
|
205
|
+
filePurpose.current = purpose;
|
|
206
|
+
const input = fileRef.current;
|
|
207
|
+
if (!input)
|
|
208
|
+
return;
|
|
209
|
+
input.accept = purpose.kind === 'image' ? 'image/*' : accept;
|
|
210
|
+
input.value = '';
|
|
211
|
+
input.click();
|
|
212
|
+
};
|
|
213
|
+
const restore = (state, op, source, next) => {
|
|
214
|
+
setHistory(next);
|
|
215
|
+
commit(op, state, source, op, undefined, false);
|
|
216
|
+
if (selected && !state.annotations.some((a) => a.id === selected))
|
|
217
|
+
setSelected(null);
|
|
218
|
+
setEditing(null);
|
|
219
|
+
};
|
|
220
|
+
const undo = (source) => {
|
|
221
|
+
const r = undoHistory(historyRef.current, current);
|
|
222
|
+
if (r)
|
|
223
|
+
restore(r.state, 'undo', source, r.history);
|
|
224
|
+
};
|
|
225
|
+
const redo = (source) => {
|
|
226
|
+
const r = redoHistory(historyRef.current, current);
|
|
227
|
+
if (r)
|
|
228
|
+
restore(r.state, 'redo', source, r.history);
|
|
229
|
+
};
|
|
230
|
+
const reset = (source) => {
|
|
231
|
+
restore(initial, 'reset', source, pushHistory(historyRef.current, current, undoLimit));
|
|
232
|
+
engine.ui?.zoomAround(1);
|
|
233
|
+
if (zoom !== 1)
|
|
234
|
+
proposeZoom(1, source);
|
|
235
|
+
};
|
|
236
|
+
const withOps = (fn) => {
|
|
237
|
+
if (ops.current && self.current)
|
|
238
|
+
fn(ops.current, self.current);
|
|
239
|
+
else
|
|
240
|
+
void loadOps().then((o) => self.current && fn((ops.current = o), self.current));
|
|
241
|
+
};
|
|
242
|
+
const clickItem = (id, event, custom) => {
|
|
243
|
+
const source = pressSource(event);
|
|
244
|
+
let prevented = false;
|
|
245
|
+
onToolbarItemClick?.({
|
|
246
|
+
id,
|
|
247
|
+
source,
|
|
248
|
+
get defaultPrevented() {
|
|
249
|
+
return prevented;
|
|
250
|
+
},
|
|
251
|
+
preventDefault() {
|
|
252
|
+
prevented = true;
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
if (prevented)
|
|
256
|
+
return;
|
|
257
|
+
if (custom)
|
|
258
|
+
return custom();
|
|
259
|
+
if (id === 'open')
|
|
260
|
+
return pickFile({ kind: 'open' });
|
|
261
|
+
if (id === 'undo')
|
|
262
|
+
return undo(source);
|
|
263
|
+
if (id === 'redo')
|
|
264
|
+
return redo(source);
|
|
265
|
+
if (id === 'reset')
|
|
266
|
+
return reset(source);
|
|
267
|
+
// Keyboard activation of a drawing tool inserts its annotation (ops.toolbarAction); a pointer picks the tool.
|
|
268
|
+
if (TOOL_ITEMS.has(id) && !(source === 'keyboard' && DRAWING.has(id))) {
|
|
269
|
+
const next = (id === tool ? 'select' : id);
|
|
270
|
+
setTool(next, source);
|
|
271
|
+
if (next === 'image')
|
|
272
|
+
pickFile({ kind: 'image', at: null });
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
withOps((o, c) => o.toolbarAction(c, id, source));
|
|
276
|
+
};
|
|
277
|
+
const ctx = {
|
|
278
|
+
text,
|
|
279
|
+
engine,
|
|
280
|
+
current,
|
|
281
|
+
transform,
|
|
282
|
+
adjustments,
|
|
283
|
+
filter,
|
|
284
|
+
frame,
|
|
285
|
+
annotations,
|
|
286
|
+
tool,
|
|
287
|
+
selected,
|
|
288
|
+
editing,
|
|
289
|
+
natural,
|
|
290
|
+
zoom,
|
|
291
|
+
imageSmoothing,
|
|
292
|
+
cropRatios,
|
|
293
|
+
styleDefaults,
|
|
294
|
+
cropTick,
|
|
295
|
+
onBeforeExport,
|
|
296
|
+
onSave,
|
|
297
|
+
latest: () => self.current ?? ctx,
|
|
298
|
+
edit,
|
|
299
|
+
changeAnnotations,
|
|
300
|
+
select,
|
|
301
|
+
setTool,
|
|
302
|
+
setEditing,
|
|
303
|
+
setStyleDefaults: (patch) => setStyleDefaults((d) => ({ ...d, ...patch })),
|
|
304
|
+
pickFile,
|
|
305
|
+
say,
|
|
306
|
+
canvas: () => canvasRef.current,
|
|
307
|
+
};
|
|
308
|
+
const loaded = (size) => {
|
|
309
|
+
onLoad?.(freeze({ width: size.width, height: size.height }));
|
|
310
|
+
if (!openedByUser.current)
|
|
311
|
+
return say('open', `${size.width} × ${size.height}`);
|
|
312
|
+
// A file opened in the editor starts a fresh edit.
|
|
313
|
+
openedByUser.current = false;
|
|
314
|
+
setHistory(emptyHistory());
|
|
315
|
+
setInitial(FRESH);
|
|
316
|
+
setSelected(null);
|
|
317
|
+
setEditing(null);
|
|
318
|
+
commit('open', FRESH, 'pointer', 'open', undefined, false);
|
|
319
|
+
};
|
|
320
|
+
const view = (kind, value, source) => {
|
|
321
|
+
if (kind === 'zoom')
|
|
322
|
+
return proposeZoom(value, source);
|
|
323
|
+
if (kind === 'panStart') {
|
|
324
|
+
panFrom.current = { ...engine.pan };
|
|
325
|
+
return run('pan', panFrom.current, panFrom.current, source, null);
|
|
326
|
+
}
|
|
327
|
+
props.onPan?.(freeze({ operation: 'pan', previous: panFrom.current, value: { ...value }, source }));
|
|
328
|
+
say('pan');
|
|
329
|
+
return true;
|
|
330
|
+
};
|
|
331
|
+
useLayoutEffect(() => {
|
|
332
|
+
self.current = ctx;
|
|
333
|
+
viewRef.current = view;
|
|
334
|
+
historyActions.current = { undo, redo, reset, loaded };
|
|
335
|
+
});
|
|
336
|
+
// Stable actions (renderToolbar and custom items get them during render, before any effect).
|
|
337
|
+
const [actions] = useState(() => {
|
|
338
|
+
const call = (fn) => loadOps().then((o) => (self.current ? fn((ops.current = o), self.current) : null));
|
|
339
|
+
return {
|
|
340
|
+
toBlob: (options = {}) => call((o, c) => o.exportBlob(c, options)),
|
|
341
|
+
toDataURL: (options = {}) => call((o, c) => o.exportDataURL(c, options)),
|
|
342
|
+
toImageData: () => call((o, c) => o.exportImageData(c)),
|
|
343
|
+
open(next) {
|
|
344
|
+
openedByUser.current = true;
|
|
345
|
+
setOpened(next);
|
|
346
|
+
},
|
|
347
|
+
undo: () => historyActions.current?.undo('api'),
|
|
348
|
+
redo: () => historyActions.current?.redo('api'),
|
|
349
|
+
reset: () => historyActions.current?.reset('api'),
|
|
350
|
+
zoomTo(z) {
|
|
351
|
+
engine.ui?.zoomAround(z);
|
|
352
|
+
engine.ui?.commitZoom('api');
|
|
353
|
+
},
|
|
354
|
+
select: (id) => void self.current?.select(id, 'api'),
|
|
355
|
+
setTool: (t) => self.current?.setTool(t, 'api'),
|
|
356
|
+
getSize: () => (self.current?.natural ? outputSize(self.current.current.transform, self.current.natural) : null),
|
|
357
|
+
focus: () => canvasRef.current?.focus(),
|
|
358
|
+
};
|
|
359
|
+
});
|
|
360
|
+
useImperativeHandle(actionsRef, () => actions, [actions]);
|
|
361
|
+
// Engine lifecycle: attach to the canvas, load the interaction and edit chunks, then sync every commit.
|
|
362
|
+
useLayoutEffect(() => {
|
|
363
|
+
const canvas = canvasRef.current;
|
|
364
|
+
if (!canvas)
|
|
365
|
+
return;
|
|
366
|
+
engine.attach(canvas);
|
|
367
|
+
let live = true;
|
|
368
|
+
void Promise.all([loadOps(), import('./interact.js')]).then(([o, i]) => {
|
|
369
|
+
if (!live)
|
|
370
|
+
return;
|
|
371
|
+
ops.current = o;
|
|
372
|
+
engine.ui ??= new i.EngineUI(engine);
|
|
373
|
+
if (engine.state)
|
|
374
|
+
engine.ui.sync(null, engine.state);
|
|
375
|
+
// Interactive from here on (tests and styles can key on it).
|
|
376
|
+
canvas.dataset['interactive'] = '';
|
|
377
|
+
engine.invalidate();
|
|
378
|
+
});
|
|
379
|
+
return () => {
|
|
380
|
+
live = false;
|
|
381
|
+
engine.detach();
|
|
382
|
+
};
|
|
383
|
+
}, [engine]);
|
|
384
|
+
useLayoutEffect(() => {
|
|
385
|
+
engine.textBox = textRef.current;
|
|
386
|
+
engine.sync({ ...current, zoom, tool, selected, editing, smoothing: imageSmoothing, minZoom, maxZoom });
|
|
387
|
+
const fops = filterOps(adjustments, filter);
|
|
388
|
+
if (fops.length)
|
|
389
|
+
void engine.need(annotations, fops);
|
|
390
|
+
});
|
|
391
|
+
// Loading: a source change starts over; a file opened in the editor also resets the edit.
|
|
392
|
+
useEffect(() => {
|
|
393
|
+
if (!source) {
|
|
394
|
+
engine.clear();
|
|
395
|
+
setStatus('empty');
|
|
396
|
+
setNatural(null);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
let live = true;
|
|
400
|
+
setStatus('loading');
|
|
401
|
+
engine.load(source, crossOrigin).then((size) => {
|
|
402
|
+
if (!live || !size)
|
|
403
|
+
return;
|
|
404
|
+
setNatural(size);
|
|
405
|
+
setStatus('ready');
|
|
406
|
+
historyActions.current?.loaded(size);
|
|
407
|
+
}, (error) => {
|
|
408
|
+
if (!live)
|
|
409
|
+
return;
|
|
410
|
+
setStatus('error');
|
|
411
|
+
onLoadError?.(error);
|
|
412
|
+
});
|
|
413
|
+
return () => {
|
|
414
|
+
live = false;
|
|
415
|
+
};
|
|
416
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
417
|
+
}, [source, crossOrigin, engine]);
|
|
418
|
+
// The text editor takes focus when it opens.
|
|
419
|
+
useEffect(() => {
|
|
420
|
+
if (editing)
|
|
421
|
+
requestAnimationFrame(() => textRef.current?.focus());
|
|
422
|
+
}, [editing]);
|
|
423
|
+
const setCanvasRef = (el) => {
|
|
424
|
+
canvasRef.current = el;
|
|
425
|
+
if (typeof ref === 'function')
|
|
426
|
+
ref(el);
|
|
427
|
+
else if (ref)
|
|
428
|
+
ref.current = el;
|
|
429
|
+
};
|
|
430
|
+
const pointerSource = (e) => (e.pointerType === 'touch' || e.pointerType === 'pen' ? 'touch' : 'pointer');
|
|
431
|
+
// ---- toolbar ----
|
|
432
|
+
const ready = status === 'ready';
|
|
433
|
+
const renderItem = (item, i) => {
|
|
434
|
+
if (item === '|')
|
|
435
|
+
return _jsx(ToolbarSeparator, {}, `|${i}`);
|
|
436
|
+
if (typeof item === 'object') {
|
|
437
|
+
const press = (e) => clickItem(item.id, e, () => item.onClick?.(actions));
|
|
438
|
+
return (_jsx(ToolbarItem, { label: item.label, icon: item.icon, children: item.icon ? (_jsx(Button, { variant: "ghost", size: "sm", iconOnly: true, "aria-label": item.label, title: item.label, disabled: item.disabled, "data-item": item.id, onClick: press, children: item.icon })) : (_jsx(Button, { variant: "ghost", size: "sm", disabled: item.disabled, "data-item": item.id, onClick: press, children: item.label })) }, item.id));
|
|
439
|
+
}
|
|
440
|
+
if (item === 'save' && !onSave)
|
|
441
|
+
return null;
|
|
442
|
+
const label = text[item];
|
|
443
|
+
const disabled = item === 'undo' ? !history.past.length : item === 'redo' ? !history.future.length : item !== 'open' && !ready;
|
|
444
|
+
return (_jsx(ToolbarItem, { label: label, icon: itemIcon(item), children: _jsx(Button, { variant: "ghost", size: "sm", iconOnly: true, "aria-label": label, title: label, "aria-pressed": TOOL_ITEMS.has(item) ? tool === item : undefined, disabled: disabled, "data-item": item, onClick: (e) => clickItem(item, e), children: itemIcon(item) }) }, item));
|
|
445
|
+
};
|
|
446
|
+
const toolbarEl = toolbar === false ? null : (_jsx(Toolbar, { "aria-label": text.toolbar, className: cx('salmon-image-editor-toolbar', classNames?.toolbar), style: styles?.toolbar, children: toolbar.map(renderItem) }));
|
|
447
|
+
const selectedAnnotation = selected ? annotations.find((a) => a.id === selected) : undefined;
|
|
448
|
+
const panel = ready && (PANEL_TOOLS.has(tool) || !!selectedAnnotation);
|
|
449
|
+
const quick = ready && quickAccessToolbar !== false && (tool === 'crop' || (!!selectedAnnotation && !editing)) ? quickAccessToolbar || [] : [];
|
|
450
|
+
const editingText = editing ? annotations.find((a) => a.id === editing) : undefined;
|
|
451
|
+
const message = status === 'ready' ? null : text[status === 'error' ? 'loadError' : status];
|
|
452
|
+
const commitText = (value, source) => withOps((o, c) => o.commitText(c, value, source));
|
|
453
|
+
return (_jsxs("div", { ...rest, className: cx('salmon-image-editor', className), "data-status": status, "data-tool": tool, onKeyDown: (event) => {
|
|
454
|
+
onKeyDown?.(event);
|
|
455
|
+
// Mod+Z / Mod+Shift+Z / Mod+Y anywhere in the editor except its text fields.
|
|
456
|
+
const k = event.key.toLowerCase();
|
|
457
|
+
if (event.defaultPrevented || !(event.ctrlKey || event.metaKey) || (k !== 'z' && k !== 'y') || event.target.matches('input, textarea, select'))
|
|
458
|
+
return;
|
|
459
|
+
event.preventDefault();
|
|
460
|
+
if (k === 'y' || event.shiftKey)
|
|
461
|
+
redo('keyboard');
|
|
462
|
+
else
|
|
463
|
+
undo('keyboard');
|
|
464
|
+
}, children: [renderToolbar ? renderToolbar(toolbarEl, actions) : toolbarEl, _jsxs("div", { className: cx('salmon-image-editor-stage', classNames?.stage), style: styles?.stage, children: [_jsx("canvas", { ref: setCanvasRef, className: cx('salmon-image-editor-canvas', classNames?.canvas), style: styles?.canvas, role: "img", "aria-label": text.canvas, "aria-describedby": hintId, tabIndex: 0, onKeyDown: (event) => {
|
|
465
|
+
if (ready && ops.current && self.current && ops.current.canvasKey(self.current, event.nativeEvent))
|
|
466
|
+
event.preventDefault();
|
|
467
|
+
}, onPointerDown: (e) => !editing && engine.ui?.pointerDown(e.nativeEvent, pointerSource(e)), onPointerMove: (e) => {
|
|
468
|
+
const ui = engine.ui;
|
|
469
|
+
if (!ui)
|
|
470
|
+
return;
|
|
471
|
+
ui.pointerMove(e.nativeEvent);
|
|
472
|
+
if (e.pointerType === 'mouse' && !e.buttons)
|
|
473
|
+
e.currentTarget.style.cursor = ui.cursorAt({ x: e.nativeEvent.offsetX, y: e.nativeEvent.offsetY });
|
|
474
|
+
}, onPointerUp: (e) => engine.ui?.pointerUp(e.nativeEvent, pointerSource(e)), onPointerCancel: (e) => engine.ui?.pointerUp(e.nativeEvent, pointerSource(e), true), onLostPointerCapture: (e) => engine.ui?.pointerUp(e.nativeEvent, pointerSource(e), true), onDoubleClick: () => (tool === 'path' ? engine.ui?.finishPath('pointer') : selectedAnnotation?.kind === 'text' && setEditing(selectedAnnotation.id)) }), _jsx("span", { id: hintId, className: "salmon-visually-hidden", children: text.keyboardHint }), message ? (_jsx("div", { className: "salmon-image-editor-message", "data-status": status, children: message })) : null, editingText?.kind === 'text' ? (_jsx("textarea", { ref: textRef, className: "salmon-image-editor-text-input", "aria-label": text.textEditor, defaultValue: editingText.text, rows: Math.max(1, editingText.text.split('\n').length), onKeyDown: (e) => {
|
|
475
|
+
if (e.key !== 'Escape' && (e.key !== 'Enter' || e.shiftKey))
|
|
476
|
+
return;
|
|
477
|
+
e.preventDefault();
|
|
478
|
+
e.stopPropagation();
|
|
479
|
+
commitText(e.key === 'Escape' ? editingText.text : e.currentTarget.value, 'keyboard');
|
|
480
|
+
}, onBlur: (e) => commitText(e.currentTarget.value, 'pointer') })) : null, panel || quick.length ? (_jsx(Suspense, { fallback: null, children: _jsx(Panels, { c: ctx, panel: panel, quick: quick, selectedAnnotation: selectedAnnotation, classNames: classNames, styles: styles }) })) : null] }), _jsx("div", { ref: liveRef, className: "salmon-visually-hidden", ...liveRegionProps('polite') }), _jsx("input", { ref: fileRef, type: "file", accept: accept, hidden: true, tabIndex: -1, "aria-hidden": "true", onChange: (e) => {
|
|
481
|
+
const f = e.currentTarget.files?.[0];
|
|
482
|
+
if (!f)
|
|
483
|
+
return;
|
|
484
|
+
const purpose = filePurpose.current;
|
|
485
|
+
if (purpose.kind === 'open') {
|
|
486
|
+
openedByUser.current = true;
|
|
487
|
+
setOpened(f);
|
|
488
|
+
}
|
|
489
|
+
else
|
|
490
|
+
withOps((o, c) => o.addImage(c, f, purpose.at));
|
|
491
|
+
} })] }));
|
|
492
|
+
}
|
|
493
|
+
//# sourceMappingURL=ImageEditor.js.map
|