Skip to content

Commit e67ac8f

Browse files
committed
Catch tabs and manual copying
1 parent b26b34f commit e67ac8f

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

preview-src/code.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ Copy me!
1111
Do something else
1212
Callout # <1>
1313
Callout # <2>
14+
Callout tab # <3>
1415
Comment # Just a regular comment after some code
1516
----
1617

1718
<1> Callout
1819
<2> Callout
20+
<3> Callout after a tab
1921

2022

2123

src/js/06-code.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ import { createElement } from './modules/dom'
3535
}
3636
})()
3737

38+
var cleanCallouts = function (code) {
39+
return code.replace(/[ |\t]+\n/g, '\n').trimEnd()
40+
}
41+
3842
document.addEventListener('DOMContentLoaded', function () {
3943
var body = document.querySelectorAll('body')
4044
var ignore = ['gram']
@@ -46,11 +50,13 @@ document.addEventListener('DOMContentLoaded', function () {
4650
input = window.neo4jDocs.copyableCommand(input)
4751
}
4852

49-
input = input.replace(/[ ]+\n/g, '\n').trimEnd()
53+
input = cleanCallouts(input)
54+
55+
// input = input.replace(/[ |\t]+\n/g, '\n').trimEnd()
5056

5157
return input
5258
}
53-
59+
5460
var copyToClipboard = function (code, language) {
5561
var textarea = document.createElement('textarea')
5662
textarea.value = cleanCode(code, language)
@@ -75,6 +81,16 @@ document.addEventListener('DOMContentLoaded', function () {
7581
}
7682
}
7783

84+
// capture copy command
85+
let copyThis = document.querySelectorAll("pre code")
86+
copyThis.forEach((code) => {
87+
code.addEventListener("copy", (e) => {
88+
const selection = document.getSelection();
89+
e.clipboardData.setData("text/plain", cleanCallouts(selection.toString()))
90+
e.preventDefault()
91+
});
92+
});
93+
7894
function capitalizeFirstLetter (string) {
7995
return string.charAt(0).toUpperCase() + string.slice(1)
8096
}

0 commit comments

Comments
 (0)