@dotcms/react 0.0.1-alpha.60 → 0.0.1-alpha.61

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.
package/index.esm.js CHANGED
@@ -1850,6 +1850,49 @@ const getPositionStyleClasses = (start, end) => {
1850
1850
  endClass
1851
1851
  };
1852
1852
  };
1853
+ /**
1854
+ * Validates the structure of a Block Editor block.
1855
+ *
1856
+ * This function checks that:
1857
+ * 1. The blocks parameter is a valid object
1858
+ * 2. The block has a 'doc' type
1859
+ * 3. The block has a valid content array that is not empty
1860
+ *
1861
+ * @param {Block} blocks - The blocks structure to validate
1862
+ * @returns {BlockEditorState} Object containing validation state and any error message
1863
+ * @property {boolean} BlockEditorState.isValid - Whether the blocks structure is valid
1864
+ * @property {string | null} BlockEditorState.error - Error message if invalid, null if valid
1865
+ */
1866
+ const isValidBlocks = blocks => {
1867
+ if (!blocks) {
1868
+ return {
1869
+ error: `Error: Blocks object is not defined`
1870
+ };
1871
+ }
1872
+ if (typeof blocks !== 'object') {
1873
+ return {
1874
+ error: `Error: Blocks must be an object, but received: ${typeof blocks}`
1875
+ };
1876
+ }
1877
+ if (blocks.type !== 'doc') {
1878
+ return {
1879
+ error: 'Error: Blocks must have a doc type'
1880
+ };
1881
+ }
1882
+ if (!blocks.content || !Array.isArray(blocks.content)) {
1883
+ return {
1884
+ error: 'Error: Blocks must have a valid content array'
1885
+ };
1886
+ }
1887
+ if (blocks.content.length === 0) {
1888
+ return {
1889
+ error: 'Error: Blocks content is empty'
1890
+ };
1891
+ }
1892
+ return {
1893
+ error: null
1894
+ };
1895
+ };
1853
1896
 
1854
1897
  var NATIVE_BIND$1 = functionBindNative;
1855
1898
 
