Skip to content

Commit

Permalink
fix: resolve caption bug with alternative solution
Browse files Browse the repository at this point in the history
  • Loading branch information
idebenone committed Dec 16, 2024
1 parent 58f111f commit e131897
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ This Tool returns `data` with following format
| -------------- | --------- | ------------------------------- |
| file | `object` | Uploaded file data. Any data got from backend uploader. Always contain the `url` property |
| caption | `string` | image's caption |
| withCaption | `boolean` | add caption to image |
| withBorder | `boolean` | add border to image |
| withBackground | `boolean` | need to add background |
| stretched | `boolean` | stretch image to screen's width |
Expand All @@ -148,6 +149,7 @@ This Tool returns `data` with following format
"url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg"
},
"caption" : "Roadster // tesla.com",
"withCaption": true,
"withBorder" : false,
"withBackground" : false,
"stretched" : true,
Expand Down
4 changes: 2 additions & 2 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
&--empty,
&--uploading {
^&__caption {
display: none !important;
visibility: hidden !important;
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@
}
}

&--caption {
&--withCaption {
^&__caption {
visibility: visible;
}
Expand Down
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export default class ImageTool implements BlockTool {
*/
this._data = {
caption: '',
withCaption: false,
withBorder: false,
withBackground: false,
stretched: false,
Expand Down Expand Up @@ -192,7 +193,7 @@ export default class ImageTool implements BlockTool {
*/
public render(): HTMLDivElement {
if (this.config.features?.caption === true || this.config.features?.caption === undefined || (this.config.features?.caption === 'optional' && this.data.caption)) {
this.ui.applyTune('caption', true);
this.ui.applyTune('withCaption', true);
}

return this.ui.render(this.data) as HTMLDivElement;
Expand Down Expand Up @@ -230,12 +231,12 @@ export default class ImageTool implements BlockTool {
border: 'withBorder',
background: 'withBackground',
stretch: 'stretched',
caption: 'caption',
caption: 'withCaption',
};

if (this.config.features?.caption === 'optional') {
tunes.push({
name: 'caption',
name: 'withCaption',
icon: IconText,
title: 'With caption',
toggle: true,
Expand Down Expand Up @@ -369,7 +370,7 @@ export default class ImageTool implements BlockTool {
});

if (data.caption) {
this.setTune('caption', true);
this.setTune('withCaption', true);
}
}

Expand Down Expand Up @@ -424,12 +425,12 @@ export default class ImageTool implements BlockTool {
*/
private tuneToggled(tuneName: keyof ImageToolData): void {
// check the tune state
const currentState = this.ui.isTuneActive(tuneName);
const currentState = this._data[tuneName] as boolean;

this.setTune(tuneName, !currentState);

// reset caption on toggle
if (tuneName === 'caption' && !this.ui.isTuneActive(tuneName)) {
if (tuneName === 'caption') {
this._data.caption = '';
this.ui.fillCaption('');
}
Expand Down
5 changes: 5 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export type ImageToolData<Actions = {}, AdditionalFileData = {}> = {
*/
caption: string;

/**
* Flag indicating whether the image has a caption.
*/
withCaption: boolean;

/**
* Flag indicating whether the image has a border.
*/
Expand Down
9 changes: 0 additions & 9 deletions src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,6 @@ export default class Ui {
this.nodes.wrapper.classList.toggle(`${this.CSS.wrapper}--${tuneName}`, status);
}

/**
* Utility to determine if a tune is currently active
* @param tuneName - one of available tunes {@link Tunes.tunes}
* @returns - true for active and vice versa
*/
public isTuneActive(tuneName: string): boolean {
return this.nodes.wrapper.classList.contains(`${this.CSS.wrapper}--${tuneName}`);
}

/**
* Renders tool UI
* @param toolData - saved tool data
Expand Down

0 comments on commit e131897

Please sign in to comment.