Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bumping plate to v30 [TOL-1834] #1605

Merged
merged 23 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d2d4e8e
feat: bumping plate to v30 [TOL-1834]
aodhagan-cf Feb 13, 2024
48c8aa2
fix: fixing table tests by bumping package [TOL-1834]
aodhagan-cf Feb 13, 2024
f92e555
fix: changing table test [TOL-1834]
aodhagan-cf Feb 13, 2024
ddd760d
chore: reverting deserializing package for now [TOL-1834]
aodhagan-cf Feb 13, 2024
05bcc13
chore: modifying character count [TOL-1834]
aodhagan-cf Feb 13, 2024
234ad90
chore: reverting code [TOL-1834]
aodhagan-cf Feb 14, 2024
5cdb204
chore: removing empty paragraph [TOL-1834]
aodhagan-cf Feb 14, 2024
0a3acc2
chore: readding plugin [TOL-1834]
aodhagan-cf Feb 14, 2024
7058625
chore: adjusting table plugins to remove paragraph [TOL-1834]
aodhagan-cf Feb 14, 2024
208f76b
chore: adjusting custom plugin logic [TOL-1834]
aodhagan-cf Feb 14, 2024
f0eb9e5
Merge branch 'master' into feat/TOL-1834
aodhagan-cf Feb 14, 2024
4c497ee
chore: adding comment [TOL-1834]
aodhagan-cf Feb 14, 2024
798a332
chore: fixing type, adjusting logic [TOL-1834]
aodhagan-cf Feb 14, 2024
af844f1
chore: bumping serialize package [TOL-1834]
aodhagan-cf Feb 14, 2024
e09be57
chore: fixing broken test [TOL-1834]
aodhagan-cf Feb 14, 2024
2508560
chore: downgrading package [TOL-1834]
aodhagan-cf Feb 15, 2024
9644838
chore: upgrading package and changing test [TOL-1834]
aodhagan-cf Feb 15, 2024
489b609
chore: downgrading package and fixing test [TOL-1834]
aodhagan-cf Feb 15, 2024
9a6637a
chore: removing normalisation plugin for table []
aodhagan-cf Feb 19, 2024
d641f5c
chore: casting at variable instantiation []
aodhagan-cf Feb 19, 2024
f8aa69d
chore: removing fixme []
aodhagan-cf Feb 19, 2024
90b249f
chore: adding comment at top of file []
aodhagan-cf Feb 19, 2024
b19bda2
chore: overriding table plugin []
aodhagan-cf Feb 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions cypress/e2e/rich-text/document-mocks/googleDocs.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
export default {
nodeType: 'document',
content: [
{
content: [
{
data: {},
marks: [],
nodeType: 'text',
value: '',
},
],
data: {},
nodeType: 'paragraph',
},
{
content: [
{
Expand Down
16 changes: 14 additions & 2 deletions packages/rich-text/src/plugins/Table/createTablePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import {
ELEMENT_TD,
ELEMENT_TH,
ELEMENT_TR,
withTable,
TablePlugin,
withNormalizeTable,
withDeleteTable,
withGetFragmentTable,
withInsertTextTable,
withSelectionTable,
withSetFragmentDataTable,
} from '@udecode/plate-table';

import { isRootLevel } from '../../helpers/editor';
Expand All @@ -27,6 +32,7 @@ import { createEmptyTableCells, getNoOfMissingTableCellsInRow, isNotEmpty } from
import { insertTableFragment } from './insertTableFragment';
import { onKeyDownTable } from './onKeyDownTable';
import { addTableTrackingEvents, withInvalidCellChildrenTracking } from './tableTracking';
import { withInsertFragmentTable } from './withInsertFragmentTable';

export const createTablePlugin = (): PlatePlugin =>
createDefaultTablePlugin<TablePlugin<Value>, Value, PlateEditor>({
Expand All @@ -38,7 +44,13 @@ export const createTablePlugin = (): PlatePlugin =>
withOverrides: (editor, plugin) => {
const { normalizeNode } = editor;
// injects important fixes from plate's original table plugin
withTable(editor, plugin);
editor = withNormalizeTable(editor);
aodhagan-cf marked this conversation as resolved.
Show resolved Hide resolved
editor = withDeleteTable(editor);
editor = withGetFragmentTable(editor);
editor = withInsertFragmentTable(editor, plugin);
editor = withInsertTextTable(editor, plugin);
editor = withSelectionTable(editor);
editor = withSetFragmentDataTable(editor);

// Resets all normalization rules added by @udecode/plate-table as
// they conflict with our own
Expand Down
166 changes: 166 additions & 0 deletions packages/rich-text/src/plugins/Table/withInsertFragmentTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copied from https://github.com/udecode/plate/blob/main/packages/table/src/withInsertFragmentTable.ts
import {
getEndPoint,
getPluginType,
getStartPoint,
getTEditor,
hasNode,
PlateEditor,
replaceNodeChildren,
select,
TElement,
Value,
withoutNormalizing,
WithPlatePlugin,
} from '@udecode/plate-common';
import { ELEMENT_TABLE, getTableAbove, getTableGridAbove, TablePlugin } from '@udecode/plate-table';
import cloneDeep from 'lodash/cloneDeep.js';
import { Path } from 'slate';

/**
* If inserting a table,
aodhagan-cf marked this conversation as resolved.
Show resolved Hide resolved
* If block above anchor is a table,
* - Replace each cell above by the inserted table until out of bounds.
* - Select the inserted cells.
*/
export const withInsertFragmentTable = <
V extends Value = Value,
E extends PlateEditor<V> = PlateEditor<V>
>(
editor: E,
{ options }: WithPlatePlugin<TablePlugin<V>, V, E>
) => {
const { insertFragment } = editor;
const { disableExpandOnInsert, insertColumn, insertRow } = options;

const myEditor = getTEditor(editor);

myEditor.insertFragment = (fragment) => {
const insertedTable = fragment.find(
(n) => (n as TElement).type === getPluginType(editor, ELEMENT_TABLE)
);

if (!insertedTable) {
const tableEntry = getTableAbove(editor, {
at: editor.selection?.anchor,
});

if (tableEntry) {
const cellEntries = getTableGridAbove(editor, {
format: 'cell',
});

if (cellEntries.length > 1) {
cellEntries.forEach((cellEntry) => {
if (cellEntry) {
const [, cellPath] = cellEntry;

replaceNodeChildren(editor, {
at: cellPath,
nodes: cloneDeep(fragment) as any,
});
}
});

select(editor, {
anchor: getStartPoint(editor, cellEntries[0][1]),
focus: getEndPoint(editor, cellEntries.at(-1)![1]),
});
return;
}
}
}

if (insertedTable) {
aodhagan-cf marked this conversation as resolved.
Show resolved Hide resolved
const tableEntry = getTableAbove(editor, {
at: editor.selection?.anchor,
});

// inserting inside table
if (tableEntry) {
const [cellEntry] = getTableGridAbove(editor, {
at: editor.selection?.anchor,
format: 'cell',
});

if (cellEntry) {
withoutNormalizing(editor, () => {
const [, startCellPath] = cellEntry;
const cellPath = [...startCellPath];

const startColIndex = cellPath.at(-1)!;
let lastCellPath: Path | null = null;

let initRow = true;
const insertedRows = (insertedTable as TElement).children as TElement[];
insertedRows.forEach((row) => {
cellPath[cellPath.length - 1] = startColIndex;

// last inserted row
if (!initRow) {
const fromRow = cellPath.slice(0, -1);
cellPath[cellPath.length - 2] += 1;

if (!hasNode(editor, cellPath)) {
if (disableExpandOnInsert) {
return;
} else {
insertRow?.(editor, {
fromRow,
});
}
}
}
initRow = false;

const insertedCells = row.children as TElement[];
let initCell = true;

insertedCells.forEach((cell) => {
if (!initCell) {
const fromCell = [...cellPath];
cellPath[cellPath.length - 1] += 1;

if (!hasNode(editor, cellPath)) {
if (disableExpandOnInsert) {
return;
} else {
insertColumn?.(editor, {
fromCell,
});
}
}
}
initCell = false;

replaceNodeChildren(editor, {
at: cellPath,
nodes: cloneDeep(cell.children as any),
});

lastCellPath = [...cellPath];
});
});

if (lastCellPath) {
select(editor, {
anchor: getStartPoint(editor, startCellPath),
focus: getEndPoint(editor, lastCellPath),
});
}
});

return;
}
} else if (fragment.length === 1 && fragment[0].type === ELEMENT_TABLE) {
// NOTE adjusting this from insertNode to insertFragment to removing preceding empty paragraph
aodhagan-cf marked this conversation as resolved.
Show resolved Hide resolved
insertFragment(fragment);
return;
}
}

insertFragment(fragment);
};

return editor;
};