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

Added a new signature pad element and styling for the new element #1576

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions src/js/control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import controlText from './text'
import controlTextarea from './textarea'
import controlTinymce from './textarea.tinymce'
import controlQuill from './textarea.quill'
import controlSignaturePad from './signaturePad'

export default {
controlAutocomplete,
Expand All @@ -20,4 +21,5 @@ export default {
controlTextarea,
controlTinymce,
controlQuill,
controlSignaturePad,
}
88 changes: 88 additions & 0 deletions src/js/control/signaturePad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import control from '../control'

/**
* SignaturePad class
* @extends control
*/
export default class controlSignaturePad extends control {
/**
* 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', { 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)

// Clear any existing drawing data
this.clearCanvas()

})
this.labelSpan = this.markup('span', this.config.label || 'Signature', { className: 'form-label' })

// Created a container div and wrap the canvas inside it
const container = this.markup('div', [this.canvas], { className: 'signature-container' })

return [this.labelSpan, container, this.clearButton]

}

/**
* Clear the canvas and any existing drawing data
*/
clearCanvas() {
const context = this.canvas.getContext('2d')

context.beginPath() // Start a new path to clear previous strokes

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 // Fixed height for the canvas

const context = this.canvas.getContext('2d')
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.canvas.addEventListener('mouseout', () => {
isDrawing = false
})
return evt
}
}
control.register('signaturePad', controlSignaturePad)
16 changes: 16 additions & 0 deletions src/sass/_controls.scss
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,19 @@
}
}
}
.signature-container {
border: 2px solid #000;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
}

.signature-pad {
//display: block;
Copy link
Owner

Choose a reason for hiding this comment

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

✂️

width: 100%;
height: 100%;
}
.signature-pad.custom-style {
border: 2px solid #000;
background-color: #f9f9f9;
}
17 changes: 17 additions & 0 deletions src/sass/form-render.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,21 @@
height: auto;
}
}
.signature-container {
border: 2px solid #000;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
}

.signature-pad {
//display: block;
Copy link
Owner

Choose a reason for hiding this comment

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

✂️

width: 100%;
height: 100%;
}
.signature-pad.custom-style {
border: 2px solid #000;
background-color: #f9f9f9;
}

}
Loading