Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { convertToRaw, convertFromRaw } from "draft-js";
  3. import { convertFromHTML, convertToHTML } from "draft-convert";
  4. import {
  5. DraftailEditor,
  6. BLOCK_TYPE,
  7. INLINE_STYLE,
  8. ENTITY_TYPE
  9. } from "draftail";
  10. import "draft-js/dist/Draft.css";
  11. import "draftail/dist/draftail.css";
  12.  
  13. class HTMLEditorComponent extends Component {
  14. exporterConfig = {
  15. blockToHTML: block => {
  16. if (block.type === BLOCK_TYPE.BLOCKQUOTE) {
  17. return <blockquote />;
  18. }
  19.  
  20. // Discard atomic blocks, as they get converted based on their entity.
  21. if (block.type === BLOCK_TYPE.ATOMIC) {
  22. return {
  23. start: "",
  24. end: ""
  25. };
  26. }
  27.  
  28. return null;
  29. },
  30.  
  31. entityToHTML: (entity, originalText) => {
  32. if (entity.type === ENTITY_TYPE.LINK) {
  33. return <a href={entity.data.url}>{originalText}</a>;
  34. }
  35.  
  36. if (entity.type === ENTITY_TYPE.IMAGE) {
  37. return <img src={entity.data.src} alt={entity.data.alt} />;
  38. }
  39.  
  40. if (entity.type === ENTITY_TYPE.HORIZONTAL_RULE) {
  41. return <hr />;
  42. }
  43.  
  44. return originalText;
  45. }
  46. };
  47.  
  48. importerConfig = {
  49. htmlToEntity: (nodeName, node, createEntity) => {
  50. // a tags will become LINK entities, marked as mutable, with only the URL as data.
  51. if (nodeName === "a") {
  52. return createEntity(ENTITY_TYPE.LINK, "MUTABLE", {
  53. url: node.href
  54. });
  55. }
  56.  
  57. if (nodeName === "img") {
  58. return createEntity(ENTITY_TYPE.IMAGE, "IMMUTABLE", {
  59. src: node.src
  60. });
  61. }
  62.  
  63. if (nodeName === "hr") {
  64. return createEntity(
  65. ENTITY_TYPE.HORIZONTAL_RULE,
  66. "IMMUTABLE",
  67. {}
  68. );
  69. }
  70.  
  71. return null;
  72. },
  73. htmlToBlock: nodeName => {
  74. if (nodeName === "hr" || nodeName === "img") {
  75. // "atomic" blocks is how Draft.js structures block-level entities.
  76. return "atomic";
  77. }
  78.  
  79. return null;
  80. }
  81. };
  82.  
  83. fromHTML = html => convertToRaw(convertFromHTML(this.importerConfig)(html));
  84. toHTML = raw =>
  85. raw ? convertToHTML(this.exporterConfig)(convertFromRaw(raw)) : "";
  86.  
  87. onSave = content => {
  88. this.props.handleSave(this.props.fieldName, this.toHTML(content));
  89. };
  90.  
  91. render() {
  92. return (
  93. <DraftailEditor
  94. rawContentState={this.fromHTML(this.props.contentValue) || null}
  95. onSave={this.onSave}
  96. blockTypes={[
  97. { type: BLOCK_TYPE.ORDERED_LIST_ITEM },
  98. { type: BLOCK_TYPE.UNORDERED_LIST_ITEM }
  99. ]}
  100. inlineStyles={[
  101. { type: INLINE_STYLE.BOLD },
  102. { type: INLINE_STYLE.ITALIC },
  103. { type: INLINE_STYLE.UNDERLINE }
  104. ]}
  105. // entityTypes={[{ type: ENTITY_TYPE.HORIZONTAL_RULE }]}
  106. />
  107. );
  108. }
  109. }
  110.  
  111. export default HTMLEditorComponent;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement