Skip to content

Commit

Permalink
fix: defalut element
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed Feb 8, 2021
1 parent 467f5d3 commit 2b00653
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions public/plugins/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { jsx } from 'slate-hyperscript';
import NxSlatePlugin from '@jswork/next-slate-plugin';
import NxCssText from '@jswork/next-css-text';
import nxIsBlockElement from '@jswork/next-is-block-element';
/**
* @usage:
* Transforms.setNodes(editor, { type:'default' })
*
* 注意这里的 style处理,所有的 block 元素,都有可能被直接设置 style;
* input/output 都需要针对style写一些代码
*/

export default NxSlatePlugin.define({
id: 'default',
serialize: {
input: ({ el }, children) => {
if (nxIsBlockElement(el)) {
const css = el.style.cssText;
return jsx('element', { type: 'default', style: NxCssText.css2obj(css) }, children);
}
},
output: (node, children) => {
const { style } = node;
return `<p${style}>${children}</p>`;
}
},
render: (_, { attributes, children, element }) => {
return <p {...attributes}>{children}</p>;
}
});

0 comments on commit 2b00653

Please sign in to comment.