<commit_before>/*
 * table.ts
 *
 * Copyright (C) 2020 by RStudio, PBC
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

import { Schema } from 'prosemirror-model';
import { EditorView } from 'prosemirror-view';
import { Transaction } from 'prosemirror-state';
import { Transform } from 'prosemirror-transform';
import { tableEditing, columnResizing, goToNextCell, deleteColumn, deleteRow } from 'prosemirror-tables';

import { findChildrenByType } from 'prosemirror-utils';

import { EditorUI } from '../../api/ui';
import { Extension } from '../../api/extension';
import { PandocExtensions } from '../../api/pandoc';
import { BaseKey } from '../../api/basekeys';
import { ProsemirrorCommand, EditorCommandId, exitNode } from '../../api/command';
import { TableCapabilities } from '../../api/table';
import { trTransform } from '../../api/transaction';
import { PandocCapabilities } from '../../api/pandoc_capabilities';

import {
  insertTable,
  deleteTable,
  deleteTableCaption,
  addRows,
  addColumns,
  TableColumnAlignmentCommand,
  TableRowCommand,
  TableColumnCommand,
  TableToggleHeaderCommand,
  TableToggleCaptionCommand,
  CssAlignment,
} from './table-commands';

import {
  tableContainerNode,
  tableNode,
  tableCaptionNode,
  tableCellNode,
  tableHeaderNode,
  tableRowNode,
} from './table-nodes';

import { fixupTableWidths } from './table-columns';

import { tablePaste } from './table-paste';

import 'prosemirror-tables/style/tables.css';
import './table-styles.css';

import { TableContextMenuPlugin } from './table-contextmenu';
const extension = (pandocExtensions: PandocExtensions, _caps: PandocCapabilities, ui: EditorUI): Extension | null => {
  // not enabled if there are no tables enabled
  if (
    !pandocExtensions.grid_tables &&
    !pandocExtensions.pipe_tables &&
    !pandocExtensions.simple_tables &&
    !pandocExtensions.multiline_tables
  ) {
    return null;
  }

  // define table capabilities
  const capabilities: TableCapabilities = {
    captions: pandocExtensions.table_captions,
    headerOptional: pandocExtensions.grid_tables,
    multiline: pandocExtensions.multiline_tables || pandocExtensions.grid_tables,
  };

  return {
    nodes: [
      tableContainerNode,
      tableNode,
      tableCaptionNode,
      tableCellNode(pandocExtensions.grid_tables),
      tableHeaderNode,
      tableRowNode,
    ],

    commands: (_schema: Schema) => {
      const commands = [
        new ProsemirrorCommand(EditorCommandId.TableInsertTable, ['Alt-Mod-t'], insertTable(capabilities, ui)),
        new ProsemirrorCommand(EditorCommandId.TableNextCell, ['Tab'], goToNextCell(1)),
        new ProsemirrorCommand(EditorCommandId.TablePreviousCell, ['Shift-Tab'], goToNextCell(-1)),
        new TableColumnCommand(EditorCommandId.TableAddColumnAfter, [], addColumns(true)),
        new TableColumnCommand(EditorCommandId.TableAddColumnBefore, [], addColumns(false)),
        new TableColumnCommand(EditorCommandId.TableDeleteColumn, [], deleteColumn),
        new TableRowCommand(EditorCommandId.TableAddRowAfter, [], addRows(true)),
        new TableRowCommand(EditorCommandId.TableAddRowBefore, [], addRows(false)),
        new TableRowCommand(EditorCommandId.TableDeleteRow, [], deleteRow),
        new ProsemirrorCommand(EditorCommandId.TableDeleteTable, [], deleteTable()),
        new TableColumnAlignmentCommand(EditorCommandId.TableAlignColumnLeft, CssAlignment.Left),
        new TableColumnAlignmentCommand(EditorCommandId.TableAlignColumnRight, CssAlignment.Right),
        new TableColumnAlignmentCommand(EditorCommandId.TableAlignColumnCenter, CssAlignment.Center),
        new TableColumnAlignmentCommand(EditorCommandId.TableAlignColumnDefault, null),
      ];
      if (capabilities.captions) {
        commands.push(new TableToggleCaptionCommand());
      }
      if (capabilities.headerOptional) {
        commands.push(new TableToggleHeaderCommand());
      }
      return commands;
    },

    plugins: (schema: Schema) => {
      return [
        columnResizing({
          handleWidth: 5,
        }),
        tableEditing(),
        tablePaste(),
        new TableContextMenuPlugin(schema, ui),
      ];
    },

    baseKeys: (schema: Schema) => {
      // core keys
      const keys = [
        { key: BaseKey.Backspace, command: deleteTableCaption() },
        { key: BaseKey.Enter, command: exitNode(schema.nodes.table_caption, -2, false) },
        { key: BaseKey.Tab, command: goToNextCell(1) },
        { key: BaseKey.ShiftTab, command: goToNextCell(-1) },
      ];

      // turn enter key variations into tab if we don't support multi-line
      if (!capabilities.multiline) {
        keys.push({ key: BaseKey.Enter, command: goToNextCell(1) });
        keys.push({ key: BaseKey.ShiftEnter, command: goToNextCell(-11) });
        keys.push({ key: BaseKey.ModEnter, command: goToNextCell(1) });
      }

      return keys;
    },

    fixups: (_schema: Schema, view: EditorView) => {
      return [fixupTableWidths(view)];
    },

    appendTransaction: (_schema: Schema) => {
      return [<commit_msg>no enter key shortcuts within single-line tables<commit_after>/*
 * table.ts
 *
 * Copyright (C) 2020 by RStudio, PBC
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

import { Schema } from 'prosemirror-model';
import { EditorView } from 'prosemirror-view';
import { Transaction } from 'prosemirror-state';
import { Transform } from 'prosemirror-transform';
import { tableEditing, columnResizing, goToNextCell, deleteColumn, deleteRow } from 'prosemirror-tables';

import { findChildrenByType } from 'prosemirror-utils';

import { EditorUI } from '../../api/ui';
import { Extension } from '../../api/extension';
import { PandocExtensions } from '../../api/pandoc';
import { BaseKey } from '../../api/basekeys';
import { ProsemirrorCommand, EditorCommandId, exitNode } from '../../api/command';
import { TableCapabilities } from '../../api/table';
import { trTransform } from '../../api/transaction';
import { PandocCapabilities } from '../../api/pandoc_capabilities';

import {
  insertTable,
  deleteTable,
  deleteTableCaption,
  addRows,
  addColumns,
  TableColumnAlignmentCommand,
  TableRowCommand,
  TableColumnCommand,
  TableToggleHeaderCommand,
  TableToggleCaptionCommand,
  CssAlignment,
} from './table-commands';

import {
  tableContainerNode,
  tableNode,
  tableCaptionNode,
  tableCellNode,
  tableHeaderNode,
  tableRowNode,
} from './table-nodes';

import { fixupTableWidths } from './table-columns';

import { tablePaste } from './table-paste';

import 'prosemirror-tables/style/tables.css';
import './table-styles.css';

import { TableContextMenuPlugin } from './table-contextmenu';
const extension = (pandocExtensions: PandocExtensions, _caps: PandocCapabilities, ui: EditorUI): Extension | null => {
  // not enabled if there are no tables enabled
  if (
    !pandocExtensions.grid_tables &&
    !pandocExtensions.pipe_tables &&
    !pandocExtensions.simple_tables &&
    !pandocExtensions.multiline_tables
  ) {
    return null;
  }

  // define table capabilities
  const capabilities: TableCapabilities = {
    captions: pandocExtensions.table_captions,
    headerOptional: pandocExtensions.grid_tables,
    multiline: pandocExtensions.multiline_tables || pandocExtensions.grid_tables,
  };

  return {
    nodes: [
      tableContainerNode,
      tableNode,
      tableCaptionNode,
      tableCellNode(pandocExtensions.grid_tables),
      tableHeaderNode,
      tableRowNode,
    ],

    commands: (_schema: Schema) => {
      const commands = [
        new ProsemirrorCommand(EditorCommandId.TableInsertTable, ['Alt-Mod-t'], insertTable(capabilities, ui)),
        new ProsemirrorCommand(EditorCommandId.TableNextCell, ['Tab'], goToNextCell(1)),
        new ProsemirrorCommand(EditorCommandId.TablePreviousCell, ['Shift-Tab'], goToNextCell(-1)),
        new TableColumnCommand(EditorCommandId.TableAddColumnAfter, [], addColumns(true)),
        new TableColumnCommand(EditorCommandId.TableAddColumnBefore, [], addColumns(false)),
        new TableColumnCommand(EditorCommandId.TableDeleteColumn, [], deleteColumn),
        new TableRowCommand(EditorCommandId.TableAddRowAfter, [], addRows(true)),
        new TableRowCommand(EditorCommandId.TableAddRowBefore, [], addRows(false)),
        new TableRowCommand(EditorCommandId.TableDeleteRow, [], deleteRow),
        new ProsemirrorCommand(EditorCommandId.TableDeleteTable, [], deleteTable()),
        new TableColumnAlignmentCommand(EditorCommandId.TableAlignColumnLeft, CssAlignment.Left),
        new TableColumnAlignmentCommand(EditorCommandId.TableAlignColumnRight, CssAlignment.Right),
        new TableColumnAlignmentCommand(EditorCommandId.TableAlignColumnCenter, CssAlignment.Center),
        new TableColumnAlignmentCommand(EditorCommandId.TableAlignColumnDefault, null),
      ];
      if (capabilities.captions) {
        commands.push(new TableToggleCaptionCommand());
      }
      if (capabilities.headerOptional) {
        commands.push(new TableToggleHeaderCommand());
      }
      return commands;
    },

    plugins: (schema: Schema) => {
      return [
        columnResizing({
          handleWidth: 5,
        }),
        tableEditing(),
        tablePaste(),
        new TableContextMenuPlugin(schema, ui),
      ];
    },

    baseKeys: (schema: Schema) => {
      // core keys
      const keys = [
        { key: BaseKey.Backspace, command: deleteTableCaption() },
        { key: BaseKey.Enter, command: exitNode(schema.nodes.table_caption, -2, false) },
        { key: BaseKey.Tab, command: goToNextCell(1) },
        { key: BaseKey.ShiftTab, command: goToNextCell(-1) },
      ];

      return keys;
    },

    fixups: (_schema: Schema, view: EditorView) => {
      return [fixupTableWidths(view)];
    },

    appendTransaction: (_schema: Schema) => {
      return [
{'fraction_non_alphanumeric': 0.1148103496244088, 'fraction_numerical': 0.0027821570991375315, 'mean_word_length': 3.7339771729587357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4149052', 'n_tokens_mistral': 3069, 'n_tokens_neox': 3018, 'n_words': 706}