-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from subquery/feat/markdown
feat: markdown
- Loading branch information
Showing
10 changed files
with
766 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2020-2022 SubQuery Pte Ltd authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import React, { FC, useState } from 'react'; | ||
import { createBEM } from 'components/utilities/createBem'; | ||
import clsx from 'clsx'; | ||
import MarkdownCompiler, { Options } from 'react-markdown'; | ||
import { attachPropertiesToComponent } from '../../utilities/attachPropertiesToCompnent'; | ||
import { Input, Radio } from 'antd'; | ||
import { Typography } from '../typography'; | ||
import { usePropsValue } from 'components/utilities/usePropsValue'; | ||
import { TextAreaProps } from 'antd/es/input'; | ||
|
||
import './markdown.less'; | ||
|
||
const bem = createBEM('subql-markdown'); | ||
const previewBem = createBEM('subql-markdown-preview'); | ||
|
||
export interface SubqlMarkdown { | ||
value?: string | undefined; | ||
onChange?: (val: string | undefined) => void; | ||
inputProps: TextAreaProps; | ||
previewProps: Options; | ||
} | ||
|
||
const MarkdownPreview: FC<Options> = (props) => { | ||
return ( | ||
<div className={clsx(previewBem())}> | ||
<MarkdownCompiler {...props}></MarkdownCompiler> | ||
</div> | ||
); | ||
}; | ||
|
||
const Markdown: FC<SubqlMarkdown> = (props) => { | ||
const [tabVal, setTabVal] = useState<'edit' | 'preview'>('edit'); | ||
const [markdownVal, setMarkdownVal] = usePropsValue({ | ||
value: props.value, | ||
defaultValue: '', | ||
onChange: props.onChange, | ||
}); | ||
return ( | ||
<div className={clsx(bem())}> | ||
<div className={clsx(bem('header'))}> | ||
<Radio.Group | ||
value={tabVal} | ||
onChange={(val) => { | ||
setTabVal(val.target.value); | ||
}} | ||
optionType="button" | ||
> | ||
<Radio value={'edit'}>Markdown Edit</Radio> | ||
<Radio value={'preview'}>Preview</Radio> | ||
</Radio.Group> | ||
<span style={{ flex: 1 }}></span> | ||
<Typography type="secondary"> | ||
This entry supports{' '} | ||
<Typography.Link href="https://commonmark.org/help/" active> | ||
 basic markdown | ||
</Typography.Link> | ||
</Typography> | ||
</div> | ||
|
||
<div className={clsx(bem('main'))}> | ||
{tabVal === 'edit' && ( | ||
<Input.TextArea | ||
rows={20} | ||
{...props.inputProps} | ||
value={markdownVal} | ||
onChange={(val) => { | ||
setMarkdownVal(val.target.value); | ||
}} | ||
></Input.TextArea> | ||
)} | ||
{tabVal === 'preview' && <MarkdownPreview {...props.previewProps}>{markdownVal}</MarkdownPreview>} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default attachPropertiesToComponent(Markdown, { | ||
Preview: MarkdownPreview, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
.subql-markdown { | ||
width: 100%; | ||
border: 1px solid var(--sq-graylight); | ||
border-radius: 8px; | ||
padding: 10px 8px; | ||
background: #fff; | ||
|
||
.ant-radio-group { | ||
background: var(--sq-gray200); | ||
padding: 4px; | ||
border-radius: 4px; | ||
.ant-radio-button-wrapper { | ||
border: none; | ||
border-radius: 4px; | ||
background: var(--sq-gray200); | ||
color: var(--sq-gray600); | ||
font-family: var(--sq-font-family); | ||
&:not(:first-child)::before { | ||
display: none; | ||
} | ||
|
||
&-checked { | ||
background: #fff; | ||
box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.05); | ||
color: var(--sq-gray800); | ||
|
||
&:hover { | ||
background: #fff; | ||
box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.05); | ||
color: var(--sq-gray800); | ||
} | ||
} | ||
|
||
& + .ant-radio-button-wrapper { | ||
margin-left: 8px; | ||
} | ||
} | ||
} | ||
|
||
&__header { | ||
display: flex; | ||
border-bottom: 1px solid var(--sq-gray200); | ||
margin-bottom: 10px; | ||
padding-bottom: 10px; | ||
} | ||
|
||
&__main { | ||
min-height: 440px; | ||
.ant-input { | ||
padding: 0; | ||
border: none; | ||
resize: none; | ||
color: var(--sq-gray500); | ||
&:focus { | ||
box-shadow: none; | ||
} | ||
} | ||
} | ||
|
||
&-preview { | ||
p { | ||
margin: 1em 0; | ||
} | ||
ul { | ||
margin-left: 22px; | ||
} | ||
li { | ||
margin: 0.5em 0; | ||
} | ||
|
||
img { | ||
max-width: 100%; | ||
margin: 1em 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2020-2022 SubQuery Pte Ltd authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export function attachPropertiesToComponent<C, P extends Record<string, any>>(component: C, properties: P): C & P { | ||
const ret = component as any; | ||
for (const key in properties) { | ||
if (properties.hasOwnProperty(key)) { | ||
ret[key] = properties[key]; | ||
} | ||
} | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2020-2022 SubQuery Pte Ltd authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Copy from antd-mobile | ||
import { SetStateAction, useRef } from 'react'; | ||
import { useMemoizedFn, useUpdate } from 'ahooks'; | ||
|
||
type Options<T> = { | ||
value?: T; | ||
defaultValue: T; | ||
onChange?: (v: T) => void; | ||
}; | ||
|
||
export function usePropsValue<T>(options: Options<T>) { | ||
const { value, defaultValue, onChange } = options; | ||
|
||
const update = useUpdate(); | ||
|
||
const stateRef = useRef<T>(value !== undefined ? value : defaultValue); | ||
if (value !== undefined) { | ||
stateRef.current = value; | ||
} | ||
|
||
const setState = useMemoizedFn((v: SetStateAction<T>, forceTrigger = false) => { | ||
// `forceTrigger` means trigger `onChange` even if `v` is the same as `stateRef.current` | ||
const nextValue = typeof v === 'function' ? (v as (prevState: T) => T)(stateRef.current) : v; | ||
if (!forceTrigger && nextValue === stateRef.current) return; | ||
stateRef.current = nextValue; | ||
update(); | ||
return onChange?.(nextValue); | ||
}); | ||
return [stateRef.current, setState] as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.