Skip to content

Commit

Permalink
add copy button for .edp & .md examples
Browse files Browse the repository at this point in the history
  • Loading branch information
phtournier committed Dec 5, 2024
1 parent 8d17563 commit fde94c3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions source/_static/css/examples.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
width:100%;
}

#ExampleCopyButton {
display: inline-block;
position: relative;
}

#checkbox-markdown-toggle {
margin-right: 2em;
}
Expand Down
35 changes: 34 additions & 1 deletion source/_static/js/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ let viewMarkdown = true

let iscurrentfilemd = false

let mdsource = ""

toggleExamples = (mode) => {
if (mode) {
subnav.children[1].style.display = 'block'
Expand Down Expand Up @@ -58,12 +60,13 @@ loadExamplefromGitHub = (name, dir, editor) => {
}
examplecode.style.display = 'flex'
examplewelcome.style.display = 'none'
showExample(viewMarkdown)
editor.setValue(data)
highlightKeyword(editor)
showExample(viewMarkdown)
}

function loadmd(data) {
mdsource = data;
iscurrentfilemd = true;
var byClass = document.getElementsByClassName("strip-button");
for (var i = 0; i < byClass.length; i++) {
Expand Down Expand Up @@ -331,6 +334,36 @@ fetch('/_static/json/all_examples.json')
})
})

var copybutton = document.createElement('button')
copybutton.className = 'copy-button'
copybutton.style.top = '0rem'
copybutton.innerHTML = '<span class="clipboard-message">.edp copied to clipboard</span><i class="far fa-clone"></i>'
copybutton.onclick = function (e) {
const textarea = document.createElement('textarea')
if (iscurrentfilemd && viewMarkdown) {
copybutton.innerHTML = '<span class="clipboard-message">.md copied to clipboard</span><i class="far fa-clone"></i>'
textarea.value = mdsource
}
else {
copybutton.innerHTML = '<span class="clipboard-message">.edp copied to clipboard</span><i class="far fa-clone"></i>'
textarea.value = editor.getValue()
}
textarea.setAttribute('readonly', '')
textarea.style.position = 'absolute'
textarea.style.left = '-9999px'
document.body.appendChild(textarea)
textarea.select()
document.execCommand('copy')
document.body.removeChild(textarea)

this.children[0].classList.toggle('clipboard-message--active')
setTimeout(() => {
this.children[0].classList.remove('clipboard-message--active')
}, 2000)
}
const examplecopybutton = document.getElementById('ExampleCopyButton')
examplecopybutton.appendChild(copybutton)

var bs1 = new ButtonStrip({
id: 'buttonStrip1'
});
Expand Down
2 changes: 1 addition & 1 deletion source/_templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</ul>
</div>
<div id="exampleCode" spellcheck="false" style="display: none">
<h2><div id='ExampleLinkToGitHub'></div><div id='markdowntoggle'></div><!-- input type="checkbox" id="checkbox-markdown-toggle"/> <!-- <span id="exampleSave">Save as EDP</span> --></h2>
<h2><div id='ExampleLinkToGitHub'></div><div id='ExampleCopyButton'></div><div id='markdowntoggle'></div><!-- input type="checkbox" id="checkbox-markdown-toggle"/> <!-- <span id="exampleSave">Save as EDP</span> --></h2>
<div id="mdout"></div>
<textarea id="code"></textarea>
</div>
Expand Down

0 comments on commit fde94c3

Please sign in to comment.