From dfa28983d70cdc5c713b284e307bae6a5dc64185 Mon Sep 17 00:00:00 2001 From: SemmyWong <67748948+semmywong@users.noreply.github.com> Date: Fri, 28 Jan 2022 13:20:01 +0800 Subject: [PATCH] feat: add Markdown component into schema components (#173) * feat: markdown migrate * feat: markdown migrate * feat: add Markdown component into schema components * refactor: modifty Markdown.Void component * refactor: modifty Markdown.Void component * refactor: modifty Markdown.Void component * refactor: modify Markdown.Void component * refactor: modify Markdown.Void component * fix: x-editable=false Co-authored-by: chenos --- packages/client/package.json | 2 + .../client/src/schema-component/antd/index.ts | 1 + .../src/schema-component/antd/input/Input.tsx | 7 +- .../src/schema-component/antd/input/index.ts | 1 + .../antd/markdown/Markdown.tsx | 91 +++++++++++++++++++ .../antd/markdown/demos/demo1.tsx | 41 +++++++++ .../antd/markdown/demos/demo2.tsx | 51 +++++++++++ .../schema-component/antd/markdown/index.md | 10 ++ .../schema-component/antd/markdown/index.ts | 1 + .../schema-component/antd/markdown/style.less | 12 +++ yarn.lock | 10 ++ 11 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 packages/client/src/schema-component/antd/markdown/Markdown.tsx create mode 100644 packages/client/src/schema-component/antd/markdown/demos/demo1.tsx create mode 100644 packages/client/src/schema-component/antd/markdown/demos/demo2.tsx create mode 100644 packages/client/src/schema-component/antd/markdown/index.ts create mode 100644 packages/client/src/schema-component/antd/markdown/style.less diff --git a/packages/client/package.json b/packages/client/package.json index bfbcc41e1..4ab9a07f1 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -27,6 +27,7 @@ "axios": "^0.24.0", "file-saver": "^2.0.5", "i18next": "^21.6.0", + "marked": "^4.0.12", "react-helmet": "^6.1.0", "react-i18next": "^11.15.1", "react-image-lightbox": "^5.1.4", @@ -47,6 +48,7 @@ "build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir esm" }, "devDependencies": { + "@types/marked": "^4.0.1", "axios-mock-adapter": "^1.20.0" } } diff --git a/packages/client/src/schema-component/antd/index.ts b/packages/client/src/schema-component/antd/index.ts index c74f55f3b..7a03d105a 100644 --- a/packages/client/src/schema-component/antd/index.ts +++ b/packages/client/src/schema-component/antd/index.ts @@ -12,6 +12,7 @@ export * from './grid'; export * from './icon-picker'; export * from './input'; export * from './input-number'; +export * from './markdown'; export * from './menu'; export * from './page'; export * from './password'; diff --git a/packages/client/src/schema-component/antd/input/Input.tsx b/packages/client/src/schema-component/antd/input/Input.tsx index b0431a9b9..47d14a5a2 100644 --- a/packages/client/src/schema-component/antd/input/Input.tsx +++ b/packages/client/src/schema-component/antd/input/Input.tsx @@ -1,14 +1,13 @@ -import React from 'react'; +import { LoadingOutlined } from '@ant-design/icons'; +import { connect, mapProps, mapReadPretty } from '@formily/react'; import { Input as AntdInput } from 'antd'; import { InputProps, TextAreaProps } from 'antd/lib/input'; -import { connect, mapProps, mapReadPretty } from '@formily/react'; -import { LoadingOutlined } from '@ant-design/icons'; +import React from 'react'; import { ReadPretty } from './ReadPretty'; type ComposedInput = React.FC & { TextArea?: React.FC; URL?: React.FC; - DesignableBar?: React.FC; }; export const Input: ComposedInput = connect( diff --git a/packages/client/src/schema-component/antd/input/index.ts b/packages/client/src/schema-component/antd/input/index.ts index ba9fe7ebc..de2898f61 100644 --- a/packages/client/src/schema-component/antd/input/index.ts +++ b/packages/client/src/schema-component/antd/input/index.ts @@ -1 +1,2 @@ export * from './Input'; +export * from './ReadPretty'; diff --git a/packages/client/src/schema-component/antd/markdown/Markdown.tsx b/packages/client/src/schema-component/antd/markdown/Markdown.tsx new file mode 100644 index 000000000..4667397c8 --- /dev/null +++ b/packages/client/src/schema-component/antd/markdown/Markdown.tsx @@ -0,0 +1,91 @@ +import { LoadingOutlined } from '@ant-design/icons'; +import { connect, mapProps, mapReadPretty, observer, useField, useFieldSchema } from '@formily/react'; +import { Button, Input as AntdInput, Space } from 'antd'; +import { marked } from 'marked'; +import React, { useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import { ReadPretty as InputReadPretty } from '../input'; + +export function markdown(text) { + if (!text) { + return ''; + } + return marked.parse(text); +} + +export const Markdown: any = connect( + AntdInput.TextArea, + mapProps((props: any, field) => { + return { + ...props, + suffix: {field?.['loading'] || field?.['validating'] ? : props.suffix}, + }; + }), + mapReadPretty((props) => { + let text = props.value; + let value =
; + return ; + }), +); + +function MarkdownTextArea(props: any) { + const { t } = useTranslation(); + const [value, setValue] = useState(props.defaultValue); + return ( +
+ { + setValue(e.target.value); + }} + /> + + + + +
+ ); +} + +Markdown.Void = observer((props: any) => { + const schema = useFieldSchema(); + const field = useField(); + const text = schema['x-component-props']?.['content'] ?? schema['default']; + let value =
; + const { onSave, onCancel } = props; + return field.editable ? ( + { + field.editable = false; + onCancel?.(); + }} + onSubmit={async (value) => { + field.editable = false; + schema['x-component-props'] ?? (schema['x-component-props'] = {}); + schema['x-component-props']['content'] = value; + onSave?.(schema); + }} + /> + ) : ( + + ); +}); + +export default Markdown; diff --git a/packages/client/src/schema-component/antd/markdown/demos/demo1.tsx b/packages/client/src/schema-component/antd/markdown/demos/demo1.tsx new file mode 100644 index 000000000..72f74260e --- /dev/null +++ b/packages/client/src/schema-component/antd/markdown/demos/demo1.tsx @@ -0,0 +1,41 @@ +/** + * title: Markdown + */ +import { FormItem } from '@formily/antd'; +import { Markdown, SchemaComponent, SchemaComponentProvider } from '@nocobase/client'; +import React from 'react'; + +const schema = { + type: 'object', + properties: { + input: { + type: 'string', + title: `Editable`, + 'x-decorator': 'FormItem', + 'x-component': 'Markdown', + 'x-reactions': { + target: 'read', + fulfill: { + state: { + value: '{{$self.value}}', + }, + }, + }, + }, + read: { + type: 'string', + title: `Read pretty`, + 'x-read-pretty': true, + 'x-decorator': 'FormItem', + 'x-component': 'Markdown', + }, + }, +}; + +export default () => { + return ( + + + + ); +}; diff --git a/packages/client/src/schema-component/antd/markdown/demos/demo2.tsx b/packages/client/src/schema-component/antd/markdown/demos/demo2.tsx new file mode 100644 index 000000000..fc7eccc78 --- /dev/null +++ b/packages/client/src/schema-component/antd/markdown/demos/demo2.tsx @@ -0,0 +1,51 @@ +/** + * title: Markdown.Void + */ +import { FormItem } from '@formily/antd'; +import { observer, useField } from '@formily/react'; +import { Markdown, SchemaComponent, SchemaComponentProvider } from '@nocobase/client'; +import { Button } from 'antd'; +import React from 'react'; + +const schema = { + type: 'object', + properties: { + markdown: { + type: 'void', + title: `Read pretty`, + 'x-decorator': 'Editable', + 'x-component': 'Markdown.Void', + 'x-editable': false, + 'x-component-props': { + content: '# Markdown content', + }, + }, + }, +}; + +const Editable = observer((props: any) => { + const filed = useField(); + if (filed.editable) { + return props.children; + } + return ( +
+ +
{props.children}
+
+ ); +}); + +export default () => { + return ( + + + + ); +}; diff --git a/packages/client/src/schema-component/antd/markdown/index.md b/packages/client/src/schema-component/antd/markdown/index.md index 300fac5b7..5be1c52ca 100644 --- a/packages/client/src/schema-component/antd/markdown/index.md +++ b/packages/client/src/schema-component/antd/markdown/index.md @@ -6,3 +6,13 @@ group: --- # Markdown + +## Examples + +### Markdown + + + +### Markdown. Void + + diff --git a/packages/client/src/schema-component/antd/markdown/index.ts b/packages/client/src/schema-component/antd/markdown/index.ts new file mode 100644 index 000000000..a5b9f6efd --- /dev/null +++ b/packages/client/src/schema-component/antd/markdown/index.ts @@ -0,0 +1 @@ +export * from './Markdown'; diff --git a/packages/client/src/schema-component/antd/markdown/style.less b/packages/client/src/schema-component/antd/markdown/style.less new file mode 100644 index 000000000..2ac3b79fc --- /dev/null +++ b/packages/client/src/schema-component/antd/markdown/style.less @@ -0,0 +1,12 @@ +.nb-markdown > *:last-child { + margin-bottom: 0; +} + +.ant-description-textarea, +.ant-description-input { + line-height: 1.612; +} + +.field-interface-datetime { + min-width: 100px; +} diff --git a/yarn.lock b/yarn.lock index 888bd5954..f3bdbbdb1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2957,6 +2957,11 @@ resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.177.tgz#f70c0d19c30fab101cad46b52be60363c43c4578" integrity sha512-0fDwydE2clKe9MNfvXHBHF9WEahRuj+msTuQqOmAApNORFvhMYZKNGGJdCzuhheVjMps/ti0Ak/iJPACMaevvw== +"@types/marked@^4.0.1": + version "4.0.1" + resolved "https://registry.npmmirror.com/@types/marked/download/@types/marked-4.0.1.tgz#d588a7bbc4d6551c5e75249bc106ffda96ae33c5" + integrity sha512-ZigEmCWdNUU7IjZEuQ/iaimYdDHWHfTe3kg8ORfKjyGYd9RWumPoOJRQXB0bO+XLkNwzCthW3wUIQtANaEZ1ag== + "@types/mathjax@^0.0.36": version "0.0.36" resolved "https://registry.npmjs.org/@types/mathjax/-/mathjax-0.0.36.tgz#18cf766f88ac0cd4e7ee8282b1286049bb6aa682" @@ -9714,6 +9719,11 @@ markdown-table@^2.0.0: dependencies: repeat-string "^1.0.0" +marked@^4.0.12: + version "4.0.12" + resolved "https://registry.npmmirror.com/marked/download/marked-4.0.12.tgz#2262a4e6fd1afd2f13557726238b69a48b982f7d" + integrity sha512-hgibXWrEDNBWgGiK18j/4lkS6ihTe9sxtV4Q1OQppb/0zzyPSzoFANBa5MfsG/zgsWklmNnhm0XACZOH/0HBiQ== + mathjax-full@^3.0.0: version "3.2.0" resolved "https://registry.npmjs.org/mathjax-full/-/mathjax-full-3.2.0.tgz#e53269842a943d4df10502937518991268996c5c"