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

TagClasses doesn't work with IMG tags (and proposed fixes) #1504

Open
AlexGhiondea opened this issue Feb 21, 2025 · 0 comments
Open

TagClasses doesn't work with IMG tags (and proposed fixes) #1504

AlexGhiondea opened this issue Feb 21, 2025 · 0 comments

Comments

@AlexGhiondea
Copy link

When trying to use the tagClasses feature to add a custom tag to the added images (both import via url and base64) the tag does not apply.

I have a workaround for that that involves applying the custom class at the time of element creation instead of later on.

The reason why it doesn't work the way it is right now is because the parameter that is used to try and find the matching tag in the tagClasses object is either the url or the base64 encoded value.

Url Import

t.execCmd('insertImage', v.url, false, true);

Base64 Import

trumbowyg.execCmd('insertImage', fReader.result, false, true);

Url Image Import Fix

To fix the url image import we can add the following code at this line:

$img.attr('alt', v.alt);

From:

var $img = $('img[src="' + v.url + '"]:not([alt])', t.$box);
$img.attr('alt', v.alt);

To:

var $img = $('img[src="' + v.url + '"]:not([alt])', t.$box);
$img.attr('alt', v.alt);

let imgClass = t.o.tagClasses['img'];
$img.addClass(imgClass);

This will ensure that the tag is applied immediately after the alt tag is added.

Base64 image upload fix

To fix the base64 we have to do something a bit more complicated here:

$(['img[src="', fReader.result, '"]:not([alt])'].join(''), trumbowyg.$box).attr('alt', values.alt);

I recommend slightly changing the existing code to only find one image (since we are only uploading one)

From:

$(['img[src="', fReader.result, '"]:not([alt])'].join(''), trumbowyg.$box).attr('alt', values.alt);

To:

let imageObj = $('img[src="', fReader.result, '"]:not([alt])', trumbowyg.$box);
imageObj.attr('alt', values.alt);

// Get the tag (if specified)
var imgClass = trumbowyg.o.tagClasses['img'];
imageObj.addClass(imgClass);

// We also have to sync the code or this change will be lost
trumbowyg.syncCode();
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

No branches or pull requests

1 participant