From c51bc1c3f51ad414ec011d9d4a75f38ce906c3e2 Mon Sep 17 00:00:00 2001 From: Katsuya Hidaka Date: Mon, 16 Sep 2024 14:02:11 +0900 Subject: [PATCH 4/7] Separate the quote reply implementation into quote_reply.js --- app/assets/javascripts/application.js | 63 --------------------------- app/assets/javascripts/quote_reply.js | 62 ++++++++++++++++++++++++++ app/views/issues/show.html.erb | 4 ++ app/views/messages/show.html.erb | 4 ++ 4 files changed, 70 insertions(+), 63 deletions(-) create mode 100644 app/assets/javascripts/quote_reply.js diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index a155b7223..c31834d04 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -1262,69 +1262,6 @@ function inlineAutoComplete(element) { tribute.attach(element); } -function quoteReply(path, selectorForContentElement) { - const contentElement = $(selectorForContentElement).get(0); - const quote = QuoteExtractor.extract(contentElement); - - $.ajax({ - url: path, - type: 'post', - data: { quote: quote } - }); -} - -class QuoteExtractor { - static extract(targetElement) { - return new QuoteExtractor(targetElement).extract(); - } - - constructor(targetElement) { - this.targetElement = targetElement; - this.selection = window.getSelection(); - } - - extract() { - const range = this.selectedRange; - - if (!range) { - return null; - } - - if (!this.targetElement.contains(range.startContainer)) { - range.setStartBefore(this.targetElement); - } - if (!this.targetElement.contains(range.endContainer)) { - range.setEndAfter(this.targetElement); - } - - return this.formatRange(range); - } - - formatRange(range) { - return range.toString().trim(); - } - - get selectedRange() { - if (!this.isSelected) { - return null; - } - - // Retrive the first range that intersects with the target element. - // NOTE: Firefox allows to select multiple ranges in the document. - for (let i = 0; i < this.selection.rangeCount; i++) { - let range = this.selection.getRangeAt(i); - if (range.intersectsNode(this.targetElement)) { - return range; - } - } - return null; - } - - get isSelected() { - return this.selection.containsNode(this.targetElement, true); - } -} - $(document).ready(setupAjaxIndicator); $(document).ready(hideOnLoad); $(document).ready(addFormObserversForDoubleSubmit); diff --git a/app/assets/javascripts/quote_reply.js b/app/assets/javascripts/quote_reply.js new file mode 100644 index 000000000..6238395dc --- /dev/null +++ b/app/assets/javascripts/quote_reply.js @@ -0,0 +1,62 @@ +function quoteReply(path, selectorForContentElement) { + const contentElement = $(selectorForContentElement).get(0); + const quote = QuoteExtractor.extract(contentElement); + + $.ajax({ + url: path, + type: 'post', + data: { quote: quote } + }); +} + +class QuoteExtractor { + static extract(targetElement) { + return new QuoteExtractor(targetElement).extract(); + } + + constructor(targetElement) { + this.targetElement = targetElement; + this.selection = window.getSelection(); + } + + extract() { + const range = this.selectedRange; + + if (!range) { + return null; + } + + if (!this.targetElement.contains(range.startContainer)) { + range.setStartBefore(this.targetElement); + } + if (!this.targetElement.contains(range.endContainer)) { + range.setEndAfter(this.targetElement); + } + + return this.formatRange(range); + } + + formatRange(range) { + return range.toString().trim(); + } + + get selectedRange() { + if (!this.isSelected) { + return null; + } + + // Retrive the first range that intersects with the target element. + // NOTE: Firefox allows to select multiple ranges in the document. + for (let i = 0; i < this.selection.rangeCount; i++) { + let range = this.selection.getRangeAt(i); + if (range.intersectsNode(this.targetElement)) { + return range; + } + } + return null; + } + + get isSelected() { + return this.selection.containsNode(this.targetElement, true); + } +} diff --git a/app/views/issues/show.html.erb b/app/views/issues/show.html.erb index 2a8f1a792..2a6e6448f 100644 --- a/app/views/issues/show.html.erb +++ b/app/views/issues/show.html.erb @@ -1,3 +1,7 @@ +<% content_for :header_tags do %> + <%= javascript_include_tag 'quote_reply' %> +<% end %> + <%= render :partial => 'action_menu' %>

<%= issue_heading(@issue) %>

<%= issue_status_type_badge(@issue.status) %> diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 50e2f65e7..5ac083cc0 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -1,3 +1,7 @@ +<% content_for :header_tags do %> + <%= javascript_include_tag 'quote_reply' %> +<% end %> + <%= board_breadcrumb(@message) %>
-- 2.44.0