@contentful/field-editor-rich-text 3.6.0 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/cjs/RichTextEditor.js +42 -53
  2. package/dist/cjs/SyncEditorValue.js +107 -0
  3. package/dist/cjs/helpers/callbacks.js +35 -0
  4. package/dist/cjs/helpers/toSlateValue.js +51 -0
  5. package/dist/cjs/internal/hooks.js +12 -2
  6. package/dist/cjs/internal/misc.js +0 -6
  7. package/dist/cjs/plugins/Table/onKeyDownTable.js +14 -0
  8. package/dist/cjs/plugins/Text/__tests__/createTextPlugin.test.js +5 -15
  9. package/dist/cjs/plugins/index.js +0 -5
  10. package/dist/cjs/plugins/shared/FetchingWrappedResourceCard.js +11 -9
  11. package/dist/cjs/plugins/shared/__tests__/FetchingWrappedResourceCard.test.js +146 -0
  12. package/dist/esm/RichTextEditor.js +37 -48
  13. package/dist/esm/SyncEditorValue.js +53 -0
  14. package/dist/esm/helpers/callbacks.js +20 -0
  15. package/dist/{cjs/helpers/sanitizeIncomingSlateDoc.js → esm/helpers/toSlateValue.js} +13 -10
  16. package/dist/esm/internal/hooks.js +9 -2
  17. package/dist/esm/internal/misc.js +0 -3
  18. package/dist/esm/plugins/Table/onKeyDownTable.js +15 -1
  19. package/dist/esm/plugins/Text/__tests__/createTextPlugin.test.js +5 -15
  20. package/dist/esm/plugins/index.js +0 -5
  21. package/dist/esm/plugins/shared/FetchingWrappedResourceCard.js +12 -10
  22. package/dist/esm/plugins/shared/__tests__/FetchingWrappedResourceCard.test.js +98 -0
  23. package/dist/types/ContentfulEditorProvider.d.ts +2 -3
  24. package/dist/types/RichTextEditor.d.ts +2 -2
  25. package/dist/types/SyncEditorValue.d.ts +13 -0
  26. package/dist/types/helpers/callbacks.d.ts +3 -0
  27. package/dist/types/helpers/toSlateValue.d.ts +7 -0
  28. package/dist/types/internal/hooks.d.ts +4 -2
  29. package/dist/types/internal/misc.d.ts +2 -2
  30. package/dist/types/plugins/shared/__tests__/FetchingWrappedResourceCard.test.d.ts +1 -0
  31. package/dist/types/test-utils/createEditor.d.ts +2 -0
  32. package/package.json +14 -14
  33. package/dist/cjs/prepareDocument.js +0 -86
  34. package/dist/cjs/useOnValueChanged.js +0 -58
  35. package/dist/esm/helpers/sanitizeIncomingSlateDoc.js +0 -23
  36. package/dist/esm/prepareDocument.js +0 -57
  37. package/dist/esm/useOnValueChanged.js +0 -43
  38. package/dist/types/helpers/sanitizeIncomingSlateDoc.d.ts +0 -6
  39. package/dist/types/prepareDocument.d.ts +0 -19
  40. package/dist/types/useOnValueChanged.d.ts +0 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-rich-text",
3
- "version": "3.6.0",
3
+ "version": "3.7.0",
4
4
  "source": "./src/index.tsx",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -44,22 +44,22 @@
44
44
  "@contentful/f36-icons": "^4.1.1",
45
45
  "@contentful/f36-tokens": "^4.0.0",
46
46
  "@contentful/f36-utils": "^4.19.0",
47
- "@contentful/field-editor-reference": "^5.11.0",
47
+ "@contentful/field-editor-reference": "^5.12.0",
48
48
  "@contentful/field-editor-shared": "^1.3.0",
49
49
  "@contentful/rich-text-plain-text-renderer": "^16.0.4",
50
50
  "@contentful/rich-text-types": "16.1.0",
51
51
  "@popperjs/core": "^2.11.5",
