@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/README.md +21 -3
- package/dist/components/content-picker/index.cjs +3 -8
- package/dist/components/content-picker/index.cjs.map +1 -1
- package/dist/components/content-picker/index.js +3 -8
- package/dist/components/content-picker/index.js.map +1 -1
- package/dist/components/document-picker/index.cjs +154 -15
- package/dist/components/document-picker/index.cjs.map +1 -1
- package/dist/components/document-picker/index.d.cts +16 -1
- package/dist/components/document-picker/index.d.ts +16 -1
- package/dist/components/document-picker/index.js +155 -16
- package/dist/components/document-picker/index.js.map +1 -1
- package/dist/index.cjs +157 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +158 -24
- package/dist/index.js.map +1 -1
- package/package.json +2 -14
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { forwardRef, useId, useState, useCallback, useRef, useMemo, useEffect } from 'react';
|
|
2
2
|
import { QueryClientProvider, QueryClient, useInfiniteQuery, keepPreviousData } from '@tanstack/react-query';
|
|
3
|
-
import { VisuallyHidden, Icon, Button, Popover, ToggleControl, Spinner, CheckboxControl } from '@wordpress/components';
|
|
3
|
+
import { VisuallyHidden, Icon, Button, Popover, ToggleControl, Spinner, CheckboxControl, TextControl } from '@wordpress/components';
|
|
4
4
|
import { __, sprintf, _n } from '@wordpress/i18n';
|
|
5
5
|
import { search, closeSmall, upload, dragHandle, caution, chevronUp, chevronDown, people } from '@wordpress/icons';
|
|
6
6
|
import styled from '@emotion/styled';
|
|
@@ -1753,10 +1753,7 @@ var useResolvedItems = (value) => {
|
|
|
1753
1753
|
nameKey,
|
|
1754
1754
|
item.id
|
|
1755
1755
|
]);
|
|
1756
|
-
out[`${item.kind}:${item.type}:${item.id}`] =
|
|
1757
|
-
record: record ?? null,
|
|
1758
|
-
resolved
|
|
1759
|
-
};
|
|
1756
|
+
out[`${item.kind}:${item.type}:${item.id}`] = resolved ? record ?? null : void 0;
|
|
1760
1757
|
}
|
|
1761
1758
|
return out;
|
|
1762
1759
|
},
|
|
@@ -1764,10 +1761,8 @@ var useResolvedItems = (value) => {
|
|
|
1764
1761
|
);
|
|
1765
1762
|
return useMemo(
|
|
1766
1763
|
() => value.map((item) => {
|
|
1767
|
-
const
|
|
1768
|
-
if (
|
|
1769
|
-
const { record, resolved } = resolvedEntry;
|
|
1770
|
-
if (!resolved) {
|
|
1764
|
+
const record = records[`${item.kind}:${item.type}:${item.id}`];
|
|
1765
|
+
if (record === void 0) {
|
|
1771
1766
|
return item;
|
|
1772
1767
|
}
|
|
1773
1768
|
if (!record) {
|
|
@@ -2844,6 +2839,54 @@ var DocMetaInline = styled.div`
|
|
|
2844
2839
|
white-space: nowrap;
|
|
2845
2840
|
}
|
|
2846
2841
|
`;
|
|
2842
|
+
var ReorderButtons = styled.span`
|
|
2843
|
+
display: none;
|
|
2844
|
+
|
|
2845
|
+
@container valu-dp-row (min-width: 340px) {
|
|
2846
|
+
display: inline-flex;
|
|
2847
|
+
}
|
|
2848
|
+
`;
|
|
2849
|
+
var LabelEditBody = styled.div`
|
|
2850
|
+
flex: 1;
|
|
2851
|
+
min-width: 0;
|
|
2852
|
+
display: flex;
|
|
2853
|
+
flex-direction: column;
|
|
2854
|
+
gap: 2px;
|
|
2855
|
+
padding: 4px 0;
|
|
2856
|
+
|
|
2857
|
+
.components-base-control,
|
|
2858
|
+
.components-text-control__input {
|
|
2859
|
+
width: 100%;
|
|
2860
|
+
min-width: 0;
|
|
2861
|
+
}
|
|
2862
|
+
|
|
2863
|
+
/* __nextHasNoMarginBottom is not honored by every bundled
|
|
2864
|
+
wp-components version — enforce the no-margin layout here. */
|
|
2865
|
+
.components-base-control.components-base-control {
|
|
2866
|
+
margin-bottom: 0;
|
|
2867
|
+
}
|
|
2868
|
+
|
|
2869
|
+
/* Doubled class beats wp-components' own input rules regardless of
|
|
2870
|
+
stylesheet order (the emotion cache may be prepended). */
|
|
2871
|
+
.components-text-control__input.components-text-control__input {
|
|
2872
|
+
font-size: ${tokens.fontSizeBody};
|
|
2873
|
+
height: 28px;
|
|
2874
|
+
min-height: 28px;
|
|
2875
|
+
padding: 2px 8px;
|
|
2876
|
+
}
|
|
2877
|
+
|
|
2878
|
+
@container valu-dp-row (min-width: ${WIDE_ROW}) {
|
|
2879
|
+
flex-direction: row;
|
|
2880
|
+
align-items: center;
|
|
2881
|
+
gap: ${tokens.spaceMd};
|
|
2882
|
+
|
|
2883
|
+
.components-base-control {
|
|
2884
|
+
flex: 0 1 340px;
|
|
2885
|
+
min-width: 180px;
|
|
2886
|
+
width: auto;
|
|
2887
|
+
}
|
|
2888
|
+
}
|
|
2889
|
+
`;
|
|
2847
2890
|
var Root3 = styled.div`
|
|
2848
2891
|
color: ${tokens.fg};
|
|
2849
2892
|
font-size: ${tokens.fontSizeBody};
|
|
@@ -3004,6 +3047,7 @@ var mapMediaToDocumentItem = (raw) => {
|
|
|
3004
3047
|
raw
|
|
3005
3048
|
};
|
|
3006
3049
|
};
|
|
3050
|
+
var displayTitle = (item) => item.label?.trim() || item.title;
|
|
3007
3051
|
var dedupeAgainst2 = (items, value, enabled) => {
|
|
3008
3052
|
if (!enabled || value.length === 0) return items;
|
|
3009
3053
|
const ids = new Set(value.map((v) => v.id));
|
|
@@ -3895,6 +3939,8 @@ var SelectedItem2 = ({
|
|
|
3895
3939
|
onRemove,
|
|
3896
3940
|
onMoveUp,
|
|
3897
3941
|
onMoveDown,
|
|
3942
|
+
enableLabelEdit = false,
|
|
3943
|
+
onLabelChange,
|
|
3898
3944
|
render
|
|
3899
3945
|
}) => {
|
|
3900
3946
|
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
|
|
@@ -3908,22 +3954,46 @@ var SelectedItem2 = ({
|
|
|
3908
3954
|
};
|
|
3909
3955
|
const isMissing = item.missing === "deleted";
|
|
3910
3956
|
const missingLabel = labels.missing ?? __("This document is no longer available.", "valu-block-components");
|
|
3957
|
+
const displayName = displayTitle(item);
|
|
3911
3958
|
const removeLabel = labels.remove ? labels.remove : sprintf(
|
|
3912
3959
|
/* translators: %s: document title */
|
|
3913
3960
|
__("Remove %s", "valu-block-components"),
|
|
3914
|
-
|
|
3961
|
+
displayName
|
|
3915
3962
|
);
|
|
3916
3963
|
const moveUpLabel = labels.moveUp ? labels.moveUp : sprintf(
|
|
3917
3964
|
/* translators: %s: document title */
|
|
3918
3965
|
__("Move %s up", "valu-block-components"),
|
|
3919
|
-
|
|
3966
|
+
displayName
|
|
3920
3967
|
);
|
|
3921
3968
|
const moveDownLabel = labels.moveDown ? labels.moveDown : sprintf(
|
|
3922
3969
|
/* translators: %s: document title */
|
|
3923
3970
|
__("Move %s down", "valu-block-components"),
|
|
3924
|
-
|
|
3971
|
+
displayName
|
|
3925
3972
|
);
|
|
3926
3973
|
const modified = showModifiedDate ? formatModified(item.modified) : "";
|
|
3974
|
+
const isLabelEdit = enableLabelEdit && !isMissing && Boolean(onLabelChange);
|
|
3975
|
+
const [labelDraft, setLabelDraft] = useState(null);
|
|
3976
|
+
const commitTimer = useRef(null);
|
|
3977
|
+
useEffect(
|
|
3978
|
+
() => () => {
|
|
3979
|
+
if (commitTimer.current) {
|
|
3980
|
+
clearTimeout(commitTimer.current);
|
|
3981
|
+
}
|
|
3982
|
+
},
|
|
3983
|
+
[]
|
|
3984
|
+
);
|
|
3985
|
+
const normalizeLabel = (value) => {
|
|
3986
|
+
const trimmed = value.trim();
|
|
3987
|
+
return trimmed === "" || trimmed === item.title ? "" : value;
|
|
3988
|
+
};
|
|
3989
|
+
const itemLabelText = labels.itemLabel ?? __("Display name", "valu-block-components");
|
|
3990
|
+
const metaParts = [item.filename];
|
|
3991
|
+
if (showFileSize && item.fileSize) {
|
|
3992
|
+
metaParts.push(formatFileSize(item.fileSize));
|
|
3993
|
+
}
|
|
3994
|
+
if (modified) {
|
|
3995
|
+
metaParts.push(modified);
|
|
3996
|
+
}
|
|
3927
3997
|
return /* @__PURE__ */ jsxs(
|
|
3928
3998
|
SelectedRow2,
|
|
3929
3999
|
{
|
|
@@ -3945,9 +4015,45 @@ var SelectedItem2 = ({
|
|
|
3945
4015
|
}
|
|
3946
4016
|
) : null,
|
|
3947
4017
|
isMissing ? /* @__PURE__ */ jsx(Icon, { icon: caution, size: 20 }) : showExtensionBadge ? /* @__PURE__ */ jsx(ExtensionBadge, { extension: item.extension }) : null,
|
|
3948
|
-
render ? render(item) : /* @__PURE__ */ jsxs(
|
|
4018
|
+
render ? render(item) : isLabelEdit ? /* @__PURE__ */ jsxs(LabelEditBody, { children: [
|
|
4019
|
+
/* @__PURE__ */ jsx(
|
|
4020
|
+
TextControl,
|
|
4021
|
+
{
|
|
4022
|
+
__nextHasNoMarginBottom: true,
|
|
4023
|
+
__next40pxDefaultSize: false,
|
|
4024
|
+
"aria-label": sprintf(
|
|
4025
|
+
/* translators: 1: display-name field label, 2: filename */
|
|
4026
|
+
"%1$s: %2$s",
|
|
4027
|
+
itemLabelText,
|
|
4028
|
+
item.filename
|
|
4029
|
+
),
|
|
4030
|
+
value: labelDraft ?? item.label ?? item.title,
|
|
4031
|
+
onChange: (value) => {
|
|
4032
|
+
setLabelDraft(value);
|
|
4033
|
+
if (commitTimer.current) {
|
|
4034
|
+
clearTimeout(commitTimer.current);
|
|
4035
|
+
}
|
|
4036
|
+
commitTimer.current = setTimeout(() => {
|
|
4037
|
+
commitTimer.current = null;
|
|
4038
|
+
onLabelChange?.(normalizeLabel(value));
|
|
4039
|
+
}, 300);
|
|
4040
|
+
},
|
|
4041
|
+
onBlur: () => {
|
|
4042
|
+
if (commitTimer.current) {
|
|
4043
|
+
clearTimeout(commitTimer.current);
|
|
4044
|
+
commitTimer.current = null;
|
|
4045
|
+
if (labelDraft !== null) {
|
|
4046
|
+
onLabelChange?.(normalizeLabel(labelDraft));
|
|
4047
|
+
}
|
|
4048
|
+
}
|
|
4049
|
+
setLabelDraft(null);
|
|
4050
|
+
}
|
|
4051
|
+
}
|
|
4052
|
+
),
|
|
4053
|
+
/* @__PURE__ */ jsx(FilenameText, { title: item.filename, children: metaParts.join(" \xB7 ") })
|
|
4054
|
+
] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
3949
4055
|
/* @__PURE__ */ jsxs(DocRowBody, { children: [
|
|
3950
|
-
/* @__PURE__ */ jsx(PrimaryText, { muted: isMissing, title:
|
|
4056
|
+
/* @__PURE__ */ jsx(PrimaryText, { muted: isMissing, title: displayName, children: isMissing ? missingLabel : displayName }),
|
|
3951
4057
|
!isMissing ? /* @__PURE__ */ jsx(FilenameText, { title: item.filename, children: item.filename }) : null
|
|
3952
4058
|
] }),
|
|
3953
4059
|
!isMissing && (showFileSize || modified) ? /* @__PURE__ */ jsxs(DocMetaInline, { children: [
|
|
@@ -3956,7 +4062,7 @@ var SelectedItem2 = ({
|
|
|
3956
4062
|
] }) : null
|
|
3957
4063
|
] }),
|
|
3958
4064
|
/* @__PURE__ */ jsxs(ActionColumn, { children: [
|
|
3959
|
-
orderable ? /* @__PURE__ */ jsxs(
|
|
4065
|
+
orderable ? /* @__PURE__ */ jsxs(ReorderButtons, { children: [
|
|
3960
4066
|
/* @__PURE__ */ jsx(
|
|
3961
4067
|
Button,
|
|
3962
4068
|
{
|
|
@@ -4008,7 +4114,7 @@ var useResolvedDocuments = (value) => {
|
|
|
4008
4114
|
"attachment",
|
|
4009
4115
|
item.id
|
|
4010
4116
|
]);
|
|
4011
|
-
out[item.id] =
|
|
4117
|
+
out[item.id] = resolved ? record ?? null : void 0;
|
|
4012
4118
|
}
|
|
4013
4119
|
return out;
|
|
4014
4120
|
},
|
|
@@ -4016,10 +4122,8 @@ var useResolvedDocuments = (value) => {
|
|
|
4016
4122
|
);
|
|
4017
4123
|
return useMemo(
|
|
4018
4124
|
() => value.map((item) => {
|
|
4019
|
-
const
|
|
4020
|
-
if (
|
|
4021
|
-
const { record, resolved } = entry;
|
|
4022
|
-
if (!resolved) return item;
|
|
4125
|
+
const record = records[item.id];
|
|
4126
|
+
if (record === void 0) return item;
|
|
4023
4127
|
if (!record) return { ...item, missing: "deleted" };
|
|
4024
4128
|
const fresh = mapMediaToDocumentItem(
|
|
4025
4129
|
record
|
|
@@ -4048,7 +4152,8 @@ var SelectedList2 = ({
|
|
|
4048
4152
|
showHeader = true,
|
|
4049
4153
|
showExtensionBadge,
|
|
4050
4154
|
showFileSize,
|
|
4051
|
-
showModifiedDate
|
|
4155
|
+
showModifiedDate,
|
|
4156
|
+
enableItemLabels = false
|
|
4052
4157
|
}) => {
|
|
4053
4158
|
const resolved = useResolvedDocuments(value);
|
|
4054
4159
|
const [activeKey, setActiveKey] = useState(null);
|
|
@@ -4063,6 +4168,22 @@ var SelectedList2 = ({
|
|
|
4063
4168
|
(key) => onChange(value.filter((i) => i.key !== key)),
|
|
4064
4169
|
[value, onChange]
|
|
4065
4170
|
);
|
|
4171
|
+
const setLabel = useCallback(
|
|
4172
|
+
(key, label) => onChange(
|
|
4173
|
+
value.map((i) => {
|
|
4174
|
+
if (i.key !== key) {
|
|
4175
|
+
return i;
|
|
4176
|
+
}
|
|
4177
|
+
if (label === "") {
|
|
4178
|
+
const rest = { ...i };
|
|
4179
|
+
delete rest.label;
|
|
4180
|
+
return rest;
|
|
4181
|
+
}
|
|
4182
|
+
return { ...i, label };
|
|
4183
|
+
})
|
|
4184
|
+
),
|
|
4185
|
+
[value, onChange]
|
|
4186
|
+
);
|
|
4066
4187
|
const reorder = useCallback(
|
|
4067
4188
|
(from, to) => {
|
|
4068
4189
|
if (from === to) return;
|
|
@@ -4111,6 +4232,10 @@ var SelectedList2 = ({
|
|
|
4111
4232
|
onRemove: () => remove(item.key),
|
|
4112
4233
|
...index > 0 ? { onMoveUp: () => reorder(index, index - 1) } : {},
|
|
4113
4234
|
...index < resolved.length - 1 ? { onMoveDown: () => reorder(index, index + 1) } : {},
|
|
4235
|
+
...enableItemLabels ? {
|
|
4236
|
+
enableLabelEdit: true,
|
|
4237
|
+
onLabelChange: (label) => setLabel(item.key, label)
|
|
4238
|
+
} : {},
|
|
4114
4239
|
...renderSelected ? { render: renderSelected } : {}
|
|
4115
4240
|
},
|
|
4116
4241
|
item.key
|
|
@@ -4133,7 +4258,7 @@ var SelectedList2 = ({
|
|
|
4133
4258
|
onDragCancel: () => setActiveKey(null),
|
|
4134
4259
|
children: [
|
|
4135
4260
|
/* @__PURE__ */ jsx(SortableContext, { items: ids, strategy: verticalListSortingStrategy, children: /* @__PURE__ */ jsx(SelectedListEl, { role: "list", children: rows }) }),
|
|
4136
|
-
/* @__PURE__ */ jsx(DragOverlay, { dropAnimation: defaultDropAnimation, children: activeItem ? /* @__PURE__ */ jsx(DragChip, { title: activeItem
|
|
4261
|
+
/* @__PURE__ */ jsx(DragOverlay, { dropAnimation: defaultDropAnimation, children: activeItem ? /* @__PURE__ */ jsx(DragChip, { title: displayTitle(activeItem) }) : null })
|
|
4137
4262
|
]
|
|
4138
4263
|
}
|
|
4139
4264
|
)
|
|
@@ -4164,6 +4289,7 @@ var DocumentPicker = ({
|
|
|
4164
4289
|
fetchOnOpen = false,
|
|
4165
4290
|
uniqueItems = true,
|
|
4166
4291
|
enableMediaLibrary = true,
|
|
4292
|
+
enableItemLabels = false,
|
|
4167
4293
|
showExtensionBadge = true,
|
|
4168
4294
|
showFileSize = true,
|
|
4169
4295
|
showModifiedDate = false,
|
|
@@ -4255,7 +4381,14 @@ var DocumentPicker = ({
|
|
|
4255
4381
|
...allowedExtensions !== void 0 ? { allowedExtensions } : {},
|
|
4256
4382
|
multiple: false,
|
|
4257
4383
|
value,
|
|
4258
|
-
onSelect: (items) =>
|
|
4384
|
+
onSelect: (items) => {
|
|
4385
|
+
const next = items[items.length - 1];
|
|
4386
|
+
const prev = value[0];
|
|
4387
|
+
const keepLabel = prev?.id === next.id && prev.label !== void 0;
|
|
4388
|
+
handleChange([
|
|
4389
|
+
keepLabel ? { ...next, label: prev.label } : next
|
|
4390
|
+
]);
|
|
4391
|
+
},
|
|
4259
4392
|
labels,
|
|
4260
4393
|
variant: "tertiary",
|
|
4261
4394
|
label: labels.replace
|
|
@@ -4273,7 +4406,8 @@ var DocumentPicker = ({
|
|
|
4273
4406
|
showHeader: value.length > 0,
|
|
4274
4407
|
showExtensionBadge,
|
|
4275
4408
|
showFileSize,
|
|
4276
|
-
showModifiedDate
|
|
4409
|
+
showModifiedDate,
|
|
4410
|
+
enableItemLabels
|
|
4277
4411
|
}
|
|
4278
4412
|
)
|
|
4279
4413
|
] }) }) });
|