summaryrefslogtreecommitdiff
path: root/src/components/News.vue
blob: ae92d58972b6d7839a9ebdd823862f985e372db7 (plain) (blame)
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
<template>
	<div role="feed" aria-label="TMW news" :aria-busy="count > 1 && !fullyLoaded" class="news" v-if="count">
		<span v-if="!entries.length">(no news entries)</span>

		<article tabindex="0" class="entry" v-for="(entry, index) in entries" :aria-posinset="index + 1" :aria-setsize="entries.length" :id="entry.id" :aria-labelledby="entry.id + '-title'" :aria-describedby="`${entry.id}-body ${entry.id}-date ${entry.id}-author`" :key="entry.id">
			<header><a tabindex="-1" :id="entry.id + '-title'" :href="'#' + entry.id">{{ entry.title }}</a></header>
			<time :id="entry.id + '-date'" aria-label="publication date" :datetime="entry.date" class="date">{{ entry.date }}</time>
			<section :id="entry.id + '-body'" class="body" v-html="entry.html"></section>
			<q :id="entry.id + '-author'" aria-label="author">{{entry.author}}</q>
		</article>
		<article v-if="count > 1 && !fullyLoaded" class="entry loading">
			<section class="body">
				Loading... Please wait.
			</section>
		</article>
	</div>
</template>

<style scoped>
.news .entry {
	margin-bottom: 0;
	padding-bottom: 1em;
	hyphens: auto;

	&:not(:last-of-type) {
		padding-bottom: 3em;
	}

	&:nth-of-type(1n + 2) {
		padding-top: 1em;
		border-top: solid 0.4ex rgba(0, 0, 0, 0.1);
	}

	&:first-child:not(:only-child) {
		padding-top: 1em;
		border-top: solid 0.4ex transparent;
	}

	&:nth-of-type(1n + 2), &:not(:only-child) {
		&:hover, &:target, &:focus-within {
			border-color: #0000FF;
			background-color: rgba(220,220,220,0.5);

			& > .date, & header > a {
				color: #0000FF;
			}
		}
	}

	& header {
		display: inline-block;
		position: relative;

		&> a {
			text-decoration: none;
			color: inherit;
			font-weight: bold;

			&:hover::before {
				content: "⚓";
				filter: grayscale(1) brightness(0);
				position: absolute;
				top: 0;
				left: -1.6em;
			}
		}
	}

	& > .date {
		float: right;
	}

	& .body {
		margin-top: 2ex;

		&:deep(b) {
			display: block;
			margin-bottom: 1ex;
		}

		& :any-link {
			color: inherit;
			text-decoration: none;

			&::before {
				content: "➜ ";
				color: blue;
			}

			&:hover {
				text-decoration: underline;
			}
		}
	}

	& q {
		color: #136E18;
		margin-top: 2ex;
		display: inline-block;

		&::before {
			content: "– ";
		}
	}
}

@media (min-width: 1100px) {
	.news .entry {
		padding-left: 30px;
		padding-right: 40px;
	}
}
</style>

<script lang="ts">
import { Options, Vue } from "vue-class-component";
import newsEntries from "@/assets/news.json";

interface NewsEntry {
	id: string;
	title: string;
	date: string;
	author: string;
	html: string;
}

@Options({
	props: {
		count: Number,
		from: Number,
	}
})
export default class News extends Vue {
	private count = Infinity;
	private from = 0;
	private entries: NewsEntry[] = (newsEntries as NewsEntry[]).slice(this.from, this.count);
	private fullyLoaded = false;

	beautify (): void {
		this.entries.forEach(entry => {
			// FIXME: weird Vue bug
			entry.html = entry.html.replace(/<br\/>/g,"<br></br>");

			// compare the entry title with its first line:
			const compare = `<b>${entry.title}</b><br></br>`;

			if (entry.html.startsWith(compare)) {
				// duplicate title: remove the extra one
				entry.html = entry.html.slice(compare.length);
			}
		});
	}

	mounted (): void {
		if (!process.env.VUE_APP_NEWS_JSON || !process.env.VUE_APP_NEWS_JSON.startsWith("https")) {
			this.beautify();
			// no extra news to load so end here
			return;
		}

		// restore from cache while we're loading a fresh copy
		if (Reflect.has(self, "localStorage")) {
			const newsCache = localStorage.getItem("newsCache");

			if (newsCache !== null) {
				this.entries = (JSON.parse(newsCache) as NewsEntry[]).slice(this.from, this.count);
			}
		}

		// initial rendering
		this.beautify();

		if (this.count === 1 && this.entries.length >= 1) {
			// don't loaad extra news unprompted
			return;
		}

		const req = new Request(process.env.VUE_APP_NEWS_JSON, {
			method: "GET",
			cache: "default", // serve if fresh, else fetch
		});

		fetch(req)
		.then(data => data.json())
		.then(data => {
			this.entries = (data as NewsEntry[]).slice(this.from, this.count);
			this.fullyLoaded = true;
			this.beautify();

			if (document.location.hash) {
				let el = null;

				if ((el = document.getElementById(document.location.hash.slice(1))) !== null) {
					el.scrollIntoView(true);
				}
			}

			if (Reflect.has(self, "localStorage")) {
				localStorage.setItem("newsCache", JSON.stringify(data));
			}
		})
		.catch(() => {
			// no news (will use cache, if any)
		})
	}
}
</script>