-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9576 from alphagov/content-modelling/631-add-copy…
…-code-link (631) Add copy code link
- Loading branch information
Showing
15 changed files
with
223 additions
and
8 deletions.
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
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
1 change: 1 addition & 0 deletions
1
...engines/content_block_manager/app/assets/javascripts/content_block_manager/application.js
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 @@ | ||
//= require ./modules/copy-embed-code |
81 changes: 81 additions & 0 deletions
81
...ent_block_manager/app/assets/javascripts/content_block_manager/modules/copy-embed-code.js
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,81 @@ | ||
'use strict' | ||
window.GOVUK = window.GOVUK || {} | ||
window.GOVUK.Modules = window.GOVUK.Modules || {} | ||
;(function (Modules) { | ||
function CopyEmbedCode(module) { | ||
this.module = module | ||
this.copyLink = this.createLink.bind(this)() | ||
} | ||
|
||
CopyEmbedCode.prototype.init = function () { | ||
const dd = document.createElement('dd') | ||
dd.classList.add('govuk-summary-list__actions') | ||
dd.append(this.copyLink) | ||
|
||
this.module.append(dd) | ||
} | ||
|
||
CopyEmbedCode.prototype.createLink = function () { | ||
const copyLink = document.createElement('a') | ||
copyLink.classList.add('govuk-link') | ||
copyLink.classList.add('govuk-link__copy-link') | ||
copyLink.setAttribute('href', '#') | ||
copyLink.setAttribute('role', 'button') | ||
copyLink.textContent = 'Copy code' | ||
copyLink.addEventListener('click', this.copyCode.bind(this)) | ||
// Handle when a keyboard user highlights the link and clicks return | ||
copyLink.addEventListener( | ||
'keydown', | ||
function (e) { | ||
if (e.keyCode === 13) { | ||
this.copyCode.bind(this) | ||
} | ||
}.bind(this) | ||
) | ||
|
||
return copyLink | ||
} | ||
|
||
CopyEmbedCode.prototype.copyCode = function (e) { | ||
e.preventDefault() | ||
|
||
const embedCode = this.module.dataset.embedCode | ||
this.writeToClipboard(embedCode).then(this.copySuccess.bind(this)) | ||
} | ||
|
||
CopyEmbedCode.prototype.copySuccess = function () { | ||
const originalText = this.copyLink.textContent | ||
this.copyLink.textContent = 'Code copied' | ||
this.copyLink.focus() | ||
|
||
setTimeout(this.restoreText.bind(this, originalText), 2000) | ||
} | ||
|
||
CopyEmbedCode.prototype.restoreText = function (originalText) { | ||
this.copyLink.textContent = originalText | ||
} | ||
|
||
// This is a fallback for browsers that do not support the async clipboard API | ||
CopyEmbedCode.prototype.writeToClipboard = function (embedCode) { | ||
return new Promise(function (resolve) { | ||
// Create a textarea element with the embed code | ||
const textArea = document.createElement('textarea') | ||
textArea.value = embedCode | ||
|
||
document.body.appendChild(textArea) | ||
|
||
// Select the text in the textarea | ||
textArea.select() | ||
|
||
// Copy the selected text | ||
document.execCommand('copy') | ||
|
||
// Remove our textarea | ||
document.body.removeChild(textArea) | ||
|
||
resolve() | ||
}) | ||
} | ||
|
||
Modules.CopyEmbedCode = CopyEmbedCode | ||
})(window.GOVUK.Modules) |
2 changes: 1 addition & 1 deletion
2
...onents/content_block_manager/content_block/document/index/summary_card_component.html.erb
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ def rows | |
{ | ||
key: item[:field], | ||
value: item[:value], | ||
data: item[:data], | ||
} | ||
end | ||
end | ||
|
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
72 changes: 72 additions & 0 deletions
72
lib/engines/content_block_manager/spec/content_block_manager/copy-code.spec.js
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,72 @@ | ||
describe('GOVUK.Modules.CopyEmbedCode', function () { | ||
let fixture, embedCode, copyEmbedCode, copyLink, fakeTextarea | ||
|
||
beforeEach(function () { | ||
embedCode = 'something' | ||
fixture = document.createElement('div') | ||
fixture.setAttribute('data-embed-code', embedCode) | ||
fixture.innerHTML = ` | ||
<dt class="govuk-summary-list__key">Embed code</dt> | ||
<dd class="govuk-summary-list__value">${embedCode}</dd> | ||
` | ||
document.body.append(fixture) | ||
|
||
copyEmbedCode = new GOVUK.Modules.CopyEmbedCode(fixture) | ||
copyEmbedCode.init() | ||
|
||
copyLink = document.querySelector('.govuk-link__copy-link') | ||
|
||
fakeTextarea = document.createElement('textarea') | ||
spyOn(document, 'createElement').and.returnValue(fakeTextarea) | ||
}) | ||
|
||
afterEach(function () { | ||
fixture.innerHTML = '' | ||
}) | ||
|
||
it('should add a link to copy the embed code', function () { | ||
expect(copyLink).toBeTruthy() | ||
expect(copyLink.textContent).toBe('Copy code') | ||
}) | ||
|
||
it('should create and populate a textarea', function () { | ||
window.GOVUK.triggerEvent(copyLink, 'click') | ||
|
||
expect(fakeTextarea.value).toEqual(embedCode) | ||
}) | ||
|
||
it('should select the text in the textarea and run the copy command', function () { | ||
const copySpy = spyOn(document, 'execCommand') | ||
const selectSpy = spyOn(fakeTextarea, 'select') | ||
|
||
window.GOVUK.triggerEvent(copyLink, 'click') | ||
|
||
expect(selectSpy).toHaveBeenCalled() | ||
expect(copySpy).toHaveBeenCalled() | ||
}) | ||
|
||
it('should add and remove the textarea', function () { | ||
const appendSpy = spyOn(document.body, 'appendChild') | ||
const removeSpy = spyOn(document.body, 'removeChild') | ||
|
||
window.GOVUK.triggerEvent(copyLink, 'click') | ||
|
||
expect(appendSpy).toHaveBeenCalled() | ||
expect(removeSpy).toHaveBeenCalled() | ||
}) | ||
|
||
it('changes and restores the link text', async function () { | ||
jasmine.clock().install() | ||
|
||
await window.GOVUK.triggerEvent(copyLink, 'click') | ||
|
||
copyLink = document.querySelector('.govuk-link__copy-link') | ||
|
||
expect(copyLink.textContent).toEqual('Code copied') | ||
jasmine.clock().tick(2000) | ||
|
||
expect(copyLink.textContent).toEqual('Copy code') | ||
|
||
jasmine.clock().uninstall() | ||
}) | ||
}) |
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 |
---|---|---|
|
@@ -4,9 +4,10 @@ | |
"application-*.js", | ||
"all-*.js" | ||
], | ||
"specDir": "spec/javascripts", | ||
"specDir": ".", | ||
"specFiles": [ | ||
"**/*[sS]pec.js" | ||
"spec/javascripts/**/*[sS]pec.js", | ||
"lib/engines/**/spec/**/*[sS]pec.js" | ||
], | ||
"helpers": [ | ||
"https://unpkg.com/[email protected]/lib/mock-ajax.js", | ||
|