-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkScreen.vue
More file actions
225 lines (188 loc) · 6.68 KB
/
LinkScreen.vue
File metadata and controls
225 lines (188 loc) · 6.68 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
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
<!-- This file is part of Flus Browser
-- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<Screen :title="link.title" header>
<div v-if="ready && alert.type === ''" class="flow">
<div class="flow">
<div class="flow flow--smaller">
<h1 class="text--normal">
{{ link.title }}
</h1>
<p class="text--secondary">
{{ link.host() }} · {{ link.readingTimeLabel() }}
<span v-if="link.isRead" :title="t('link.is_read')">
<Icon name="check" />
</span>
<span v-if="link.isReadLater" :title="t('link.is_read_later')">
<Icon name="bookmark" />
</span>
</p>
<div v-if="link.tags">
<span v-for="tag in link.tags" class="tag badge badge--accent">#{{ tag }}</span>
</div>
</div>
<div v-if="link.url != currentTabUrl" class="panel panel--rounded panel--grey cols cols--always cols--gap-small cols--center">
<p class="col--extend">
{{ t("link.tab_unsync") }}
</p>
<div>
<button type="button" @click="refreshForCurrentTab">
<Icon name="sync" />
{{ t("link.refresh") }}
</button>
</div>
</div>
<div class="cols cols--always cols--center cols--gap-small">
<div class="col--extend cols cols--always cols--center cols--gap-small">
<button v-if="!link.isRead || link.isReadLater" class="button--icon" @click.prevent="markAsRead">
<Icon name="check" />
<span class="sr-only">
{{ t("link.mark_as_read") }}
</span>
</button>
<button v-if="!link.isReadLater" class="button--icon" @click.prevent="markAsReadLater">
<Icon name="bookmark" />
<span class="sr-only">
{{ t("link.mark_as_read_later") }}
</span>
</button>
</div>
<button
@click="toggleCollections"
:aria-expanded="displayCollections"
aria-controls="collections"
>
<Icon name="collection" />
{{ t("link.count_collections", link.collections.length) }}
</button>
</div>
<Collections
id="collections"
:link="link"
:open="displayCollections"
/>
</div>
<hr>
<Notes :link="link" />
</div>
<div v-else-if="ready && alert.type == 'info'">
<p class="panel panel--rounded panel--caribbean text--bold" role="alert">
<Icon name="info" />
{{ alert.message }}
</p>
</div>
<div v-else-if="ready && alert.type == 'error'">
<p class="panel panel--rounded panel--danger text--bold" role="alert">
<Icon name="error" />
{{ t("forms.error") }}
{{ alert.message }}
</p>
</div>
<div v-else-if="!ready" class="flow text--center">
<div class="spinner"></div>
<p>
{{ t("link.loading") }}
</p>
</div>
</Screen>
</template>
<script setup>
import { ref, reactive, onMounted } from "vue";
import { useI18n } from "vue-i18n";
import browser from "webextension-polyfill";
import { store } from "../store.js";
import api from "../api.js";
import http from "../http.js";
import Link from "../models/link.js";
import Collections from "../components/Collections.vue";
import Notes from "../components/Notes.vue";
const { t, locale } = useI18n();
locale.value = store.locale;
const link = reactive(new Link());
const currentTabUrl = ref("");
const ready = ref(false);
const displayCollections = ref(false);
const alert = ref({
type: "",
message: "",
});
function toggleCollections() {
displayCollections.value = !displayCollections.value;
}
async function getCurrentTab() {
return await browser.tabs
.query({
active: true,
currentWindow: true,
})
.then((tabs) => {
return tabs[0];
});
}
async function refreshForCurrentTab() {
ready.value = false;
const url = (await getCurrentTab()).url;
if (!url.startsWith("http://") && !url.startsWith("https://")) {
alert.value = {
type: "info",
message: t("link.invalid_protocol"),
};
ready.value = true;
return;
}
api.search(url)
.then((data) => {
link.init(data.links[0]);
ready.value = true;
})
.catch((error) => {
if (error instanceof http.HttpError) {
alert.value = {
type: "error",
message: error.errors.url.map((error) => {
return t(`errors.url.${error.code}`);
}),
};
} else {
alert.value = {
type: "error",
message: t("errors.unknown"),
};
}
ready.value = true;
});
}
async function refreshCurrentTabUrl() {
currentTabUrl.value = (await getCurrentTab()).url;
}
async function markAsRead() {
api.markLinkAsRead(link)
.then(() => {
link.markAsRead();
})
.catch(() => {
alert.value = {
type: "error",
message: t("errors.unknown"),
};
});
}
async function markAsReadLater() {
api.markLinkAsReadLater(link)
.then(() => {
link.markAsReadLater();
})
.catch(() => {
alert.value = {
type: "error",
message: t("errors.unknown"),
};
});
}
onMounted(refreshForCurrentTab);
onMounted(refreshCurrentTabUrl);
browser.tabs.onUpdated.addListener(refreshCurrentTabUrl);
browser.tabs.onActivated.addListener(refreshCurrentTabUrl);
browser.windows.onFocusChanged.addListener(refreshCurrentTabUrl);
</script>