Skip to content

Commit

Permalink
Merge pull request #3 from marcolink/feat/update-controls
Browse files Browse the repository at this point in the history
feat: update controls
  • Loading branch information
marcolink authored Jul 23, 2024
2 parents ec31777 + e737e13 commit 52f992a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const BLURHASH_DEFAULTS = {
imageField: 'image',
blurhashField: 'blurhash',
componentX: 4,
componentY: 3,
}
5 changes: 3 additions & 2 deletions src/hooks/useCreateBlurhash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {PlainClientAPI} from "contentful-management";
import {useMemo, useRef} from "react";
import {useQuery} from "@tanstack/react-query";
import {useInstanceParameters} from "./useInstanceParameters";
import {BLURHASH_DEFAULTS} from "../constants";

type InputProps = {
assetId?: string
Expand All @@ -20,8 +21,8 @@ type Props = InputProps & Required<Pick<InputProps, 'width' | 'height' | 'compon
const defaultProps: Props = {
width: 320,
height: 320,
componentX: 6,
componentY: 6,
componentX: BLURHASH_DEFAULTS.componentX,
componentY: BLURHASH_DEFAULTS.componentY,
language: 'en-US'
}

Expand Down
20 changes: 20 additions & 0 deletions src/locations/ConfigScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {useSDK} from '@contentful/react-apps-toolkit';
import {css} from 'emotion';
import {useCallback, useEffect, useState} from 'react';
import {makeContentType} from "../logic/makeContentType";
import {BLURHASH_DEFAULTS} from "../constants";

const CONTENT_TYPE_NAME = 'Blurhash Image'
export const CONTENT_TYPE_ID = 'blurhashImage'
Expand Down Expand Up @@ -42,6 +43,25 @@ const ConfigScreen = () => {
const newContentType = await sdk.cma.contentType.createWithId(params, makeContentType(CONTENT_TYPE_NAME))
await sdk.cma.contentType.publish(params, newContentType)
console.log({newContentType})

const editorInterface = await sdk.cma.editorInterface.get({
contentTypeId: newContentType.sys.id
})

const blurhashFieldControls = {
fieldId: BLURHASH_DEFAULTS.blurhashField,
widgetId: sdk.ids.app,
widgetNamespace: "app",
settings: {
sourceImageFieldId: BLURHASH_DEFAULTS.imageField,
componentX: BLURHASH_DEFAULTS.componentX,
componentY: BLURHASH_DEFAULTS.componentY
}
}

editorInterface.controls = editorInterface.controls?.filter(control => control.fieldId !== BLURHASH_DEFAULTS.blurhashField) || []
editorInterface.controls.push(blurhashFieldControls)
await sdk.cma.editorInterface.update({contentTypeId: newContentType.sys.id}, editorInterface)
}

return {
Expand Down
5 changes: 3 additions & 2 deletions src/logic/makeContentType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {CreateContentTypeProps} from "contentful-management/dist/typings/entities/content-type";
import {BLURHASH_DEFAULTS} from "../constants";

export const makeContentType = (contentTypeName: string): CreateContentTypeProps => ({
name: contentTypeName,
Expand All @@ -12,15 +13,15 @@ export const makeContentType = (contentTypeName: string): CreateContentTypeProps
localized: false,
},
{
id: 'image',
id: BLURHASH_DEFAULTS.imageField,
name: 'Image',
required: true,
type: 'Link',
linkType: 'Asset',
localized: false,
},
{
id: 'blurhash',
id: BLURHASH_DEFAULTS.blurhashField,
name: 'Blurhash',
required: true,
type: 'Symbol',
Expand Down

0 comments on commit 52f992a

Please sign in to comment.