All files / app/assets/javascripts/content_editor/components/wrappers table_cell_base.vue

100% Statements 17/17
100% Branches 46/46
100% Functions 2/2
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192            62x 62x     70x 70x 70x 70x 70x   70x 70x 70x 70x 70x   70x 70x 70x 70x   70x                                                                                                                                                                                                                                                                                                                                        
<script>
import { GlDisclosureDropdown } from '@gitlab/ui';
import { NodeViewWrapper, NodeViewContent } from '@tiptap/vue-2';
import { selectedRect as getSelectedRect } from '@tiptap/pm/tables';
import { __, n__ } from '~/locale';
 
const TABLE_CELL_HEADER = 'th';
const TABLE_CELL_BODY = 'td';
 
function getDropdownItems({ selectedRect, cellType, rowspan = 1, colspan = 1, align = 'left' }) {
  const totalRows = selectedRect?.map.height;
  const totalCols = selectedRect?.map.width;
  const isTableBodyCell = cellType === TABLE_CELL_BODY;
  const selectedRows = selectedRect ? selectedRect.bottom - selectedRect.top : 0;
  const selectedCols = selectedRect ? selectedRect.right - selectedRect.left : 0;
  const showSplitCellOption =
    selectedRows === rowspan && selectedCols === colspan && (rowspan > 1 || colspan > 1);
  const showMergeCellsOption = selectedRows !== rowspan || selectedCols !== colspan;
  const numCellsToMerge = (selectedRows - rowspan + 1) * (selectedCols - colspan + 1);
  const showDeleteRowOption = totalRows > selectedRows + 1 && isTableBodyCell;
  const showDeleteColumnOption = totalCols > selectedCols;
 
  const isTableBodyHeader = cellType === TABLE_CELL_HEADER;
  const showAlignLeftOption = isTableBodyHeader && (align === 'center' || align === 'right');
  const showAlignCenterOption = isTableBodyHeader && align !== 'center';
  const showAlignRightOption = isTableBodyHeader && align !== 'right';
 
  return [
    {
      items: [
        showAlignLeftOption && { text: __('Align column left'), value: 'alignColumnLeft' },
        showAlignCenterOption && { text: __('Align column center'), value: 'alignColumnCenter' },
        showAlignRightOption && { text: __('Align column right'), value: 'alignColumnRight' },
      ].filter(Boolean),
    },
    {
      items: [
        { text: __('Insert column before'), value: 'addColumnBefore' },
        { text: __('Insert column after'), value: 'addColumnAfter' },
        isTableBodyCell && { text: __('Insert row before'), value: 'addRowBefore' },
        { text: __('Insert row after'), value: 'addRowAfter' },
      ].filter(Boolean),
    },
    {
      items: [
        showSplitCellOption && { text: __('Split cell'), value: 'splitCell' },
        showMergeCellsOption && {
          text: n__('Merge %d cell', 'Merge %d cells', numCellsToMerge),
          value: 'mergeCells',
        },
      ].filter(Boolean),
    },
    {
      items: [
        showDeleteRowOption && {
          text: n__('Delete row', 'Delete %d rows', selectedRows),
          value: 'deleteRow',
        },
        showDeleteColumnOption && {
          text: n__('Delete column', 'Delete %d columns', selectedCols),
          value: 'deleteColumn',
        },
        { text: __('Delete table'), value: 'deleteTable' },
      ].filter(Boolean),
    },
  ].filter(({ items }) => items.length);
}
 
export default {
  name: 'TableCellBaseWrapper',
  components: {
    NodeViewWrapper,
    NodeViewContent,
    GlDisclosureDropdown,
  },
  props: {
    getPos: {
      type: Function,
      required: true,
    },
    cellType: {
      type: String,
      validator: (type) => [TABLE_CELL_HEADER, TABLE_CELL_BODY].includes(type),
      required: true,
    },
    editor: {
      type: Object,
      required: true,
    },
    node: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      displayActionsDropdown: false,
      selectedRect: null,
    };
  },
  computed: {
    dropdownItems() {
      return getDropdownItems({
        selectedRect: this.selectedRect,
        cellType: this.cellType,
        rowspan: this.node.attrs.rowspan,
        colspan: this.node.attrs.colspan,
        align: this.node.attrs.align,
      });
    },
  },
  mounted() {
    this.editor.on('selectionUpdate', this.handleSelectionUpdate);
    this.handleSelectionUpdate();
  },
  beforeDestroy() {
    this.editor.off('selectionUpdate', this.handleSelectionUpdate);
  },
  methods: {
    handleSelectionUpdate() {
      const { state } = this.editor;
      const { $cursor } = state.selection;
 
      try {
        this.selectedRect = getSelectedRect(state);
      } catch (e) {
        // ignore error if the selection is not in a table
        return;
      }
 
      if (!$cursor) return;
 
      this.displayActionsDropdown = false;
 
      for (let level = 0; level < $cursor.depth; level += 1) {
        if ($cursor.node(level) === this.node) {
          this.displayActionsDropdown = true;
          break;
        }
      }
    },
 
    runCommand({ value: command }) {
      this.hideDropdown();
      this.editor.chain()[command](this.getPos()).run();
    },
 
    hideDropdown() {
      this.$refs.dropdown?.close();
    },
  },
};
</script>
<template>
  <node-view-wrapper
    :as="cellType"
    :rowspan="node.attrs.rowspan || 1"
    :colspan="node.attrs.colspan || 1"
    :align="node.attrs.align || 'left'"
    dir="auto"
    class="gl-m-0! gl-p-0! gl-relative"
    @click="hideDropdown"
  >
    <span
      v-if="displayActionsDropdown"
      :contenteditable="false"
      data-testid="actions-dropdown"
      class="gl-absolute gl-right-0 gl-top-0 gl-pr-1 gl-pt-1"
    >
      <gl-disclosure-dropdown
        ref="dropdown"
        dropup
        icon="chevron-down"
        size="small"
        category="tertiary"
        boundary="viewport"
        no-caret
        text-sr-only
        :items="dropdownItems"
        :toggle-text="__('Edit table')"
        positioning-strategy="fixed"
        @action="runCommand"
      />
    </span>
    <node-view-content
      as="div"
      class="gl-p-5 gl-min-w-10"
      :style="{ 'text-align': node.attrs.align || 'left' }"
    />
  </node-view-wrapper>
</template>