mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-02-21 20:20:34 +09:00
49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
|
|
const unfurled_attachment_selector = ".og-embed"
|
|
|
|
export default class extends Controller {
|
|
static targets = [ "body", "link", "author" ]
|
|
static outlets = [ "composer" ]
|
|
|
|
connect() {
|
|
this.#formatLinkTargets()
|
|
}
|
|
|
|
reply() {
|
|
const content = `<blockquote>${this.#bodyContent}</blockquote><cite>${this.authorTarget.innerHTML} ${this.#linkToOriginal}</cite><br>`
|
|
this.composerOutlet.replaceMessageContent(content)
|
|
}
|
|
|
|
#formatLinkTargets() {
|
|
this.bodyTarget.querySelectorAll("a").forEach(link => {
|
|
const sameDomain = link.href.startsWith(window.location.origin)
|
|
link.target = sameDomain ? "_top" : "_blank"
|
|
})
|
|
}
|
|
|
|
get #bodyContent() {
|
|
const body = this.bodyTarget.querySelector(".trix-content").cloneNode(true)
|
|
return this.#stripMentionAttachments(this.#stripUnfurledAttachments(body)).innerHTML
|
|
}
|
|
|
|
#stripMentionAttachments(node) {
|
|
node.querySelectorAll(".mention").forEach(mention => mention.outerHTML = mention.textContent.trim())
|
|
return node
|
|
}
|
|
|
|
#stripUnfurledAttachments(node) {
|
|
const firstUnfurledLink = node.querySelector(`${unfurled_attachment_selector} a`)?.href
|
|
node.querySelectorAll(unfurled_attachment_selector).forEach(embed => embed.remove())
|
|
|
|
// Use unfurled link as the content when the node has no additional text
|
|
if (firstUnfurledLink && !node.textContent.trim()) node.textContent = firstUnfurledLink
|
|
|
|
return node
|
|
}
|
|
|
|
get #linkToOriginal() {
|
|
return `<a href="${this.linkTarget.href}">#</a>`
|
|
}
|
|
}
|