Skip to content

Commit

Permalink
feat: block quote
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed Feb 4, 2021
1 parent ab2cbfd commit 16544c3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
},
"dependencies": {
"@jswork/next-compose": "^1.0.0",
"@jswork/next-slate-defaults": "^1.0.0",
"@jswork/next-slate-defaults": "^1.0.1",
"@jswork/next-slate-deserialize": "^1.0.7",
"@jswork/next-slate-serialize": "^1.0.6",
"@jswork/noop": "^1.0.0",
Expand Down
6 changes: 6 additions & 0 deletions public/assets/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@
h3 {
font-size: 14px;
}

blockquote{
border-left: 3px solid #ccc;
padding: 10px;
background: #eee;
}
}
}
11 changes: 10 additions & 1 deletion public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Underline from './plugins/underline';
import Strikethrough from './plugins/strikethrough';
import Code from './plugins/code';
import Heading from './plugins/heading';
import Blockquote from './plugins/blockquote';
import './assets/style.scss';

class App extends React.Component {
Expand Down Expand Up @@ -53,7 +54,15 @@ class App extends React.Component {
<ReactRteSlate
placeholder="type your text."
header={this.headerView}
plugins={[Bold, Italic, Underline, Strikethrough, Code, Heading]}
plugins={[
Bold,
Italic,
Underline,
Strikethrough,
Code,
Heading,
Blockquote
]}
value={this.state.value}
onChange={(e) => {
this.setState({ value: e.target.value });
Expand Down
25 changes: 25 additions & 0 deletions public/plugins/blockquote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import { jsx } from 'slate-hyperscript';

/**
* @usage:
* Transforms.setNodes(editor, { type:'blockquote' })
*/

export default {
name: 'blockquote',
importer: (el, children) => {
const nodeName = el.nodeName.toLowerCase();
if (nodeName === 'blockquote') {
return jsx('element', { type: 'blockquote' }, children);
}
},
exporter: (node, children) => {
return `<blockquote>${children}</blockquote>`;
},
hooks: {
element: (_, { attributes, children, element }) => {
return <blockquote {...attributes}>{children}</blockquote>;
}
}
};
4 changes: 1 addition & 3 deletions public/plugins/heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export default {
},
hooks: {
element: (_, { attributes, children, element }) => {
if (element.type === 'heading') {
return React.createElement(`h${element.value}`, null, children);
}
return React.createElement(`h${element.value}`, attributes, children);
}
}
};
10 changes: 5 additions & 5 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export default class ReactRteSlate extends Component {
}

renderElement = (inProps) => {
const handlers = this.hooks
.map((item) => item.hooks.element)
.filter(Boolean);
const handler = handlers.find((fn) => fn(this, inProps));
return handler ? handler(this, inProps) : <DefaultElement {...inProps} />;
const { element } = inProps;
const { plugins } = this.props;
const plugin = plugins.find((plg) => plg.name === element.type);
if (!plugin) return <DefaultElement {...inProps} />;
return plugin.hooks.element(this, inProps);
};

renderLeaf = (inProps) => {
Expand Down

0 comments on commit 16544c3

Please sign in to comment.