Skip to content

Commit

Permalink
feat: background
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed Feb 4, 2021
1 parent 16544c3 commit f44e368
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
6 changes: 5 additions & 1 deletion public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Strikethrough from './plugins/strikethrough';
import Code from './plugins/code';
import Heading from './plugins/heading';
import Blockquote from './plugins/blockquote';
import Color from './plugins/color';
import BackgroundColor from './plugins/background-color';
import './assets/style.scss';

class App extends React.Component {
Expand Down Expand Up @@ -61,7 +63,9 @@ class App extends React.Component {
Strikethrough,
Code,
Heading,
Blockquote
Blockquote,
Color,
BackgroundColor
]}
value={this.state.value}
onChange={(e) => {
Expand Down
35 changes: 35 additions & 0 deletions public/plugins/background-color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import { jsx } from 'slate-hyperscript';

/**
* @usage:
* Editor.addMark(editor,'backgroundColor', 'lightgreen');
*/

export default {
name: 'backgroundColor',
importer: (el, children) => {
const nodeName = el.nodeName.toLowerCase();
if (nodeName === 'span' && el.style.backgroundColor) {
return jsx(
'text',
{ backgroundColor: el.style.backgroundColor },
children
);
}
},
exporter: (el, node) => {
el.style.backgroundColor = node.backgroundColor;
return el;
},
hooks: {
leaf: (_, { attributes, children, leaf }) => {
const { backgroundColor } = leaf;
return (
<span style={{ backgroundColor }} {...attributes}>
{children}
</span>
);
}
}
};
31 changes: 31 additions & 0 deletions public/plugins/color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { jsx } from 'slate-hyperscript';

/**
* @usage:
* Editor.addMark(editor,'color', '#f00')
*/

export default {
name: 'color',
importer: (el, children) => {
const nodeName = el.nodeName.toLowerCase();
if (nodeName === 'span' && el.style.color) {
return jsx('text', { color: el.style.color }, children);
}
},
exporter: (el, node) => {
el.style.color = node.color;
return el;
},
hooks: {
leaf: (_, { attributes, children, leaf }) => {
const { color } = leaf;
return (
<span style={{ color }} {...attributes}>
{children}
</span>
);
}
}
};
6 changes: 4 additions & 2 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ export default class ReactRteSlate extends Component {
el.innerText = node.text;
const target = activePlugins.reduce((el, mark) => {
const { exporter, name } = mark;
return node[name] && exporter(el);
return node[name] && exporter && exporter(el, node);
}, el);
return target.outerHTML;
return target
? target.outerHTML
: NxSlateDefaults.exporter(node, children);
} else {
// element
const { plugins } = this.props;
Expand Down

0 comments on commit f44e368

Please sign in to comment.