trix icon indicating copy to clipboard operation
trix copied to clipboard

Added support for HTML className for Block objects

Open SergeyBurtsev opened this issue 4 years ago • 17 comments

This PR allows to use HTML class attribute (className) for Block level objects and that allows to add extended functionality like text alignment adding and toggling those classes for the nearest block object.

SergeyBurtsev avatar May 23 '20 18:05 SergeyBurtsev

If this is going to be eventually supported I would love to see this extended to textAttributes as well, for inline elements. I know @rafaelfranca closed an feature request that was disguised as an Issue, @javan would Sergey's PR be sufficient to mark as a solution to the issue?

tbcooney avatar Jun 02 '20 03:06 tbcooney

Awesome work @SergeyBurtsev! This will make adding classes to block objects way easier.

clarkcode avatar Jul 09 '20 22:07 clarkcode

Thank you @clarkcode ! Maybe I did this patch too hastly without discussing with anyone here and only then realized that it can violate Trix philisophy and long-term plans. I am less professional than others here, so I am not surprised if I missed the point :)

SergeyBurtsev avatar Jul 10 '20 06:07 SergeyBurtsev

When will this be resolved? I been thinking all the time that TRIX can be extended with custom toolbar items, but apparently there's no way I can extend it to support a block level element like "textAlign: center" in a clean way without className support. This is a dealbreaker since I have to support existing rich text content and I've lost a fair amount of time on researching this. I'm very happy with TRIX but can you please give an update on this matter?

vitality82 avatar Oct 08 '20 12:10 vitality82

Hoping there's some movement on merging this soon as well.

I need to wrap some text in a div with a class in a project I'm working on. Is there any workaround for this currently until this is merged?

mguidetti avatar Nov 08 '20 14:11 mguidetti

Any updates on this?

davissp14 avatar Nov 25 '20 01:11 davissp14

I am currently attempting to impliment mentions in a Trix editor. I am attempting to do something like editor.insertHTML(<strong class="mention-${userId}>${userName} ); but can not add class attribute or any other way to target that with CSS. This PR would help very much.

Shelob9 avatar Feb 09 '21 17:02 Shelob9

@SergeyBurtsev was hoping you might be able to help advise. I'm adding a block object with tags successfully from a custom button on the toolbar, but was thinking an addition like this would allow your changes to also append a class, alas ave tried a few things but unable to get them to show.

 Trix.config.blockAttributes.smallbox = { 
      tagName: 'small-box', 
      className: 'class-name-test',
      };

richlarcombe avatar Feb 20 '21 03:02 richlarcombe

@richlarcombe I've refactored the fork, so now it should work the way you suggested.

SergeyBurtsev avatar Feb 22 '21 15:02 SergeyBurtsev

@SergeyBurtsev, this is a great help, many thanks. There is one related issue that is holding this back being usable though. When you initially load the trix editor and add the block object, the class is applied and block appears as intended and also when the page is viewed on the front end, all appears well. The problem arises when you reopen the trix editor the className (and tagName if you have one) appears to be stripped on load. I'm not suggesting that your change is introducing this problem, it's just impacted by this existing issue, so i'm not sure where to go from here? The issue https://github.com/basecamp/trix/issues/864 is similar, but for me the other block objects i have on the same page without classNames added, still render ok (tags remain intact) - so don't believe the same solution with the ordering of the sanitising is applicable. Frustratingly close to nice solution.

richlarcombe avatar Feb 25 '21 00:02 richlarcombe

@SergeyBurtsev actually after further testing have found that this issue only occurs for multiple strings in the class field. So using just a single class name does render ok fine upon re-edit, although not sure how useful this will be in it's current form as the main use case i would of thought would be to add multiple class names - as in "col-md-4" & "col-sm-6" etc with bootstrap styling for example. Any chance you could amend to facilitate a list of classes?

richlarcombe avatar Feb 25 '21 05:02 richlarcombe

@richlarcombe You are right about multiple classes. I've updated the HTMLParser, so it now compares full className string of the element, so you could use it like: Trix.config.blockAttributes.smallbox = { tagName: 'small-box', className: 'class-name-1 class-name-2', };

But I still don't know if this addon is valuable for the core Trix editor. Maybe I should close this PR and implement it as a separate patch, which can by applied over the official release if needed.

SergeyBurtsev avatar Feb 27 '21 08:02 SergeyBurtsev

@SergeyBurtsev thanks for that, works great. I think there'll be a fair few people keen to see this go into the mainline, but guess that's not our call. I'm a +1.

richlarcombe avatar Mar 02 '21 03:03 richlarcombe

But I still don't know if this addon is valuable for the core Trix editor. Maybe I should close this PR and implement it as a separate patch, which can by applied over the official release if needed.

@SergeyBurtsev I would donate towards this patch.

petebytes avatar Mar 16 '21 13:03 petebytes

@SergeyBurtsev thank you for this! :)

artisr avatar Jun 16 '21 06:06 artisr

Is this ever be implemented? It would be such a game-breaker feature for Trix usage... @dhh

Mth0158 avatar Feb 27 '22 19:02 Mth0158

@Mth0158 I already published adding this PR custom trix version, including the align center and right button in toolbar also, you can check the code if you having my similar requirement.

Eric-Guo avatar Mar 06 '22 07:03 Eric-Guo