Skip to content

Commit 2af4fd3

Browse files
committed
fix(browser): add loading state
1 parent ef569c9 commit 2af4fd3

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

addons/browser/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
"productName": "浏览器",
1616
"description": "网上冲浪"
1717
}
18+
},
19+
"devDependencies": {
20+
"normalize-url": "^8.0.1"
1821
}
1922
}

addons/browser/src/renderer/BrowserPane.vue

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import type { TerminalTab } from '@commas/types/terminal'
33
import * as commas from 'commas:api/renderer'
44
import { shell } from 'electron'
5+
import normalizeURL from 'normalize-url'
56
import { nextTick, watchEffect } from 'vue'
67
78
const { tab } = defineProps<{
@@ -58,8 +59,16 @@ async function startCustomization() {
5859
}
5960
6061
async function customize() {
61-
if (customURL !== url) {
62-
url = customURL
62+
if (customURL && customURL !== url) {
63+
url = normalizeURL(customURL, {
64+
defaultProtocol: 'https',
65+
stripTextFragment: false,
66+
stripWWW: false,
67+
removeQueryParameters: false,
68+
removeTrailingSlash: false,
69+
removeSingleSlash: false,
70+
sortQueryParameters: false,
71+
})
6372
}
6473
isCustomizing = false
6574
}
@@ -95,7 +104,7 @@ watchEffect((onInvalidate) => {
95104
<template>
96105
<TerminalPane :tab="tab" class="browser-pane">
97106
<div class="browser-view">
98-
<div class="form-line action-line">
107+
<div :class="['form-line', 'action-line', { 'is-loading': view?.loading }]">
99108
<span :class="['link', 'form-action', { disabled: !view?.canGoBack }]" @click="goBack">
100109
<VisualIcon name="lucide-undo-2" />
101110
</span>
@@ -134,10 +143,20 @@ watchEffect((onInvalidate) => {
134143
padding: 0;
135144
overflow: visible;
136145
}
146+
@keyframes background-continuous {
147+
from {
148+
background-position: right;
149+
}
150+
}
137151
.action-line {
138152
flex: none;
139153
gap: 4px;
140154
padding: 8px;
155+
&.is-loading {
156+
background: linear-gradient(to right, transparent 25%, var(--design-highlight-background), transparent 66.6667%);
157+
background-size: 300% 100%;
158+
animation: background-continuous 1s infinite linear;
159+
}
141160
.form-action {
142161
width: 18px;
143162
height: 18px;

package-lock.json

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/lib/message.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ declare module '@commas/electron-ipc' {
4949
'view-title-updated': (id: number, title: string) => void,
5050
'view-icon-updated': (id: number, icon: string | undefined) => void,
5151
'view-url-updated': (id: number, url: string, canGoBack: boolean) => void,
52+
'view-loading-updated': (id: number, loading: boolean) => void,
5253
'view-open-url': (url: string) => void,
5354
}
5455
}
@@ -287,6 +288,12 @@ function handleViewEvents(view: WebContentsView, parent: WebContents) {
287288
view.webContents.on('did-start-navigation', (details) => {
288289
send(parent, 'view-url-updated', view.webContents.id, details.url, view.webContents.navigationHistory.canGoBack())
289290
})
291+
view.webContents.on('did-start-loading', () => {
292+
send(parent, 'view-loading-updated', view.webContents.id, true)
293+
})
294+
view.webContents.on('did-stop-loading', () => {
295+
send(parent, 'view-loading-updated', view.webContents.id, false)
296+
})
290297
}
291298

292299
export {

src/renderer/compositions/web-contents.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class RendererWebContentsView {
1212
url?: string
1313
title?: string
1414
icon?: string
15+
loading?: boolean
1516
canGoBack?: boolean
1617

1718
constructor(id: number) {
@@ -80,6 +81,11 @@ export function handleWebContentsMessages() {
8081
view.url = url
8182
view.canGoBack = canGoBack
8283
})
84+
ipcRenderer.on('view-loading-updated', (event, id, loading) => {
85+
const view = webContentsViews.find(item => item.id === id)
86+
if (!view) return
87+
view.loading = loading
88+
})
8389
ipcRenderer.on('view-open-url', (event, url) => {
8490
openURL(url)
8591
})

0 commit comments

Comments
 (0)