-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathDetailsLayout.vue
More file actions
261 lines (229 loc) · 7.42 KB
/
DetailsLayout.vue
File metadata and controls
261 lines (229 loc) · 7.42 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
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
<script setup lang="ts">
import { faPen, faSave, faUndo } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { BButton, BFormInput, BFormTextarea } from "bootstrap-vue";
import { storeToRefs } from "pinia";
import { computed, ref } from "vue";
import { useUserStore } from "@/stores/userStore";
import l from "@/utils/localization";
import type { DetailsLayoutSummarized } from "./types";
import ClickToEdit from "@/components/Collections/common/ClickToEdit.vue";
import Heading from "@/components/Common/Heading.vue";
import TextSummary from "@/components/Common/TextSummary.vue";
import StatelessTags from "@/components/TagsMultiselect/StatelessTags.vue";
interface Props {
name?: string;
tags?: string[];
writeable?: boolean;
renameable?: boolean;
annotation?: string;
showAnnotation?: boolean;
summarized?: DetailsLayoutSummarized;
}
const props = withDefaults(defineProps<Props>(), {
name: undefined,
tags: undefined,
writeable: true,
renameable: true,
annotation: undefined,
showAnnotation: true,
summarized: undefined,
});
const emit = defineEmits(["save"]);
const userStore = useUserStore();
const { isAnonymous } = storeToRefs(userStore);
const nameRef = ref<HTMLInputElement | null>(null);
const editing = ref(false);
const textSelected = ref(false);
const localProps = ref<{ name: string; annotation: string | null; tags: string[] }>({
name: "",
annotation: null,
tags: [],
});
const clickToEditName = computed({
get: () => props.name ?? "",
set: (newName) => {
if (newName && newName !== props.name) {
emit("save", { name: newName.trim() });
localProps.value.name = newName;
}
},
});
const detailsClass = computed(() => {
const classes: Record<string, boolean> = {
details: true,
"summarized-details": !!props.summarized,
"m-3": !props.summarized || editing.value,
};
if (props.summarized) {
classes[props.summarized] = true;
}
return classes;
});
const editButtonTitle = computed(() => {
if (isAnonymous.value) {
return l("Log in to Rename History");
} else {
if (props.writeable) {
return l("Edit");
} else {
return l("Not Editable");
}
}
});
function onSave() {
editing.value = false;
emit("save", localProps.value);
}
function onToggle() {
editing.value = !editing.value;
localProps.value = {
name: props.name ?? "",
annotation: props.annotation ?? null,
tags: props.tags ?? [],
};
if (nameRef.value) {
nameRef.value.focus();
}
}
function selectText() {
if (!textSelected.value) {
nameRef.value?.select();
textSelected.value = true;
} else {
nameRef.value?.focus();
textSelected.value = false;
}
}
</script>
<template>
<section :class="detailsClass" data-description="edit details">
<div class="d-flex justify-content-between w-100">
<template v-if="!summarized && !editing">
<ClickToEdit
v-if="renameable"
v-model="clickToEditName"
component="h3"
data-description="name display"
no-save-on-blur
class="name-display my-2 w-100" />
<Heading v-else h3 :clamp="2" class="my-2 w-100">
{{ props.name || "..." }}
</Heading>
</template>
<div v-else class="overflow-hidden" style="max-width: 80%">
<TextSummary
:description="name"
data-description="name display"
class="my-2"
component="h3"
one-line-summary
no-expand />
</div>
<BButton
:disabled="isAnonymous || !writeable"
class="edit-button ml-1 float-right"
data-description="editor toggle"
size="sm"
variant="link"
:title="editButtonTitle"
:pressed="editing"
@click="onToggle">
<FontAwesomeIcon :icon="faPen" fixed-width />
</BButton>
</div>
<slot name="description" />
<div v-if="!editing">
<div
v-if="annotation && !summarized"
v-short="annotation"
class="mt-2"
data-description="annotation value" />
<div
v-else-if="summarized"
:class="{ annotation: ['both', 'annotation'].includes(summarized), hidden: summarized === 'hidden' }">
<TextSummary
v-if="annotation"
:description="annotation"
data-description="annotation value"
one-line-summary
no-expand />
</div>
<StatelessTags
v-if="tags"
:class="{
'mt-2': !summarized,
tags: ['both', 'tags'].includes(summarized || ''),
hidden: summarized === 'hidden',
}"
:value="tags"
disabled
:max-visible-tags="summarized ? 1 : 5" />
<slot v-if="summarized" name="update-time" />
</div>
<div v-else class="mt-3" data-description="edit form">
<BFormInput
ref="name"
v-model="localProps.name"
class="mb-2"
placeholder="Name"
trim
max-rows="4"
data-description="name input"
@keyup.enter="onSave"
@keyup.esc="onToggle"
@focus="selectText"
@blur="textSelected = false" />
<BFormTextarea
v-if="showAnnotation"
v-model="localProps.annotation"
class="mb-2"
placeholder="Annotation (optional)"
trim
max-rows="4"
data-description="annotation input"
@keyup.esc="onToggle" />
<StatelessTags v-if="localProps.tags" v-model="localProps.tags" class="mb-3 tags" />
<BButton
class="save-button mb-1"
data-description="editor save button"
size="sm"
variant="primary"
:disabled="!localProps.name"
@click="onSave">
<FontAwesomeIcon :icon="faSave" fixed-width />
<span v-localize>Save</span>
</BButton>
<BButton class="cancel-button mb-1" data-description="editor cancel button" size="sm" @click="onToggle">
<FontAwesomeIcon :icon="faUndo" fixed-width />
<span v-localize>Cancel</span>
</BButton>
</div>
<slot></slot>
</section>
</template>
<style lang="scss" scoped>
.name-display :deep(h3),
h3.name-display {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
.summarized-details {
margin-left: 0.5rem;
max-width: 15rem;
&.both {
min-height: 8.5em;
}
.tags {
min-height: 2rem;
}
.annotation {
min-height: 2rem;
}
.hidden {
display: none;
}
}
</style>