Skip to content

Conversation

@dependenthodor
Copy link

Problem

This PR resolves #30 by adding support for alt text in the Image Tool. Currently, the Image Tool lacks the ability to add alternative text to images, which is essential for accessibility (WCAG compliance) and improves SEO.

Cause

The Image Tool was missing:

  1. An alt text field in the UI
  2. Data structure to store alt text
  3. Proper handling of alt text in the saved data
  4. Configuration options to control alt text behavior

Solution

  1. Added alt field to ImageToolData interface
  2. Added alt configuration option to FeaturesConfig
  3. Implemented UI for entering and displaying alt text
  4. Added CSS styles for the alt text input
  5. Ensured proper saving and loading of alt text
  6. Added alt text to the rendered image element
  7. Made alt text toggleable via block settings

Configuration

The alt text feature can be configured via:

image: {
  class: ImageTool,
  config: {
    features: {
      alt: true // or 'optional' or false
    }
  }
}

Copy link

@e11sy e11sy left a comment

Choose a reason for hiding this comment

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

image alt `contenteditable` should have same spacing as a caption

<div id="editorjs"></div>
<script src="https://cdn.jsdelivr.net/npm/@editorjs/editorjs@latest"></script>
<script type="module">
import ImageTool from "../src/index.ts";
Copy link

Choose a reason for hiding this comment

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

unexpected change, dev mode should not depend on built files

src/index.ts Outdated
Comment on lines 85 to 91
/**
* Alt text enabled state
* Null when user has not toggled the alt tune
* True when user has toggled the alt tune
* False when user has toggled the alt tune
*/
private isAltEnabled: boolean | null = null;
Copy link

Choose a reason for hiding this comment

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

why we need to distinguish when user has not toggled the alt tune and when user has toggled the alt tune to false?

maybe we can initialise it with false?

@dependenthodor
Copy link
Author

Is this good? @e11sy
image

Comment on lines 73 to 97
&__alt {
visibility: hidden;
position: absolute;
bottom: 0;
left: 0;
margin-bottom: 10px;

&[contentEditable="true"][data-placeholder]::before {
position: absolute !important;
content: attr(data-placeholder);
color: #707684;
font-weight: normal;
display: none;
}

&[contentEditable="true"][data-placeholder]:empty {
&::before {
display: block;
}

&:focus::before {
display: none;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

can we reuse existing input styles and do not duplicate them?

src/index.ts Outdated
tunes.push({
name: 'alt',
icon: IconText,
title: 'With alt text',
Copy link
Contributor

Choose a reason for hiding this comment

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

i18n missed

Copy link
Author

Choose a reason for hiding this comment

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

should it be applied to caption as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

yep

src/ui.ts Outdated

// Add alt attribute for IMG tags
if (tag === 'IMG') {
attributes.alt = this.nodes.alt.innerHTML || '';
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest to get textContent instead

src/index.ts Outdated
Comment on lines 262 to 271
name: this.api.i18n.t('caption'),
icon: IconText,
title: this.api.i18n.t('With caption'),
toggle: true,
});
}

if (this.config.features?.alt === 'optional') {
tunes.push({
name: this.api.i18n.t('alt'),
Copy link
Contributor

Choose a reason for hiding this comment

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

i18n is not needed for name because it is used only for key name in result JSON, not for the UI.

Leave it for title only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alt tag

3 participants