@contentful/field-editor-rich-text 3.5.0 → 3.6.1

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 (88) hide show
  1. package/dist/cjs/RichTextEditor.js +42 -53
  2. package/dist/cjs/SyncEditorValue.js +107 -0
  3. package/dist/cjs/Toolbar/components/EmbedEntityWidget.js +8 -3
  4. package/dist/cjs/helpers/__tests__/removeInternalMarks.test.js +37 -21
  5. package/dist/cjs/helpers/callbacks.js +35 -0
  6. package/dist/cjs/helpers/getAllowedResourcesForNodeType.js +25 -0
  7. package/dist/cjs/helpers/newResourceEntitySelectorConfigFromRichTextField.js +21 -0
  8. package/dist/cjs/helpers/toSlateValue.js +51 -0
  9. package/dist/cjs/internal/hooks.js +12 -2
  10. package/dist/cjs/internal/misc.js +0 -6
  11. package/dist/cjs/plugins/DragAndDrop/index.js +1 -0
  12. package/dist/cjs/plugins/EmbeddedEntityBlock/LinkedEntityBlock.js +27 -44
  13. package/dist/cjs/plugins/EmbeddedEntityBlock/index.js +3 -35
  14. package/dist/cjs/plugins/EmbeddedEntityInline/index.js +3 -2
  15. package/dist/cjs/plugins/EmbeddedResourceBlock/LinkedResourceBlock.js +54 -0
  16. package/dist/cjs/plugins/EmbeddedResourceBlock/index.js +55 -0
  17. package/dist/cjs/plugins/Hyperlink/components/EntityHyperlink.js +2 -1
  18. package/dist/cjs/plugins/Hyperlink/createHyperlinkPlugin.js +2 -3
  19. package/dist/cjs/plugins/Table/onKeyDownTable.js +14 -0
  20. package/dist/cjs/plugins/Text/__tests__/createTextPlugin.test.js +5 -15
  21. package/dist/cjs/plugins/Text/createTextPlugin.js +1 -0
  22. package/dist/cjs/plugins/index.js +2 -5
  23. package/dist/cjs/plugins/links-tracking.js +8 -17
  24. package/dist/cjs/plugins/{EmbeddedEntityBlock/ToolbarIcon.js → shared/EmbeddedBlockToolbarIcon.js} +15 -7
  25. package/dist/cjs/plugins/shared/EmbeddedBlockUtil.js +170 -0
  26. package/dist/cjs/plugins/shared/FetchingWrappedResourceCard.js +110 -0
  27. package/dist/cjs/plugins/shared/LinkedBlockWrapper.js +45 -0
  28. package/dist/esm/RichTextEditor.js +37 -48
  29. package/dist/esm/SyncEditorValue.js +53 -0
  30. package/dist/esm/Toolbar/components/EmbedEntityWidget.js +8 -3
  31. package/dist/esm/helpers/__tests__/removeInternalMarks.test.js +37 -21
  32. package/dist/esm/helpers/callbacks.js +20 -0
  33. package/dist/esm/helpers/getAllowedResourcesForNodeType.js +10 -0
  34. package/dist/esm/helpers/newResourceEntitySelectorConfigFromRichTextField.js +6 -0
  35. package/dist/{cjs/helpers/sanitizeIncomingSlateDoc.js → esm/helpers/toSlateValue.js} +13 -10
  36. package/dist/esm/internal/hooks.js +9 -2
  37. package/dist/esm/internal/misc.js +0 -3
  38. package/dist/esm/plugins/DragAndDrop/index.js +1 -0
  39. package/dist/esm/plugins/EmbeddedEntityBlock/LinkedEntityBlock.js +27 -44
  40. package/dist/esm/plugins/EmbeddedEntityBlock/index.js +3 -27
  41. package/dist/esm/plugins/EmbeddedEntityInline/index.js +4 -3
  42. package/dist/esm/plugins/EmbeddedResourceBlock/LinkedResourceBlock.js +39 -0
  43. package/dist/esm/plugins/EmbeddedResourceBlock/index.js +45 -0
  44. package/dist/esm/plugins/Hyperlink/components/EntityHyperlink.js +2 -1
  45. package/dist/esm/plugins/Hyperlink/createHyperlinkPlugin.js +2 -3
  46. package/dist/esm/plugins/Table/onKeyDownTable.js +15 -1
  47. package/dist/esm/plugins/Text/__tests__/createTextPlugin.test.js +5 -15
  48. package/dist/esm/plugins/Text/createTextPlugin.js +1 -0
  49. package/dist/esm/plugins/index.js +2 -5
  50. package/dist/esm/plugins/links-tracking.js +6 -10
  51. package/dist/esm/plugins/{EmbeddedEntityBlock/ToolbarIcon.js → shared/EmbeddedBlockToolbarIcon.js} +14 -6
  52. package/dist/esm/plugins/shared/EmbeddedBlockUtil.js +144 -0
  53. package/dist/esm/plugins/shared/FetchingWrappedResourceCard.js +56 -0
  54. package/dist/esm/plugins/shared/LinkedBlockWrapper.js +30 -0
  55. package/dist/types/ContentfulEditorProvider.d.ts +2 -3
  56. package/dist/types/RichTextEditor.d.ts +2 -2
  57. package/dist/types/SyncEditorValue.d.ts +13 -0
  58. package/dist/types/dialogs/HypelinkDialog/HyperlinkDialog.d.ts +3 -3
  59. package/dist/types/helpers/callbacks.d.ts +3 -0
  60. package/dist/types/helpers/getAllowedResourcesForNodeType.d.ts +25 -0
  61. package/dist/types/helpers/newResourceEntitySelectorConfigFromRichTextField.d.ts +16 -0
  62. package/dist/types/helpers/toSlateValue.d.ts +7 -0
  63. package/dist/types/internal/hooks.d.ts +4 -2
  64. package/dist/types/internal/misc.d.ts +2 -2
  65. package/dist/types/plugins/EmbeddedEntityBlock/LinkedEntityBlock.d.ts +0 -1
  66. package/dist/types/plugins/EmbeddedEntityBlock/index.d.ts +1 -2
  67. package/dist/types/plugins/EmbeddedResourceBlock/LinkedResourceBlock.d.ts +18 -0
  68. package/dist/types/plugins/EmbeddedResourceBlock/index.d.ts +3 -0
  69. package/dist/types/plugins/links-tracking.d.ts +3 -3
  70. package/dist/types/plugins/shared/EmbeddedBlockToolbarIcon.d.ts +11 -0
  71. package/dist/types/plugins/shared/EmbeddedBlockUtil.d.ts +8 -0
  72. package/dist/types/plugins/shared/FetchingWrappedResourceCard.d.ts +14 -0
  73. package/dist/types/plugins/shared/LinkedBlockWrapper.d.ts +25 -0
  74. package/dist/types/test-utils/createEditor.d.ts +2 -0
  75. package/dist/types/test-utils/jsx.d.ts +1 -1
  76. package/package.json +18 -18
  77. package/dist/cjs/plugins/EmbeddedEntityBlock/Util.js +0 -108
  78. package/dist/cjs/prepareDocument.js +0 -86
  79. package/dist/cjs/useOnValueChanged.js +0 -58
  80. package/dist/esm/helpers/sanitizeIncomingSlateDoc.js +0 -23
  81. package/dist/esm/plugins/EmbeddedEntityBlock/Util.js +0 -85
  82. package/dist/esm/prepareDocument.js +0 -57
  83. package/dist/esm/useOnValueChanged.js +0 -43
  84. package/dist/types/helpers/sanitizeIncomingSlateDoc.d.ts +0 -6
  85. package/dist/types/plugins/EmbeddedEntityBlock/ToolbarIcon.d.ts +0 -11
  86. package/dist/types/plugins/EmbeddedEntityBlock/Util.d.ts +0 -4
  87. package/dist/types/prepareDocument.d.ts +0 -19
  88. package/dist/types/useOnValueChanged.d.ts +0 -8
