-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathDocumentTitle.ts
More file actions
97 lines (75 loc) · 2.72 KB
/
Copy pathDocumentTitle.ts
File metadata and controls
97 lines (75 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Widget, WidgetConfig } from "../ui/Widget";
import { RenderingContext } from "../ui/RenderingContext";
import { Instance } from "../ui/Instance";
import { StringProp } from "../ui/Prop";
export interface DocumentTitleConfig extends WidgetConfig {
/** Text value to be used for the document title. */
value?: StringProp;
/** Text value to be used for the document title. */
text?: StringProp;
/** Deprecated. Use `action: "append"` instead. */
append?: boolean;
/** How to combine the title with existing document title. Default is `append`. */
action?: "append" | "replace" | "prepend";
/** Separator used when appending or prepending to the title. Default is empty string. */
separator?: StringProp;
}
export class DocumentTitle extends Widget<DocumentTitleConfig> {
declare value?: StringProp;
declare text?: StringProp;
declare append?: boolean;
declare action?: "append" | "replace" | "prepend";
declare separator?: StringProp;
constructor(config?: DocumentTitleConfig) {
super(config);
}
init(): void {
if (this.value) this.text = this.value;
if (this.append) this.action = "append";
super.init();
}
declareData(...args: Record<string, unknown>[]): void {
super.declareData(...args, {
value: undefined,
text: undefined,
action: undefined,
separator: undefined,
});
}
explore(context: RenderingContext, instance: Instance): void {
if (!(context as any).documentTitle) {
(context as any).documentTitle = {
activeInstance: instance,
title: "",
};
}
let { data } = instance;
if (data.text) {
switch (data.action) {
case "append":
if ((context as any).documentTitle.title) (context as any).documentTitle.title += data.separator;
(context as any).documentTitle.title += data.text;
break;
case "prepend":
(context as any).documentTitle.title = data.text + data.separator + (context as any).documentTitle.title;
break;
default:
case "replace":
(context as any).documentTitle.title = data.text;
break;
}
}
super.explore(context, instance);
}
prepare(context: RenderingContext, instance: Instance): void {
if (typeof document == "undefined") return;
if ((context as any).documentTitle.activeInstance == instance)
document.title = (context as any).documentTitle.title;
}
render(): null {
return null;
}
}
DocumentTitle.prototype.action = "append";
DocumentTitle.prototype.separator = "";
Widget.alias("document-title", DocumentTitle);