-
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
Open in New Window option for links does not exist #55
Comments
+1 |
In the context menu? Shows up for me in Safari 8.0.8. Edit: I'm not a smart person. |
@codeclown I’m guessing @Quirksmode means an option in the editor to add |
That's correct @adamschwartz, whilst I don't agree it's best practice, I am yet to meet a client who doesn't want the ability to open their links in a new window. A simple checkbox below the input where you add the url would suffice :-) |
@Quirksmode Absolutely I think it’s a very reasonable suggestion. 👍 |
Yes please! |
Thanks for the feature request, but I don’t think we’ll be adding this to Trix. If you need to add a target attribute to links, I’d suggest post-processing the HTML when you render it in your application. |
This would be a great, and trivial feature to add, while adding post-processing to any application that would like this feature is definitely more complex. Would you reconsider? |
You could also handle this client side instead of post-processing your HTML: // Open all external links in a new window
addEventListener("click", function(event) {
var el = event.target
if (el.tagName === "A" && !el.isContentEditable && el.host !== window.location.host) {
el.setAttribute("target", "_blank")
}
}, true) |
IMO, Workaround suggest to post-process is poor. I might want to create links to external content and internal content. |
+1 yeah - i'll need this one too |
If anyone is looking for a quick way to fix this in rails. Here's what you can do:
Then in your view you can: |
as the usage is growing, I am facing limitations with trix and this is one of them. The other one is inability to set constraints like required for the input boxes. |
+1 on this one... it's a very common use-case, and doing this outside the editor (post-processing) seems like a hack. It seems a a strange decision to reject it outright like this. |
Adding my +1 to make this feature part of Trix. |
+1 Is there a philosophical reason this is not to be developed? As in, if a PR were to come in for this, would it be rejected? |
+1 Post process only handle trix html after, we also need it to target blank when editing. |
FYI, at last I add it works for editor and show trix content. $(document).ready(function() {
$(".trix-content a").click(function(e) {
$(this).attr("target","_blank");
});
// For those content inject by javascript, use an event handler for your trix container may be better.
$(YourTrixContainerSelector).on('click', '.trix-content a', function(e) {
$(this).attr("target","_blank");
});
}); P.S. I prefer stick the solution to Javascript. So it can turn on case by case for each view. |
@RicottaZhang this workaround works well. Thank you. All Trix links are opened in a new tab. |
@RicottaZhang's solution didn't work for me b/c in some places, our content gets put on the page by JavaScript, so it isn't there when the page loads. This is the best I've come up with: # file: app/helpers/trix_helper.rb
module TrixHelper
SCRUBBER = Loofah::Scrubber.new do |node|
node[:target] = '_blank' if node.name == 'a'
end
# Overriding https://github.com/rails/rails/blob/da795a678b341adc8031c4d9cf0b5ef2176e2a5a/actiontext/app/helpers/action_text/content_helper.rb#L17
def sanitize_action_text_content(content)
original_scrubber, self.scrubber = self.scrubber, SCRUBBER
super
ensure
self.scrubber = original_scrubber
end
end |
I was able to easily serialize my data coming from ActionText to accomplish this. Maybe it'll help someone else out
|
For some use cases (all the time for me), the user needs to be able to choose new window. Post processing with ruby or js to open in new window for all links or for external links is to broad brush. needs option to choose. If I make my own button to add links (simple enough) how can I stop trix stripping my target=_blank ? |
Adding a +1 - this is a requirement for my project too. Whilst I can do a workaround for now, I can see that we will need to ask as part of the link dialog as to whether the link should open in a new window or not |
It is very hard to believe this is not an option on a per-link basis and is not even considered after people suggest it. |
+1 |
1 similar comment
+1 |
For those of you wondering if there is a Stimulus approach to this problem I found the following answer on StackOverflow:
And wrap your rich text display as follow:
|
Yeah, I ended up taking the stimulus approach as well, but what a poor thing to have to do for such a basic feature. |
+1 |
Rather pointless, but anyway - I'd like to emphasise that proposed post processing solutions work only for internally rendered links. It would be much cleaner to hold Rich Text with everything we need, so it can be reused and rendered elsewhere. EDIT: Create a ActionText::ContentHelper.allowed_attributes += ['target'] Then in your model, which content.gsub('<a href', '<a target="_blank" href') |
Thank You @spaquet for the snippet but it is disappointing that we need to use a hack for such a basic thing. |
Another +1 for this feature. Hard to believe it was simply dismissed out of hand back in 2015, and never reconsidered. Literally every project I work on, the client want to be able to optionally open embedded links in a new tab. |
A mix of @wawer77 and @PQALAB approaches with a concern to include in your model. adding a before_save on all has_rich_text attributes of the class. # app/models/concerns/rich_text_target_blank.rb
module RichTextTargetBlank
extend ActiveSupport::Concern
class_methods do
# Override has_rich_text to include target="_blank" functionality
def has_rich_text(name)
super(name) # Call the original has_rich_text to set up the rich text association
# Define the before_save callback to modify the links
before_save do
rich_text_attribute = self.send(name)
if rich_text_attribute.present?
doc = Nokogiri::HTML::DocumentFragment.parse(rich_text_attribute.body.to_html)
doc.css('a').each { |a| a['target'] = '_blank' }
rich_text_attribute.body = doc.to_html
end
end
end
end
end |
I'm working on a fork of trix, and added code in the library to control this. Curious if PR is worth it and approach like this would be accepted? The implementation is simple, you can set attributes property like this: The only actual code change in the library is in
So basically those 3 lines, one of which one is a closing brace, allows the users to add whatever custom attributes they want purely through a config. You could technically change the config programmatically with some checkbox, but once you undo/redo it will use the latest config value. But even with this issue, I think the solution is clean and will allow library consumers to at least somewhat control this? |
Something needs to be done I think :) I'll have a play and see if there's a straightforward way to do this in a way that allows users to choose each time :) |
I think the fundamental hard thing about this is that the toolbar module wasn't built to do more than update a single attribute gleaned from a form input: trix/src/trix/controllers/toolbar_controller.js Lines 205 to 216 in d34ebea
It's 'abstracted' to some point but has only been created for this one (built in) use case given there's no other toolbar interfaces like the link insertion one. I'll have a go at extending these methods to cope with more than one attribute at a time. The fallback will be to just replace the default link insertion dialog with one that kicks off a stimulus controller. I've done this for YouTube videos. |
I've created a PR, please lend your support if you are able to figure out the last little bits. |
I did this in an initializer, it's better than the stimulus approach because it works for search engines to prevent following links in user generated content . config.after_initialize do
ActionText::ContentHelper.scrubber = Loofah::Scrubber.new do |node|
if node.name == 'a'
node[:target] = '_blank'
node[:rel] = 'noopener ugc nofollow'
end
end |
That's a good option if you want all links to open in a new tab. Still hoping someone will review my PR that allows the editor user to choose... |
No description provided.
The text was updated successfully, but these errors were encountered: