|
| 1 | +/* |
| 2 | + * Hibernate Infra - Asciidoctor extensions |
| 3 | + * |
| 4 | + * License: Apache License, Version 2.0 |
| 5 | + * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. |
| 6 | + */ |
| 7 | +package org.hibernate.infra.asciidoctor.extensions.copytoclipboard; |
| 8 | + |
| 9 | +import java.io.BufferedReader; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.io.InputStreamReader; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +import org.asciidoctor.ast.Document; |
| 17 | +import org.asciidoctor.extension.DocinfoProcessor; |
| 18 | +import org.asciidoctor.extension.Location; |
| 19 | +import org.asciidoctor.extension.LocationType; |
| 20 | + |
| 21 | +@Location(LocationType.FOOTER) |
| 22 | +public class CopyToClipboardProcessor extends DocinfoProcessor { |
| 23 | + @Override |
| 24 | + public String process(Document document) { |
| 25 | + try ( |
| 26 | + InputStream is = this.getClass().getResourceAsStream( "/copy/clipboard.min.js" ); |
| 27 | + InputStreamReader isr = new InputStreamReader( is, StandardCharsets.UTF_8 ); |
| 28 | + BufferedReader reader = new BufferedReader( isr ) |
| 29 | + ) { |
| 30 | + String clipboardJs = reader.lines().collect( Collectors.joining( "\n" ) ); |
| 31 | + return String.format( |
| 32 | + "<script>%s</script>\n<link rel=\"stylesheet\" href=\"https://use.fontawesome.com/releases/v6.7.2/css/all.css\" crossorigin=\"anonymous\">\n<style>%s</style>\n<script>%s</script>", |
| 33 | + clipboardJs, |
| 34 | + "pre.highlight .btn-copy{-webkit-transition:opacity 0.3s ease-in-out;-o-transition:opacity 0.3s ease-in-out;transition:opacity 0.3s ease-in-out;opacity:0;padding:2px 6px;position:absolute;right:4px;top:.225rem;background-color:#fff0;border:none}.listingblock:hover .btn-copy,pre.highlight .btn-copy:focus{opacity:.5}pre.highlight{position:relative}.listingblock code[data-lang]::before{display:block;content:attr(data-lang) '|';right:1.5rem;-webkit-transition:opacity 0.3s ease-in-out;-o-transition:opacity 0.3s ease-in-out;transition:opacity 0.3s ease-in-out;opacity:0}.listingblock:hover code[data-lang]::before{opacity:.5}.tooltip-text{display:block;position:absolute;top:1.5rem;right:4px;background:#444;color:#fff;padding:6px 10px;border-radius:4px;white-space:nowrap;z-index:1;opacity:0;font-size:11px}.listingblock:hover .tooltip-text.show{opacity:1}", |
| 35 | + "const codes=document.querySelectorAll('pre.highlight > code');let index=0;codes.forEach((code)=>{code.setAttribute(\"id\",\"code\"+index);const block=document.createElement('div');block.className=\"tooltip\";const btn=document.createElement('button');btn.className=\"btn-copy fa-regular fa-copy\";btn.setAttribute(\"data-clipboard-action\",\"copy\");btn.setAttribute(\"data-clipboard-target\",\"#code\"+index);btn.setAttribute(\"title\",\"Copy to clipboard\");btn.setAttribute(\"float-right\",\"true\");code.before(btn);const tooltip=document.createElement('div');tooltip.className=\"tooltip-text\";tooltip.textContent=\"Copied!\";code.before(tooltip);index++});const clipboard=new ClipboardJS('.btn-copy');clipboard.on('success',function(e){e.clearSelection();e.trigger.className=e.trigger.className.replace(\"fa-copy\",\"fa-check\").replace(\"fa-regular \",\"fa \");e.trigger.setAttribute(\"title\",\"Copied!\");e.trigger.nextSibling.classList.toggle(\"show\");e.trigger.blur();setTimeout(function(){e.trigger.className=e.trigger.className.replace(\"fa-check\",\"fa-copy\").replace(\"fa \",\"fa-regular \");e.trigger.setAttribute(\"title\",\"Copy to clipboard\");e.trigger.nextSibling.classList.toggle(\"show\")},1500)})" |
| 36 | + ); |
| 37 | + } |
| 38 | + catch (IOException e) { |
| 39 | + throw new RuntimeException( e ); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments