-
Notifications
You must be signed in to change notification settings - Fork 96
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
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
30f5175
initial copy to clipboard functionality for the concept page
osma 23e2451
add translations for "Copy to clipboard" text. Fixes #926
osma d1115ff
add Cypress tests for copy to clipboard functionality
osma 07be644
remove copy to clipboard icon from search result page (not necessary)
osma 7cdc87f
avoid sonarqube warnings about returning Promise
osma 4ec2b98
more fun with async/await
osma 396932f
simplify JS code, avoiding async/await proliferation
osma 893e1f0
register copy to clipboard event handlers also after partial page loads
osma 404633e
make prefLabel and URI selectable using single click
osma 44e1a93
avoid wrapping the prefLabel copy button to the next line
osma 864c4af
better comments for Cypress tests
osma f5ee24c
add focus indicator for copy to clipboard buttons
osma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
document.addEventListener('loadConceptPage', registerCopyToClipboardEvents) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The things you learn..! -__- There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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) | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.