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 all commits
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
13 changes: 7 additions & 6 deletions cypress/e2e/rich-text/RichTextEditor.Tables.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import { RichTextPage } from './RichTextPage';
describe('Rich Text Editor', { viewportHeight: 2000 }, () => {
let richText: RichTextPage;

// copied from the 'is-hotkey' library we use for RichText shortcuts
const IS_MAC =
typeof window != 'undefined' && /Mac|iPod|iPhone|iPad/.test(window.navigator.platform);
const mod = IS_MAC ? 'meta' : 'control';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not used anymore, highlighting table content in a different way now as the test was breaking due to the CMD+A logic

const buildHelper =
(type) =>
(...children) =>
Expand Down Expand Up @@ -177,10 +173,15 @@ describe('Rich Text Editor', { viewportHeight: 2000 }, () => {
);
});

it('does not delete table header cells when selecting the whole table', () => {
it('does not delete table header cells when selecting the whole header row', () => {
aodhagan-cf marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue this is now a completely different test and probably worth a follow up to see if it's even needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree, will sync with @colomolo on this tomorrow

insertTable();

richText.editor.type(`hey{${mod}}a{backspace}`);
richText.editor
.type(`hey`)
.type('{shift}{downarrow}', {
delay: 100,
})
.type('{backspace}');

richText.expectValue(
doc(
Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/rich-text/RichTextEditor.Tracking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('Rich Text Editor - Tracking', { viewportHeight: 2000 }, () => {

richText.expectTrackingValue([
action('paste', 'shortcut-or-viewport', {
characterCountAfter: 14,
characterCountAfter: 11,
aodhagan-cf marked this conversation as resolved.
Show resolved Hide resolved
characterCountBefore: 0,
characterCountSelection: 0,
source: 'Apple Notes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default {
marks: [],
nodeType: 'text',
value:
" is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. ",
" is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. ",
aodhagan-cf marked this conversation as resolved.
Show resolved Hide resolved
},
{
data: {},
Expand Down
24 changes: 12 additions & 12 deletions packages/rich-text/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@
"@contentful/rich-text-plain-text-renderer": "^16.0.4",
"@contentful/rich-text-types": "16.3.0",
"@popperjs/core": "^2.11.5",
"@udecode/plate-basic-marks": "23.7.0",
"@udecode/plate-break": "23.7.0",
"@udecode/plate-common": "23.7.0",
"@udecode/plate-core": "23.6.0",
"@udecode/plate-list": "23.7.0",
"@udecode/plate-paragraph": "23.7.0",
"@udecode/plate-reset-node": "23.7.0",
"@udecode/plate-select": "23.7.0",
"@udecode/plate-basic-marks": "30.1.2",
"@udecode/plate-break": "30.1.2",
"@udecode/plate-common": "30.1.2",
"@udecode/plate-core": "30.1.2",
"@udecode/plate-list": "30.1.2",
"@udecode/plate-paragraph": "30.1.2",
"@udecode/plate-reset-node": "30.1.2",
"@udecode/plate-select": "30.1.2",
"@udecode/plate-serializer-docx": "23.7.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of the pasting from word online and google doc break with this update. Probably better to handle in another ticket

"@udecode/plate-serializer-html": "23.7.0",
"@udecode/plate-table": "23.7.0",
"@udecode/plate-trailing-block": "23.7.0",
"@udecode/plate-serializer-html": "30.1.2",
"@udecode/plate-table": "30.1.2",
"@udecode/plate-trailing-block": "30.1.2",
"constate": "3.2.0",
"fast-deep-equal": "^3.1.3",
"is-hotkey": "^0.2.0",
Expand All @@ -69,7 +69,7 @@
"slate": "0.94.1",
"slate-history": "0.100.0",
"slate-hyperscript": "0.77.0",
"slate-react": "0.98.3"
"slate-react": "0.102.0"
},
"peerDependencies": {
"react": ">=16.14.0",
Expand Down
16 changes: 5 additions & 11 deletions packages/rich-text/src/RichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FieldAppSDK } from '@contentful/app-sdk';
import { EntityProvider } from '@contentful/field-editor-reference';
import { FieldConnector } from '@contentful/field-editor-shared';
import * as Contentful from '@contentful/rich-text-types';
import { Plate, PlateProvider } from '@udecode/plate-common';
import { PlateContent, Plate, PlatePlugin } from '@udecode/plate-common';
import { css, cx } from 'emotion';
import deepEquals from 'fast-deep-equal';
import noop from 'lodash/noop';
Expand Down Expand Up @@ -64,10 +64,10 @@ export const ConnectedRichTextEditor = (props: ConnectedProps) => {
<SdkProvider sdk={sdk}>
<ContentfulEditorIdProvider value={id}>
<div className={styles.root} data-test-id="rich-text-editor">
<PlateProvider
<Plate
id={id}
initialValue={initialValue}
plugins={plugins}
plugins={plugins as PlatePlugin[]}
disableCorePlugins={disableCorePlugins}
>
{!props.isToolbarHidden && (
Expand All @@ -76,14 +76,8 @@ export const ConnectedRichTextEditor = (props: ConnectedProps) => {
</StickyToolbarWrapper>
)}
<SyncEditorChanges incomingValue={initialValue} onChange={props.onChange} />
<Plate
id={id}
editableProps={{
className: classNames,
readOnly: props.isDisabled,
}}
/>
</PlateProvider>
<PlateContent id={id} className={classNames} readOnly={props.isDisabled} />
</Plate>
</div>
</ContentfulEditorIdProvider>
</SdkProvider>
Expand Down
24 changes: 11 additions & 13 deletions packages/rich-text/src/SyncEditorChanges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import equal from 'fast-deep-equal';
import { createOnChangeCallback } from './helpers/callbacks';
import { usePlateSelectors } from './internal/hooks';
import { setEditorValue } from './internal/transforms';
import { Value } from './internal/types';
import { PlateEditor, Value } from './internal/types';

/**
* A hook responsible for keeping the editor state in sync with incoming
* changes (aka. external updates
*/
const useAcceptIncomingChanges = (incomingValue?: Value) => {
const editor = usePlateSelectors().editor();
const editor = usePlateSelectors().editor() as PlateEditor;

// Cache latest editor value to avoid unnecessary updates
const lastIncomingValue = React.useRef(incomingValue);
Expand All @@ -39,19 +39,17 @@ const useOnValueChanged = (onChange?: (doc: Contentful.Document) => unknown) =>
React.useEffect(() => {
const cb = createOnChangeCallback(onChange);

setEditorOnChange({
fn: (document) => {
// Skip irrelevant events e.g. mouse selection
const operations = editor?.operations.filter((op) => {
return op.type !== 'set_selection';
});
setEditorOnChange((document) => {
// Skip irrelevant events e.g. mouse selection
const operations = editor?.operations.filter((op) => {
return op.type !== 'set_selection';
});

if (operations.length === 0) {
return;
}
if (operations.length === 0) {
return;
}

cb(document);
},
cb(document);
});
}, [editor, onChange, setEditorOnChange]);
};
Expand Down
6 changes: 3 additions & 3 deletions packages/rich-text/src/internal/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { PlateEditor, Value } from './types';
export const useReadOnly = sr.useReadOnly;

export const usePlateEditorRef = (id?: string) => {
return p.usePlateEditorRef<Value, PlateEditor>(id);
return p.useEditorRef<Value, PlateEditor>(id);
};

export const usePlateEditorState = (id?: string) => {
return p.usePlateEditorState<Value, PlateEditor>(id);
return p.useEditorState<Value, PlateEditor>(id);
};

export const usePlateSelectors = (id?: string) => {
return p.usePlateSelectors<Value, PlateEditor>(id);
return p.usePlateSelectors(id);
};

export const useFocused = () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/rich-text/src/plugins/List/withList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const withList: WithOverride = (editor) => {
const { deleteForward, deleteFragment } = editor;

editor.deleteForward = (unit) => {
if (deleteForwardList(editor)) return;
if (deleteForwardList(editor, deleteForward, unit)) return;

deleteForward(unit);
};
Expand Down
17 changes: 15 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,
withDeleteTable,
withGetFragmentTable,
withInsertTextTable,
withSelectionTable,
withSetFragmentDataTable,
withInsertFragmentTable,
} 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 { withInsertFragmentTableOverride } from './withInsertFragmentTableOverride';

export const createTablePlugin = (): PlatePlugin =>
createDefaultTablePlugin<TablePlugin<Value>, Value, PlateEditor>({
Expand All @@ -38,7 +44,14 @@ export const createTablePlugin = (): PlatePlugin =>
withOverrides: (editor, plugin) => {
const { normalizeNode } = editor;
// injects important fixes from plate's original table plugin
withTable(editor, plugin);
editor = withDeleteTable(editor);
editor = withGetFragmentTable(editor);
editor = withInsertFragmentTable(editor, plugin);
// overrides insertFragment to handle table insertion to not add empty paragraph before table
editor = withInsertFragmentTableOverride(editor);
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
2 changes: 1 addition & 1 deletion packages/rich-text/src/plugins/Table/onKeyDownTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getTableEntries, onKeyDownTable as defaultKeyDownTable } from '@udecode

import { insertEmptyParagraph } from '../../helpers/editor';
import { blurEditor } from '../../internal/misc';
import { getAboveNode, isLastChildPath, getText, isFirstChild } from '../../internal/queries';
import { getAboveNode, getText, isFirstChild, isLastChildPath } from '../../internal/queries';
import { KeyboardHandler, HotkeyPlugin, NodeEntry } from '../../internal/types';
import { addRowBelow } from './actions';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ELEMENT_DEFAULT, getPluginType, getTEditor, TElement } from '@udecode/plate-common';
import { ELEMENT_TABLE } from '@udecode/plate-table';

import { insertNodes, PlateEditor } from '../../internal';

export const withInsertFragmentTableOverride = (editor: PlateEditor) => {
const myEditor = getTEditor(editor);

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

// Overriding https://github.com/udecode/plate/blob/aa5ad441cb72cfb6704315237c430f9a43570ffe/packages/table/src/withInsertFragmentTable.ts#L159C9-L163C8
// as we don't want the empty paragraph before the table when inserting a table
if (insertedTable && fragment.length === 1 && fragment[0].type === ELEMENT_TABLE) {
// needed to insert as node, otherwise it will be inserted as text
insertNodes(editor, fragment, {
removeEmpty: {
// removes empty paragraph before table
exclude: [ELEMENT_DEFAULT],
},
});
return;
} else {
upstreamInsertFragment(fragment);
}
};

return editor;
};
Loading
Loading