Skip to content

Allow inline elements configuration (#141) #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions packages/draft-js-export-html/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ let options = {
let html = stateToHTML(contentState, options);
```

### `entityRenderFn`

It is passed an [`entity`](https://draftjs.org/docs/api-reference-entity.html) object
and should return a string representing the entity in the html output.

## Contributing

If you want to help out, please open an issue to discuss or join us on [Slack](https://draftjs.herokuapp.com/).
Expand Down
7 changes: 6 additions & 1 deletion packages/draft-js-export-html/src/stateToHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ type StyleMap = {[styleName: string]: RenderConfig};

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


type Options = {
inlineStyles?: StyleMap;
inlineStyleFn?: InlineStyleFn;
blockRenderers?: BlockRendererMap;
blockStyleFn?: BlockStyleFn;
entityStyleFn?: EntityStyleFn;
entityRenderFn?: EntityRenderFn;
defaultBlockTag?: ?string;
};

Expand Down Expand Up @@ -372,7 +375,9 @@ class MarkupGenerator {
.map(([entityKey, stylePieces]) => {
let content = stylePieces
.map(([text, styleSet]) => {
let content = encodeContent(text);
let content = (entityKey &&
this.options.entityRenderFn && this.options.entityRenderFn(this.contentState.getEntity(entityKey), text))
|| encodeContent(text);
for (let styleName of this.styleOrder) {
// If our block type is CODE then don't wrap inline code elements.
if (styleName === CODE && blockType === BLOCK_TYPE.CODE) {
Expand Down
2 changes: 2 additions & 0 deletions packages/draft-js-export-html/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare module 'draft-js-export-html' {

type BlockStyleFn = (block: draftjs.ContentBlock) => RenderConfig|undefined;
type EntityStyleFn = (entity: draftjs.EntityInstance) => RenderConfig|undefined;
type EntityRenderFn = (entity: draftjs.EntityInstance, text: string) => string|undefined;
type BlockRenderer = (block: draftjs.ContentBlock) => string;
type RenderConfig = {
element?: string;
Expand All @@ -18,6 +19,7 @@ declare module 'draft-js-export-html' {
blockRenderers?: { [blockType: string]: BlockRenderer };
blockStyleFn?: BlockStyleFn;
entityStyleFn?: EntityStyleFn;
entityRenderFn?: EntityRenderFn;
}

export function stateToHTML(content: draftjs.ContentState, options?: Options): string;
Expand Down
7 changes: 7 additions & 0 deletions packages/draft-js-import-element/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ stateFromElement(element, {
});
```

- `inlineElements`: Array of (lowercase) tag names which should be handled as inline tags. Example:
```js
stateFromElement(element, {
inlineElements: ['my-first-custom-tag', 'my-second-custom-tag']
});
```

## License

This software is [BSD Licensed](/LICENSE).
5 changes: 4 additions & 1 deletion packages/draft-js-import-element/src/stateFromElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type Options = {
blockTypes?: {[key: string]: string};
customBlockFn?: CustomBlockFn;
customInlineFn?: CustomInlineFn;
inlineElements?: string[];
};
type DataMap<T> = {[key: string]: T};

Expand Down Expand Up @@ -403,10 +404,12 @@ class ContentGenerator {

processNode(node: DOMNode) {
if (node.nodeType === NODE_TYPE_ELEMENT) {
let {inlineElements} = this.options;
// $FlowIssue
let element: DOMElement = node;
let tagName = element.nodeName.toLowerCase();
if (INLINE_ELEMENTS.hasOwnProperty(tagName)) {
if (INLINE_ELEMENTS.hasOwnProperty(tagName)
|| (inlineElements && inlineElements.find((el) => el === tagName))) {
this.processInlineElement(element);
} else {
this.processBlockElement(element);
Expand Down
1 change: 1 addition & 0 deletions packages/draft-js-import-html/src/stateFromHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Options = {
blockTypes?: {[key: string]: string};
customBlockFn?: CustomBlockFn;
customInlineFn?: CustomInlineFn;
inlineElements?: string[];
};

const defaultOptions: Options = {};
Expand Down
1 change: 1 addition & 0 deletions packages/draft-js-import-html/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ declare module 'draft-js-import-html' {
elementStyles?: { [styleName: string]: string };
customBlockFn?: CustomBlockFn;
customInlineFn?: CustomInlineFn;
inlineElements?: string []
}

export function stateFromHTML(html: string, options?: Options): draftjs.ContentState;
Expand Down
1 change: 1 addition & 0 deletions packages/draft-js-import-markdown/src/stateFromMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Options = {
customBlockFn?: CustomBlockFn;
customInlineFn?: CustomInlineFn;
parserOptions?: {[key: string]: mixed}; // TODO: Be more explicit
inlineElements?: string[];
};

let defaultOptions: Options = {};
Expand Down