-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Still not possible to create a new paragraph on Enter #680
Comments
We need this. |
Yep, this is frustrating. Just got far enough along integrating Trix into an app only to find out that it turns a clean semantic document of My understanding is that the Trix developers don't see any way forward since they can't use paragraph elements lest someone inserts an attachment inside of one: #202 (comment). I haven't seen any explanation for why they can't simply break a There are several other WYSIWYG editor options listed in this discussion from another project that hit the issue before migrating to Quill.js: vapid/vapid#53 (comment) |
Hi all, I came across this and it annoyed me. I didn't want to switch editor so I build a hacky JS based solution but thought I'd share it as it does work (and handles images - probably other attachments too, but I'm only using images). Right now I'm separating my new lines with The code works by updating what I call the
Trix naturally updates this input when a change is made to the main form, and I highjack that change on Handling everything (text, pre [code blocks], blockquotes, h1, ol, ul, copy and paste) except images:
Handling images: Trix has an inbuilt 'trix-file-accept' event which you can listen to to do js validations on file types / sizes, which I was already using. So I added to that by making sure I got an alert if the upload conditions were satisfied (saveAlert). This also activates an imageRemovalChecker function which runs every second after an image has been uploaded to check if it has been subsequently removed without a further keystroke being made by the user.
The only other edge case that I had to resolve for was if someone uploads an image, begins to alter the image 'attachment_caption' and submits before pressing enter/starting a new line. I removed this edge case by simply setting:
So, it is a big hack - but it works (as far as I can tell). I'm a junior junior developer, so this might be very bad practice to solve problems like this - if so please to tell me. And if not, then please contact me to hire me :). ✌️ |
Very hacky indeed. We already had multiple proposed pull requests for it anyway, it's just that they never get merged. |
did anyone figure out the best solution for this yet? |
+1. I was trying to migrate from TinyMCE to Trix, but this is a dealbreaker. Trix isn't able to handle existing HTML values with We need to maintain the same paragraph and line-break behavior as the other editor to avoid messing up or reformatting existing markup. |
Same boat, was pretty excited about this editor until I found out it does not generate html with paragraph tags. |
It's not actually too difficult to convert the document structure to HTML manually, converting textual paragraphs to HTML That what I did for a public initiative's site in Estonia — Rahvaalgatus. Serialization example is in https://github.com/rahvaalgatus/rahvaalgatus/blob/master/lib/trix.js and its tests at https://github.com/rahvaalgatus/rahvaalgatus/blob/master/test/lib/trix_test.jsx. The Trix editor itself is utilized on https://github.com/rahvaalgatus/rahvaalgatus/blob/master/views/initiatives/update_page.jsx, which, at the time of submit, grabs the structured document via |
@moll we can do hacks to bypass the shortcomings of this library, but the default behaviour remains contrary to everyone's expectation. And it's not for a lack of proposed pull requests but a general unwillingness to merge them into the main repo. |
https://github.com/lazaronixon/trix/tree/contentify <h1>title<h1>
<div class="p">paragraph1</div>
<div class="p">paragraph1</div> You can configure paragraph class with:
package.json "trix": "lazaronixon/trix#contentify" |
This issue has been automatically marked as stale after 90 days of inactivity. It will be closed if no further activity occurs. |
Still no hope? The |
This issue has been automatically marked as stale after 90 days of inactivity. It will be closed if no further activity occurs. |
Monkey-patch version of this solution, for these who want latest version of Trix and need this feature Trix.config.blockAttributes.default.tagName = "p"
Trix.config.blockAttributes.default.breakOnReturn = true;
Trix.Block.prototype.breaksOnReturn = function(){
const attr = this.getLastAttribute();
const config = Trix.getBlockConfig(attr ? attr : "default");
return config ? config.breakOnReturn : false;
};
Trix.LineBreakInsertion.prototype.shouldInsertBlockBreak = function(){
if(this.block.hasAttributes() && this.block.isListItem() && !this.block.isEmpty()) {
return this.startLocation.offset > 0
} else {
return !this.shouldBreakFormattedBlock() ? this.breaksOnReturn : false;
}
}; |
Small mistake, replace the following line: return startLocation.offset > 0 with: return this.startLocation.offset > 0 But apart from this, I can confirm that @MaksimBurnin solution works for new content. You can try it yourself with the usual caveat about images which you've to do manually because the default behavior uses the If you want to use Trix to edit existing content however, it doesn't seem to be enough - here I give Trix the starting value |
Good catch. Fixed in my snippet. Thank you @JosephHalter
This one is weird, because i can see it in your gist, but can not replicate in my project. Edit: this looks like a race condition. See modified example: https://jsfiddle.net/xcpr8qv5/ |
Thank you, it indeed works better this way. There're plenty of monkey patches to force the square peg that is Trix into the round hole that I need for many client projects, each one more uglier that the next. I quite like your version, it's quite nice and limited in scope compared to many alternatives that I've seen and used before. |
My version is basically a direct translation from https://github.com/lazaronixon/trix/tree/contentify so credit goes there :) |
@MaksimBurnin Any idea where to do this monkey patching when using trix in a Rails project? I can't seem to get it to work. EDIT: Found a solution! For those looking to implement in Rails 6, I ended up doing it like this in a custom See the PR here: lnu-norge/lokaler.lnu.no#26 |
So for those who don't stuff trix into "your next best framework" what's the solution here? I tried appending some config options to trix in a script tag, but that messed up form submission to where the visible tags were not the ones submitted. For countless reasons using div everywhere is troublesome when div behavior is not consistent with many theme frameworks such as bootstrap. It would be nice to not have div placed at all and just br on line breaks, but nothing in the documentation mentions on how to accomplish this, could this be explained somewhere easily accessible without having to dig through various issue tickets and PRs? |
@MaksimBurnin Thanks a lot for your solution. It works really well but sometimes when saving, it adds empty 's between the paragraphs in my app (Rails 7). Have you experienced similar issues and is there a fix to this? Thank you |
@MaksimBurnin Thanks! tags). One method is missing but it can be fixed with line: |
Thanks, @MaksimBurnin , your solution works with small change: Trix.config.blockAttributes.default.tagName = "p";
Trix.config.blockAttributes.default.breakOnReturn = true;
Trix.Block.prototype.breaksOnReturn = function() {
const attr = this.getLastAttribute();
// Function `Trix.getBlockConfig()` was not found so I replaced it with analog:
const config = Trix.config.blockAttributes[attr ? attr : "default"];
return config ? config.breakOnReturn : false;
};
Trix.LineBreakInsertion.prototype.shouldInsertBlockBreak = function() {
if (this.block.hasAttributes() && this.block.isListItem() && !this.block.isEmpty()) {
return this.startLocation.offset > 0;
} else {
return !this.shouldBreakFormattedBlock() ? this.breaksOnReturn : false;
}
}; |
Selfishly this frustrates me greatly when I try to do things like "take the first few elements out as a preview." You'll get a big In case my personal anecdote doesn't resonate, I'll tack on that accessibility is supposedly troublesome if you don't use
|
There's been a few related issues:
#75
#117
#123
#202
And a few pull requests:
#187
#240
It's now possible to configure Trix to use a
<p>
tag instead of<div>
with:However, it's still not possible to configure Trix so that when I press Enter it creates a new paragraph instead of inserting a
<br>
tag. I'm aware of the issue with the<figure>
tag which can't be put inside a paragraph and stay a valid HTML, however I'm not using images (and if I was I would just do it without using the<figure>
tag). I don't say that creating a new paragraph on Enter should be the default but there should at least be an option. For example, thebreakOnReturn
option like proposed in pull request 187.iTunes only supports the following tags:
<p>
,<ol>
,<ul>
,<li>
and<a>
. No other tag is supported, and this includes the<br>
tag as far as I know. It would be really great if someone could have a fresh new look at that issue. Especially as it has been asked by numerous people already, and code was provided multiple times but no solution ever gets merged.The text was updated successfully, but these errors were encountered: