Skip to content

Commit

Permalink
feat: add code plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed Feb 4, 2021
1 parent ac286ec commit f26e392
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Bold from './plugins/bold';
import Italic from './plugins/italic';
import Underline from './plugins/underline';
import Strikethrough from './plugins/strikethrough';
import Code from './plugins/code';
import './assets/style.scss';

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

export default {
name: 'code',
importer: (el, children) => {
const nodeName = el.nodeName.toLowerCase();
if (nodeName === 'code') {
return jsx('text', { code: true }, children);
}
},
exporter: (el) => {
const code = document.createElement('code');
code.appendChild(el);
return code;
},
hooks: {
leaf: (_, { attributes, children, leaf }) => {
return <code {...attributes}>{children}</code>;
}
}
};
2 changes: 1 addition & 1 deletion public/plugins/strikethrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
name: 'strikethrough',
importer: (el, children) => {
const nodeName = el.nodeName.toLowerCase();
if (nodeName === 'span' && el.style.textDecoration === 'line-through') {
if (nodeName === 's') {
return jsx('text', { strikethrough: true }, children);
}
},
Expand Down
2 changes: 1 addition & 1 deletion public/plugins/underline.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
name: 'underline',
importer: (el, children) => {
const nodeName = el.nodeName.toLowerCase();
if (nodeName === 'span' && el.style.textDecoration === 'underline') {
if (nodeName === 'u') {
return jsx('text', { underline: true }, children);
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default class ReactRteSlate extends Component {
if (!children) {
// pure text:
if (!activePlugins.length) return node.text;
const el = document.createElement('span'); /** props: serialze:{ tag:'span' } */
const el = document.createElement('span');
el.innerText = node.text;
const target = activePlugins.reduce((el, mark) => {
const { exporter, name } = mark;
Expand Down

0 comments on commit f26e392

Please sign in to comment.