Skip to content

Commit

Permalink
fix : only disable required feature from config
Browse files Browse the repository at this point in the history
  • Loading branch information
idebenone committed Oct 27, 2024
1 parent bd0b1fd commit b5b4e5c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 31 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,12 @@ actions: [

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

You can enable/disable features such as border, background tunes and caption by adding `features` array in the configuration:
You can disable features such as border, background tunes and caption by defining `features` in the configuration:
```js
features: {
background: boolean,
border: boolean,
caption: boolean | 'optional',
stretched: boolean
border: false,
caption: 'optional',
stretched: false
}
```

Expand Down
6 changes: 3 additions & 3 deletions dev/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
byUrl: "http://localhost:8008/fetchUrl",
},
features: {
background: true,
border: true,
caption: true,
caption: "optional",
border: false,
background: false,
stretched: true,
},
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@editorjs/image",
"version": "2.10",
"version": "2.10.0",
"keywords": [
"codex editor",
"image",
Expand Down
30 changes: 12 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default class ImageTool implements BlockTool {
buttonContent: config.buttonContent,
uploader: config.uploader,
actions: config.actions,
features: config.features,
features: config.features || {},
};

/**
Expand Down Expand Up @@ -191,7 +191,7 @@ export default class ImageTool implements BlockTool {
* Renders Block content
*/
public render(): HTMLDivElement {
if (this.config.features?.caption === true || (this.config.features?.caption === 'optional' && this.data.caption)) {
if (this.config.features?.caption === true || this.config.features?.caption === undefined || (this.config.features?.caption === 'optional' && this.data.caption)) {
this.ui.applyTune('caption', true);
}

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

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

const availableTunes = tunes.filter((tune) => {
if (this.config.features) {
const featureKey = [...featureTuneMap.entries()].find(
([, value]) => value === tune.name
)?.[0];

if (featureKey != null) {
return this.config.features[featureKey as keyof FeaturesConfig];
}
const featureKey = Object.keys(featureTuneMap).find(key => featureTuneMap[key] === tune.name);

return false;
if (featureKey === 'caption') {
return this.config.features?.caption !== false;
}

return false;
return featureKey == null || this.config.features?.[featureKey as keyof FeaturesConfig] !== false;
});

return availableTunes.map(tune => ({
Expand Down
8 changes: 4 additions & 4 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@ export type FeaturesConfig = {
/**
* Flag to enable/disable tune - background.
*/
background: boolean;
background?: boolean;
/**
* Flag to enable/disable tune - border.
*/
border: boolean;
border?: boolean;
/**
* Flag to enable/disable caption.
* Can be set to 'optional' to allow users to toggle via block tunes.
*/
caption: boolean | 'optional';
caption?: boolean | 'optional';
/**
* Flag to enable/disable tune - stretched
*/
stretched: boolean;
stretched?: boolean;
};

/**
Expand Down

0 comments on commit b5b4e5c

Please sign in to comment.