Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy to clipboard functionality for concept page #1590

Merged
merged 12 commits into from
Feb 1, 2024
Merged
32 changes: 15 additions & 17 deletions resource/css/skosmos.css
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ body {
margin: 0 5px;
}

#main-container.termpage .fa-copy {
color: var(--vocab-text);
font-size: 30px;
position: relative;
bottom: -3px;
}

#main-container.searchpage .fa-arrow-right, #main-container.searchpage-multi-vocab .fa-arrow-right {
color: var(--vocab-text);
font-size: 12px;
Expand All @@ -105,11 +98,6 @@ body {
text-align: center;
}

#main-container.searchpage .fa-copy, #main-container.searchpage-multi-vocab .fa-copy {
position: relative;
bottom: 5px;
}

.fa-magnifying-glass {
color: var(--search-button-text);
font-size: 22px;
Expand Down Expand Up @@ -703,15 +691,25 @@ body {
}

#concept-label h1 {
float: left;
display: inline;
}

.copy-clipboard {
border: none;
font-size: 20px;
#copy-preflabel .fa-copy {
color: var(--vocab-text);
font-size: 30px;
position: relative;
bottom: 10px;
left: 5px;
}

#copy-uri .fa-copy {
color: var(--vocab-text);
position: relative;
bottom: 3px;
left: -0.6rem;
}

