-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Create adapter class for EventTarget so that it would work in older iOS. #581
Conversation
It would check whether it could create EventTarget object. If failed, then it use document object as its EventTarget object. This is to support iOS 13 and older.
Can you write a test for this to make sure |
beginStroke, beforeUpdateStroke, afterUpdateStroke, endStroke events are tested.
Added an option to use document as EventTarget Also added test cases where using document as EventTarget.
Added two commits. 11286e4 contains unit tests that should have been added in PR #567, but for some reason those aren't. 40965b7 added a new option 'documentAsEventTarget' and unit tests to see whether the SignaturePad events still works if using document as EventTarget. I hope these suffices for this to be merged. |
src/signature_pad.ts
Outdated
addEventListener( | ||
type: string, | ||
listener: EventListenerOrEventListenerObject | null, | ||
options?: boolean | AddEventListenerOptions, | ||
): void { | ||
this._et.addEventListener(type, listener, options); | ||
} | ||
|
||
dispatchEvent(event: Event): boolean { | ||
return this._et.dispatchEvent(event); | ||
} | ||
|
||
removeEventListener( | ||
type: string, | ||
callback: EventListenerOrEventListenerObject | null, | ||
options?: boolean | EventListenerOptions, | ||
): void { | ||
return this._et.removeEventListener(type, callback, options); | ||
} | ||
|
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.
It looks like these methods are copied from SignatureEventTarget
wouldn't it just be easier for SignaturePad
to extend SignatureEventTarget
?
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.
yes, you're right. Using composition over inheritance here won't get better advantage. There wouldn't be other EventTarget
behaviour, would it? However when it does need it or this class needs to inherit other class, then perhaps we could implements the SignatureEventTarget
, instead of extends 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.
ah yeah, i'll change this in my next commit.
src/signature_pad.ts
Outdated
@@ -37,13 +37,57 @@ export interface Options extends Partial<PointGroupOptions> { | |||
velocityFilterWeight?: number; | |||
backgroundColor?: string; | |||
throttle?: number; | |||
documentAsEventTarget?: boolean; |
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 don't think we should pollute the options object with items used for tests.
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'm happy to remove this in my next commit. However, it wouldn't be able to be tested using document as EventTarget.
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.
You should be able to do something like signpad['_et'] = document
to force it to be a document in the tests.
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.
Good tip, will try it right away.
src/signature_pad.ts
Outdated
} | ||
|
||
export interface PointGroup extends PointGroupOptions { | ||
points: BasicPoint[]; | ||
} | ||
|
||
export default class SignaturePad extends EventTarget { | ||
class SignatureEventTarget implements EventTarget { |
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 class should move to it's own file.
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.
noted. i'm happy to change this in my next commit.
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.
hey since you're currently online, do you prefer 'signature-event-target.ts' or 'signature_event_target.ts' ? What's the convention in this project?
- remove documentAsEventTarget option - move SignatureEventTarget class to another file - change SignaturePad to inherit SignatureEventTarget instead of compose it.
tests/signature_pad.test.ts
Outdated
[{ useDocument: true }, { useDocument: false }].forEach((param) => { | ||
describe(`use document as EventTarget=${param.useDocument}.`, () => { | ||
beforeEach(() => { | ||
signpad['_et'] = document; |
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.
signpad['_et'] = document; | |
if (param.useDocument) { | |
signpad['_et'] = document; | |
} |
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.
ah thanks for reminding me about this. But I think I will keep implement like this, but without test parameters. Because testing with EventTarget object has been done at previous test case (above). I will provide a new commit.
@UziTech Thanks for approving this PR. So are we (am i) done here? what's the next step? |
Looks good to me we will try to get out a new version with this fix soon. |
src/signature_event_target.ts
Outdated
try { | ||
this._et = new EventTarget(); | ||
} catch (error) { | ||
console.warn('EventTarget object not supported, use document instead.'); |
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.
Hi! First of all, thank you so much for creating this PR!
I've got just one small issue - I'm not sure about polluting people's console with such warnings. Is it actually something that they should be aware of or can act upon? The warning says "use document", but it looks like we're doing that for them automatically already.
Maybe it would be also helpful to add a comment here that it's mostly to support Safari 13.x? Might make it easier to remove it if we ever decide to stop supporting this version.
And replace it with comments.
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.
Thank you so much!
🎉 This PR is included in version 4.0.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
It would check whether it could create EventTarget object. If failed,
then it use document object as its EventTarget object.
This is to support iOS 13 and older version.
If this needs anything else, please don't hesitate to let me know.