This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
/
location.js
614 lines (548 loc) · 15.1 KB
/
location.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/* globals customElements */
import { ipcRenderer } from 'electron'
import { LitElement, html, css } from '../../vendor/lit-element/lit-element'
import { classMap } from '../../vendor/lit-element/lit-html/directives/class-map'
import { findWordBoundary } from 'pauls-word-boundary'
import prettyHash from 'pretty-hash'
import * as bg from '../bg-process-rpc'
import buttonResetCSS from './button-reset.css'
import './site-info'
const isDatHashRegex = /^[a-z0-9]{64}/i
class NavbarLocation extends LitElement {
static get properties () {
return {
activeTabIndex: {type: Number},
url: {type: String},
title: {type: String},
peers: {type: Number},
zoom: {type: Number},
loadError: {type: Object},
donateLinkHref: {type: String, attribute: 'donate-link-href'},
localPath: {type: String, attribute: 'local-path'},
availableAlternative: {type: String, attribute: 'available-alternative'},
isLiveReloading: {type: Boolean, attribute: 'is-live-reloading'},
isLocalPathMenuOpen: {type: Boolean},
isDonateMenuOpen: {type: Boolean},
isPeersMenuOpen: {type: Boolean},
isPageMenuOpen: {type: Boolean},
isBookmarked: {type: Boolean, attribute: 'is-bookmarked'},
isLocationFocused: {type: Boolean}
}
}
constructor () {
super()
this.activeTabIndex = -1
this.url = ''
this.title = ''
this.peers = 0
this.zoom = 0
this.loadError = null
this.donateLinkHref = false
this.localPath = false
this.availableAlternative = ''
this.isLocalPathMenuOpen = false
this.isDonateMenuOpen = false
this.isPeersMenuOpen = false
this.isPageMenuOpen = false
this.isBookmarked = false
this.isLocationFocused = false
// listen for commands from the main process
ipcRenderer.on('command', this.onCommand.bind(this))
}
get isDat () {
return this.url.startsWith('dat://')
}
focusLocation () {
var input = this.shadowRoot.querySelector('.input-container input')
input.focus()
input.setSelectionRange(0, input.value.length)
bg.views.focusShellWindow() // focus the shell-window UI
}
unfocusLocation () {
var input = this.shadowRoot.querySelector('.input-container input')
input.blur()
}
// rendering
// =
render () {
return html`
<link rel="stylesheet" href="beaker://assets/font-awesome.css">
<shell-window-navbar-site-info
url=${this.url}
.loadError=${this.loadError}
>
</shell-window-navbar-site-info>
${this.renderLocation()}
${this.renderZoom()}
${this.renderLocalPathBtn()}
${this.renderAvailableAlternativeBtn()}
${this.renderLiveReloadingBtn()}
${this.renderDonateBtn()}
${this.renderPeersBtn()}
${this.renderPageMenuBtn()}
${this.renderBookmarkBtn()}
`
}
renderLocation () {
return html`
<div class="input-container">
<input
type="text"
value="${this.url}"
@contextmenu=${this.onContextMenuLocation}
@mousedown=${this.onMousedownLocation}
@mouseup=${this.onMouseupLocation}
@dblclick=${this.onDblclickLocation}
@focus=${this.onFocusLocation}
@blur=${this.onBlurLocation}
@input=${this.onInputLocation}
@keydown=${this.onKeydownLocation}
>
${this.isLocationFocused ? '' : this.renderInputPretty()}
</div>
`
}
renderInputPretty () {
if (/^(dat|http|https|beaker):\/\//.test(this.url)) {
try {
var { protocol, host, pathname, search, hash } = new URL(this.url)
var hostVersion
if (protocol === 'dat:') {
let match = /(.*)\+(.*)/.exec(host)
if (match) {
host = match[1]
hostVersion = '+' + match[2]
}
if (isDatHashRegex.test(host)) {
host = prettyHash(host)
}
}
var cls = 'protocol'
if (['beaker:'].includes(protocol)) cls += ' protocol-secure'
if (['https:'].includes(protocol) && !this.loadError) cls += ' protocol-secure'
if (['https:'].includes(protocol) && this.loadError && this.loadError.isInsecureResponse) cls += ' protocol-insecure'
if (['dat:'].includes(protocol)) cls += ' protocol-secure'
if (['beaker:'].includes(protocol)) cls += ' protocol-secure'
return html`
<div class="input-pretty">
<span class=${cls}>${protocol.slice(0, -1)}</span><span class="syntax">://</span><span class="host">${host}</span>${hostVersion ? html`<span class="host-version">${hostVersion}</span>` : ''}<span class="path">${pathname}${search}${hash}</span>
</div>
`
} catch (e) {
// invalid URL
return html`
<div class="input-pretty">${this.url}</div>
`
}
}
}
renderZoom () {
if (this.zoom === 0) return ''
var zoomPct = ({
'-0.5': 90,
'-1': 75,
'-1.5': 67,
'-2': 50,
'-2.5': 33,
'-3': 25,
'0': 100,
'0.5': 110,
'1': 125,
'1.5': 150,
'2': 175,
'2.5': 200,
'3': 250,
'3.5': 300,
'4': 400,
'4.5': 500
})[this.zoom]
return html`
<button @click=${this.onClickZoom} title="Zoom: ${zoomPct}%" class="zoom">
${zoomPct}%
</button>
`
}
renderLocalPathBtn () {
if (!this.localPath) {
return ''
}
var cls = classMap({'local-path': true, pressed: this.isLocalPathMenuOpen})
return html`
<button class="${cls}" @click=${this.onClickLocalPathMenu}>
<span class="value">${this.localPath}</span>
<span class="fa fa-caret-down"></span>
</button>
`
}
renderAvailableAlternativeBtn () {
const aa = this.availableAlternative
if (aa === 'dat:') {
return html`
<button class="available-alternative" title="Go to Dat Version of this Site" @click=${this.onClickAvailableAlternative}>
P2P version available
</button>
`
}
if (aa === 'http:' || aa === 'https:') {
return html`
<button class="available-alternative" title="Go to HTTP/S Version of this Site" @click=${this.onClickAvailableAlternative}>
HTTP/S version available
</button>
`
}
return ''
}
renderLiveReloadingBtn () {
if (!this.isLiveReloading) {
return ''
}
return html`
<button class="live-reload" @click=${this.onClickLiveReloadingBtn} title="Toggle live reloading">
<i class="fa fa-bolt"></i>
</button>
`
}
renderDonateBtn () {
if (!this.donateLinkHref) {
return ''
}
var cls = classMap({donate: true, pressed: this.isDonateMenuOpen})
return html`
<button class="${cls}" @click=${this.onClickDonateMenu}>
<i class="far fa-heart"></i>
</button>
`
}
renderPeersBtn () {
if (!this.isDat) {
return ''
}
var cls = classMap({peers: true, pressed: this.isPeersMenuOpen})
return html`
<button class="${cls}" @click=${this.onClickPeersMenu}>
<i class="fa fa-share-alt"></i>
${this.peers || 0}
</button>
`
}
renderPageMenuBtn () {
if (!this.isDat) {
return ''
}
var cls = classMap({pressed: this.isPageMenuOpen})
return html`
<button class="${cls}" @click=${this.onClickPageMenu}>
<span class="fa fa-ellipsis-h"></span>
</button>
`
}
renderBookmarkBtn () {
const cls = classMap({
far: !this.isBookmarked,
fas: this.isBookmarked,
'fa-star': true
})
return html`
<button class="bookmark-btn" style="margin-right: 2px" @click=${this.onClickBookmark}>
<span class="${cls}"></span>
</button>
`
}
// events
// =
onCommand (e, cmd) {
if (cmd === 'create-bookmark') {
this.onClickBookmark()
}
if (cmd === 'focus-location') {
this.focusLocation()
}
if (cmd === 'unfocus-location') {
this.unfocusLocation()
}
}
onContextMenuLocation (e) {
bg.views.showLocationBarContextMenu('active')
}
onMousedownLocation (e) {
// track if the user is clicking, doubleclicking, or dragging the location before its focused
// if a click, select all; if a doubleclick, select word under cursor; if a drag, do default behavior
if (!e.currentTarget.matches(':focus')) {
this.lastMousedownLocationTs = Date.now()
}
}
onMouseupLocation (e) {
if (Date.now() - this.lastMousedownLocationTs <= 300) {
// was a fast click (probably not a drag) so select all
let inputEl = e.currentTarget
this.mouseupClickIndex = inputEl.selectionStart
inputEl.select()
// setup double-click override
this.lastMousedownLocationTs = 0
this.lastMouseupLocationTs = Date.now()
}
}
onDblclickLocation (e) {
if (Date.now() - this.lastMouseupLocationTs <= 300) {
e.preventDefault()
// select the text under the cursor
// (we have to do this manually because we previously selected all on mouseup, which f's that default behavior up)
let inputEl = e.currentTarget
let {start, end} = findWordBoundary(inputEl.value, this.mouseupClickIndex)
inputEl.setSelectionRange(start, end)
this.lastMouseupLocationTs = 0
}
}
onFocusLocation (e) {
e.currentTarget.value = this.url
this.isLocationFocused = true
}
onBlurLocation (e) {
// clear the selection range so that the next focusing doesnt carry it over
window.getSelection().empty()
bg.views.hideLocationBar()
this.shadowRoot.querySelector('.input-container input').value = this.url // reset value
this.isLocationFocused = false
}
onInputLocation (e) {
var rect = this.getClientRects()[0]
bg.views.runLocationBarCmd('set-value', {
bounds: {
x: rect.left|0,
y: rect.bottom|0,
width: rect.width|0
},
value: e.currentTarget.value
})
}
async onKeydownLocation (e) {
if (e.key === 'Escape') {
this.unfocusLocation()
return
}
if (e.key === 'Enter') {
bg.views.runLocationBarCmd('choose-selection')
this.unfocusLocation()
return
}
var up = (e.key === 'ArrowUp' || (e.ctrlKey && e.key === 'p'))
var down = (e.key === 'ArrowDown' || (e.ctrlKey && e.key === 'n'))
if (up || down) {
e.preventDefault()
var value = await bg.views.runLocationBarCmd('move-selection', {up, down})
this.shadowRoot.querySelector('.input-container input').value = value
return
}
}
onClickZoom (e) {
bg.views.resetZoom(this.activeTabIndex)
}
async onClickLocalPathMenu (e) {
this.isLocalPathMenuOpen = true
var rect1 = this.getClientRects()[0]
var rect2 = e.currentTarget.getClientRects()[0]
await bg.views.toggleMenu('local-path', {
bounds: {
top: (rect1.bottom|0),
right: (rect2.right|0)
},
params: {url: this.url}
})
this.isLocalPathMenuOpen = false
}
onClickAvailableAlternative (e) {
var url = new URL(this.url)
url.protocol = this.availableAlternative
if (e.metaKey || e.ctrlKey) {
bg.views.createTab(url.toString(), {setActive: true, addToNoRedirects: true})
} else {
bg.views.loadURL(this.activeTabIndex, url.toString(), {addToNoRedirects: true})
}
}
onClickLiveReloadingBtn (e) {
bg.views.toggleLiveReloading('active')
this.isLiveReloading = false
}
async onClickDonateMenu (e) {
this.isDonateMenuOpen = true
var rect1 = this.getClientRects()[0]
var rect2 = e.currentTarget.getClientRects()[0]
await bg.views.toggleMenu('donate', {
bounds: {
top: (rect1.bottom|0),
right: (rect2.right|0)
},
params: {url: this.url}
})
this.isDonateMenuOpen = false
}
async onClickPeersMenu (e) {
this.isPeersMenuOpen = true
var rect1 = this.getClientRects()[0]
var rect2 = e.currentTarget.getClientRects()[0]
await bg.views.toggleMenu('peers', {
bounds: {
top: (rect1.bottom|0),
right: (rect2.right|0)
},
params: {url: this.url}
})
this.isPeersMenuOpen = false
}
async onClickPageMenu (e) {
var rect1 = this.getClientRects()[0]
var rect2 = e.currentTarget.getClientRects()[0]
this.isPageMenuOpen = true
await bg.views.showMenu('page', {
bounds: {
top: (rect1.bottom|0),
right: (rect2.right|0)
},
params: {url: this.url}
})
this.isPageMenuOpen = false
}
async onClickBookmark () {
var rect = this.shadowRoot.querySelector('.bookmark-btn').getClientRects()[0]
// create a bookmark if needed
if (!this.isBookmarked) {
await bg.bookmarks.bookmarkPrivate(this.url, {title: this.title})
bg.views.refreshState(this.activeTabIndex) // pull latest state
}
// show menu
bg.views.toggleMenu('bookmark', {
bounds: {
top: Number(rect.bottom),
right: Number(rect.right)
},
params: {url: this.url}
})
}
}
NavbarLocation.styles = [buttonResetCSS, css`
:host {
display: flex;
flex: 1;
background: #fff;
border: 1px solid var(--color-border-input);
border-radius: 4px;
}
button {
width: 27px;
border-radius: 0;
color: #666;
}
button .fa,
button .far,
button .fas {
font-size: 15px;
}
button .fa-star {
font-size: 14px;
}
button .fas.fa-star {
color: #ffcc00;
-webkit-text-stroke: 1px #f7c600;
}
button.zoom {
width: auto;
font-size: 11px;
line-height: 10px;
background: #f5f5f5;
border-radius: 10px;
margin: 4px;
padding: 0 9px;
border: 1px solid #ccc;
font-weight: 500;
}
button.zoom:hover {
background: #eaeaea;
}
button.peers {
width: auto;
font-size: 13px;
font-variant: tabular-nums;
padding: 0 6px;
}
button.peers .fa {
font-size: 13px;
}
button.local-path {
width: auto;
display: flex;
align-items: center;
line-height: 26px;
padding: 0 6px;
}
button.local-path .value {
display: inline-block;
max-width: 300px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
margin-right: 5px;
}
button.available-alternative {
width: auto;
line-height: 26px;
padding: 0 6px;
}
button.live-reload {
width: 24px;
}
button.live-reload .fa {
position: relative;
top: -1px;
color: #f2f200;
-webkit-text-stroke: 1px #dabb15;
}
.input-container {
position: relative;
flex: 1;
margin: 0 8px;
}
.input-pretty,
input {
position: absolute;
left: 0;
top: 0;
box-sizing: border-box;
border: 0;
padding: 0;
line-height: 26px;
width: 100%;
height: 25px;
color: #222;
font-size: 13.5px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Ubuntu, Cantarell, "Oxygen Sans", "Helvetica Neue", sans-serif;
font-weight: 500;
letter-spacing: -.2px;
}
input:focus {
outline: 0;
}
.input-pretty {
z-index: 1;
text-overflow: ellipsis;
color: #000;
background: #fff;
cursor: text;
pointer-events: none;
}
.input-pretty .protocol {
color: var(--color-text--light);
}
.input-pretty .protocol-secure {
color: var(--color-secure);
}
.input-pretty .protocol-insecure {
color: var(--color-insecure);
}
.input-pretty .host-version,
.input-pretty .syntax,
.input-pretty .path {
color: var(--color-text--light);
white-space: nowrap;
}
`]
customElements.define('shell-window-navbar-location', NavbarLocation)