utsushiiro 7 лет назад
Родитель
Сommit
290ecdc7f7
1 измененных файлов с 79 добавлено и 6 удалено
  1. 79 6
      src/client/js/components/PageEditor/HandsontableModal.jsx

+ 79 - 6
src/client/js/components/PageEditor/HandsontableModal.jsx

@@ -21,10 +21,21 @@ const MARKDOWNTABLE_TO_HANDSONTABLE_ALIGNMENT_SYMBOL_MAPPING = {
 
 
 export default class HandsontableModal extends React.PureComponent {
 export default class HandsontableModal extends React.PureComponent {
 
 
-
   constructor(props) {
   constructor(props) {
     super(props);
     super(props);
 
 
+    /*
+     * ## Note ##
+     * Currently, this component try to synchronize the cells data and alignment data of state.markdownTable with these of the HotTable.
+     * However, changes made by the following operations are not synchronized.
+     *
+     * 1. move columns: Alignment changes are synchronized but data changes are not.
+     * 2. move rows: Data changes are not synchronized.
+     * 3. insert columns or rows: Data changes are synchronized but alignment changes are not.
+     * 4. delete columns or rows: Data changes are synchronized but alignment changes are not.
+     *
+     * However, all operations are reflected in the data to be saved because the HotTable data is used when the save method is called.
+     */
     this.state = {
     this.state = {
       show: false,
       show: false,
       isDataImportAreaExpanded: false,
       isDataImportAreaExpanded: false,
@@ -142,8 +153,27 @@ export default class HandsontableModal extends React.PureComponent {
     this.hide();
     this.hide();
   }
   }
 
 
+  /**
+   * An afterLoadData hook
+   *
+   * This performs the following operations.
+   * - clear 'manuallyResizedColumnIndicesSet' for the first loading
+   * - synchronize the handsontable alignment to the markdowntable alignment
+   *
+   * ## Note ##
+   * The afterLoadData hook is called when one of the following states of this component are passed into the setState.
+   *
+   * - markdownTable
+   * - handsontableHeight
+   *
+   * In detail, when the setState method is called with those state passed,
+   * React will start re-render process for the HotTable of this component because the HotTable receives those state values by props.
+   * HotTable#shouldComponentUpdate is called in this re-render process and calls the updateSettings method for the Handsontable instance.
+   * In updateSettings method, the loadData method is called in some case. (refs: https://github.com/handsontable/handsontable/blob/6.2.0/src/core.js#L1652-L1657)
+   * The updateSettings method calls in the HotTable always lead to call the loadData method because the HotTable passes data source by settings.data.
+   * After the loadData method is executed, afterLoadData hooks are called.
+   */
   afterLoadDataHandler(initialLoad) {
   afterLoadDataHandler(initialLoad) {
-    // clear 'manuallyResizedColumnIndicesSet' for the first loading
     if (initialLoad) {
     if (initialLoad) {
       this.manuallyResizedColumnIndicesSet.clear();
       this.manuallyResizedColumnIndicesSet.clear();
     }
     }
@@ -192,12 +222,56 @@ export default class HandsontableModal extends React.PureComponent {
     this.manuallyResizedColumnIndicesSet.clear();
     this.manuallyResizedColumnIndicesSet.clear();
   }
   }
 
 
+  /**
+   * An afterColumnMove hook.
+   *
+   * This synchronizes alignment when columns are moved by manualColumnMove
+   */
   afterColumnMoveHandler(columns, target) {
   afterColumnMoveHandler(columns, target) {
     const align = [].concat(this.state.markdownTable.options.align);
     const align = [].concat(this.state.markdownTable.options.align);
     const removed = align.splice(columns[0], columns.length);
     const removed = align.splice(columns[0], columns.length);
 
 
+    /*
+     * The following is a description of the algorithm for the alignment synchronization.
+     *
+     * Consider the case where the target is X and the columns are [2,3] and data is as follows.
+     *
+     * 0 1 2 3 4 5 (insert position number)
+     * +-+-+-+-+-+
+     * | | | | | |
+     * +-+-+-+-+-+
+     *  0 1 2 3 4  (column index number)
+     *
+     * At first, remove columns by the splice.
+     *
+     * 0 1 2   4 5
+     * +-+-+   +-+
+     * | | |   | |
+     * +-+-+   +-+
+     *  0 1     4
+     *
+     * Next, insert those columns into a new position.
+     * However the target number is a insert position number before deletion, it may be changed.
+     * These are changed as follows.
+     *
+     * Before:
+     * 0 1 2   4 5
+     * +-+-+   +-+
+     * | | |   | |
+     * +-+-+   +-+
+     *
+     * After:
+     * 0 1 2   2 3
+     * +-+-+   +-+
+     * | | |   | |
+     * +-+-+   +-+
+     *
+     * If X is 0, 1 or 2, that is, lower than columns[0], the target number is not changed.
+     * If X is 4 or 5, that is, higher than columns[columns.length - 1], the target number is modified to the original value minus columns.length.
+     *
+     */
     let insertPosition = 0;
     let insertPosition = 0;
-    if (target < columns[0]) {
+    if (target <= columns[0]) {
       insertPosition = target;
       insertPosition = target;
     }
     }
     else if (columns[columns.length - 1] < target) {
     else if (columns[columns.length - 1] < target) {
@@ -206,9 +280,8 @@ export default class HandsontableModal extends React.PureComponent {
     align.splice.apply(align, [insertPosition, 0].concat(removed));
     align.splice.apply(align, [insertPosition, 0].concat(removed));
 
 
     this.setState((prevState) => {
     this.setState((prevState) => {
-      const newMarkdownTable = prevState.markdownTable.clone();
-      newMarkdownTable.options.align = align;
-
+      // change only align info, so share table data to avoid redundant copy
+      const newMarkdownTable = new MarkdownTable(prevState.markdownTable.table, {align: align});
       return { markdownTable: newMarkdownTable };
       return { markdownTable: newMarkdownTable };
     }, () => {
     }, () => {
       this.synchronizeAlignment();
       this.synchronizeAlignment();