@jbpark/live-editor 2.0.1 → 2.0.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.
@@ -1,7 +1,7 @@
1
- import { C as __toESM, b as DRAGGABLE_ITEMS, f as nanoid, g as BINDING_PROP, h as require_lib, m as require_lib$1, n as createSectionPreviewCache, r as fillSectionIds, y as DEFAULT_TEMPLATE } from "./document-DEx4WyCn.js";
1
+ import { C as __toESM, b as DRAGGABLE_ITEMS, f as nanoid, g as BINDING_PROP, m as require_lib, n as createSectionPreviewCache, r as fillSectionIds, y as DEFAULT_TEMPLATE } from "./document-DEx4WyCn.js";
2
2
  import { d as replaceSections, o as extractSections, r as cn, s as generateSection, u as preloadScripts } from "./utils-CXq1KaM3.js";
3
3
  import { i as usePreview } from "./states-Ci1AvoQ9.js";
4
- import { S as generateCode, _ as extractObjectProperties, b as parseValue, c as extract, d as getStructuredValue, f as parseBinding, g as extractNodeValue, h as createNodeFromValue, i as replaceIds, l as findEditableChildren, m as arrayExpressionToCode, n as clone, o as update, r as fillIds, t as validateBindingValue, u as getCurrentValue, y as parseArrayExpression } from "./ast-BmGLa00g.js";
4
+ import { A as parseValue, D as extractObjectProperties, E as extractNodeValue, M as generateCode, S as parseBinding, _ as moveSelectedIndices, a as extract, b as getCurrentValue, c as moveArrayItem, d as removeArrayItems, f as updateArrayItemProperty, g as replaceIds, h as fillIds, k as parseArrayExpression, l as moveArrayItems, o as appendArrayItem, p as updateArrayItemValue, r as update, s as duplicateArrayItems, t as validateBindingValue, v as removeIndices, x as getStructuredValue, y as findEditableChildren } from "./ast-BKQmEBj9.js";
5
5
  import { n as useCompiledModule, r as Frame, t as useDynamicTailwind } from "./use-dynamic-tailwind-_3tC-S5j.js";
6
6
  import { n as Error, t as ErrorBoundary } from "./boundary-b-U70XU_.js";
7
7
  import { t as Core } from "./core-mdFWxqB2.js";
@@ -190,8 +190,7 @@ const Tiptap = ({ value = "", placeholder = "Enter text...", className, onChange
190
190
  };
191
191
  //#endregion
192
192
  //#region src/components/dnd/panel/node.tsx
193
- var import_lib = require_lib();
194
- var import_lib$1 = /* @__PURE__ */ __toESM(require_lib$1(), 1);
193
+ var import_lib = /* @__PURE__ */ __toESM(require_lib(), 1);
195
194
  const Node = ({ data, onChange }) => {
196
195
  const idAttr = data.dataAttributes.find((a) => a.name === "data-id");
197
196
  const bindingAttr = data.dataAttributes.find((a) => a.name === "data-binding");
@@ -249,33 +248,6 @@ const Node = ({ data, onChange }) => {
249
248
  });
250
249
  };
251
250
  //#endregion
252
- //#region src/components/dnd/panel/selection.ts
253
- const removeIndices = (items, indices) => {
254
- return items.filter((_, index) => !indices.has(index));
255
- };
256
- /**
257
- * Shifts every selected index up/down by one step as a block, preserving
258
- * relative order — scattered selections stop moving individually once they
259
- * hit an unselected neighbor, so the whole group slides together instead of
260
- * items passing through each other.
261
- */
262
- const moveSelectedIndices = (items, indices, direction) => {
263
- const next = [...items];
264
- const nextIndices = new Set(indices);
265
- const ordered = [...indices].sort((a, b) => direction === "up" ? a - b : b - a);
266
- for (const index of ordered) {
267
- const target = direction === "up" ? index - 1 : index + 1;
268
- if (target < 0 || target >= next.length || nextIndices.has(target)) continue;
269
- [next[index], next[target]] = [next[target], next[index]];
270
- nextIndices.delete(index);
271
- nextIndices.add(target);
272
- }
273
- return {
274
- items: next,
275
- indices: nextIndices
276
- };
277
- };
278
- //#endregion
279
251
  //#region src/components/dnd/panel/items.tsx
280
252
  const BulkActionsBar = ({ count, onDuplicate, onMoveUp, onMoveDown, onDelete, onClear }) => {
281
253
  if (count === 0) return null;
@@ -322,33 +294,30 @@ const BulkActionsBar = ({ count, onDuplicate, onMoveUp, onMoveDown, onDelete, on
322
294
  });
323
295
  };
