Browse Source

Add quadruple click to select all

master
Gabriel Augendre 1 month ago
parent
commit
921994d2ad
  1. 28
      articles/static/copy-code.js
  2. 3
      articles/templates/articles/base.html

28
articles/static/copy-code.js

@ -0,0 +1,28 @@
"use strict";
function addCopyCode() {
const codeBlocks = document.querySelectorAll("pre");
codeBlocks.forEach(pre => {
pre.addEventListener("click", event => {
if (event.detail === 4) {
const selection = window.getSelection();
selection.setBaseAndExtent(
pre.querySelector("code").firstChild,
0,
pre.querySelector("code").lastChild,
1
);
}
});
pre.setAttribute("title", "Quadruple click to select all");
});
}
((readyState) => {
if (readyState === "interactive") {
addCopyCode();
} else if (readyState === "loading") {
window.addEventListener("DOMContentLoaded", addCopyCode, false);
}
})(document.readyState);

3
articles/templates/articles/base.html

@ -63,6 +63,9 @@
{% if user.is_authenticated %}
<script src="{% static 'edit-keymap.js' %}" async defer></script>
{% endif %}
{% if article and article.has_code %}
<script src="{% static 'copy-code.js' %}" async defer></script>
{% endif %}
</body>
</html>

Loading…
Cancel
Save