@opentiny/tiny-robot 0.3.0-alpha.8 → 0.3.0-rc.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.
@@ -1,497 +0,0 @@
1
- import { defineComponent as H, ref as p, computed as P, watch as K, watchEffect as ne, onMounted as Y, onBeforeUnmount as Z, createElementBlock as v, openBlock as g, normalizeClass as j, createElementVNode as u, withDirectives as oe, Fragment as z, renderList as M, createVNode as E, unref as w, toDisplayString as T, vShow as le, nextTick as ie, renderSlot as $, createCommentVNode as N, createBlock as L, normalizeStyle as ae, resolveComponent as ce, withCtx as O } from "vue";
2
- import { m as Q, U as G, a as re, v as ue, A as ge } from "../tiny-robot-svgs.js";
3
- function de(l, c = "") {
4
- const s = document.createElement("div");
5
- c && (s.className = c), s.style.visibility = "hidden", s.style.position = "absolute", s.style.whiteSpace = "nowrap", s.textContent = l, document.body.appendChild(s);
6
- const o = s.offsetWidth;
7
- return document.body.removeChild(s), o;
8
- }
9
- const F = /* @__PURE__ */ new Map();
10
- function X(l, c = "") {
11
- const s = `${l}::${c}`;
12
- if (F.has(s))
13
- return F.get(s);
14
- const o = de(l, c);
15
- return F.set(s, o), o;
16
- }
17
- const ve = { class: "tr-common-suggestions_content" }, me = { class: "tr-common-suggestions_expanded-wrapper" }, pe = { class: "tr-common-suggestions_expanded-area" }, he = ["onClick"], fe = { class: "tr-common-suggestions_item_icon" }, ye = { class: "tr-common-suggestions_item_text" }, _e = { class: "tr-common-suggestions_container" }, ke = ["onClick"], Ce = { class: "tr-common-suggestions_item_icon" }, $e = { class: "tr-common-suggestions_item_text" }, xe = /* @__PURE__ */ H({
18
- __name: "SuggestionCapsule",
19
- props: {
20
- suggestions: {
21
- type: Array,
22
- required: !0
23
- },
24
- isExpanded: {
25
- type: Boolean,
26
- default: !1
27
- }
28
- },
29
- emits: ["suggestion-click", "show-expand-button"],
30
- setup(l, { emit: c }) {
31
- const s = l, o = c, n = p(null), f = p(!1), t = p(!1), m = p([]), a = p([]), h = p(0), S = p(null), R = P(() => {
32
- if (h.value <= 0 || a.value.length === 0) return [];
33
- const i = [], d = [...a.value];
34
- for (; d.length > 0; )
35
- i.push(d.splice(0, h.value));
36
- return i.reverse();
37
- }), b = async () => {
38
- if (await ie(), !n.value || s.suggestions.length === 0) {
39
- f.value = !1, o("show-expand-button", !1);
40
- return;
41
- }
42
- const d = n.value.clientWidth;
43
- m.value = [], a.value = [];
44
- let r = 0;
45
- const _ = 8;
46
- let k = 0;
47
- const B = Math.min(3, s.suggestions.length);
48
- for (let C = 0; C < B; C++) {
49
- const I = X(s.suggestions[C].text, "tr-common-suggestions_item") + _;
50
- k += I;
51
- }
52
- const q = k / B;
53
- h.value = Math.max(1, Math.floor(d / q));
54
- for (let C = 0; C < s.suggestions.length; C++) {
55
- const I = s.suggestions[C], A = X(I.text, "tr-common-suggestions_item") + _;
56
- r + A <= d ? (r += A, m.value.push(I)) : a.value.push(I);
57
- }
58
- f.value = a.value.length > 0, o("show-expand-button", f.value);
59
- }, y = (i) => {
60
- o("suggestion-click", i);
61
- };
62
- return K(
63
- () => s.isExpanded,
64
- (i) => {
65
- t.value = i;
66
- }
67
- ), ne(() => {
68
- n.value && s.suggestions.length && b();
69
- }), Y(() => {
70
- n.value && (S.value = new ResizeObserver(() => {
71
- b();
72
- }), S.value.observe(n.value));
73
- }), Z(() => {
74
- S.value && S.value.disconnect();
75
- }), (i, d) => (g(), v("div", {
76
- class: j(["tr-common-suggestions", { expanded: t.value }]),
77
- ref_key: "capsuleRef",
78
- ref: n
79
- }, [
80
- u("div", ve, [
81
- u("div", me, [
82
- oe(u("div", pe, [
83
- (g(!0), v(z, null, M(R.value, (r, _) => (g(), v("div", {
84
- key: `row-${_}`,
85
- class: "tr-common-suggestions_row"
86
- }, [
87
- (g(!0), v(z, null, M(r, (k, B) => (g(), v("div", {
88
- key: `hidden-${k.id}-${B}`,
89
- class: "tr-common-suggestions_item",
90
- onClick: (q) => y(k)
91
- }, [
92
- u("div", fe, [
93
- E(w(Q))
94
- ]),
95
- u("div", ye, T(k.text), 1)
96
- ], 8, he))), 128))
97
- ]))), 128))
98
- ], 512), [
99
- [le, t.value && a.value.length > 0]
100
- ])
101
- ]),
102
- u("div", _e, [
103
- (g(!0), v(z, null, M(m.value, (r, _) => (g(), v("div", {
104
- key: `visible-${r.id}-${_}`,
105
- class: "tr-common-suggestions_item",
106
- onClick: (k) => y(r)
107
- }, [
108
- u("div", Ce, [
109
- E(w(Q))
110
- ]),
111
- u("div", $e, T(r.text), 1)
112
- ], 8, ke))), 128))
113
- ])
114
- ])
115
- ], 2));
116
- }
117
- }), be = { class: "tr-suggestion-categories" }, we = ["onClick"], Se = {
118
- key: 0,
119
- class: "category-icon"
120
- }, Ie = /* @__PURE__ */ H({
121
- __name: "CategoryNav",
122
- props: {
123
- categories: {
124
- type: Array,
125
- required: !0
126
- },
127
- activeCategory: {
128
- type: String,
129
- default: ""
130
- }
131
- },
132
- emits: ["category-select"],
133
- setup(l, { emit: c }) {
134
- const s = c, o = (n) => {
135
- s("category-select", n);
136
- };
137
- return (n, f) => (g(), v("div", be, [
138
- (g(!0), v(z, null, M(l.categories, (t) => (g(), v("div", {
139
- key: t.id,
140
- class: j(["tr-suggestion-categories-item", { active: l.activeCategory === t.id }]),
141
- onClick: (m) => o(t.id)
142
- }, [
143
- $(n.$slots, "category-label", { category: t }, () => [
144
- t.icon ? (g(), v("div", Se, T(t.icon), 1)) : N("", !0),
145
- u("span", null, T(t.label), 1)
146
- ])
147
- ], 10, we))), 128))
148
- ]));
149
- }
150
- });
151
- function Ee(l, c = {}) {
152
- const s = p(c.initialIndex || -1);
153
- K(l, (t) => {
154
- t.length > 0 && s.value === -1 ? s.value = 0 : t.length === 0 ? s.value = -1 : s.value >= t.length && (s.value = t.length - 1);
155
- });
156
- const o = (t) => {
157
- var m, a;
158
- if (l.value.length !== 0)
159
- switch (t.key) {
160
- case "ArrowUp":
161
- t.preventDefault(), s.value = (s.value - 1 + l.value.length) % l.value.length, n();
162
- break;
163
- case "ArrowDown":
164
- t.preventDefault(), s.value = (s.value + 1) % l.value.length, n();
165
- break;
166
- case "Enter":
167
- case "Tab":
168
- t.preventDefault(), s.value >= 0 && s.value < l.value.length && ((m = c.onSelect) == null || m.call(c, l.value[s.value]));
169
- break;
170
- case "Escape":
171
- t.preventDefault(), (a = c.onClose) == null || a.call(c);
172
- break;
173
- }
174
- }, n = () => {
175
- setTimeout(() => {
176
- const t = document.querySelector(`.tr-suggestion-list-item:nth-child(${s.value + 1})`), m = t == null ? void 0 : t.closest(".tr-suggestion-content");
177
- if (t && m) {
178
- const a = m.getBoundingClientRect(), h = t.getBoundingClientRect();
179
- h.bottom > a.bottom ? m.scrollTop += h.bottom - a.bottom : h.top < a.top && (m.scrollTop -= a.top - h.top);
180
- }
181
- }, 0);
182
- };
183
- return {
184
- activeIndex: s,
185
- handleKeyDown: o,
186
- scrollToActive: n,
187
- setActiveIndex: (t) => {
188
- t >= -1 && t < l.value.length && (s.value = t);
189
- }
190
- };
191
- }
192
- const Te = { class: "tr-suggestion-panel" }, Be = { class: "tr-suggestion-header" }, De = { class: "tr-suggestion-header-left" }, Oe = { class: "tr-suggestion-header-icon" }, Re = { class: "tr-suggestion-header-title" }, Ae = { class: "close-icon" }, Ve = {
193
- key: 0,
194
- class: "tr-suggestion-loading"
195
- }, We = { key: 1 }, Ke = ["onClick", "onMouseenter"], ze = { class: "item-content" }, Me = { class: "item-label" }, Ne = {
196
- key: 0,
197
- class: "item-description"
198
- }, qe = {
199
- key: 2,
200
- class: "tr-suggestion-empty"
201
- }, Le = /* @__PURE__ */ H({
202
- __name: "SuggestionPanel",
203
- props: {
204
- items: {
205
- type: Array,
206
- required: !0
207
- },
208
- categories: {
209
- type: Array,
210
- default: () => []
211
- },
212
- loading: {
213
- type: Boolean,
214
- default: !1
215
- },
216
- title: {
217
- type: String,
218
- default: "指令"
219
- },
220
- maxVisibleItems: {
221
- type: Number,
222
- default: 5
223
- }
224
- },
225
- emits: ["close", "select", "category-select", "item-hover"],
226
- setup(l, { expose: c, emit: s }) {
227
- const o = l, n = s, f = p(o.items), { handleKeyDown: t, activeIndex: m } = Ee(f, {
228
- initialIndex: 0,
229
- onClose: () => n("close"),
230
- onSelect: (i) => n("select", i)
231
- }), a = p(o.categories.length > 0 ? o.categories[0].id : ""), h = P(() => {
232
- if (o.categories.length === 0 || !a.value)
233
- return o.items;
234
- const i = o.categories.find((d) => d.id === a.value);
235
- return i ? i.items : [];
236
- }), S = (i) => {
237
- a.value = i;
238
- const d = o.categories.find((r) => r.id === i);
239
- d && n("category-select", d);
240
- }, R = (i) => {
241
- n("select", i);
242
- }, b = () => {
243
- n("close");
244
- }, y = (i) => {
245
- n("item-hover", i);
246
- };
247
- return c({
248
- handleKeyDown: t
249
- }), (i, d) => (g(), v("div", Te, [
250
- u("div", Be, [
251
- u("div", De, [
252
- u("div", Oe, [
253
- $(i.$slots, "title-icon", {}, () => [
254
- E(w(G), { style: { color: "#1476ff", "font-size": "24px" } })
255
- ])
256
- ]),
257
- u("div", Re, T(l.title), 1)
258
- ]),
259
- u("span", {
260
- class: "tr-suggestion-close-btn",
261
- onClick: b
262
- }, [
263
- u("span", Ae, [
264
- E(w(re))
265
- ])
266
- ])
267
- ]),
268
- l.categories.length > 0 ? (g(), L(Ie, {
269
- key: 0,
270
- categories: l.categories,
271
- "active-category": a.value,
272
- onCategorySelect: S
273
- }, null, 8, ["categories", "active-category"])) : N("", !0),
274
- u("div", {
275
- class: "tr-suggestion-content",
276
- style: ae({ "max-height": h.value.length > l.maxVisibleItems ? `${l.maxVisibleItems * 56}px` : "auto" })
277
- }, [
278
- l.loading ? (g(), v("div", Ve, [
279
- $(i.$slots, "loading-indicator", {}, () => [
280
- d[0] || (d[0] = u("div", { class: "tr-suggestion-loading-spinner" }, null, -1))
281
- ])
282
- ])) : h.value.length > 0 ? (g(), v("ul", We, [
283
- (g(!0), v(z, null, M(h.value, (r, _) => (g(), v("li", {
284
- key: r.id,
285
- class: j(["tr-suggestion-list-item", { "tr-suggestion-item-active": _ === w(m) }]),
286
- onClick: (k) => R(r),
287
- onMouseenter: (k) => y(_)
288
- }, [
289
- $(i.$slots, "item", {
290
- item: r,
291
- active: _ === w(m)
292
- }, () => [
293
- u("div", ze, [
294
- u("div", Me, T(r.text), 1),
295
- r.description ? (g(), v("div", Ne, T(r.description), 1)) : N("", !0)
296
- ])
297
- ])
298
- ], 42, Ke))), 128))
299
- ])) : (g(), v("div", qe, [
300
- $(i.$slots, "empty", {}, () => [
301
- d[1] || (d[1] = u("p", null, "无匹配结果", -1))
302
- ])
303
- ]))
304
- ], 4)
305
- ]));
306
- }
307
- }), Pe = (l) => {
308
- const c = (o) => l.triggerKeys.includes(o);
309
- return {
310
- isTriggerKey: c,
311
- detectTrigger: (o, n) => {
312
- const t = o.target.selectionStart || 0;
313
- return t > 0 && c(n[t - 1]) && (t === 1 || n[t - 2] === " " || n[t - 2] === `
314
- `) ? {
315
- text: n[t - 1],
316
- position: t - 1
317
- } : null;
318
- }
319
- };
320
- }, Ue = { class: "tr-suggestion__header" }, U = /* @__PURE__ */ H({
321
- __name: "index",
322
- props: {
323
- triggerKeys: { default: () => ["/"] },
324
- items: { default: () => [] },
325
- categories: { default: () => [] },
326
- open: { type: Boolean, default: void 0 },
327
- className: {},
328
- theme: { default: "light" },
329
- loading: { type: Boolean, default: !1 },
330
- closeOnOutsideClick: { type: Boolean, default: !0 },
331
- title: { default: "快捷指令" },
332
- maxVisibleItems: { default: 5 },
333
- defaultExpanded: { type: Boolean, default: !1 }
334
- },
335
- emits: ["update:open", "select", "close", "trigger", "category-select", "suggestion-select", "update:expanded", "fill-template"],
336
- setup(l, { expose: c, emit: s }) {
337
- const o = l, n = s, f = p(!1), t = p([]), m = p({
338
- text: "",
339
- position: 0
340
- }), a = p(o.defaultExpanded), h = p(!1), S = () => {
341
- a.value = !a.value, n("update:expanded", a.value);
342
- }, R = (e) => {
343
- h.value = e;
344
- }, b = p(""), y = p(-1), i = p(null), d = p(null), r = P(() => o.open !== void 0 ? o.open : f.value), _ = P(() => {
345
- if (o.categories.length === 0 || !b.value)
346
- return t.value;
347
- const e = o.categories.find((x) => x.id === b.value);
348
- return e ? e.items : [];
349
- });
350
- K(r, (e) => {
351
- e ? y.value = _.value.length > 0 ? 0 : -1 : y.value = -1;
352
- }), K(_, (e) => {
353
- e.length > 0 && y.value === -1 ? y.value = 0 : e.length === 0 ? y.value = -1 : y.value >= e.length && (y.value = e.length - 1);
354
- }), K(
355
- () => o.categories,
356
- (e) => {
357
- e.length > 0 && !b.value && (b.value = e[0].id);
358
- },
359
- { immediate: !0 }
360
- );
361
- const k = () => {
362
- t.value = [...o.items];
363
- }, { detectTrigger: B } = Pe(o), q = (e) => {
364
- n("suggestion-select", e), n("select", e.value, {
365
- text: "",
366
- position: 0
367
- }), e.template && n("fill-template", e.template);
368
- }, C = (e) => {
369
- e === !1 ? V() : (typeof e == "object" && e !== null && (m.value = {
370
- text: e.text || "",
371
- position: e.position || 0
372
- }), k(), f.value = !0, n("update:open", !0));
373
- }, I = (e, x) => {
374
- const W = B(e, x);
375
- return W ? (m.value = W, C(W), !0) : r.value && x === "" ? (C(!1), !0) : !1;
376
- }, A = (e) => {
377
- if (r.value && d.value) {
378
- d.value.handleKeyDown(e);
379
- return;
380
- }
381
- }, V = () => {
382
- o.open === void 0 && (f.value = !1), n("update:open", !1), n("close");
383
- }, ee = (e) => {
384
- b.value = e.id, y.value = 0, n("category-select", e);
385
- }, te = (e, x) => {
386
- n("select", e.text, x), e.template && n("fill-template", e.template), V();
387
- }, J = (e) => {
388
- o.closeOnOutsideClick && r.value && i.value && !i.value.contains(e.target) && V();
389
- }, se = () => {
390
- r.value ? V() : (k(), f.value = !0, n("update:open", !0));
391
- };
392
- return Y(() => {
393
- o.closeOnOutsideClick && document.addEventListener("click", J);
394
- }), Z(() => {
395
- o.closeOnOutsideClick && document.removeEventListener("click", J);
396
- }), c({
397
- trigger: C,
398
- keyDown: A,
399
- input: I,
400
- toggleExpand: S
401
- }), (e, x) => {
402
- const W = ce("IconEdit");
403
- return g(), v("div", {
404
- ref_key: "rootRef",
405
- ref: i,
406
- class: j(["tr-suggestion", e.className, { "tr-suggestion--dark": e.theme === "dark" }])
407
- }, [
408
- u("div", Ue, [
409
- u("div", {
410
- class: "tr-suggestion__trigger",
411
- onClick: se
412
- }, [
413
- E(w(G), { style: { color: "#1476ff" } })
414
- ]),
415
- E(xe, {
416
- suggestions: e.items,
417
- isExpanded: a.value,
418
- onSuggestionClick: q,
419
- onShowExpandButton: R
420
- }, {
421
- "suggestion-icon": O((D) => [
422
- $(e.$slots, "capsule-icon", {
423
- suggestion: D.suggestion
424
- }, () => [
425
- E(W)
426
- ])
427
- ]),
428
- _: 3
429
- }, 8, ["suggestions", "isExpanded"]),
430
- h.value ? (g(), v("div", {
431
- key: 0,
432
- class: "tr-suggestion__expand-button",
433
- onClick: S
434
- }, [
435
- a.value ? (g(), L(w(ge), { key: 1 })) : (g(), L(w(ue), { key: 0 }))
436
- ])) : N("", !0)
437
- ]),
438
- $(e.$slots, "trigger", {
439
- onTrigger: C,
440
- onKeyDown: A,
441
- onInput: I
442
- }),
443
- r.value ? (g(), L(Le, {
444
- key: 0,
445
- ref_key: "panelRef",
446
- ref: d,
447
- class: "tr-suggestion-panel placement-top",
448
- items: t.value,
449
- categories: e.categories,
450
- loading: e.loading,
451
- title: e.title,
452
- maxVisibleItems: e.maxVisibleItems,
453
- triggerContext: m.value,
454
- onClose: V,
455
- onSelect: te,
456
- onCategorySelect: ee
457
- }, {
458
- "title-icon": O(() => [
459
- $(e.$slots, "title-icon", {}, () => [
460
- E(w(G), { style: { "font-size": "24px", color: "#1476ff" } })
461
- ])
462
- ]),
463
- "category-label": O((D) => [
464
- $(e.$slots, "category-label", {
465
- category: D.category
466
- })
467
- ]),
468
- item: O((D) => [
469
- $(e.$slots, "item", {
470
- item: D.item,
471
- active: D.active
472
- })
473
- ]),
474
- "loading-indicator": O(() => [
475
- $(e.$slots, "loading-indicator", {}, () => [
476
- x[0] || (x[0] = u("div", { class: "tr-suggestion__loading-spinner" }, null, -1))
477
- ])
478
- ]),
479
- empty: O(() => [
480
- $(e.$slots, "empty", {}, () => [
481
- x[1] || (x[1] = u("p", null, "无匹配结果", -1))
482
- ])
483
- ]),
484
- _: 3
485
- }, 8, ["items", "categories", "loading", "title", "maxVisibleItems", "triggerContext"])) : N("", !0)
486
- ], 2);
487
- };
488
- }
489
- });
490
- U.name = "TrSuggestion";
491
- const He = function(l) {
492
- l.component(U.name, U);
493
- };
494
- U.install = He;
495
- export {
496
- U as default
497
- };