@contentful/field-editor-rich-text 3.6.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 (35) 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/esm/RichTextEditor.js +37 -48
  11. package/dist/esm/SyncEditorValue.js +53 -0
  12. package/dist/esm/helpers/callbacks.js +20 -0
  13. package/dist/{cjs/helpers/sanitizeIncomingSlateDoc.js → esm/helpers/toSlateValue.js} +13 -10
  14. package/dist/esm/internal/hooks.js +9 -2
  15. package/dist/esm/internal/misc.js +0 -3
  16. package/dist/esm/plugins/Table/onKeyDownTable.js +15 -1
  17. package/dist/esm/plugins/Text/__tests__/createTextPlugin.test.js +5 -15
  18. package/dist/esm/plugins/index.js +0 -5
  19. package/dist/types/ContentfulEditorProvider.d.ts +2 -3
  20. package/dist/types/RichTextEditor.d.ts +2 -2
  21. package/dist/types/SyncEditorValue.d.ts +13 -0
  22. package/dist/types/helpers/callbacks.d.ts +3 -0
  23. package/dist/types/helpers/toSlateValue.d.ts +7 -0
  24. package/dist/types/internal/hooks.d.ts +4 -2
  25. package/dist/types/internal/misc.d.ts +2 -2
  26. package/dist/types/test-utils/createEditor.d.ts +2 -0
  27. package/package.json +13 -13
  28. package/dist/cjs/prepareDocument.js +0 -86
  29. package/dist/cjs/useOnValueChanged.js +0 -58
  30. package/dist/esm/helpers/sanitizeIncomingSlateDoc.js +0 -23
  31. package/dist/esm/prepareDocument.js +0 -57
  32. package/dist/esm/useOnValueChanged.js +0 -43
  33. package/dist/types/helpers/sanitizeIncomingSlateDoc.d.ts +0 -6
  34. package/dist/types/prepareDocument.d.ts +0 -19
  35. package/dist/types/useOnValueChanged.d.ts +0 -8
@@ -0,0 +1,20 @@
1
+ import { toContentfulDocument } from '@contentful/contentful-slatejs-adapter';
2
+ import equal from 'fast-deep-equal';
3
+ import debounce from 'lodash/debounce';
4
+ import schema from '../constants/Schema';
5
+ import { removeInternalMarks } from './removeInternalMarks';
6
+ export const createOnChangeCallback = (handler)=>{
7
+ let cache = null;
8
+ return debounce((document)=>{
9
+ if (equal(document, cache)) {
10
+ return;
11
+ }
12
+ cache = document;
13
+ const doc = removeInternalMarks(toContentfulDocument({
14
+ document: document,
15
+ schema: schema
16
+ }));
17
+ const cleanedDocument = removeInternalMarks(doc);
18
+ handler?.(cleanedDocument);
19
+ }, 500);
20
+ };
@@ -1,13 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "sanitizeIncomingSlateDoc", {
6
- enumerable: true,
7
- get: function() {
8
- return sanitizeIncomingSlateDoc;
9
- }
10
- });
1
+ import { toSlatejsDocument } from '@contentful/contentful-slatejs-adapter';
2
+ import { EMPTY_DOCUMENT } from '@contentful/rich-text-types';
3
+ import schema from '../constants/Schema';
11
4
  const isTextElement = (node)=>'text' in node;
12
5
  function sanitizeIncomingSlateDoc(nodes = []) {
13
6
  return nodes.map((node)=>{
@@ -31,3 +24,13 @@ function sanitizeIncomingSlateDoc(nodes = []) {
31
24
  };
32
25
  });
33
26
  }
27
+ export const toSlateValue = (doc)=>{
28
+ const hasContent = (doc)=>{
29
+ return (doc?.content || []).length > 0;
30
+ };
31
+ const slateDoc = toSlatejsDocument({
32
+ document: doc && hasContent(doc) ? doc : EMPTY_DOCUMENT,
33
+ schema
34
+ });
35
+ return sanitizeIncomingSlateDoc(slateDoc);
36
+ };
@@ -1,5 +1,12 @@
1
1
  import * as p from '@udecode/plate-core';
2
2
  import * as sr from 'slate-react';
3
3
  export const useReadOnly = sr.useReadOnly;
