WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 1553eb6

Browse files
authored
Merge pull request #19 from collective/issue-18-default-settings
Allow overriding the default settings for an ImageEditor per project
2 parents ab5f98c + d6e4add commit 1553eb6

File tree

8 files changed

+113
-20
lines changed

8 files changed

+113
-20
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,80 @@ To install this add-on, edit the `package.json` of your policy package (usually
4141

4242
After editing, follow your project's standard process to update dependencies and rebuild.
4343

44+
## ⚙️ Configure Default Settings
45+
46+
This add-on allows you to customize the image editor's behavior by modifying default settings. All settings are optional and can be configured in your Volto project's add-on configuration.
47+
48+
### Available Settings
49+
50+
The following table describes all available settings for the image editor:
51+
52+
| Setting | Description | Type | Default Value |
53+
|---------|-------------|------|----------------|
54+
| aspectRatio | The aspect ratio constraint for cropping (width:height format) | `string` | `'16:9'` |
55+
| imageRestriction | How the image should be restricted within the crop area | `string` | `'fit-area'` |
56+
| stencilType | The shape of the crop stencil/box | `string` | `'rectangle'` |
57+
| minWidth | Minimum width for the crop area in pixels | `number` | `50` |
58+
| minHeight | Minimum height for the crop area in pixels | `number` | `50` |
59+
| maxCropWidth | Maximum width for the crop area in pixels | `undefined \| number` | `undefined` |
60+
| maxCropHeight | Maximum height for the crop area in pixels | `undefined \| number` | `undefined` |
61+
| scalable | Whether the image can be scaled/zoomed | `boolean` | `true` |
62+
| stencilGrid | Whether to display a grid overlay on the crop stencil | `boolean` | `true` |
63+
| minScale | Minimum scale factor for zooming | `number` | `0.1` |
64+
| maxScale | Maximum scale factor for zooming | `number` | `3` |
65+
66+
### Configuration Examples
67+
68+
#### Single Setting Modification
69+
70+
To modify a single setting, update `config.settings.imageEditor` in your add-on's configuration file:
71+
72+
```typescript
73+
import type { ConfigType } from '@plone/registry';
74+
75+
function applyConfig(config: ConfigType) {
76+
// Modify the default aspect ratio to 1:1
77+
config.settings.imageEditor.aspectRatio = '1:1';
78+
79+
return config;
80+
}
81+
82+
export default applyConfig;
83+
```
84+
85+
#### Multiple Settings Modification
86+
87+
If you need to modify multiple settings, we recommend using the `ImageSettings` type definition for type safety and better code documentation:
88+
89+
```typescript
90+
import type { ConfigType } from '@plone/registry';
91+
import type { ImageSettings } from '@plone-collective/volto-image-editor/types/ImageSettings';
92+
93+
function applyConfig(config: ConfigType) {
94+
// Define your custom image editor settings
95+
const imageEditorSettings: ImageSettings = {
96+
aspectRatio: '1:1',
97+
imageRestriction: 'fit-area',
98+
stencilType: 'rectangle',
99+
minWidth: 100,
100+
minHeight: 100,
101+
maxCropWidth: undefined,
102+
maxCropHeight: undefined,
103+
scalable: true,
104+
stencilGrid: true,
105+
minScale: 0.1,
106+
maxScale: 3,
107+
};
108+
109+
// Apply the custom settings
110+
config.settings.imageEditor = imageEditorSettings;
111+
112+
return config;
113+
}
114+
115+
export default applyConfig;
116+
```
117+
44118
## 🧪 Test Installation
45119

46120
Visit http://localhost:3000/ in a browser, log in, and verify that the image editor features are available in the File widget.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow overriding the default settings for an ImageEditor per project. @ericof

packages/volto-image-editor/src/components/ImageEditor/Editor/ImageEditor.tsx

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import {
88
ImageRestriction,
99
} from 'react-advanced-cropper';
1010
import type { CropperRef, CropperPreviewRef } from 'react-advanced-cropper';
11-
import { AdjustablePreviewBackground } from '../components/AdjustablePreviewBackground';
12-
import { Navigation } from '../components/Navigation/Navigation';
13-
import { AdjustableCropperBackground } from '../components/AdjustableCropperBackground';
11+
import config from '@plone/volto/registry';
1412
import { Button, Icon, Slider } from '@plone/components';
15-
import { SettingsModal } from '../components/SettingsModal/SettingsModal';
16-
import type { ImageSettings } from '../types/ImageSettings';
13+
import { AdjustablePreviewBackground } from '@plone-collective/volto-image-editor/components/ImageEditor/components/AdjustablePreviewBackground';
14+
import { Navigation } from '@plone-collective/volto-image-editor/components/ImageEditor/components/Navigation/Navigation';
15+
import { AdjustableCropperBackground } from '@plone-collective/volto-image-editor/components/ImageEditor/components/AdjustableCropperBackground';
16+
import { SettingsModal } from '@plone-collective/volto-image-editor/components/ImageEditor/components/SettingsModal/SettingsModal';
17+
import type { ImageSettings } from '@plone-collective/volto-image-editor/types/ImageSettings';
1718
import { ResetIcon } from '@plone-collective/volto-image-editor/icons/ResetIcon';
1819
import './ImageEditor.scss';
1920

@@ -36,19 +37,9 @@ const ImageEditor = ({ src, onImageSave, onCancel }) => {
3637
contrast: 0,
3738
});
3839

