From 00dc74331b00460de175d3f8baa6154af89a2652 Mon Sep 17 00:00:00 2001 From: mummified-corroding-granny Date: Tue, 14 Feb 2023 02:05:16 +0000 Subject: [PATCH] add javascript to generate format copy buttons programatically --- files/assets/js/formatting.js | 37 +++++++++++++++++++++++++++++++++ files/templates/formatting.html | 12 +++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 files/assets/js/formatting.js diff --git a/files/assets/js/formatting.js b/files/assets/js/formatting.js new file mode 100644 index 000000000..32cd3ed72 --- /dev/null +++ b/files/assets/js/formatting.js @@ -0,0 +1,37 @@ + +/* addFormattingCopyButtons(): creates a button in the first column of each row of a table + that copies the text in the second column of that row */ +function addFormattingCopyButtons() { + var allTablesGenerateCopyButtons = document.getElementsByClassName('generate-copy-buttons') + + for (let table = 0; table < allTablesGenerateCopyButtons.length; table++) { + + if(allTablesGenerateCopyButtons[table].tagName != 'TABLE') { + continue; + } + + for (var i = 1, row; row = allTablesGenerateCopyButtons[table].rows[i]; i++) { + + let textCopyButton = document.createElement("button"); + textCopyButton.setAttribute("type", "button"); + textCopyButton.className = "btn caction py-0 nobackground px-1 text-muted copy-link"; + + /* replace HTML newlines with text newlines */ + var cleanedText = row.cells[1].cloneNode(true) + cleanedText.innerHTML = cleanedText.innerHTML.replace(/
/gi, "\n") + /* remove lots of extraneous tabs */ + cleanedText = cleanedText.textContent.replace(/\t/g,''); + textCopyButton.setAttribute("data-clipboard-text", cleanedText); + + copyIcon = document.createElement("i"); + copyIcon.className = "fas fa-copy"; + + textCopyButton.insertAdjacentElement('afterbegin', copyIcon) + row.cells[0].appendChild(textCopyButton); + } + } + + +} + +addFormattingCopyButtons(); diff --git a/files/templates/formatting.html b/files/templates/formatting.html index 70aedc200..24accb0d2 100644 --- a/files/templates/formatting.html +++ b/files/templates/formatting.html @@ -5,7 +5,7 @@
You can use Markdown formatting:
-
+
@@ -237,7 +237,7 @@ And we allow custom HTML in most places:

Allowed Tags

-
Name
+
@@ -626,4 +626,12 @@ line breaks
Name
+
+
+ Text copied to clipboard +
+
+ + + {% endblock %} -- 2.34.1