@valu/block-components 0.3.1 → 0.5.0

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.cjs CHANGED
@@ -1761,10 +1761,7 @@ var useResolvedItems = (value) => {
1761
1761
  nameKey,
1762
1762
  item.id
1763
1763
  ]);
1764
- out[`${item.kind}:${item.type}:${item.id}`] = {
1765
- record: record ?? null,
1766
- resolved
1767
- };
1764
+ out[`${item.kind}:${item.type}:${item.id}`] = resolved ? record ?? null : void 0;
1768
1765
  }
1769
1766
  return out;
1770
1767
  },
@@ -1772,10 +1769,8 @@ var useResolvedItems = (value) => {
1772
1769
  );
1773
1770
  return react$1.useMemo(
1774
1771
  () => value.map((item) => {
1775
- const resolvedEntry = records[`${item.kind}:${item.type}:${item.id}`];
1776
- if (!resolvedEntry) return item;
1777
- const { record, resolved } = resolvedEntry;
1778
- if (!resolved) {
1772
+ const record = records[`${item.kind}:${item.type}:${item.id}`];
1773
+ if (record === void 0) {
1779
1774
  return item;
1780
1775
  }
1781
1776
  if (!record) {
@@ -2852,6 +2847,54 @@ var DocMetaInline = styled__default.default.div`
2852
2847
  white-space: nowrap;
2853
2848
  }
2854
2849
  `;
2850
+ var ReorderButtons = styled__default.default.span`
2851
+ display: none;
2852
+
2853
+ @container valu-dp-row (min-width: 340px) {
2854
+ display: inline-flex;
2855
+ }
2856
+ `;
2857
+ var LabelEditBody = styled__default.default.div`
2858
+ flex: 1;
2859
+ min-width: 0;
2860
+ display: flex;
2861
+ flex-direction: column;
2862
+ gap: 2px;
2863
+ padding: 4px 0;
2864
+
2865
+ .components-base-control,
2866
+ .components-text-control__input {
2867
+ width: 100%;
2868
+ min-width: 0;
2869
+ }
2870
+
2871
+ /* __nextHasNoMarginBottom is not honored by every bundled
2872
+ wp-components version — enforce the no-margin layout here. */
2873
+ .components-base-control.components-base-control {
2874
+ margin-bottom: 0;
2875
+ }
2876
+
2877
+ /* Doubled class beats wp-components' own input rules regardless of
2878
+ stylesheet order (the emotion cache may be prepended). */
2879
+ .components-text-control__input.components-text-control__input {
2880
+ font-size: ${tokens.fontSizeBody};
2881
+ height: 28px;
2882
+ min-height: 28px;
2883
+ padding: 2px 8px;
2884
+ }
2885
+
2886
+ @container valu-dp-row (min-width: ${WIDE_ROW}) {
2887
+ flex-direction: row;
2888
+ align-items: center;
2889
+ gap: ${tokens.spaceMd};
2890
+
2891
+ .components-base-control {
2892
+ flex: 0 1 340px;
2893
+ min-width: 180px;
2894
+ width: auto;
2895
+ }
2896
+ }
2897
+ `;
2855
2898
  var Root3 = styled__default.default.div`
2856
2899
  color: ${tokens.fg};
2857
2900
  font-size: ${tokens.fontSizeBody};
@@ -3012,6 +3055,7 @@ var mapMediaToDocumentItem = (raw) => {
3012
3055
  raw
3013
3056
  };
3014
3057
  };
3058
+ var displayTitle = (item) => item.label?.trim() || item.title;
3015
3059
  var dedupeAgainst2 = (items, value, enabled) => {
3016
3060
  if (!enabled || value.length === 0) return items;
3017
3061
  const ids = new Set(value.map((v) => v.id));
@@ -3903,6 +3947,8 @@ var SelectedItem2 = ({
3903
3947
  onRemove,
3904
3948
  onMoveUp,
3905
3949
  onMoveDown,
3950
+ enableLabelEdit = false,
3951
+ onLabelChange,
3906
3952
  render
3907
3953
  }) => {
3908
3954
  const { attributes, listeners, setNodeRef, transform, transition, isDragging } = sortable.useSortable({
@@ -3916,22 +3962,46 @@ var SelectedItem2 = ({
3916
3962
  };
3917
3963
  const isMissing = item.missing === "deleted";
3918
3964
  const missingLabel = labels.missing ?? i18n.__("This document is no longer available.", "valu-block-components");
3965
+ const displayName = displayTitle(item);
3919
3966
  const removeLabel = labels.remove ? labels.remove : i18n.sprintf(
3920
3967
  /* translators: %s: document title */
3921
3968
  i18n.__("Remove %s", "valu-block-components"),
3922
- item.title
3969
+ displayName
3923
3970
  );
3924
3971
  const moveUpLabel = labels.moveUp ? labels.moveUp : i18n.sprintf(
3925
3972
  /* translators: %s: document title */
3926
3973
  i18n.__("Move %s up", "valu-block-components"),
3927
- item.title
3974
+ displayName
3928
3975
  );
3929
3976
  const moveDownLabel = labels.moveDown ? labels.moveDown : i18n.sprintf(
3930
3977
  /* translators: %s: document title */
3931
3978
  i18n.__("Move %s down", "valu-block-components"),
3932
- item.title
3979
+ displayName
3933
3980
  );
3934
3981
  const modified = showModifiedDate ? formatModified(item.modified) : "";
3982
+ const isLabelEdit = enableLabelEdit && !isMissing && Boolean(onLabelChange);
3983
+ const [labelDraft, setLabelDraft] = react$1.useState(null);
3984
+ const commitTimer = react$1.useRef(null);
3985
+ react$1.useEffect(
3986
+ () => () => {
3987
+ if (commitTimer.current) {
3988
+ clearTimeout(commitTimer.current);
3989
+ }
3990
+ },
3991
+ []
3992
+ );
3993
+ const normalizeLabel = (value) => {
3994
+ const trimmed = value.trim();
3995
+ return trimmed === "" || trimmed === item.title ? "" : value;
3996
+ };
3997
+ const itemLabelText = labels.itemLabel ?? i18n.__("Display name", "valu-block-components");
3998
+ const metaParts = [item.filename];
3999
+ if (showFileSize && item.fileSize) {
4000
+ metaParts.push(formatFileSize(item.fileSize));
4001
+ }
4002
+ if (modified) {
4003
+ metaParts.push(modified);
4004
+ }
3935
4005
  return /* @__PURE__ */ jsxRuntime.jsxs(
3936
4006
  SelectedRow2,
3937
4007
  {
@@ -3953,9 +4023,45 @@ var SelectedItem2 = ({
3953
4023
  }
3954
4024
  ) : null,
3955
4025
  isMissing ? /* @__PURE__ */ jsxRuntime.jsx(components.Icon, { icon: icons.caution, size: 20 }) : showExtensionBadge ? /* @__PURE__ */ jsxRuntime.jsx(ExtensionBadge, { extension: item.extension }) : null,
3956
- render ? render(item) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
4026
+ render ? render(item) : isLabelEdit ? /* @__PURE__ */ jsxRuntime.jsxs(LabelEditBody, { children: [
4027
+ /* @__PURE__ */ jsxRuntime.jsx(
4028
+ components.TextControl,
4029
+ {
4030
+ __nextHasNoMarginBottom: true,
4031
+ __next40pxDefaultSize: false,
4032
+ "aria-label": i18n.sprintf(
4033
+ /* translators: 1: display-name field label, 2: filename */
4034
+ "%1$s: %2$s",
4035
+ itemLabelText,
4036
+ item.filename
4037
+ ),
4038
+ value: labelDraft ?? item.label ?? item.title,
4039
+ onChange: (value) => {
4040
+ setLabelDraft(value);
4041
+ if (commitTimer.current) {
4042
+ clearTimeout(commitTimer.current);
4043
+ }
4044
+ commitTimer.current = setTimeout(() => {
4045
+ commitTimer.current = null;
4046
+ onLabelChange?.(normalizeLabel(value));
4047
+ }, 300);
4048
+ },
4049
+ onBlur: () => {
4050
+ if (commitTimer.current) {
4051
+ clearTimeout(commitTimer.current);
4052
+ commitTimer.current = null;
4053
+ if (labelDraft !== null) {
4054
+ onLabelChange?.(normalizeLabel(labelDraft));
4055
+ }
4056
+ }
4057
+ setLabelDraft(null);
4058
+ }
4059
+ }
4060
+ ),
4061
+ /* @__PURE__ */ jsxRuntime.jsx(FilenameText, { title: item.filename, children: metaParts.join(" \xB7 ") })
4062
+ ] }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
3957
4063
  /* @__PURE__ */ jsxRuntime.jsxs(DocRowBody, { children: [
3958
- /* @__PURE__ */ jsxRuntime.jsx(PrimaryText, { muted: isMissing, title: item.title, children: isMissing ? missingLabel : item.title }),
4064
+ /* @__PURE__ */ jsxRuntime.jsx(PrimaryText, { muted: isMissing, title: displayName, children: isMissing ? missingLabel : displayName }),
3959
4065
  !isMissing ? /* @__PURE__ */ jsxRuntime.jsx(FilenameText, { title: item.filename, children: item.filename }) : null
3960
4066
  ] }),
3961
4067
  !isMissing && (showFileSize || modified) ? /* @__PURE__ */ jsxRuntime.jsxs(DocMetaInline, { children: [
@@ -3964,7 +4070,7 @@ var SelectedItem2 = ({
3964
4070
  ] }) : null
3965
4071
  ] }),
3966
4072
  /* @__PURE__ */ jsxRuntime.jsxs(ActionColumn, { children: [
3967
- orderable ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
4073
+ orderable ? /* @__PURE__ */ jsxRuntime.jsxs(ReorderButtons, { children: [
3968
4074
  /* @__PURE__ */ jsxRuntime.jsx(
3969
4075
  components.Button,
3970
4076
  {
@@ -4016,7 +4122,7 @@ var useResolvedDocuments = (value) => {
4016
4122
  "attachment",
4017
4123
  item.id
4018
4124
  ]);
4019
- out[item.id] = { record: record ?? null, resolved };
4125
+ out[item.id] = resolved ? record ?? null : void 0;
4020
4126
  }
4021
4127
  return out;
4022
4128
  },
@@ -4024,10 +4130,8 @@ var useResolvedDocuments = (value) => {
4024
4130
  );
4025
4131
  return react$1.useMemo(
4026
4132
  () => value.map((item) => {
4027
- const entry = records[item.id];
4028
- if (!entry) return item;
4029
- const { record, resolved } = entry;
4030
- if (!resolved) return item;
4133
+ const record = records[item.id];
4134
+ if (record === void 0) return item;
4031
4135
  if (!record) return { ...item, missing: "deleted" };
4032
4136
  const fresh = mapMediaToDocumentItem(
4033
4137
  record
@@ -4056,7 +4160,8 @@ var SelectedList2 = ({
4056
4160
  showHeader = true,
4057
4161
  showExtensionBadge,
4058
4162
  showFileSize,
4059
- showModifiedDate
4163
+ showModifiedDate,
4164
+ enableItemLabels = false
4060
4165
  }) => {
4061
4166
  const resolved = useResolvedDocuments(value);
4062
4167
  const [activeKey, setActiveKey] = react$1.useState(null);
@@ -4071,6 +4176,22 @@ var SelectedList2 = ({
4071
4176
  (key) => onChange(value.filter((i) => i.key !== key)),
4072
4177
  [value, onChange]
4073
4178
  );
4179
+ const setLabel = react$1.useCallback(
4180
+ (key, label) => onChange(
4181
+ value.map((i) => {
4182
+ if (i.key !== key) {
4183
+ return i;
4184
+ }
4185
+ if (label === "") {
4186
+ const rest = { ...i };
4187
+ delete rest.label;
4188
+ return rest;
4189
+ }
4190
+ return { ...i, label };
4191
+ })
4192
+ ),
4193
+ [value, onChange]
4194
+ );
4074
4195
  const reorder = react$1.useCallback(
4075
4196
  (from, to) => {
4076
4197
  if (from === to) return;
@@ -4119,6 +4240,10 @@ var SelectedList2 = ({
4119
4240
  onRemove: () => remove(item.key),
4120
4241
  ...index > 0 ? { onMoveUp: () => reorder(index, index - 1) } : {},
4121
4242
  ...index < resolved.length - 1 ? { onMoveDown: () => reorder(index, index + 1) } : {},
4243
+ ...enableItemLabels ? {
4244
+ enableLabelEdit: true,
4245
+ onLabelChange: (label) => setLabel(item.key, label)
4246
+ } : {},
4122
4247
  ...renderSelected ? { render: renderSelected } : {}
4123
4248
  },
4124
4249
  item.key
@@ -4141,7 +4266,7 @@ var SelectedList2 = ({
4141
4266
  onDragCancel: () => setActiveKey(null),
4142
4267
  children: [
4143
4268
  /* @__PURE__ */ jsxRuntime.jsx(sortable.SortableContext, { items: ids, strategy: sortable.verticalListSortingStrategy, children: /* @__PURE__ */ jsxRuntime.jsx(SelectedListEl, { role: "list", children: rows }) }),
4144
- /* @__PURE__ */ jsxRuntime.jsx(core.DragOverlay, { dropAnimation: core.defaultDropAnimation, children: activeItem ? /* @__PURE__ */ jsxRuntime.jsx(DragChip, { title: activeItem.title }) : null })
4269
+ /* @__PURE__ */ jsxRuntime.jsx(core.DragOverlay, { dropAnimation: core.defaultDropAnimation, children: activeItem ? /* @__PURE__ */ jsxRuntime.jsx(DragChip, { title: displayTitle(activeItem) }) : null })
4145
4270
  ]
4146
4271
  }
4147
4272
  )
@@ -4172,6 +4297,7 @@ var DocumentPicker = ({
4172
4297
  fetchOnOpen = false,
4173
4298
  uniqueItems = true,
4174
4299
  enableMediaLibrary = true,
4300
+ enableItemLabels = false,
4175
4301
  showExtensionBadge = true,
4176
4302
  showFileSize = true,
4177
4303
  showModifiedDate = false,
@@ -4263,7 +4389,14 @@ var DocumentPicker = ({
4263
4389
  ...allowedExtensions !== void 0 ? { allowedExtensions } : {},
4264
4390
  multiple: false,
4265
4391
  value,
4266
- onSelect: (items) => handleChange([items[items.length - 1]]),
4392
+ onSelect: (items) => {
4393
+ const next = items[items.length - 1];
4394
+ const prev = value[0];
4395
+ const keepLabel = prev?.id === next.id && prev.label !== void 0;
4396
+ handleChange([
4397
+ keepLabel ? { ...next, label: prev.label } : next
4398
+ ]);
4399
+ },
4267
4400
  labels,
4268
4401
  variant: "tertiary",
4269
4402
  label: labels.replace
@@ -4281,7 +4414,8 @@ var DocumentPicker = ({
4281
4414
  showHeader: value.length > 0,
4282
4415
  showExtensionBadge,
4283
4416
  showFileSize,
4284
- showModifiedDate
4417
+ showModifiedDate,
4418
+ enableItemLabels
4285
4419
  }
4286
4420
  )
4287
4421
  ] }) }) });