Skip to content

Commit 0dd0dee

Browse files
committed
chore: new plugin impl
1 parent a7b704b commit 0dd0dee

File tree

4 files changed

+63
-69
lines changed

4 files changed

+63
-69
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# slate-plugin-color
22
> SlateJS color plugin.
33
4+
45
## installation
56
```bash
67
npm install -S @jswork/slate-plugin-color

dist/index.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,52 @@ Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
66

7-
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
7+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /**
8+
* @usage:
9+
* Editor.addMark(editor,'color', '#f00')
10+
*/
811

912
var _react = require('react');
1013

1114
var _react2 = _interopRequireDefault(_react);
1215

1316
var _slateHyperscript = require('slate-hyperscript');
1417

18+
var _nextSlatePlugin = require('@jswork/next-slate-plugin');
19+
20+
var _nextSlatePlugin2 = _interopRequireDefault(_nextSlatePlugin);
21+
1522
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1623

17-
exports.default = {
18-
name: 'color',
19-
importer: function importer(el, children) {
20-
var nodeName = el.nodeName.toLowerCase();
21-
if (nodeName === 'span' && el.style.color) {
22-
return (0, _slateHyperscript.jsx)('text', { value: el.style.color }, children);
23-
}
24-
},
25-
// to-html
26-
exporter: function exporter(node, children) {
27-
if (!children) {
28-
if (node.color) {
29-
var value = node.color.value;
24+
exports.default = _nextSlatePlugin2.default.define({
25+
id: 'color',
26+
serialize: {
27+
input: function input(_ref, children) {
28+
var el = _ref.el;
3029

31-
return '<span style="color: ' + value + ';">' + node.text + '</span>';
30+
var nodeName = el.nodeName.toLowerCase();
31+
if (nodeName === 'span' && el.style.color) {
32+
return (0, _slateHyperscript.jsx)('text', { color: el.style.color }, children);
3233
}
34+
},
35+
output: function output(_ref2) {
36+
var el = _ref2.el,
37+
color = _ref2.color;
38+
39+
el.style.color = color;
40+
return el;
3341
}
3442
},
35-
hooks: {
36-
leaf: function leaf(inContext, _ref) {
37-
var attributes = _ref.attributes,
38-
children = _ref.children,
39-
_leaf = _ref.leaf;
40-
41-
if (_leaf.color) {
42-
var value = _leaf.color.value;
43-
44-
return _react2.default.createElement(
45-
'span',
46-
_extends({}, attributes, { style: { color: value } }),
47-
children
48-
);
49-
}
50-
}
43+
render: function render(_, _ref3) {
44+
var attributes = _ref3.attributes,
45+
children = _ref3.children,
46+
leaf = _ref3.leaf;
47+
var color = leaf.color;
48+
49+
return _react2.default.createElement(
50+
'span',
51+
_extends({ style: { color: color } }, attributes),
52+
children
53+
);
5154
}
52-
};
55+
});

package.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,5 @@
3636
"publishConfig": {
3737
"access": "public",
3838
"registry": "https://registry.npmjs.org/"
39-
},
40-
"dependencies": {
41-
"@jswork/next": "^1.0.5",
42-
"@jswork/next-registry-choices": "^1.0.1"
43-
},
44-
"keywords": [
45-
"slate",
46-
"plugin",
47-
"color"
48-
]
49-
}
39+
}
40+
}

src/index.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1+
/**
2+
* @usage:
3+
* Editor.addMark(editor,'color', '#f00')
4+
*/
5+
16
import React from 'react';
27
import { jsx } from 'slate-hyperscript';
8+
import NxSlatePlugin from '@jswork/next-slate-plugin';
39

4-
export default {
5-
name: 'color',
6-
importer: (el, children) => {
7-
const nodeName = el.nodeName.toLowerCase();
8-
if (nodeName === 'span' && el.style.color) {
9-
return jsx('text', { value: el.style.color }, children);
10-
}
11-
},
12-
// to-html
13-
exporter: (node, children) => {
14-
if (!children) {
15-
if (node.color) {
16-
const { value } = node.color;
17-
return `<span style="color: ${value};">${node.text}</span>`;
10+
export default NxSlatePlugin.define({
11+
id: 'color',
12+
serialize: {
13+
input: ({ el }, children) => {
14+
const nodeName = el.nodeName.toLowerCase();
15+
if (nodeName === 'span' && el.style.color) {
16+
return jsx('text', { color: el.style.color }, children);
1817
}
18+
},
19+
output: ({ el, color }) => {
20+
el.style.color = color;
21+
return el;
1922
}
2023
},
21-
hooks: {
22-
leaf: (inContext, { attributes, children, leaf }) => {
23-
if (leaf.color) {
24-
const { value } = leaf.color;
25-
return (
26-
<span {...attributes} style={{ color: value }}>
27-
{children}
28-
</span>
29-
);
30-
}
31-
}
24+
render: (_, { attributes, children, leaf }) => {
25+
const { color } = leaf;
26+
return (
27+
<span style={{ color }} {...attributes}>
28+
{children}
29+
</span>
30+
);
3231
}
33-
};
32+
});

0 commit comments

Comments
 (0)