@@ -1,85 +0,0 @@
1
- import { BLOCKS, TEXT_CONTAINERS } from '@contentful/rich-text-types';
2
- import { focus, insertEmptyParagraph, moveToTheNextChar } from '../../helpers/editor';
3
- import newEntitySelectorConfigFromRichTextField from '../../helpers/newEntitySelectorConfigFromRichTextField';
4
- import { watchCurrentSlide } from '../../helpers/sdkNavigatorSlideIn';
5
- import { getText, getAboveNode, getLastNodeByLevel } from '../../internal/queries';
6
- import { insertNodes, select, setNodes } from '../../internal/transforms';
7
- export async function selectEntityAndInsert(nodeType, sdk, editor, logAction) {
8
- logAction('openCreateEmbedDialog', {
9
- nodeType
10
- });
11
- const { field , dialogs } = sdk;
12
- const baseConfig = newEntitySelectorConfigFromRichTextField(field, nodeType);
13
- const selectEntity = baseConfig.entityType === 'Asset' ? dialogs.selectSingleAsset : dialogs.selectSingleEntry;
14
- const config = {
15
- ...baseConfig,
16
- withCreate: true
17
- };
18
- const { selection } = editor;
19
- const rteSlide = watchCurrentSlide(sdk.navigator);
20
- const entity = await selectEntity(config);
21
- if (!entity) {
22
- logAction('cancelCreateEmbedDialog', {
23
- nodeType
24
- });
25
- } else {
26
- select(editor, selection);
27
- insertBlock(editor, nodeType, entity);
28
- ensureFollowingParagraph(editor);
29
- logAction('insert', {
30
- nodeType
31
- });
32
- }
33
- rteSlide.onActive(()=>{
34
- rteSlide.unwatch();
35
- focus(editor);
36
- });
37
- }
38
- function ensureFollowingParagraph(editor) {
39
- const entityBlock = getAboveNode(editor, {
40
- match: {
41
- type: [
42
- BLOCKS.EMBEDDED_ASSET,
43
- BLOCKS.EMBEDDED_ENTRY
44
- ]
45
- }
46
- });
47
- if (!entityBlock) {
48
- return;
49
- }
50
- const level = entityBlock[1].length - 1;
51
- const lastNode = getLastNodeByLevel(editor, level);
52
- const isTextContainer = TEXT_CONTAINERS.includes(lastNode?.[0].type ?? '');
53
- if (level !== 0 && !isTextContainer) {
54
- insertEmptyParagraph(editor);
55
- }
56
- moveToTheNextChar(editor);
57
- }
58
- const createNode = (nodeType, entity)=>({
59
- type: nodeType,
60
- data: {
61
- target: {
62
- sys: {
63
- id: entity.sys.id,
64
- type: 'Link',
65
- linkType: entity.sys.type
66
- }
67
- }
68
- },
69
- children: [
70
- {
71
- text: ''
72
- }
73
- ],
74
- isVoid: true
75
- });
76
- export function insertBlock(editor, nodeType, entity) {
77
- if (!editor?.selection) return;
78
- const linkedEntityBlock = createNode(nodeType, entity);
79
- const hasText = editor.selection && !!getText(editor, editor.selection.focus.path);
80
- if (hasText) {
81
- insertNodes(editor, linkedEntityBlock);
82
- } else {
83
- setNodes(editor, linkedEntityBlock);
84
- }
85
- }
@@ -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,
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,11 +0,0 @@
1
- import * as React from 'react';
2
- export declare const styles: {
3
- icon: string;
4
- };
5
- interface EmbeddedEntityBlockToolbarIconProps {
6
- isDisabled: boolean;
7
- nodeType: string;
8
- onClose: () => void;
9
- }
10
- export declare function EmbeddedEntityBlockToolbarIcon({ isDisabled, nodeType, onClose, }: EmbeddedEntityBlockToolbarIconProps): React.JSX.Element;
11
- export {};
@@ -1,4 +0,0 @@
1
- import { PlateEditor } from '../../internal/types';
2
- import { TrackingPluginActions } from '../../plugins/Tracking';
3
- export declare function selectEntityAndInsert(nodeType: any, sdk: any, editor: any, logAction: TrackingPluginActions['onToolbarAction'] | TrackingPluginActions['onShortcutAction']): Promise<void>;
4
- export declare function insertBlock(editor: PlateEditor, nodeType: string, entity: any): void;
@@ -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;