Skip to content

Commit

Permalink
feat: Provide a document-based component capable of displaying CSV co…
Browse files Browse the repository at this point in the history
…ntent. (close  #1012)
  • Loading branch information
cnouguier committed Dec 9, 2024
1 parent 27be67c commit 18ffb92
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 19 deletions.
55 changes: 55 additions & 0 deletions core/client/components/document/KCsv.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<template>
<q-scroll-area class="fit">
<table v-if="data">
<template v-for="(row, index) in data" :key="index">
<tr>
<template v-for="cell in row" :key="cell">
<td>{{ cell }}</td>
</template>
</tr>
</template>
</table>
</q-scroll-area>
</template>

<script setup>
import { ref, watch } from 'vue'
import Papa from 'papaparse'
import { Document } from '../../document.js'
// Props
const props = defineProps({
url: {
type: String,
default: null
},
localize: {
type: Boolean,
default: true
},
options: {
type: Object,
default: () => null
}
})
// Data
const data = ref(null)
// Watch
watch(() => props.url, async (value) => {
const response = await Document.fetchUrl(value, props.localize)
if (response?.ok) {
const csv = await response.text()
data.value = Papa.parse(csv, props.options).data
}
else data.value = null
}, { immediate: true })
</script>
<style lang="scss">
td {
padding-left: 4px;
padding-right: 4px;
}
</style>
11 changes: 10 additions & 1 deletion core/client/components/document/KMarkdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<script setup>
import { ref, watch } from 'vue'
import showdown from 'showdown'
import { Document } from '../../document.js'
// Props
Expand All @@ -20,6 +21,10 @@ const props = defineProps({
localize: {
type: Boolean,
default: true
},
options: {
type: Object,
default: () => null
}
})
Expand All @@ -29,7 +34,11 @@ const html = ref(null)
// Watch
watch(() => props.url, async (value) => {
const response = await Document.fetchUrl(value, props.localize)
if (response?.ok) html.value = Document.sanitizeHtml(Document.convertMdToHtml(await response.text()))
if (response?.ok) {
const markdown = await response.text()
const converter = new showdown.Converter(props.options || Document.options.mdConverter)
html.value = Document.sanitizeHtml(converter.makeHtml(markdown))
}
else html.value = null
}, { immediate: true })
</script>
15 changes: 7 additions & 8 deletions core/client/components/document/KVideo.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<template>
<div v-if="url" class="fit">
<video
:src="url"
:controls="controls"
:autoplay="autoplay"
class="fit"
/>
</div>
<video
v-if="url"
:src="url"
:controls="controls"
:autoplay="autoplay"
class="fit"
/>
</template>

<script setup>
Expand Down
16 changes: 6 additions & 10 deletions core/client/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import _ from 'lodash'
import logger from 'loglevel'
import config from 'config'
import sanitize from 'sanitize-html'
import showdown from 'showdown'
import { i18n } from './i18n.js'

export const Document = {
Expand All @@ -12,6 +11,10 @@ export const Document = {
htm: 'document/KHtml',
html: 'document/KHtml',
'text/html': 'document/KHtml',
txt: 'document/KHtml',
'text/plain': 'document/KHtml',
csv: 'document/KCsv',
'text/csv': 'document/KCsv',
md: 'document/KMarkdown',
'text/markdown': 'document/KMarkdown',
pdf: 'document/KPdf',
Expand All @@ -27,16 +30,14 @@ export const Document = {
'image/gif': 'document/KImage',
svg: 'document/KImage',
'image/svg+xml': 'document/KImage',
txt: 'document/KHtml',
'text/plain': 'document/KHtml',
webp: 'document/KImage',
'image/webp': 'document/KImage',
'mp4': 'document/KVideo',
'video/mp4': 'document/KVideo',
'mkv': 'document/KVideo',
'video/x-matroska': 'document/KVideo',
'mov': 'document/KVideo',
'video/quicktime': 'document/KVideo',
webp: 'document/KImage',
'image/webp': 'document/KImage',
webm: 'document/KVideo',
'video/webm': 'document/KVideo'
},
Expand All @@ -60,11 +61,6 @@ export const Document = {
if (_.isNil(html)) return null
return sanitize(html, this.options.htmlSanitizer)
},
convertMdToHtml (md) {
if (_.isNil(md)) return null
const converter = new showdown.Converter(this.options.mdConverter)
return converter.makeHtml(md)
},
async fetchUrl (url, localize) {
if (_.isEmpty(url)) return null
// localize the url if needed
Expand Down

0 comments on commit 18ffb92

Please sign in to comment.