Skip to content

Commit fa8b69f

Browse files
fix(rich-text): revert upgrade slate, slate-react and add slate-dom (#2010)
This reverts commit 3a5b6fc.
1 parent 705e847 commit fa8b69f

File tree

8 files changed

+107
-34
lines changed

8 files changed

+107
-34
lines changed

cypress/component/rich-text/RichTextEditor.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,18 @@ describe('Rich Text Editor', { viewportHeight: 2000, viewportWidth: 1000 }, () =
147147
.parent()
148148
.parent()
149149
.dragTo(() => richText.editor.findByText('some text.'));
150-
151-
richText.expectValue(doc(entryBlock(), paragraph, emptyParagraph()));
150+
if (Cypress.browser.name === 'firefox') {
151+
richText.expectValue(doc(entryBlock(), paragraph, emptyParagraph()));
152+
} else {
153+
richText.expectValue(
154+
doc(
155+
block(BLOCKS.PARAGRAPH, {}, text('some')),
156+
entryBlock(),
157+
block(BLOCKS.PARAGRAPH, {}, text(' text.')),
158+
emptyParagraph(),
159+
),
160+
);
161+
}
152162

153163
// undo
154164
// Ensures that drag&drop was recorded in a separate history batch,

packages/rich-text/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@ import 'codemirror/lib/codemirror.css';
1111
import { RichTextEditor } from '@contentful/field-editor-rich-text';
1212
```
1313

14+
## ⚠️ Important: Package Configuration
15+
16+
Due to peer dependency resolution in npm, you must add the following to your `package.json` to ensure compatible versions are installed:
17+
This is a temporary workaround until we have upgraded the underlying rich text packages.
18+
19+
**For npm:**
20+
21+
```json
22+
{
23+
"overrides": {
24+
"slate": "0.94.1",
25+
"slate-react": "0.102.0"
26+
}
27+
}
28+
```
29+
30+
**For yarn:**
31+
32+
```json
33+
{
34+
"resolutions": {
35+
"slate": "0.94.1",
36+
"slate-react": "0.102.0"
37+
}
38+
}
39+
```
40+
1441
## Migrating to v2
1542

1643
To bring support for Rich Text Tables we rewrote most of the internals of this package to adapt the latest version of [Slate][slate]. We are releasing this change as v2.0.0.

packages/rich-text/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@
6868
"moment": "^2.20.0",
6969
"p-debounce": "^2.1.0",
7070
"react-popper": "^2.3.0",
71-
"slate": "0.118.1",
72-
"slate-dom": "0.118.1",
71+
"slate": "0.94.1",
7372
"slate-history": "0.100.0",
7473
"slate-hyperscript": "0.77.0",
75-
"slate-react": "0.118.2"
74+
"slate-react": "0.102.0"
7675
},
7776
"peerDependencies": {
7877
"@lingui/core": "^5.3.0",

packages/rich-text/src/RichTextEditor.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { FieldAppSDK } from '@contentful/app-sdk';
44
import { EntityProvider } from '@contentful/field-editor-reference';
55
import { FieldConnector } from '@contentful/field-editor-shared';
66
import * as Contentful from '@contentful/rich-text-types';
7-
import { PlateContent, Plate, PlatePlugin } from '@udecode/plate-common';
7+
import { PlateContent, Plate, PlatePlugin, PlateContentProps } from '@udecode/plate-common';
88
import { css, cx } from 'emotion';
99
import deepEquals from 'fast-deep-equal';
1010
import noop from 'lodash/noop';
11-
import { defaultScrollSelectionIntoView } from 'slate-react';
1211

1312
import { CharConstraints } from './CharConstraints';
1413
import { ContentfulEditorIdProvider, getContentfulEditorId } from './ContentfulEditorProvider';
14+
import { defaultScrollSelectionIntoView } from './editor-overrides';
1515
import { toSlateValue } from './helpers/toSlateValue';
1616
import { normalizeInitialValue } from './internal/misc';
1717
import { getPlugins, disableCorePlugins } from './plugins';
@@ -111,7 +111,9 @@ export const ConnectedRichTextEditor = (props: ConnectedRichTextProps) => {
111111
id={id}
112112
className={classNames}
113113
readOnly={props.isDisabled}
114-
scrollSelectionIntoView={defaultScrollSelectionIntoView}
114+
scrollSelectionIntoView={
115+
defaultScrollSelectionIntoView as PlateContentProps['scrollSelectionIntoView']
116+
}
115117
/>
116118
{props.withCharValidation && <CharConstraints />}
117119
</Plate>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './scroll-selection-into-view';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* TODO: This is an override from a later version of slate-react
3+
* ultimately instead of overriding we want to upgrade the version
4+
*
5+
* https://github.com/ianstormtaylor/slate/pull/5902/files
6+
*/
7+
import scrollIntoView from 'scroll-into-view-if-needed';
8+
9+
import { DOMRange, ReactEditor, Range } from '../internal';
10+
11+
export const defaultScrollSelectionIntoView = (editor: ReactEditor, domRange: DOMRange) => {
12+
// This was affecting the selection of multiple blocks and dragging behavior,
13+
// so enabled only if the selection has been collapsed.
14+
if (
15+
domRange.getBoundingClientRect &&
16+
(!editor.selection || (editor.selection && Range.isCollapsed(editor.selection)))
17+
) {
18+
const leafEl = domRange.startContainer.parentElement!;
19+
20+
// COMPAT: In Chrome, domRange.getBoundingClientRect() can return zero dimensions for valid ranges (e.g. line breaks).
21+
// When this happens, do not scroll like most editors do.
22+
const domRect = domRange.getBoundingClientRect();
23+
const isZeroDimensionRect =
24+
domRect.width === 0 && domRect.height === 0 && domRect.x === 0 && domRect.y === 0;
25+
26+
if (isZeroDimensionRect) {
27+
const leafRect = leafEl.getBoundingClientRect();
28+
const leafHasDimensions = leafRect.width > 0 || leafRect.height > 0;
29+
30+
if (leafHasDimensions) {
31+
return;
32+
}
33+
}
34+
35+
// Default behavior: use domRange's getBoundingClientRect
36+
leafEl.getBoundingClientRect = domRange.getBoundingClientRect.bind(domRange);
37+
scrollIntoView(leafEl, {
38+
scrollMode: 'if-needed',
39+
});
40+
// @ts-expect-error an unorthodox delete D:
41+
delete leafEl.getBoundingClientRect;
42+
}
43+
};

packages/rich-text/src/internal/types/editor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import { MARKS } from '@contentful/rich-text-types';
55
import * as p from '@udecode/plate-common';
66
import * as s from 'slate';
7-
import { DOMRange as SlateReactDomRange } from 'slate-dom';
87
import * as sr from 'slate-react';
8+
import { DOMRange as SlateReactDomRange } from 'slate-react/dist/utils/dom';
99
import type {
1010
SelectionMoveOptions as SlateSelectionMoveOptions,
1111
SelectionCollapseOptions as SlateSelectionCollapseOptions,

yarn.lock

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7646,7 +7646,7 @@
76467646
resolved "https://registry.yarnpkg.com/@types/is-empty/-/is-empty-1.2.3.tgz#a2d55ea8a5ec57bf61e411ba2a9e5132fe4f0899"
76477647
integrity sha512-4J1l5d79hoIvsrKh5VUKVRA1aIdsOb10Hu5j3J2VfP/msDnfTdGPmNp2E1Wg+vs97Bktzo+MZePFFXSGoykYJw==
76487648

7649-
"@types/is-hotkey@^0.1.6":
7649+
"@types/is-hotkey@^0.1.6", "@types/is-hotkey@^0.1.8":
76507650
version "0.1.10"
76517651
resolved "https://registry.yarnpkg.com/@types/is-hotkey/-/is-hotkey-0.1.10.tgz#cf440fab9bf75ffba4e1a16e8df28938de0778c9"
76527652
integrity sha512-RvC8KMw5BCac1NvRRyaHgMMEtBaZ6wh0pyPTBu7izn4Sj/AX9Y4aXU5c7rX8PnM/knsuUpC1IeoBkANtxBypsQ==
@@ -7730,7 +7730,7 @@
77307730
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.202.tgz#f09dbd2fb082d507178b2f2a5c7e74bd72ff98f8"
77317731
integrity sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ==
77327732

7733-
"@types/lodash@^4.14.167":
7733+
"@types/lodash@^4.14.167", "@types/lodash@^4.14.200":
77347734
version "4.17.0"
77357735
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.17.0.tgz#d774355e41f372d5350a4d0714abb48194a489c3"
77367736
integrity sha512-t7dhREVv6dbNj0q17X12j7yDG4bD/DHYX7o5/DbDxobP0HnGPgpRz2Ej77aL7TZT3DSw13fqUTj8J4mMnqa7WA==
@@ -15631,7 +15631,7 @@ immer@^10.0.3:
1563115631
resolved "https://registry.yarnpkg.com/immer/-/immer-10.0.4.tgz#09af41477236b99449f9d705369a4daaf780362b"
1563215632
integrity sha512-cuBuGK40P/sk5IzWa9QPUaAdvPHjkk1c+xYsd9oZw+YQQEV+10G0P5uMpGctZZKnyQ+ibRO08bD25nWLmYi2pw==
1563315633

15634-
immer@^9.0.7:
15634+
immer@^9.0.6, immer@^9.0.7:
1563515635
version "9.0.21"
1563615636
resolved "https://registry.yarnpkg.com/immer/-/immer-9.0.21.tgz#1e025ea31a40f24fb064f1fef23e931496330176"
1563715637
integrity sha512-bc4NBHqOqSfRW7POMkHd51LvClaeMXpm8dx0e8oE2GORbq5aRK7Bxl4FyzVLdGtLmvLKL7BTDBG5ACQm4HWjTA==
@@ -23879,19 +23879,6 @@ slash@^4.0.0:
2387923879
resolved "https://registry.yarnpkg.com/slash/-/slash-4.0.0.tgz#2422372176c4c6c5addb5e2ada885af984b396a7"
2388023880
integrity sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew==
2388123881

23882-
23883-
version "0.118.1"
23884-
resolved "https://registry.yarnpkg.com/slate-dom/-/slate-dom-0.118.1.tgz#162f79e9d1c02270f8cdc0c5991492d9a5ba60bb"
23885-
integrity sha512-D6J0DF9qdJrXnRDVhYZfHzzpVxzqKRKFfS0Wcin2q0UC+OnQZ0lbCGJobatVbisOlbSe7dYFHBp9OZ6v1lEcbQ==
23886-
dependencies:
23887-
"@juggle/resize-observer" "^3.4.0"
23888-
direction "^1.0.4"
23889-
is-hotkey "^0.2.0"
23890-
is-plain-object "^5.0.0"
23891-
lodash "^4.17.21"
23892-
scroll-into-view-if-needed "^3.1.0"
23893-
tiny-invariant "1.3.1"
23894-
2389523882
2389623883
version "0.100.0"
2389723884
resolved "https://registry.yarnpkg.com/slate-history/-/slate-history-0.100.0.tgz#a8549af61182a18db2dfedff6ebab7452c841666"
@@ -23906,24 +23893,28 @@ [email protected]:
2390623893
dependencies:
2390723894
is-plain-object "^5.0.0"
2390823895

23909-
slate-react@0.118.2:
23910-
version "0.118.2"
23911-
resolved "https://registry.yarnpkg.com/slate-react/-/slate-react-0.118.2.tgz#5e4ca05c63895a6a39d566dc750bd38d9bf97213"
23912-
integrity sha512-D7eQVZGgiqV36mooozu8sNWuCkzJqcHQWERQn9FxqmugnbEOKaPBj5OX1x5WGAVexfrxAT5dTAHUaRb0lGqFDw==
23896+
slate-react@0.102.0:
23897+
version "0.102.0"
23898+
resolved "https://registry.yarnpkg.com/slate-react/-/slate-react-0.102.0.tgz#8f4539055f336019abbbe8b52acc23ff02c9601b"
23899+
integrity sha512-SAcFsK5qaOxXjm0hr/t2pvIxfRv6HJGzmWkG58TdH4LdJCsgKS1n6hQOakHPlRVCwPgwvngB6R+t3pPjv8MqwA==
2391323900
dependencies:
2391423901
"@juggle/resize-observer" "^3.4.0"
23902+
"@types/is-hotkey" "^0.1.8"
23903+
"@types/lodash" "^4.14.200"
2391523904
direction "^1.0.4"
2391623905
is-hotkey "^0.2.0"
23906+
is-plain-object "^5.0.0"
2391723907
lodash "^4.17.21"
2391823908
scroll-into-view-if-needed "^3.1.0"
2391923909
tiny-invariant "1.3.1"
2392023910

23921-
slate@0.118.1:
23922-
version "0.118.1"
23923-
resolved "https://registry.yarnpkg.com/slate/-/slate-0.118.1.tgz#8403a5604994d02b4fb2a9db4435ce034c42d774"
23924-
integrity sha512-6H1DNgnSwAFhq/pIgf+tLvjNzH912M5XrKKhP9Frmbds2zFXdSJ6L/uFNyVKxQIkPzGWPD0m+wdDfmEuGFH5Tg==
23911+
slate@0.94.1:
23912+
version "0.94.1"
23913+
resolved "https://registry.yarnpkg.com/slate/-/slate-0.94.1.tgz#13b0ba7d0a7eeb0ec89a87598e9111cbbd685696"
23914+
integrity sha512-GH/yizXr1ceBoZ9P9uebIaHe3dC/g6Plpf9nlUwnvoyf6V1UOYrRwkabtOCd3ZfIGxomY4P7lfgLr7FPH8/BKA==
2392523915
dependencies:
23926-
immer "^10.0.3"
23916+
immer "^9.0.6"
23917+
is-plain-object "^5.0.0"
2392723918
tiny-warning "^1.0.3"
2392823919

2392923920

0 commit comments

Comments
 (0)