52
- "@udecode/plate-basic-marks": "16.8.0",
53
- "@udecode/plate-break": "16.8.0",
54
- "@udecode/plate-core": "16.8.0",
55
- "@udecode/plate-list": "16.8.0",
56
- "@udecode/plate-paragraph": "16.8.0",
57
- "@udecode/plate-reset-node": "16.8.0",
58
- "@udecode/plate-select": "16.8.0",
59
- "@udecode/plate-serializer-docx": "16.8.0",
60
- "@udecode/plate-serializer-html": "16.8.0",
61
- "@udecode/plate-table": "16.8.0",
62
- "@udecode/plate-trailing-block": "16.8.0",
52
+ "@udecode/plate-basic-marks": "18.15.0",
53
+ "@udecode/plate-break": "18.15.0",
54
+ "@udecode/plate-core": "18.15.0",
55
+ "@udecode/plate-list": "18.15.0",
56
+ "@udecode/plate-paragraph": "18.15.0",
57
+ "@udecode/plate-reset-node": "18.15.0",
58
+ "@udecode/plate-select": "18.15.0",
59
+ "@udecode/plate-serializer-docx": "18.15.0",
60
+ "@udecode/plate-serializer-html": "18.15.0",
61
+ "@udecode/plate-table": "18.15.0",
62
+ "@udecode/plate-trailing-block": "18.15.0",
63
63
  "fast-deep-equal": "^3.1.3",
64
64
  "is-hotkey": "^0.2.0",
65
65
  "is-plain-obj": "^3.0.0",
@@ -80,5 +80,5 @@
80
80
  "@udecode/plate-test-utils": "^3.2.0",
81
81
  "react": ">=16.14.0"
82
82
  },
83
- "gitHead": "8d9a4c6609496a27db64f964bdc388a552eae59b"
83
+ "gitHead": "667723f7f4c7fa168ad233f26e353108851fa4e0"
84
84
  }
