@gradio/imageeditor 0.12.14 → 0.13.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/CHANGELOG.md +14 -0
- package/Example.svelte +1 -1
- package/IMAGE_EDITOR_OVERVIEW.md +224 -0
- package/ImageEditor.stories.svelte +4 -4
- package/Index.svelte +35 -46
- package/{shared/InteractiveImageEditor.svelte → InteractiveImageEditor.svelte} +56 -103
- package/dist/Example.svelte.d.ts +1 -1
- package/dist/Index.svelte +29 -33
- package/dist/Index.svelte.d.ts +17 -16
- package/dist/{shared/InteractiveImageEditor.svelte → InteractiveImageEditor.svelte} +47 -88
- package/dist/{shared/InteractiveImageEditor.svelte.d.ts → InteractiveImageEditor.svelte.d.ts} +13 -15
- package/dist/shared/Anchor.svelte +311 -0
- package/dist/shared/Anchor.svelte.d.ts +18 -0
- package/dist/shared/Controls.svelte +162 -23
- package/dist/shared/Controls.svelte.d.ts +7 -2
- package/dist/shared/{tools/IconButton.svelte → IconButton.svelte} +19 -3
- package/dist/shared/{tools/IconButton.svelte.d.ts → IconButton.svelte.d.ts} +4 -1
- package/dist/shared/ImageEditor.svelte +546 -289
- package/dist/shared/ImageEditor.svelte.d.ts +51 -56
- package/dist/shared/Layers.svelte +233 -0
- package/dist/shared/Layers.svelte.d.ts +35 -0
- package/dist/shared/Resize.svelte +218 -0
- package/dist/shared/Resize.svelte.d.ts +29 -0
- package/dist/shared/SecondaryToolbar.svelte +54 -0
- package/dist/shared/SecondaryToolbar.svelte.d.ts +35 -0
- package/dist/shared/Toolbar.svelte +337 -0
- package/dist/shared/Toolbar.svelte.d.ts +42 -0
- package/dist/shared/{tools → brush}/BrushOptions.svelte +68 -64
- package/dist/shared/{tools → brush}/BrushOptions.svelte.d.ts +9 -9
- package/dist/shared/{tools → brush}/BrushSize.svelte +2 -27
- package/dist/shared/{tools → brush}/ColorField.svelte +2 -1
- package/dist/shared/{tools → brush}/ColorField.svelte.d.ts +1 -0
- package/dist/shared/{tools → brush}/ColorPicker.svelte +81 -9
- package/dist/shared/{tools → brush}/ColorPicker.svelte.d.ts +1 -0
- package/dist/shared/{tools → brush}/ColorSwatch.svelte +78 -38
- package/dist/shared/{tools → brush}/ColorSwatch.svelte.d.ts +6 -2
- package/dist/shared/brush/brush-cursor.d.ts +101 -0
- package/dist/shared/brush/brush-cursor.js +327 -0
- package/dist/shared/brush/brush-textures.d.ts +71 -0
- package/dist/shared/brush/brush-textures.js +404 -0
- package/dist/shared/brush/brush-utils.d.ts +32 -0
- package/dist/shared/brush/brush-utils.js +59 -0
- package/dist/shared/brush/brush.d.ts +152 -0
- package/dist/shared/brush/brush.js +396 -0
- package/dist/shared/brush/types.d.ts +37 -0
- package/dist/shared/brush/types.js +1 -0
- package/dist/shared/core/editor.d.ts +252 -0
- package/dist/shared/core/editor.js +1221 -0
- package/dist/shared/crop/crop.d.ts +201 -0
- package/dist/shared/crop/crop.js +727 -0
- package/dist/shared/image/image.d.ts +89 -0
- package/dist/shared/image/image.js +185 -0
- package/dist/shared/resize/resize.d.ts +270 -0
- package/dist/shared/resize/resize.js +1413 -0
- package/dist/shared/types.d.ts +12 -0
- package/dist/shared/utils/pixi.d.ts +16 -57
- package/dist/shared/utils/pixi.js +15 -113
- package/dist/shared/zoom/zoom.d.ts +136 -0
- package/dist/shared/zoom/zoom.js +548 -0
- package/package.json +10 -9
- package/shared/Anchor.svelte +384 -0
- package/shared/Controls.svelte +194 -27
- package/shared/{tools/IconButton.svelte → IconButton.svelte} +21 -4
- package/shared/ImageEditor.svelte +682 -349
- package/shared/Layers.svelte +249 -0
- package/shared/Resize.svelte +239 -0
- package/shared/SecondaryToolbar.svelte +61 -0
- package/shared/Toolbar.svelte +386 -0
- package/shared/brush/BRUSH_TOOL.md +134 -0
- package/shared/brush/BrushOptions.svelte +200 -0
- package/shared/{tools → brush}/BrushSize.svelte +2 -30
- package/shared/{tools → brush}/ColorField.svelte +3 -1
- package/shared/{tools → brush}/ColorPicker.svelte +86 -9
- package/shared/{tools → brush}/ColorSwatch.svelte +95 -42
- package/shared/brush/brush-cursor.ts +408 -0
- package/shared/brush/brush-textures.ts +508 -0
- package/shared/brush/brush-utils.ts +76 -0
- package/shared/brush/brush.ts +506 -0
- package/shared/brush/types.ts +41 -0
- package/shared/core/EDITOR.md +182 -0
- package/shared/core/editor.ts +1730 -0
- package/shared/crop/CROP.md +139 -0
- package/shared/crop/crop.ts +1027 -0
- package/shared/image/IMAGE.md +135 -0
- package/shared/image/image.ts +300 -0
- package/shared/resize/resize.ts +1894 -0
- package/shared/types.ts +14 -0
- package/shared/utils/pixi.ts +19 -246
- package/shared/zoom/zoom.ts +729 -0
- package/dist/shared/layers/Layers.svelte +0 -231
- package/dist/shared/layers/Layers.svelte.d.ts +0 -18
- package/dist/shared/layers/utils.d.ts +0 -73
- package/dist/shared/layers/utils.js +0 -156
- package/dist/shared/tools/Brush.svelte +0 -215
- package/dist/shared/tools/Brush.svelte.d.ts +0 -29
- package/dist/shared/tools/Crop.svelte +0 -155
- package/dist/shared/tools/Crop.svelte.d.ts +0 -16
- package/dist/shared/tools/Cropper.svelte +0 -271
- package/dist/shared/tools/Cropper.svelte.d.ts +0 -41
- package/dist/shared/tools/Handle.svelte +0 -260
- package/dist/shared/tools/Handle.svelte.d.ts +0 -29
- package/dist/shared/tools/Sources.svelte +0 -257
- package/dist/shared/tools/Sources.svelte.d.ts +0 -34
- package/dist/shared/tools/Tools.svelte +0 -102
- package/dist/shared/tools/Tools.svelte.d.ts +0 -48
- package/dist/shared/tools/brush.d.ts +0 -62
- package/dist/shared/tools/brush.js +0 -133
- package/dist/shared/tools/crop.d.ts +0 -15
- package/dist/shared/tools/crop.js +0 -154
- package/dist/shared/tools/index.d.ts +0 -5
- package/dist/shared/tools/index.js +0 -5
- package/dist/shared/tools/sources.d.ts +0 -46
- package/dist/shared/tools/sources.js +0 -121
- package/dist/shared/tools/types.d.ts +0 -1
- package/shared/layers/Layers.svelte +0 -250
- package/shared/layers/utils.ts +0 -294
- package/shared/tools/Brush.svelte +0 -280
- package/shared/tools/BrushOptions.svelte +0 -180
- package/shared/tools/Crop.svelte +0 -189
- package/shared/tools/Cropper.svelte +0 -346
- package/shared/tools/Handle.svelte +0 -289
- package/shared/tools/Sources.svelte +0 -283
- package/shared/tools/Tools.svelte +0 -162
- package/shared/tools/brush.ts +0 -247
- package/shared/tools/crop.ts +0 -213
- package/shared/tools/index.ts +0 -5
- package/shared/tools/sources.ts +0 -195
- package/shared/tools/types.ts +0 -1
- /package/dist/shared/{tools → brush}/BrushSize.svelte.d.ts +0 -0
- /package/dist/shared/{tools/types.js → types.js} +0 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# Core Editor Documentation
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The Core Editor is the central component of the image editor that manages the canvas, tools, layers, and user interactions. It provides a flexible architecture for integrating various tools and maintaining the state of the editor. This document explains how the core editor works and the relationships between its components.
|
|
6
|
+
|
|
7
|
+
## Key Files
|
|
8
|
+
|
|
9
|
+
- `js/imageeditor/shared/core/editor.ts`: Main implementation of the editor
|
|
10
|
+
- `js/imageeditor/shared/Toolbar.svelte`: Defines tool types and handles tool selection
|
|
11
|
+
- `js/imageeditor/shared/ImageEditor.svelte`: Main Svelte component that integrates the editor
|
|
12
|
+
|
|
13
|
+
## Architecture
|
|
14
|
+
|
|
15
|
+
The image editor is built around several key classes that work together:
|
|
16
|
+
|
|
17
|
+
1. **ImageEditor**: The main class that initializes and manages the editor
|
|
18
|
+
2. **CommandManager**: Handles undo/redo functionality
|
|
19
|
+
3. **LayerManager**: Manages layers and their textures
|
|
20
|
+
4. **EditorState**: Maintains the editor's state and notifies subscribers of changes
|
|
21
|
+
5. **Tool Interface**: Defines the contract for all tools to implement
|
|
22
|
+
|
|
23
|
+
### Class Structure
|
|
24
|
+
|
|
25
|
+
#### ImageEditor
|
|
26
|
+
|
|
27
|
+
The `ImageEditor` class is the main entry point and provides the following functionality:
|
|
28
|
+
|
|
29
|
+
1. **Initialization**: Sets up the PIXI.js application, containers, and initial state
|
|
30
|
+
2. **Tool Management**: Registers and manages tools
|
|
31
|
+
3. **Layer Management**: Creates and manages layers through the LayerManager
|
|
32
|
+
4. **Command Execution**: Executes commands and manages undo/redo through the CommandManager
|
|
33
|
+
5. **State Management**: Maintains and updates the editor's state
|
|
34
|
+
6. **Rendering**: Handles the rendering loop and updates
|
|
35
|
+
|
|
36
|
+
#### CommandManager
|
|
37
|
+
|
|
38
|
+
The `CommandManager` class implements the Command pattern to support undo/redo functionality:
|
|
39
|
+
|
|
40
|
+
1. **Command Execution**: Executes commands and adds them to the undo stack
|
|
41
|
+
2. **Undo**: Reverts the most recent command and moves it to the redo stack
|
|
42
|
+
3. **Redo**: Re-executes a previously undone command and moves it back to the undo stack
|
|
43
|
+
|
|
44
|
+
#### LayerManager
|
|
45
|
+
|
|
46
|
+
The `LayerManager` class manages the layers in the editor:
|
|
47
|
+
|
|
48
|
+
1. **Layer Creation**: Creates new layers with associated textures
|
|
49
|
+
2. **Layer Deletion**: Removes layers and cleans up resources
|
|
50
|
+
3. **Layer Order**: Manages the z-index ordering of layers
|
|
51
|
+
4. **Active Layer**: Tracks and sets the currently active layer
|
|
52
|
+
5. **Background Layer**: Special handling for the background layer
|
|
53
|
+
|
|
54
|
+
#### EditorState
|
|
55
|
+
|
|
56
|
+
The `EditorState` class maintains the state of the editor and notifies subscribers of changes:
|
|
57
|
+
|
|
58
|
+
1. **State Properties**: Maintains scale, position, and tool information
|
|
59
|
+
2. **Subscription**: Allows components to subscribe to state changes
|
|
60
|
+
3. **Notification**: Notifies subscribers when state changes occur
|
|
61
|
+
|
|
62
|
+
### Tool Interface
|
|
63
|
+
|
|
64
|
+
The `Tool` interface defines the contract that all tools must implement:
|
|
65
|
+
|
|
66
|
+
1. **setup**: Initializes the tool with the editor context
|
|
67
|
+
2. **cleanup**: Cleans up resources when the tool is deactivated
|
|
68
|
+
3. **set_tool**: Updates the tool's state when the active tool changes
|
|
69
|
+
|
|
70
|
+
## Rendering Pipeline
|
|
71
|
+
|
|
72
|
+
The editor uses PIXI.js for rendering and manages several containers:
|
|
73
|
+
|
|
74
|
+
1. **image_container**: Contains the layers and their content
|
|
75
|
+
2. **ui_container**: Contains UI elements that overlay the canvas
|
|
76
|
+
3. **outline_container**: Contains the outline around the canvas
|
|
77
|
+
|
|
78
|
+
The rendering pipeline follows these steps:
|
|
79
|
+
|
|
80
|
+
1. **Layer Rendering**: Each layer renders its content to a texture
|
|
81
|
+
2. **Container Composition**: Layers are composed in the image container
|
|
82
|
+
3. **UI Overlay**: UI elements are rendered on top of the image
|
|
83
|
+
4. **Outline Drawing**: The canvas outline is drawn around the image
|
|
84
|
+
5. **Scale and Position**: The image container is scaled and positioned based on user interactions
|
|
85
|
+
|
|
86
|
+
## State Management
|
|
87
|
+
|
|
88
|
+
The editor uses Svelte's spring store for smooth animations of state changes:
|
|
89
|
+
|
|
90
|
+
1. **dimensions**: Tracks the width and height of the canvas
|
|
91
|
+
2. **scale**: Tracks the zoom level of the canvas
|
|
92
|
+
3. **position**: Tracks the position of the canvas in the viewport
|
|
93
|
+
|
|
94
|
+
These stores are used to animate transitions when the user interacts with the canvas.
|
|
95
|
+
|
|
96
|
+
## Command Pattern
|
|
97
|
+
|
|
98
|
+
The editor implements the Command pattern for undo/redo functionality:
|
|
99
|
+
|
|
100
|
+
1. **Command Interface**: Defines execute and undo methods
|
|
101
|
+
2. **Command Execution**: Commands are executed and added to the undo stack
|
|
102
|
+
3. **Undo/Redo**: Commands can be undone and redone
|
|
103
|
+
|
|
104
|
+
This pattern allows for complex operations to be encapsulated and reversed.
|
|
105
|
+
|
|
106
|
+
## Layer Management
|
|
107
|
+
|
|
108
|
+
The editor supports multiple layers with the following features:
|
|
109
|
+
|
|
110
|
+
1. **Layer Creation**: New layers can be created with associated textures
|
|
111
|
+
2. **Layer Deletion**: Layers can be deleted, cleaning up associated resources
|
|
112
|
+
3. **Layer Order**: Layers can be reordered to change their z-index
|
|
113
|
+
4. **Active Layer**: One layer is designated as the active layer for editing
|
|
114
|
+
5. **Background Layer**: A special layer can be designated as the background
|
|
115
|
+
|
|
116
|
+
## Tool Integration
|
|
117
|
+
|
|
118
|
+
Tools are integrated with the editor through the Tool interface:
|
|
119
|
+
|
|
120
|
+
1. **Registration**: Tools are registered with the editor during initialization
|
|
121
|
+
2. **Context Access**: Tools receive the editor context during setup
|
|
122
|
+
3. **Lifecycle Management**: Tools are set up and cleaned up as needed
|
|
123
|
+
4. **Event Handling**: Tools can handle events from the editor
|
|
124
|
+
|
|
125
|
+
## Event Handling
|
|
126
|
+
|
|
127
|
+
The editor handles various events:
|
|
128
|
+
|
|
129
|
+
1. **Resize**: Responds to changes in the container size
|
|
130
|
+
2. **Tool Selection**: Updates the active tool when the user selects a new tool
|
|
131
|
+
3. **Command Execution**: Executes commands when triggered by tools
|
|
132
|
+
4. **Animation**: Animates state changes using springs
|
|
133
|
+
|
|
134
|
+
## Integration with Svelte
|
|
135
|
+
|
|
136
|
+
The editor is designed to work with Svelte:
|
|
137
|
+
|
|
138
|
+
1. **Stores**: Uses Svelte stores for reactive state management
|
|
139
|
+
2. **Springs**: Uses Svelte springs for smooth animations
|
|
140
|
+
3. **Component Integration**: Can be integrated with Svelte components
|
|
141
|
+
|
|
142
|
+
## Performance Considerations
|
|
143
|
+
|
|
144
|
+
The editor uses several techniques to maintain performance:
|
|
145
|
+
|
|
146
|
+
1. **Texture Management**: Efficiently manages textures to minimize memory usage
|
|
147
|
+
2. **Layer Composition**: Composes layers efficiently to minimize rendering overhead
|
|
148
|
+
3. **Event Throttling**: Throttles events to avoid excessive updates
|
|
149
|
+
4. **Resolution Scaling**: Adjusts resolution based on device pixel ratio
|
|
150
|
+
|
|
151
|
+
## Customization API
|
|
152
|
+
|
|
153
|
+
The editor exposes several methods for customization:
|
|
154
|
+
|
|
155
|
+
- `set_image_properties`: Updates the canvas dimensions, scale, and position
|
|
156
|
+
- `execute_command`: Executes a command and adds it to the undo stack
|
|
157
|
+
- `undo`: Undoes the most recent command
|
|
158
|
+
- `redo`: Redoes a previously undone command
|
|
159
|
+
- `add_image`: Adds an image to the canvas
|
|
160
|
+
- `set_tool`: Sets the active tool
|
|
161
|
+
- `set_subtool`: Sets the active subtool
|
|
162
|
+
- `set_background_image`: Sets the background image
|
|
163
|
+
|
|
164
|
+
## Maintenance Notes
|
|
165
|
+
|
|
166
|
+
When modifying the editor, consider the following:
|
|
167
|
+
|
|
168
|
+
1. **Resource Cleanup**: Always clean up resources to prevent memory leaks
|
|
169
|
+
2. **Event Listener Management**: Properly add and remove event listeners
|
|
170
|
+
3. **State Updates**: Update state through the appropriate methods to ensure proper notification
|
|
171
|
+
4. **Command Pattern**: Use the Command pattern for operations that should be undoable
|
|
172
|
+
5. **Layer Management**: Properly manage layers and their resources
|
|
173
|
+
|
|
174
|
+
## Future Improvements
|
|
175
|
+
|
|
176
|
+
Potential areas for enhancement:
|
|
177
|
+
|
|
178
|
+
1. **Performance Optimization**: Further optimize rendering for large canvases
|
|
179
|
+
2. **Tool Extensions**: Add support for more tools and tool options
|
|
180
|
+
3. **Layer Effects**: Add support for layer effects and blending modes
|
|
181
|
+
4. **Selection Tools**: Enhance selection tools and operations
|
|
182
|
+
5. **Export Options**: Add more export options and formats
|