Skip to content

Commit

Permalink
Merge pull request #1 from thousandsofraccoons/fix/image-alt-text-upd…
Browse files Browse the repository at this point in the history
…ate-on-media-update

Fix/image alt text update on media update
  • Loading branch information
multipliedtwice authored Nov 14, 2022
2 parents d81c795 + 22b83f3 commit 78ec132
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
12 changes: 10 additions & 2 deletions admin/src/components/Editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,25 @@ const Editor = ({onChange, name, value, editor, disabled, settings}) => {
const [forceInsert, setForceInsert] = useState(false);
const handleToggleMediaLib = () => setMediaLibVisible(prev => !prev);

const getUpdatedImage = (asset) => ({
src: asset.url,
alt: asset.alt,
...(asset.width && {width: asset.width}),
...(asset.height && {height: asset.height}),
...(asset.url?.includes('lazy') || asset.caption === 'lazy' && { loading: 'lazy' }),
})

const handleChangeAssets = assets => {
if (!forceInsert && editor.isActive('image')) {
assets.map(asset => {
if (asset.mime.includes('image')) {
editor.chain().focus().setImage({src: asset.url}).run()
editor.chain().focus().setImage(getUpdatedImage(asset)).run()
}
})
} else {
assets.map(asset => {
if (asset.mime.includes('image')) {
editor.commands.setImage({src: asset.url, alt: asset.alt})
editor.commands.setImage(getUpdatedImage(asset))
}
});
}
Expand Down
1 change: 1 addition & 0 deletions admin/src/components/MediaLib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const MediaLib = ({ isOpen, onChange, onToggle }) => {

const handleSelectAssets = files => {
const formattedFiles = files.map(f => ({
...f,
alt: f.alternativeText || f.name,
url: prefixFileUrlWithBackendUrl(f.url),
mime: f.mime,
Expand Down
20 changes: 19 additions & 1 deletion admin/src/components/Wysiwyg/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import OrderedListExtension from "@tiptap/extension-ordered-list"
import BulletListExtension from '@tiptap/extension-bullet-list'
import ListItemExtension from '@tiptap/extension-list-item'
import GapcursorExtension from '@tiptap/extension-gapcursor'
import History from '@tiptap/extension-history'
import BlockquoteExtension from '@tiptap/extension-blockquote'
import CodeBlockExtension from '@tiptap/extension-code-block'
import DocumentExtension from '@tiptap/extension-document'
Expand Down Expand Up @@ -159,7 +160,23 @@ const WysiwygContent = ({ name, onChange, value, intlLabel, labelAction, disable
}) : null,

// Images
settings.image.enabled ? ImageExtension.configure({
settings.image.enabled ? ImageExtension.extend({
addAttributes() {
return {
...this.parent?.(),
width: { default: null },
height: { default: null },
loading: { default: null },
renderHTML: (attributes) => {
return {
width: attributes.width,
height: attributes.height,
loading: attributes.loading
};
}
};
}
}).configure({
inline: settings.image.inline,
allowBase64: settings.image.allowBase64,
}) : null,
Expand All @@ -182,6 +199,7 @@ const WysiwygContent = ({ name, onChange, value, intlLabel, labelAction, disable
settings.youtube.enabled ? YouTubeExtension.configure({
inline: false,
}) : null,
History
],
parseOptions: {
preserveWhitespace: 'full',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@tiptap/extension-gapcursor": "2.0.0-beta.199",
"@tiptap/extension-hard-break": "2.0.0-beta.199",
"@tiptap/extension-heading": "2.0.0-beta.199",
"@tiptap/extension-history": "^2.0.0-beta.202",
"@tiptap/extension-horizontal-rule": "2.0.0-beta.199",
"@tiptap/extension-image": "2.0.0-beta.199",
"@tiptap/extension-italic": "2.0.0-beta.199",
Expand Down

0 comments on commit 78ec132

Please sign in to comment.