-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Added a new signature pad element and styling for the new element #1576
Open
keerthi-balaji
wants to merge
8
commits into
kevinchappell:master
Choose a base branch
from
keerthi-balaji:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d33611d
Added signature pad element and added styling for it before and after…
keerthi-balaji 73cbed6
Merge pull request #1 from keerthi-balaji/signaturePad
keerthi-balaji 4ed922c
Fixed the label, placed the clear button within the canvas, renamed t…
keerthi-balaji e0bb509
Added a method to stop signature pad from being dragged around prerende
keerthi-balaji aa4a6b1
Merge pull request #3 from kevinchappell/master
keerthi-balaji 91fa1e6
Merge pull request #2 from keerthi-balaji/signaturePad
keerthi-balaji e3efb04
Saved the signature to form data and created the tests
keerthi-balaji 01575d8
Merge pull request #5 from keerthi-balaji/SignaturePad
keerthi-balaji 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,145 @@ | ||
import control from '../control' | ||
|
||
/** | ||
* SignaturePad class | ||
* @extends control | ||
*/ | ||
export default class controlSignaturePad extends control { | ||
constructor(config) { | ||
super(config) | ||
this.userData = config.userData || null | ||
} | ||
|
||
/** | ||
* definition | ||
* @return {Object} | ||
*/ | ||
static get definition() { | ||
return { | ||
icon: '🖊️', | ||
i18n: { | ||
default: 'Signature Pad', | ||
}, | ||
} | ||
} | ||
|
||
/** | ||
* build a signature pad DOM element | ||
* @return {Object} DOM Element to be injected into the form. | ||
*/ | ||
build() { | ||
this.canvas = this.markup('canvas', null, { className: 'signature-pad' }) | ||
this.clearButton = this.markup('button', 'Clear Signature', { type: 'button', className: 'clear-button' }) | ||
this.clearButton.addEventListener('click', () => { | ||
const context = this.canvas.getContext('2d') | ||
context.clearRect(0, 0, this.canvas.width, this.canvas.height) | ||
this.clearCanvas() | ||
}) | ||
|
||
this.labelSpan = this.markup('span', this.config.label, { className: 'form-label' }) | ||
|
||
const container = this.markup('div', [this.canvas, this.clearButton], { className: 'signature-container' }) | ||
|
||
if (this.userData) { | ||
this.loadSignature(this.userData) | ||
} | ||
|
||
return [this.labelSpan, container] | ||
} | ||
|
||
/** | ||
* Clear the canvas and any existing drawing data | ||
*/ | ||
clearCanvas() { | ||
const context = this.canvas.getContext('2d') | ||
if (context) { | ||
context.beginPath() | ||
context.clearRect(0, 0, this.canvas.width, this.canvas.height) | ||
} | ||
} | ||
|
||
/** | ||
* onRender callback | ||
* @param {Object} evt Event object | ||
*/ | ||
onRender(evt) { | ||
this.canvas.width = this.canvas.parentElement.offsetWidth | ||
this.canvas.height = 150 | ||
|
||
const context = this.canvas.getContext('2d') | ||
if (context) { | ||
context.strokeStyle = '#000' | ||
context.lineWidth = 2 | ||
|
||
let isDrawing = false | ||
this.canvas.addEventListener('mousedown', e => { | ||
isDrawing = true | ||
context.moveTo(e.offsetX, e.offsetY) | ||
}) | ||
this.canvas.addEventListener('mousemove', e => { | ||
if (isDrawing) { | ||
context.lineTo(e.offsetX, e.offsetY) | ||
context.stroke() | ||
} | ||
}) | ||
this.canvas.addEventListener('mouseup', () => { | ||
isDrawing = false | ||
this.saveSignature() | ||
}) | ||
this.canvas.addEventListener('mouseout', () => { | ||
isDrawing = false | ||
}) | ||
|
||
// Load saved user data if available | ||
if (this.userData) { | ||
this.loadSignature(this.userData) | ||
} | ||
} | ||
return evt | ||
} | ||
|
||
|
||
/** | ||
* Load signature data from userData | ||
* @param {Array} userData | ||
*/ | ||
loadSignature(userData) { | ||
const context = this.canvas.getContext('2d') | ||
const image = new Image() | ||
image.onload = () => { | ||
context.drawImage(image, 0, 0) | ||
} | ||
image.src = JSON.parse(userData[0]) | ||
} | ||
|
||
/** | ||
* Save the signature data to user data | ||
*/ | ||
saveSignature() { | ||
const dataUrl = this.canvas.toDataURL('image/png') | ||
this.config.userData = [dataUrl] | ||
console.log('save signature:', this.config.userData) | ||
} | ||
/** | ||
* extend the default events to add a prerender for the signature pad | ||
* @param {string} eventType | ||
* @return {Function} prerender function | ||
*/ | ||
on(eventType) { | ||
if (eventType === 'prerender' && this.preview) { | ||
return element => { | ||
if (this.field) { | ||
element = this.field | ||
} | ||
|
||
$(element).on('mousedown', e => { | ||
e.stopPropagation() | ||
}) | ||
} | ||
} | ||
return super.on(eventType) | ||
} | ||
|
||
} | ||
|
||
control.register('signaturePad', controlSignaturePad) |
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 |
---|---|---|
|
@@ -46,4 +46,28 @@ | |
height: auto; | ||
} | ||
} | ||
.signature-container { | ||
border: 2px solid #000; | ||
padding: 10px; | ||
margin-bottom: 10px; | ||
border-radius: 5px; | ||
} | ||
|
||
.signature-pad { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
.signature-pad.custom-style { | ||
border: 2px solid #000; | ||
background-color: #f9f9f9; | ||
} | ||
.clear-button { | ||
top: 10px; /* Adjust as needed */ | ||
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. this looks repeated from above. maybe signature style can be its own file that is imported where needed. |
||
right: 10px; /* Adjust as needed */ | ||
right: 10px; | ||
background-color: #f8f8f8; | ||
border: 1px solid #ccc; | ||
padding: 5px; | ||
cursor: pointer; | ||
} | ||
} |
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,100 @@ | ||
import controlSignaturePad from '../../src/js/control/signaturePad.js'; | ||
|
||
describe('controlSignaturePad', () => { | ||
let controlInstance; | ||
|
||
beforeEach(() => { | ||
// Mocking the necessary config and functions | ||
const config = { label: 'Signature', userData: null }; | ||
controlInstance = new controlSignaturePad(config); | ||
document.body.innerHTML = '<div id="root"></div>'; | ||
const builtElements = controlInstance.build(); | ||
document.getElementById('root').appendChild(builtElements[1]); | ||
controlInstance.onRender({}); | ||
}); | ||
|
||
test('should create a canvas and clear button', () => { | ||
const canvas = document.querySelector('canvas.signature-pad'); | ||
const clearButton = document.querySelector('button.clear-button'); | ||
|
||
expect(canvas).not.toBeNull(); | ||
expect(clearButton).not.toBeNull(); | ||
expect(clearButton.textContent).toBe('Clear Signature'); | ||
}); | ||
|
||
test('should clear the canvas when clear button is clicked', () => { | ||
const canvas = document.querySelector('canvas.signature-pad'); | ||
expect(canvas).not.toBeNull(); | ||
|
||
// Set the canvas dimensions | ||
canvas.width = 200; | ||
canvas.height = 150; | ||
|
||
const context = canvas.getContext('2d'); | ||
expect(context).not.toBeNull(); | ||
|
||
// Draw something on the canvas | ||
context.fillRect(0, 0, canvas.width, canvas.height); | ||
expect(context.getImageData(0, 0, 1, 1).data[3]).toBe(255); // Check if pixel is not empty | ||
|
||
// Click the clear button | ||
const clearButton = document.querySelector('button.clear-button'); | ||
clearButton.click(); | ||
|
||
// Check if the canvas is cleared | ||
expect(context.getImageData(0, 0, 1, 1).data[3]).toBe(0); // Check if pixel is empty | ||
}); | ||
|
||
test('should save signature data on mouseup', () => { | ||
const canvas = document.querySelector('canvas.signature-pad'); | ||
expect(canvas).not.toBeNull(); | ||
|
||
// Set the canvas dimensions | ||
canvas.width = 200; | ||
canvas.height = 150; | ||
|
||
const context = canvas.getContext('2d'); | ||
expect(context).not.toBeNull(); | ||
|
||
// Simulate drawing on the canvas | ||
const mouseDownEvent = new MouseEvent('mousedown', { offsetX: 10, offsetY: 10 }); | ||
const mouseMoveEvent = new MouseEvent('mousemove', { offsetX: 20, offsetY: 20 }); | ||
const mouseUpEvent = new MouseEvent('mouseup'); | ||
|
||
canvas.dispatchEvent(mouseDownEvent); | ||
canvas.dispatchEvent(mouseMoveEvent); | ||
canvas.dispatchEvent(mouseUpEvent); | ||
|
||
// Check if saveSignature was called and userData is updated | ||
expect(controlInstance.config.userData).not.toBeNull(); | ||
expect(controlInstance.config.userData[0]).toContain('data:image/png;base64,'); | ||
}); | ||
|
||
test('should load saved signature data on render', () => { | ||
const canvas = document.querySelector('canvas.signature-pad'); | ||
expect(canvas).not.toBeNull(); | ||
|
||
// Set the canvas dimensions | ||
canvas.width = 200; | ||
canvas.height = 150; | ||
|
||
const context = canvas.getContext('2d'); | ||
expect(context).not.toBeNull(); | ||
|
||
// Mock user data | ||
const userData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA'; | ||
controlInstance.config.userData = [userData]; | ||
|
||
// Simulate rendering | ||
controlInstance.onRender({}); | ||
|
||
// Check if the canvas is updated with the saved data | ||
const image = new Image(); | ||
image.src = userData; | ||
image.onload = () => { | ||
context.drawImage(image, 0, 0); | ||
expect(context.getImageData(0, 0, 1, 1).data[3]).toBeGreaterThan(0); // Check if pixel is not empty | ||
}; | ||
}); | ||
|
||
}); |
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.
✂️ these comment, looks ai generated.