Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/draft-js-export-html/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let options = {
};
let html = stateToHTML(contentState, options);
```
### `inlineStylesFn`
### `inlineStyleFn`

You can define custom function to return rendering options based on inline styles. Similar to draft.js [customStyleFn](https://draftjs.org/docs/api-reference-editor.html#customstylefn).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,14 @@ describe('stateToHTML', () => {

it('should support inline style function', () => {
let options = {
inlineStyleFn: (styles) => {
inlineStyleFn: (style) => {
let key = 'color-';
let color = styles.filter((value) => value.startsWith(key)).first();

if (color) {
if (style.startsWith(key)) {
return {
element: 'span',
style: {
color: color.replace(key, ''),
color: style.replace(key, ''),
},
};
}
Expand Down
28 changes: 16 additions & 12 deletions packages/draft-js-export-html/src/stateToHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import type {
EntityInstance,
} from 'draft-js';
import type {CharacterMetaList} from 'draft-js-utils';
import type {DraftInlineStyle} from 'draft-js/lib/DraftInlineStyle';

type AttrMap = {[key: string]: string};
type Attributes = {[key: string]: string};
Expand All @@ -37,7 +36,7 @@ type StyleMap = {[styleName: string]: RenderConfig};

type BlockStyleFn = (block: ContentBlock) => ?RenderConfig;
type EntityStyleFn = (entity: Entity) => ?RenderConfig;
type InlineStyleFn = (style: DraftInlineStyle) => ?RenderConfig;
type InlineStyleFn = (style: string) => ?RenderConfig;

type Options = {
inlineStyles?: StyleMap;
Expand Down Expand Up @@ -344,18 +343,23 @@ class MarkupGenerator {
return content;
}

const renderConfig = this.inlineStyleFn(styleSet);
if (!renderConfig) {
return content;
}
const inlineStyleFn = this.inlineStyleFn;

return styleSet.reduce((nextContent, styleItem) => {
const renderConfig = inlineStyleFn(styleItem);

if (!renderConfig) {
return nextContent;
}

const {element = 'span', attributes, style} = renderConfig;
const attrString = stringifyAttrs({
...attributes,
style: style && styleToCSS(style),
});
const {element = 'span', attributes, style} = renderConfig;
const attrString = stringifyAttrs({
...attributes,
style: style && styleToCSS(style),
});

return `<${element}${attrString}>${content}</${element}>`;
return `<${element}${attrString}>${nextContent}</${element}>`;
}, content);
}

renderBlockContent(block: ContentBlock): string {
Expand Down