Skip to content

Commit

Permalink
feat: create rich-text field
Browse files Browse the repository at this point in the history
closes #811
  • Loading branch information
Ashu96 authored May 21, 2020
1 parent 7b73764 commit ee1dadb
Show file tree
Hide file tree
Showing 27 changed files with 785 additions and 16 deletions.
10 changes: 6 additions & 4 deletions packages/api-headless-cms/src/handler/plugins/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export default ({ type }) => [
typeDefs: gql`
${i18nFieldType("CmsString", "String")}
${i18nFieldInput("CmsString", "String")}
${i18nFieldType("CmsJSON", "JSON")}
${i18nFieldInput("CmsJSON", "JSON")}
${i18nFieldType("CmsAny", "Any")}
${i18nFieldInput("CmsAny", "Any")}
Expand All @@ -75,10 +77,10 @@ export default ({ type }) => [
fields: [String]
operator: String
}
${contentModelGroup.getTypeDefs(type)}
${meta.typeDefs}
type SecurityUser {
id: ID
firstName: String
Expand All @@ -95,7 +97,7 @@ export default ({ type }) => [
next: String
previous: String
}
type CmsListMeta {
cursors: CmsCursors
hasNextPage: Boolean
Expand Down Expand Up @@ -166,7 +168,7 @@ export default ({ type }) => [
input CmsFieldRendererInput {
name: String
}
type CmsContentModelField {
_id: ID
label: CmsString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import ref from "./ref";
import number from "./number";
import boolean from "./boolean";
import datetime from "./datetime";
import richText from "./richText";

export default [text, ref, number, datetime, boolean, longText];
export default [text, ref, number, datetime, boolean, longText, richText];
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { CmsModelFieldToGraphQLPlugin } from "@webiny/api-headless-cms/types";

const plugin: CmsModelFieldToGraphQLPlugin = {
name: "cms-model-field-to-graphql-rich-text",
type: "cms-model-field-to-graphql",
fieldType: "rich-text",
read: {
createTypeField({ field }) {
const localeArg = "(locale: String)";
return `${field.fieldId}${localeArg}: JSON`;
},
createGetFilters({ field }) {
return `${field.fieldId}: JSON`;
},
createResolver({ field }) {
return (instance, args) => {
return instance[field.fieldId].value(args.locale);
};
}
},
manage: {
createResolver({ field }) {
return instance => {
return instance[field.fieldId];
};
},
createTypeField({ field }) {
return field.fieldId + ": CmsJSON";
},
createInputField({ field }) {
return field.fieldId + ": CmsJSONInput";
}
}
};

export default plugin;
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ const createListFilters = ({ field }) => {
return `
# Matches if the field is equal to the given value
${field.fieldId}: String
# Matches if the field is not equal to the given value
${field.fieldId}_not: String
# Matches if the field value equal one of the given values
${field.fieldId}_in: [String]
# Matches if the field value does not equal any of the given values
${field.fieldId}_not_in: [String]
# Matches if given value is a substring of the the field value
${field.fieldId}_contains: String
# Matches if given value is not a substring of the the field value
${field.fieldId}_not_contains: String
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import number from "./number";
import boolean from "./boolean";
import datetime from "./datetime";
import json from "./json";
import richText from "./richText";

export default [text, ref, number, boolean, datetime, json, longText];
export default [text, ref, number, boolean, datetime, richText, json, longText];
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CmsModelFieldToCommodoFieldPlugin } from "@webiny/api-headless-cms/types";
import { withFields, object } from "@webiny/commodo";
import { i18nField } from "./i18nFields";

const plugin: CmsModelFieldToCommodoFieldPlugin = {
name: "cms-model-field-to-commodo-field-rich-text",
type: "cms-model-field-to-commodo-field",
fieldType: "rich-text",
dataModel({ model, field, validation, context }) {
withFields({
[field.fieldId]: i18nField({ field: object({ validation }), context })
})(model);
},
searchModel({ model, field, validation }) {
withFields({
[field.fieldId]: object({ validation })
})(model);
}
};

