Skip to content

Commit

Permalink
feat: alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed Feb 4, 2021
1 parent f44e368 commit 068ae9b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Code from './plugins/code';
import Heading from './plugins/heading';
import Blockquote from './plugins/blockquote';
import Color from './plugins/color';
import Alignment from './plugins/alignment';
import BackgroundColor from './plugins/background-color';
import './assets/style.scss';

Expand Down Expand Up @@ -65,6 +66,7 @@ class App extends React.Component {
Heading,
Blockquote,
Color,
Alignment,
BackgroundColor
]}
value={this.state.value}
Expand Down
33 changes: 33 additions & 0 deletions public/plugins/alignment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { jsx } from 'slate-hyperscript';

/**
* @usage:
* Transforms.setNodes(editor, { type:'alignment', value: 'right'});
*
* <div style="text-align:center">
* <p>xxx</p>
* </div>
*/

export default {
name: 'alignment',
importer: (el, children) => {
if (el.style.textAlign) {
return jsx('element', { textAlign: el.style.textAlign }, children);
}
},
exporter: (node, children) => {
return `<div style="text-align: ${node.value}">${children}</div>`;
},
hooks: {
element: (_, { attributes, children, element }) => {
const { value } = element;
return (
<div style={{ textAlign: value }} {...attributes}>
{children}
</div>
);
}
}
};

0 comments on commit 068ae9b

Please sign in to comment.