@deepseek-ai/dsh-client-ui-context-menu 0.0.0-stage → 0.2.2-rc.1
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 +21 -0
- package/README.i18n.yaml +40 -0
- package/README.md +99 -2
- package/README.zh.md +100 -0
- package/lib/client.js +1445 -0
- package/lib/index.js +6 -0
- package/lib/types/client/View.d.ts +26 -0
- package/lib/types/client/index.d.ts +52 -0
- package/lib/types/client/locales.d.ts +28 -0
- package/lib/types/client/registry.d.ts +25 -0
- package/lib/types/client/store.d.ts +35 -0
- package/lib/types/client/target-key.d.ts +9 -0
- package/lib/types/client/trigger.d.ts +11 -0
- package/lib/types/client/types.d.ts +161 -0
- package/lib/types/index.d.ts +4 -0
- package/locale/de.json +5 -0
- package/locale/en.json +5 -0
- package/locale/es.json +5 -0
- package/locale/fr.json +5 -0
- package/locale/ja.json +5 -0
- package/locale/pt.json +5 -0
- package/locale/ru.json +5 -0
- package/locale/zh.json +5 -0
- package/package.json +67 -3
package/lib/client.js
ADDED
|
@@ -0,0 +1,1445 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: "@deepseek-ai/dsh-client-ui-context-menu",
|
|
3
|
+
factory: (require) => {
|
|
4
|
+
var module = { exports: {} };
|
|
5
|
+
var exports = module.exports;
|
|
6
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
7
|
+
let _deepseek_ai_dsh_client_store = require("@deepseek-ai/dsh-client-store");
|
|
8
|
+
let _deepseek_ai_dsh_client_ui_primitives = require("@deepseek-ai/dsh-client-ui-primitives");
|
|
9
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
10
|
+
//#region ../../../vendor/cosmokit/src/misc.ts
|
|
11
|
+
/** Return true when a value is `null` or `undefined`. */
|
|
12
|
+
function isNullable(value) {
|
|
13
|
+
return value === null || value === void 0;
|
|
14
|
+
}
|
|
15
|
+
/** Return true for non-array object values. */
|
|
16
|
+
function isPlainObject(data) {
|
|
17
|
+
return data && typeof data === "object" && !Array.isArray(data);
|
|
18
|
+
}
|
|
19
|
+
/** Filter object entries and return a new object. */
|
|
20
|
+
function filterKeys(object, filter) {
|
|
21
|
+
return Object.fromEntries(Object.entries(object).filter(([key, value]) => filter(key, value)));
|
|
22
|
+
}
|
|
23
|
+
/** Map object values while preserving the original key set. */
|
|
24
|
+
function mapValues(object, transform) {
|
|
25
|
+
return Object.fromEntries(Object.entries(object).map(([key, value]) => [key, transform(value, key)]));
|
|
26
|
+
}
|
|
27
|
+
/** Pick selected keys from an object, optionally including `undefined` values. */
|
|
28
|
+
function pick(source, keys, forced) {
|
|
29
|
+
if (!keys) return { ...source };
|
|
30
|
+
const result = {};
|
|
31
|
+
for (const key of keys) if (forced || source[key] !== void 0) result[key] = source[key];
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region ../../../vendor/cosmokit/src/volatile.ts
|
|
36
|
+
/** Shared config references used by schema validators and plugin runtimes. */
|
|
37
|
+
const write = Symbol.for("cosmokit.volatile.write");
|
|
38
|
+
function snapshot(value, ancestors = /* @__PURE__ */ new Set()) {
|
|
39
|
+
if (typeof value === "function") throw new TypeError("volatile config cannot contain functions");
|
|
40
|
+
if (value === null || typeof value !== "object") return value;
|
|
41
|
+
if (ancestors.has(value)) throw new TypeError("volatile config cannot contain cycles");
|
|
42
|
+
ancestors.add(value);
|
|
43
|
+
try {
|
|
44
|
+
if (Array.isArray(value)) return Object.freeze(value.map((item) => snapshot(item, ancestors)));
|
|
45
|
+
if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) throw new TypeError("volatile config objects must be plain objects or arrays");
|
|
46
|
+
return Object.freeze(Object.fromEntries(Object.entries(value).map(([key, item]) => [key, snapshot(item, ancestors)])));
|
|
47
|
+
} finally {
|
|
48
|
+
ancestors.delete(value);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Create a detached reference containing an immutable copy of the supplied data.
|
|
53
|
+
* @param value - validated config data; class instances and functions are unsupported.
|
|
54
|
+
* @returns a reference whose value is updated only by its owning runtime.
|
|
55
|
+
*/
|
|
56
|
+
function createVolatile(value) {
|
|
57
|
+
let current = snapshot(value);
|
|
58
|
+
return Object.freeze({
|
|
59
|
+
get: () => current,
|
|
60
|
+
[write]: (value) => {
|
|
61
|
+
current = value;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Identify references across ESM/CJS copies of the shared library.
|
|
67
|
+
* @param value - a parsed config value.
|
|
68
|
+
* @returns whether the value implements the shared reference protocol.
|
|
69
|
+
*/
|
|
70
|
+
function isVolatile(value) {
|
|
71
|
+
return typeof value === "object" && value !== null && write in value;
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region ../../../vendor/cosmokit/src/types.ts
|
|
75
|
+
/** Test values using `instanceof` with a `toStringTag` fallback. */
|
|
76
|
+
function is(type, value) {
|
|
77
|
+
if (arguments.length === 1) return (value) => is(type, value);
|
|
78
|
+
return type in globalThis && value instanceof globalThis[type] || Object.prototype.toString.call(value).slice(8, -1) === type;
|
|
79
|
+
}
|
|
80
|
+
function isArrayBufferLike(value) {
|
|
81
|
+
return is("ArrayBuffer", value) || is("SharedArrayBuffer", value);
|
|
82
|
+
}
|
|
83
|
+
function isArrayBufferSource(value) {
|
|
84
|
+
return isArrayBufferLike(value) || ArrayBuffer.isView(value);
|
|
85
|
+
}
|
|
86
|
+
let Binary;
|
|
87
|
+
(function(_Binary) {
|
|
88
|
+
_Binary.is = isArrayBufferLike;
|
|
89
|
+
_Binary.isSource = isArrayBufferSource;
|
|
90
|
+
function fromSource(source) {
|
|
91
|
+
if (ArrayBuffer.isView(source)) return source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength);
|
|
92
|
+
else return source;
|
|
93
|
+
}
|
|
94
|
+
_Binary.fromSource = fromSource;
|
|
95
|
+
function toBase64(source) {
|
|
96
|
+
source = fromSource(source);
|
|
97
|
+
if (typeof Buffer !== "undefined") return Buffer.from(source).toString("base64");
|
|
98
|
+
let binary = "";
|
|
99
|
+
const bytes = new Uint8Array(source);
|
|
100
|
+
for (let i = 0; i < bytes.byteLength; i++) binary += String.fromCharCode(bytes[i]);
|
|
101
|
+
return btoa(binary);
|
|
102
|
+
}
|
|
103
|
+
_Binary.toBase64 = toBase64;
|
|
104
|
+
function fromBase64(source) {
|
|
105
|
+
if (typeof Buffer !== "undefined") return fromSource(Buffer.from(source, "base64"));
|
|
106
|
+
return Uint8Array.from(atob(source), (c) => c.charCodeAt(0));
|
|
107
|
+
}
|
|
108
|
+
_Binary.fromBase64 = fromBase64;
|
|
109
|
+
function toHex(source) {
|
|
110
|
+
source = fromSource(source);
|
|
111
|
+
if (typeof Buffer !== "undefined") return Buffer.from(source).toString("hex");
|
|
112
|
+
return Array.from(new Uint8Array(source), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
113
|
+
}
|
|
114
|
+
_Binary.toHex = toHex;
|
|
115
|
+
function fromHex(source) {
|
|
116
|
+
if (typeof Buffer !== "undefined") return fromSource(Buffer.from(source, "hex"));
|
|
117
|
+
const hex = source.length % 2 === 0 ? source : source.slice(0, source.length - 1);
|
|
118
|
+
const buffer = [];
|
|
119
|
+
for (let i = 0; i < hex.length; i += 2) buffer.push(parseInt(`${hex[i]}${hex[i + 1]}`, 16));
|
|
120
|
+
return Uint8Array.from(buffer).buffer;
|
|
121
|
+
}
|
|
122
|
+
_Binary.fromHex = fromHex;
|
|
123
|
+
})(Binary || (Binary = {}));
|
|
124
|
+
Binary.fromBase64;
|
|
125
|
+
Binary.toBase64;
|
|
126
|
+
Binary.fromHex;
|
|
127
|
+
Binary.toHex;
|
|
128
|
+
/** Deep-clone common JavaScript values while preserving prototypes and cycles. */
|
|
129
|
+
function clone(source, refs = /* @__PURE__ */ new Map()) {
|
|
130
|
+
if (!source || typeof source !== "object") return source;
|
|
131
|
+
if (is("Date", source)) return new Date(source.valueOf());
|
|
132
|
+
if (is("RegExp", source)) return new RegExp(source.source, source.flags);
|
|
133
|
+
if (isArrayBufferLike(source)) return source.slice(0);
|
|
134
|
+
if (ArrayBuffer.isView(source)) return source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength);
|
|
135
|
+
const cached = refs.get(source);
|
|
136
|
+
if (cached) return cached;
|
|
137
|
+
if (Array.isArray(source)) {
|
|
138
|
+
const result = [];
|
|
139
|
+
refs.set(source, result);
|
|
140
|
+
source.forEach((value, index) => {
|
|
141
|
+
result[index] = Reflect.apply(clone, null, [value, refs]);
|
|
142
|
+
});
|
|
143
|
+
return result;
|
|
144
|
+
}
|
|
145
|
+
const result = Object.create(Object.getPrototypeOf(source));
|
|
146
|
+
refs.set(source, result);
|
|
147
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
148
|
+
const descriptor = { ...Reflect.getOwnPropertyDescriptor(source, key) };
|
|
149
|
+
if ("value" in descriptor) descriptor.value = Reflect.apply(clone, null, [descriptor.value, refs]);
|
|
150
|
+
Reflect.defineProperty(result, key, descriptor);
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Compare values recursively, treating two volatile references as equal regardless of value.
|
|
156
|
+
* Strict comparison distinguishes null/undefined, treats opaque objects by identity,
|
|
157
|
+
* compares URLs by normalized href, treats array holes as undefined, and considers distinct cyclic structures unequal.
|
|
158
|
+
* @param a - first value.
|
|
159
|
+
* @param b - second value.
|
|
160
|
+
* @param strict - whether to require strict data equality outside volatile references.
|
|
161
|
+
* @returns whether the values compare equal.
|
|
162
|
+
*/
|
|
163
|
+
function deepEqual(a, b, strict) {
|
|
164
|
+
const ancestors = /* @__PURE__ */ new Set();
|
|
165
|
+
function compare(a, b) {
|
|
166
|
+
if (a === b) return true;
|
|
167
|
+
if (isVolatile(a) || isVolatile(b)) return isVolatile(a) && isVolatile(b);
|
|
168
|
+
if (!strict && isNullable(a) && isNullable(b)) return true;
|
|
169
|
+
if (typeof a !== typeof b || typeof a !== "object" || !a || !b) return false;
|
|
170
|
+
if (ancestors.has(a)) return false;
|
|
171
|
+
function check(test, then) {
|
|
172
|
+
return test(a) ? test(b) ? then(a, b) : false : test(b) ? false : void 0;
|
|
173
|
+
}
|
|
174
|
+
ancestors.add(a);
|
|
175
|
+
try {
|
|
176
|
+
return check(Array.isArray, (a, b) => {
|
|
177
|
+
if (a.length !== b.length) return false;
|
|
178
|
+
for (let index = 0; index < a.length; index++) if (!compare(a[index], b[index])) return false;
|
|
179
|
+
return true;
|
|
180
|
+
}) ?? check(is("Date"), (a, b) => a.valueOf() === b.valueOf()) ?? check(is("URL"), (a, b) => a.href === b.href) ?? check(is("RegExp"), (a, b) => a.source === b.source && a.flags === b.flags) ?? check(isArrayBufferLike, (a, b) => {
|
|
181
|
+
if (a.byteLength !== b.byteLength) return false;
|
|
182
|
+
const viewA = new Uint8Array(a);
|
|
183
|
+
const viewB = new Uint8Array(b);
|
|
184
|
+
for (let i = 0; i < viewA.length; i++) if (viewA[i] !== viewB[i]) return false;
|
|
185
|
+
return true;
|
|
186
|
+
}) ?? ((!strict || [a, b].every((value) => Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) && Object.keys({
|
|
187
|
+
...a,
|
|
188
|
+
...b
|
|
189
|
+
}).every((key) => compare(a[key], b[key])));
|
|
190
|
+
} finally {
|
|
191
|
+
ancestors.delete(a);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return compare(a, b);
|
|
195
|
+
}
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region ../../../vendor/cosmokit/src/time.ts
|
|
198
|
+
let Time;
|
|
199
|
+
(function(_Time) {
|
|
200
|
+
_Time.millisecond = 1;
|
|
201
|
+
const second = _Time.second = 1e3;
|
|
202
|
+
const minute = _Time.minute = second * 60;
|
|
203
|
+
const hour = _Time.hour = minute * 60;
|
|
204
|
+
const day = _Time.day = hour * 24;
|
|
205
|
+
const week = _Time.week = day * 7;
|
|
206
|
+
let timezoneOffset = (/* @__PURE__ */ new Date()).getTimezoneOffset();
|
|
207
|
+
function setTimezoneOffset(offset) {
|
|
208
|
+
timezoneOffset = offset;
|
|
209
|
+
}
|
|
210
|
+
_Time.setTimezoneOffset = setTimezoneOffset;
|
|
211
|
+
function getTimezoneOffset() {
|
|
212
|
+
return timezoneOffset;
|
|
213
|
+
}
|
|
214
|
+
_Time.getTimezoneOffset = getTimezoneOffset;
|
|
215
|
+
function getDateNumber(date = /* @__PURE__ */ new Date(), offset) {
|
|
216
|
+
if (typeof date === "number") date = new Date(date);
|
|
217
|
+
if (offset === void 0) offset = timezoneOffset;
|
|
218
|
+
return Math.floor((date.valueOf() / minute - offset) / 1440);
|
|
219
|
+
}
|
|
220
|
+
_Time.getDateNumber = getDateNumber;
|
|
221
|
+
function fromDateNumber(value, offset) {
|
|
222
|
+
const date = new Date(value * day);
|
|
223
|
+
if (offset === void 0) offset = timezoneOffset;
|
|
224
|
+
return new Date(+date + offset * minute);
|
|
225
|
+
}
|
|
226
|
+
_Time.fromDateNumber = fromDateNumber;
|
|
227
|
+
const numeric = /\d+(?:\.\d+)?/.source;
|
|
228
|
+
const timeRegExp = new RegExp(`^${[
|
|
229
|
+
"w(?:eek(?:s)?)?",
|
|
230
|
+
"d(?:ay(?:s)?)?",
|
|
231
|
+
"h(?:our(?:s)?)?",
|
|
232
|
+
"m(?:in(?:ute)?(?:s)?)?",
|
|
233
|
+
"s(?:ec(?:ond)?(?:s)?)?"
|
|
234
|
+
].map((unit) => `(${numeric}${unit})?`).join("")}$`);
|
|
235
|
+
function parseTime(source) {
|
|
236
|
+
const capture = timeRegExp.exec(source);
|
|
237
|
+
if (!capture) return 0;
|
|
238
|
+
return (parseFloat(capture[1]) * week || 0) + (parseFloat(capture[2]) * day || 0) + (parseFloat(capture[3]) * hour || 0) + (parseFloat(capture[4]) * minute || 0) + (parseFloat(capture[5]) * second || 0);
|
|
239
|
+
}
|
|
240
|
+
_Time.parseTime = parseTime;
|
|
241
|
+
function parseDate(date) {
|
|
242
|
+
const parsed = parseTime(date);
|
|
243
|
+
if (parsed) date = Date.now() + parsed;
|
|
244
|
+
else if (/^\d{1,2}(:\d{1,2}){1,2}$/.test(date)) date = `${(/* @__PURE__ */ new Date()).toLocaleDateString()}-${date}`;
|
|
245
|
+
else if (/^\d{1,2}-\d{1,2}-\d{1,2}(:\d{1,2}){1,2}$/.test(date)) date = `${(/* @__PURE__ */ new Date()).getFullYear()}-${date}`;
|
|
246
|
+
return date ? new Date(date) : /* @__PURE__ */ new Date();
|
|
247
|
+
}
|
|
248
|
+
_Time.parseDate = parseDate;
|
|
249
|
+
function format(ms) {
|
|
250
|
+
const abs = Math.abs(ms);
|
|
251
|
+
if (abs >= day - hour / 2) return Math.round(ms / day) + "d";
|
|
252
|
+
else if (abs >= hour - minute / 2) return Math.round(ms / hour) + "h";
|
|
253
|
+
else if (abs >= minute - second / 2) return Math.round(ms / minute) + "m";
|
|
254
|
+
else if (abs >= second) return Math.round(ms / second) + "s";
|
|
255
|
+
return ms + "ms";
|
|
256
|
+
}
|
|
257
|
+
_Time.format = format;
|
|
258
|
+
function toDigits(source, length = 2) {
|
|
259
|
+
return source.toString().padStart(length, "0");
|
|
260
|
+
}
|
|
261
|
+
_Time.toDigits = toDigits;
|
|
262
|
+
function template(template, time = /* @__PURE__ */ new Date()) {
|
|
263
|
+
return template.replace("yyyy", time.getFullYear().toString()).replace("yy", time.getFullYear().toString().slice(2)).replace("MM", toDigits(time.getMonth() + 1)).replace("dd", toDigits(time.getDate())).replace("hh", toDigits(time.getHours())).replace("mm", toDigits(time.getMinutes())).replace("ss", toDigits(time.getSeconds())).replace("SSS", toDigits(time.getMilliseconds(), 3));
|
|
264
|
+
}
|
|
265
|
+
_Time.template = template;
|
|
266
|
+
})(Time || (Time = {}));
|
|
267
|
+
//#endregion
|
|
268
|
+
//#region ../../../vendor/schemastery/src/index.ts
|
|
269
|
+
const kSchema = Symbol.for("schemastery");
|
|
270
|
+
const kValidationError = Symbol.for("ValidationError");
|
|
271
|
+
globalThis.__schemastery_index__ ??= 0;
|
|
272
|
+
globalThis.__schemastery_refs__ = void 0;
|
|
273
|
+
var ValidationError = class extends TypeError {
|
|
274
|
+
options;
|
|
275
|
+
name = "ValidationError";
|
|
276
|
+
constructor(message, options) {
|
|
277
|
+
let prefix = "$";
|
|
278
|
+
for (const segment of options.path || []) if (typeof segment === "string") prefix += "." + segment;
|
|
279
|
+
else if (typeof segment === "number") prefix += "[" + segment + "]";
|
|
280
|
+
else if (typeof segment === "symbol") prefix += `[Symbol(${segment.toString()})]`;
|
|
281
|
+
if (prefix.startsWith(".")) prefix = prefix.slice(1);
|
|
282
|
+
super((prefix === "$" ? "" : `${prefix} `) + message);
|
|
283
|
+
this.options = options;
|
|
284
|
+
}
|
|
285
|
+
static is(error) {
|
|
286
|
+
return !!error?.[kValidationError];
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
Object.defineProperty(ValidationError.prototype, kValidationError, { value: true });
|
|
290
|
+
const Schema = function(options) {
|
|
291
|
+
const schema = function(data, options = {}) {
|
|
292
|
+
return Schema.resolve(data, schema, options)[0];
|
|
293
|
+
};
|
|
294
|
+
if (options.refs) {
|
|
295
|
+
const refs = mapValues(options.refs, (options) => new Schema(options));
|
|
296
|
+
const getRef = (uid) => refs[uid];
|
|
297
|
+
for (const key in refs) {
|
|
298
|
+
const options = refs[key];
|
|
299
|
+
options.sKey = getRef(options.sKey);
|
|
300
|
+
options.inner = getRef(options.inner);
|
|
301
|
+
options.list = options.list && options.list.map(getRef);
|
|
302
|
+
options.dict = options.dict && mapValues(options.dict, getRef);
|
|
303
|
+
}
|
|
304
|
+
return refs[options.uid];
|
|
305
|
+
}
|
|
306
|
+
Object.assign(schema, options);
|
|
307
|
+
if (typeof schema.callback === "string") try {
|
|
308
|
+
schema.callback = new Function("return " + schema.callback)();
|
|
309
|
+
} catch {}
|
|
310
|
+
Object.defineProperty(schema, "uid", { value: globalThis.__schemastery_index__++ });
|
|
311
|
+
Object.setPrototypeOf(schema, Schema.prototype);
|
|
312
|
+
schema.meta ||= {};
|
|
313
|
+
schema.toString = schema.toString.bind(schema);
|
|
314
|
+
return schema;
|
|
315
|
+
};
|
|
316
|
+
Schema.prototype = Object.create(Function.prototype);
|
|
317
|
+
Schema.prototype[kSchema] = true;
|
|
318
|
+
Object.defineProperty(Schema.prototype, "~standard", { get() {
|
|
319
|
+
return {
|
|
320
|
+
version: 1,
|
|
321
|
+
vendor: "schemastery",
|
|
322
|
+
validate: (value) => {
|
|
323
|
+
try {
|
|
324
|
+
return { value: Schema.resolve(value, this, {})[0] };
|
|
325
|
+
} catch (error) {
|
|
326
|
+
if (ValidationError.is(error)) return { issues: [{
|
|
327
|
+
message: error.message,
|
|
328
|
+
path: error.options.path
|
|
329
|
+
}] };
|
|
330
|
+
throw error;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
} });
|
|
335
|
+
Schema.ValidationError = ValidationError;
|
|
336
|
+
Schema.prototype.toJSON = function toJSON() {
|
|
337
|
+
if (globalThis.__schemastery_refs__) {
|
|
338
|
+
globalThis.__schemastery_refs__[this.uid] ??= JSON.parse(JSON.stringify({ ...this }));
|
|
339
|
+
return this.uid;
|
|
340
|
+
}
|
|
341
|
+
globalThis.__schemastery_refs__ = { [this.uid]: { ...this } };
|
|
342
|
+
globalThis.__schemastery_refs__[this.uid] = JSON.parse(JSON.stringify({ ...this }));
|
|
343
|
+
const result = {
|
|
344
|
+
uid: this.uid,
|
|
345
|
+
refs: globalThis.__schemastery_refs__
|
|
346
|
+
};
|
|
347
|
+
globalThis.__schemastery_refs__ = void 0;
|
|
348
|
+
return result;
|
|
349
|
+
};
|
|
350
|
+
Schema.prototype.set = function set(key, value) {
|
|
351
|
+
this.dict[key] = value;
|
|
352
|
+
return this;
|
|
353
|
+
};
|
|
354
|
+
Schema.prototype.push = function push(value) {
|
|
355
|
+
this.list.push(value);
|
|
356
|
+
return this;
|
|
357
|
+
};
|
|
358
|
+
function mergeDesc(original, messages) {
|
|
359
|
+
const result = typeof original === "string" ? { "": original } : { ...original };
|
|
360
|
+
for (const locale in messages) {
|
|
361
|
+
const value = messages[locale];
|
|
362
|
+
if (value?.$description || value?.$desc) result[locale] = value.$description || value.$desc;
|
|
363
|
+
else if (typeof value === "string") result[locale] = value;
|
|
364
|
+
}
|
|
365
|
+
return result;
|
|
366
|
+
}
|
|
367
|
+
function getInner(value) {
|
|
368
|
+
return value?.$value ?? value?.$inner;
|
|
369
|
+
}
|
|
370
|
+
function extractKeys(data) {
|
|
371
|
+
return filterKeys(data ?? {}, (key) => !key.startsWith("$"));
|
|
372
|
+
}
|
|
373
|
+
Schema.prototype.i18n = function i18n(messages) {
|
|
374
|
+
const schema = Schema(this);
|
|
375
|
+
const desc = mergeDesc(schema.meta.description, messages);
|
|
376
|
+
if (Object.keys(desc).length) schema.meta.description = desc;
|
|
377
|
+
if (schema.dict) schema.dict = mapValues(schema.dict, (inner, key) => {
|
|
378
|
+
return inner.i18n(mapValues(messages, (data) => getInner(data)?.[key] ?? data?.[key]));
|
|
379
|
+
});
|
|
380
|
+
if (schema.list) schema.list = schema.list.map((inner, index) => {
|
|
381
|
+
return inner.i18n(mapValues(messages, (data = {}) => {
|
|
382
|
+
if (Array.isArray(getInner(data))) return getInner(data)[index];
|
|
383
|
+
if (Array.isArray(data)) return data[index];
|
|
384
|
+
return extractKeys(data);
|
|
385
|
+
}));
|
|
386
|
+
});
|
|
387
|
+
if (schema.inner) schema.inner = schema.inner.i18n(mapValues(messages, (data) => {
|
|
388
|
+
if (getInner(data)) return getInner(data);
|
|
389
|
+
return extractKeys(data);
|
|
390
|
+
}));
|
|
391
|
+
if (schema.sKey) schema.sKey = schema.sKey.i18n(mapValues(messages, (data) => data?.$key));
|
|
392
|
+
return schema;
|
|
393
|
+
};
|
|
394
|
+
Schema.prototype.extra = function extra(key, value) {
|
|
395
|
+
const schema = Schema(this);
|
|
396
|
+
schema.meta = {
|
|
397
|
+
...schema.meta,
|
|
398
|
+
[key]: value
|
|
399
|
+
};
|
|
400
|
+
return schema;
|
|
401
|
+
};
|
|
402
|
+
for (const key of [
|
|
403
|
+
"required",
|
|
404
|
+
"disabled",
|
|
405
|
+
"collapse",
|
|
406
|
+
"hidden",
|
|
407
|
+
"loose"
|
|
408
|
+
]) Object.assign(Schema.prototype, { [key](value = true) {
|
|
409
|
+
const schema = Schema(this);
|
|
410
|
+
schema.meta = {
|
|
411
|
+
...schema.meta,
|
|
412
|
+
[key]: value
|
|
413
|
+
};
|
|
414
|
+
return schema;
|
|
415
|
+
} });
|
|
416
|
+
Schema.prototype.deprecated = function deprecated() {
|
|
417
|
+
const schema = Schema(this);
|
|
418
|
+
schema.meta.badges ||= [];
|
|
419
|
+
schema.meta.badges.push({
|
|
420
|
+
text: "deprecated",
|
|
421
|
+
type: "danger"
|
|
422
|
+
});
|
|
423
|
+
return schema;
|
|
424
|
+
};
|
|
425
|
+
Schema.prototype.experimental = function experimental() {
|
|
426
|
+
const schema = Schema(this);
|
|
427
|
+
schema.meta.badges ||= [];
|
|
428
|
+
schema.meta.badges.push({
|
|
429
|
+
text: "experimental",
|
|
430
|
+
type: "warning"
|
|
431
|
+
});
|
|
432
|
+
return schema;
|
|
433
|
+
};
|
|
434
|
+
Schema.prototype.pattern = function pattern(regexp) {
|
|
435
|
+
const schema = Schema(this);
|
|
436
|
+
const pattern = pick(regexp, ["source", "flags"]);
|
|
437
|
+
schema.meta = {
|
|
438
|
+
...schema.meta,
|
|
439
|
+
pattern
|
|
440
|
+
};
|
|
441
|
+
return schema;
|
|
442
|
+
};
|
|
443
|
+
Schema.prototype.simplify = function simplify(value) {
|
|
444
|
+
if (isVolatile(value)) value = value.get();
|
|
445
|
+
if (deepEqual(value, this.meta.default, this.type === "dict")) return null;
|
|
446
|
+
if (isNullable(value)) return value;
|
|
447
|
+
if (this.type === "object" || this.type === "dict") {
|
|
448
|
+
const result = {};
|
|
449
|
+
for (const key in value) {
|
|
450
|
+
const item = (this.type === "object" ? this.dict[key] : this.inner)?.simplify(value[key]);
|
|
451
|
+
if (this.type === "dict" || !isNullable(item)) result[key] = item;
|
|
452
|
+
}
|
|
453
|
+
if (deepEqual(result, this.meta.default, this.type === "dict")) return null;
|
|
454
|
+
return result;
|
|
455
|
+
} else if (this.type === "array" || this.type === "tuple") {
|
|
456
|
+
const result = [];
|
|
457
|
+
value.forEach((value, index) => {
|
|
458
|
+
const schema = this.type === "array" ? this.inner : this.list[index];
|
|
459
|
+
const item = schema ? schema.simplify(value) : value;
|
|
460
|
+
result.push(item);
|
|
461
|
+
});
|
|
462
|
+
return result;
|
|
463
|
+
} else if (this.type === "intersect") {
|
|
464
|
+
const result = {};
|
|
465
|
+
for (const item of this.list) Object.assign(result, item.simplify(value));
|
|
466
|
+
return result;
|
|
467
|
+
} else if (this.type === "union") for (const schema of this.list) try {
|
|
468
|
+
Schema.resolve(value, schema, {});
|
|
469
|
+
return schema.simplify(value);
|
|
470
|
+
} catch {}
|
|
471
|
+
return value;
|
|
472
|
+
};
|
|
473
|
+
Schema.prototype.toString = function toString(inline) {
|
|
474
|
+
return formatters[this.type]?.(this, inline) ?? `Schema<${this.type}>`;
|
|
475
|
+
};
|
|
476
|
+
Schema.prototype.role = function role(role, extra) {
|
|
477
|
+
const schema = Schema(this);
|
|
478
|
+
schema.meta = {
|
|
479
|
+
...schema.meta,
|
|
480
|
+
role,
|
|
481
|
+
extra
|
|
482
|
+
};
|
|
483
|
+
return schema;
|
|
484
|
+
};
|
|
485
|
+
for (const key of [
|
|
486
|
+
"default",
|
|
487
|
+
"link",
|
|
488
|
+
"comment",
|
|
489
|
+
"description",
|
|
490
|
+
"max",
|
|
491
|
+
"min",
|
|
492
|
+
"step"
|
|
493
|
+
]) Object.assign(Schema.prototype, { [key](value) {
|
|
494
|
+
const schema = Schema(this);
|
|
495
|
+
schema.meta = {
|
|
496
|
+
...schema.meta,
|
|
497
|
+
[key]: value
|
|
498
|
+
};
|
|
499
|
+
return schema;
|
|
500
|
+
} });
|
|
501
|
+
Schema.prototype.volatile = function volatile() {
|
|
502
|
+
if (this.meta.volatile) throw new TypeError("volatile schema is already wrapped");
|
|
503
|
+
return this.extra("volatile", true);
|
|
504
|
+
};
|
|
505
|
+
const resolvers = {};
|
|
506
|
+
const checkedVolatile = Symbol("checked-volatile-schema");
|
|
507
|
+
function validateVolatileSchema(schema, path = [], blocked = false, seen = /* @__PURE__ */ new Map()) {
|
|
508
|
+
const states = seen.get(schema) ?? /* @__PURE__ */ new Set();
|
|
509
|
+
if (states.has(blocked)) return;
|
|
510
|
+
states.add(blocked);
|
|
511
|
+
seen.set(schema, states);
|
|
512
|
+
if (schema.meta?.volatile && blocked) throw new ValidationError("volatile fields require a fixed object path without an enclosing volatile field", { path });
|
|
513
|
+
const nested = blocked || !!schema.meta?.volatile;
|
|
514
|
+
if (schema.dict) for (const [key, child] of Object.entries(schema.dict)) validateVolatileSchema(child, [...path, key], nested, seen);
|
|
515
|
+
if (schema.sKey) validateVolatileSchema(schema.sKey, [...path, "<key>"], true, seen);
|
|
516
|
+
if (schema.inner && (schema.type !== "lazy" || schema.inner[kSchema])) validateVolatileSchema(schema.inner, [...path, "*"], true, seen);
|
|
517
|
+
if (schema.list) for (let index = 0; index < schema.list.length; index++) validateVolatileSchema(schema.list[index], [...path, String(index)], true, seen);
|
|
518
|
+
}
|
|
519
|
+
Schema.extend = function extend(type, resolve) {
|
|
520
|
+
resolvers[type] = resolve;
|
|
521
|
+
};
|
|
522
|
+
Schema.resolve = function resolve(data, schema, options = {}, strict = false) {
|
|
523
|
+
if (!schema) return [data];
|
|
524
|
+
if (!options[checkedVolatile]) {
|
|
525
|
+
validateVolatileSchema(schema, options.path);
|
|
526
|
+
options = {
|
|
527
|
+
...options,
|
|
528
|
+
[checkedVolatile]: true
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
if (schema.meta?.volatile) {
|
|
532
|
+
const inner = Schema(schema);
|
|
533
|
+
inner.meta = {
|
|
534
|
+
...schema.meta,
|
|
535
|
+
volatile: false
|
|
536
|
+
};
|
|
537
|
+
const [value, adapted] = Schema.resolve(data, inner, options, strict);
|
|
538
|
+
try {
|
|
539
|
+
return [createVolatile(value), adapted];
|
|
540
|
+
} catch (error) {
|
|
541
|
+
throw new ValidationError(error instanceof Error ? error.message : String(error), options);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
if (options.ignore?.(data, schema)) return [data];
|
|
545
|
+
if (isNullable(data) && schema.type !== "lazy") {
|
|
546
|
+
if (schema.meta.required) throw new ValidationError(`missing required value`, options);
|
|
547
|
+
let current = schema;
|
|
548
|
+
let fallback = schema.meta.default;
|
|
549
|
+
while (current?.type === "intersect" && isNullable(fallback)) {
|
|
550
|
+
current = current.list[0];
|
|
551
|
+
fallback = current?.meta.default;
|
|
552
|
+
}
|
|
553
|
+
if (isNullable(fallback)) return [data];
|
|
554
|
+
data = clone(fallback);
|
|
555
|
+
}
|
|
556
|
+
const callback = resolvers[schema.type];
|
|
557
|
+
if (!callback) throw new ValidationError(`unsupported type "${schema.type}"`, options);
|
|
558
|
+
try {
|
|
559
|
+
return callback(data, schema, options, strict);
|
|
560
|
+
} catch (error) {
|
|
561
|
+
if (!schema.meta.loose) throw error;
|
|
562
|
+
return [schema.meta.default];
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
Schema.from = function from(source) {
|
|
566
|
+
if (isNullable(source)) return Schema.any();
|
|
567
|
+
else if ([
|
|
568
|
+
"string",
|
|
569
|
+
"number",
|
|
570
|
+
"boolean"
|
|
571
|
+
].includes(typeof source)) return Schema.const(source).required();
|
|
572
|
+
else if (source[kSchema]) return source;
|
|
573
|
+
else if (typeof source === "function") switch (source) {
|
|
574
|
+
case String: return Schema.string().required();
|
|
575
|
+
case Number: return Schema.number().required();
|
|
576
|
+
case Boolean: return Schema.boolean().required();
|
|
577
|
+
case Function: return Schema.function().required();
|
|
578
|
+
default: return Schema.is(source).required();
|
|
579
|
+
}
|
|
580
|
+
else throw new TypeError(`cannot infer schema from ${source}`);
|
|
581
|
+
};
|
|
582
|
+
Schema.lazy = function lazy(builder) {
|
|
583
|
+
const toJSON = () => {
|
|
584
|
+
if (!schema.inner[kSchema]) {
|
|
585
|
+
schema.inner = schema.builder();
|
|
586
|
+
schema.inner.meta = {
|
|
587
|
+
...schema.meta,
|
|
588
|
+
...schema.inner.meta
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
return schema.inner.toJSON();
|
|
592
|
+
};
|
|
593
|
+
const schema = new Schema({
|
|
594
|
+
type: "lazy",
|
|
595
|
+
builder,
|
|
596
|
+
inner: { toJSON }
|
|
597
|
+
});
|
|
598
|
+
return schema;
|
|
599
|
+
};
|
|
600
|
+
Schema.natural = function natural() {
|
|
601
|
+
return Schema.number().step(1).min(0);
|
|
602
|
+
};
|
|
603
|
+
Schema.percent = function percent() {
|
|
604
|
+
return Schema.number().step(.01).min(0).max(1).role("slider");
|
|
605
|
+
};
|
|
606
|
+
Schema.date = function date() {
|
|
607
|
+
return Schema.union([Schema.is(Date), Schema.transform(Schema.string().role("datetime"), (value, options) => {
|
|
608
|
+
const date = new Date(value);
|
|
609
|
+
if (isNaN(+date)) throw new ValidationError(`invalid date "${value}"`, options);
|
|
610
|
+
return date;
|
|
611
|
+
}, true)]);
|
|
612
|
+
};
|
|
613
|
+
Schema.regExp = function regExp(flag = "") {
|
|
614
|
+
return Schema.union([Schema.is(RegExp), Schema.transform(Schema.string().role("regexp", { flag }), (value, options) => {
|
|
615
|
+
try {
|
|
616
|
+
return new RegExp(value, flag);
|
|
617
|
+
} catch (e) {
|
|
618
|
+
throw new ValidationError(e.message, options);
|
|
619
|
+
}
|
|
620
|
+
}, true)]);
|
|
621
|
+
};
|
|
622
|
+
Schema.arrayBuffer = function arrayBuffer(encoding) {
|
|
623
|
+
return Schema.union([
|
|
624
|
+
Schema.is(ArrayBuffer),
|
|
625
|
+
Schema.is(SharedArrayBuffer),
|
|
626
|
+
Schema.transform(Schema.any(), (value, options) => {
|
|
627
|
+
if (Binary.isSource(value)) return Binary.fromSource(value);
|
|
628
|
+
throw new ValidationError(`expected ArrayBufferSource but got ${value}`, options);
|
|
629
|
+
}, true),
|
|
630
|
+
...encoding ? [Schema.transform(Schema.string(), (value, options) => {
|
|
631
|
+
try {
|
|
632
|
+
return encoding === "base64" ? Binary.fromBase64(value) : Binary.fromHex(value);
|
|
633
|
+
} catch (e) {
|
|
634
|
+
throw new ValidationError(e.message, options);
|
|
635
|
+
}
|
|
636
|
+
}, true)] : []
|
|
637
|
+
]);
|
|
638
|
+
};
|
|
639
|
+
Schema.extend("lazy", (data, schema, options, strict) => {
|
|
640
|
+
if (!schema.inner[kSchema]) {
|
|
641
|
+
schema.inner = schema.builder();
|
|
642
|
+
schema.inner.meta = {
|
|
643
|
+
...schema.meta,
|
|
644
|
+
...schema.inner.meta
|
|
645
|
+
};
|
|
646
|
+
validateVolatileSchema(schema.inner, options.path, true);
|
|
647
|
+
}
|
|
648
|
+
return Schema.resolve(data, schema.inner, options, strict);
|
|
649
|
+
});
|
|
650
|
+
Schema.extend("any", (data) => {
|
|
651
|
+
return [data];
|
|
652
|
+
});
|
|
653
|
+
Schema.extend("never", (data, _, options) => {
|
|
654
|
+
throw new ValidationError(`expected nullable but got ${data}`, options);
|
|
655
|
+
});
|
|
656
|
+
Schema.extend("const", (data, { value }, options) => {
|
|
657
|
+
if (deepEqual(data, value)) return [value];
|
|
658
|
+
throw new ValidationError(`expected ${value} but got ${data}`, options);
|
|
659
|
+
});
|
|
660
|
+
function checkWithinRange(data, meta, description, options, skipMin = false) {
|
|
661
|
+
const { max = Infinity, min = -Infinity } = meta;
|
|
662
|
+
if (data > max) throw new ValidationError(`expected ${description} <= ${max} but got ${data}`, options);
|
|
663
|
+
if (data < min && !skipMin) throw new ValidationError(`expected ${description} >= ${min} but got ${data}`, options);
|
|
664
|
+
}
|
|
665
|
+
Schema.extend("string", (data, { meta }, options) => {
|
|
666
|
+
if (typeof data !== "string") throw new ValidationError(`expected string but got ${data}`, options);
|
|
667
|
+
if (meta.pattern) {
|
|
668
|
+
const regexp = new RegExp(meta.pattern.source, meta.pattern.flags);
|
|
669
|
+
if (!regexp.test(data)) throw new ValidationError(`expect string to match regexp ${regexp}`, options);
|
|
670
|
+
}
|
|
671
|
+
checkWithinRange(data.length, meta, "string length", options);
|
|
672
|
+
return [data];
|
|
673
|
+
});
|
|
674
|
+
function decimalShift(data, digits) {
|
|
675
|
+
const str = data.toString();
|
|
676
|
+
if (str.includes("e")) return data * Math.pow(10, digits);
|
|
677
|
+
const index = str.indexOf(".");
|
|
678
|
+
if (index === -1) return data * Math.pow(10, digits);
|
|
679
|
+
const frac = str.slice(index + 1);
|
|
680
|
+
const integer = str.slice(0, index);
|
|
681
|
+
if (frac.length <= digits) return +(integer + frac.padEnd(digits, "0"));
|
|
682
|
+
return +(integer + frac.slice(0, digits) + "." + frac.slice(digits));
|
|
683
|
+
}
|
|
684
|
+
function isMultipleOf(data, min, step) {
|
|
685
|
+
step = Math.abs(step);
|
|
686
|
+
if (!/^\d+\.\d+$/.test(step.toString())) return (data - min) % step === 0;
|
|
687
|
+
const index = step.toString().indexOf(".");
|
|
688
|
+
const digits = step.toString().slice(index + 1).length;
|
|
689
|
+
return Math.abs(decimalShift(data, digits) - decimalShift(min, digits)) % decimalShift(step, digits) === 0;
|
|
690
|
+
}
|
|
691
|
+
Schema.extend("number", (data, { meta }, options) => {
|
|
692
|
+
if (typeof data !== "number") throw new ValidationError(`expected number but got ${data}`, options);
|
|
693
|
+
checkWithinRange(data, meta, "number", options);
|
|
694
|
+
const { step } = meta;
|
|
695
|
+
if (step && !isMultipleOf(data, meta.min ?? 0, step)) throw new ValidationError(`expected number multiple of ${step} but got ${data}`, options);
|
|
696
|
+
return [data];
|
|
697
|
+
});
|
|
698
|
+
Schema.extend("boolean", (data, _, options) => {
|
|
699
|
+
if (typeof data === "boolean") return [data];
|
|
700
|
+
throw new ValidationError(`expected boolean but got ${data}`, options);
|
|
701
|
+
});
|
|
702
|
+
Schema.extend("bitset", (data, { bits, meta }, options) => {
|
|
703
|
+
let value = 0, keys = [];
|
|
704
|
+
if (typeof data === "number") {
|
|
705
|
+
value = data;
|
|
706
|
+
for (const key in bits) if (data & bits[key]) keys.push(key);
|
|
707
|
+
} else if (Array.isArray(data)) {
|
|
708
|
+
keys = data;
|
|
709
|
+
for (const key of keys) {
|
|
710
|
+
if (typeof key !== "string") throw new ValidationError(`expected string but got ${key}`, options);
|
|
711
|
+
if (key in bits) value |= bits[key];
|
|
712
|
+
}
|
|
713
|
+
} else throw new ValidationError(`expected number or array but got ${data}`, options);
|
|
714
|
+
if (value === meta.default) return [value];
|
|
715
|
+
return [value, keys];
|
|
716
|
+
});
|
|
717
|
+
Schema.extend("function", (data, _, options) => {
|
|
718
|
+
if (typeof data === "function") return [data];
|
|
719
|
+
throw new ValidationError(`expected function but got ${data}`, options);
|
|
720
|
+
});
|
|
721
|
+
Schema.extend("is", (data, { constructor }, options) => {
|
|
722
|
+
if (typeof constructor === "function") {
|
|
723
|
+
if (data instanceof constructor) return [data];
|
|
724
|
+
throw new ValidationError(`expected ${constructor.name} but got ${data}`, options);
|
|
725
|
+
} else {
|
|
726
|
+
if (isNullable(data)) throw new ValidationError(`expected ${constructor} but got ${data}`, options);
|
|
727
|
+
let prototype = Object.getPrototypeOf(data);
|
|
728
|
+
while (prototype) {
|
|
729
|
+
if (prototype.constructor?.name === constructor) return [data];
|
|
730
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
731
|
+
}
|
|
732
|
+
throw new ValidationError(`expected ${constructor} but got ${data}`, options);
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
function property(data, key, schema, options) {
|
|
736
|
+
try {
|
|
737
|
+
const [value, adapted] = Schema.resolve(data[key], schema, {
|
|
738
|
+
...options,
|
|
739
|
+
path: [...options.path || [], key]
|
|
740
|
+
});
|
|
741
|
+
if (adapted !== void 0) data[key] = adapted;
|
|
742
|
+
return value;
|
|
743
|
+
} catch (e) {
|
|
744
|
+
if (!options?.autofix) throw e;
|
|
745
|
+
delete data[key];
|
|
746
|
+
return schema.meta.volatile ? createVolatile(schema.meta.default) : schema.meta.default;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
Schema.extend("array", (data, { inner, meta }, options) => {
|
|
750
|
+
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options);
|
|
751
|
+
checkWithinRange(data.length, meta, "array length", options, !isNullable(inner.meta.default));
|
|
752
|
+
return [data.map((_, index) => property(data, index, inner, options))];
|
|
753
|
+
});
|
|
754
|
+
Schema.extend("dict", (data, { inner, sKey }, options, strict) => {
|
|
755
|
+
if (!isPlainObject(data)) throw new ValidationError(`expected object but got ${data}`, options);
|
|
756
|
+
const result = {};
|
|
757
|
+
for (const key in data) {
|
|
758
|
+
let rKey;
|
|
759
|
+
try {
|
|
760
|
+
rKey = Schema.resolve(key, sKey, options)[0];
|
|
761
|
+
} catch (error) {
|
|
762
|
+
if (strict) continue;
|
|
763
|
+
throw error;
|
|
764
|
+
}
|
|
765
|
+
result[rKey] = property(data, key, inner, options);
|
|
766
|
+
data[rKey] = data[key];
|
|
767
|
+
if (key !== rKey) delete data[key];
|
|
768
|
+
}
|
|
769
|
+
return [result];
|
|
770
|
+
});
|
|
771
|
+
Schema.extend("tuple", (data, { list }, options, strict) => {
|
|
772
|
+
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options);
|
|
773
|
+
const result = list.map((inner, index) => property(data, index, inner, options));
|
|
774
|
+
if (strict) return [result];
|
|
775
|
+
result.push(...data.slice(list.length));
|
|
776
|
+
return [result];
|
|
777
|
+
});
|
|
778
|
+
function merge(result, data) {
|
|
779
|
+
for (const key in data) {
|
|
780
|
+
if (key in result) continue;
|
|
781
|
+
result[key] = data[key];
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
Schema.extend("object", (data, { dict }, options, strict) => {
|
|
785
|
+
if (!isPlainObject(data)) throw new ValidationError(`expected object but got ${data}`, options);
|
|
786
|
+
const result = {};
|
|
787
|
+
for (const key in dict) {
|
|
788
|
+
const value = property(data, key, dict[key], options);
|
|
789
|
+
if (!isNullable(value) || key in data) result[key] = value;
|
|
790
|
+
}
|
|
791
|
+
if (!strict) merge(result, data);
|
|
792
|
+
return [result];
|
|
793
|
+
});
|
|
794
|
+
Schema.extend("union", (data, { list, toString }, options, strict) => {
|
|
795
|
+
const messages = [];
|
|
796
|
+
for (const inner of list) try {
|
|
797
|
+
return Schema.resolve(data, inner, options, strict);
|
|
798
|
+
} catch (error) {
|
|
799
|
+
messages.push(error);
|
|
800
|
+
}
|
|
801
|
+
throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options);
|
|
802
|
+
});
|
|
803
|
+
Schema.extend("intersect", (data, { list, toString }, options, strict) => {
|
|
804
|
+
if (!list.length) return [data];
|
|
805
|
+
let result;
|
|
806
|
+
for (const inner of list) {
|
|
807
|
+
const value = Schema.resolve(data, inner, options, true)[0];
|
|
808
|
+
if (isNullable(value)) continue;
|
|
809
|
+
if (isNullable(result)) result = value;
|
|
810
|
+
else if (typeof result !== typeof value) throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options);
|
|
811
|
+
else if (typeof value === "object") merge(result ??= {}, value);
|
|
812
|
+
else if (result !== value) throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options);
|
|
813
|
+
}
|
|
814
|
+
if (!strict && isPlainObject(data)) merge(result, data);
|
|
815
|
+
return [result];
|
|
816
|
+
});
|
|
817
|
+
Schema.extend("transform", (data, { inner, callback, preserve }, options) => {
|
|
818
|
+
const [result, adapted = data] = Schema.resolve(data, inner, options, true);
|
|
819
|
+
if (preserve) return [callback(result)];
|
|
820
|
+
else return [callback(result), callback(adapted)];
|
|
821
|
+
});
|
|
822
|
+
const formatters = {};
|
|
823
|
+
function defineMethod(name, keys, format) {
|
|
824
|
+
formatters[name] = format;
|
|
825
|
+
Object.assign(Schema, { [name](...args) {
|
|
826
|
+
const schema = new Schema({ type: name });
|
|
827
|
+
keys.forEach((key, index) => {
|
|
828
|
+
switch (key) {
|
|
829
|
+
case "sKey":
|
|
830
|
+
schema.sKey = args[index] ?? Schema.string();
|
|
831
|
+
break;
|
|
832
|
+
case "inner":
|
|
833
|
+
schema.inner = Schema.from(args[index]);
|
|
834
|
+
break;
|
|
835
|
+
case "list":
|
|
836
|
+
schema.list = args[index].map(Schema.from);
|
|
837
|
+
break;
|
|
838
|
+
case "dict":
|
|
839
|
+
schema.dict = mapValues(args[index], Schema.from);
|
|
840
|
+
break;
|
|
841
|
+
case "bits":
|
|
842
|
+
schema.bits = {};
|
|
843
|
+
for (const key in args[index]) {
|
|
844
|
+
if (typeof args[index][key] !== "number") continue;
|
|
845
|
+
schema.bits[key] = args[index][key];
|
|
846
|
+
}
|
|
847
|
+
break;
|
|
848
|
+
case "callback": {
|
|
849
|
+
const callback = schema.callback = args[index];
|
|
850
|
+
callback["toJSON"] ||= () => callback.toString();
|
|
851
|
+
break;
|
|
852
|
+
}
|
|
853
|
+
case "constructor": {
|
|
854
|
+
const constructor = schema.constructor = args[index];
|
|
855
|
+
if (typeof constructor === "function") constructor["toJSON"] ||= () => constructor["name"];
|
|
856
|
+
break;
|
|
857
|
+
}
|
|
858
|
+
default: schema[key] = args[index];
|
|
859
|
+
}
|
|
860
|
+
});
|
|
861
|
+
if (name === "object" || name === "dict") schema.meta.default = {};
|
|
862
|
+
else if (name === "array" || name === "tuple") schema.meta.default = [];
|
|
863
|
+
else if (name === "bitset") schema.meta.default = 0;
|
|
864
|
+
return schema;
|
|
865
|
+
} });
|
|
866
|
+
}
|
|
867
|
+
defineMethod("is", ["constructor"], ({ constructor }) => {
|
|
868
|
+
if (typeof constructor === "function") return constructor.name;
|
|
869
|
+
else return constructor;
|
|
870
|
+
});
|
|
871
|
+
defineMethod("any", [], () => "any");
|
|
872
|
+
defineMethod("never", [], () => "never");
|
|
873
|
+
defineMethod("const", ["value"], ({ value }) => typeof value === "string" ? JSON.stringify(value) : value);
|
|
874
|
+
defineMethod("string", [], () => "string");
|
|
875
|
+
defineMethod("number", [], () => "number");
|
|
876
|
+
defineMethod("boolean", [], () => "boolean");
|
|
877
|
+
defineMethod("bitset", ["bits"], () => "bitset");
|
|
878
|
+
defineMethod("function", [], () => "function");
|
|
879
|
+
defineMethod("array", ["inner"], ({ inner }) => `${inner.toString(true)}[]`);
|
|
880
|
+
defineMethod("dict", ["inner", "sKey"], ({ inner, sKey }) => `{ [key: ${sKey.toString()}]: ${inner.toString()} }`);
|
|
881
|
+
defineMethod("tuple", ["list"], ({ list }) => `[${list.map((inner) => inner.toString()).join(", ")}]`);
|
|
882
|
+
defineMethod("object", ["dict"], ({ dict }) => {
|
|
883
|
+
if (Object.keys(dict).length === 0) return "{}";
|
|
884
|
+
return `{ ${Object.entries(dict).map(([key, inner]) => {
|
|
885
|
+
return `${key}${inner.meta.required ? "" : "?"}: ${inner.toString()}`;
|
|
886
|
+
}).join(", ")} }`;
|
|
887
|
+
});
|
|
888
|
+
defineMethod("union", ["list"], ({ list }, inline) => {
|
|
889
|
+
const result = list.map(({ toString: format }) => format()).join(" | ");
|
|
890
|
+
return inline ? `(${result})` : result;
|
|
891
|
+
});
|
|
892
|
+
defineMethod("intersect", ["list"], ({ list }) => {
|
|
893
|
+
return `${list.map((inner) => inner.toString(true)).join(" & ")}`;
|
|
894
|
+
});
|
|
895
|
+
defineMethod("transform", [
|
|
896
|
+
"inner",
|
|
897
|
+
"callback",
|
|
898
|
+
"preserve"
|
|
899
|
+
], ({ inner }, isInner) => inner.toString(isInner));
|
|
900
|
+
//#endregion
|
|
901
|
+
//#region lib/types/client/target-key.js
|
|
902
|
+
/**
|
|
903
|
+
* Compare captured JSON targets independently of object key insertion order.
|
|
904
|
+
* @param target - captured immutable target.
|
|
905
|
+
* @returns canonical JSON identity.
|
|
906
|
+
*/
|
|
907
|
+
function menuTargetKey(target) {
|
|
908
|
+
return JSON.stringify(target, (_key, value) => {
|
|
909
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return value;
|
|
910
|
+
return Object.fromEntries(Object.entries(value).sort(([left], [right]) => left.localeCompare(right)));
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
//#endregion
|
|
914
|
+
//#region lib/types/client/registry.js
|
|
915
|
+
/** React-free registry; each occurrence captures the providers registered at opening. */
|
|
916
|
+
var MenuRegistry = class {
|
|
917
|
+
failed;
|
|
918
|
+
timeoutMs;
|
|
919
|
+
providers = /* @__PURE__ */ new Map();
|
|
920
|
+
occurrences = /* @__PURE__ */ new Set();
|
|
921
|
+
disposed = false;
|
|
922
|
+
constructor(failed, timeoutMs) {
|
|
923
|
+
this.failed = failed;
|
|
924
|
+
this.timeoutMs = timeoutMs;
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* Register actions for subsequent openings.
|
|
928
|
+
* @param input - provider definition.
|
|
929
|
+
* @returns removal of this contribution and its pending resolutions.
|
|
930
|
+
*/
|
|
931
|
+
register(input) {
|
|
932
|
+
if (this.disposed) throw new Error("Menu registry is disposed");
|
|
933
|
+
const key = `${encodeURIComponent(input.target)}/${encodeURIComponent(input.id)}`;
|
|
934
|
+
if (this.providers.has(key)) throw new Error(`Duplicate menu contribution: ${key}`);
|
|
935
|
+
const provider = {
|
|
936
|
+
id: input.id,
|
|
937
|
+
kind: input.target,
|
|
938
|
+
order: input.order,
|
|
939
|
+
lifetime: new AbortController(),
|
|
940
|
+
observe: (target) => input.observe?.(target.value) ?? [],
|
|
941
|
+
resolve: (target, signal) => input.resolve(target.value, signal)
|
|
942
|
+
};
|
|
943
|
+
this.providers.set(key, provider);
|
|
944
|
+
return () => {
|
|
945
|
+
if (provider.lifetime.signal.aborted) return;
|
|
946
|
+
this.providers.delete(key);
|
|
947
|
+
provider.lifetime.abort();
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* Resolve an independently owned occurrence.
|
|
952
|
+
* @param target - captured clicked identity.
|
|
953
|
+
* @returns presentation, selection, and disposal.
|
|
954
|
+
*/
|
|
955
|
+
resolve(target) {
|
|
956
|
+
if (this.disposed) throw new Error("Menu registry is disposed");
|
|
957
|
+
const lifetime = new AbortController();
|
|
958
|
+
const source = (0, _deepseek_ai_dsh_client_store.createSnapshotStore)([]);
|
|
959
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
960
|
+
const callbacks = /* @__PURE__ */ new Map();
|
|
961
|
+
const providers = [...this.providers.values()].filter((p) => p.kind === target.kind).sort((a, b) => a.order - b.order || a.id.localeCompare(b.id));
|
|
962
|
+
const refresh = () => {
|
|
963
|
+
source.set(providers.flatMap((provider) => provider.lifetime.signal.aborted ? [] : resolved.get(provider) ?? []));
|
|
964
|
+
};
|
|
965
|
+
const cleanup = [];
|
|
966
|
+
const dispose = () => {
|
|
967
|
+
if (lifetime.signal.aborted) return;
|
|
968
|
+
lifetime.abort();
|
|
969
|
+
for (const detach of cleanup) detach();
|
|
970
|
+
callbacks.clear();
|
|
971
|
+
this.occurrences.delete(dispose);
|
|
972
|
+
};
|
|
973
|
+
this.occurrences.add(dispose);
|
|
974
|
+
const pending = [];
|
|
975
|
+
for (const provider of providers) {
|
|
976
|
+
let preparation;
|
|
977
|
+
let actionIds = [];
|
|
978
|
+
let preparing;
|
|
979
|
+
const clear = () => {
|
|
980
|
+
for (const id of actionIds) callbacks.delete(id);
|
|
981
|
+
actionIds = [];
|
|
982
|
+
resolved.delete(provider);
|
|
983
|
+
refresh();
|
|
984
|
+
};
|
|
985
|
+
const prepare = () => {
|
|
986
|
+
preparation?.abort();
|
|
987
|
+
const deadline = new AbortController();
|
|
988
|
+
preparation = deadline;
|
|
989
|
+
const signal = AbortSignal.any([
|
|
990
|
+
lifetime.signal,
|
|
991
|
+
provider.lifetime.signal,
|
|
992
|
+
deadline.signal
|
|
993
|
+
]);
|
|
994
|
+
const accept = (actions) => {
|
|
995
|
+
if (signal.aborted) return;
|
|
996
|
+
const next = /* @__PURE__ */ new Map();
|
|
997
|
+
const project = (action, parent) => {
|
|
998
|
+
const id = `${parent}/${encodeURIComponent(action.id)}`;
|
|
999
|
+
if (next.has(id)) throw new Error(`Duplicate menu action: ${id}`);
|
|
1000
|
+
next.set(id, action);
|
|
1001
|
+
const { run: _run, failureLabel: _failureLabel, children, ...view } = action;
|
|
1002
|
+
return {
|
|
1003
|
+
...view,
|
|
1004
|
+
id,
|
|
1005
|
+
...children === void 0 ? {} : { children: children.map((child) => project(child, id)) }
|
|
1006
|
+
};
|
|
1007
|
+
};
|
|
1008
|
+
const views = actions.map((action) => project(action, encodeURIComponent(provider.id)));
|
|
1009
|
+
for (const id of actionIds) callbacks.delete(id);
|
|
1010
|
+
for (const [id, action] of next) callbacks.set(id, {
|
|
1011
|
+
provider,
|
|
1012
|
+
action
|
|
1013
|
+
});
|
|
1014
|
+
actionIds = [...next.keys()];
|
|
1015
|
+
resolved.set(provider, views);
|
|
1016
|
+
refresh();
|
|
1017
|
+
};
|
|
1018
|
+
preparing = void 0;
|
|
1019
|
+
try {
|
|
1020
|
+
const result = provider.resolve(target, signal);
|
|
1021
|
+
if (result instanceof Promise) clear();
|
|
1022
|
+
preparing = result instanceof Promise ? new Promise((settle) => {
|
|
1023
|
+
const timer = this.timeoutMs === void 0 ? void 0 : setTimeout(() => {
|
|
1024
|
+
deadline.abort();
|
|
1025
|
+
this.failed();
|
|
1026
|
+
}, this.timeoutMs);
|
|
1027
|
+
const finish = () => {
|
|
1028
|
+
clearTimeout(timer);
|
|
1029
|
+
signal.removeEventListener("abort", finish);
|
|
1030
|
+
settle();
|
|
1031
|
+
};
|
|
1032
|
+
signal.addEventListener("abort", finish, { once: true });
|
|
1033
|
+
result.then(accept).catch(() => {
|
|
1034
|
+
if (!signal.aborted) this.failed();
|
|
1035
|
+
}).finally(finish);
|
|
1036
|
+
}) : void 0;
|
|
1037
|
+
if (!(result instanceof Promise)) accept(result);
|
|
1038
|
+
} catch (_error) {
|
|
1039
|
+
if (!signal.aborted) {
|
|
1040
|
+
clear();
|
|
1041
|
+
this.failed();
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
};
|
|
1045
|
+
const subscriptions = [];
|
|
1046
|
+
const withdraw = () => {
|
|
1047
|
+
preparation?.abort();
|
|
1048
|
+
for (const detach of subscriptions.splice(0)) detach();
|
|
1049
|
+
clear();
|
|
1050
|
+
};
|
|
1051
|
+
provider.lifetime.signal.addEventListener("abort", withdraw, { once: true });
|
|
1052
|
+
cleanup.push(() => {
|
|
1053
|
+
provider.lifetime.signal.removeEventListener("abort", withdraw);
|
|
1054
|
+
withdraw();
|
|
1055
|
+
});
|
|
1056
|
+
try {
|
|
1057
|
+
for (const input of provider.observe(target)) subscriptions.push(input.subscribe(prepare));
|
|
1058
|
+
prepare();
|
|
1059
|
+
} catch (_error) {
|
|
1060
|
+
if (!lifetime.signal.aborted && !provider.lifetime.signal.aborted) this.failed();
|
|
1061
|
+
}
|
|
1062
|
+
pending.push((async () => {
|
|
1063
|
+
let current;
|
|
1064
|
+
do {
|
|
1065
|
+
current = preparing;
|
|
1066
|
+
await current;
|
|
1067
|
+
} while (current !== preparing);
|
|
1068
|
+
})());
|
|
1069
|
+
}
|
|
1070
|
+
return {
|
|
1071
|
+
source,
|
|
1072
|
+
dispose,
|
|
1073
|
+
ready: Promise.all(pending).then(() => {}),
|
|
1074
|
+
select: (id) => {
|
|
1075
|
+
const selected = callbacks.get(id);
|
|
1076
|
+
if (lifetime.signal.aborted || selected === void 0 || selected.provider.lifetime.signal.aborted || selected.action.disabled || selected.action.children !== void 0) return;
|
|
1077
|
+
const run = selected.action.run;
|
|
1078
|
+
dispose();
|
|
1079
|
+
try {
|
|
1080
|
+
Promise.resolve(run?.()).catch(() => {
|
|
1081
|
+
this.failed(selected.action.failureLabel);
|
|
1082
|
+
});
|
|
1083
|
+
} catch (_error) {
|
|
1084
|
+
this.failed(selected.action.failureLabel);
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
};
|
|
1088
|
+
}
|
|
1089
|
+
/** Cancel every occurrence and reject further registrations or resolutions. */
|
|
1090
|
+
dispose() {
|
|
1091
|
+
if (this.disposed) return;
|
|
1092
|
+
this.disposed = true;
|
|
1093
|
+
for (const dispose of this.occurrences) dispose();
|
|
1094
|
+
for (const provider of this.providers.values()) provider.lifetime.abort();
|
|
1095
|
+
this.providers.clear();
|
|
1096
|
+
}
|
|
1097
|
+
};
|
|
1098
|
+
//#endregion
|
|
1099
|
+
//#region lib/types/client/store.js
|
|
1100
|
+
/** Single-popup viewing state and feedback retained outside trigger components. */
|
|
1101
|
+
/**
|
|
1102
|
+
* Create the root popup store.
|
|
1103
|
+
* @returns the presentation and its mutation actions.
|
|
1104
|
+
*/
|
|
1105
|
+
function createMenuStore() {
|
|
1106
|
+
return (0, _deepseek_ai_dsh_client_store.defineStore)({
|
|
1107
|
+
init: () => ({
|
|
1108
|
+
occurrence: 0,
|
|
1109
|
+
closing: false,
|
|
1110
|
+
replacing: false,
|
|
1111
|
+
target: null,
|
|
1112
|
+
point: {
|
|
1113
|
+
x: 0,
|
|
1114
|
+
y: 0
|
|
1115
|
+
},
|
|
1116
|
+
items: [],
|
|
1117
|
+
notice: null
|
|
1118
|
+
}),
|
|
1119
|
+
actions: {
|
|
1120
|
+
open(draft, target, point) {
|
|
1121
|
+
draft.occurrence++;
|
|
1122
|
+
draft.closing = false;
|
|
1123
|
+
draft.replacing = false;
|
|
1124
|
+
draft.target = target;
|
|
1125
|
+
draft.point = point;
|
|
1126
|
+
draft.items = [];
|
|
1127
|
+
},
|
|
1128
|
+
items(draft, items) {
|
|
1129
|
+
draft.items = items;
|
|
1130
|
+
},
|
|
1131
|
+
exit(draft, replacing) {
|
|
1132
|
+
draft.closing = true;
|
|
1133
|
+
draft.replacing = replacing;
|
|
1134
|
+
},
|
|
1135
|
+
close(draft) {
|
|
1136
|
+
draft.target = null;
|
|
1137
|
+
draft.items = [];
|
|
1138
|
+
},
|
|
1139
|
+
notify(draft, notice) {
|
|
1140
|
+
draft.notice = notice;
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
//#endregion
|
|
1146
|
+
//#region lib/types/client/locales.js
|
|
1147
|
+
/** Context-menu operation feedback, separate from plugin display metadata. */
|
|
1148
|
+
const dictionaries = {
|
|
1149
|
+
zh: { failed: "操作失败,请重试" },
|
|
1150
|
+
en: { failed: "Could not complete the action. Try again." },
|
|
1151
|
+
ja: { failed: "操作に失敗しました。再試行してください。" },
|
|
1152
|
+
es: { failed: "No se pudo completar la acción. Inténtelo de nuevo." },
|
|
1153
|
+
fr: { failed: "Impossible de terminer l’action. Réessayez." },
|
|
1154
|
+
de: { failed: "Die Aktion konnte nicht abgeschlossen werden. Versuchen Sie es erneut." },
|
|
1155
|
+
ru: { failed: "Не удалось выполнить действие. Повторите попытку." },
|
|
1156
|
+
pt: { failed: "Não foi possível concluir a ação. Tente novamente." }
|
|
1157
|
+
};
|
|
1158
|
+
//#endregion
|
|
1159
|
+
//#region lib/types/client/View.js
|
|
1160
|
+
/** Shared Web renderer for Client menu contributions. */
|
|
1161
|
+
/**
|
|
1162
|
+
* @param actions - plain presentation.
|
|
1163
|
+
* @param catalog - effective user shortcuts.
|
|
1164
|
+
* @param renderSlot - owner-authorized keyed icon renderer.
|
|
1165
|
+
* @returns Existing Menu entries.
|
|
1166
|
+
*/
|
|
1167
|
+
function menuEntries(actions, catalog, renderSlot) {
|
|
1168
|
+
return actions.flatMap((action) => {
|
|
1169
|
+
const shortcut = catalog.find((entry) => entry.id === action.commandId);
|
|
1170
|
+
const item = {
|
|
1171
|
+
id: action.id,
|
|
1172
|
+
label: action.label,
|
|
1173
|
+
...shortcut === void 0 ? {} : { shortcut },
|
|
1174
|
+
...action.disabled === void 0 ? {} : { disabled: action.disabled },
|
|
1175
|
+
...action.danger === void 0 ? {} : { danger: action.danger },
|
|
1176
|
+
...action.icon === void 0 ? {} : { icon: renderSlot("contextMenu.icon", { icon: action.icon }, { entryKey: action.icon.key }) },
|
|
1177
|
+
...action.children === void 0 ? {} : { submenu: menuEntries(action.children, catalog, renderSlot) }
|
|
1178
|
+
};
|
|
1179
|
+
return action.separatorBefore ? [{
|
|
1180
|
+
type: "separator",
|
|
1181
|
+
id: `${action.id}:separator`
|
|
1182
|
+
}, item] : [item];
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
/** @param props - registered state, target-specific legacy rows, and actions. @returns one menu and persistent feedback. */
|
|
1186
|
+
function MenuOverlay({ useStore, actions, renderSlot, close, select, returnFocus, useCatalog }) {
|
|
1187
|
+
const catalog = useCatalog((value) => value);
|
|
1188
|
+
const { target, point, items, notice, occurrence, closing, replacing } = useStore((s) => s);
|
|
1189
|
+
return (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [(0, react_jsx_runtime.jsx)(_deepseek_ai_dsh_client_ui_primitives.Menu, {
|
|
1190
|
+
open: target !== null,
|
|
1191
|
+
anchor: null,
|
|
1192
|
+
portal: true,
|
|
1193
|
+
autoFocus: true,
|
|
1194
|
+
contextual: true,
|
|
1195
|
+
returnFocus,
|
|
1196
|
+
exiting: closing ? replacing ? "replace" : "dismiss" : void 0,
|
|
1197
|
+
getAnchorRect: () => new DOMRect(point.x, point.y, 0, 0),
|
|
1198
|
+
items: menuEntries(items.filter((item) => !item.afterContent), catalog, renderSlot),
|
|
1199
|
+
footer: menuEntries(items.filter((item) => item.afterContent), catalog, renderSlot),
|
|
1200
|
+
onClose: close,
|
|
1201
|
+
onSelect: select,
|
|
1202
|
+
children: target !== null && renderSlot("contextMenu.content", {
|
|
1203
|
+
target,
|
|
1204
|
+
dismiss: close
|
|
1205
|
+
}, { entryKey: target.kind })
|
|
1206
|
+
}, occurrence), notice !== null && (0, react_jsx_runtime.jsx)(_deepseek_ai_dsh_client_ui_primitives.Toast, {
|
|
1207
|
+
text: notice.text,
|
|
1208
|
+
...notice.action === void 0 ? {} : { actions: [notice.action] },
|
|
1209
|
+
...notice.success ? { tone: "success" } : {},
|
|
1210
|
+
onDone: () => {
|
|
1211
|
+
actions.notify(null);
|
|
1212
|
+
}
|
|
1213
|
+
}, notice.id)] });
|
|
1214
|
+
}
|
|
1215
|
+
//#endregion
|
|
1216
|
+
//#region lib/types/client/trigger.js
|
|
1217
|
+
/**
|
|
1218
|
+
* Create stable bindings for the plugin's injected hook.
|
|
1219
|
+
* @param open - Open against the supplied captured target and viewport position.
|
|
1220
|
+
* @param active - Whether the plugin still owns its service.
|
|
1221
|
+
* @returns Ordinary trigger-prop factory; no React subscription or context is created.
|
|
1222
|
+
*/
|
|
1223
|
+
function createContextMenuBindings(open, active) {
|
|
1224
|
+
return { getTriggerProps(target, handlers = {}) {
|
|
1225
|
+
if (target === void 0) return handlers;
|
|
1226
|
+
const show = (event, x, y) => {
|
|
1227
|
+
if (event.defaultPrevented || !active()) return;
|
|
1228
|
+
const captured = typeof target === "function" ? target() : target;
|
|
1229
|
+
event.preventDefault();
|
|
1230
|
+
event.stopPropagation();
|
|
1231
|
+
event.currentTarget.focus({ preventScroll: true });
|
|
1232
|
+
open(captured, {
|
|
1233
|
+
x,
|
|
1234
|
+
y
|
|
1235
|
+
});
|
|
1236
|
+
};
|
|
1237
|
+
return {
|
|
1238
|
+
...handlers,
|
|
1239
|
+
tabIndex: handlers.tabIndex ?? 0,
|
|
1240
|
+
onContextMenu(event) {
|
|
1241
|
+
handlers.onContextMenu?.(event);
|
|
1242
|
+
const rect = event.clientX === 0 && event.clientY === 0 ? event.currentTarget.getBoundingClientRect() : void 0;
|
|
1243
|
+
show(event, rect?.left ?? event.clientX, rect?.bottom ?? event.clientY);
|
|
1244
|
+
},
|
|
1245
|
+
onKeyDown(event) {
|
|
1246
|
+
handlers.onKeyDown?.(event);
|
|
1247
|
+
if (event.defaultPrevented || event.repeat || event.nativeEvent.isComposing || event.ctrlKey || event.altKey || event.metaKey || !(event.key === "ContextMenu" || event.key === "F10" && event.shiftKey)) return;
|
|
1248
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
1249
|
+
show(event, rect.left, rect.bottom);
|
|
1250
|
+
}
|
|
1251
|
+
};
|
|
1252
|
+
} };
|
|
1253
|
+
}
|
|
1254
|
+
//#endregion
|
|
1255
|
+
//#region lib/types/client/index.js
|
|
1256
|
+
/** Window-local menu capability and its Web overlay. */
|
|
1257
|
+
/** Validated Client menu preparation budget. */
|
|
1258
|
+
const Config = Schema.object({ resolutionTimeoutMs: Schema.natural().min(1).default(1e3) });
|
|
1259
|
+
/** Required overlay and localization services. */
|
|
1260
|
+
const inject = [
|
|
1261
|
+
"slots",
|
|
1262
|
+
"locale",
|
|
1263
|
+
"shortcuts"
|
|
1264
|
+
];
|
|
1265
|
+
/** @param ctx - plugin-owned Client context. @param config - validated preparation budget. */
|
|
1266
|
+
function apply(ctx, config = Config({})) {
|
|
1267
|
+
ctx.effect(() => ctx.locale.register("contextMenu", dictionaries), "context menu: locale");
|
|
1268
|
+
const t = ctx.locale.bind("contextMenu");
|
|
1269
|
+
const handle = createMenuStore();
|
|
1270
|
+
const instance = handle.create();
|
|
1271
|
+
const store = {
|
|
1272
|
+
...handle,
|
|
1273
|
+
create: () => instance
|
|
1274
|
+
};
|
|
1275
|
+
const targetSource = (0, _deepseek_ai_dsh_client_store.createSnapshotStore)(null);
|
|
1276
|
+
let sequence = 0;
|
|
1277
|
+
let active;
|
|
1278
|
+
let closeTimer;
|
|
1279
|
+
let openingTimer;
|
|
1280
|
+
let finishOpening;
|
|
1281
|
+
let returnFocus = null;
|
|
1282
|
+
let disposed = false;
|
|
1283
|
+
const notify = (text, success = false, action) => {
|
|
1284
|
+
if (disposed) return;
|
|
1285
|
+
const id = ++sequence;
|
|
1286
|
+
instance.actions.notify({
|
|
1287
|
+
id,
|
|
1288
|
+
text,
|
|
1289
|
+
success,
|
|
1290
|
+
...action === void 0 ? {} : { action: {
|
|
1291
|
+
label: action.label,
|
|
1292
|
+
onClick: () => {
|
|
1293
|
+
if (disposed || instance.getSnapshot().notice?.id !== id) return;
|
|
1294
|
+
instance.actions.notify(null);
|
|
1295
|
+
try {
|
|
1296
|
+
Promise.resolve(action.run()).catch(() => {
|
|
1297
|
+
notify(action.failureLabel);
|
|
1298
|
+
});
|
|
1299
|
+
} catch (_error) {
|
|
1300
|
+
notify(action.failureLabel);
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
} }
|
|
1304
|
+
});
|
|
1305
|
+
};
|
|
1306
|
+
const registry = new MenuRegistry((message) => {
|
|
1307
|
+
notify(message ?? t("failed"));
|
|
1308
|
+
}, config.resolutionTimeoutMs);
|
|
1309
|
+
const cancelOpening = () => {
|
|
1310
|
+
clearTimeout(openingTimer);
|
|
1311
|
+
finishOpening?.();
|
|
1312
|
+
finishOpening = void 0;
|
|
1313
|
+
active?.unsubscribe();
|
|
1314
|
+
active?.resolution.dispose();
|
|
1315
|
+
active = void 0;
|
|
1316
|
+
targetSource.set(null);
|
|
1317
|
+
};
|
|
1318
|
+
const dismiss = (replacing) => {
|
|
1319
|
+
cancelOpening();
|
|
1320
|
+
clearTimeout(closeTimer);
|
|
1321
|
+
if (instance.getSnapshot().target === null) return;
|
|
1322
|
+
instance.actions.exit(replacing);
|
|
1323
|
+
closeTimer = setTimeout(() => {
|
|
1324
|
+
instance.actions.close();
|
|
1325
|
+
}, replacing ? _deepseek_ai_dsh_client_ui_primitives.MENU_TRANSITION_MS.replace : _deepseek_ai_dsh_client_ui_primitives.MENU_TRANSITION_MS.dismiss);
|
|
1326
|
+
};
|
|
1327
|
+
const close = () => {
|
|
1328
|
+
dismiss(false);
|
|
1329
|
+
};
|
|
1330
|
+
const contextMenus = {
|
|
1331
|
+
active: targetSource,
|
|
1332
|
+
register: (input) => registry.register(input),
|
|
1333
|
+
resolve: (target) => registry.resolve(target),
|
|
1334
|
+
notify,
|
|
1335
|
+
close,
|
|
1336
|
+
open: (target, point) => {
|
|
1337
|
+
const previous = active;
|
|
1338
|
+
if (previous !== void 0 && Math.hypot(previous.point.x - point.x, previous.point.y - point.y) <= 4 && menuTargetKey(previous.target) === menuTargetKey(target)) {
|
|
1339
|
+
close();
|
|
1340
|
+
return;
|
|
1341
|
+
}
|
|
1342
|
+
const focused = document.activeElement;
|
|
1343
|
+
if (!(focused instanceof Element) || focused.closest("[data-context-menu]") === null) returnFocus = focused instanceof HTMLElement ? focused : null;
|
|
1344
|
+
dismiss(true);
|
|
1345
|
+
const resolution = registry.resolve(target);
|
|
1346
|
+
targetSource.set(target);
|
|
1347
|
+
let displayed = false;
|
|
1348
|
+
const publish = () => {
|
|
1349
|
+
if (displayed) instance.actions.items(resolution.source.getSnapshot());
|
|
1350
|
+
};
|
|
1351
|
+
const occurrence = {
|
|
1352
|
+
resolution,
|
|
1353
|
+
unsubscribe: resolution.source.subscribe(publish),
|
|
1354
|
+
target,
|
|
1355
|
+
point
|
|
1356
|
+
};
|
|
1357
|
+
active = occurrence;
|
|
1358
|
+
const delay = new Promise((resolve) => {
|
|
1359
|
+
finishOpening = resolve;
|
|
1360
|
+
openingTimer = setTimeout(resolve, _deepseek_ai_dsh_client_ui_primitives.MENU_TRANSITION_MS.prepare);
|
|
1361
|
+
});
|
|
1362
|
+
Promise.all([resolution.ready, delay]).then(() => {
|
|
1363
|
+
if (active !== occurrence) return;
|
|
1364
|
+
clearTimeout(closeTimer);
|
|
1365
|
+
instance.actions.open(target, point);
|
|
1366
|
+
displayed = true;
|
|
1367
|
+
publish();
|
|
1368
|
+
});
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
ctx.provide("contextMenus", contextMenus);
|
|
1372
|
+
const bindings = createContextMenuBindings((target, point) => {
|
|
1373
|
+
contextMenus.open(target, point);
|
|
1374
|
+
}, () => !disposed);
|
|
1375
|
+
ctx.slots.provideRoot({ props: { useContextMenu: () => bindings } });
|
|
1376
|
+
ctx.effect(() => {
|
|
1377
|
+
const outside = (event) => {
|
|
1378
|
+
if (event.button === 2 || event.target instanceof Element && event.target.closest("[role=\"menu\"], [data-context-menu-trigger]")) return;
|
|
1379
|
+
close();
|
|
1380
|
+
};
|
|
1381
|
+
const context = (event) => {
|
|
1382
|
+
if (!event.defaultPrevented) close();
|
|
1383
|
+
};
|
|
1384
|
+
const escape = (event) => {
|
|
1385
|
+
if (active !== void 0 && instance.getSnapshot().target === null && event.key === "Escape") {
|
|
1386
|
+
event.preventDefault();
|
|
1387
|
+
event.stopImmediatePropagation();
|
|
1388
|
+
close();
|
|
1389
|
+
}
|
|
1390
|
+
};
|
|
1391
|
+
document.addEventListener("pointerdown", outside);
|
|
1392
|
+
document.addEventListener("contextmenu", context);
|
|
1393
|
+
window.addEventListener("keydown", escape, true);
|
|
1394
|
+
return () => {
|
|
1395
|
+
disposed = true;
|
|
1396
|
+
cancelOpening();
|
|
1397
|
+
clearTimeout(closeTimer);
|
|
1398
|
+
registry.dispose();
|
|
1399
|
+
instance.actions.close();
|
|
1400
|
+
document.removeEventListener("pointerdown", outside);
|
|
1401
|
+
document.removeEventListener("contextmenu", context);
|
|
1402
|
+
window.removeEventListener("keydown", escape, true);
|
|
1403
|
+
};
|
|
1404
|
+
}, "context menu: lifetime");
|
|
1405
|
+
ctx.slots.inject("shell.overlay", () => ctx.slots.register({
|
|
1406
|
+
name: "shell.overlay",
|
|
1407
|
+
id: "context-menu",
|
|
1408
|
+
store,
|
|
1409
|
+
children: {
|
|
1410
|
+
"contextMenu.content": {
|
|
1411
|
+
kind: "keyed",
|
|
1412
|
+
scope: "root"
|
|
1413
|
+
},
|
|
1414
|
+
"contextMenu.icon": {
|
|
1415
|
+
kind: "keyed",
|
|
1416
|
+
scope: "root",
|
|
1417
|
+
inline: true
|
|
1418
|
+
}
|
|
1419
|
+
},
|
|
1420
|
+
inject: () => ({
|
|
1421
|
+
hooks: { catalog: ctx.shortcuts.catalog },
|
|
1422
|
+
close,
|
|
1423
|
+
returnFocus: () => returnFocus,
|
|
1424
|
+
select: (id) => {
|
|
1425
|
+
const selected = active?.resolution;
|
|
1426
|
+
active?.unsubscribe();
|
|
1427
|
+
active = void 0;
|
|
1428
|
+
targetSource.set(null);
|
|
1429
|
+
dismiss(false);
|
|
1430
|
+
selected?.select(id);
|
|
1431
|
+
selected?.dispose();
|
|
1432
|
+
}
|
|
1433
|
+
})
|
|
1434
|
+
}, MenuOverlay));
|
|
1435
|
+
}
|
|
1436
|
+
//#endregion
|
|
1437
|
+
exports.Config = Config;
|
|
1438
|
+
exports.apply = apply;
|
|
1439
|
+
exports.createMenuStore = createMenuStore;
|
|
1440
|
+
exports.inject = inject;
|
|
1441
|
+
return module.exports;
|
|
1442
|
+
}
|
|
1443
|
+
});
|
|
1444
|
+
|
|
1445
|
+
//# sourceMappingURL=client.js.map
|