chore: Upgrade all of prosemirror (#5366)

Co-authored-by: Apoorv Mishra <apoorvmishra101092@gmail.com>
This commit is contained in:
Tom Moor
2023-05-24 22:24:05 -04:00
committed by GitHub
parent e340e568e2
commit d5341a486c
77 changed files with 875 additions and 675 deletions

View File

@@ -1,5 +1,6 @@
import { NodeSpec, Node as ProsemirrorNode, Schema } from "prosemirror-model";
import { EditorState, Plugin, TextSelection } from "prosemirror-state";
import { chainCommands } from "prosemirror-commands";
import { NodeSpec, Node as ProsemirrorNode } from "prosemirror-model";
import { Command, Plugin, TextSelection } from "prosemirror-state";
import {
addColumnAfter,
addColumnBefore,
@@ -7,25 +8,19 @@ import {
deleteRow,
deleteTable,
goToNextCell,
isInTable,
tableEditing,
toggleHeaderCell,
toggleHeaderColumn,
toggleHeaderRow,
CellSelection,
} from "prosemirror-tables";
import {
addRowAt,
createTable,
getCellsInColumn,
moveRow,
setTextSelection,
} from "prosemirror-utils";
import { Decoration, DecorationSet } from "prosemirror-view";
import {
addRowAfterAndMoveSelection,
setColumnAttr,
createTable,
} from "../commands/table";
import { MarkdownSerializerState } from "../lib/markdown/serializer";
import { getRowIndexFromText } from "../queries/getRowIndex";
import tablesRule from "../rules/tables";
import { Dispatch } from "../types";
import Node from "./Node";
export default class Table extends Node {
@@ -58,49 +53,32 @@ export default class Table extends Node {
return [tablesRule];
}
commands({ schema }: { schema: Schema }) {
commands() {
return {
createTable:
({ rowsCount, colsCount }: { rowsCount: number; colsCount: number }) =>
(state: EditorState, dispatch: Dispatch) => {
const offset = state.tr.selection.anchor + 1;
const nodes = createTable(schema, rowsCount, colsCount);
const tr = state.tr.replaceSelectionWith(nodes).scrollIntoView();
const resolvedPos = tr.doc.resolve(offset);
tr.setSelection(TextSelection.near(resolvedPos));
dispatch(tr);
return true;
},
setColumnAttr:
({ index, alignment }: { index: number; alignment: string }) =>
(state: EditorState, dispatch: Dispatch) => {
const cells = getCellsInColumn(index)(state.selection) || [];
let transaction = state.tr;
cells.forEach(({ pos }) => {
transaction = transaction.setNodeMarkup(pos, undefined, {
alignment,
});
});
dispatch(transaction);
return true;
},
addColumnBefore: () => addColumnBefore,
addColumnAfter: () => addColumnAfter,
deleteColumn: () => deleteColumn,
addRowAfter:
({ index }: { index: number }) =>
(state: EditorState, dispatch: Dispatch) => {
if (index === 0) {
// A little hack to avoid cloning the heading row by cloning the row
// beneath and then moving it to the right index.
const tr = addRowAt(index + 2, true)(state.tr);
dispatch(moveRow(index + 2, index + 1)(tr));
} else {
dispatch(addRowAt(index + 1, true)(state.tr));
({
rowsCount,
colsCount,
}: {
rowsCount: number;
colsCount: number;
}): Command =>
(state, dispatch) => {
if (dispatch) {
const offset = state.tr.selection.anchor + 1;
const nodes = createTable(state, rowsCount, colsCount);
const tr = state.tr.replaceSelectionWith(nodes).scrollIntoView();
const resolvedPos = tr.doc.resolve(offset);
tr.setSelection(TextSelection.near(resolvedPos));
dispatch(tr);
}
return true;
},
setColumnAttr,
addColumnBefore: () => addColumnBefore,
addColumnAfter: () => addColumnAfter,
deleteColumn: () => deleteColumn,
addRowAfter: addRowAfterAndMoveSelection,
deleteRow: () => deleteRow,
deleteTable: () => deleteTable,
toggleHeaderColumn: () => toggleHeaderColumn,
@@ -111,31 +89,9 @@ export default class Table extends Node {
keys() {
return {
Tab: goToNextCell(1),
Tab: chainCommands(goToNextCell(1), addRowAfterAndMoveSelection()),
"Shift-Tab": goToNextCell(-1),
Enter: (state: EditorState, dispatch: Dispatch) => {
if (!isInTable(state)) {
return false;
}
const index = getRowIndexFromText(
state.selection as unknown as CellSelection
);
if (index === 0) {
const cells = getCellsInColumn(0)(state.selection);
if (!cells) {
return false;
}
const tr = addRowAt(index + 2, true)(state.tr);
dispatch(
setTextSelection(cells[1].pos)(moveRow(index + 2, index + 1)(tr))
);
} else {
dispatch(addRowAt(index + 1, true)(state.tr));
}
return true;
},
Enter: addRowAfterAndMoveSelection(),
};
}