Przeglądaj źródła

normalize cell data when importing or saving a markdowntable

utsushiiro 7 lat temu
rodzic
commit
8e8e2870fd

+ 1 - 1
src/client/js/components/PageEditor/HandsontableModal.jsx

@@ -130,7 +130,7 @@ export default class HandsontableModal extends React.PureComponent {
 
   save() {
     if (this.props.onSave != null) {
-      this.props.onSave(this.state.markdownTable);
+      this.props.onSave(this.state.markdownTable.clone().normalizeCells());
     }
 
     this.hide();

+ 1 - 1
src/client/js/components/PageEditor/MarkdownTableDataImportForm.jsx

@@ -45,7 +45,7 @@ export default class MarkdownTableDataImportForm extends React.Component {
         result = MarkdownTable.fromHTMLTableTag(this.state.data);
         break;
     }
-    return result;
+    return result.normalizeCells();
   }
 
   render() {

+ 13 - 0
src/client/js/models/MarkdownTable.js

@@ -40,6 +40,19 @@ export default class MarkdownTable {
     return new MarkdownTable(newTable, this.options);
   }
 
+  /**
+   * normalize all cell data(trim & convert the newline character to space)
+   */
+  normalizeCells() {
+    for (let i = 0; i < this.table.length; i++) {
+      for (let j = 0; j < this.table[i].length; j++) {
+        this.table[i][j] = this.table[i][j].trim().replace(/\r?\n/g, ' ');
+      }
+    }
+
+    return this;
+  }
+
   /**
    * return a MarkdownTable instance made from a string of HTML table tag
    *