chore(tooling): add husky and lint-staged
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import { langEnum } from '../types/lang';
|
||||
import { langEnum } from "../types/lang"
|
||||
|
||||
import type { GenericObjectType } from '@contentstack/app-sdk/dist/src/types/common.types';
|
||||
import type { Lang } from '../types/lang';
|
||||
import type { GenericObjectType } from "@contentstack/app-sdk/dist/src/types/common.types"
|
||||
import type { Lang } from "../types/lang"
|
||||
import type {
|
||||
Config,
|
||||
ImageVaultAsset,
|
||||
InsertResponse,
|
||||
PublishDetails,
|
||||
} from "../types/imagevault";
|
||||
} from "../types/imagevault"
|
||||
|
||||
const metaIds = {
|
||||
[langEnum.de]: { altText: 68, title: 77 },
|
||||
@@ -16,20 +16,20 @@ const metaIds = {
|
||||
[langEnum.no]: { altText: 71, title: 79 },
|
||||
[langEnum.sv]: { altText: 74, title: 82 },
|
||||
[langEnum.en]: { altText: 69, title: 65 },
|
||||
};
|
||||
}
|
||||
|
||||
export function getMetaIds(lang: Lang) {
|
||||
return metaIds[lang];
|
||||
return metaIds[lang]
|
||||
}
|
||||
|
||||
export type EntryDataPublishDetails = {
|
||||
branch: string;
|
||||
contentTypeUid: string;
|
||||
locale: Lang;
|
||||
stackApiKey: string;
|
||||
title: string;
|
||||
uid: string;
|
||||
};
|
||||
branch: string
|
||||
contentTypeUid: string
|
||||
locale: Lang
|
||||
stackApiKey: string
|
||||
title: string
|
||||
uid: string
|
||||
}
|
||||
|
||||
export function getPublishDetails(
|
||||
baseUrl: string,
|
||||
@@ -42,43 +42,43 @@ export function getPublishDetails(
|
||||
uid,
|
||||
}: EntryDataPublishDetails
|
||||
): PublishDetails {
|
||||
const text = `${title} (${uid})`;
|
||||
const url = `${baseUrl}#!/stack/${stackApiKey}/content-type/${contentTypeUid}/${locale}/entry/${uid}/edit?branch=${branch}`;
|
||||
const text = `${title} (${uid})`
|
||||
const url = `${baseUrl}#!/stack/${stackApiKey}/content-type/${contentTypeUid}/${locale}/entry/${uid}/edit?branch=${branch}`
|
||||
|
||||
return { text, url };
|
||||
return { text, url }
|
||||
}
|
||||
|
||||
export function isInsertResponse(
|
||||
res: InsertResponse | GenericObjectType
|
||||
): res is InsertResponse {
|
||||
return (res as InsertResponse).MediaConversions !== undefined;
|
||||
return (res as InsertResponse).MediaConversions !== undefined
|
||||
}
|
||||
|
||||
export type ImageVaultDAMConfig = {
|
||||
imageVaultUrl: string;
|
||||
baseUrl: string;
|
||||
formatId: string;
|
||||
};
|
||||
imageVaultUrl: string
|
||||
baseUrl: string
|
||||
formatId: string
|
||||
}
|
||||
|
||||
export function isImageVaultDAMConfig(
|
||||
config: Record<string, string>
|
||||
): config is ImageVaultDAMConfig {
|
||||
return !!(config.baseUrl && config.formatId && config.imageVaultUrl);
|
||||
return !!(config.baseUrl && config.formatId && config.imageVaultUrl)
|
||||
}
|
||||
|
||||
// Utility function to convert InsertResponse to ImageVaultAsset, used mainly for custom field images
|
||||
// For RTE this function is not enough since rte:s also need attrs, like position and the size thats
|
||||
// For RTE this function is not enough since rte:s also need attrs, like position and the size thats
|
||||
// chosen in the editor
|
||||
export function insertResponseToImageVaultAsset(
|
||||
response: InsertResponse
|
||||
): ImageVaultAsset {
|
||||
const alt = response.Metadata?.find((meta) =>
|
||||
meta.Name.includes("AltText_")
|
||||
)?.Value;
|
||||
)?.Value
|
||||
|
||||
const caption = response.Metadata?.find((meta) =>
|
||||
meta.Name.includes("Title_")
|
||||
)?.Value;
|
||||
)?.Value
|
||||
|
||||
return {
|
||||
url: response.MediaConversions[0].Url,
|
||||
@@ -93,15 +93,15 @@ export function insertResponseToImageVaultAsset(
|
||||
height: response.MediaConversions[0].Height,
|
||||
aspectRatio: response.MediaConversions[0].FormatAspectRatio,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export type openImageVaultParams = {
|
||||
config: ImageVaultDAMConfig;
|
||||
entryData: EntryDataPublishDetails;
|
||||
onSuccess: (result: InsertResponse) => void;
|
||||
onClose?: () => void;
|
||||
};
|
||||
config: ImageVaultDAMConfig
|
||||
entryData: EntryDataPublishDetails
|
||||
onSuccess: (result: InsertResponse) => void
|
||||
onClose?: () => void
|
||||
}
|
||||
|
||||
export function openImageVault({
|
||||
config,
|
||||
@@ -110,8 +110,8 @@ export function openImageVault({
|
||||
onClose,
|
||||
}: openImageVaultParams) {
|
||||
if (window.ImageVault) {
|
||||
const publishDetails = getPublishDetails(config.baseUrl, entryData);
|
||||
const metaIdsForLocale = getMetaIds(entryData.locale);
|
||||
const publishDetails = getPublishDetails(config.baseUrl, entryData)
|
||||
const metaIdsForLocale = getMetaIds(entryData.locale)
|
||||
|
||||
const insertMediaWindowOptions: Config = {
|
||||
imageVaultUiUrl: config.imageVaultUrl,
|
||||
@@ -123,27 +123,27 @@ export function openImageVault({
|
||||
publishDetails,
|
||||
insertMultiple: false,
|
||||
success: (result) => {
|
||||
onSuccess(result.response);
|
||||
onSuccess(result.response)
|
||||
},
|
||||
close: () => {
|
||||
if (typeof onClose === "function") {
|
||||
onClose();
|
||||
onClose()
|
||||
}
|
||||
windowInserter.containerWindow?.close();
|
||||
windowInserter.containerWindow?.close()
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (metaIdsForLocale) {
|
||||
const additionalMetadataIds = Object.values(metaIdsForLocale).join(",");
|
||||
insertMediaWindowOptions.additionalMetadataIds = additionalMetadataIds;
|
||||
const additionalMetadataIds = Object.values(metaIdsForLocale).join(",")
|
||||
insertMediaWindowOptions.additionalMetadataIds = additionalMetadataIds
|
||||
}
|
||||
|
||||
const windowInserter = new window.ImageVault.InsertMediaWindow(
|
||||
insertMediaWindowOptions,
|
||||
`left=0,top=0,width=${window.screen.width},height=${window.screen.height}`
|
||||
);
|
||||
windowInserter.openImageVault();
|
||||
)
|
||||
windowInserter.openImageVault()
|
||||
} else {
|
||||
console.error("Missing ImageVault global. ImageVault script not loaded?");
|
||||
console.error("Missing ImageVault global. ImageVault script not loaded?")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user