-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.ts
249 lines (201 loc) · 4.75 KB
/
lib.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
export const DEFAULT_STYLES = `
* {
user-select: none;
}
:host {
position: fixed;
z-index: 999;
right: 5vw;
bottom: 5vw;
opacity: 50%;
filter: invert(50%);
font-size: 16px;
/* System Fonts as used by Medium and WordPress, copy-pasted from https://css-tricks.com/snippets/css/system-font-stack/ */
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
text-align: start;
}
::part(title) {
font-size: 1.5em;
}
::part(detail) {
font-size: .9em;
max-width: 40ch;
}
a {
color: inherit;
text-decoration: underline;
}
a:hover {
text-decoration: underline dashed;
}
a:visited {
color: inherit;
}
`
const ATTR_LC_MAP = {
'gototext': 'gotoText',
'gotolink': 'gotoLink',
'titlehtml': 'titleHtml',
'detailhtml': 'detailHtml',
}
export const OPTION_KEYS = ['name', 'gotoText', 'gotoLink', 'titleHtml', 'detailHtml']
const FALSY_STRINGS = ['false', 'null', 'undefined']
const TAG_NAME = 'activate-web'
export interface ActivateOptions {
name: string,
gotoText: string,
gotoLink: string,
titleHtml: string,
detailHtml: string,
}
export const DEFAULT_OPTIONS: ActivateOptions = {
name: 'Open Web',
gotoText: 'your favorite editor',
gotoLink: '',
titleHtml: '',
detailHtml: '',
}
export default class ActivateWebElement extends HTMLElement {
#options: ActivateOptions = Object.create(DEFAULT_OPTIONS)
titleEl: HTMLElement
detailEl: HTMLElement
mutationObserver: MutationObserver
constructor() {
super()
this.titleEl = document.createElement('div')
this.titleEl.setAttribute('part', 'title')
this.detailEl = document.createElement('div')
this.detailEl.setAttribute('part', 'detail')
const defaultStyleEl = document.createElement('style')
defaultStyleEl.textContent = DEFAULT_STYLES
this.attachShadow({
mode: 'open',
})
this.shadowRoot?.append(defaultStyleEl, this.titleEl, this.detailEl)
this.mutationObserver = new MutationObserver(() => {
this.updateTitle()
this.updateDetail()
})
}
get name() {
return this.#options.name
}
get gotoText() {
return this.#options.gotoText
}
get gotoLink() {
return this.#options.gotoLink
}
get titleHtml() {
return this.#options.titleHtml
}
get detailHtml() {
return this.#options.detailHtml
}
set name(value: string) {
this.#options.name = value
this.updateTitle()
this.updateDetail()
}
set gotoText(value: string) {
this.#options.gotoText = value
this.updateDetail()
}
set gotoLink(value: string) {
this.#options.gotoLink = value
this.updateDetail()
}
set titleHtml(value: string) {
this.#options.titleHtml = value
this.updateTitle()
}
set detailHtml(value: string) {
this.#options.detailHtml = value
this.updateDetail()
}
updateAttribute(attr: string, value: string) {
if(attr in ATTR_LC_MAP) {
// @ts-ignore
attr = ATTR_LC_MAP[attr]
}
if(!OPTION_KEYS.includes(attr)) {
return
}
switch(attr) {
case 'titleHtml':
case 'detailHtml':
if(FALSY_STRINGS.includes(value)) {
value = ''
}
}
if(FALSY_STRINGS.includes(value)) {
return
}
// @ts-ignore
this.#options[attr] = value
switch(attr) {
case 'name':
case 'titleHtml':
case 'detailHtml':
this.updateTitle()
case 'gotoText':
case 'gotoLink':
this.updateDetail()
}
}
updateTitle() {
this.titleEl.innerHTML = this.titleHtml || this._titleHtml()
}
updateDetail() {
this.detailEl.innerHTML = this.detailHtml || this._detailHtml()
}
_titleHtml() {
return `Activate ${this.#options.name}`
}
_detailHtml() {
return `Go to ${this._makeGoto()} to activate ${this.#options.name}.`
}
_makeGoto() {
const text = this.#options.gotoText
const link = this.#options.gotoLink
return link ? `<a href="${link}">${text}</a>` : text
}
static get observedAttributes() {
return Object.keys(ATTR_LC_MAP)
}
connectedCallback() {
let attr: keyof ActivateOptions
for (attr in this.#options) {
const value = this.getAttribute(attr)
if(value && value !== this.#options[attr] && value !== 'false') {
this.#options[attr] = value
}
}
this.updateTitle()
this.updateDetail()
this.mutationObserver.observe(this, {
attributeFilter: ActivateWebElement.observedAttributes
})
}
disconnectedCallback() {
this.mutationObserver.disconnect()
}
attributeChangedCallback(attr: string, _: string, curr: string) {
this.updateAttribute(attr, curr)
}
}
declare global {
interface Window {
ActivateWebElement: typeof ActivateWebElement
}
interface HTMLElementTagNameMap {
[TAG_NAME]: ActivateWebElement
}
}
export function registerCustomElement() {
if(!window.customElements.get(TAG_NAME)) {
window.ActivateWebElement = ActivateWebElement
window.customElements.define(TAG_NAME, ActivateWebElement)
}
}
registerCustomElement()