@blocklet/labels 1.0.63

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/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2018-2020 ArcBlock
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,16 @@
1
+ /// <reference types="react" />
2
+ import { Label } from './types';
3
+ interface LabelsProviderProps {
4
+ fetchLabels: () => Promise<Label[]>;
5
+ children: React.ReactNode;
6
+ }
7
+ interface LabelsContextValue {
8
+ labels: Label[];
9
+ loading: boolean;
10
+ updateLabels: () => void;
11
+ getLabelsById: (ids: string[]) => Label[];
12
+ }
13
+ export declare const useLabelsContext: () => LabelsContextValue;
14
+ export declare const useLabelsUpdateOnDestroy: () => void;
15
+ export declare function LabelsProvider({ fetchLabels, children }: LabelsProviderProps): JSX.Element;
16
+ export {};
@@ -0,0 +1,7 @@
1
+ export { default as LabelTree } from './label-tree';
2
+ export { default as LabelPicker } from './label-picker';
3
+ export { default as LabelManager } from './label-manager';
4
+ export { default as Labels } from './labels';
5
+ export { transformLabels } from './utils';
6
+ export * from './context';
7
+ export * from './types';
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ import { Label } from './types';
3
+ interface LabelDeleteDialogProps {
4
+ open: boolean;
5
+ onClose: () => void;
6
+ label: Label;
7
+ onSubmit: () => void;
8
+ }
9
+ export default function LabelDeleteDialog({ open, label, onSubmit, onClose, ...rest }: LabelDeleteDialogProps): JSX.Element;
10
+ export {};
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ import { Label } from './types';
3
+ interface LabelFormDialogProps {
4
+ open: boolean;
5
+ onClose: () => void;
6
+ initialLabel: Label | null;
7
+ onSubmit: (payload: Label) => void;
8
+ }
9
+ export default function LabelFormDialog({ open, initialLabel, onSubmit, onClose, ...rest }: LabelFormDialogProps): JSX.Element;
10
+ export {};
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ import { Label, LabelDto } from './types';
3
+ interface API {
4
+ createLabel: (label: LabelDto) => Promise<void>;
5
+ updateLabel: (label: LabelDto) => Promise<void>;
6
+ deleteLabel: (id: string) => Promise<void>;
7
+ }
8
+ interface LabelManagerProps {
9
+ data: Label[];
10
+ api: API;
11
+ }
12
+ export default function LabelManager({ data, api, ...rest }: LabelManagerProps): JSX.Element;
13
+ export {};
@@ -0,0 +1,18 @@
1
+ /// <reference types="react" />
2
+ import { GroupBase } from 'react-select';
3
+ import { Label } from './types';
4
+ interface LabelPickerProps {
5
+ data: Label[];
6
+ value: string[];
7
+ onChange: (labels: Label[]) => void;
8
+ compact?: boolean;
9
+ editable?: boolean;
10
+ addon?: any;
11
+ }
12
+ declare module 'react-select/dist/declarations/src/Select' {
13
+ interface Props<Option, IsMulti extends boolean, Group extends GroupBase<Option>> {
14
+ data: Label[];
15
+ }
16
+ }
17
+ export default function LabelPicker({ data, value, editable, addon, onChange, compact, ...rest }: LabelPickerProps): JSX.Element;
18
+ export {};
@@ -0,0 +1,17 @@
1
+ /// <reference types="react" />
2
+ import { SxProps } from '@mui/system';
3
+ import { Label } from './types';
4
+ interface SharedProps {
5
+ selectable?: boolean;
6
+ selected?: string[];
7
+ onSelect?: (id: string) => void;
8
+ renderItem?: (children: React.ReactElement, label: Label) => React.ReactElement;
9
+ }
10
+ export interface LabelTreeProps extends SharedProps {
11
+ data: Label[];
12
+ height?: number;
13
+ sx?: SxProps;
14
+ rowHeight?: number;
15
+ }
16
+ export default function LabelTree({ data, selectable, selected, onSelect, rowHeight, renderItem, sx, ...rest }: LabelTreeProps): JSX.Element;
17
+ export {};
@@ -0,0 +1,11 @@
1
+ /// <reference types="react" />
2
+ import { SxProps } from '@mui/system';
3
+ import { Label } from './types';
4
+ interface LabelsProps {
5
+ labels: string[] | undefined | null;
6
+ editable?: boolean;
7
+ onChange?: (labels: Label[]) => void;
8
+ sx?: SxProps;
9
+ }
10
+ export default function Labels({ labels, editable, onChange, sx }: LabelsProps): JSX.Element | null;
11
+ export {};
@@ -0,0 +1,15 @@
1
+ export interface Label {
2
+ id: string;
3
+ name: string;
4
+ icon?: string;
5
+ color: string;
6
+ type?: 'system' | 'user-defined';
7
+ parent?: Label;
8
+ children?: Label[];
9
+ }
10
+ export interface LabelDto {
11
+ id: string;
12
+ name: string;
13
+ color: string;
14
+ parentId?: string;
15
+ }
@@ -0,0 +1,8 @@
1
+ import { LabelDto, Label } from './types';
2
+ export declare const flatten: <T extends {
3
+ children?: T[] | undefined;
4
+ }>(data: T[]) => T[];
5
+ export declare const copyTree: <T extends {
6
+ children?: T[] | undefined;
7
+ }>(data: T[]) => T[];
8
+ export declare const transformLabels: (labelDtos: LabelDto[]) => Label[];
@@ -0,0 +1,9 @@
1
+ declare module '@arcblock/ux/*';
2
+ declare module '@arcblock/did-connect/*';
3
+ interface Window {
4
+ blocklet: {
5
+ appId: string;
6
+ prefix: string;
7
+ preferences: { [key: string]: any };
8
+ };
9
+ }
@@ -0,0 +1 @@
1
+ export * from './components/label';
@@ -0,0 +1,872 @@
1
+ import { createContext as V, useRef as N, useState as I, useCallback as B, useEffect as P, useMemo as z, useContext as W } from "react";
2
+ import E from "@mui/icons-material/ExpandMore";
3
+ import m from "@mui/material/Box";
4
+ import { styled as j } from "@mui/material/styles";
5
+ import { Tree as Z } from "react-arborist";
6
+ import { Icon as $ } from "@iconify/react";
7
+ import { jsx as t, jsxs as y, Fragment as T } from "react/jsx-runtime";
8
+ import G from "lodash/omit";
9
+ import U, { components as q } from "react-select";
10
+ import Y from "@mui/material/Typography";
11
+ import A from "@mui/material/Button";
12
+ import O from "@mui/material/IconButton";
13
+ import X from "@mui/icons-material/DeleteOutlineOutlined";
14
+ import _ from "@mui/icons-material/EditOutlined";
15
+ import ee from "@mui/icons-material/Add";
16
+ import te from "react-use/lib/useSetState";
17
+ import L from "@mui/material/TextField";
18
+ import ne from "@mui/base/ClickAwayListener";
19
+ import { GithubPicker as re } from "react-color";
20
+ import F from "@arcblock/ux/lib/Dialog";
21
+ import oe from "@mui/material/Alert";
22
+ import ie from "@mui/icons-material/Edit";
23
+ import le from "@mui/icons-material/LabelOutlined";
24
+ import se from "@mui/material/ClickAwayListener";
25
+ import ae from "@mui/material/Chip";
26
+ const de = (e) => /* @__PURE__ */ t("svg", {
27
+ viewBox: "0 0 24 24",
28
+ width: "1.2em",
29
+ height: "1.2em",
30
+ ...e,
31
+ children: /* @__PURE__ */ t("path", {
32
+ fill: "currentColor",
33
+ d: "M5 19q-.825 0-1.413-.587Q3 17.825 3 17V7q0-.825.587-1.412Q4.175 5 5 5h10q.5 0 .938.225q.437.225.712.625l3.525 5q.375.525.375 1.15q0 .625-.375 1.15l-3.525 5q-.275.4-.712.625Q15.5 19 15 19Z"
34
+ })
35
+ }), ce = (e) => /* @__PURE__ */ t("svg", {
36
+ viewBox: "0 0 24 24",
37
+ width: "1.2em",
38
+ height: "1.2em",
39
+ ...e,
40
+ children: /* @__PURE__ */ t("path", {
41
+ fill: "currentColor",
42
+ d: "m10 16.4l-4-4L7.4 11l2.6 2.6L16.6 7L18 8.4Z"
43
+ })
44
+ }), R = V({});
45
+ function ue({
46
+ selectable: e = !1,
47
+ selected: n = [],
48
+ onSelect: i,
49
+ renderItem: l,
50
+ children: o
51
+ }) {
52
+ const r = (h) => n.indexOf(h) !== -1, s = (h) => {
53
+ Array.from(new Set(n).add(h)), i == null || i(h);
54
+ }, a = z(
55
+ () => ({
56
+ selectable: e,
57
+ selected: n,
58
+ isSelected: r,
59
+ select: s,
60
+ renderItem: l
61
+ }),
62
+ [n]
63
+ );
64
+ return /* @__PURE__ */ t(R.Provider, {
65
+ value: a,
66
+ children: o
67
+ });
68
+ }
69
+ const he = (e) => {
70
+ var n;
71
+ return e.isLeaf || !((n = e.data.children) != null && n.length);
72
+ };
73
+ function pe({
74
+ node: e
75
+ }) {
76
+ return he(e) ? /* @__PURE__ */ t("span", {}) : e.isOpen ? /* @__PURE__ */ t(E, {
77
+ style: {
78
+ fontSize: 20
79
+ }
80
+ }) : /* @__PURE__ */ t(E, {
81
+ style: {
82
+ fontSize: 20,
83
+ transform: "rotate(-90deg)"
84
+ }
85
+ });
86
+ }
87
+ function me({
88
+ node: e,
89
+ style: n,
90
+ dragHandle: i
91
+ }) {
92
+ const {
93
+ selectable: l,
94
+ select: o,
95
+ isSelected: r,
96
+ renderItem: s
97
+ } = W(R), {
98
+ data: a
99
+ } = e, h = a.icon ? /* @__PURE__ */ t($, {
100
+ icon: a.icon,
101
+ style: {
102
+ fontSize: 18
103
+ }
104
+ }) : /* @__PURE__ */ t(de, {
105
+ style: {
106
+ fontSize: 18,
107
+ color: a.color || "#ddd"
108
+ }
109
+ }), u = (p) => {
110
+ p.stopPropagation(), e.isInternal && e.toggle();
111
+ }, g = (p) => {
112
+ p.stopPropagation(), l && o(e.id);
113
+ }, f = /* @__PURE__ */ y(T, {
114
+ children: [/* @__PURE__ */ y(m, {
115
+ sx: {
116
+ display: "flex",
117
+ alignItems: "center",
118
+ flex: 1
119
+ },
120
+ children: [/* @__PURE__ */ t(m, {
121
+ sx: {
122
+ display: "flex",
123
+ cursor: "pointer"
124
+ },
125
+ onClick: u,
126
+ children: /* @__PURE__ */ t(pe, {
127
+ node: e
128
+ })
129
+ }), /* @__PURE__ */ t(m, {
130
+ sx: {
131
+ display: "flex",
132
+ alignItems: "center",
133
+ width: 22,
134
+ height: 22
135
+ },
136
+ children: h
137
+ }), /* @__PURE__ */ t(m, {
138
+ component: "span",
139
+ sx: {
140
+ color: "grey.700"
141
+ },
142
+ children: e.data.name
143
+ })]
144
+ }), l && r(e.id) && /* @__PURE__ */ t(ce, {})]
145
+ });
146
+ return /* @__PURE__ */ y(m, {
147
+ className: "label-tree-item",
148
+ style: n,
149
+ sx: {
150
+ display: "flex",
151
+ alignItems: "center",
152
+ justifyContent: "space-between"
153
+ },
154
+ ref: i,
155
+ onClick: g,
156
+ children: [!s && f, !!s && s(f, e.data)]
157
+ });
158
+ }
159
+ const fe = j(m)`
160
+ > div,
161
+ > div > div,
162
+ > div > div > div {
163
+ width: auto !important;
164
+ }
165
+ [role='treeitem'] {
166
+ display: flex;
167
+ align-items: stretch;
168
+ }
169
+ .label-tree-item {
170
+ width: 100%;
171
+ }
172
+ `;
173
+ function D({
174
+ data: e,
175
+ selectable: n,
176
+ selected: i = [],
177
+ onSelect: l,
178
+ rowHeight: o = 28,
179
+ renderItem: r,
180
+ sx: s,
181
+ ...a
182
+ }) {
183
+ const h = N(), u = [...Array.isArray(s) ? s : [s]], [g, f] = I(0), p = B(() => {
184
+ var S, d;
185
+ f((((d = (S = h.current) == null ? void 0 : S.visibleNodes) == null ? void 0 : d.length) || 0) * o);
186
+ }, [o]);
187
+ return P(() => {
188
+ p();
189
+ }, [e, p]), /* @__PURE__ */ t(ue, {
190
+ selectable: n,
191
+ selected: i,
192
+ onSelect: l,
193
+ renderItem: r,
194
+ children: /* @__PURE__ */ t(fe, {
195
+ ...a,
196
+ sx: u,
197
+ children: /* @__PURE__ */ t(Z, {
198
+ data: e,
199
+ rowHeight: o,
200
+ height: g,
201
+ indent: 32,
202
+ ref: h,
203
+ onToggle: () => {
204
+ setTimeout(p);
205
+ },
206
+ children: me
207
+ })
208
+ })
209
+ });
210
+ }
211
+ const M = (e) => e.reduce((n, i) => {
212
+ var l;
213
+ return n.push(i), (l = i.children) != null && l.length && n.push(...M(i.children)), n;
214
+ }, []), H = (e) => e.map((n) => {
215
+ var i;
216
+ return (i = n.children) != null && i.length && (n.children = H(n.children)), n;
217
+ }), ge = (e) => {
218
+ const n = e.reduce((o, r) => (o[r.id] = o[r.id] || {}, r.parentId && (o[r.id].parent = r.parentId, o[r.parentId] = o[r.parentId] || {}, o[r.parentId].children = o[r.parentId].children || [], o[r.parentId].children.push(r.id)), o), {}), i = e.map(({ parentId: o, ...r }) => ({ ...r, children: [] })), l = i.reduce((o, r) => ({ ...o, [r.id]: r }), {});
219
+ return i.forEach((o) => {
220
+ const r = n[o.id];
221
+ r.parent && l[r.parent] && (o.parent = l[r.parent]), r.children && (o.children = r.children.map((s) => l[s]));
222
+ }), i.filter((o) => !o.parent);
223
+ }, xe = (e) => {
224
+ const {
225
+ options: n,
226
+ getValue: i,
227
+ selectProps: l,
228
+ selectOption: o
229
+ } = e, r = i(), s = (a) => {
230
+ const h = n.find((u) => u.data.id === a);
231
+ h && o(h);
232
+ };
233
+ return /* @__PURE__ */ y(T, {
234
+ children: [/* @__PURE__ */ t(q.MenuList, {
235
+ ...G(e, ["addon"]),
236
+ children: /* @__PURE__ */ t(m, {
237
+ sx: {
238
+ px: 2,
239
+ py: 1
240
+ },
241
+ children: /* @__PURE__ */ t(D, {
242
+ data: l.data,
243
+ selectable: !0,
244
+ onSelect: s,
245
+ selected: r.map((a) => a.data.id)
246
+ })
247
+ })
248
+ }), e.addon]
249
+ });
250
+ }, ye = ({
251
+ children: e,
252
+ ...n
253
+ }) => {
254
+ const [i, l] = e, o = Array.isArray(i) ? i.length : 0;
255
+ return /* @__PURE__ */ y(q.ValueContainer, {
256
+ ...n,
257
+ children: [/* @__PURE__ */ t(m, {
258
+ component: "span",
259
+ sx: {
260
+ fontSize: 13,
261
+ fontWeight: "bold"
262
+ },
263
+ children: o ? "Labels" : "Filter by labels"
264
+ }), !!o && /* @__PURE__ */ t(m, {
265
+ sx: {
266
+ display: "inline-flex",
267
+ justifyContent: "center",
268
+ alignItems: "center",
269
+ width: 22,
270
+ height: 22,
271
+ ml: 1,
272
+ borderRadius: "100%",
273
+ fontSize: 12,
274
+ bgcolor: "grey.200"
275
+ },
276
+ children: o
277
+ }), l]
278
+ });
279
+ }, Ce = {
280
+ control: (e) => ({
281
+ ...e,
282
+ minHeight: 31
283
+ }),
284
+ valueContainer: (e) => ({
285
+ ...e,
286
+ display: "flex"
287
+ }),
288
+ dropdownIndicator: (e) => ({
289
+ ...e,
290
+ padding: "0 4px"
291
+ }),
292
+ clearIndicator: (e) => ({
293
+ ...e,
294
+ padding: "4px"
295
+ })
296
+ };
297
+ function ve({
298
+ data: e,
299
+ value: n = [],
300
+ editable: i = !1,
301
+ addon: l,
302
+ onChange: o,
303
+ compact: r,
304
+ ...s
305
+ }) {
306
+ const a = N(null), h = z(() => M(e).map((d) => ({
307
+ data: d,
308
+ label: d.name,
309
+ value: d.id
310
+ })), [e]), u = h.reduce((d, b) => ({
311
+ ...d,
312
+ [b.data.id]: b
313
+ }), {}), g = Array.isArray(n) ? n : [n], f = g.map((d) => u[d]), p = N({
314
+ prev: g.map((d) => u[d].data)
315
+ }), v = (d, b) => {
316
+ p.current.current = d.map((k) => k.data), ["clear", "remove-value"].includes(b.action) && a.current && setTimeout(() => {
317
+ var k;
318
+ return (k = a == null ? void 0 : a.current) == null ? void 0 : k.blur();
319
+ }, 1);
320
+ }, S = () => {
321
+ p.current.current && JSON.stringify(p.current.prev.map((b) => b.id)) !== JSON.stringify(p.current.current.map((b) => b.id)) && (o(p.current.current), p.current.prev = p.current.current);
322
+ };
323
+ return /* @__PURE__ */ t(m, {
324
+ ...s,
325
+ children: /* @__PURE__ */ t(U, {
326
+ ref: a,
327
+ defaultValue: f,
328
+ options: h,
329
+ onChange: v,
330
+ components: {
331
+ MenuList: (d) => /* @__PURE__ */ t(xe, {
332
+ ...d,
333
+ addon: l
334
+ }),
335
+ ...r && {
336
+ ValueContainer: ye
337
+ }
338
+ },
339
+ placeholder: "Select labels",
340
+ styles: {
341
+ ...r && Ce,
342
+ menu: (d) => ({
343
+ ...d,
344
+ ...i && {
345
+ paddingBottom: "36px"
346
+ },
347
+ zIndex: 99
348
+ }),
349
+ menuList: (d) => ({
350
+ ...d,
351
+ overflowY: "auto",
352
+ maxHeight: "264px"
353
+ })
354
+ },
355
+ theme: (d) => ({
356
+ ...d,
357
+ colors: {
358
+ ...d.colors,
359
+ primary25: "#ddd",
360
+ primary50: "#ddd",
361
+ primary: "#ddd"
362
+ }
363
+ }),
364
+ isSearchable: !1,
365
+ isMulti: !0,
366
+ closeMenuOnSelect: !1,
367
+ onMenuClose: S,
368
+ data: e
369
+ })
370
+ });
371
+ }
372
+ function be({
373
+ open: e,
374
+ initialLabel: n,
375
+ onSubmit: i,
376
+ onClose: l,
377
+ ...o
378
+ }) {
379
+ const r = !(n != null && n.id), [s, a] = te({
380
+ name: (n == null ? void 0 : n.name) || "",
381
+ color: (n == null ? void 0 : n.color) || "#ddd",
382
+ slug: (n == null ? void 0 : n.id) || ""
383
+ }), h = (d) => /^#([0-9A-F]{3}){1,2}$/i.test(d), u = z(() => r ? s.name && s.color && s.slug && h(s.color) : s.name && s.color && h(s.color), [s, r]), [g, f] = I(!1), p = () => {
384
+ i({
385
+ ...n,
386
+ name: s.name,
387
+ color: s.color,
388
+ id: s.slug
389
+ });
390
+ }, v = (d) => {
391
+ a({
392
+ color: d.hex
393
+ }), setTimeout(() => f(!1));
394
+ }, S = (d) => {
395
+ a({
396
+ color: d.target.value
397
+ });
398
+ };
399
+ return /* @__PURE__ */ t(F, {
400
+ open: e,
401
+ showCloseButton: !0,
402
+ maxWidth: "lg",
403
+ title: r ? "Create label" : "Edit label",
404
+ actions: /* @__PURE__ */ y(T, {
405
+ children: [/* @__PURE__ */ t(A, {
406
+ color: "inherit",
407
+ variant: "contained",
408
+ size: "small",
409
+ onClick: l,
410
+ children: "Cancel"
411
+ }), /* @__PURE__ */ t(A, {
412
+ color: "primary",
413
+ variant: "contained",
414
+ size: "small",
415
+ onClick: p,
416
+ disabled: !u,
417
+ children: r ? "Create" : "Save Changes"
418
+ })]
419
+ }),
420
+ onClose: l,
421
+ ...o,
422
+ children: /* @__PURE__ */ y(m, {
423
+ width: 600,
424
+ minHeight: 280,
425
+ children: [/* @__PURE__ */ t(m, {
426
+ sx: {
427
+ position: "relative",
428
+ zIndex: 9999
429
+ },
430
+ children: /* @__PURE__ */ t(L, {
431
+ label: "Name",
432
+ value: s.name,
433
+ placeholder: "New label",
434
+ size: "small",
435
+ fullWidth: !0,
436
+ onChange: (d) => a({
437
+ name: d.target.value
438
+ })
439
+ })
440
+ }), /* @__PURE__ */ t(L, {
441
+ label: "Slug",
442
+ value: s.slug,
443
+ size: "small",
444
+ fullWidth: !0,
445
+ disabled: !r,
446
+ onChange: (d) => a({
447
+ slug: d.target.value
448
+ }),
449
+ sx: {
450
+ mt: 2
451
+ }
452
+ }), (n == null ? void 0 : n.parent) && /* @__PURE__ */ t(L, {
453
+ label: "Parent",
454
+ value: n.parent.name,
455
+ size: "small",
456
+ fullWidth: !0,
457
+ disabled: !0,
458
+ sx: {
459
+ mt: 2
460
+ }
461
+ }), /* @__PURE__ */ t(ne, {
462
+ onClickAway: () => f(!1),
463
+ children: /* @__PURE__ */ y(m, {
464
+ sx: {
465
+ position: "relative",
466
+ mt: 2
467
+ },
468
+ children: [/* @__PURE__ */ y(m, {
469
+ sx: {
470
+ display: "flex",
471
+ alignItems: "center",
472
+ gap: 1
473
+ },
474
+ children: [/* @__PURE__ */ t(m, {
475
+ sx: {
476
+ width: 30,
477
+ height: 30,
478
+ bgcolor: s.color,
479
+ borderRadius: 1
480
+ },
481
+ onClick: () => f(!0)
482
+ }), /* @__PURE__ */ t(L, {
483
+ label: "",
484
+ value: s.color,
485
+ size: "small",
486
+ onChange: S,
487
+ inputProps: {
488
+ maxLength: 7
489
+ },
490
+ sx: {
491
+ width: 100,
492
+ ".MuiInputBase-root": {
493
+ height: 32
494
+ }
495
+ }
496
+ })]
497
+ }), g && /* @__PURE__ */ t(m, {
498
+ sx: {
499
+ position: "absolute",
500
+ top: 48,
501
+ zIndex: 1
502
+ },
503
+ children: /* @__PURE__ */ t(re, {
504
+ color: s.color,
505
+ onChangeComplete: v
506
+ })
507
+ })]
508
+ })
509
+ })]
510
+ })
511
+ });
512
+ }
513
+ function we({
514
+ open: e,
515
+ label: n,
516
+ onSubmit: i,
517
+ onClose: l,
518
+ ...o
519
+ }) {
520
+ return /* @__PURE__ */ t(F, {
521
+ open: e,
522
+ showCloseButton: !0,
523
+ maxWidth: "md",
524
+ title: "Delete",
525
+ actions: /* @__PURE__ */ y(T, {
526
+ children: [/* @__PURE__ */ t(A, {
527
+ color: "inherit",
528
+ variant: "contained",
529
+ size: "small",
530
+ onClick: l,
531
+ children: "Cancel"
532
+ }), /* @__PURE__ */ t(A, {
533
+ color: "primary",
534
+ variant: "contained",
535
+ size: "small",
536
+ onClick: i,
537
+ children: "Delete"
538
+ })]
539
+ }),
540
+ onClose: l,
541
+ ...o,
542
+ children: /* @__PURE__ */ y(m, {
543
+ width: 600,
544
+ minHeight: 280,
545
+ children: [/* @__PURE__ */ t(oe, {
546
+ severity: "info",
547
+ sx: {
548
+ mb: 2
549
+ },
550
+ children: "Are you sure you want to delete these labels ?"
551
+ }), /* @__PURE__ */ t(D, {
552
+ data: [n]
553
+ })]
554
+ })
555
+ });
556
+ }
557
+ const Se = j(D)`
558
+ .label-tree-item {
559
+ &:before {
560
+ content: '';
561
+ position: absolute;
562
+ top: 0;
563
+ left: 0;
564
+ right: 0;
565
+ border-top: 1px solid #ddd;
566
+ }
567
+ }
568
+ & [role='treeitem']:first-of-type > .label-tree-item {
569
+ &:before {
570
+ display: none;
571
+ }
572
+ }
573
+ [role='treeitem'] {
574
+ box-sizing: border-box;
575
+ padding: 0 16px;
576
+ }
577
+ `;
578
+ function Ge({
579
+ data: e,
580
+ api: n,
581
+ ...i
582
+ }) {
583
+ const [l, o] = I(null), [r, s] = I(null), [a, h] = I(H(e)), u = z(() => M(a), [a]), g = z(() => {
584
+ const c = (x, w) => x.map((C) => (w && (C.parent = w), C.children && (C.children = c(C.children, C)), C));
585
+ return c(a);
586
+ }, [a]), f = (c) => {
587
+ var x, w;
588
+ c.parent ? (c.parent.children = (w = (x = c.parent) == null ? void 0 : x.children) == null ? void 0 : w.map((C) => C.id === c.id ? {
589
+ ...c
590
+ } : C), f(c.parent)) : h(a.map((C) => C.id === c.id ? {
591
+ ...c
592
+ } : C));
593
+ }, p = (c) => {
594
+ c.parent ? (c.parent.children = c.parent.children || [], c.parent.children.push(c), h([...a])) : h([...a, c]);
595
+ }, v = (c) => {
596
+ var x;
597
+ c.parent ? c.parent.children = (x = c.parent.children) == null ? void 0 : x.filter((w) => w.id !== c.id) : h(a.filter((w) => w.id !== c.id));
598
+ }, S = (c) => {
599
+ if (l)
600
+ return;
601
+ o({
602
+ id: "",
603
+ name: "New label",
604
+ parent: c,
605
+ color: "#dddddd"
606
+ });
607
+ }, d = (c) => {
608
+ l || o(c);
609
+ }, b = async (c) => {
610
+ if (!l)
611
+ return;
612
+ const {
613
+ parent: x,
614
+ children: w,
615
+ ...C
616
+ } = c;
617
+ l.id ? (await n.updateLabel({
618
+ ...C,
619
+ parentId: x == null ? void 0 : x.id
620
+ }), f(c)) : (await n.createLabel({
621
+ ...C,
622
+ parentId: x == null ? void 0 : x.id
623
+ }), p(c)), o(null);
624
+ }, k = async () => {
625
+ !r || (await n.deleteLabel(r.id), v(r), s(null));
626
+ }, J = (c, x) => /* @__PURE__ */ y(T, {
627
+ children: [c, /* @__PURE__ */ y(m, {
628
+ sx: {
629
+ display: "flex",
630
+ gap: 1,
631
+ flex: "0 0 auto"
632
+ },
633
+ children: [/* @__PURE__ */ t(O, {
634
+ color: "inherit",
635
+ size: "small",
636
+ sx: {
637
+ color: "grey.500"
638
+ },
639
+ onClick: () => S(x),
640
+ children: /* @__PURE__ */ t(ee, {
641
+ sx: {
642
+ fontSize: 20
643
+ }
644
+ })
645
+ }), /* @__PURE__ */ t(O, {
646
+ color: "inherit",
647
+ size: "small",
648
+ sx: {
649
+ color: "grey.500"
650
+ },
651
+ onClick: () => d(x),
652
+ children: /* @__PURE__ */ t(_, {
653
+ sx: {
654
+ fontSize: 18
655
+ }
656
+ })
657
+ }), /* @__PURE__ */ t(O, {
658
+ color: "inherit",
659
+ size: "small",
660
+ sx: {
661
+ color: "grey.500"
662
+ },
663
+ onClick: () => s(x),
664
+ children: /* @__PURE__ */ t(X, {
665
+ sx: {
666
+ fontSize: 20
667
+ }
668
+ })
669
+ })]
670
+ })]
671
+ });
672
+ return /* @__PURE__ */ y(m, {
673
+ ...i,
674
+ children: [/* @__PURE__ */ y(m, {
675
+ sx: {
676
+ display: "flex",
677
+ alignItems: "center",
678
+ justifyContent: "space-between"
679
+ },
680
+ children: [/* @__PURE__ */ t(Y, {
681
+ component: "h2",
682
+ variant: "h6",
683
+ children: "Manage labels"
684
+ }), /* @__PURE__ */ t(A, {
685
+ color: "primary",
686
+ variant: "contained",
687
+ size: "small",
688
+ sx: {
689
+ textTransform: "none"
690
+ },
691
+ onClick: () => S(),
692
+ children: "New label"
693
+ })]
694
+ }), /* @__PURE__ */ y(m, {
695
+ sx: {
696
+ mt: 2,
697
+ border: 1,
698
+ borderColor: "grey.300",
699
+ borderRadius: 1,
700
+ overflow: "hidden"
701
+ },
702
+ children: [/* @__PURE__ */ y(m, {
703
+ sx: {
704
+ p: 2,
705
+ fontSize: 14,
706
+ fontWeight: "bold",
707
+ bgcolor: "grey.200"
708
+ },
709
+ children: [u.length, " labels"]
710
+ }), /* @__PURE__ */ t(m, {
711
+ sx: {
712
+ pt: 0.5
713
+ },
714
+ children: /* @__PURE__ */ t(Se, {
715
+ data: g,
716
+ selected: [],
717
+ renderItem: J,
718
+ rowHeight: 64
719
+ })
720
+ })]
721
+ }), /* @__PURE__ */ t(be, {
722
+ open: !!l,
723
+ onClose: () => o(null),
724
+ initialLabel: l,
725
+ onSubmit: b
726
+ }, l == null ? void 0 : l.id), /* @__PURE__ */ t(we, {
727
+ open: !!r,
728
+ onClose: () => s(null),
729
+ label: r,
730
+ onSubmit: k
731
+ }, r == null ? void 0 : r.id)]
732
+ });
733
+ }
734
+ const K = V({}), Q = () => W(K), Ue = () => {
735
+ const {
736
+ updateLabels: e
737
+ } = Q();
738
+ P(() => () => {
739
+ e();
740
+ }, []);
741
+ };
742
+ function Ye({
743
+ fetchLabels: e,
744
+ children: n
745
+ }) {
746
+ const [i, l] = I({
747
+ loading: !0,
748
+ labels: [],
749
+ updateCounter: 1
750
+ });
751
+ P(() => {
752
+ (async () => {
753
+ try {
754
+ l((f) => ({
755
+ ...f,
756
+ loading: !0
757
+ }));
758
+ const g = await e();
759
+ l((f) => ({
760
+ ...f,
761
+ loading: !1,
762
+ labels: ge(g)
763
+ }));
764
+ } catch (g) {
765
+ console.error(g), l((f) => ({
766
+ ...f,
767
+ loading: !1,
768
+ labels: []
769
+ }));
770
+ }
771
+ })();
772
+ }, [i.updateCounter]);
773
+ const o = z(() => {
774
+ var g;
775
+ return (g = i.labels) != null && g.length ? M(i.labels).reduce((f, p) => ({
776
+ ...f,
777
+ [p.id]: p
778
+ }), {}) : {};
779
+ }, [i.labels]), r = B((u) => u.map((g) => o[g]), [o]), s = B((u) => (u == null ? void 0 : u.split(",")) || [], []), a = B((u = []) => {
780
+ if (!!(u != null && u.length))
781
+ return u.join(",");
782
+ }, []), h = z(() => ({
783
+ loading: i.loading,
784
+ labels: i.labels || [],
785
+ updateLabels: () => l((u) => ({
786
+ ...u,
787
+ updateCounter: ++i.updateCounter
788
+ })),
789
+ getLabelsById: r,
790
+ parseLabelIds: s,
791
+ stringifyLabelIds: a
792
+ }), [i, r, s, a]);
793
+ return /* @__PURE__ */ t(K.Provider, {
794
+ value: h,
795
+ children: n
796
+ });
797
+ }
798
+ function Xe({
799
+ labels: e,
800
+ editable: n,
801
+ onChange: i,
802
+ sx: l
803
+ }) {
804
+ const o = !(e != null && e.length), {
805
+ labels: r,
806
+ loading: s,
807
+ getLabelsById: a
808
+ } = Q(), [h, u] = I(!1);
809
+ if (s || !n && o)
810
+ return null;
811
+ const g = (v) => {
812
+ i == null || i(v), setTimeout(() => {
813
+ u(!1);
814
+ }, 300);
815
+ };
816
+ if (h)
817
+ return /* @__PURE__ */ t(se, {
818
+ onClickAway: () => h && u(!1),
819
+ children: /* @__PURE__ */ t("div", {
820
+ children: /* @__PURE__ */ t(ve, {
821
+ data: r,
822
+ value: e || [],
823
+ onChange: g
824
+ })
825
+ })
826
+ });
827
+ const f = a(e || []), p = [{
828
+ display: "flex",
829
+ gap: 1,
830
+ alignItems: "center",
831
+ flexWrap: "wrap"
832
+ }, ...Array.isArray(l) ? l : [l]];
833
+ return /* @__PURE__ */ y(m, {
834
+ sx: p,
835
+ children: [f.map((v) => v ? /* @__PURE__ */ t(ae, {
836
+ label: v.name,
837
+ variant: "filled",
838
+ size: "small",
839
+ sx: {
840
+ borderRadius: 1
841
+ }
842
+ }, v.id) : null), n && !o && /* @__PURE__ */ t(O, {
843
+ color: "inherit",
844
+ size: "small",
845
+ onClick: () => u(!0),
846
+ sx: {
847
+ color: "grey.400"
848
+ },
849
+ children: /* @__PURE__ */ t(ie, {
850
+ sx: {
851
+ fontSize: 20
852
+ }
853
+ })
854
+ }), n && o && /* @__PURE__ */ t(A, {
855
+ color: "inherit",
856
+ variant: "outlined",
857
+ startIcon: /* @__PURE__ */ t(le, {}),
858
+ onClick: () => u(!0),
859
+ children: "Edit labels"
860
+ })]
861
+ });
862
+ }
863
+ export {
864
+ Ge as LabelManager,
865
+ ve as LabelPicker,
866
+ D as LabelTree,
867
+ Xe as Labels,
868
+ Ye as LabelsProvider,
869
+ ge as transformLabels,
870
+ Q as useLabelsContext,
871
+ Ue as useLabelsUpdateOnDestroy
872
+ };
@@ -0,0 +1,34 @@
1
+ (function(s,f){typeof exports=="object"&&typeof module<"u"?f(exports,require("react"),require("@mui/icons-material/ExpandMore"),require("@mui/material/Box"),require("@mui/material/styles"),require("react-arborist"),require("@iconify/react"),require("react/jsx-runtime"),require("lodash/omit"),require("react-select"),require("@mui/material/Typography"),require("@mui/material/Button"),require("@mui/material/IconButton"),require("@mui/icons-material/DeleteOutlineOutlined"),require("@mui/icons-material/EditOutlined"),require("@mui/icons-material/Add"),require("react-use/lib/useSetState"),require("@mui/material/TextField"),require("@mui/base/ClickAwayListener"),require("react-color"),require("@arcblock/ux/lib/Dialog"),require("@mui/material/Alert"),require("@mui/icons-material/Edit"),require("@mui/icons-material/LabelOutlined"),require("@mui/material/ClickAwayListener"),require("@mui/material/Chip")):typeof define=="function"&&define.amd?define(["exports","react","@mui/icons-material/ExpandMore","@mui/material/Box","@mui/material/styles","react-arborist","@iconify/react","react/jsx-runtime","lodash/omit","react-select","@mui/material/Typography","@mui/material/Button","@mui/material/IconButton","@mui/icons-material/DeleteOutlineOutlined","@mui/icons-material/EditOutlined","@mui/icons-material/Add","react-use/lib/useSetState","@mui/material/TextField","@mui/base/ClickAwayListener","react-color","@arcblock/ux/lib/Dialog","@mui/material/Alert","@mui/icons-material/Edit","@mui/icons-material/LabelOutlined","@mui/material/ClickAwayListener","@mui/material/Chip"],f):(s=typeof globalThis<"u"?globalThis:s||self,f(s.DiscussKitComponents={},s.React,s.ExpandMoreIcon,s.Box,s.styles,s.reactArborist,s.react$1,s.jsxRuntime,s.omit,s.Select,s.Typography,s.Button,s.IconButton,s.DeleteOutlineOutlinedIcon,s.EditOutlinedIcon,s.AddIcon,s.useSetState,s.TextField,s.ClickAwayListener,s.reactColor,s.Dialog,s.Alert,s.EditIcon,s.LabelOutlinedIcon,s.ClickAwayListener$1,s.Chip))})(this,function(s,f,j,H,B,K,$,e,Q,T,J,U,Z,G,Y,X,R,ee,te,re,ne,le,ae,oe,ie,se){"use strict";const C=t=>t&&typeof t=="object"&&"default"in t?t:{default:t},E=C(j),m=C(H),de=C(Q),ce=C(T),ue=C(J),k=C(U),A=C(Z),fe=C(G),he=C(Y),pe=C(X),me=C(R),q=C(ee),ye=C(te),D=C(ne),ge=C(le),xe=C(ae),Ce=C(oe),be=C(ie),ve=C(se),Se=t=>e.jsx("svg",{viewBox:"0 0 24 24",width:"1.2em",height:"1.2em",...t,children:e.jsx("path",{fill:"currentColor",d:"M5 19q-.825 0-1.413-.587Q3 17.825 3 17V7q0-.825.587-1.412Q4.175 5 5 5h10q.5 0 .938.225q.437.225.712.625l3.525 5q.375.525.375 1.15q0 .625-.375 1.15l-3.525 5q-.275.4-.712.625Q15.5 19 15 19Z"})}),we=t=>e.jsx("svg",{viewBox:"0 0 24 24",width:"1.2em",height:"1.2em",...t,children:e.jsx("path",{fill:"currentColor",d:"m10 16.4l-4-4L7.4 11l2.6 2.6L16.6 7L18 8.4Z"})}),P=f.createContext({});function Ie({selectable:t=!1,selected:r=[],onSelect:a,renderItem:o,children:l}){const n=p=>r.indexOf(p)!==-1,i=p=>{Array.from(new Set(r).add(p)),a==null||a(p)},d=f.useMemo(()=>({selectable:t,selected:r,isSelected:n,select:i,renderItem:o}),[r]);return e.jsx(P.Provider,{value:d,children:l})}const _e=t=>{var r;return t.isLeaf||!((r=t.data.children)!=null&&r.length)};function ke({node:t}){return _e(t)?e.jsx("span",{}):t.isOpen?e.jsx(E.default,{style:{fontSize:20}}):e.jsx(E.default,{style:{fontSize:20,transform:"rotate(-90deg)"}})}function Le({node:t,style:r,dragHandle:a}){const{selectable:o,select:l,isSelected:n,renderItem:i}=f.useContext(P),{data:d}=t,p=d.icon?e.jsx($.Icon,{icon:d.icon,style:{fontSize:18}}):e.jsx(Se,{style:{fontSize:18,color:d.color||"#ddd"}}),h=y=>{y.stopPropagation(),t.isInternal&&t.toggle()},x=y=>{y.stopPropagation(),o&&l(t.id)},g=e.jsxs(e.Fragment,{children:[e.jsxs(m.default,{sx:{display:"flex",alignItems:"center",flex:1},children:[e.jsx(m.default,{sx:{display:"flex",cursor:"pointer"},onClick:h,children:e.jsx(ke,{node:t})}),e.jsx(m.default,{sx:{display:"flex",alignItems:"center",width:22,height:22},children:p}),e.jsx(m.default,{component:"span",sx:{color:"grey.700"},children:t.data.name})]}),o&&n(t.id)&&e.jsx(we,{})]});return e.jsxs(m.default,{className:"label-tree-item",style:r,sx:{display:"flex",alignItems:"center",justifyContent:"space-between"},ref:a,onClick:x,children:[!i&&g,!!i&&i(g,t.data)]})}const Ae=B.styled(m.default)`
2
+ > div,
3
+ > div > div,
4
+ > div > div > div {
5
+ width: auto !important;
6
+ }
7
+ [role='treeitem'] {
8
+ display: flex;
9
+ align-items: stretch;
10
+ }
11
+ .label-tree-item {
12
+ width: 100%;
13
+ }
14
+ `;function z({data:t,selectable:r,selected:a=[],onSelect:o,rowHeight:l=28,renderItem:n,sx:i,...d}){const p=f.useRef(),h=[...Array.isArray(i)?i:[i]],[x,g]=f.useState(0),y=f.useCallback(()=>{var _,c;g((((c=(_=p.current)==null?void 0:_.visibleNodes)==null?void 0:c.length)||0)*l)},[l]);f.useEffect(()=>{y()},[t,y]);const S=()=>{setTimeout(y)};return e.jsx(Ie,{selectable:r,selected:a,onSelect:o,renderItem:n,children:e.jsx(Ae,{...d,sx:h,children:e.jsx(K.Tree,{data:t,rowHeight:l,height:x,indent:32,ref:p,onToggle:S,children:Le})})})}const O=t=>t.reduce((r,a)=>{var o;return r.push(a),(o=a.children)!=null&&o.length&&r.push(...O(a.children)),r},[]),F=t=>t.map(r=>{var a;return(a=r.children)!=null&&a.length&&(r.children=F(r.children)),r}),N=t=>{const r=t.reduce((l,n)=>(l[n.id]=l[n.id]||{},n.parentId&&(l[n.id].parent=n.parentId,l[n.parentId]=l[n.parentId]||{},l[n.parentId].children=l[n.parentId].children||[],l[n.parentId].children.push(n.id)),l),{}),a=t.map(({parentId:l,...n})=>({...n,children:[]})),o=a.reduce((l,n)=>({...l,[n.id]:n}),{});return a.forEach(l=>{const n=r[l.id];n.parent&&o[n.parent]&&(l.parent=o[n.parent]),n.children&&(l.children=n.children.map(i=>o[i]))}),a.filter(l=>!l.parent)},qe=t=>{const{options:r,getValue:a,selectProps:o,selectOption:l}=t,n=a(),i=d=>{const p=r.find(h=>h.data.id===d);p&&l(p)};return e.jsxs(e.Fragment,{children:[e.jsx(T.components.MenuList,{...de.default(t,["addon"]),children:e.jsx(m.default,{sx:{px:2,py:1},children:e.jsx(z,{data:o.data,selectable:!0,onSelect:i,selected:n.map(d=>d.data.id)})})}),t.addon]})},ze=({children:t,...r})=>{const[a,o]=t,l=Array.isArray(a)?a.length:0;return e.jsxs(T.components.ValueContainer,{...r,children:[e.jsx(m.default,{component:"span",sx:{fontSize:13,fontWeight:"bold"},children:l?"Labels":"Filter by labels"}),!!l&&e.jsx(m.default,{sx:{display:"inline-flex",justifyContent:"center",alignItems:"center",width:22,height:22,ml:1,borderRadius:"100%",fontSize:12,bgcolor:"grey.200"},children:l}),o]})},Oe={control:t=>({...t,minHeight:31}),valueContainer:t=>({...t,display:"flex"}),dropdownIndicator:t=>({...t,padding:"0 4px"}),clearIndicator:t=>({...t,padding:"4px"})};function V({data:t,value:r=[],editable:a=!1,addon:o,onChange:l,compact:n,...i}){const d=f.useRef(null),p=f.useMemo(()=>O(t).map(c=>({data:c,label:c.name,value:c.id})),[t]),h=p.reduce((c,w)=>({...c,[w.data.id]:w}),{}),x=Array.isArray(r)?r:[r],g=x.map(c=>h[c]),y=f.useRef({prev:x.map(c=>h[c].data)}),S=(c,w)=>{y.current.current=c.map(L=>L.data),["clear","remove-value"].includes(w.action)&&d.current&&setTimeout(()=>{var L;return(L=d==null?void 0:d.current)==null?void 0:L.blur()},1)},_=()=>{y.current.current&&JSON.stringify(y.current.prev.map(w=>w.id))!==JSON.stringify(y.current.current.map(w=>w.id))&&(l(y.current.current),y.current.prev=y.current.current)};return e.jsx(m.default,{...i,children:e.jsx(ce.default,{ref:d,defaultValue:g,options:p,onChange:S,components:{MenuList:c=>e.jsx(qe,{...c,addon:o}),...n&&{ValueContainer:ze}},placeholder:"Select labels",styles:{...n&&Oe,menu:c=>({...c,...a&&{paddingBottom:"36px"},zIndex:99}),menuList:c=>({...c,overflowY:"auto",maxHeight:"264px"})},theme:c=>({...c,colors:{...c.colors,primary25:"#ddd",primary50:"#ddd",primary:"#ddd"}}),isSearchable:!1,isMulti:!0,closeMenuOnSelect:!1,onMenuClose:_,data:t})})}function Te({open:t,initialLabel:r,onSubmit:a,onClose:o,...l}){const n=!(r!=null&&r.id),[i,d]=me.default({name:(r==null?void 0:r.name)||"",color:(r==null?void 0:r.color)||"#ddd",slug:(r==null?void 0:r.id)||""}),p=c=>/^#([0-9A-F]{3}){1,2}$/i.test(c),h=f.useMemo(()=>n?i.name&&i.color&&i.slug&&p(i.color):i.name&&i.color&&p(i.color),[i,n]),[x,g]=f.useState(!1),y=()=>{a({...r,name:i.name,color:i.color,id:i.slug})},S=c=>{d({color:c.hex}),setTimeout(()=>g(!1))},_=c=>{d({color:c.target.value})};return e.jsx(D.default,{open:t,showCloseButton:!0,maxWidth:"lg",title:n?"Create label":"Edit label",actions:e.jsxs(e.Fragment,{children:[e.jsx(k.default,{color:"inherit",variant:"contained",size:"small",onClick:o,children:"Cancel"}),e.jsx(k.default,{color:"primary",variant:"contained",size:"small",onClick:y,disabled:!h,children:n?"Create":"Save Changes"})]}),onClose:o,...l,children:e.jsxs(m.default,{width:600,minHeight:280,children:[e.jsx(m.default,{sx:{position:"relative",zIndex:9999},children:e.jsx(q.default,{label:"Name",value:i.name,placeholder:"New label",size:"small",fullWidth:!0,onChange:c=>d({name:c.target.value})})}),e.jsx(q.default,{label:"Slug",value:i.slug,size:"small",fullWidth:!0,disabled:!n,onChange:c=>d({slug:c.target.value}),sx:{mt:2}}),(r==null?void 0:r.parent)&&e.jsx(q.default,{label:"Parent",value:r.parent.name,size:"small",fullWidth:!0,disabled:!0,sx:{mt:2}}),e.jsx(ye.default,{onClickAway:()=>g(!1),children:e.jsxs(m.default,{sx:{position:"relative",mt:2},children:[e.jsxs(m.default,{sx:{display:"flex",alignItems:"center",gap:1},children:[e.jsx(m.default,{sx:{width:30,height:30,bgcolor:i.color,borderRadius:1},onClick:()=>g(!0)}),e.jsx(q.default,{label:"",value:i.color,size:"small",onChange:_,inputProps:{maxLength:7},sx:{width:100,".MuiInputBase-root":{height:32}}})]}),x&&e.jsx(m.default,{sx:{position:"absolute",top:48,zIndex:1},children:e.jsx(re.GithubPicker,{color:i.color,onChangeComplete:S})})]})})]})})}function Me({open:t,label:r,onSubmit:a,onClose:o,...l}){return e.jsx(D.default,{open:t,showCloseButton:!0,maxWidth:"md",title:"Delete",actions:e.jsxs(e.Fragment,{children:[e.jsx(k.default,{color:"inherit",variant:"contained",size:"small",onClick:o,children:"Cancel"}),e.jsx(k.default,{color:"primary",variant:"contained",size:"small",onClick:a,children:"Delete"})]}),onClose:o,...l,children:e.jsxs(m.default,{width:600,minHeight:280,children:[e.jsx(ge.default,{severity:"info",sx:{mb:2},children:"Are you sure you want to delete these labels ?"}),e.jsx(z,{data:[r]})]})})}const Be=B.styled(z)`
15
+ .label-tree-item {
16
+ &:before {
17
+ content: '';
18
+ position: absolute;
19
+ top: 0;
20
+ left: 0;
21
+ right: 0;
22
+ border-top: 1px solid #ddd;
23
+ }
24
+ }
25
+ & [role='treeitem']:first-of-type > .label-tree-item {
26
+ &:before {
27
+ display: none;
28
+ }
29
+ }
30
+ [role='treeitem'] {
31
+ box-sizing: border-box;
32
+ padding: 0 16px;
33
+ }
34
+ `;function Ee({data:t,api:r,...a}){const[o,l]=f.useState(null),[n,i]=f.useState(null),[d,p]=f.useState(F(t)),h=f.useMemo(()=>O(d),[d]),x=f.useMemo(()=>{const u=(b,I)=>b.map(v=>(I&&(v.parent=I),v.children&&(v.children=u(v.children,v)),v));return u(d)},[d]),g=u=>{var b,I;u.parent?(u.parent.children=(I=(b=u.parent)==null?void 0:b.children)==null?void 0:I.map(v=>v.id===u.id?{...u}:v),g(u.parent)):p(d.map(v=>v.id===u.id?{...u}:v))},y=u=>{u.parent?(u.parent.children=u.parent.children||[],u.parent.children.push(u),p([...d])):p([...d,u])},S=u=>{var b;u.parent?u.parent.children=(b=u.parent.children)==null?void 0:b.filter(I=>I.id!==u.id):p(d.filter(I=>I.id!==u.id))},_=u=>{if(o)return;l({id:"",name:"New label",parent:u,color:"#dddddd"})},c=u=>{o||l(u)},w=async u=>{if(!o)return;const{parent:b,children:I,...v}=u;o.id?(await r.updateLabel({...v,parentId:b==null?void 0:b.id}),g(u)):(await r.createLabel({...v,parentId:b==null?void 0:b.id}),y(u)),l(null)},L=async()=>{!n||(await r.deleteLabel(n.id),S(n),i(null))},Ne=(u,b)=>e.jsxs(e.Fragment,{children:[u,e.jsxs(m.default,{sx:{display:"flex",gap:1,flex:"0 0 auto"},children:[e.jsx(A.default,{color:"inherit",size:"small",sx:{color:"grey.500"},onClick:()=>_(b),children:e.jsx(pe.default,{sx:{fontSize:20}})}),e.jsx(A.default,{color:"inherit",size:"small",sx:{color:"grey.500"},onClick:()=>c(b),children:e.jsx(he.default,{sx:{fontSize:18}})}),e.jsx(A.default,{color:"inherit",size:"small",sx:{color:"grey.500"},onClick:()=>i(b),children:e.jsx(fe.default,{sx:{fontSize:20}})})]})]});return e.jsxs(m.default,{...a,children:[e.jsxs(m.default,{sx:{display:"flex",alignItems:"center",justifyContent:"space-between"},children:[e.jsx(ue.default,{component:"h2",variant:"h6",children:"Manage labels"}),e.jsx(k.default,{color:"primary",variant:"contained",size:"small",sx:{textTransform:"none"},onClick:()=>_(),children:"New label"})]}),e.jsxs(m.default,{sx:{mt:2,border:1,borderColor:"grey.300",borderRadius:1,overflow:"hidden"},children:[e.jsxs(m.default,{sx:{p:2,fontSize:14,fontWeight:"bold",bgcolor:"grey.200"},children:[h.length," labels"]}),e.jsx(m.default,{sx:{pt:.5},children:e.jsx(Be,{data:x,selected:[],renderItem:Ne,rowHeight:64})})]}),e.jsx(Te,{open:!!o,onClose:()=>l(null),initialLabel:o,onSubmit:w},o==null?void 0:o.id),e.jsx(Me,{open:!!n,onClose:()=>i(null),label:n,onSubmit:L},n==null?void 0:n.id)]})}const W=f.createContext({}),M=()=>f.useContext(W),De=()=>{const{updateLabels:t}=M();f.useEffect(()=>()=>{t()},[])};function Pe({fetchLabels:t,children:r}){const[a,o]=f.useState({loading:!0,labels:[],updateCounter:1});f.useEffect(()=>{(async()=>{try{o(g=>({...g,loading:!0}));const x=await t();o(g=>({...g,loading:!1,labels:N(x)}))}catch(x){console.error(x),o(g=>({...g,loading:!1,labels:[]}))}})()},[a.updateCounter]);const l=f.useMemo(()=>{var x;return(x=a.labels)!=null&&x.length?O(a.labels).reduce((g,y)=>({...g,[y.id]:y}),{}):{}},[a.labels]),n=f.useCallback(h=>h.map(x=>l[x]),[l]),i=f.useCallback(h=>(h==null?void 0:h.split(","))||[],[]),d=f.useCallback((h=[])=>{if(!!(h!=null&&h.length))return h.join(",")},[]),p=f.useMemo(()=>({loading:a.loading,labels:a.labels||[],updateLabels:()=>o(h=>({...h,updateCounter:++a.updateCounter})),getLabelsById:n,parseLabelIds:i,stringifyLabelIds:d}),[a,n,i,d]);return e.jsx(W.Provider,{value:p,children:r})}function Fe({labels:t,editable:r,onChange:a,sx:o}){const l=!(t!=null&&t.length),{labels:n,loading:i,getLabelsById:d}=M(),[p,h]=f.useState(!1);if(i||!r&&l)return null;const x=S=>{a==null||a(S),setTimeout(()=>{h(!1)},300)};if(p)return e.jsx(be.default,{onClickAway:()=>p&&h(!1),children:e.jsx("div",{children:e.jsx(V,{data:n,value:t||[],onChange:x})})});const g=d(t||[]),y=[{display:"flex",gap:1,alignItems:"center",flexWrap:"wrap"},...Array.isArray(o)?o:[o]];return e.jsxs(m.default,{sx:y,children:[g.map(S=>S?e.jsx(ve.default,{label:S.name,variant:"filled",size:"small",sx:{borderRadius:1}},S.id):null),r&&!l&&e.jsx(A.default,{color:"inherit",size:"small",onClick:()=>h(!0),sx:{color:"grey.400"},children:e.jsx(xe.default,{sx:{fontSize:20}})}),r&&l&&e.jsx(k.default,{color:"inherit",variant:"outlined",startIcon:e.jsx(Ce.default,{}),onClick:()=>h(!0),children:"Edit labels"})]})}s.LabelManager=Ee,s.LabelPicker=V,s.LabelTree=z,s.Labels=Fe,s.LabelsProvider=Pe,s.transformLabels=N,s.useLabelsContext=M,s.useLabelsUpdateOnDestroy=De,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@blocklet/labels",
3
+ "version": "1.0.63",
4
+ "files": [
5
+ "dist"
6
+ ],
7
+ "main": "./dist/index.umd.js",
8
+ "module": "./dist/index.es.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./dist/index.es.js",
13
+ "require": "./dist/index.umd.js"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "dev": "npm run storybook",
18
+ "build": "tsc && vite build",
19
+ "build:watch": "vite build --watch",
20
+ "preview": "vite preview",
21
+ "storybook": "start-storybook -p 6007",
22
+ "build-storybook": "build-storybook",
23
+ "lint": "eslint src --ext .mjs,.js,.jsx,.ts,.tsx",
24
+ "lint:fix": "eslint src --ext .mjs,.js,.jsx,.ts,.tsx --fix"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "dependencies": {
30
+ "@emotion/css": "^11.10.5",
31
+ "@emotion/react": "^11.10.5",
32
+ "@emotion/styled": "^11.10.5",
33
+ "@iconify/react": "^4.0.1",
34
+ "react-arborist": "^2.2.0",
35
+ "react-color": "^2.19.3",
36
+ "react-select": "^5.6.0"
37
+ },
38
+ "peerDependencies": {
39
+ "@arcblock/ux": "^2.4.61",
40
+ "@mui/icons-material": "^5.10.9",
41
+ "@mui/material": "^5.10.13",
42
+ "lodash": "^4.17.21",
43
+ "react": ">=18.0.0",
44
+ "react-dom": ">=18.0.0",
45
+ "react-router-dom": "^6.4.0"
46
+ },
47
+ "devDependencies": {
48
+ "@arcblock/eslint-config-ts": "^0.2.3",
49
+ "@babel/core": "^7.20.2",
50
+ "@iconify-json/iconoir": "^1.1.14",
51
+ "@iconify-json/material-symbols": "^1.1.26",
52
+ "@iconify-json/mdi": "^1.1.36",
53
+ "@storybook/addon-actions": "^6.5.13",
54
+ "@storybook/addon-essentials": "^6.5.13",
55
+ "@storybook/addon-interactions": "^6.5.13",
56
+ "@storybook/addon-links": "^6.5.13",
57
+ "@storybook/builder-vite": "^0.2.5",
58
+ "@storybook/react": "^6.5.13",
59
+ "@storybook/testing-library": "^0.0.13",
60
+ "@svgr/core": "^6.5.1",
61
+ "@types/react": "^18.0.24",
62
+ "@types/react-color": "^3.0.6",
63
+ "@types/react-dom": "^18.0.8",
64
+ "@vitejs/plugin-react": "^2.2.0",
65
+ "babel-loader": "^8.3.0",
66
+ "react": "^18.2.0",
67
+ "react-dom": "^18.2.0",
68
+ "typescript": "^4.6.4",
69
+ "unplugin-icons": "^0.14.14",
70
+ "vite": "3.1.8",
71
+ "vite-plugin-dts": "^1.7.1",
72
+ "vite-plugin-libcss": "^1.0.5"
73
+ },
74
+ "resolutions": {
75
+ "react": "^18.2.0"
76
+ },
77
+ "gitHead": "3a664f181b9cbcae5b07d37861720c29e75b9ba5"
78
+ }