UBERF-8180 Text file preview (#6633)

Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
This commit is contained in:
Alexander Onnikov 2024-09-19 19:19:09 +07:00 committed by GitHub
parent 892bbbe8b8
commit e248aec920
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 104 additions and 5 deletions

View File

@ -647,7 +647,7 @@ export function createModel (builder: Builder): void {
presentation.class.FilePreviewExtension,
core.space.Model,
{
contentType: ['application/pdf', 'application/json', 'text/*'],
contentType: ['application/pdf'],
alignment: 'float',
component: view.component.PDFViewer,
extension: presentation.extension.FilePreviewExtension
@ -655,6 +655,18 @@ export function createModel (builder: Builder): void {
view.extension.PDF
)
builder.createDoc(
presentation.class.FilePreviewExtension,
core.space.Model,
{
contentType: ['application/json', 'text/*'],
alignment: 'float',
component: view.component.TextViewer,
extension: presentation.extension.FilePreviewExtension
},
view.extension.Text
)
createAction(
builder,
{

View File

@ -88,7 +88,8 @@ export default mergeIds(viewId, view, {
AudioViewer: '' as AnyComponent,
ImageViewer: '' as AnyComponent,
VideoViewer: '' as AnyComponent,
PDFViewer: '' as AnyComponent
PDFViewer: '' as AnyComponent,
TextViewer: '' as AnyComponent
},
string: {
Table: '' as IntlString,
@ -150,6 +151,7 @@ export default mergeIds(viewId, view, {
Audio: '' as Ref<FilePreviewExtension>,
Image: '' as Ref<FilePreviewExtension>,
Video: '' as Ref<FilePreviewExtension>,
PDF: '' as Ref<FilePreviewExtension>
PDF: '' as Ref<FilePreviewExtension>,
Text: '' as Ref<FilePreviewExtension>
}
})

View File

@ -14,7 +14,14 @@
-->
<script lang="ts">
import { type Blob, type Ref } from '@hcengineering/core'
import { Button, Component, Label, resizeObserver, deviceOptionsStore as deviceInfo } from '@hcengineering/ui'
import {
Button,
Component,
Label,
resizeObserver,
deviceOptionsStore as deviceInfo,
Loading
} from '@hcengineering/ui'
import presentation from '../plugin'
@ -34,9 +41,11 @@
let minHeight: number | undefined
$: parentHeight = ($deviceInfo.docHeight * 80) / 100
let loading = true
let previewType: FilePreviewExtension | undefined = undefined
$: void getPreviewType(contentType, $previewTypes).then((res) => {
previewType = res
loading = false
})
const updateHeight = (
@ -91,6 +100,8 @@
</div>
{:else if previewType !== undefined}
<Component is={previewType.component} props={{ value: file, name, contentType, metadata, ...props, fit }} />
{:else if loading}
<Loading />
{:else}
<div class="flex-col items-center flex-gap-3">
<Label label={presentation.string.ContentTypeNotSupported} />

View File

@ -0,0 +1,72 @@
<!--
// Copyright © 2024 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
-->
<script lang="ts">
import { type Blob, type Ref } from '@hcengineering/core'
import { getFileUrl } from '@hcengineering/presentation'
import { Loading, Scroller } from '@hcengineering/ui'
export let value: Ref<Blob>
export let name: string
export let fit: boolean = false
$: void fetchFile(value, name)
let loading = true
let text: string | undefined = undefined
async function fetchFile (value: Ref<Blob>, name: string): Promise<void> {
loading = true
const src = getFileUrl(value, name)
const res = await fetch(src)
text = await res.text()
loading = false
}
</script>
{#if loading}
<div class="flex-center w-full h-full clear-mins">
<Loading />
</div>
{:else}
<div class="container h-full w-full" class:fit>
<Scroller horizontal padding="0 1rem">
<pre class="select-text">{text}</pre>
</Scroller>
</div>
{/if}
<style lang="scss">
.container {
max-height: 80vh;
overflow: hidden;
border: 1px solid var(--theme-button-border);
border-radius: 0.25rem;
&.fit {
min-height: 100%;
}
&:not(.fit) {
height: 80vh;
min-height: 20rem;
}
}
pre {
font-family: var(--mono-font);
}
</style>

View File

@ -99,6 +99,7 @@ import AudioViewer from './components/viewer/AudioViewer.svelte'
import ImageViewer from './components/viewer/ImageViewer.svelte'
import VideoViewer from './components/viewer/VideoViewer.svelte'
import PDFViewer from './components/viewer/PDFViewer.svelte'
import TextViewer from './components/viewer/TextViewer.svelte'
import { blobImageMetadata, blobVideoMetadata } from './blob'
@ -295,7 +296,8 @@ export default async (): Promise<Resources> => ({
AudioViewer,
ImageViewer,
VideoViewer,
PDFViewer
PDFViewer,
TextViewer
},
popup: {
PositionElementAlignment