Skip to content

Commit

Permalink
feat(app-headless-cms): add ref field settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed May 7, 2024
1 parent bbdd960 commit ccab286
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ const fieldSchema = zod.object({
.optional(),
renderer: zod
.object({
name: shortString
name: shortString,
settings: zod.object({}).passthrough().nullable().optional()
})
.optional()
.nullable()
Expand Down
2 changes: 2 additions & 0 deletions packages/api-headless-cms/src/graphql/schema/contentModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export const createModelsSchema = ({ context }: Params): ICmsGraphQLSchemaPlugin
}
input CmsFieldRendererInput {
name: String
settings: JSON
}
input CmsFieldValidationInput {
Expand Down Expand Up @@ -235,6 +236,7 @@ export const createModelsSchema = ({ context }: Params): ICmsGraphQLSchemaPlugin
type CmsFieldRenderer {
name: String
settings: JSON
}
type CmsPredefinedValue {
Expand Down
5 changes: 5 additions & 0 deletions packages/app-headless-cms-common/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ export interface CmsModelFieldTypePlugin extends Plugin {
};
}

export interface CmsModelFieldRendererSettingsProps {
field: CmsModelField;
}

export interface CmsModelFieldRendererProps {
field: CmsModelField;
Label: React.ComponentType<React.PropsWithChildren>;
Expand Down Expand Up @@ -313,6 +317,7 @@ export interface CmsModelFieldRendererPlugin extends Plugin {
* ```
*/
render(props: CmsModelFieldRendererProps): React.ReactNode;
renderSettings?: (props: CmsModelFieldRendererSettingsProps) => React.ReactNode;
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/app-headless-cms-common/src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type CmsModelField<T = unknown> = T & {
renderer:
| {
name: string;
settings?: Record<string, any>;
}
/**
* Use this only for programmatic assignment of renderers.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import React, { useEffect } from "react";
import { plugins } from "@webiny/plugins";
import React from "react";
import { Grid, Cell } from "@webiny/ui/Grid";
import { CmsModelFieldRendererPlugin, CmsModelField } from "~/types";
import { i18n } from "@webiny/app/i18n";
import { Radio, RadioGroup } from "@webiny/ui/Radio";
import { css } from "emotion";
import { validation } from "@webiny/validation";
import { useModel, useModelField } from "~/admin/hooks";
import { useForm, Bind } from "@webiny/form";
import { Alert } from "@webiny/ui/Alert";
import { useBind } from "@webiny/form";
import { allowCmsLegacyRichTextInput } from "~/utils/allowCmsLegacyRichTextInput";
import { Typography } from "@webiny/ui/Typography";
import { RendererOptions } from "./AppearanceTab/RendererOptions";
import { LegacyRichTextInput } from "./AppearanceTab/LegacyRichTextInput";
import { useRendererPlugins } from "./AppearanceTab/useRendererPlugins";
import { useSelectFirstAvailableRenderer } from "./AppearanceTab/useSelectFirstAvailableRenderer";

const t = i18n.ns("app-headless-cms/admin/content-model-editor/tabs/appearance-tab");

const style = {
topLabel: css({
marginBottom: 25
}),
noComponentsMessage: css({
textAlign: "center",
padding: 25
Expand All @@ -28,52 +25,21 @@ const style = {
})
};

/**
* We want the "hidden" renderer to always be the last one in the list.
* @param a
* @param b
*/
const hiddenLast = (a: CmsModelFieldRendererPlugin, b: CmsModelFieldRendererPlugin) => {
if (a.renderer.rendererName === "hidden") {
return 1;
}

if (b.renderer.rendererName === "hidden") {
return -1;
}

return 0;
};

const AppearanceTab = () => {
const form = useForm<CmsModelField>();
const { model } = useModel();
const { field, fieldPlugin } = useModelField();
const renderers = useRendererPlugins();

const renderPlugins = plugins
.byType<CmsModelFieldRendererPlugin>("cms-editor-field-renderer")
.filter(item => item.renderer.canUse({ field, fieldPlugin, model }))
.sort(hiddenLast);
const rendererName = useBind({
name: "renderer.name",
validate: validation.create("required")
});

useEffect((): void => {
// If the currently selected render plugin is no longer available, select the first available one.
const currentlySelectedRenderAvailable = renderPlugins.find(
item => item.renderer.rendererName === field.renderer.name
);

if (currentlySelectedRenderAvailable) {
return;
}
useSelectFirstAvailableRenderer(rendererName);

if (renderPlugins[0]) {
form.setValue("renderer.name", renderPlugins[0].renderer.rendererName);
return;
}
const selectedPlugin = rendererName.value
? renderers.find(pl => pl.renderer.rendererName === rendererName.value)
: undefined;

console.info(`No renderers for field ${field.fieldId} found.`, field);
}, [field.id, field.multipleValues, field.predefinedValues?.enabled]);

if (renderPlugins.length === 0) {
if (renderers.length === 0) {
return (
<Grid>
<Cell
Expand All @@ -85,35 +51,18 @@ const AppearanceTab = () => {
}

return (
<Grid>
{allowCmsLegacyRichTextInput && (
<Cell span={6}>
<Alert title={"You have legacy editor enabled"} type={"info"}>
Your project has been upgraded from an older Webiny version, with EditorJS
as the default rich text editor. We recommend switching to the new Lexical
rich text editor, where possible.
<br />
<br />
Read more about this in our{" "}
<a
href={"https://webiny.link/hcms-legacy-rte-support"}
rel="noreferrer"
target={"_blank"}
>
upgrade guide
</a>
.
</Alert>
</Cell>
)}
<Cell span={12}>
<div
className={style.topLabel}
>{t`Choose a component that will render the field:`}</div>
<Bind name={"renderer.name"} validate={validation.create("required")}>
<RadioGroup>
<>
<Grid>
{allowCmsLegacyRichTextInput && (
<Cell span={6}>
<LegacyRichTextInput />
</Cell>
)}
<Cell span={12}>Choose a component that will render the field:</Cell>
<Cell span={12}>
<RadioGroup {...rendererName}>
{({ onChange, getValue }) =>
renderPlugins.map(item => {
renderers.map(item => {
const setValue = onChange(item.renderer.rendererName);
return (
<div key={item.name} className={style.radioContainer}>
Expand All @@ -135,9 +84,10 @@ const AppearanceTab = () => {
})
}
</RadioGroup>
</Bind>
</Cell>
</Grid>
</Cell>
</Grid>
<RendererOptions plugin={selectedPlugin} />
</>
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react";
import { Alert } from "@webiny/ui/Alert";

export const LegacyRichTextInput = () => {
return (
<Alert title={"You have legacy editor enabled"} type={"info"}>
Your project has been upgraded from an older Webiny version, with EditorJS as the
default rich text editor. We recommend switching to the new Lexical rich text editor,
where possible.
<br />
<br />
Read more about this in our{" "}
<a
href={"https://webiny.link/hcms-legacy-rte-support"}
rel="noreferrer"
target={"_blank"}
>
upgrade guide
</a>
.
</Alert>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from "react";
import { CmsModelFieldRendererPlugin } from "@webiny/app-headless-cms-common/types";
import { useModelField } from "~/admin/components/ModelFieldProvider";
import { Cell, Grid } from "@webiny/ui/Grid";

interface RendererOptionsProps {
plugin: CmsModelFieldRendererPlugin | undefined;
}

export const RendererOptions = ({ plugin }: RendererOptionsProps) => {
const { field } = useModelField();
if (!plugin || !plugin.renderer.renderSettings) {
return null;
}

const settings = plugin.renderer.renderSettings({ field });

if (!settings) {
return null;
}

return (
<Grid>
<Cell span={12}>Renderer Settings:</Cell>
{settings}
</Grid>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { plugins } from "@webiny/plugins";
import { CmsModelFieldRendererPlugin } from "@webiny/app-headless-cms-common/types";
import { useModel } from "~/admin/components/ModelProvider";
import { useModelField } from "~/admin/components/ModelFieldProvider";

/**
* We want the "hidden" renderer to always be the last one in the list.
* @param a
* @param b
*/
const hiddenLast = (a: CmsModelFieldRendererPlugin, b: CmsModelFieldRendererPlugin) => {
if (a.renderer.rendererName === "hidden") {
return 1;
}

if (b.renderer.rendererName === "hidden") {
return -1;
}

return 0;
};

export const useRendererPlugins = () => {
const { model } = useModel();
const { field, fieldPlugin } = useModelField();

return plugins
.byType<CmsModelFieldRendererPlugin>("cms-editor-field-renderer")
.filter(item => item.renderer.canUse({ field, fieldPlugin, model }))
.sort(hiddenLast);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useEffect } from "react";
import { useRendererPlugins } from "~/admin/components/FieldEditor/EditFieldDialog/AppearanceTab/useRendererPlugins";
import { useModelField } from "~/admin/components/ModelFieldProvider";
import { UseBindHook } from "@webiny/form";

export const useSelectFirstAvailableRenderer = (bind: UseBindHook) => {
const renderers = useRendererPlugins();
const { field } = useModelField();

useEffect(() => {
// If the currently selected render plugin is no longer available, select the first available one.
const currentlySelectedRenderAvailable = renderers.find(
item => item.renderer.rendererName === field.renderer.name
);

if (currentlySelectedRenderAvailable) {
return;
}

if (renderers[0]) {
bind.onChange(renderers[0].renderer.rendererName);
return;
}

console.info(`No renderers for field ${field.fieldId} found.`, field);
}, [field.id, field.multipleValues, field.predefinedValues?.enabled]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const FIELDS_FIELDS = `
multipleValues
renderer {
name
settings
}
validation {
name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export const AdvancedMultipleReferenceField = (props: AdvancedMultipleReferenceF
{linkEntryDialogModel && (
<ReferencesDialog
{...props}
field={field}
multiple={true}
values={values}
contentModel={linkEntryDialogModel}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Bind } from "@webiny/form";
import React from "react";
import { Select } from "@webiny/ui/Select";
import { Cell } from "@webiny/ui/Grid";

export interface MultiRefFieldSettings {
newItemPosition: "first" | "last";
}

export const AdvancedMultipleReferenceSettings = () => {
return (
<>
<Cell span={12}>
<Bind name={"renderer.settings.newItemPosition"} defaultValue={"last"}>
<Select
label={"New item position"}
description={"Where should the new items be added?"}
>
<option value={"first"}>Top of the list</option>
<option value={"last"}>Bottom of the list</option>
</Select>
</Bind>
</Cell>
</>
);
};
Loading

0 comments on commit ccab286

Please sign in to comment.