Skip to content

Commit

Permalink
feat: define custom file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
bigaru committed Aug 9, 2024
1 parent 0e886f9 commit 219cd61
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/blocks/download/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"type": "string",
"default": "file"
},
"fileExt": {
"type": "string"
},
"metadata": {
"type": "object",
"default": {
Expand Down
22 changes: 20 additions & 2 deletions src/blocks/download/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InseriRoot, useDiscover } from '@inseri/lighthouse'
import { IconFileDownload } from '@tabler/icons-react'
import { BlockControls, InspectorControls } from '@wordpress/block-editor'
import type { BlockEditProps } from '@wordpress/blocks'
import { PanelBody, PanelRow, TextControl, ToolbarButton, ToolbarGroup } from '@wordpress/components'
import { PanelBody, PanelRow, TextControl, ToggleControl, ToolbarButton, ToolbarGroup } from '@wordpress/components'
import { useEffect, useState } from '@wordpress/element'
import { __ } from '@wordpress/i18n'
import { edit } from '@wordpress/icons'
Expand All @@ -15,7 +15,7 @@ import View from './view'

function EditComponent(props: BlockEditProps<Attributes>) {
const { isSelected, setAttributes, attributes } = props
const { blockId, metadata, inputKey, label, fileName } = attributes
const { blockId, metadata, inputKey, label, fileName, fileExt } = attributes

const [isWizardMode, setWizardMode] = useRecoilState(wizardState(blockId))
const isValueSet = !!inputKey
Expand Down Expand Up @@ -59,6 +59,24 @@ function EditComponent(props: BlockEditProps<Attributes>) {
<PanelRow>
<TextControl label={__('File Name', 'inseri-core')} value={fileName} onChange={(value) => setAttributes({ fileName: value })} />
</PanelRow>
<PanelRow>
<ToggleControl
label={__('Determine file extension automatically', 'inseri-core')}
checked={fileExt === undefined}
onChange={(checked) => setAttributes({ fileExt: checked ? undefined : '' })}
/>
</PanelRow>
{fileExt !== undefined && (
<PanelRow>
<TextControl
label={__('File Extension', 'inseri-core')}
value={fileExt ?? ''}
onChange={(value) => {
setAttributes({ fileExt: value })
}}
/>
</PanelRow>
)}
</PanelBody>
</InspectorControls>
{isWizardMode ? (
Expand Down
1 change: 1 addition & 0 deletions src/blocks/download/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Attributes {
inputKey: string
label: string
fileName: string
fileExt: string | undefined
metadata: {
name: string
}
Expand Down
5 changes: 3 additions & 2 deletions src/blocks/download/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { downloadLinkState, extensionState, wizardState } from './state'

export default function View(props: { attributes: Attributes; setAttributes?: (item: Partial<Attributes>) => void }) {
const { attributes, setAttributes } = props
const { blockId, inputKey, label, fileName } = attributes
const { blockId, inputKey, label, fileName, fileExt } = attributes

const [downloadLink, setDownloadLink] = useRecoilState(downloadLinkState(blockId))
const [extension, setExtension] = useRecoilState(extensionState(blockId))
Expand Down Expand Up @@ -52,7 +52,8 @@ export default function View(props: { attributes: Attributes; setAttributes?: (i
},
})

const fullFileName = fileName + (extension ? '.' + extension : '')
const preparedExt = fileExt ?? extension
const fullFileName = fileName + (preparedExt ? '.' + preparedExt : '')

return (
<Button style={{ color: '#fff' }} component="a" href={downloadLink} download={fullFileName} disabled={!isReady}>
Expand Down

0 comments on commit 219cd61

Please sign in to comment.