1. block 样式
1.1 blockRenderMap
1.1.1 作用:指定block的节点类型及其wrapper节点。
const { DefaultDraftBlockRenderMap } = Draft;
import Immutable from 'immutable';
const blockRenderMap = DefaultDraftBlockRenderMap.merge(
Immutable.Map({
'atomic': {
element: 'span',
wrapper?: <Wrapper />,
}
})
);
const { Editor } = Draft;
function MyComponent() {
return <Editor blockRenderMap={blockRenderMap} ... />;
}
1.1.2 DefaultDraftBlockRenderMap
var DefaultDraftBlockRenderMap = Immutable.Map({
'header-one': {
element: 'h1'
},
'header-two': {
element: 'h2'
},
'header-three': {
element: 'h3'
},
'header-four': {
element: 'h4'
},
'header-five': {
element: 'h5'
},
'header-six': {
element: 'h6'
},
...
)}
1.2 blockStyleFn
1.2.1 作用:设置block的节点的类名,从而影响block的样式。
function blockStyleFn(contentBlock: ContentBlock): string|null {
if (contentBlock.getType() === 'atomic') {
return 'myCssClassName';
}
}
const { Editor } = Draft;
function MyComponent() {
return <Editor blockStyleFn={blockStyleFn} ... />;
}
2. inline 样式
2.1 customStyleMap
2.1.1 作用:指定行内元素的样式
const customStyleMap = {
...DefaultDraftInlineStyle,
CODE: {
backgroundColor: 'rgba(0, 0, 0, 0.05)',
},
customStyle1: React.CSSProperties;
}
const { Editor } = Draft;
function MyComponent() {
return <Editor customStyleMap={customStyleMap} ... />;
}
2.1.2 添加或移除online的样式
const newContentState = Modifier.applyInlineStyle(editorState.getCurrentContent(), selection, styleName: string);
this.setState({
editorState: EditorState.push(editorState, newContentState, 'change-inline-style')
});
const newContentState = Modifier.removeInlineStyle(editorState.getCurrentContent(), selection, styleName: string);
this.setState({
editorState: EditorState.push(editorState, newContentState, 'change-inline-style')
});
const { RichUtils } = Draft;
const newEditorState = RichUtils.toggleInlineStyle(editorState, styleName: string);
this.setState({ editorState: newEditorState });
2.1.3 DefaultDraftInlineStyle 已预定义了的行内样式
const DefaultDraftInlineStyle = {
BOLD: {
fontWeight: 'bold',
},
CODE: {
fontFamily: 'monospace',
wordWrap: 'break-word',
},
ITALIC: {
fontStyle: 'italic',
},
STRIKETHROUGH: {
textDecoration: 'line-through',
},
UNDERLINE: {
textDecoration: 'underline',
},
};
2.2 customStyleFn
2.2.1 作用:动态设置行内元素的样式
function customStyleFn((style: DraftInlineStyle, block: BlockNodeRecord): Object | undefined | null {
if (style.has('CODE')) {
// 会和已有的样式混合,优先级最高
return {
backgroundColor: 'blue',
}
}
}