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 1 commit
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
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
15 changes: 9 additions & 6 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 @@ -112,10 +117,6 @@
display: none;
}
}

.cdx-button {
display: none;
}
}

/**
Expand Down Expand Up @@ -151,8 +152,10 @@

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

padding-bottom: 50px
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ export default class ImageTool implements BlockTool {

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

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

/**
Expand Down Expand Up @@ -419,11 +423,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.ui.isTuneActive(tuneName);

this.setTune(tuneName, !currentState);

// reset caption on toggle
if (tuneName === 'caption' && !this._data[tuneName]) {
if (tuneName === 'caption' && !this.ui.isTuneActive(tuneName)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why we depend on style instead of actual data state?

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason why I considered style because the way caption in features needs to be interpreted as boolean while its a string, leading to the inconsistency in maintaining the tune state (On successive enable/disable while resetting the caption). This doesn't feel like a a permanent fix.

So I took a step back and thought of introducing a withCaption field which is of type boolean like other tune values which are very consistent with tune state.

I also tried to turn the field caption into an object:

{
    state: boolean,
    value: string,
}

But introducing a complex structure compared to a straight forward boolean seems unnecessary.

this._data.caption = '';
this.ui.fillCaption('');
}
Expand Down
9 changes: 9 additions & 0 deletions src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ 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
Loading