324
296
  const Items = ({ value, render, onChange, onChildChange }) => {
325
- const { objectItems, primitiveItems, allElements, parseError } = useMemo(() => {
297
+ const { objectItems, primitiveItems, parseError } = useMemo(() => {
326
298
  const ast = parseArrayExpression(value);
327
299
  if (!ast) return {
328
300
  objectItems: [],
329
301
  primitiveItems: [],
330
- allElements: [],
331
302
  parseError: true
332
303
  };
333
304
  const objectItems = [];
334
305
  const primitiveItems = [];
335
- const allElements = ast.elements.filter(Boolean);
336
- ast.elements.forEach((element) => {
337
- if (!element) return;
338
- if (!import_lib$1.isObjectExpression(element)) {
306
+ ast.elements.filter((element) => Boolean(element)).forEach((element, elementIndex) => {
307
+ if (!import_lib.isObjectExpression(element)) {
339
308
  const extracted = extractNodeValue(element);
340
309
  primitiveItems.push({
341
310
  id: nanoid(6),
342
311
  index: primitiveItems.length,
312
+ elementIndex,
343
313
  value: extracted.value,
344
- type: extracted.type,
345
- astNode: element
314
+ type: extracted.type
346
315
  });
347
316
  return;
348
317
  }
349
318
  const jsxBindings = {};
350
319
  element.properties.forEach((prop) => {
351
- if (!import_lib$1.isObjectProperty(prop) || !import_lib$1.isIdentifier(prop.key) || !import_lib$1.isJSXElement(prop.value)) return;
320
+ if (!import_lib.isObjectProperty(prop) || !import_lib.isIdentifier(prop.key) || !import_lib.isJSXElement(prop.value)) return;
352
321
  const propertyName = prop.key.name;
353
322
  try {
354
323
  const jsxCode = generateCode(prop.value);
@@ -369,15 +338,14 @@ const Items = ({ value, render, onChange, onChildChange }) => {
369
338
  objectItems.push({
370
339
  id: nanoid(6),
371
340
  index: objectItems.length,
341
+ elementIndex,
372
342
  editableProperties: extractObjectProperties(element),
373
- originalElement: element,
374
343
  jsxBindings
375
344
  });
376
345
  });
377
346
  return {
378
347
  objectItems,
379
348
  primitiveItems,
380
- allElements,
381
349
  parseError: false
382
350
  };
383
351
  }, [value]);
@@ -386,158 +354,73 @@ const Items = ({ value, render, onChange, onChildChange }) => {
386
354
  }, [parseError]);
387
355
  const isPrimitive = primitiveItems.length > 0 && objectItems.length === 0;
388
356
  const selection = useMultiSelect(isPrimitive ? primitiveItems.length : objectItems.length);
389
- const updatePrimitive = (index, next) => {
390
- const ast = parseArrayExpression(value);
391
- if (!ast) {
357
+ const commit = (next) => {
358
+ if (next === null) {
392
359
  Toast.error("Failed to update this item", { description: "Check the console for details." });
393
360
  return;
394
361
  }
395
- const elements = ast.elements.filter(Boolean);
396
- const item = primitiveItems.find((p) => p.index === index);
397
- if (!item) return;
398
- const newNode = createNodeFromValue(item.type, parseValue(next));
399
- if (!newNode) return;
400
- elements[index] = newNode;
401
- onChange?.(generateCode(import_lib$1.arrayExpression(elements)));
362
+ onChange?.(next);
402
363
  };
403
- const movePrimitive = (fromIndex, toIndex) => {
404
- const nextElements = [...allElements];
405
- const [moved] = nextElements.splice(fromIndex, 1);
406
- nextElements.splice(toIndex, 0, moved);
407
- onChange?.(generateCode(import_lib$1.arrayExpression(nextElements)));
364
+ const elementIndicesOf = (items, indices) => {
365
+ return new Set(items.filter((item) => indices.has(item.index)).map((item) => item.elementIndex));
366
+ };
367
+ const updatePrimitive = (elementIndex, next) => {
368
+ commit(updateArrayItemValue(value, elementIndex, next));
369
+ };
370
+ const movePrimitive = (from, to) => {
371
+ const target = primitiveItems.find((item) => item.index === to);
372
+ if (!target) return;
373
+ commit(moveArrayItem(value, from, target.elementIndex));
408
374
  };
409
375
  const deleteSelectedPrimitives = (indices) => {
410
- if (allElements.length - indices.size < 1) return;
411
- const nextElements = removeIndices(allElements, indices);
412
- onChange?.(generateCode(import_lib$1.arrayExpression(nextElements)));
376
+ commit(removeArrayItems(value, elementIndicesOf(primitiveItems, indices), "primitive"));
377
+ };
378
+ const deletePrimitive = (elementIndex) => {
379
+ commit(removeArrayItems(value, /* @__PURE__ */ new Set([elementIndex]), "primitive"));
413
380
  };
414
- const deletePrimitive = (index) => deleteSelectedPrimitives(/* @__PURE__ */ new Set([index]));
415
381
  const addPrimitive = () => {
416
- const first = primitiveItems[0];
417
- if (!first) return;
418
- const newNode = createNodeFromValue(first.type, first.value);
419
- if (!newNode) return;
420
- onChange?.(generateCode(import_lib$1.arrayExpression([...allElements, newNode])));
382
+ commit(appendArrayItem(value, "primitive"));
421
383
  };
422
384
  const duplicateSelectedPrimitives = (indices) => {
423
- const clones = [...indices].sort((a, b) => a - b).map((index) => allElements[index]).filter((node) => Boolean(node)).map((node) => clone(node));
424
- if (clones.length === 0) return;
425
- onChange?.(generateCode(import_lib$1.arrayExpression([...allElements, ...clones])));
385
+ commit(duplicateArrayItems(value, elementIndicesOf(primitiveItems, indices)));
426
386
  };
427
387
  const moveSelectedPrimitives = (indices, direction) => {
428
- const { items: nextElements, indices: nextIndices } = moveSelectedIndices(allElements, indices, direction);
429
- selection.replace(nextIndices);
430
- onChange?.(generateCode(import_lib$1.arrayExpression(nextElements)));
431
- };
432
- const moveItem = (fromIndex, toIndex) => {
433
- const nextItems = [...objectItems];
434
- const [movedItem] = nextItems.splice(fromIndex, 1);
435
- nextItems.splice(toIndex, 0, movedItem);
436
- const nextValue = arrayExpressionToCode(nextItems.map((item) => item.originalElement));
437
- onChange?.(nextValue);
438
- };
439
- const updateProperty = (itemIndex, propertyKey, value) => {
440
- const item = objectItems[itemIndex];
441
- const property = item.editableProperties[propertyKey];
442
- const renderLeaf = render?.[propertyKey] && "type" in render[propertyKey] ? render[propertyKey] : null;
443
- const isJsx = renderLeaf?.type === "jsx";
444
- const isInnerHTML = renderLeaf?.property === BINDING_PROP.INNER_HTML;
445
- let nextAstValue = null;
446
- if (isInnerHTML) {
447
- const str = String(value);
448
- nextAstValue = import_lib$1.templateLiteral([import_lib$1.templateElement({
449
- raw: str,
450
- cooked: str
451
- }, true)], []);
452
- } else if (property.type === "array" || property.type === "object") try {
453
- nextAstValue = (0, import_lib.parseExpression)(String(value), { plugins: ["jsx", "typescript"] });
454
- } catch {
388
+ const result = moveArrayItems(value, elementIndicesOf(primitiveItems, indices), direction);
389
+ if (!result) {
390
+ commit(null);
455
391
  return;
456
392
  }
457
- else if (!isJsx) nextAstValue = createNodeFromValue(property.type, value);
458
- if (!isJsx && !nextAstValue) return;
459
- const targetProperty = item.originalElement.properties.find((prop) => import_lib$1.isObjectProperty(prop) && import_lib$1.isIdentifier(prop.key) && prop.key.name === propertyKey);
460
- const jsxPlaceholders = /* @__PURE__ */ new Map();
461
- const originalValues = /* @__PURE__ */ new Map();
462
- if (isJsx && targetProperty) {
463
- const trimmed = String(value).trim();
464
- if (trimmed.startsWith("<")) {
465
- const placeholder = `__JSX_${nanoid(6)}__`;
466
- jsxPlaceholders.set(placeholder, trimmed);
467
- targetProperty.value = import_lib$1.identifier(placeholder);
468
- } else targetProperty.value = import_lib$1.stringLiteral(trimmed);
469
- } else if (targetProperty && nextAstValue) targetProperty.value = nextAstValue;
470
- objectItems.forEach((obj) => {
471
- obj.originalElement.properties.forEach((prop) => {
472
- if (!import_lib$1.isObjectProperty(prop) || !import_lib$1.isIdentifier(prop.key)) return;
473
- if (import_lib$1.isJSXElement(prop.value) || import_lib$1.isJSXFragment(prop.value)) {
474
- const placeholder = `__JSX_${nanoid(6)}__`;
475
- jsxPlaceholders.set(placeholder, generateCode(prop.value));
476
- originalValues.set(prop, prop.value);
477
- prop.value = import_lib$1.identifier(placeholder);
478
- }
479
- });
480
- });
481
- let nextValue = arrayExpressionToCode(objectItems.map((item) => item.originalElement));
482
- for (const [prop, original] of originalValues) prop.value = original;
483
- for (const [placeholder, code] of jsxPlaceholders) nextValue = nextValue.replace(placeholder, () => code);
484
- onChange?.(nextValue);
393
+ selection.replace(result.indices);
394
+ commit(result.code);
395
+ };
396
+ const moveItem = (from, to) => {
397
+ const target = objectItems.find((item) => item.index === to);
398
+ if (!target) return;
399
+ commit(moveArrayItem(value, from, target.elementIndex));
400
+ };
401
+ const updateProperty = (elementIndex, propertyKey, next) => {
402
+ commit(updateArrayItemProperty(value, elementIndex, propertyKey, next, render));
485
403
  };
486
404
  const deleteSelectedItems = (indices) => {
487
- if (objectItems.length - indices.size < 1) return;
488
- const nextItems = removeIndices(objectItems, indices);
489
- const nextValue = arrayExpressionToCode(nextItems.map((item) => item.originalElement));
490
- onChange?.(nextValue);
405
+ commit(removeArrayItems(value, elementIndicesOf(objectItems, indices), "object"));
491
406
  };
492
- const deleteItem = (index) => deleteSelectedItems(/* @__PURE__ */ new Set([index]));
493
- const cloneObjectItemElement = (source) => {
494
- const clonedElement = clone(source.originalElement);
495
- clonedElement.properties.forEach((prop) => {
496
- if (import_lib$1.isObjectProperty(prop) && import_lib$1.isIdentifier(prop.key)) {
497
- const key = prop.key.name;
498
- const editableProp = source.editableProperties[key];
499
- if (key === "key" && import_lib$1.isStringLiteral(prop.value)) {
500
- const uniqueKey = `${prop.value.value}-${nanoid(6)}`;
501
- prop.value = import_lib$1.stringLiteral(uniqueKey);
502
- return;
503
- }
504
- if (editableProp) {
505
- const nextValue = createNodeFromValue(editableProp.type, editableProp.value);
506
- if (nextValue) prop.value = nextValue;
507
- }
508
- }
509
- });
510
- return clonedElement;
407
+ const deleteItem = (elementIndex) => {
408
+ commit(removeArrayItems(value, /* @__PURE__ */ new Set([elementIndex]), "object"));
511
409
  };
512
410
  const addItem = () => {
513
- const firstItem = objectItems[0];
514
- if (!firstItem) return;
515
- const clonedElement = cloneObjectItemElement(firstItem);
516
- const editableProperties = extractObjectProperties(clonedElement);
517
- const nextItems = [...objectItems];
518
- const newItemData = {
519
- id: nanoid(6),
520
- index: nextItems.length,
521
- editableProperties,
522
- originalElement: clonedElement,
523
- jsxBindings: {}
524
- };
525
- nextItems.push(newItemData);
526
- const nextValue = arrayExpressionToCode(nextItems.map((item) => item.originalElement));
527
- onChange?.(nextValue);
411
+ commit(appendArrayItem(value, "object"));
528
412
  };
529
413
  const duplicateSelectedItems = (indices) => {
530
- const sources = [...indices].sort((a, b) => a - b).map((index) => objectItems[index]).filter((item) => Boolean(item));
531
- if (sources.length === 0) return;
532
- const clonedElements = sources.map(cloneObjectItemElement);
533
- const nextValue = arrayExpressionToCode([...objectItems.map((item) => item.originalElement), ...clonedElements]);
534
- onChange?.(nextValue);
414
+ commit(duplicateArrayItems(value, elementIndicesOf(objectItems, indices)));
535
415
  };
536
416
  const moveSelectedItems = (indices, direction) => {
537
- const { items: nextItems, indices: nextIndices } = moveSelectedIndices(objectItems, indices, direction);
538
- selection.replace(nextIndices);
539
- const nextValue = arrayExpressionToCode(nextItems.map((item) => item.originalElement));
540
- onChange?.(nextValue);
417
+ const result = moveArrayItems(value, elementIndicesOf(objectItems, indices), direction);
418
+ if (!result) {
419
+ commit(null);
420
+ return;
421
+ }
422
+ selection.replace(result.indices);
423
+ commit(result.code);
541
424
  };
542
425
  if (isPrimitive) return /* @__PURE__ */ jsxs("div", {
543
426
  className: "space-y-4",
@@ -589,20 +472,20 @@ const Items = ({ value, render, onChange, onChildChange }) => {
589
472
  size: "small",
590
473
  icon: /* @__PURE__ */ jsx(ArrowUp, {}),
591
474
  disabled: i === 0,
592
- onClick: () => movePrimitive(item.index, item.index - 1)
475
+ onClick: () => movePrimitive(item.elementIndex, item.index - 1)
593
476
  }),
594
477
  /* @__PURE__ */ jsx(Button, {
595
478
  size: "small",
596
479
  icon: /* @__PURE__ */ jsx(ArrowDown, {}),
597
480
  disabled: i === primitiveItems.length - 1,
598
- onClick: () => movePrimitive(item.index, item.index + 1)
481
+ onClick: () => movePrimitive(item.elementIndex, item.index + 1)
599
482
  }),
600
483
  /* @__PURE__ */ jsx(Button, {
601
484
  danger: true,
602
485
  size: "small",
603
486
  icon: /* @__PURE__ */ jsx(X, {}),
604
487
  disabled: primitiveItems.length <= 1,
605
- onClick: () => deletePrimitive(item.index)
488
+ onClick: () => deletePrimitive(item.elementIndex)
606
489
  })
607
490
  ]
608
491
  })]
@@ -613,7 +496,7 @@ const Items = ({ value, render, onChange, onChildChange }) => {
613
496
  property: item.type,
614
497
  value: item.value ?? "",
615
498
  rawValue: String(item.value ?? ""),
616
- onChange: (next) => updatePrimitive(item.index, next)
499
+ onChange: (next) => updatePrimitive(item.elementIndex, next)
617
500
  },
618
501
  onNodeChange: onChildChange
619
502
  })]
@@ -678,20 +561,20 @@ const Items = ({ value, render, onChange, onChildChange }) => {
678
561
  size: "small",
679
562
  icon: /* @__PURE__ */ jsx(ArrowUp, {}),
680
563
  disabled: item.index === 0,
681
- onClick: () => moveItem(item.index, item.index - 1)
564
+ onClick: () => moveItem(item.elementIndex, item.index - 1)
682
565
  }),
683
566
  /* @__PURE__ */ jsx(Button, {
684
567
  size: "small",
685
568
  icon: /* @__PURE__ */ jsx(ArrowDown, {}),
686
569
  disabled: item.index === objectItems.length - 1,
687
- onClick: () => moveItem(item.index, item.index + 1)
570
+ onClick: () => moveItem(item.elementIndex, item.index + 1)
688
571
  }),
689
572
  /* @__PURE__ */ jsx(Button, {
690
573
  danger: true,
691
574
  size: "small",
692
575
  icon: /* @__PURE__ */ jsx(X, {}),
693
576
  disabled: objectItems.length <= 1,
694
- onClick: () => deleteItem(item.index)
577
+ onClick: () => deleteItem(item.elementIndex)
695
578
  })
696
579
  ]
697
580
  })]
@@ -712,7 +595,7 @@ const Items = ({ value, render, onChange, onChildChange }) => {
712
595
  render: render?.[key] && "type" in render[key] ? render[key].render : render?.[key] && !("type" in render[key]) ? render[key] : void 0,
713
596
  value: parseValue(String(prop.value)),
714
597
  rawValue: String(prop.value),
715
- onChange: (next) => updateProperty(item.index, key, parseValue(next))
598
+ onChange: (next) => updateProperty(item.elementIndex, key, next)
716
599
  },
717
600
  onNodeChange: onChildChange
718
601
  })]
@@ -1688,4 +1571,4 @@ Dnd.DefaultPanel = Panel;
1688
1571
  //#endregion
1689
1572
  export { DraggableItem as a, ICON_OPTIONS as i, Panel as n, ICON_MAP as r, Dnd as t };
1690
1573
 
1691
- //# sourceMappingURL=dnd-DXI5JfwJ.js.map
1574
+ //# sourceMappingURL=dnd-Cyuif73L.js.map