-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes.vue
More file actions
177 lines (147 loc) · 5.08 KB
/
Notes.vue
File metadata and controls
177 lines (147 loc) · 5.08 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
<!-- This file is part of Flus Browser
-- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div v-if="ready" class="flow">
<div class="cols cols--always cols--center cols--gap">
<h2 class="col--extend text--big">{{ t('notes.title') }}</h2>
<a
class="button button--ghost"
:href="notesUrl"
:title="t('notes.open_in_flus')"
target="_blank"
>
<Icon name="pop-out" />
{{ t('notes.open') }}
</a>
</div>
<article v-if="link.notes.length > 0" class="panel panel--base panel--rounded text-container">
<div v-for="[dateIso, notes] in notesByDates">
<p class="text--secondary text--small text--right">
<time :datetime="dateIso">
{{ dateToLocaleString(dateIso) }}
</time>
</p>
<!-- This displays raw HTML coming from the API. The HTML
should already be safe as it is generated from Markdown
syntax and limited to a few tags. As an additional security
measure, the HTML is also sanitized on client-side (see the
models/link.js file). -->
<div
v-for="note in notes"
v-html="note.htmlContent"
>
</div>
</div>
</article>
<form
@submit.prevent="addNote"
class="flow"
:disabled="form.inProgress()"
>
<p v-if="form.isInvalid('@base')" class="form-group__error" role="alert">
{{ t('forms.error') }}
{{ form.error('@base') }}
</p>
<div class="form-group">
<label for="note-content">
{{ t('notes.content') }}
</label>
<p v-if="form.isInvalid('content')" id="note-content-error" class="form-group__error" role="alert">
{{ t('forms.error') }}
{{ form.error('content') }}
</p>
<textarea
v-model="newNoteContent"
id="note-content"
required
ref="note-content"
:aria-invalid="form.isInvalid('content')"
:aria-errormessage="form.isInvalid('content') ? 'note-content-error' : null"
:disabled="form.inProgress()"
></textarea>
</div>
<div class="text--center">
<button class="button--primary button--big" :disabled="form.inProgress()">
{{ t('notes.add') }}
</button>
</div>
</form>
</div>
</template>
<script setup>
import { ref, useTemplateRef, onMounted, computed, watch } from "vue";
import { useI18n } from "vue-i18n";
import api from "../api.js";
import http from "../http.js";
import { store } from "../store.js";
import form from "../form.js";
const { t, locale } = useI18n();
locale.value = store.locale;
const props = defineProps({
link: {
type: Object,
required: true,
},
});
const ready = ref(false);
const newNoteContent = ref(store.getDraft(props.link));
const noteContentRef = useTemplateRef("note-content");
watch(newNoteContent, (noteContent) => {
store.saveDraft(props.link, noteContent);
});
const notesByDates = computed(() => {
const linkNotes = props.link.notes;
linkNotes.sort((note1, note2) => {
return note1.createdAt - note2.createdAt;
});
const linkNotesByDates = Map.groupBy(linkNotes, (note) => {
const date = new Date(note.createdAt);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
return date.toISOString();
});
return Array.from(linkNotesByDates.entries());
});
const notesUrl = computed(() => {
return `${store.auth.server}/links/${props.link.id}`;
});
function dateToLocaleString(dateIso) {
const date = new Date(dateIso);
return date.toLocaleDateString(locale.value, {
year: "numeric",
month: "long",
day: "numeric",
});
}
function loadNotes() {
api.notes(props.link).then((linkNotes) => {
for (const note of linkNotes) {
props.link.addNote(note);
}
ready.value = true;
});
}
function addNote() {
form.startRequest();
api.addNoteToLink(props.link, newNoteContent.value)
.then((note) => {
form.finishRequest();
newNoteContent.value = "";
props.link.addNote(note);
noteContentRef.value.focus();
})
.catch((error) => {
form.finishRequest();
if (error instanceof http.HttpError) {
form.setAndFormatErrors(error.errors, t);
} else {
store.notify("error", t("errors.unknown"));
}
noteContentRef.value.focus();
});
}
onMounted(loadNotes);
</script>