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

Update ts: alternative to #5 #6

Merged
merged 2 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions scripts-base/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "manGoweb <[email protected]> (https://mangoweb.cz)",
"license": "MIT",
"devDependencies": {
"typescript": "^3.1.6"
"typescript": "^3.5.1"
},
"prepare": "npm run build",
"publishConfig": {
Expand Down
13 changes: 9 additions & 4 deletions scripts-base/src/components/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,27 @@ export class Component<D, E extends HTMLElement = HTMLElement> {
// EventListenerSpec
const [type, callback] = listenersSpec

this.el.addEventListener(type, callback.bind(this), false)
this.el.addEventListener<typeof type>(type, callback.bind(this) as EventListenerCallback<typeof type>, false)
} else {
// DelegateEventListenerSpec
const [type, delegateSelector, callback] = listenersSpec

this.el.addEventListener(
type,
(e: Event) => {
(e: HTMLElementEventMap[typeof type]) => {
let target = e.target

while (target && target instanceof HTMLElement && target !== this.el) {
if (matchesSelector(target, delegateSelector)) {
const delegateEvent: any = e
const delegateEvent: HTMLElementEventMap[typeof type] & {
delegateTarget?: HTMLElement
} = e
delegateEvent.delegateTarget = target

return callback.call(this, delegateEvent)
return (callback as DelegateEventListenerCallback<typeof type>).call(
this,
delegateEvent as DelegateEvent<typeof type>
)
}

target = target.parentElement
Expand Down
3 changes: 3 additions & 0 deletions scripts-base/src/components/InView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export class InView extends Component<InViewData> {
(entries) => {
//callback
entries.forEach((entry) => {
if (!entry.rootBounds) {
return
}
const target = entry.target
const intersectionRatio = entry.intersectionRatio
this._updateState(
Expand Down
11 changes: 6 additions & 5 deletions scripts-base/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ type DelegateEvent<E extends BubblingEventType> = HTMLElementEventMap[E] & {
delegateTarget: HTMLElement
}

type DelegateEventListenerCallback<E extends BubblingEventType> = (event: DelegateEvent<E>) => void

type DelegateEventListenerSpec<E extends BubblingEventType> = [
E,
string,
(event: DelegateEvent<E>) => void
DelegateEventListenerCallback<E>
]

type EventListenerSpec<E extends keyof HTMLElementEventMap> = [
E,
(event: HTMLElementEventMap[E]) => void
]
type EventListenerCallback<E extends keyof HTMLElementEventMap> = (event: HTMLElementEventMap[E]) => void

type EventListenerSpec<E extends keyof HTMLElementEventMap> = [E, EventListenerCallback<E>]

type EventListeners = Array<
| { [E in keyof HTMLElementEventMap]: EventListenerSpec<E> }[keyof HTMLElementEventMap]
Expand Down
8 changes: 3 additions & 5 deletions scripts-base/src/utils/inject.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const head = document.head
const noop = () => {}

type ScriptElementConfig = {
[P in Exclude<keyof HTMLScriptElement, keyof HTMLElement>]?: HTMLScriptElement[P]
}
type ScriptElementConfig = Partial<Omit<HTMLScriptElement, keyof HTMLElement>>
type ScriptConfig = ScriptElementConfig & { content: string }

export const inject = (
Expand All @@ -30,8 +28,8 @@ export const inject = (
} else {
const keyCast = key as keyof ScriptElementConfig

if (options[keyCast]) {
script[keyCast] = options[keyCast]!
if (options[keyCast] !== undefined) {
(script as any)[keyCast] = options[keyCast]
}
}
}
Expand Down