Add New Detail Type :: Image

This commit is contained in:
Ibrahim Nergiz 2023-08-09 15:41:24 +03:00 committed by GitHub
parent 2cbcc8bb1d
commit 44df0c3d09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 2 deletions

View file

@ -21,6 +21,13 @@
</a> </a>
</div> </div>
</template> </template>
<template v-else-if="detail.type === 'image'">
<div class="tooltip tooltip-primary tooltip-right" :data-tip="detail.href">
<a class="btn btn-primary btn-xs" :href="detail.href" target="_blank">
<img :src="detail.href" class="w-6 h-6">
</a>
</div>
</template>
<template v-else-if="detail.type === 'markdown'"> <template v-else-if="detail.type === 'markdown'">
<ClientOnly> <ClientOnly>
<Markdown :source="detail.text" /> <Markdown :source="detail.text" />

View file

@ -33,7 +33,13 @@ export type Detail = BaseDetail & {
copyable?: boolean; copyable?: boolean;
}; };
export type AnyDetail = DateDetail | CurrencyDetail | LinkDetail | MarkdownDetail | Detail; type ImageDetail = BaseDetail & {
type: "image";
text: string;
href: string;
};
export type AnyDetail = DateDetail | CurrencyDetail | LinkDetail | MarkdownDetail | Detail | ImageDetail;
export type Details = Array<Detail | AnyDetail>; export type Details = Array<Detail | AnyDetail>;
@ -46,6 +52,8 @@ export function filterZeroValues(details: Details): Details {
return !!detail.text; return !!detail.text;
case "link": case "link":
return !!detail.text && !!detail.href; return !!detail.text && !!detail.href;
case "image":
return !!detail.text && !!detail.href;
case undefined: case undefined:
case "text": case "text":
case "markdown": case "markdown":

View file

@ -38,7 +38,12 @@ export function fmtCurrency(value: number | string, currency = "USD", locale = "
return formatter.format(value); return formatter.format(value);
} }
export function isImage(url: string) {
return /\.(jpg|jpeg|png|webp|avif|gif|svg)$/.test(url);
}
export type MaybeUrlResult = { export type MaybeUrlResult = {
isImage: boolean,
isUrl: boolean; isUrl: boolean;
url: string; url: string;
text: string; text: string;
@ -46,6 +51,7 @@ export type MaybeUrlResult = {
export function maybeUrl(str: string): MaybeUrlResult { export function maybeUrl(str: string): MaybeUrlResult {
const result: MaybeUrlResult = { const result: MaybeUrlResult = {
isImage: isImage(str),
isUrl: str.startsWith("http://") || str.startsWith("https://"), isUrl: str.startsWith("http://") || str.startsWith("https://"),
url: "", url: "",
text: "", text: "",

View file

@ -186,7 +186,14 @@
* Support Special URL Syntax * Support Special URL Syntax
*/ */
const url = maybeUrl(field.textValue); const url = maybeUrl(field.textValue);
if (url.isUrl) { if (url.isImage) {
return {
type: "image",
name: field.name,
text: url.text,
href: url.url,
} as AnyDetail;
} else {
return { return {
type: "link", type: "link",
name: field.name, name: field.name,