@@ -1,86 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- hasContent: function() {
13
- return hasContent;
14
- },
15
- setEditorContent: function() {
16
- return setEditorContent;
17
- },
18
- documentToEditorValue: function() {
19
- return documentToEditorValue;
20
- },
21
- normalizeEditorValue: function() {
22
- return normalizeEditorValue;
23
- }
24
- });
25
- const _contentfulslatejsadapter = require("@contentful/contentful-slatejs-adapter");
26
- const _richtexttypes = require("@contentful/rich-text-types");
27
- const _Schema = _interop_require_default(require("./constants/Schema"));
28
- const _sanitizeIncomingSlateDoc = require("./helpers/sanitizeIncomingSlateDoc");
29
- const _internal = require("./internal");
30
- const _queries = require("./internal/queries");
31
- const _transforms = require("./internal/transforms");
32
- function _interop_require_default(obj) {
33
- return obj && obj.__esModule ? obj : {
34
- default: obj
35
- };
36
- }
37
- const hasContent = (doc)=>{
38
- return (doc?.content || []).length > 0;
39
- };
40
- const setEditorContent = (editor, nodes)=>{
41
- (0, _internal.withoutNormalizing)(editor, ()=>{
42
- const children = [
43
- ...editor.children
44
- ];
45
- children.forEach((node)=>editor.apply({
46
- type: 'remove_node',
47
- path: [
48
- 0
49
- ],
50
- node
51
- }));
52
- if (nodes) {
53
- const nodesArray = (0, _queries.isNode)(nodes) ? [
54
- nodes
55
- ] : nodes;
56
- nodesArray.forEach((node, i)=>{
57
- editor.apply({
58
- type: 'insert_node',
59
- path: [
60
- i
61
- ],
62
- node
63
- });
64
- });
65
- }
66
- const point = (0, _queries.getEndPoint)(editor, []);
67
- if (point) {
68
- (0, _transforms.select)(editor, point);
69
- }
70
- });
71
- };
72
- const documentToEditorValue = (doc)=>{
73
- const slateDoc = (0, _contentfulslatejsadapter.toSlatejsDocument)({
74
- document: hasContent(doc) ? doc : _richtexttypes.EMPTY_DOCUMENT,
75
- schema: _Schema.default
76
- });
77
- return (0, _sanitizeIncomingSlateDoc.sanitizeIncomingSlateDoc)(slateDoc);
78
- };
79
- const normalizeEditorValue = (value, options)=>{
80
- const editor = (0, _internal.createPlateEditor)(options);
81
- editor.children = value;
82
- (0, _transforms.normalize)(editor, {
83
- force: true
84
- });
85
- return editor.children;
86
- };
@@ -1,58 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "useOnValueChanged", {
6
- enumerable: true,
7
- get: function() {
8
- return useOnValueChanged;
9
- }
10
- });
11
- const _react = require("react");
12
- const _contentfulslatejsadapter = require("@contentful/contentful-slatejs-adapter");
13
- const _platecore = require("@udecode/plate-core");
14
- const _debounce = _interop_require_default(require("lodash/debounce"));
15
- const _Schema = _interop_require_default(require("./constants/Schema"));
16
- const _removeInternalMarks = require("./helpers/removeInternalMarks");
17
- function _interop_require_default(obj) {
18
- return obj && obj.__esModule ? obj : {
19
- default: obj
20
- };
21
- }
22
- const isRelevantOperation = (op)=>{
23
- if (op.type === 'set_selection') {
24
- return false;
25
- }
26
- return true;
27
- };
28
- const useOnValueChanged = ({ editorId , handler , skip , onSkip })=>{
29
- const onChange = (0, _react.useMemo)(()=>(0, _debounce.default)((document)=>{
30
- const contentfulDocument = (0, _contentfulslatejsadapter.toContentfulDocument)({
31
- document: document,
32
- schema: _Schema.default
33
- });
34
- const cleanedDocument = (0, _removeInternalMarks.removeInternalMarks)(contentfulDocument);
35
- handler?.(cleanedDocument);
36
- }, 500), [
37
- handler
38
- ]);
39
- return (0, _react.useCallback)((value)=>{
40
- const editor = (0, _platecore.getPlateSelectors)(editorId).editor();
41
- if (!editor) {
42
- throw new Error('Editor change callback called but editor not defined. Editor id: ' + editorId);
43
- }
44
- const operations = editor?.operations.filter(isRelevantOperation);
45
- if (operations.length > 0) {
46
- if (skip) {
47
- onSkip?.();
48
- return;
49
- }
50
- onChange(value);
51
- }
52
- }, [
53
- editorId,
54
- onChange,
55
- skip,
56
- onSkip
57
- ]);
58
- };
@@ -1,23 +0,0 @@
1
- const isTextElement = (node)=>'text' in node;
2
- export function sanitizeIncomingSlateDoc(nodes = []) {
3
- return nodes.map((node)=>{
4
- if (isTextElement(node)) {
5
- return node;
6
- }
7
- if (node.children?.length === 0) {
8
- return {
9
- ...node,
10
- children: [
11
- {
12
- text: '',
13
- data: {}
14
- }
15
- ]
16
- };
17
- }
18
- return {
19
- ...node,
20
- children: sanitizeIncomingSlateDoc(node?.children)
21
- };
22
- });
23
- }
@@ -1,57 +0,0 @@
1
- import { toSlatejsDocument } from '@contentful/contentful-slatejs-adapter';
2
- import { EMPTY_DOCUMENT } from '@contentful/rich-text-types';
3
- import schema from './constants/Schema';
4
- import { sanitizeIncomingSlateDoc } from './helpers/sanitizeIncomingSlateDoc';
5
- import { createPlateEditor, withoutNormalizing } from './internal';
6
- import { getEndPoint, isNode } from './internal/queries';
7
- import { normalize, select } from './internal/transforms';
8
- export const hasContent = (doc)=>{
9
- return (doc?.content || []).length > 0;
10
- };
11
- export const setEditorContent = (editor, nodes)=>{
12
- withoutNormalizing(editor, ()=>{
13
- const children = [
14
- ...editor.children
15
- ];
16
- children.forEach((node)=>editor.apply({
17
- type: 'remove_node',
18
- path: [
19
- 0
20
- ],
21
- node
22
- }));
23
- if (nodes) {
24
- const nodesArray = isNode(nodes) ? [
25
- nodes
26
- ] : nodes;
27
- nodesArray.forEach((node, i)=>{
28
- editor.apply({
29
- type: 'insert_node',
30
- path: [
31
- i
32
- ],
33
- node
34
- });
35
- });
36
- }
37
- const point = getEndPoint(editor, []);
38
- if (point) {
39
- select(editor, point);
40
- }
41
- });
42
- };
43
- export const documentToEditorValue = (doc)=>{
44
- const slateDoc = toSlatejsDocument({
45
- document: hasContent(doc) ? doc : EMPTY_DOCUMENT,
46
- schema
47
- });
48
- return sanitizeIncomingSlateDoc(slateDoc);
49
- };
50
- export const normalizeEditorValue = (value, options)=>{
51
- const editor = createPlateEditor(options);
52
- editor.children = value;
53
- normalize(editor, {
54
- force: true
55
- });
56
- return editor.children;
57
- };
@@ -1,43 +0,0 @@
1
- import { useCallback, useMemo } from 'react';
2
- import { toContentfulDocument } from '@contentful/contentful-slatejs-adapter';
3
- import { getPlateSelectors } from '@udecode/plate-core';
4
- import debounce from 'lodash/debounce';
5
- import schema from './constants/Schema';
6
- import { removeInternalMarks } from './helpers/removeInternalMarks';
7
- const isRelevantOperation = (op)=>{
8
- if (op.type === 'set_selection') {
9
- return false;
10
- }
11
- return true;
12
- };
13
- export const useOnValueChanged = ({ editorId , handler , skip , onSkip })=>{
14
- const onChange = useMemo(()=>debounce((document)=>{
15
- const contentfulDocument = toContentfulDocument({
16
- document: document,
17
- schema
18
- });
19
- const cleanedDocument = removeInternalMarks(contentfulDocument);
20
- handler?.(cleanedDocument);
21
- }, 500), [
22
- handler
23
- ]);
24
- return useCallback((value)=>{
25
- const editor = getPlateSelectors(editorId).editor();
26
- if (!editor) {
27
- throw new Error('Editor change callback called but editor not defined. Editor id: ' + editorId);
28
- }
29
- const operations = editor?.operations.filter(isRelevantOperation);
30
- if (operations.length > 0) {
31
- if (skip) {
32
- onSkip?.();
33
- return;
34
- }
35
- onChange(value);
36
- }
37
- }, [
38
- editorId,
39
- onChange,
40
- skip,
41
- onSkip
42
- ]);
43
- };
@@ -1,6 +0,0 @@
1
- import { Node } from '../internal/types';
2
- /**
3
- * Ensures all nodes have a child leaf text element. This should be handled by
4
- * Slate but its behavior has proven to be buggy and unpredictable.
5
- */
6
- export declare function sanitizeIncomingSlateDoc(nodes?: Node[]): Node[];
@@ -1,19 +0,0 @@
1
- import { Document } from '@contentful/rich-text-types';
2
- import { CreatePlateEditorOptions } from './internal';
3
- import { Node, PlateEditor, Value } from './internal/types';
4
- /**
5
- * For legacy reasons, a document may not have any content at all
6
- * e.g:
7
- *
8
- * {nodeType: document, data: {}, content: []}
9
- *
10
- * Rendering such document will break the Slate editor
11
- */
12
- export declare const hasContent: (doc?: Document) => boolean;
13
- export declare const setEditorContent: (editor: PlateEditor, nodes?: Node[]) => void;
14
- /**
15
- * Converts a Contentful rich text document to the corresponding slate editor
16
- * value
17
- */
18
- export declare const documentToEditorValue: (doc?: Document) => Node[];
19
- export declare const normalizeEditorValue: (value: Value, options?: Omit<CreatePlateEditorOptions, 'id' | 'editor'>) => Value;
@@ -1,8 +0,0 @@
1
- import { Document } from '@contentful/rich-text-types';
2
- export type OnValueChangedProps = {
3
- editorId: string;
4
- handler?: (value: Document) => unknown;
5
- skip?: boolean;
6
- onSkip?: VoidFunction;
7
- };
8
- export declare const useOnValueChanged: ({ editorId, handler, skip, onSkip }: OnValueChangedProps) => (value: unknown) => void;