.copy-clipboard:active, .copy-clipboard:focus, .copy-clipboard:active:focus {
.copy-clipboard, .copy-clipboard:active, .copy-clipboard:focus, .copy-clipboard:active:focus {
border: none;
}

Expand Down
30 changes: 30 additions & 0 deletions resource/js/copy-to-clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// function for copying the content from a specific element (by id) to the clipboard
function copyToClipboard (id) {
const copyElem = document.getElementById(id)
const sel = window.getSelection()
const range = document.createRange()
range.selectNodeContents(copyElem)
sel.removeAllRanges()
sel.addRange(range)

navigator.clipboard.writeText(copyElem.innerText).catch((err) =>
console.error('Failed to copy text to clipboard: ', err))
}

function registerCopyToClipboardEvents () {
const copyPrefElem = document.getElementById('copy-preflabel')
if (copyPrefElem) {
copyPrefElem.addEventListener('click', () => copyToClipboard('concept-preflabel'))
}

const copyUriElem = document.getElementById('copy-uri')
if (copyUriElem) {
copyUriElem.addEventListener('click', () => copyToClipboard('concept-uri'))
}
}

// register the copyToClipboard function as event an handler for the copy buttons
registerCopyToClipboardEvents()

// re-register the event handlers after partial page loads
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line fills me with dread. We should make sure all event handlers are re-registered collectively. Partial page loading as a specific test case hasn't been used as of yet. Should we have an issue about it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is a more general issue, but currently we don't have that many event handlers and AFAICT only the concept-mappings component needs to re-register its event handler after a partial page load, and it already does that.

document.addEventListener('loadConceptPage', registerCopyToClipboardEvents)
3 changes: 3 additions & 0 deletions resource/translations/skosmos_en.po
Original file line number Diff line number Diff line change
Expand Up @@ -879,3 +879,6 @@ msgstr "Information"

msgid "Breadcrumbs"
msgstr "Breadcrumbs"

msgid "Copy to clipboard"
msgstr "Copy to clipboard"
3 changes: 3 additions & 0 deletions resource/translations/skosmos_fi.po
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,6 @@ msgstr "Tietoja"

msgid "Breadcrumbs"
msgstr "Murupolut"

msgid "Copy to clipboard"
msgstr "Kopioi leikepöydälle"
3 changes: 3 additions & 0 deletions resource/translations/skosmos_sv.po
Original file line number Diff line number Diff line change
Expand Up @@ -887,3 +887,6 @@ msgstr "Information"

msgid "Breadcrumbs"
msgstr "Brödsmulor"

msgid "Copy to clipboard"
msgstr "Kopiera till urklipp"
14 changes: 10 additions & 4 deletions src/view/concept-card.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
<div class="row" id="concept-heading">
<div class="col-sm-4 px-0" id="concept-property-label">{{ "skos:prefLabel" | trans }}</div>
<div class="col-sm-8" id="concept-label">
<h1 class="mb-0">{{ concept.label }}</h1>
<button class="btn btn-default copy-clipboard" type="button"
<h1 id="concept-preflabel" class="mb-0 user-select-all">{{ concept.label }}</h1>
<button class="btn btn-default copy-clipboard px-0" type="button" id="copy-preflabel"
data-bs-toggle="tooltip" data-bs-placement="button" title="{{ 'Copy to clipboard' | trans }}">
<i class="fa-regular fa-copy"></i>
<i class="fa-regular fa-copy"></i>
</button>
</div>
</div>
Expand Down Expand Up @@ -87,7 +87,13 @@
{% endif %}
<div class="row property prop-uri">
<div class="col-sm-4 px-0 property-label"><h2>URI</h2></div>
<div class="col-sm-8" id="concept-uri"><a href="#">{{ concept.uri }}</a></div>
<div class="col-sm-8">
<span id="concept-uri" class="user-select-all">{{ concept.uri }}</span>
<button class="btn btn-default copy-clipboard" type="button" id="copy-uri"
data-bs-toggle="tooltip" data-bs-placement="button" title="{{ 'Copy to clipboard' | trans }}">
<i class="fa-regular fa-copy"></i>
</button>
</div>
</div>
<div class="row property prop-download">
<div class="col-sm-4 px-0 property-label">
Expand Down
5 changes: 4 additions & 1 deletion src/view/scripts.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const SKOSMOS = {
{%- if request.plugins.callbacks ~%}
"pluginCallbacks": [{% for function in request.plugins.callbacks %}{% if not loop.first %}, {% endif %}"{{ function }}"{% endfor %}],
{%- endif ~%}
"language_strings" :{ "fi": { "fi": "suomi",
"language_strings": { "fi": { "fi": "suomi",
"en": "englanti",
"se": "pohjoissaame",
"sv": "ruotsi",
Expand Down Expand Up @@ -73,3 +73,6 @@ const SKOSMOS = {
<script src="resource/js/tab-alpha.js"></script>
<script src="resource/js/tab-hierarchy.js"></script>
<script src="resource/js/vocab-search.js"></script>

<!-- Other (non-Vue) JS functionality -->
<script src="resource/js/copy-to-clipboard.js"></script>
1 change: 0 additions & 1 deletion src/view/search-results.inc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
<li class="list-group-item px-0 py-1">
<span class="uri-icon">URI</span>
<span class="search-result-uri">{{ concept.uri }}</span>
<i class="fa-regular fa-copy"></i> <!-- no copy to clipboard functionality yet -->
</li>
{%~ endif ~%}
</ul>
Expand Down
24 changes: 24 additions & 0 deletions tests/cypress/template/concept.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ describe('Concept page', () => {
// check the concept prefLabel
cy.get('#concept-heading h1').invoke('text').should('equal', 'music research')
})
it('concept preflabel can be copied to clipboard', () => {
cy.visit('/yso/en/page/p21685') // go to "music research" concept page

// click the copy to clipboard button next to the prefLabel
cy.get('#copy-preflabel').click()

// check that the clipboard now contains "music research"
// NOTE: This test may fail when running Cypress interactively in a browser.
// The reason is browser security policies for accessing the clipboard.
// If that happens, make sure the browser window has focus and re-run the test.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The things you learn..! -__-

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😁 Ideally the test would just be skipped when this happens. Not sure if that's easy to implement.

cy.window().its('navigator.clipboard').invoke('readText').then((result) => {}).should('equal', 'music research');
})
it('contains concept type', () => {
cy.visit('/yso/en/page/p21685') // go to "music research" concept page

Expand Down Expand Up @@ -219,6 +231,18 @@ describe('Concept page', () => {
// check the broader concept
cy.get('#concept-uri').invoke('text').should('equal', 'http://www.yso.fi/onto/yso/p21685')
})
it('concept URI can be copied to clipboard', () => {
cy.visit('/yso/en/page/p21685') // go to "music research" concept page

// click the copy to clipboard button next to the URI
cy.get('#copy-uri').click()

// check that the clipboard now contains "http://www.yso.fi/onto/yso/p21685"
// NOTE: This test may fail when running Cypress interactively in a browser.
// The reason is browser security policies for accessing the clipboard.
// If that happens, make sure the browser window has focus and re-run the test.
cy.window().its('navigator.clipboard').invoke('readText').then((result) => {}).should('equal', 'http://www.yso.fi/onto/yso/p21685');
})
it('contains created & modified times (English)', () => {
cy.visit('/yso/en/page/p21685') // go to "music research" concept page (English)

Expand Down
Loading