4
- export const usePlateEditorRef = p.usePlateEditorRef;
5
- export const usePlateEditorState = p.usePlateEditorState;
4
+ export const usePlateEditorRef = (id)=>{
5
+ return p.usePlateEditorRef(id);
6
+ };
7
+ export const usePlateEditorState = (id)=>{
8
+ return p.usePlateEditorState(id);
9
+ };
10
+ export const usePlateSelectors = (id)=>{
11
+ return p.usePlateSelectors(id);
12
+ };
@@ -23,6 +23,3 @@ export const fromDOMPoint = (editor, domPoint, opts = {
23
23
  export const mockPlugin = (plugin)=>{
24
24
  return p.mockPlugin(plugin);
25
25
  };
26
- export const getPlateSelectors = (id)=>{
27
- return p.getPlateSelectors(id);
28
- };
@@ -2,7 +2,7 @@ import { BLOCKS } from '@contentful/rich-text-types';
2
2
  import { getTableEntries, onKeyDownTable as defaultKeyDownTable } from '@udecode/plate-table';
3
3
  import { insertEmptyParagraph } from '../../helpers/editor';
4
4
  import { blurEditor } from '../../internal/misc';
5
- import { getAboveNode, isLastChildPath } from '../../internal/queries';
5
+ import { getAboveNode, isLastChildPath, getText, isFirstChild } from '../../internal/queries';
6
6
  import { addRowBelow } from './actions';
7
7
  export const onKeyDownTable = (editor, plugin)=>{
8
8
  const defaultHandler = defaultKeyDownTable(editor, plugin);
@@ -30,6 +30,20 @@ export const onKeyDownTable = (editor, plugin)=>{
30
30
  return;
31
31
  }
32
32
  }
33
+ if (event.key === 'Backspace') {
34
+ const entry = getTableEntries(editor, {});
35
+ if (entry) {
36
+ const { table , row , cell } = entry;
37
+ const cellText = getText(editor, cell[1]);
38
+ const isFirstCell = isFirstChild(row[1]);
39
+ const isFirstRow = isFirstChild(table[1]);
40
+ if (isFirstCell && isFirstRow && !cellText) {
41
+ event.preventDefault();
42
+ event.stopPropagation();
43
+ return;
44
+ }
45
+ }
46
+ }
33
47
  if (event.key === 'Tab' && !event.shiftKey) {
34
48
  event.preventDefault();
35
49
  const entry = getTableEntries(editor, {});
@@ -7,19 +7,9 @@ describe('delete backward', ()=>{
7
7
  expected: jsx("hul", null, jsx("hli", null, jsx("hp", null, "p", jsx("cursor", null))))
8
8
  },
9
9
  {
10
- title: 'deletes the empty paragraph at the beginning of the RTE followed by another paragraph',
10
+ title: 'does not delete the very first paragraph',
11
11
  input: jsx("fragment", null, jsx("hp", null, jsx("cursor", null)), jsx("hp", null, "text")),
12
- expected: jsx("hp", null, jsx("cursor", null), "text")
13
- },
14
- {
15
- title: 'deletes the empty paragraph at the beginning of the RTE followed by li',
16
- input: jsx("fragment", null, jsx("hp", null, jsx("cursor", null)), jsx("hul", null, jsx("hli", null, jsx("hp", null, "p1")))),
17
- expected: jsx("hul", null, jsx("hli", null, jsx("hp", null, jsx("cursor", null), "p1")))
18
- },
19
- {
20
- title: 'deletes the empty paragraph at the beginning of the RTE followed by a blockquote',
21
- input: jsx("fragment", null, jsx("hp", null, jsx("cursor", null)), jsx("hquote", null, jsx("hp", null, "p1"))),
22
- expected: jsx("hquote", null, jsx("hp", null, jsx("cursor", null), "p1"))
12
+ expected: jsx("fragment", null, jsx("hp", null, jsx("cursor", null)), jsx("hp", null, "text"))
23
13
  }
24
14
  ];
25
15
  const render = (children)=>jsx("editor", null, children, jsx("hp", null, jsx("htext", null)));
@@ -44,17 +34,17 @@ describe('delete forward', ()=>{
44
34
  expected: jsx("hul", null, jsx("hli", null, jsx("hp", null, jsx("cursor", null), "1")))
45
35
  },
46
36
  {
47
- title: 'deletes the empty paragraph at the beginning of the RTE followed by another paragraph',
37
+ title: 'deletes the first paragraph when followed by another paragraph',
48
38
  input: jsx("fragment", null, jsx("hp", null, jsx("cursor", null)), jsx("hp", null, "text")),
49
39
  expected: jsx("hp", null, jsx("cursor", null), "text")
50
40
  },
51
41
  {
52
- title: 'deletes the empty paragraph at the beginning of the RTE followed by li',
42
+ title: 'deletes the first paragraph when followed by li',
53
43
  input: jsx("fragment", null, jsx("hp", null, jsx("cursor", null)), jsx("hul", null, jsx("hli", null, jsx("hp", null, "p1")))),
54
44
  expected: jsx("hul", null, jsx("hli", null, jsx("hp", null, jsx("cursor", null), "p1")))
55
45
  },
56
46
  {
57
- title: 'deletes the empty paragraph at the beginning of the RTE followed by a blockquote',
47
+ title: 'deletes the first paragraph when followed by a blockquote',
58
48
  input: jsx("fragment", null, jsx("hp", null, jsx("cursor", null)), jsx("hquote", null, jsx("hp", null, "p1"))),
59
49
  expected: jsx("hquote", null, jsx("hp", null, jsx("cursor", null), "p1"))
60
50
  }
@@ -1,4 +1,3 @@
1
- import { createDeserializeAstPlugin, createDeserializeHtmlPlugin } from '@udecode/plate-core';
2
1
  import { createDeserializeDocxPlugin } from '@udecode/plate-serializer-docx';
3
2
  import { createSoftBreakPlugin, createExitBreakPlugin, createResetNodePlugin } from './Break';
4
3
  import { createCommandPalettePlugin } from './CommandPalette';
@@ -23,8 +22,6 @@ import { createTrackingPlugin } from './Tracking';
23
22
  import { createTrailingParagraphPlugin } from './TrailingParagraph';
24
23
  import { createVoidsPlugin } from './Voids';
25
24
  export const getPlugins = (sdk, onAction, restrictedMarks)=>[
26
- createDeserializeHtmlPlugin(),
27
- createDeserializeAstPlugin(),
28
25
  createDeserializeDocxPlugin(),
29
26
  createTrackingPlugin(onAction),
30
27
  createDragAndDropPlugin(),
@@ -54,7 +51,5 @@ export const getPlugins = (sdk, onAction, restrictedMarks)=>[
54
51
  createNormalizerPlugin()
55
52
  ];
56
53
  export const disableCorePlugins = {
57
- deserializeAst: true,
58
- deserializeHtml: true,
59
54
  eventEditor: true
60
55
  };
@@ -1,9 +1,8 @@
1
1
  /// <reference types="react" />
2
2
  import { FieldExtensionSDK } from '@contentful/app-sdk';
3
- import { PlateEditor } from './internal/types';
4
3
  export declare function getContentfulEditorId(sdk: FieldExtensionSDK): string;
5
4
  export declare const editorContext: import("react").Context<string>;
6
5
  export declare const ContentfulEditorIdProvider: import("react").Provider<string>;
7
6
  export declare function useContentfulEditorId(id?: string): string;
8
- export declare function useContentfulEditor(id?: string): PlateEditor | null;
9
- export declare function useContentfulEditorRef(id?: string): PlateEditor | null;
7
+ export declare function useContentfulEditor(id?: string): import("./internal").PlateEditor;
8
+ export declare function useContentfulEditorRef(id?: string): import("./internal").PlateEditor;
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import * as React from 'react';
2
2
  import { FieldExtensionSDK } from '@contentful/app-sdk';
3
3
  import * as Contentful from '@contentful/rich-text-types';
4
4
  import { RichTextTrackingActionHandler } from './plugins/Tracking';
@@ -6,7 +6,7 @@ type ConnectedProps = {
6
6
  sdk: FieldExtensionSDK;
7
7
  onAction?: RichTextTrackingActionHandler;
8
8
  minHeight?: string | number;
9
- value?: object;
9
+ value?: Contentful.Document;
10
10
  isDisabled?: boolean;
11
11
  onChange?: (doc: Contentful.Document) => unknown;
12
12
  isToolbarHidden?: boolean;
@@ -0,0 +1,13 @@
1
+ import { Value } from './internal/types';
2
+ export type SyncEditorStateProps = {
3
+ incomingValue?: Value;
4
+ };
5
+ /**
6
+ * A void component that's responsible for keeping the editor
7
+ * state in sync with incoming changes (aka. external updates)
8
+ *
9
+ * This component is a hack to workaround the limitation of Plate v17+
10
+ * where we can no longer access the editor instance outside the Plate
11
+ * provider.
12
+ */
13
+ export declare const SyncEditorValue: ({ incomingValue }: SyncEditorStateProps) => null;
@@ -0,0 +1,3 @@
1
+ /// <reference types="lodash" />
2
+ import { Document } from '@contentful/rich-text-types';
3
+ export declare const createOnChangeCallback: (handler?: ((value: Document) => void) | undefined) => import("lodash").DebouncedFunc<(document: unknown) => void>;
@@ -0,0 +1,7 @@
1
+ import { Document } from '@contentful/rich-text-types';
2
+ import { Element } from '../internal/types';
3
+ /**
4
+ * Converts a Contentful rich text document to the corresponding slate editor
5
+ * value
6
+ */
7
+ export declare const toSlateValue: (doc?: Document) => Element[];
@@ -1,4 +1,6 @@
1
1
  import * as p from '@udecode/plate-core';
2
+ import { PlateEditor, Value } from './types';
2
3
  export declare const useReadOnly: () => boolean;
3
- export declare const usePlateEditorRef: <V extends p.Value = p.Value, E extends p.PlateEditor<V> = p.PlateEditor<V>>(id?: string | undefined) => E | null;
4
- export declare const usePlateEditorState: <V extends p.Value = p.Value, E extends p.PlateEditor<V> = p.PlateEditor<V>>(id?: string | undefined) => E | null;
4
+ export declare const usePlateEditorRef: (id?: string) => PlateEditor;
5
+ export declare const usePlateEditorState: (id?: string) => PlateEditor;
6
+ export declare const usePlateSelectors: (id?: string) => p.GetRecord<p.PlateStoreState<Value, PlateEditor>>;
@@ -1,6 +1,5 @@
1
1
  /// <reference types="react" />
2
2
  import * as p from '@udecode/plate-core';
3
- import { StoreApiGet } from '@udecode/zustood';
4
3
  import * as s from 'slate';
5
4
  import type { Value, PlateEditor, Location, PlatePlugin } from './types';
6
5
  export type CreatePlateEditorOptions = Omit<p.CreatePlateEditorOptions<Value, PlateEditor>, 'plugins'> & {
@@ -23,6 +22,8 @@ export declare const createPlateEditor: (options?: CreatePlateEditorOptions) =>
23
22
  plugins: p.WithPlatePlugin<{}, Value, p.PlateEditor<Value>>[];
24
23
  pluginsByKey: Record<string, p.WithPlatePlugin<{}, Value, p.PlateEditor<Value>>>;
25
24
  prevSelection: s.BaseRange | null;
25
+ blockFactory: (node?: Partial<p.TElement> | undefined, path?: s.Path | undefined) => p.TElement;
26
+ childrenFactory: () => Value;
26
27
  currentKeyboardEvent: import("react").KeyboardEvent<Element> | null;
27
28
  };
28
29
  export declare const withoutNormalizing: (editor: PlateEditor, fn: () => boolean | void) => boolean;
@@ -34,4 +35,3 @@ export declare const fromDOMPoint: (editor: PlateEditor, domPoint: [Node, number
34
35
  suppressThrow: boolean;
35
36
  }) => s.BasePoint | null | undefined;
36
37
  export declare const mockPlugin: (plugin?: Partial<PlatePlugin> | undefined) => p.WithPlatePlugin<p.AnyObject, p.Value, p.PlateEditor<p.Value>>;
37
- export declare const getPlateSelectors: (id?: string | undefined) => StoreApiGet<p.PlateStoreState<Value, PlateEditor>, {}>;
@@ -25,6 +25,8 @@ export declare const createTestEditor: (options: {
25
25
  plugins: import("@udecode/plate-core").WithPlatePlugin<{}, Value, import("@udecode/plate-core").PlateEditor<Value>>[];
26
26
  pluginsByKey: Record<string, import("@udecode/plate-core").WithPlatePlugin<{}, Value, import("@udecode/plate-core").PlateEditor<Value>>>;
27
27
  prevSelection: import("slate").BaseRange | null;
28
+ blockFactory: (node?: Partial<import("@udecode/plate-core").TElement> | undefined, path?: import("slate").Path | undefined) => import("@udecode/plate-core").TElement;
29
+ childrenFactory: () => Value;
28
30
  currentKeyboardEvent: import("react").KeyboardEvent<Element> | null;
29
31
  };
30
32
  normalize: () => void;
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.6.1",
4
4
  "source": "./src/index.tsx",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -49,17 +49,17 @@
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": "83b651f1638ad6bab53565479e76b8d29b86af3a"
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;