Skip to content

Commit

Permalink
feat(app): update document title only in app context
Browse files Browse the repository at this point in the history
  • Loading branch information
christianliebel committed Sep 2, 2021
1 parent 43c8296 commit 3b29530
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
19 changes: 19 additions & 0 deletions custom-elements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": 2,
"tags": [
{
"name": "paint-app",
"description": "An open-source, Web Components-based remake of MS Paint using modern web capabilities.",
"properties": [],
"events": [
{
"name": "titlechange",
"description": "The titlechange event fires whenever the document's title changes, for example, when a document is saved with a new name, or a document was opened from the file system.",
"type": "{ title: string }"
}
],
"slots": [],
"cssProperties": []
}
]
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@
"workbox-cli": "^6.2.4"
},
"lint-staged": {
"{src,cypress}/**/*.{ts,js}": ["eslint --cache --fix", "prettier --write"],
"public/*.{css,html}": "prettier --write",
"{src,cypress}/**/*.{ts,js}": [
"eslint --cache --fix",
"prettier --write"
],
"public/*.{css,html,json}": "prettier --write",
"*.md": "prettier --write"
}
}
7 changes: 5 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, viewport-fit=cover"
/>
<meta name="theme-color" content="#000080" />
<meta
name="description"
Expand Down Expand Up @@ -33,7 +36,7 @@
property="og:description"
content="Open-Source, Web Components-based remake of MS Paint using modern web capabilities."
/>
<title>untitled - Paint</title>
<title>Paint</title>
<link rel="stylesheet" href="styles.css" />
<link rel="manifest" href="manifest.webmanifest" />
<link rel="shortcut icon" href="favicon.ico" />
Expand Down
18 changes: 17 additions & 1 deletion src/elements/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class App extends LitElement {
@state() drawingContext = DRAWING_CONTEXT;

private readonly beforeUnloadListener?: (event: BeforeUnloadEvent) => void;
private previousTitle = '';

static get styles(): CSSResultGroup {
return css`
Expand Down Expand Up @@ -212,7 +213,9 @@ export class App extends LitElement {
}

render(): TemplateResult {
document.title = `${this.drawingContext.document.title} - Paint`;
// TODO: Should not happen as a part of the render loop.
this.dispatchTitleChangeEvent();

return html`
<paint-menu-bar
.entries="${menus}"
Expand Down Expand Up @@ -252,6 +255,19 @@ export class App extends LitElement {
`;
}

private dispatchTitleChangeEvent(): void {
if (this.previousTitle !== this.drawingContext.document.title) {
this.previousTitle = this.drawingContext.document.title;
this.dispatchEvent(
new CustomEvent<{ title: string }>('titlechange', {
detail: { title: this.drawingContext.document.title },
composed: true,
bubbles: true,
}),
);
}
}

onBeforeUnload(event: BeforeUnloadEvent): void {
if (this.drawingContext.document.dirty) {
event.preventDefault();
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/bind-to-document-title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
document
.querySelector('paint-app')
?.addEventListener(
'titlechange' as any,
(evt: CustomEvent<{ title: string }>) =>
(document.title = `${evt.detail.title} - Paint`),
);
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import './elements';
import './helpers/bind-to-document-title';
import './helpers/register-sw';

0 comments on commit 3b29530

Please sign in to comment.