Skip to content

Commit b5b4e5c

Browse files
committed
fix : only disable required feature from config
1 parent bd0b1fd commit b5b4e5c

File tree

5 files changed

+24
-31
lines changed

5 files changed

+24
-31
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,12 @@ actions: [
116116

117117
**_NOTE:_** return value of `action` callback for settings whether action button should be toggled or not is *deprecated*. Consider using `toggle` option instead.
118118

119-
You can enable/disable features such as border, background tunes and caption by adding `features` array in the configuration:
119+
You can disable features such as border, background tunes and caption by defining `features` in the configuration:
120120
```js
121121
features: {
122-
background: boolean,
123-
border: boolean,
124-
caption: boolean | 'optional',
125-
stretched: boolean
122+
border: false,
123+
caption: 'optional',
124+
stretched: false
126125
}
127126
```
128127

dev/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
byUrl: "http://localhost:8008/fetchUrl",
2222
},
2323
features: {
24-
background: true,
25-
border: true,
26-
caption: true,
24+
caption: "optional",
25+
border: false,
26+
background: false,
2727
stretched: true,
2828
},
2929
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/image",
3-
"version": "2.10",
3+
"version": "2.10.0",
44
"keywords": [
55
"codex editor",
66
"image",

src/index.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default class ImageTool implements BlockTool {
9999
buttonContent: config.buttonContent,
100100
uploader: config.uploader,
101101
actions: config.actions,
102-
features: config.features,
102+
features: config.features || {},
103103
};
104104

105105
/**
@@ -191,7 +191,7 @@ export default class ImageTool implements BlockTool {
191191
* Renders Block content
192192
*/
193193
public render(): HTMLDivElement {
194-
if (this.config.features?.caption === true || (this.config.features?.caption === 'optional' && this.data.caption)) {
194+
if (this.config.features?.caption === true || this.config.features?.caption === undefined || (this.config.features?.caption === 'optional' && this.data.caption)) {
195195
this.ui.applyTune('caption', true);
196196
}
197197

@@ -226,12 +226,12 @@ export default class ImageTool implements BlockTool {
226226
// Merge default tunes with the ones that might be added by user
227227
// @see https://github.com/editor-js/image/pull/49
228228
const tunes = ImageTool.tunes.concat(this.config.actions || []);
229-
const featureTuneMap = new Map<string, string>([
230-
['border', 'withBorder'],
231-
['background', 'withBackground'],
232-
['stretched', 'stretched'],
233-
['caption', 'caption'],
234-
]);
229+
const featureTuneMap: Record<string, string> = {
230+
border: 'withBorder',
231+
background: 'withBackground',
232+
stretched: 'stretched',
233+
caption: 'caption',
234+
};
235235

236236
if (this.config.features?.caption === 'optional') {
237237
tunes.push({
@@ -243,19 +243,13 @@ export default class ImageTool implements BlockTool {
243243
}
244244

245245
const availableTunes = tunes.filter((tune) => {
246-
if (this.config.features) {
247-
const featureKey = [...featureTuneMap.entries()].find(
248-
([, value]) => value === tune.name
249-
)?.[0];
250-
251-
if (featureKey != null) {
252-
return this.config.features[featureKey as keyof FeaturesConfig];
253-
}
246+
const featureKey = Object.keys(featureTuneMap).find(key => featureTuneMap[key] === tune.name);
254247

255-
return false;
248+
if (featureKey === 'caption') {
249+
return this.config.features?.caption !== false;
256250
}
257251

258-
return false;
252+
return featureKey == null || this.config.features?.[featureKey as keyof FeaturesConfig] !== false;
259253
});
260254

261255
return availableTunes.map(tune => ({

src/types/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,20 @@ export type FeaturesConfig = {
107107
/**
108108
* Flag to enable/disable tune - background.
109109
*/
110-
background: boolean;
110+
background?: boolean;
111111
/**
112112
* Flag to enable/disable tune - border.
113113
*/
114-
border: boolean;
114+
border?: boolean;
115115
/**
116116
* Flag to enable/disable caption.
117117
* Can be set to 'optional' to allow users to toggle via block tunes.
118118
*/
119-
caption: boolean | 'optional';
119+
caption?: boolean | 'optional';
120120
/**
121121
* Flag to enable/disable tune - stretched
122122
*/
123-
stretched: boolean;
123+
stretched?: boolean;
124124
};
125125

126126
/**

0 commit comments

Comments
 (0)