Customize image html or attributes in RichContentRenderer
Hey!
I'm basically trying to lazy-load images in RichEditor content.
I tried extending
RichContentRenderer
and also with "mixin" (like awcodes show me), but I'm not sure if it's even possible...
Don't have I any other way other than writing my own node? I don't want to lose the features included in the default Image node...40 Replies
Might be better to add the lazy load attribute to the Core Image extension with a PR.
sure
so you think it's possible? I hope it's not blocked in tiptap-php Image Node
We extend the Tiptap extension. Same thing I did in my plugin.
of course... I need to understand why I can't see my custom attribute
I believe we should be able to pass any attribute to any node, or wrap in any html... because there are a million different use cases
don't you agree?
if only prosemirror worked that way
mmh... but there is an
addAttributes()
method in ImageExtension.php
, so it should be possible to have an API around this, as well in some others Extensions, right?it's hard to make it global though, because the attributes need to be specific to the node or mark so they are processed correctly
GitHub
Add a method to lazy load RichEditor images by CharlieEtienne · Pu...
Description
This PR adds a new lazyLoadImages method to RichContentRenderer.
use Filament\Forms\Components\RichEditor\RichContentRenderer;
RichContentRenderer::make($record->content)
-&...
Looks good. I would be hesitant to do it as an all or nothing though since images that appear above the fold should not be set to lazy load according to the html specs. 🤔
I agree, but what are the alternatives?
Add it as an attribute to the image extension and add a toggle in the modal action to set it.
Unfortunately there’s a bug at the moment with the merging in core, so you can’t override from an external extension. Working on that.
there’s a bug at the moment with the merging in coreWhat do you mean? When I dump attributes here, I have my attributes. But on the next step, they disappear and are not merged. They are not passed in
vendor/ueberdosis/tiptap-php/src/Core/DOMSerializer.php
Is this what you're talking about?Sorry, maybe I’m lost. What do you mean by 'next step'?
The bug i'm referring to is on the JS side
oh ok.
I'm working on a PR suggested by Dan to be able to do that:
https://github.com/filamentphp/filament/pull/17526
But I can't find a clean way to pass node attributes to extensions, without having to modify each extension
the attributes should be handled by their own extension
not by the renderer. again i may be misunderstanding
ie, extension-image.js and ImageExtension.php should handle the lazy loading attribute
then the renderer will automatically handle it
on my first PR, Dan told the following:
We could add endless methods to customize the output like this, so maybe we just need a method where you can tap into the editorhttps://github.com/filamentphp/filament/pull/17507#issuecomment-3221738582 So I'm trying to do that. But I'm completely lost I think 🙂
ok. maybe we're talking about 2 different things. lol
i see what he's thinking, but that's not quite the same as being able to set these kind of things in the editor js instance, like having some images lazy and other not.
To clarify, I was trying to add any attribute to any node.
yeah, that's another topic, of course
i'm also not 100% sure Tiptap-PHP allows for setting arbitrary attrs on the object.
ie, the attrs are read from the data
it's possible. For example, if I do that:
it's showing in
toUnsafeHtml()
It works also when adding some attributes here :
And also from extension, if I add the attributes keys in addAttributes
method.Could it just be a rule on the sanitizer stripping it from the html?
of course, it's striped from html thanks to the sanitizer. But not loading="lazy". That's not the problem.
My goal at the moment is to be able to set any dynamic attribute to a node, and pass it to the extension to have it in HTML.
(maybe that's a bad idea though 🙂 )
looks like 'lazy' isn't an allowed attribute in the sanitizer's 'allowSafeElements()', try adding this to the sanitizer config in the
support/src/SupportServiceProvider.php
no I'm sure it's allowed
ok
it works when I add it in the ImageExtension
let me play around with it. you're PR code looks ok to me.
wonder if it could be a conflict with the processFileAttachments method since it's also acting against images
wouldn't think it would be a problem though
I don't think so because it doesn't work if I do that:
ok, i think the problem is that if an attribute doesn't exist in the php version of the extension then it's just going to get stripped out regardless. It only works with this in ImageExtension.php:

yes, exactly
technically this would be the code. sorry:
but, yea, i don't know that there's anything we can do on our side since that logic is handle inside the tiptap php package
it's trying to mimic the tiptap js, which requires the attributes to be specifically defined due to prosemirror's restrictions.
but we can pass options to extensions like that:
options and attributes aren't the same thing.
in options, I believe if you have an array of attributes in 'HTMLAttributes' it works
you could do it that way, but i would expect that to force the image to always have the loading attribute, the attribute would still need to be defined in the getAttributes(), for the merging to work that way.
sure, it will force it...
basically, though, i guess what i'm getting at, is that in tiptap php any attribute that can be dynamically set has to be registered as an attribute explicitly.
yeah, either in
addAttributes
or addOptions
I think the PR is still good though. Opens up some possibilities. Just won’t work with existing extensions unless those attrs are also added to the core js/php extensions.
But the reason Dan is pulling them out of the container is so you can bind in your own version of the php extension as a replacement. So if you didn’t want loading in the core extension you could just copy it and add whatever you need then bind it in a service provider.