Skip to content

Commit 7001096

Browse files
committed
improve copying from code blocks
Set a `copy` event handler on "SCodeFlow" elements to perform a text copy that strips away double newlines. This improves copying in Firefox and Chrome, and it doesn't seem to hurt Safari.
1 parent 41171ec commit 7001096

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

scribble-lib/scribble/manual-racket.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ AddOnLoad(function() {
1515
}
1616
})
1717

18+
// for copies that start in Racket code, strip out extra newlines
19+
AddOnLoad(function() {
20+
const codeBlocks = document.getElementsByClassName("SCodeFlow");
21+
for (var i = 0; i < codeBlocks.length; i++) {
22+
var codeBlock = codeBlocks[i];
23+
console.log("add");
24+
codeBlock.addEventListener('copy', function(e) {
25+
var selection = window.getSelection();
26+
var text = selection.toString();
27+
var codeText = text.replace(/\n\n/g, '\n');
28+
if (text != codeText) {
29+
e.preventDefault();
30+
e.clipboardData.setData('text/plain', codeText);
31+
}
32+
})
33+
}
34+
})
35+
1836
// cache of source urls
1937
var cache = {};
2038

0 commit comments

Comments
 (0)