Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve caption state handling and CSS issues #278

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's redundant property since we already have "caption" field that can be empty.

Developer can disable caption for all image blocks using tool config. It should not affect output data

| 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
29 changes: 24 additions & 5 deletions dev/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,43 @@
</head>
<body>
<div id="editorjs"></div>
<script src="https://cdn.jsdelivr.net/npm/@editorjs/editorjs@latest/dist/editor.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@editorjs/editorjs@latest"></script>
<script type="module">
import ImageTool from "../src/index.ts"
import ImageTool from "../src/index.ts";
const editor = new EditorJS({
holder: "editorjs",
data: {
time: 1700475383740,
blocks: [
{
id: "hZAjSnqYMX",
type: "image",
data: {
file: {
url: "https://images.unsplash.com/photo-1607604276583-eef5d076aa5f?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
},
withBorder: false,
withBackground: false,
stretched: false,
caption: "kimitsu no yayiba",
},
},
],
},

tools: {
code: {
image: {
class: ImageTool,
config: {
endpoints: {
byFile: "http://localhost:8008/uploadFile",
byUrl: "http://localhost:8008/fetchUrl",
},
features: {
// caption: false,
caption: "optional",
border: false,
background: false,
stretch: false,
stretch: 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.1",
"version": "2.10.2",
"keywords": [
"codex editor",
"image",
Expand Down
19 changes: 11 additions & 8 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
border-radius: 3px;
overflow: hidden;
margin-bottom: 10px;
padding-bottom: 0;

&-picture {
max-width: 100%;
Expand Down Expand Up @@ -44,7 +45,11 @@
}

&__caption {
display: none;
visibility: hidden;
position: absolute;
bottom: 0;
left: 0;
margin-bottom: 10px;

&[contentEditable="true"][data-placeholder]::before {
position: absolute !important;
Expand Down Expand Up @@ -74,7 +79,7 @@
&--empty,
&--uploading {
^&__caption {
display: none !important;
visibility: hidden !important;
}
}

Expand Down Expand Up @@ -112,10 +117,6 @@
display: none;
}
}

.cdx-button {
display: none;
}
}

/**
Expand Down Expand Up @@ -149,10 +150,12 @@
}
}

&--caption {
&--withCaption {
^&__caption {
display: block;
visibility: visible;
}

padding-bottom: 50px
}
}

Expand Down
19 changes: 13 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 @@ -367,6 +368,10 @@ export default class ImageTool implements BlockTool {

this.setTune(tune as keyof ImageToolData, value);
});

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

/**
Expand Down Expand Up @@ -419,11 +424,13 @@ export default class ImageTool implements BlockTool {
* @param tuneName - tune that has been clicked
*/
private tuneToggled(tuneName: keyof ImageToolData): void {
// inverse tune state
this.setTune(tuneName, !(this._data[tuneName] as boolean));
// check the tune state
const currentState = this._data[tuneName] as boolean;

this.setTune(tuneName, !currentState);

// reset caption on toggle
if (tuneName === 'caption' && !this._data[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
Loading