@accelerated-agency/visual-editor 0.3.0 → 0.3.2

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/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import { create } from 'zustand';
4
4
  import { persist } from 'zustand/middleware';
5
5
  import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
6
6
  import { HexColorPicker } from 'react-colorful';
7
+ import 'bootstrap-icons/font/bootstrap-icons.css';
7
8
 
8
9
  var PERSIST_NAME_PREFIX = "conversion-editor-variations-v3-";
9
10
  function variationsPersistStorageName(experimentId) {
@@ -4646,6 +4647,7 @@ function PlatformVisualEditor({
4646
4647
  }, [dirty, onDirtyChange]);
4647
4648
  const loadPayload = useMemo(
4648
4649
  () => ({
4650
+ iid: experiment?.iid,
4649
4651
  experimentId: experiment?.experimentId,
4650
4652
  name: experiment?.name,
4651
4653
  status: experiment?.status,
@@ -4655,6 +4657,7 @@ function PlatformVisualEditor({
4655
4657
  }),
4656
4658
  [experiment]
4657
4659
  );
4660
+ console.log("loadPayload", loadPayload);
4658
4661
  useEffect(() => {
4659
4662
  if (!editorReady) return;
4660
4663
  const payloadKey = JSON.stringify(loadPayload);
@@ -4833,6 +4836,7 @@ function PlatformVisualEditorV2({
4833
4836
  }) {
4834
4837
  const iframeRef = useRef(null);
4835
4838
  const [editorReady, setEditorReady] = useState(false);
4839
+ const [isSaving, setIsSaving] = useState(false);
4836
4840
  const [dirty, setDirty] = useState(false);
4837
4841
  const dirtyRef = useRef(false);
4838
4842
  const mutationSkipCountRef = useRef(0);
@@ -4857,6 +4861,7 @@ function PlatformVisualEditorV2({
4857
4861
  }, []);
4858
4862
  const loadPayload = useMemo(
4859
4863
  () => ({
4864
+ iid: experiment?.iid,
4860
4865
  experimentId: experiment?.experimentId,
4861
4866
  name: experiment?.name,
4862
4867
  status: experiment?.status,
@@ -4866,6 +4871,7 @@ function PlatformVisualEditorV2({
4866
4871
  }),
4867
4872
  [experiment]
4868
4873
  );
4874
+ console.log("loadPayload", loadPayload);
4869
4875
  const editorSrc = useMemo(() => {
4870
4876
  const safeBaseUrl = normalizeProxyBaseUrl(proxyBaseUrl);
4871
4877
  return safeBaseUrl ? `${safeBaseUrl}/vvveb-editor` : "/vvveb-editor";
@@ -5020,5 +5026,344 @@ function PlatformVisualEditorV2({
5020
5026
  ) })
5021
5027
  ] });
5022
5028
  }
5029
+ var AI_EDITOR_CHANNEL = "ve-ai-editor";
5030
+ var AI_VIEWPORT_WIDTH = {
5031
+ desktop: "100%",
5032
+ tablet: "768px",
5033
+ mobile: "390px"
5034
+ };
5035
+ var AI_VIEWPORT_LABEL = {
5036
+ desktop: "1440px",
5037
+ tablet: "768px",
5038
+ mobile: "390px"
5039
+ };
5040
+ function fingerprint(value) {
5041
+ let hash = 5381;
5042
+ for (let index = 0; index < value.length; index += 1) {
5043
+ hash = hash * 33 ^ value.charCodeAt(index);
5044
+ }
5045
+ return (hash >>> 0).toString(36);
5046
+ }
5047
+ function PlatformAiEditor({
5048
+ embeddedGlobalKey = "__CONVERSION_EMBEDDED__",
5049
+ proxyBaseUrl = "",
5050
+ chatComponent,
5051
+ className = "fixed inset-0 z-[9999] flex flex-col bg-white",
5052
+ editorClassName = "flex-1 min-h-0",
5053
+ showHeader = true,
5054
+ title,
5055
+ status,
5056
+ tabs = [],
5057
+ activeTab = "AI Editor",
5058
+ error = null,
5059
+ showCloseButton = true,
5060
+ closeLabel = "Close",
5061
+ experiment,
5062
+ activeVariationId: controlledActiveVariationId,
5063
+ onClose,
5064
+ onTabChange,
5065
+ onDirtyChange,
5066
+ onEditorReady,
5067
+ onEditorUrlChanged,
5068
+ onSaveSuccess,
5069
+ onSaveError,
5070
+ onRequestSave,
5071
+ renderHeader,
5072
+ renderError,
5073
+ setActiveVariationId: controlledSetActiveVariationId
5074
+ }) {
5075
+ const iframeRef = useRef(null);
5076
+ const [localActiveVariationId, setLocalActiveVariationId] = useState(null);
5077
+ const [isSaving, setIsSaving] = useState(false);
5078
+ const [isIframeLoading, setIsIframeLoading] = useState(false);
5079
+ const [viewport, setViewport] = useState("desktop");
5080
+ useEffect(() => {
5081
+ window[embeddedGlobalKey] = true;
5082
+ return () => {
5083
+ delete window[embeddedGlobalKey];
5084
+ };
5085
+ }, [embeddedGlobalKey]);
5086
+ useEffect(() => {
5087
+ onDirtyChange?.(false);
5088
+ }, [onDirtyChange]);
5089
+ const variations = experiment?.variations ?? [];
5090
+ const activeVariationId = controlledActiveVariationId ?? localActiveVariationId;
5091
+ const setActiveVariationId = controlledSetActiveVariationId ?? setLocalActiveVariationId;
5092
+ useEffect(() => {
5093
+ if (!variations.length) {
5094
+ setActiveVariationId(null);
5095
+ return;
5096
+ }
5097
+ setActiveVariationId((current) => {
5098
+ if (current && variations.some((variation) => variation._id === current)) return current;
5099
+ const baseline = variations.find((variation) => variation.baseline);
5100
+ return baseline?._id ?? variations[0]._id;
5101
+ });
5102
+ }, [variations]);
5103
+ const activeVariation = useMemo(
5104
+ () => variations.find((variation) => variation._id === activeVariationId) ?? null,
5105
+ [variations, activeVariationId]
5106
+ );
5107
+ const effectiveCssCode = useMemo(() => {
5108
+ return (activeVariation?.csscode ?? "").trim();
5109
+ }, [activeVariation?.csscode]);
5110
+ const effectiveJsCode = useMemo(() => {
5111
+ return (activeVariation?.jscode ?? "").trim();
5112
+ }, [activeVariation?.jscode]);
5113
+ const cssFingerprint = useMemo(() => fingerprint(effectiveCssCode), [effectiveCssCode]);
5114
+ const jsFingerprint = useMemo(() => fingerprint(effectiveJsCode), [effectiveJsCode]);
5115
+ const hasAiCode = useMemo(
5116
+ () => Boolean(effectiveCssCode.trim() || effectiveJsCode.trim()),
5117
+ [effectiveCssCode, effectiveJsCode]
5118
+ );
5119
+ const iframeKey = useMemo(
5120
+ () => `ai-iframe-${activeVariationId ?? "default"}-${cssFingerprint}-${jsFingerprint}`,
5121
+ [activeVariationId, cssFingerprint, jsFingerprint]
5122
+ );
5123
+ const iframeWidth = AI_VIEWPORT_WIDTH[viewport];
5124
+ const editorSrc = useMemo(() => {
5125
+ const pageUrl = experiment?.pageUrl;
5126
+ if (!pageUrl) return "about:blank";
5127
+ const safeBaseUrl = normalizeProxyBaseUrl(proxyBaseUrl);
5128
+ const query = new URLSearchParams({
5129
+ password: experiment?.editorPassword ?? "",
5130
+ url: pageUrl
5131
+ }).toString();
5132
+ const previewPath = `/api/conversion-proxy?${query}`;
5133
+ return safeBaseUrl ? `${safeBaseUrl}${previewPath}` : previewPath;
5134
+ }, [proxyBaseUrl, experiment?.pageUrl, experiment?.editorPassword]);
5135
+ const postAiCodeToIframe = useCallback(() => {
5136
+ const targetWindow = iframeRef.current?.contentWindow;
5137
+ if (!targetWindow) return;
5138
+ targetWindow.postMessage(
5139
+ {
5140
+ channel: AI_EDITOR_CHANNEL,
5141
+ type: "apply-ai-code",
5142
+ payload: {
5143
+ cssCode: effectiveCssCode,
5144
+ jsCode: effectiveJsCode
5145
+ }
5146
+ },
5147
+ "*"
5148
+ );
5149
+ }, [effectiveCssCode, effectiveJsCode]);
5150
+ const parseProxyPayloadFromUrl = useCallback((rawUrl) => {
5151
+ if (!rawUrl) return null;
5152
+ try {
5153
+ const parsed = new URL(rawUrl, window.location.href);
5154
+ const url = parsed.searchParams.get("url") ?? void 0;
5155
+ const password = parsed.searchParams.get("password") ?? void 0;
5156
+ return { url, password };
5157
+ } catch {
5158
+ return null;
5159
+ }
5160
+ }, []);
5161
+ const applyAiCodeInIframe = useCallback(() => {
5162
+ postAiCodeToIframe();
5163
+ }, [postAiCodeToIframe]);
5164
+ useEffect(() => {
5165
+ applyAiCodeInIframe();
5166
+ }, [applyAiCodeInIframe]);
5167
+ useEffect(() => {
5168
+ if (!hasAiCode) {
5169
+ setIsIframeLoading(false);
5170
+ return;
5171
+ }
5172
+ setIsIframeLoading(true);
5173
+ const timeoutId = window.setTimeout(() => {
5174
+ setIsIframeLoading(false);
5175
+ }, 5e3);
5176
+ return () => window.clearTimeout(timeoutId);
5177
+ }, [hasAiCode, cssFingerprint, jsFingerprint, activeVariationId]);
5178
+ const handleIframeLoad = useCallback(() => {
5179
+ applyAiCodeInIframe();
5180
+ postAiCodeToIframe();
5181
+ const currentSrc = iframeRef.current?.src ?? null;
5182
+ const proxiedPayload = parseProxyPayloadFromUrl(currentSrc);
5183
+ if (proxiedPayload?.url || proxiedPayload?.password) {
5184
+ void onEditorUrlChanged?.(proxiedPayload);
5185
+ }
5186
+ onEditorReady?.();
5187
+ }, [
5188
+ applyAiCodeInIframe,
5189
+ postAiCodeToIframe,
5190
+ onEditorReady,
5191
+ onEditorUrlChanged,
5192
+ parseProxyPayloadFromUrl
5193
+ ]);
5194
+ useEffect(() => {
5195
+ const onMessage = (event) => {
5196
+ const msg = event.data;
5197
+ if (!msg || msg.channel !== AI_EDITOR_CHANNEL) return;
5198
+ if (msg.type === "ai-code-ready") {
5199
+ postAiCodeToIframe();
5200
+ return;
5201
+ }
5202
+ if (msg.type === "ai-url-changed") {
5203
+ const payload = msg.payload ?? {};
5204
+ void onEditorUrlChanged?.({
5205
+ url: payload.url,
5206
+ password: payload.password
5207
+ });
5208
+ return;
5209
+ }
5210
+ if (msg.type === "ai-code-applied") {
5211
+ setIsIframeLoading(false);
5212
+ }
5213
+ };
5214
+ window.addEventListener("message", onMessage);
5215
+ return () => window.removeEventListener("message", onMessage);
5216
+ }, [postAiCodeToIframe, onEditorUrlChanged]);
5217
+ const onTabClick = useCallback(
5218
+ (tab) => onTabChange?.(tab),
5219
+ [onTabChange]
5220
+ );
5221
+ const handleSave = useCallback(async () => {
5222
+ if (!onRequestSave || isSaving) return;
5223
+ const payload = {
5224
+ experimentId: experiment?.experimentId,
5225
+ variations,
5226
+ cssCode: effectiveCssCode,
5227
+ jsCode: effectiveJsCode,
5228
+ selectedVariationId: activeVariation?._id,
5229
+ selectedVariation: activeVariation ?? void 0
5230
+ };
5231
+ setIsSaving(true);
5232
+ try {
5233
+ await onRequestSave(payload);
5234
+ onSaveSuccess?.(payload);
5235
+ } catch (error2) {
5236
+ onSaveError?.(error2);
5237
+ } finally {
5238
+ setIsSaving(false);
5239
+ }
5240
+ }, [
5241
+ onRequestSave,
5242
+ isSaving,
5243
+ experiment?.experimentId,
5244
+ variations,
5245
+ effectiveCssCode,
5246
+ effectiveJsCode,
5247
+ activeVariation,
5248
+ onSaveSuccess,
5249
+ onSaveError
5250
+ ]);
5251
+ if (error) {
5252
+ if (renderError) return renderError(error);
5253
+ return /* @__PURE__ */ jsx("div", { className: "fixed inset-0 z-[9999] flex items-center justify-center bg-slate-50", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2 text-center px-8", children: [
5254
+ /* @__PURE__ */ jsx("div", { className: "text-2xl text-red-300", children: "\u26A0" }),
5255
+ /* @__PURE__ */ jsx("p", { className: "text-sm font-medium text-slate-700", children: "Something went wrong" }),
5256
+ /* @__PURE__ */ jsx("p", { className: "text-xs text-red-400", children: error })
5257
+ ] }) });
5258
+ }
5259
+ return /* @__PURE__ */ jsxs("div", { className, children: [
5260
+ showHeader ? renderHeader?.({
5261
+ title: title ?? experiment?.name,
5262
+ status: status ?? experiment?.status,
5263
+ tabs,
5264
+ activeTab,
5265
+ onTabClick,
5266
+ onClose
5267
+ }) ?? /* @__PURE__ */ jsxs("div", { className: "h-12 border-b border-slate-200 bg-white flex items-center justify-between px-4 shadow-[0_1px_3px_rgba(0,0,0,0.04)] shrink-0", children: [
5268
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2.5 min-w-0", children: [
5269
+ /* @__PURE__ */ jsx("span", { className: "text-[10px] font-bold tracking-wide px-1.5 py-0.5 rounded bg-indigo-50 text-indigo-600 border border-indigo-100 shrink-0", children: "AI" }),
5270
+ /* @__PURE__ */ jsx("span", { className: "font-semibold text-sm text-slate-800 truncate", children: title ?? experiment?.name ?? "AI Editor" }),
5271
+ status ? /* @__PURE__ */ jsx("span", { className: "hidden sm:inline-flex text-[10px] px-2 py-0.5 rounded-full border border-slate-200 text-slate-500 font-medium bg-slate-50 capitalize", children: status }) : null
5272
+ ] }),
5273
+ tabs.length > 0 ? /* @__PURE__ */ jsx("div", { className: "hidden md:flex items-center gap-1 absolute left-1/2 -translate-x-1/2", children: tabs.map((tab) => /* @__PURE__ */ jsx(
5274
+ "button",
5275
+ {
5276
+ type: "button",
5277
+ onClick: () => onTabClick(tab),
5278
+ className: `text-[11px] font-semibold px-3 py-1 rounded-full border transition-all ${tab.label === activeTab ? "bg-indigo-600 text-white border-indigo-600 shadow-sm" : "bg-white text-slate-500 border-slate-200 hover:border-slate-300 hover:text-slate-700"}`,
5279
+ children: tab.label
5280
+ },
5281
+ tab.hash
5282
+ )) }) : null,
5283
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
5284
+ /* @__PURE__ */ jsxs("div", { className: "h-8 px-3 rounded-md border border-slate-200 bg-white flex items-center gap-2 text-xs font-medium text-slate-700", children: [
5285
+ /* @__PURE__ */ jsx("span", { children: AI_VIEWPORT_LABEL[viewport] }),
5286
+ /* @__PURE__ */ jsx("i", { className: "bi bi-chevron-down text-[10px] text-slate-500" })
5287
+ ] }),
5288
+ /* @__PURE__ */ jsx("div", { className: "h-8 rounded-lg bg-slate-100 px-1 flex items-center gap-1", style: { backgroundColor: "#F0F0F0" }, children: [
5289
+ { key: "desktop", icon: "bi-display", title: "Desktop \u2014 full width" },
5290
+ { key: "tablet", icon: "bi-tablet", title: "Tablet \u2014 768px" },
5291
+ { key: "mobile", icon: "bi-phone", title: "Mobile \u2014 390px" }
5292
+ ].map((vp) => /* @__PURE__ */ jsx(
5293
+ "button",
5294
+ {
5295
+ type: "button",
5296
+ onClick: () => setViewport(vp.key),
5297
+ title: vp.title,
5298
+ className: `w-7 h-7 rounded-md border flex items-center justify-center transition-all ${viewport === vp.key ? "bg-white border-slate-300 text-slate-700 shadow-sm" : "bg-transparent border-transparent text-slate-500 hover:text-slate-700 hover:bg-white/70"}`,
5299
+ children: /* @__PURE__ */ jsx("i", { className: `bi ${vp.icon} text-[12px]` })
5300
+ },
5301
+ vp.key
5302
+ )) })
5303
+ ] }),
5304
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
5305
+ variations.length > 0 ? /* @__PURE__ */ jsx(
5306
+ "select",
5307
+ {
5308
+ className: "h-8 max-w-[220px] rounded-md border border-slate-200 bg-white px-2 text-xs text-slate-700",
5309
+ value: activeVariationId ?? "",
5310
+ onChange: (event) => setActiveVariationId(event.target.value || null),
5311
+ "aria-label": "Select variation",
5312
+ children: variations.map((variation) => /* @__PURE__ */ jsx("option", { value: variation._id, children: variation.name }, variation._id))
5313
+ }
5314
+ ) : null,
5315
+ onRequestSave ? /* @__PURE__ */ jsx(
5316
+ "button",
5317
+ {
5318
+ type: "button",
5319
+ className: "text-[11px] font-medium px-3 py-1.5 rounded-md border border-indigo-200 text-indigo-700 bg-indigo-50 hover:bg-indigo-100 hover:border-indigo-300 transition-all disabled:opacity-60 disabled:cursor-not-allowed",
5320
+ onClick: () => void handleSave(),
5321
+ disabled: isSaving,
5322
+ children: isSaving ? "Saving..." : "Save"
5323
+ }
5324
+ ) : null,
5325
+ showCloseButton ? /* @__PURE__ */ jsx(
5326
+ "button",
5327
+ {
5328
+ type: "button",
5329
+ className: "text-[11px] font-medium px-3 py-1.5 rounded-md border border-slate-200 text-slate-600 hover:bg-slate-50 hover:border-slate-300 transition-all",
5330
+ onClick: () => onClose?.(),
5331
+ children: closeLabel
5332
+ }
5333
+ ) : null
5334
+ ] })
5335
+ ] }) : null,
5336
+ /* @__PURE__ */ jsxs("div", { className: `${editorClassName} relative`, children: [
5337
+ /* @__PURE__ */ jsx("div", { className: "w-full h-full bg-slate-100 overflow-auto flex justify-center items-start p-3", children: /* @__PURE__ */ jsx(
5338
+ "div",
5339
+ {
5340
+ className: "h-full bg-white border border-slate-200 shadow-sm overflow-hidden",
5341
+ style: {
5342
+ width: iframeWidth,
5343
+ minWidth: iframeWidth,
5344
+ maxWidth: "100%"
5345
+ },
5346
+ children: /* @__PURE__ */ jsx(
5347
+ "iframe",
5348
+ {
5349
+ ref: iframeRef,
5350
+ src: editorSrc,
5351
+ onLoad: handleIframeLoad,
5352
+ className: "w-full h-full border-0",
5353
+ title: "AI Editor Preview",
5354
+ allow: "same-origin"
5355
+ },
5356
+ iframeKey
5357
+ )
5358
+ }
5359
+ ) }),
5360
+ isIframeLoading ? /* @__PURE__ */ jsx("div", { className: "absolute inset-0 z-30 flex items-center justify-center bg-slate-100/95 backdrop-blur-[1px]", children: /* @__PURE__ */ jsxs("div", { className: "w-[560px] max-w-[88vw]", children: [
5361
+ /* @__PURE__ */ jsx("p", { className: "mb-3 text-center text-base font-semibold text-slate-700", children: "Loading changes..." }),
5362
+ /* @__PURE__ */ jsx("div", { className: "h-4 w-full rounded-full border border-slate-300 bg-white/90 p-1 shadow-sm", children: /* @__PURE__ */ jsx("div", { className: "h-full w-full overflow-hidden rounded-full bg-slate-200", children: /* @__PURE__ */ jsx("div", { className: "h-full w-1/3 rounded-full bg-gradient-to-r from-emerald-300 via-sky-500 to-indigo-600 animate-pulse" }) }) })
5363
+ ] }) }) : null,
5364
+ chatComponent ?? null
5365
+ ] })
5366
+ ] });
5367
+ }
5023
5368
 
5024
- export { EditorShell, PlatformVisualEditor, PlatformVisualEditorV2, ToastProvider, hydrateVariationsFromStorage, useToast, variationsPersistStorageName };
5369
+ export { EditorShell, PlatformAiEditor, PlatformVisualEditor, PlatformVisualEditorV2, ToastProvider, hydrateVariationsFromStorage, useToast, variationsPersistStorageName };