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

web: Update based on latest client script changes #1153

Merged
merged 4 commits into from
Feb 26, 2023
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
4 changes: 2 additions & 2 deletions web/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 web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "giscus",
"version": "1.2.6",
"version": "1.2.7",
"type": "module",
"main": "dist/giscus.mjs",
"module": "dist/giscus.mjs",
Expand Down
27 changes: 20 additions & 7 deletions web/src/giscus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class GiscusWidget extends LitElement {
localStorage.setItem(this.GISCUS_SESSION_KEY, JSON.stringify(urlSession));
this.__session = urlSession;
url.searchParams.delete('giscus');
url.hash = '';
history.replaceState(undefined, document.title, url.toString());
return;
}
Expand All @@ -160,6 +161,12 @@ export class GiscusWidget extends LitElement {
}
}

private signOut() {
localStorage.removeItem(this.GISCUS_SESSION_KEY);
this.__session = '';
this.update(new Map());
}

private handleMessageEvent(event: MessageEvent) {
if (event.origin !== this.host) return;

Expand All @@ -170,6 +177,12 @@ export class GiscusWidget extends LitElement {
this.iframeRef.style.height = `${data.giscus.resizeHeight}px`;
}

if (data.giscus.signOut) {
console.log(`[giscus] User has logged out. Session has been cleared.`);
this.signOut();
return;
}

if (!data.giscus.error) return;

const message: string = data.giscus.error;
Expand All @@ -181,11 +194,8 @@ export class GiscusWidget extends LitElement {
) {
// Might be because token is expired or other causes
if (localStorage.getItem(this.GISCUS_SESSION_KEY) !== null) {
localStorage.removeItem(this.GISCUS_SESSION_KEY);
this.__session = '';
console.warn(`${this._formatError(message)} Session has been cleared.`);
// Reload iframe
this.update(new Map());
this.signOut();
return;
}

Expand All @@ -207,6 +217,7 @@ export class GiscusWidget extends LitElement {
}

private sendMessage<T>(message: T) {
if (!this.iframeRef || !this.iframeRef.src.startsWith(this.host)) return;
this.iframeRef?.contentWindow?.postMessage({ giscus: message }, this.host);
}

Expand All @@ -232,9 +243,10 @@ export class GiscusWidget extends LitElement {
}

firstUpdated() {
this.iframeRef?.addEventListener('load', () =>
this.iframeRef?.classList.remove('loading')
);
this.iframeRef?.addEventListener('load', () => {
this.iframeRef?.classList.remove('loading');
this.updateConfig();
});
}

requestUpdate(
Expand Down Expand Up @@ -263,6 +275,7 @@ export class GiscusWidget extends LitElement {
private _getCleanedUrl() {
const url = new URL(location.href);
url.searchParams.delete('giscus');
url.hash = '';
return url;
}

Expand Down