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

fix: 🐛 Cannot set property src of #<OpenStoriesElement> which has only a getter #14

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Changes from 3 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
39 changes: 33 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ class OpenStoriesElement extends HTMLElement {
open: boolean = false
goToBinding: () => void
items: OpenStoriesFeed["items"] = []
_src: string
_duration: number

constructor() {
super()
Expand Down Expand Up @@ -483,6 +485,14 @@ class OpenStoriesElement extends HTMLElement {
this.link = this.root.querySelector('a#link')!
this.time = this.root.querySelector('#time')!
this.goToBinding = this.goTo.bind(this, 1)

this._src = this.hasAttribute("src")
? this.formatSrc(this.getAttribute("src"))
: "";

this._duration = this.hasAttribute("duration")
? Number(this.getAttribute("duration"))
: 5;
}

get isHighlight() {
Expand Down Expand Up @@ -525,7 +535,7 @@ class OpenStoriesElement extends HTMLElement {
this.button.click()
})

const src = this.getAttribute('src')
const src = this.src
if (src) this.fetchData(src)

const style = document.createElement('style')
Expand All @@ -540,12 +550,20 @@ class OpenStoriesElement extends HTMLElement {
})
}

get src() {
return this.hasAttribute('src') ? new URL(this.getAttribute('src') || '', location.href) : ''
set src(path: string) {
this._src = this.formatSrc(path);
}

get src(): string {
return this._src;
}

get duration() {
return this.hasAttribute('duration') ? Number(this.getAttribute('duration')) : 5
set duration(value: number) {
this._duration = Number(value);
}

get duration(): number {
return this._duration;
}

async sendHeart() {
Expand Down Expand Up @@ -696,6 +714,15 @@ class OpenStoriesElement extends HTMLElement {
this.setIndexToUnread()
}

/**
* Format a path to a valid URL.
* @param path - The path to format.
* @returns - The formatted path.
*/
formatSrc(path: string | null): string {
return new URL(path || "", location.href).toString()
}

setIndexToUnread() {
if (this.isHighlight) return false

Expand Down Expand Up @@ -834,7 +861,7 @@ class OpenStoriesElement extends HTMLElement {
}

get viewedKey() {
return new URL(this.getAttribute('src')!, location.origin).toString()
return this.src
}

get lazyLoad() {
Expand Down