Skip to content

Commit

Permalink
add: serialize function
Browse files Browse the repository at this point in the history
  • Loading branch information
lingjieee committed Jun 21, 2020
1 parent 273dda7 commit cff9736
Show file tree
Hide file tree
Showing 21 changed files with 1,680 additions and 30 deletions.
1 change: 1 addition & 0 deletions docs/examples/full.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: FullDemo
order: 1
group:
title: Core
order: 1
---

<code src="../../examples/full" compact/>
1 change: 1 addition & 0 deletions docs/examples/full.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: 完整Demo
order: 1
group:
title: 核心
order: 1
nav:
title: 演示
---
Expand Down
9 changes: 9 additions & 0 deletions docs/examples/serialize/html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Html
order: 1
group:
title: Serialize
order: 2
---

<code src="../../../examples/serialize/html" compact/>
9 changes: 9 additions & 0 deletions docs/examples/serialize/html.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Html
order: 1
group:
title: 格式转换
order: 2
---

<code src="../../../examples/serialize/html-zh" compact/>
9 changes: 9 additions & 0 deletions docs/examples/serialize/markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Markdown
order: 2
group:
title: Serialize
order: 2
---

<code src="../../../examples/serialize/md" compact/>
9 changes: 9 additions & 0 deletions docs/examples/serialize/markdown.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Markdown
order: 2
group:
title: 格式转换
order: 2
---

<code src="../../../examples/serialize/md-zh" compact/>
9 changes: 9 additions & 0 deletions docs/examples/serialize/plaintext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Plaintext
order: 3
group:
title: Serialize
order: 2
---

<code src="../../../examples/serialize/text" compact/>
9 changes: 9 additions & 0 deletions docs/examples/serialize/plaintext.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Plaintext
order: 3
group:
title: 格式转换
order: 2
---

<code src="../../../examples/serialize/text-zh" compact/>
203 changes: 203 additions & 0 deletions examples/serialize/html-zh.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import React, {FunctionComponent, useCallback, useMemo, useState} from 'react';
// @ts-ignore
import initValue from './value.json';
import {createEditor, Node} from 'slate';
import {enUS, SlatePlugin} from "fantasy-editor";
import {Slate, withReact} from "slate-react";
import {withHistory} from "slate-history";
import {Button, Switch} from 'antd';
import 'antd/lib/switch/style';
import 'antd/lib/button/style';

import classNames from 'classnames';

import './index.less';
import {
AlignPlugin,
BgColorPlugin,
BlockQuotePlugin,
BoldPlugin,
CodeBlockPlugin,
CodePlugin,
FilePlugin,
FontColorPlugin,
FontSizePlugin,
HeadingPlugin,
HrPlugin,
IdentPlugin,
ImagePlugin,
ItalicPlugin,
LineHeightPlugin,
LinkPlugin,
ListPlugin,
pipe,
SoftBreakPlugin,
StrikeThroughPlugin,
SubscriptPlugin,
SuperscriptPlugin,
TaskListPlugin,
UnderlinePlugin,
withFile,
withImage,
withInline,
withMention,
withShortcuts,
withTrailingNode,
withTable,
withVoid,
LocaleProvider,
FullPageProvider,
Toolbar,
PluginEditor,
ButtonUndo,
BLOCK_LINK,
BLOCK_FILE,
BLOCK_HR,
ButtonRedo,
ButtonHeading,
ButtonFontSize,
ButtonBold,
ButtonItalic,
ButtonUnderline,
ButtonStrikeThrough,
ButtonBlockQuote,
ButtonSubscript,
ButtonSuperscript,
ButtonFontColor,
ButtonBgColor,
ButtonAlign,
ButtonCode,
ButtonCodeBlock,
ButtonNumberedList,
ButtonBulletedList,
ButtonIdentInc,
ButtonIdentDec,
ButtonLineHeight,
ButtonLink, ButtonImage, ButtonFile, ButtonHr, ButtonFull, ButtonTaskList,
serializeHtml
} from "fantasy-editor";

const withPlugins = [
withReact,
withHistory,
withInline([BLOCK_LINK, BLOCK_FILE]),
withImage(),
withFile(),
withTrailingNode(),
withVoid([BLOCK_HR]),
withShortcuts(),
withMention(),
withTable(),
];

const FullDemo: FunctionComponent = () => {

const [value, setValue] = useState<Node[]>(initValue as any);
const [html, setHtml] = useState('');
const [format, setFormat] = useState(false);

const plugins: SlatePlugin[] = useMemo(() => {
const list: SlatePlugin[] = [];
list.push(SoftBreakPlugin());
list.push(HeadingPlugin());
list.push(FontSizePlugin());
list.push(BoldPlugin());
list.push(ItalicPlugin());
list.push(UnderlinePlugin());
list.push(StrikeThroughPlugin());
list.push(BlockQuotePlugin());
list.push(SubscriptPlugin());
list.push(SuperscriptPlugin());
list.push(FontColorPlugin());
list.push(BgColorPlugin());
list.push(AlignPlugin());
list.push(CodePlugin());
list.push(CodeBlockPlugin());
list.push(ListPlugin());
list.push(IdentPlugin());
list.push(LinkPlugin());
list.push(ImagePlugin());
list.push(HrPlugin());
list.push(FilePlugin());
list.push(LineHeightPlugin());
list.push(TaskListPlugin())

return list;
}, []);


const editor = useMemo(() => pipe(createEditor(), ...withPlugins), []);
const [fullPage, setFullPage] = useState(false);

const onChange = useCallback(
(value: Node[]) => {
setValue(value);
},
[editor, setValue],
);

const serialize = () => {
setHtml(serializeHtml(value, format));
};

return (
<div>
<div className={classNames('fc-editor', {'full-page': fullPage})}>
<LocaleProvider locale={enUS}>
<FullPageProvider full={fullPage} setFull={setFullPage}>
<Slate
editor={editor}
value={value}
onChange={onChange}
>
<Toolbar>
<ButtonUndo/>
<ButtonRedo/>
<ButtonHeading/>
<ButtonFontSize/>
<ButtonBold/>
<ButtonItalic/>
<ButtonUnderline/>
<ButtonStrikeThrough/>
<ButtonBlockQuote/>
<ButtonSubscript/>
<ButtonSuperscript/>
<ButtonFontColor/>
<ButtonBgColor/>
<ButtonAlign/>
<ButtonCode/>
<ButtonCodeBlock/>
<ButtonNumberedList/>
<ButtonBulletedList/>
<ButtonTaskList/>
<ButtonIdentInc/>
<ButtonIdentDec/>
<ButtonLineHeight/>
<ButtonLink/>
<ButtonImage/>
<ButtonFile/>
<ButtonHr/>
<ButtonFull/>
</Toolbar>
<div className="fc-content" style={{height: 500}}>
<PluginEditor plugins={plugins}/>
</div>
</Slate>
</FullPageProvider>
</LocaleProvider>
</div>
<div style={{padding: 16}}>
<Switch checked={format} onChange={e=>setFormat(e)}/>转换代码块
&nbsp;&nbsp;
<Button onClick={serialize} type="primary">生成HTML</Button>
</div>
{html&&(
<div className="content-display">
{html}
</div>
)}
</div>
);
};

export default FullDemo;
Loading

0 comments on commit cff9736

Please sign in to comment.