39-
const [imageSettings, setImageSettings] = useState<ImageSettings>({
40-
aspectRatio: 'free',
41-
imageRestriction: 'fit-area',
42-
stencilType: 'rectangle',
43-
minWidth: 50,
44-
minHeight: 50,
45-
maxCropWidth: undefined,
46-
maxCropHeight: undefined,
47-
scalable: true,
48-
stencilGrid: false,
49-
minScale: 0.1,
50-
maxScale: 3,
51-
});
40+
const defaultImageSettings = config.settings.imageEditor as ImageSettings;
41+
const [imageSettings, setImageSettings] =
42+
useState<ImageSettings>(defaultImageSettings);
5243

5344
const onAction = (action: string) => {
5445
switch (action) {

packages/volto-image-editor/src/components/ImageEditor/components/SettingsModal/SettingsModal.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Meta, StoryObj } from '@storybook/react';
22
import React from 'react';
33
import { SettingsModal } from './SettingsModal';
4-
import type { ImageSettings } from '../../types/ImageSettings';
4+
import type { ImageSettings } from '../../../../types/ImageSettings';
55

66
const meta: Meta = {
77
title: 'ImageEditor/Controls/SettingsModal',

packages/volto-image-editor/src/components/ImageEditor/components/SettingsModal/SettingsModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '@plone/components';
1111
import { Radio } from 'react-aria-components';
1212
import { SettingsIcon } from '@plone-collective/volto-image-editor/icons/SettingsIcon';
13-
import type { ImageSettings } from '../../types/ImageSettings';
13+
import type { ImageSettings } from '../../../../types/ImageSettings';
1414
import {
1515
FreeIcon,
1616
SquareIcon,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { ConfigType } from '@plone/registry';
2+
import type { ImageSettings } from '@plone-collective/volto-image-editor/types/ImageSettings';
3+
4+
export const install = (config: ConfigType) => {
5+
const imageEditorSettings: ImageSettings = {
6+
aspectRatio: '16:9',
7+
imageRestriction: 'fit-area',
8+
stencilType: 'rectangle',
9+
minWidth: 50,
10+
minHeight: 50,
11+
maxCropWidth: undefined,
12+
maxCropHeight: undefined,
13+
scalable: true,
14+
stencilGrid: true,
15+
minScale: 0.1,
16+
maxScale: 3,
17+
};
18+
19+
// Default settings for the image editor
20+
config.settings.imageEditor = imageEditorSettings;
21+
22+
return config;
23+
};
24+
25+
export default install;

packages/volto-image-editor/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { ConfigType } from '@plone/registry';
2+
import installSettings from './config/settings';
23
import { registerWidgets } from './config/widgets';
34

45
function applyConfig(config: ConfigType) {
6+
installSettings(config);
57
registerWidgets(config);
68

79
return config;

0 commit comments

Comments
 (0)