Przeglądaj źródła

Fix Draftail icon wrapping happening even if there is no icon

Thibaud Colas 7 lat temu
rodzic
commit
d8489574d3
1 zmienionych plików z 33 dodań i 22 usunięć
  1. 33 22
      client/src/components/Draftail/index.js

+ 33 - 22
client/src/components/Draftail/index.js

@@ -11,12 +11,22 @@ import EmbedBlock from './blocks/EmbedBlock';
 
 import registry from './registry';
 
+const wrapWagtailIcon = type => {
+  if (type.icon) {
+    return Object.assign(type, {
+      icon: <Icon name={type.icon} />,
+    });
+  }
+
+  return type;
+}
+
 export const initEditor = (fieldName, options = {}) => {
   const field = document.querySelector(`[name="${fieldName}"]`);
   const editorWrapper = document.createElement('div');
   field.parentNode.appendChild(editorWrapper);
 
-  const serialiseInputValue = (rawContentState) => {
+  const serialiseInputValue = rawContentState => {
     // TODO Remove default {} when finishing https://github.com/springload/wagtaildraftail/issues/32.
     field.value = JSON.stringify(rawContentState || {});
   };
@@ -26,30 +36,28 @@ export const initEditor = (fieldName, options = {}) => {
   let entityTypes;
 
   if (options && options.blockTypes) {
-    blockTypes = options.blockTypes.map(type => Object.assign(type, {
-      icon: <Icon name={type.icon} />,
-    }));
+    blockTypes = options.blockTypes.map(wrapWagtailIcon);
   }
 
   if (options && options.inlineStyles) {
-    inlineStyles = options.inlineStyles.map(type => Object.assign(type, {
-      icon: <Icon name={type.icon} />,
-    }));
+    inlineStyles = options.inlineStyles.map(wrapWagtailIcon);
   }
 
   if (options && options.entityTypes) {
-    entityTypes = options.entityTypes.map(type => Object.assign(type, {
-      icon: <Icon name={type.icon} />,
-      source: registry.getSource(type.source),
-      strategy: registry.getStrategy(type.type) || null,
-      decorator: registry.getDecorator(type.decorator),
-      block: registry.getBlock(type.block),
-    }));
+    entityTypes = options.entityTypes.map(wrapWagtailIcon).map(type =>
+      Object.assign(type, {
+        source: registry.getSource(type.source),
+        strategy: registry.getStrategy(type.type) || null,
+        decorator: registry.getDecorator(type.decorator),
+        block: registry.getBlock(type.block),
+      }),
+    );
   }
 
   const fieldValue = JSON.parse(field.value);
   // TODO Remove default null when finishing https://github.com/springload/wagtaildraftail/issues/32.
-  const rawContentState = fieldValue && Object.keys(fieldValue).length === 0 ? null : fieldValue;
+  const rawContentState =
+    fieldValue && Object.keys(fieldValue).length === 0 ? null : fieldValue;
 
   const editor = (
     <DraftailEditor
@@ -74,12 +82,15 @@ registry.registerBlocks({
   EmbedBlock,
 });
 
-const draftail = Object.assign({
-  initEditor: initEditor,
-  // Expose basic React methods for basic needs
-  // TODO Expose React as global as part of Wagtail vendor file instead of doing this.
-  // createClass: React.createClass,
-  // createElement: React.createElement,
-}, registry);
+const draftail = Object.assign(
+  {
+    initEditor: initEditor,
+    // Expose basic React methods for basic needs
+    // TODO Expose React as global as part of Wagtail vendor file instead of doing this.
+    // createClass: React.createClass,
+    // createElement: React.createElement,
+  },
+  registry,
+);
 
 export default draftail;