fix(csv-viewer): add support for missing text encodings in dropdown list (#5795)
This commit is contained in:
142
frontend/src/components/DropdownModal.vue
Normal file
142
frontend/src/components/DropdownModal.vue
Normal file
@@ -0,0 +1,142 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, watch, onMounted, onBeforeUnmount } from "vue";
|
||||
import { vClickOutside } from "@/utils/index";
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
position?: "top-left" | "bottom-left" | "bottom-right" | "top-right";
|
||||
closeOnClick?: boolean;
|
||||
}>(),
|
||||
{
|
||||
position: "bottom-right",
|
||||
closeOnClick: true,
|
||||
}
|
||||
);
|
||||
|
||||
const isOpen = defineModel<boolean>();
|
||||
|
||||
const triggerRef = ref<HTMLElement | null>(null);
|
||||
const listRef = ref<HTMLElement | null>(null);
|
||||
const dropdownStyle = ref<Record<string, string>>({});
|
||||
|
||||
const updatePosition = () => {
|
||||
if (!isOpen.value || !triggerRef.value) return;
|
||||
|
||||
const rect = triggerRef.value.getBoundingClientRect();
|
||||
|
||||
dropdownStyle.value = {
|
||||
position: "fixed",
|
||||
top: props.position.includes("bottom")
|
||||
? `${rect.bottom + 2}px`
|
||||
: `${rect.top}px`,
|
||||
left: props.position.includes("left") ? `${rect.left}px` : "auto",
|
||||
right: props.position.includes("right")
|
||||
? `${window.innerWidth - rect.right}px`
|
||||
: "auto",
|
||||
zIndex: "11000",
|
||||
};
|
||||
};
|
||||
|
||||
watch(isOpen, (open) => {
|
||||
if (open) {
|
||||
updatePosition();
|
||||
}
|
||||
});
|
||||
|
||||
const onWindowChange = () => {
|
||||
updatePosition();
|
||||
};
|
||||
|
||||
const closeDropdown = (e: Event) => {
|
||||
if (
|
||||
e.target instanceof HTMLElement &&
|
||||
listRef.value?.contains(e.target) &&
|
||||
!props.closeOnClick
|
||||
) {
|
||||
return;
|
||||
}
|
||||
isOpen.value = false;
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
window.addEventListener("resize", onWindowChange);
|
||||
window.addEventListener("scroll", onWindowChange, true);
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.removeEventListener("resize", onWindowChange);
|
||||
window.removeEventListener("scroll", onWindowChange, true);
|
||||
});
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
export default {
|
||||
directives: {
|
||||
clickOutside: vClickOutside,
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
class="dropdown-modal-container"
|
||||
v-click-outside="closeDropdown"
|
||||
ref="triggerRef"
|
||||
>
|
||||
<button @click="isOpen = !isOpen" class="dropdown-modal-trigger">
|
||||
<slot></slot>
|
||||
<i class="material-icons">chevron_right</i>
|
||||
</button>
|
||||
|
||||
<teleport to="body">
|
||||
<div
|
||||
ref="listRef"
|
||||
class="dropdown-modal-list"
|
||||
:class="{ 'dropdown-modal-open': isOpen }"
|
||||
:style="dropdownStyle"
|
||||
>
|
||||
<div>
|
||||
<slot name="list"></slot>
|
||||
</div>
|
||||
</div>
|
||||
</teleport>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.dropdown-modal-trigger {
|
||||
background: var(--surfacePrimary);
|
||||
color: var(--textSecondary);
|
||||
border: 1px solid var(--borderPrimary);
|
||||
border-radius: 0.1em;
|
||||
padding: 0.5em 1em;
|
||||
transition: 0.2s ease all;
|
||||
margin: 0;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 1rem;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.dropdown-modal-trigger > i {
|
||||
transform: rotate(90deg);
|
||||
}
|
||||
|
||||
.dropdown-modal-list {
|
||||
padding: 0.25rem;
|
||||
background-color: var(--surfacePrimary);
|
||||
color: var(--textSecondary);
|
||||
display: none;
|
||||
border: 1px solid var(--borderPrimary);
|
||||
border-radius: 0.1em;
|
||||
}
|
||||
|
||||
.dropdown-modal-list > div {
|
||||
max-height: 450px;
|
||||
padding: 0.25rem;
|
||||
}
|
||||
|
||||
.dropdown-modal-open {
|
||||
display: block;
|
||||
}
|
||||
</style>
|
||||
@@ -21,19 +21,37 @@
|
||||
</div>
|
||||
<div class="header-select" v-if="isEncodedContent">
|
||||
<label for="fileEncoding">{{ $t("files.fileEncoding") }}</label>
|
||||
<select
|
||||
id="fileEncoding"
|
||||
class="input input--block"
|
||||
v-model="selectedEncoding"
|
||||
<DropdownModal
|
||||
v-model="isEncondingDropdownOpen"
|
||||
:close-on-click="false"
|
||||
>
|
||||
<option
|
||||
v-for="encoding in availableEncodings"
|
||||
:value="encoding"
|
||||
:key="encoding"
|
||||
>
|
||||
{{ encoding }}
|
||||
</option>
|
||||
</select>
|
||||
<div>
|
||||
<span class="selected-encoding">{{ selectedEncoding }}</span>
|
||||
</div>
|
||||
<template v-slot:list>
|
||||
<input
|
||||
v-model="encodingSearch"
|
||||
:placeholder="$t('search.search')"
|
||||
class="input input--block"
|
||||
name="encoding"
|
||||
/>
|
||||
<div class="encoding-list">
|
||||
<div v-if="encodingList.length == 0" class="message">
|
||||
<i class="material-icons">sentiment_dissatisfied</i>
|
||||
<span>{{ $t("files.lonely") }}</span>
|
||||
</div>
|
||||
<button
|
||||
v-for="encoding in encodingList"
|
||||
:value="encoding"
|
||||
:key="encoding"
|
||||
class="encoding-button"
|
||||
@click="selectedEncoding = encoding"
|
||||
>
|
||||
{{ encoding }}
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
</DropdownModal>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="displayError" class="csv-error">
|
||||
@@ -74,10 +92,11 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watchEffect } from "vue";
|
||||
import { computed, ref, watch, watchEffect } from "vue";
|
||||
import { parse } from "csv-parse/browser/esm";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { availableEncodings, decode } from "@/utils/encodings";
|
||||
import DropdownModal from "../DropdownModal.vue";
|
||||
|
||||
const { t } = useI18n({});
|
||||
|
||||
@@ -90,6 +109,16 @@ const props = withDefaults(defineProps<Props>(), {
|
||||
error: "",
|
||||
});
|
||||
|
||||
const isEncondingDropdownOpen = ref(false);
|
||||
|
||||
const encodingSearch = ref<string>("");
|
||||
|
||||
const encodingList = computed(() => {
|
||||
return availableEncodings.filter((e) =>
|
||||
e.toLowerCase().includes(encodingSearch.value.toLowerCase())
|
||||
);
|
||||
});
|
||||
|
||||
const columnSeparator = ref([",", ";"]);
|
||||
|
||||
const selectedEncoding = ref("utf-8");
|
||||
@@ -128,6 +157,11 @@ watchEffect(() => {
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
watch(selectedEncoding, () => {
|
||||
isEncondingDropdownOpen.value = false;
|
||||
encodingSearch.value = "";
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
@@ -271,14 +305,21 @@ watchEffect(() => {
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 0.5rem;
|
||||
flex-direction: column;
|
||||
@media (width >= 640px) {
|
||||
flex-direction: row;
|
||||
}
|
||||
}
|
||||
|
||||
.header-select > label {
|
||||
font-size: small;
|
||||
max-width: 80px;
|
||||
@media (width >= 640px) {
|
||||
max-width: 70px;
|
||||
}
|
||||
}
|
||||
|
||||
.header-select > select {
|
||||
.header-select > select,
|
||||
.header-select > div {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
@@ -286,4 +327,40 @@ watchEffect(() => {
|
||||
font-size: 1.2rem;
|
||||
color: var(--blue);
|
||||
}
|
||||
|
||||
.encoding-list {
|
||||
max-height: 300px;
|
||||
min-width: 120px;
|
||||
overflow: auto;
|
||||
overscroll-behavior: contain;
|
||||
-webkit-overflow-scrolling: touch;
|
||||
touch-action: pan-y;
|
||||
}
|
||||
|
||||
.encoding-button {
|
||||
background-color: transparent;
|
||||
border: none;
|
||||
outline: none;
|
||||
padding: 0.25rem 0.5rem;
|
||||
color: var(--textPrimary);
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
border-radius: 0.2rem;
|
||||
white-space: nowrap;
|
||||
display: block;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.encoding-button:hover {
|
||||
background-color: var(--surfaceSecondary);
|
||||
}
|
||||
|
||||
.selected-encoding {
|
||||
white-space: nowrap;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.message {
|
||||
font-size: 1.25em;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user