export default plugin;
2 changes: 1 addition & 1 deletion packages/app-admin/src/plugins/init.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const plugin: WebinyInitPlugin = {
init() {
// Settings
// Apps / integrations can register settings plugins and add menu items like the following.
const settingsPlugins = getPlugins<AdminMenuSettingsPlugin>("menu-settings");
const settingsPlugins = getPlugins<AdminMenuSettingsPlugin>("admin-menu-settings");

registerPlugins({
name: "menu-settings",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import number from "./number";
import text from "./text";
import longText from "./longText";
import richText from "./richText";
import boolean from "./boolean";
import dateTime from "./dateTime";

export default [number, text, longText, boolean, dateTime];
export default [number, text, longText, richText, boolean, dateTime];
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import richTextInput from "./richTextInput";

export default [richTextInput]
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from "react";
import { CmsEditorFieldRendererPlugin } from "@webiny/app-headless-cms/types";
import { I18NValue } from "@webiny/app-i18n/components";
import { i18n } from "@webiny/app/i18n";

import I18NRichTextEditor from "@webiny/app-i18n/admin/components/I18NRichTextEditor";
import numberedListPlugins from "@webiny/app-i18n/admin/plugins/richTextEditor/numberedList";
import bulletedListPlugins from "@webiny/app-i18n/admin/plugins/richTextEditor/bulletedList";
import headingPlugins from "@webiny/app-i18n/admin/plugins/richTextEditor/heading";
import codePlugins from "@webiny/app-i18n/admin/plugins/richTextEditor/code";

const t = i18n.ns("app-headless-cms/admin/fields/text");

const richTextPlugins = [
numberedListPlugins.plugin,
bulletedListPlugins.plugin,
headingPlugins.plugin,
codePlugins.plugin
];

const plugin: CmsEditorFieldRendererPlugin = {
type: "cms-editor-field-renderer",
name: "cms-editor-field-renderer-rich-text",
renderer: {
rendererName: "rich-text-input",
name: t`Rich Text Input`,
description: t`Renders a rich text input.`,
canUse({ field }) {
return field.type === "rich-text" && !field.multipleValues && !field.predefinedValues;
},
render({ field, getBind }) {
const Bind = getBind();

return (
<Bind>
{bind => (
<I18NRichTextEditor
{...bind}
onChange={bind.onChange}
label={I18NValue({ value: field.label })}
placeholder={I18NValue({ value: field.placeholderText })}
description={I18NValue({ value: field.helpText })}
plugins={richTextPlugins}
/>
)}
</Bind>
);
}
}
};

export default plugin;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion packages/app-headless-cms/src/admin/plugins/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import longText from "./longText";
import number from "./number";
import boolean from "./boolean";
import dateTime from "./dateTime";
import richText from "./richText";

const plugins: CmsEditorFieldTypePlugin[] = [text, number, boolean, dateTime, longText];
const plugins: CmsEditorFieldTypePlugin[] = [text, number, boolean, dateTime, richText, longText];

export default plugins;
53 changes: 53 additions & 0 deletions packages/app-headless-cms/src/admin/plugins/fields/richText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from "react";
import { ReactComponent as NotesIcon } from "./icons/notes-black-24px.svg";
import { Grid, Cell } from "@webiny/ui/Grid";
import { I18NInput } from "@webiny/app-i18n/admin/components";
import { CmsEditorFieldTypePlugin } from "@webiny/app-headless-cms/types";
import { i18n } from "@webiny/app/i18n";
const t = i18n.ns("app-headless-cms/admin/fields");

const plugin: CmsEditorFieldTypePlugin = {
type: "cms-editor-field-type",
name: "cms-editor-field-type-richText",
field: {
type: "rich-text",
label: t`Rich text`,
description: t`Text formatting with references and media.`,
icon: <NotesIcon />,
allowMultipleValues: false,
allowPredefinedValues: false,
allowIndexes: {
singleValue: false,
multipleValues: false
},
createField() {
return {
type: this.type,
name: this.name,
validation: [],
settings: {
defaultValue: ""
},
renderer: {
name: ""
}
};
},
renderSettings({ form: { Bind } }) {
return (
<Grid>
<Cell span={12}>
<Bind name={"placeholderText"}>
<I18NInput
label={t`Placeholder text`}
description={t`Placeholder text (optional)`}
/>
</Bind>
</Cell>
</Grid>
);
}
}
};

export default plugin;
1 change: 1 addition & 0 deletions packages/app-i18n/src/admin/assets/icons/format_code.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions packages/app-i18n/src/admin/assets/icons/format_list_bulleted.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions packages/app-i18n/src/admin/assets/icons/format_list_numbered.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/app-i18n/src/admin/assets/icons/format_size.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ee1dadb

Please sign in to comment.