Add controller to retarget links
So that internal links can skip turbo frames, and external links open in new pages
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
|
||||
export default class extends Controller {
|
||||
connect() {
|
||||
this.element.querySelectorAll("a").forEach(this.#retargetLink.bind(this))
|
||||
}
|
||||
|
||||
#retargetLink(link) {
|
||||
link.target = this.#targetsSameDomain(link) ? "_top" : "_blank"
|
||||
}
|
||||
|
||||
#targetsSameDomain(link) {
|
||||
return link.href.startsWith(window.location.origin)
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div class="comment__body rich-text-content txt-align-start" data-controller="syntax-highlight">
|
||||
<div class="comment__body rich-text-content txt-align-start" data-controller="syntax-highlight retarget-links">
|
||||
<%= comment.body %>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<h1 class="card__title flex align-start gap-half">
|
||||
<%= link_to card.title, edit_collection_card_path(card.collection, card), class: "card__title-link" %>
|
||||
</h1>
|
||||
<div class="card__description rich-text-content margin-block-half rich-text-content" data-controller="syntax-highlight">
|
||||
<div class="card__description rich-text-content margin-block-half rich-text-content" data-controller="syntax-highlight retarget-links">
|
||||
<%= card.description %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user