@@ -5202,6 +5245,24 @@ const BlockEditorRenderer = ({
5202
5245
  customRenderers
5203
5246
  }) => {
5204
5247
  const ref = useRef(null);
5248
+ const [blockEditorState, setBlockEditorState] = useState({
5249
+ error: null
5250
+ });
5251
+ /**
5252
+ * Sets up inline editing functionality when the component is editable and inside the editor.
5253
+ *
5254
+ * This effect:
5255
+ * 1. Checks if inline editing should be enabled based on props and editor context
5256
+ * 2. Validates required props for inline editing (contentlet and fieldName)
5257
+ * 3. Extracts necessary data from the contentlet
5258
+ * 4. Adds a click handler to initialize inline editing with the block editor
5259
+ * 5. Cleans up event listener on unmount
5260
+ *
5261
+ * @dependency {boolean} editable - Flag to enable/disable inline editing
5262
+ * @dependency {DotCMSContentlet} contentlet - Contentlet data required for editing
5263
+ * @dependency {Block} blocks - The content blocks to edit
5264
+ * @dependency {string} fieldName - Name of the field being edited
5265
+ */
5205
5266
  useEffect(() => {
5206
5267
  if (!editable || !ref.current || !isInsideEditor()) {
5207
5268
  return;
@@ -5233,13 +5294,40 @@ const BlockEditorRenderer = ({
5233
5294
  element.addEventListener('click', handleClickEvent);
5234
5295
  return () => element.removeEventListener('click', handleClickEvent);
5235
5296
  }, [editable, contentlet, blocks, fieldName]);
5297
+ /**
5298
+ * Validates the blocks structure and updates the block editor state.
5299
+ *
5300
+ * This effect:
5301
+ * 1. Validates that blocks have the correct structure (doc type, content array, etc)
5302
+ * 2. Updates the block editor state with validation result
5303
+ * 3. Logs any validation errors to console
5304
+ *
5305
+ * @dependency {Block} blocks - The content blocks to validate
5306
+ */
5307
+ useEffect(() => {
5308
+ const validationResult = isValidBlocks(blocks);
5309
+ setBlockEditorState(validationResult);
5310
+ if (validationResult.error) {
5311
+ console.error(validationResult.error);
5312
+ }
5313
+ }, [blocks]);
5314
+ if (blockEditorState.error) {
5315
+ console.error(blockEditorState.error);
5316
+ if (isInsideEditor()) {
5317
+ return jsx("div", {
5318
+ "data-testid": "invalid-blocks-message",
5319
+ children: blockEditorState.error
5320
+ });
5321
+ }
5322
+ return null;
5323
+ }
5236
5324
  return jsx("div", {
5237
5325
  className: className,
5238
5326
  style: style,
5239
5327
  ref: ref,
5240
5328
  "data-testid": "dot-block-editor-container",
5241
5329
  children: jsx(BlockEditorBlock, {
5242
- content: blocks.content,
5330
+ content: blocks == null ? void 0 : blocks.content,
5243
5331
  customRenderers: customRenderers
5244
5332
  })
5245
5333
  });
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@dotcms/react",
3
- "version": "0.0.1-alpha.60",
3
+ "version": "0.0.1-alpha.61",
4
4
  "peerDependencies": {
5
5
  "react": ">=18",
6
6
  "react-dom": ">=18",
7
- "@dotcms/client": "0.0.1-alpha.60",
7
+ "@dotcms/client": "0.0.1-alpha.61",
8
8
  "@tinymce/tinymce-react": "^5.1.1"
9
9
  },
10
10
  "description": "Official React Components library to render a dotCMS page.",
@@ -33,5 +33,5 @@ type BlockEditorRendererProps = EditableProps | NonEditableProps;
33
33
  * @param {string} [props.fieldName] - Field name for inline editing. Required when `editable` is true.
34
34
  * @returns {JSX.Element} A div containing the rendered blocks of content.
35
35
  */
36
- export declare const BlockEditorRenderer: ({ style, blocks, editable, fieldName, className, contentlet, customRenderers }: BlockEditorRendererProps) => import("react/jsx-runtime").JSX.Element;
36
+ export declare const BlockEditorRenderer: ({ style, blocks, editable, fieldName, className, contentlet, customRenderers }: BlockEditorRendererProps) => import("react/jsx-runtime").JSX.Element | null;
37
37
  export {};
@@ -8,6 +8,7 @@ import { ContentNode } from './content-node.interface';
8
8
  */
9
9
  export interface Block {
10
10
  content: ContentNode[];
11
+ type: string;
11
12
  }
12
13
  /**
13
14
  * Props for a contentlet inside the Block Editor
@@ -76,3 +77,13 @@ export declare enum Blocks {
76
77
  TABLE = "table",
77
78
  DOT_CONTENT = "dotContent"
78
79
  }
80
+ /**
81
+ * Represents the validation state of a Block Editor instance
82
+ *
83
+ * @interface BlockEditorState
84
+ * @property {boolean} isValid - Whether the blocks structure is valid
85
+ * @property {string | null} error - Error message if blocks are invalid, null otherwise
86
+ */
87
+ export interface BlockEditorState {
88
+ error: string | null;
89
+ }
@@ -1,4 +1,5 @@
1
1
  import { ContainerData, DotCMSPageContext } from '../models';
2
+ import { Block, BlockEditorState } from '../models/blocks.interface';
2
3
  /**
3
4
  * Get the container data from the containers object using the current container reference obtained from the layout.
4
5
  *
@@ -41,3 +42,17 @@ export declare const getPositionStyleClasses: (start: number, end: number) => {
41
42
  startClass: string;
42
43
  endClass: string;
43
44
  };
45
+ /**
46
+ * Validates the structure of a Block Editor block.
47
+ *
48
+ * This function checks that:
49
+ * 1. The blocks parameter is a valid object
50
+ * 2. The block has a 'doc' type
51
+ * 3. The block has a valid content array that is not empty
52
+ *
53
+ * @param {Block} blocks - The blocks structure to validate
54
+ * @returns {BlockEditorState} Object containing validation state and any error message
55
+ * @property {boolean} BlockEditorState.isValid - Whether the blocks structure is valid
56
+ * @property {string | null} BlockEditorState.error - Error message if invalid, null if valid
57
+ */
58
+ export declare const isValidBlocks: (blocks: Block) => BlockEditorState;