-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
DamFileDownloadLinkBlock
(#1228)
Can be used to download a DAM file or open it in a new tab. --------- Co-authored-by: Florian Ragger <florian.ragger@vivid-planet.com> Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
- Loading branch information
1 parent
0597b1e
commit 3ee8c7a
Showing
15 changed files
with
508 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@comet/cms-admin": minor | ||
"@comet/cms-site": minor | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
Add a `DamFileDownloadLinkBlock` that can be used to download a file or open it in a new tab | ||
|
||
Also, add new `/dam/files/download/:hash/:fileId/:filename` endpoint for downloading assets. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import { createLinkBlock, ExternalLinkBlock, InternalLinkBlock } from "@comet/cms-admin"; | ||
import { createLinkBlock, DamFileDownloadLinkBlock, ExternalLinkBlock, InternalLinkBlock } from "@comet/cms-admin"; | ||
import { NewsLinkBlock } from "@src/news/blocks/NewsLinkBlock"; | ||
|
||
export const LinkBlock = createLinkBlock({ | ||
supportedBlocks: { internal: InternalLinkBlock, external: ExternalLinkBlock, news: NewsLinkBlock }, | ||
supportedBlocks: { | ||
internal: InternalLinkBlock, | ||
external: ExternalLinkBlock, | ||
news: NewsLinkBlock, | ||
damFileDownload: DamFileDownloadLinkBlock, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { ExternalLinkBlock } from "@comet/blocks-api"; | ||
import { createLinkBlock, InternalLinkBlock } from "@comet/cms-api"; | ||
import { createLinkBlock, DamFileDownloadLinkBlock, InternalLinkBlock } from "@comet/cms-api"; | ||
import { NewsLinkBlock } from "@src/news/blocks/news-link.block"; | ||
|
||
export const LinkBlock = createLinkBlock({ supportedBlocks: { internal: InternalLinkBlock, external: ExternalLinkBlock, news: NewsLinkBlock } }); | ||
export const LinkBlock = createLinkBlock({ | ||
supportedBlocks: { internal: InternalLinkBlock, external: ExternalLinkBlock, news: NewsLinkBlock, damFileDownload: DamFileDownloadLinkBlock }, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
packages/admin/cms-admin/src/dam/blocks/DamFileDownloadLinkBlock.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { gql } from "@apollo/client"; | ||
import { Field, FinalFormSelect, messages } from "@comet/admin"; | ||
import { Delete } from "@comet/admin-icons"; | ||
import { AdminComponentButton, AdminComponentPaper, BlockCategory, BlockInterface, BlocksFinalForm, createBlockSkeleton } from "@comet/blocks-admin"; | ||
import { Box, Divider, MenuItem, Typography } from "@mui/material"; | ||
import * as React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
import { DamFileDownloadLinkBlockData, DamFileDownloadLinkBlockInput } from "../../blocks.generated"; | ||
import { CmsBlockContext } from "../../blocks/CmsBlockContextProvider"; | ||
import { DamPathLazy } from "../../form/file/DamPathLazy"; | ||
import { FileField } from "../../form/file/FileField"; | ||
import { GQLDamFileDownloadLinkFileQuery, GQLDamFileDownloadLinkFileQueryVariables } from "./DamFileDownloadLinkBlock.generated"; | ||
|
||
export const DamFileDownloadLinkBlock: BlockInterface<DamFileDownloadLinkBlockData, DamFileDownloadLinkBlockData, DamFileDownloadLinkBlockInput> = { | ||
...createBlockSkeleton(), | ||
|
||
name: "DamFileDownloadLink", | ||
|
||
displayName: <FormattedMessage id="comet.blocks.damFileDownloadLink" defaultMessage="CMS Asset" />, | ||
|
||
previewContent: (state) => (state.file ? [{ type: "text", content: state.file?.name }] : []), | ||
|
||
category: BlockCategory.Media, | ||
|
||
defaultValues: () => ({ | ||
openFileType: "Download", | ||
}), | ||
|
||
state2Output: (state) => ({ | ||
fileId: state.file?.id ?? undefined, | ||
openFileType: state.openFileType, | ||
}), | ||
|
||
output2State: async (output, { apolloClient }: CmsBlockContext): Promise<DamFileDownloadLinkBlockData> => { | ||
const ret: DamFileDownloadLinkBlockData = { | ||
openFileType: output.openFileType, | ||
}; | ||
|
||
if (output.fileId === undefined) { | ||
return ret; | ||
} | ||
|
||
const { data } = await apolloClient.query<GQLDamFileDownloadLinkFileQuery, GQLDamFileDownloadLinkFileQueryVariables>({ | ||
query: gql` | ||
query DamFileDownloadLinkFile($id: ID!) { | ||
damFile(id: $id) { | ||
id | ||
name | ||
fileUrl | ||
} | ||
} | ||
`, | ||
variables: { id: output.fileId }, | ||
}); | ||
|
||
const { damFile } = data; | ||
|
||
ret.file = { | ||
id: damFile.id, | ||
name: damFile.name, | ||
fileUrl: damFile.fileUrl, | ||
}; | ||
|
||
return ret; | ||
}, | ||
|
||
definesOwnPadding: true, | ||
|
||
AdminComponent: ({ state, updateState }) => { | ||
return ( | ||
<BlocksFinalForm<{ | ||
openFileType: DamFileDownloadLinkBlockData["openFileType"]; | ||
file?: DamFileDownloadLinkBlockData["file"]; | ||
}> | ||
onSubmit={(newValues) => { | ||
updateState({ | ||
file: newValues.file ?? undefined, | ||
openFileType: newValues.openFileType, | ||
}); | ||
}} | ||
initialValues={{ | ||
file: state.file, | ||
openFileType: state.openFileType ?? "Download", | ||
}} | ||
> | ||
{state.file === undefined ? ( | ||
<Field name="file" component={FileField} fullWidth /> | ||
) : ( | ||
<AdminComponentPaper disablePadding> | ||
<Box padding={3}> | ||
<Typography variant="subtitle1">{state.file.name}</Typography> | ||
<Typography variant="body1" color="textSecondary"> | ||
<DamPathLazy fileId={state.file.id} /> | ||
</Typography> | ||
</Box> | ||
<Divider /> | ||
<AdminComponentButton startIcon={<Delete />} onClick={() => updateState({ ...state, file: undefined })}> | ||
<FormattedMessage {...messages.empty} /> | ||
</AdminComponentButton> | ||
</AdminComponentPaper> | ||
)} | ||
<Divider /> | ||
<AdminComponentPaper> | ||
<Field | ||
name="openFileType" | ||
fullWidth | ||
label={<FormattedMessage id="comet.blocks.damFileDownloadLink.openFileType" defaultMessage="Open file" />} | ||
> | ||
{(props) => ( | ||
<> | ||
<FinalFormSelect {...props}> | ||
<MenuItem value="Download"> | ||
<FormattedMessage | ||
id="comet.blocks.damFileDownloadLink.openFileType.download" | ||
defaultMessage="as a download" | ||
/> | ||
</MenuItem> | ||
<MenuItem value="NewTab"> | ||
<FormattedMessage id="comet.blocks.damFileDownloadLink.openFileType.newTab" defaultMessage="in a new tab" /> | ||
</MenuItem> | ||
</FinalFormSelect> | ||
</> | ||
)} | ||
</Field> | ||
</AdminComponentPaper> | ||
</BlocksFinalForm> | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.