@alpaca-headless/alpaca-headless-nextjs 1.0.4454 → 1.0.4455

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.
@@ -7,6 +7,10 @@ interface MultiComponentEditorProps {
7
7
  thumbZoom?: number;
8
8
  thumbWidth?: string;
9
9
  thumbHeight?: string;
10
+ onSelectedComponentChange?: (payload: {
11
+ selectedId: string | null;
12
+ selectedIndex: number;
13
+ }) => void;
10
14
  }
11
- export declare function MultiComponentEditor({ children, component, placeholderName, thumbZoom, thumbWidth, thumbHeight, }: MultiComponentEditorProps): import("react/jsx-runtime").JSX.Element | undefined;
15
+ export declare function MultiComponentEditor({ children, component, placeholderName, thumbZoom, thumbWidth, thumbHeight, onSelectedComponentChange, }: MultiComponentEditorProps): import("react/jsx-runtime").JSX.Element | undefined;
12
16
  export {};
@@ -1,7 +1,8 @@
1
1
  "use client";
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
- import React, { useState, useEffect, Fragment, useCallback, } from "react";
4
- export function MultiComponentEditor({ children, component, placeholderName, thumbZoom, thumbWidth, thumbHeight, }) {
3
+ import React, { Fragment, useCallback, useEffect, useState, } from "react";
4
+ export function MultiComponentEditor({ children, component, placeholderName, thumbZoom, thumbWidth, thumbHeight, onSelectedComponentChange, }) {
5
+ var _a, _b;
5
6
  const [selectedId, setSelectedId] = useState(null);
6
7
  const placeholder = component.placeholders.find((x) => x.name === placeholderName);
7
8
  if (!placeholder)
@@ -21,9 +22,23 @@ export function MultiComponentEditor({ children, component, placeholderName, thu
21
22
  };
22
23
  }, []);
23
24
  const selectedIndex = components.findIndex((x) => x.id === selectedId);
25
+ const resolvedIndex = selectedIndex < 0 ? 0 : selectedIndex;
26
+ const resolvedComponent = (_a = components[resolvedIndex]) !== null && _a !== void 0 ? _a : null;
27
+ const resolvedId = (_b = resolvedComponent === null || resolvedComponent === void 0 ? void 0 : resolvedComponent.id) !== null && _b !== void 0 ? _b : null;
28
+ useEffect(() => {
29
+ if (resolvedId !== null && selectedId !== resolvedId) {
30
+ setSelectedId(resolvedId);
31
+ }
32
+ }, [resolvedId, selectedId]);
33
+ useEffect(() => {
34
+ onSelectedComponentChange === null || onSelectedComponentChange === void 0 ? void 0 : onSelectedComponentChange({
35
+ selectedId: resolvedId,
36
+ selectedIndex: resolvedIndex,
37
+ });
38
+ }, [resolvedId, resolvedIndex, onSelectedComponentChange]);
24
39
  const childrenArray = React.Children.toArray(children);
25
40
  const filteredChildren = childrenArray.filter((child) => child.type !== "script");
26
- return (_jsxs(_Fragment, { children: [filteredChildren[selectedIndex < 0 ? 0 : selectedIndex], _jsxs("div", { className: "a-editor-mce", children: [_jsx("script", { "data-placeholder-start": placeholder.key, "data-orientation": "horizontal" }), filteredChildren.map((x, index) => thumbnail(x, index)), _jsx("script", { "data-placeholder-end": placeholder.key })] })] }));
41
+ return (_jsxs(_Fragment, { children: [filteredChildren[resolvedIndex], _jsxs("div", { className: "a-editor-mce", children: [_jsx("script", { "data-placeholder-start": placeholder.key, "data-orientation": "horizontal" }), filteredChildren.map((x, index) => thumbnail(x, index)), _jsx("script", { "data-placeholder-end": placeholder.key })] })] }));
27
42
  function thumbnail(x, index) {
28
43
  var _a, _b, _c, _d, _e, _f, _g;
29
44
  const style = {};
@@ -36,7 +51,7 @@ export function MultiComponentEditor({ children, component, placeholderName, thu
36
51
  const componentArray = (_c = (_b = (_a = x.props) === null || _a === void 0 ? void 0 : _a.children) === null || _b === void 0 ? void 0 : _b.props) === null || _c === void 0 ? void 0 : _c.children;
37
52
  const component = components[index];
38
53
  return (_jsxs(Fragment, { children: [_jsxs("div", { className: "a-editor-mce-thumbnail" +
39
- (index === selectedIndex ? " a-editor-mce-thumbnail--selected" : ""), style: style, onClick: () => {
54
+ (index === resolvedIndex ? " a-editor-mce-thumbnail--selected" : ""), style: style, onClick: () => {
40
55
  selectComponents([components[index].id]);
41
56
  }, children: [_jsx("script", { "data-component-start": component.id, "data-itemid": ((_e = (_d = component._editor) === null || _d === void 0 ? void 0 : _d.linkedComponentItem) === null || _e === void 0 ? void 0 : _e.id) || component.id, "data-layoutid": (_g = (_f = component._editor) === null || _f === void 0 ? void 0 : _f.hostingPageItem) === null || _g === void 0 ? void 0 : _g.id }), _jsx("div", { style: { pointerEvents: "none", zoom: thumbZoom || 0.25 }, children: componentArray.slice(1, -1) }), _jsx("script", { "data-component-end": component.id })] }), _jsx("div", { "data-dropzone": components[index].id })] }, index));
42
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alpaca-headless/alpaca-headless-nextjs",
3
- "version": "1.0.4454",
3
+ "version": "1.0.4455",
4
4
  "type": "module",
5
5
  "description": "Alpaca Headless",
6
6
  "main": "dist/index.js",
@@ -51,7 +51,7 @@
51
51
  "typescript": "5.9.3"
52
52
  },
53
53
  "dependencies": {
54
- "@alpaca-headless/alpaca-headless": "^1.0.4454",
54
+ "@alpaca-headless/alpaca-headless": "^1.0.4455",
55
55
  "axios": "^1.13.6",
56
56
  "next": "16.2.1",
57
57
  "react": "19.2.4",
@@ -2,12 +2,12 @@
2
2
  import { ComponentData } from "@alpaca-headless/alpaca-headless";
3
3
 
4
4
  import React, {
5
- useState,
5
+ Fragment,
6
6
  ReactElement,
7
7
  ReactNode,
8
- useEffect,
9
- Fragment,
10
8
  useCallback,
9
+ useEffect,
10
+ useState,
11
11
  } from "react";
12
12
 
13
13
  interface MultiComponentEditorProps {
@@ -17,6 +17,10 @@ interface MultiComponentEditorProps {
17
17
  thumbZoom?: number;
18
18
  thumbWidth?: string;
19
19
  thumbHeight?: string;
20
+ onSelectedComponentChange?: (payload: {
21
+ selectedId: string | null;
22
+ selectedIndex: number;
23
+ }) => void;
20
24
  }
21
25
 
22
26
  export function MultiComponentEditor({
@@ -26,6 +30,7 @@ export function MultiComponentEditor({
26
30
  thumbZoom,
27
31
  thumbWidth,
28
32
  thumbHeight,
33
+ onSelectedComponentChange,
29
34
  }: MultiComponentEditorProps) {
30
35
  const [selectedId, setSelectedId] = useState<string | null>(null);
31
36
  const placeholder = component.placeholders.find(
@@ -55,6 +60,22 @@ export function MultiComponentEditor({
55
60
  const selectedIndex = components.findIndex(
56
61
  (x: ComponentData) => x.id === selectedId
57
62
  );
63
+ const resolvedIndex = selectedIndex < 0 ? 0 : selectedIndex;
64
+ const resolvedComponent = components[resolvedIndex] ?? null;
65
+ const resolvedId = resolvedComponent?.id ?? null;
66
+
67
+ useEffect(() => {
68
+ if (resolvedId !== null && selectedId !== resolvedId) {
69
+ setSelectedId(resolvedId);
70
+ }
71
+ }, [resolvedId, selectedId]);
72
+
73
+ useEffect(() => {
74
+ onSelectedComponentChange?.({
75
+ selectedId: resolvedId,
76
+ selectedIndex: resolvedIndex,
77
+ });
78
+ }, [resolvedId, resolvedIndex, onSelectedComponentChange]);
58
79
 
59
80
  const childrenArray = React.Children.toArray(children) as ReactElement[];
60
81
 
@@ -64,7 +85,7 @@ export function MultiComponentEditor({
64
85
 
65
86
  return (
66
87
  <>
67
- {filteredChildren[selectedIndex < 0 ? 0 : selectedIndex]}
88
+ {filteredChildren[resolvedIndex]}
68
89
  <div className="a-editor-mce">
69
90
  <script
70
91
  data-placeholder-start={placeholder.key}
@@ -100,7 +121,7 @@ export function MultiComponentEditor({
100
121
  <div
101
122
  className={
102
123
  "a-editor-mce-thumbnail" +
103
- (index === selectedIndex ? " a-editor-mce-thumbnail--selected" : "")
124
+ (index === resolvedIndex ? " a-editor-mce-thumbnail--selected" : "")
104
125
  }
105
126
  style={style}
106
127
  onClick={() => {