@samline/string 1.0.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -3
- package/README.md +95 -32
- package/dist/browser/string.global.js +1 -1
- package/dist/browser/string.global.js.map +1 -1
- package/dist/chunk-3EFE2B7D.js +567 -0
- package/dist/chunk-3EFE2B7D.js.map +1 -0
- package/dist/index.cjs +503 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +280 -9
- package/dist/index.d.ts +280 -9
- package/dist/index.js +1 -1
- package/dist/vanilla/index.cjs +1239 -15
- package/dist/vanilla/index.cjs.map +1 -1
- package/dist/vanilla/index.d.cts +254 -1
- package/dist/vanilla/index.d.ts +254 -1
- package/dist/vanilla/index.js +714 -1
- package/dist/vanilla/index.js.map +1 -1
- package/package.json +56 -8
- package/dist/chunk-7CEOX6CZ.js +0 -105
- package/dist/chunk-7CEOX6CZ.js.map +0 -1
package/dist/vanilla/index.cjs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// src/core/words.ts
|
|
4
4
|
var words = (input) => {
|
|
5
5
|
if (typeof input !== "string" || input === "") return [];
|
|
6
|
-
return input.replace(/([\p{Ll}\p{N}])(\p{Lu})/gu, "$1 $2").replace(/(\p{Lu})(\p{Lu}\p{Ll})/gu, "$1 $2").split(/[^\p{L}\p{N}]+/u).filter(Boolean);
|
|
6
|
+
return input.replace(/([\p{Ll}\p{N}]\p{M}*)(\p{Lu})/gu, "$1 $2").replace(/(\p{Lu})(\p{Lu}\p{Ll})/gu, "$1 $2").split(/[^\p{L}\p{M}\p{N}]+/u).filter(Boolean);
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
// src/core/accents.ts
|
|
@@ -27,7 +27,105 @@ var TRANSLITERATIONS = {
|
|
|
27
27
|
\u0131: "i"
|
|
28
28
|
};
|
|
29
29
|
var SPECIAL = new RegExp(`[${Object.keys(TRANSLITERATIONS).join("")}]`, "g");
|
|
30
|
-
var
|
|
30
|
+
var escape = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
31
|
+
var patterns = /* @__PURE__ */ new WeakMap();
|
|
32
|
+
var patternFor = (map) => {
|
|
33
|
+
if (patterns.has(map)) return patterns.get(map);
|
|
34
|
+
const keys = Object.keys(map).filter(Boolean).sort((a, b) => b.length - a.length);
|
|
35
|
+
const pattern = keys.length ? new RegExp(keys.map(escape).join("|"), "gu") : null;
|
|
36
|
+
patterns.set(map, pattern);
|
|
37
|
+
return pattern;
|
|
38
|
+
};
|
|
39
|
+
var applyMap = (value, map) => {
|
|
40
|
+
const pattern = patternFor(map);
|
|
41
|
+
return pattern ? value.replace(pattern, (key) => map[key] ?? key) : value;
|
|
42
|
+
};
|
|
43
|
+
var removeAccents = (input, { charMap } = {}) => {
|
|
44
|
+
let value = String(input ?? "");
|
|
45
|
+
if (charMap) value = applyMap(value, charMap);
|
|
46
|
+
value = value.normalize("NFKD").replace(/\p{M}+/gu, "").replace(SPECIAL, (char) => TRANSLITERATIONS[char] ?? char);
|
|
47
|
+
return charMap ? applyMap(value, charMap) : value;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/core/charmaps.ts
|
|
51
|
+
var withUpper = (lower2) => {
|
|
52
|
+
const map = { ...lower2 };
|
|
53
|
+
for (const [char, latin] of Object.entries(lower2)) {
|
|
54
|
+
const upper2 = char.toUpperCase();
|
|
55
|
+
if (upper2 !== char && !(upper2 in map))
|
|
56
|
+
map[upper2] = latin.charAt(0).toUpperCase() + latin.slice(1);
|
|
57
|
+
}
|
|
58
|
+
return map;
|
|
59
|
+
};
|
|
60
|
+
var CYRILLIC = withUpper({
|
|
61
|
+
\u0430: "a",
|
|
62
|
+
\u0431: "b",
|
|
63
|
+
\u0432: "v",
|
|
64
|
+
\u0433: "g",
|
|
65
|
+
\u0491: "g",
|
|
66
|
+
\u0434: "d",
|
|
67
|
+
\u0435: "e",
|
|
68
|
+
\u0451: "yo",
|
|
69
|
+
\u0454: "ye",
|
|
70
|
+
\u0436: "zh",
|
|
71
|
+
\u0437: "z",
|
|
72
|
+
\u0438: "i",
|
|
73
|
+
\u0456: "i",
|
|
74
|
+
\u0457: "yi",
|
|
75
|
+
\u0439: "y",
|
|
76
|
+
\u043A: "k",
|
|
77
|
+
\u043B: "l",
|
|
78
|
+
\u043C: "m",
|
|
79
|
+
\u043D: "n",
|
|
80
|
+
\u043E: "o",
|
|
81
|
+
\u043F: "p",
|
|
82
|
+
\u0440: "r",
|
|
83
|
+
\u0441: "s",
|
|
84
|
+
\u0442: "t",
|
|
85
|
+
\u0443: "u",
|
|
86
|
+
\u045E: "u",
|
|
87
|
+
\u0444: "f",
|
|
88
|
+
\u0445: "kh",
|
|
89
|
+
\u0446: "ts",
|
|
90
|
+
\u0447: "ch",
|
|
91
|
+
\u0448: "sh",
|
|
92
|
+
\u0449: "shch",
|
|
93
|
+
\u044A: "",
|
|
94
|
+
\u044B: "y",
|
|
95
|
+
\u044C: "",
|
|
96
|
+
\u044D: "e",
|
|
97
|
+
\u044E: "yu",
|
|
98
|
+
\u044F: "ya"
|
|
99
|
+
});
|
|
100
|
+
var GREEK = withUpper({
|
|
101
|
+
\u03B1: "a",
|
|
102
|
+
\u03B2: "v",
|
|
103
|
+
\u03B3: "g",
|
|
104
|
+
\u03B4: "d",
|
|
105
|
+
\u03B5: "e",
|
|
106
|
+
\u03B6: "z",
|
|
107
|
+
\u03B7: "i",
|
|
108
|
+
\u03B8: "th",
|
|
109
|
+
\u03B9: "i",
|
|
110
|
+
\u03BA: "k",
|
|
111
|
+
\u03BB: "l",
|
|
112
|
+
\u03BC: "m",
|
|
113
|
+
\u03BD: "n",
|
|
114
|
+
\u03BE: "x",
|
|
115
|
+
\u03BF: "o",
|
|
116
|
+
\u03C0: "p",
|
|
117
|
+
\u03C1: "r",
|
|
118
|
+
\u03C3: "s",
|
|
119
|
+
\u03C2: "s",
|
|
120
|
+
\u03C4: "t",
|
|
121
|
+
\u03C5: "y",
|
|
122
|
+
\u03C6: "f",
|
|
123
|
+
\u03C7: "ch",
|
|
124
|
+
\u03C8: "ps",
|
|
125
|
+
\u03C9: "o"
|
|
126
|
+
});
|
|
127
|
+
var GERMAN = withUpper({ \u00E4: "ae", \u00F6: "oe", \u00FC: "ue" });
|
|
128
|
+
var CHAR_MAPS = { cyrillic: CYRILLIC, greek: GREEK, german: GERMAN };
|
|
31
129
|
|
|
32
130
|
// src/core/case.ts
|
|
33
131
|
var lower = (value, locale) => value.toLocaleLowerCase(locale);
|
|
@@ -43,11 +141,13 @@ var joinLower = (separator) => (input, { locale } = {}) => words(input).map((wor
|
|
|
43
141
|
var kebabCase = joinLower("-");
|
|
44
142
|
var snakeCase = joinLower("_");
|
|
45
143
|
var dotCase = joinLower(".");
|
|
144
|
+
var pathCase = joinLower("/");
|
|
145
|
+
var trainCase = (input, { locale } = {}) => words(input).map((word) => titleWord(word, locale)).join("-");
|
|
46
146
|
var constantCase = (input, { locale } = {}) => words(input).map((word) => upper(word, locale)).join("_");
|
|
47
147
|
var titleCase = (input, { locale, smallWords = [] } = {}) => {
|
|
48
148
|
const small = new Set(smallWords.map((word) => lower(word, locale)));
|
|
49
149
|
let index = 0;
|
|
50
|
-
return String(input ?? "").replace(/[\p{L}\p{N}]+/gu, (word) => {
|
|
150
|
+
return String(input ?? "").replace(/[\p{L}\p{M}\p{N}]+/gu, (word) => {
|
|
51
151
|
const first = index++ === 0;
|
|
52
152
|
const lowered = lower(word, locale);
|
|
53
153
|
return !first && small.has(lowered) ? lowered : upperFirst(lowered, locale);
|
|
@@ -61,21 +161,27 @@ var uncapitalize = (input, { locale } = {}) => {
|
|
|
61
161
|
};
|
|
62
162
|
|
|
63
163
|
// src/core/slugify.ts
|
|
64
|
-
var
|
|
164
|
+
var escape2 = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
65
165
|
var slugify = (input, {
|
|
66
166
|
separator = "-",
|
|
67
167
|
lower: lower2 = true,
|
|
68
168
|
replacements = {},
|
|
69
169
|
maxLength,
|
|
70
170
|
trim = true,
|
|
71
|
-
locale
|
|
171
|
+
locale,
|
|
172
|
+
charMap,
|
|
173
|
+
preserve = ""
|
|
72
174
|
} = {}) => {
|
|
73
175
|
let value = String(input ?? "");
|
|
74
|
-
|
|
176
|
+
const pairs = Array.isArray(replacements) ? replacements : Object.entries(replacements);
|
|
177
|
+
for (const [from, to] of pairs) {
|
|
178
|
+
value = typeof from === "string" ? value.split(from).join(` ${to} `) : value.replace(from, ` ${to} `);
|
|
179
|
+
}
|
|
75
180
|
if (lower2) value = value.toLocaleLowerCase(locale);
|
|
76
|
-
value = removeAccents(value);
|
|
77
|
-
const sep =
|
|
78
|
-
|
|
181
|
+
value = removeAccents(value, charMap ? { charMap } : {});
|
|
182
|
+
const sep = escape2(separator);
|
|
183
|
+
const kept = preserve.replace(/[\]\\^-]/g, "\\$&");
|
|
184
|
+
value = value.replace(new RegExp(`[^A-Za-z0-9${kept}]+`, "g"), separator);
|
|
79
185
|
if (separator) value = value.replace(new RegExp(`(?:${sep}){2,}`, "g"), separator);
|
|
80
186
|
if (trim && separator) value = value.replace(new RegExp(`^(?:${sep})+|(?:${sep})+$`, "g"), "");
|
|
81
187
|
else if (separator) value = value.replace(new RegExp(`^(?:${sep})+`), "");
|
|
@@ -87,35 +193,1153 @@ var slugify = (input, {
|
|
|
87
193
|
}
|
|
88
194
|
return value;
|
|
89
195
|
};
|
|
196
|
+
var LIMIT = 1e5;
|
|
197
|
+
var uniqueSlug = (input, taken, { start = 2, ...options } = {}) => {
|
|
198
|
+
const base = slugify(input, options);
|
|
199
|
+
let isTaken;
|
|
200
|
+
if (typeof taken === "function") isTaken = taken;
|
|
201
|
+
else {
|
|
202
|
+
const set = new Set(taken);
|
|
203
|
+
isTaken = (slug) => set.has(slug);
|
|
204
|
+
}
|
|
205
|
+
if (base && !isTaken(base)) return base;
|
|
206
|
+
const separator = options.separator ?? "-";
|
|
207
|
+
const { maxLength } = options;
|
|
208
|
+
const first = Number.isFinite(start) ? Math.floor(start) : 2;
|
|
209
|
+
for (let n = first; n < first + LIMIT; n++) {
|
|
210
|
+
const suffix = base ? `${separator}${n}` : String(n);
|
|
211
|
+
let head = base;
|
|
212
|
+
if (maxLength !== void 0 && maxLength >= 0 && head.length + suffix.length > maxLength) {
|
|
213
|
+
head = head.slice(0, Math.max(0, maxLength - suffix.length));
|
|
214
|
+
if (separator) while (head.endsWith(separator)) head = head.slice(0, -separator.length);
|
|
215
|
+
}
|
|
216
|
+
const candidate = head ? head + suffix : String(n);
|
|
217
|
+
if (!isTaken(candidate)) return candidate;
|
|
218
|
+
}
|
|
219
|
+
throw new RangeError(`uniqueSlug: no free slug for "${base}" after ${LIMIT} tries`);
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
// src/core/graphemes.ts
|
|
223
|
+
var SegmenterImpl = globalThis.Intl?.Segmenter;
|
|
224
|
+
var segmenters = /* @__PURE__ */ new Map();
|
|
225
|
+
var segmenter = (granularity, locale) => {
|
|
226
|
+
if (!SegmenterImpl) return void 0;
|
|
227
|
+
const key = `${granularity}|${locale === void 0 ? "" : [locale].flat().join(",")}`;
|
|
228
|
+
let instance = segmenters.get(key);
|
|
229
|
+
if (!instance) {
|
|
230
|
+
instance = new SegmenterImpl(locale, { granularity });
|
|
231
|
+
segmenters.set(key, instance);
|
|
232
|
+
}
|
|
233
|
+
return instance;
|
|
234
|
+
};
|
|
235
|
+
var graphemes = (input) => {
|
|
236
|
+
const text = String(input ?? "");
|
|
237
|
+
const instance = segmenter("grapheme");
|
|
238
|
+
return instance ? Array.from(instance.segment(text), (part) => part.segment) : Array.from(text);
|
|
239
|
+
};
|
|
90
240
|
|
|
91
241
|
// src/core/text.ts
|
|
92
|
-
var
|
|
93
|
-
|
|
242
|
+
var WORD_CHAR = /[\p{L}\p{N}]/u;
|
|
243
|
+
var EDGE = "[\\s.,;:!?-]+";
|
|
244
|
+
var TRAILING = new RegExp(`${EDGE}$`, "u");
|
|
245
|
+
var LEADING = new RegExp(`^${EDGE}`, "u");
|
|
246
|
+
var truncate = (input, length, { ellipsis = "\u2026", wordBoundary = true, position = "end" } = {}) => {
|
|
247
|
+
const chars = graphemes(input);
|
|
94
248
|
if (!Number.isFinite(length) || length < 0 || chars.length <= length) return chars.join("");
|
|
95
|
-
const room = Math.max(0, length -
|
|
249
|
+
const room = Math.max(0, length - graphemes(ellipsis).length);
|
|
250
|
+
if (position === "middle") {
|
|
251
|
+
const head = chars.slice(0, Math.ceil(room / 2)).join("");
|
|
252
|
+
const tail = room > 1 ? chars.slice(chars.length - Math.floor(room / 2)).join("") : "";
|
|
253
|
+
return head.replace(/\s+$/u, "") + ellipsis + tail.replace(/^\s+/u, "");
|
|
254
|
+
}
|
|
255
|
+
if (position === "start") {
|
|
256
|
+
const start = chars.length - room;
|
|
257
|
+
let cut2 = chars.slice(start).join("");
|
|
258
|
+
if (wordBoundary && room > 0 && WORD_CHAR.test(chars[start - 1])) {
|
|
259
|
+
const firstSpace = cut2.search(/\s/u);
|
|
260
|
+
if (firstSpace >= 0 && firstSpace < cut2.length - 1) cut2 = cut2.slice(firstSpace);
|
|
261
|
+
}
|
|
262
|
+
return ellipsis + cut2.replace(LEADING, "");
|
|
263
|
+
}
|
|
96
264
|
let cut = chars.slice(0, room).join("");
|
|
97
|
-
if (wordBoundary && chars[room] !== void 0 &&
|
|
265
|
+
if (wordBoundary && chars[room] !== void 0 && WORD_CHAR.test(chars[room])) {
|
|
98
266
|
const lastSpace = cut.search(/\s\S*$/u);
|
|
99
267
|
if (lastSpace > 0) cut = cut.slice(0, lastSpace);
|
|
100
268
|
}
|
|
101
|
-
return cut.replace(
|
|
269
|
+
return cut.replace(TRAILING, "") + ellipsis;
|
|
270
|
+
};
|
|
271
|
+
var initials = (input, { max = 2, locale } = {}) => words(String(input ?? "")).slice(0, Math.max(0, max)).map((word) => graphemes(word)[0].toLocaleUpperCase(locale)).join("");
|
|
272
|
+
|
|
273
|
+
// src/core/convert.ts
|
|
274
|
+
var CONVERSIONS = [
|
|
275
|
+
"camel",
|
|
276
|
+
"pascal",
|
|
277
|
+
"kebab",
|
|
278
|
+
"snake",
|
|
279
|
+
"dot",
|
|
280
|
+
"path",
|
|
281
|
+
"constant",
|
|
282
|
+
"train",
|
|
283
|
+
"title",
|
|
284
|
+
"sentence",
|
|
285
|
+
"capitalize",
|
|
286
|
+
"uncapitalize",
|
|
287
|
+
"lower",
|
|
288
|
+
"upper",
|
|
289
|
+
"slug",
|
|
290
|
+
"accents",
|
|
291
|
+
"truncate",
|
|
292
|
+
"initials"
|
|
293
|
+
];
|
|
294
|
+
var isConversion = (value) => CONVERSIONS.includes(value);
|
|
295
|
+
var convert = (input, to, options = {}) => {
|
|
296
|
+
const text = String(input ?? "");
|
|
297
|
+
const { locale } = options;
|
|
298
|
+
const caseOptions = locale === void 0 ? {} : { locale };
|
|
299
|
+
switch (to) {
|
|
300
|
+
case "camel":
|
|
301
|
+
return camelCase(text, caseOptions);
|
|
302
|
+
case "pascal":
|
|
303
|
+
return pascalCase(text, caseOptions);
|
|
304
|
+
case "kebab":
|
|
305
|
+
return kebabCase(text, caseOptions);
|
|
306
|
+
case "snake":
|
|
307
|
+
return snakeCase(text, caseOptions);
|
|
308
|
+
case "dot":
|
|
309
|
+
return dotCase(text, caseOptions);
|
|
310
|
+
case "path":
|
|
311
|
+
return pathCase(text, caseOptions);
|
|
312
|
+
case "constant":
|
|
313
|
+
return constantCase(text, caseOptions);
|
|
314
|
+
case "train":
|
|
315
|
+
return trainCase(text, caseOptions);
|
|
316
|
+
case "title":
|
|
317
|
+
return titleCase(text, options);
|
|
318
|
+
case "sentence":
|
|
319
|
+
return sentenceCase(text, caseOptions);
|
|
320
|
+
case "capitalize":
|
|
321
|
+
return capitalize(text, caseOptions);
|
|
322
|
+
case "uncapitalize":
|
|
323
|
+
return uncapitalize(text, caseOptions);
|
|
324
|
+
case "lower":
|
|
325
|
+
return text.toLocaleLowerCase(locale);
|
|
326
|
+
case "upper":
|
|
327
|
+
return text.toLocaleUpperCase(locale);
|
|
328
|
+
case "slug":
|
|
329
|
+
return options.taken === void 0 ? slugify(text, options) : uniqueSlug(text, options.taken, options);
|
|
330
|
+
case "accents":
|
|
331
|
+
return removeAccents(text, options.charMap ? { charMap: options.charMap } : {});
|
|
332
|
+
case "truncate":
|
|
333
|
+
return options.length === void 0 ? text : truncate(text, options.length, options);
|
|
334
|
+
case "initials":
|
|
335
|
+
return initials(text, options);
|
|
336
|
+
default:
|
|
337
|
+
return text;
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// src/core/search.ts
|
|
342
|
+
var fold = (input, { locale } = {}) => removeAccents(String(input ?? "").toLocaleLowerCase(locale));
|
|
343
|
+
var termsOf = (query, { locale, split = true }) => {
|
|
344
|
+
const folded = fold(query, locale === void 0 ? {} : { locale }).trim();
|
|
345
|
+
if (!folded) return [];
|
|
346
|
+
return split ? Array.from(new Set(folded.split(/\s+/u))) : [folded];
|
|
347
|
+
};
|
|
348
|
+
var contains = (input, query, options = {}) => {
|
|
349
|
+
const terms = termsOf(query, options);
|
|
350
|
+
if (terms.length === 0) return true;
|
|
351
|
+
const text = fold(input, options.locale === void 0 ? {} : { locale: options.locale });
|
|
352
|
+
return terms.every((term) => text.includes(term));
|
|
353
|
+
};
|
|
354
|
+
var ASCII = /^[\u0000-\u007f]*$/;
|
|
355
|
+
var highlight = (input, query, options = {}) => {
|
|
356
|
+
const chars = Array.from(String(input ?? ""));
|
|
357
|
+
if (chars.length === 0) return [];
|
|
358
|
+
const terms = termsOf(query, options);
|
|
359
|
+
if (terms.length === 0) return [{ text: chars.join(""), match: false }];
|
|
360
|
+
const locale = options.locale;
|
|
361
|
+
let folded = "";
|
|
362
|
+
const owner = [];
|
|
363
|
+
const empty = /* @__PURE__ */ new Set();
|
|
364
|
+
chars.forEach((char, index) => {
|
|
365
|
+
const lower2 = char.toLocaleLowerCase(locale);
|
|
366
|
+
const piece = ASCII.test(lower2) ? lower2 : removeAccents(lower2);
|
|
367
|
+
folded += piece;
|
|
368
|
+
if (piece === "") empty.add(index);
|
|
369
|
+
for (let unit = 0; unit < piece.length; unit++) owner.push(index);
|
|
370
|
+
});
|
|
371
|
+
const marked = new Array(chars.length).fill(false);
|
|
372
|
+
for (const term of terms) {
|
|
373
|
+
for (let at = folded.indexOf(term); at !== -1; at = folded.indexOf(term, at + term.length)) {
|
|
374
|
+
for (let index = owner[at]; index <= owner[at + term.length - 1]; index++) {
|
|
375
|
+
marked[index] = true;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
empty.forEach((index) => {
|
|
380
|
+
if (index > 0) marked[index] = marked[index - 1];
|
|
381
|
+
});
|
|
382
|
+
const segments = [];
|
|
383
|
+
chars.forEach((char, index) => {
|
|
384
|
+
const match = marked[index];
|
|
385
|
+
const last = segments[segments.length - 1];
|
|
386
|
+
if (last && last.match === match) last.text += char;
|
|
387
|
+
else segments.push({ text: char, match });
|
|
388
|
+
});
|
|
389
|
+
return segments;
|
|
390
|
+
};
|
|
391
|
+
var collators = /* @__PURE__ */ new Map();
|
|
392
|
+
var compareNatural = (a, b, { locale, sensitivity = "base" } = {}) => {
|
|
393
|
+
const key = `${locale === void 0 ? "" : [locale].flat().join(",")}|${sensitivity}`;
|
|
394
|
+
let collator = collators.get(key);
|
|
395
|
+
if (!collator) {
|
|
396
|
+
collator = new Intl.Collator(locale, { numeric: true, sensitivity });
|
|
397
|
+
collators.set(key, collator);
|
|
398
|
+
}
|
|
399
|
+
return collator.compare(String(a ?? ""), String(b ?? ""));
|
|
400
|
+
};
|
|
401
|
+
var distance = (a, b) => {
|
|
402
|
+
const left = Array.from(String(a ?? ""));
|
|
403
|
+
const right = Array.from(String(b ?? ""));
|
|
404
|
+
if (left.length === 0) return right.length;
|
|
405
|
+
if (right.length === 0) return left.length;
|
|
406
|
+
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
407
|
+
for (let i = 1; i <= left.length; i++) {
|
|
408
|
+
const current = [i];
|
|
409
|
+
for (let j = 1; j <= right.length; j++) {
|
|
410
|
+
const cost = left[i - 1] === right[j - 1] ? 0 : 1;
|
|
411
|
+
current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
|
|
412
|
+
}
|
|
413
|
+
previous = current;
|
|
414
|
+
}
|
|
415
|
+
return previous[right.length];
|
|
416
|
+
};
|
|
417
|
+
var similarity = (a, b, { fold: shouldFold = true, locale } = {}) => {
|
|
418
|
+
const options = locale === void 0 ? {} : { locale };
|
|
419
|
+
const left = shouldFold ? fold(a, options) : String(a ?? "");
|
|
420
|
+
const right = shouldFold ? fold(b, options) : String(b ?? "");
|
|
421
|
+
const longest = Math.max(Array.from(left).length, Array.from(right).length);
|
|
422
|
+
return longest === 0 ? 1 : 1 - distance(left, right) / longest;
|
|
423
|
+
};
|
|
424
|
+
var PARTIAL_WEIGHT = 0.98;
|
|
425
|
+
var partialScore = (input, candidate) => {
|
|
426
|
+
const left = Array.from(input);
|
|
427
|
+
const right = Array.from(candidate);
|
|
428
|
+
if (left.length < 3 || right.length <= left.length) return 0;
|
|
429
|
+
const needle = left.join("");
|
|
430
|
+
let best = 0;
|
|
431
|
+
right.forEach((char, index) => {
|
|
432
|
+
if (index > 0 && !/[\s\p{P}]/u.test(right[index - 1])) return;
|
|
433
|
+
if (/\s/u.test(char)) return;
|
|
434
|
+
const piece = right.slice(index, index + left.length).join("");
|
|
435
|
+
best = Math.max(best, similarity(needle, piece, { fold: false }));
|
|
436
|
+
});
|
|
437
|
+
return best * PARTIAL_WEIGHT;
|
|
438
|
+
};
|
|
439
|
+
var closest = (input, candidates, { threshold = 0.5, partial = true, ...options } = {}) => {
|
|
440
|
+
const shouldFold = options.fold ?? true;
|
|
441
|
+
const foldOptions = options.locale === void 0 ? {} : { locale: options.locale };
|
|
442
|
+
const prepare = (value) => shouldFold ? fold(value, foldOptions) : String(value ?? "");
|
|
443
|
+
const needle = prepare(input);
|
|
444
|
+
let best;
|
|
445
|
+
let bestScore = -1;
|
|
446
|
+
for (const candidate of candidates) {
|
|
447
|
+
const whole = similarity(input, candidate, options);
|
|
448
|
+
const score = partial ? Math.max(whole, partialScore(needle, prepare(candidate))) : whole;
|
|
449
|
+
if (score > bestScore) {
|
|
450
|
+
best = candidate;
|
|
451
|
+
bestScore = score;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return bestScore >= threshold ? best : void 0;
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
// src/core/prose.ts
|
|
458
|
+
var wordCount = (input, { locale } = {}) => {
|
|
459
|
+
const text = String(input ?? "");
|
|
460
|
+
const instance = segmenter("word", locale);
|
|
461
|
+
if (!instance) return text.split(/[^\p{L}\p{M}\p{N}'’]+/u).filter(Boolean).length;
|
|
462
|
+
let count2 = 0;
|
|
463
|
+
for (const part of instance.segment(text)) if (part.isWordLike) count2++;
|
|
464
|
+
return count2;
|
|
465
|
+
};
|
|
466
|
+
var readingTime = (input, { wordsPerMinute = 200, ...options } = {}) => {
|
|
467
|
+
const words2 = wordCount(input, options);
|
|
468
|
+
const speed = Number.isFinite(wordsPerMinute) && wordsPerMinute > 0 ? wordsPerMinute : 200;
|
|
469
|
+
return { words: words2, minutes: words2 === 0 ? 0 : Math.max(1, Math.ceil(words2 / speed)) };
|
|
470
|
+
};
|
|
471
|
+
var pluralRules = /* @__PURE__ */ new Map();
|
|
472
|
+
var numberFormats = /* @__PURE__ */ new Map();
|
|
473
|
+
var keyOf = (locale) => locale === void 0 ? "" : [locale].flat().join(",");
|
|
474
|
+
var pluralize = (count2, forms, { locale, ordinal = false } = {}) => {
|
|
475
|
+
const key = keyOf(locale);
|
|
476
|
+
const rulesKey = `${key}|${ordinal}`;
|
|
477
|
+
let rules = pluralRules.get(rulesKey);
|
|
478
|
+
if (!rules) {
|
|
479
|
+
rules = new Intl.PluralRules(locale, { type: ordinal ? "ordinal" : "cardinal" });
|
|
480
|
+
pluralRules.set(rulesKey, rules);
|
|
481
|
+
}
|
|
482
|
+
let format = numberFormats.get(key);
|
|
483
|
+
if (!format) {
|
|
484
|
+
format = new Intl.NumberFormat(locale);
|
|
485
|
+
numberFormats.set(key, format);
|
|
486
|
+
}
|
|
487
|
+
const category = rules.select(count2);
|
|
488
|
+
const form = count2 === 0 && forms.zero || forms[category] || forms.other;
|
|
489
|
+
return form.replace(/#/g, format.format(count2));
|
|
490
|
+
};
|
|
491
|
+
var template = (input, values, { missing } = {}) => String(input ?? "").replace(
|
|
492
|
+
/\{\{|\}\}|\{\s*([^{}\s]+)\s*\}/g,
|
|
493
|
+
(match, path) => {
|
|
494
|
+
if (path === void 0) return match[0];
|
|
495
|
+
let value = values;
|
|
496
|
+
for (const key of path.split(".")) {
|
|
497
|
+
value = value !== null && typeof value === "object" && Object.prototype.hasOwnProperty.call(value, key) ? value[key] : void 0;
|
|
498
|
+
}
|
|
499
|
+
if (value !== void 0 && value !== null) return String(value);
|
|
500
|
+
if (missing === void 0) return match;
|
|
501
|
+
return typeof missing === "function" ? missing(path) : missing;
|
|
502
|
+
}
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
// src/core/html.ts
|
|
506
|
+
var ESCAPES = {
|
|
507
|
+
"&": "&",
|
|
508
|
+
"<": "<",
|
|
509
|
+
">": ">",
|
|
510
|
+
'"': """,
|
|
511
|
+
"'": "'"
|
|
512
|
+
};
|
|
513
|
+
var escapeHtml = (input) => String(input ?? "").replace(/[&<>"']/g, (char) => ESCAPES[char]);
|
|
514
|
+
var NAMED = {
|
|
515
|
+
amp: "&",
|
|
516
|
+
lt: "<",
|
|
517
|
+
gt: ">",
|
|
518
|
+
quot: '"',
|
|
519
|
+
apos: "'",
|
|
520
|
+
nbsp: "\xA0",
|
|
521
|
+
copy: "\xA9",
|
|
522
|
+
reg: "\xAE",
|
|
523
|
+
hellip: "\u2026",
|
|
524
|
+
mdash: "\u2014",
|
|
525
|
+
ndash: "\u2013",
|
|
526
|
+
laquo: "\xAB",
|
|
527
|
+
raquo: "\xBB"
|
|
528
|
+
};
|
|
529
|
+
var fromCodePoint = (code, entity) => Number.isInteger(code) && code > 0 && code <= 1114111 && (code < 55296 || code > 57343) ? String.fromCodePoint(code) : entity;
|
|
530
|
+
var unescapeHtml = (input) => String(input ?? "").replace(/&(#x[\da-f]+|#\d+|[a-z]+);/gi, (entity, body) => {
|
|
531
|
+
if (body[0] !== "#") return NAMED[body.toLowerCase()] ?? entity;
|
|
532
|
+
const hex = body[1] === "x" || body[1] === "X";
|
|
533
|
+
return fromCodePoint(parseInt(body.slice(hex ? 2 : 1), hex ? 16 : 10), entity);
|
|
534
|
+
});
|
|
535
|
+
var BLOCK = /<\/?(?:p|div|br|li|ul|ol|h[1-6]|tr|td|th|table|section|article|header|footer|blockquote|pre|hr)\b[^>]*>/gi;
|
|
536
|
+
var stripTags = (input) => unescapeHtml(
|
|
537
|
+
String(input ?? "").replace(/<!--[\s\S]*?-->/g, "").replace(/<(script|style|template|noscript)\b[\s\S]*?<\/\1\s*>/gi, "").replace(BLOCK, " ").replace(/<[^>]*>/g, "")
|
|
538
|
+
).replace(/\s+/g, " ").trim();
|
|
539
|
+
|
|
540
|
+
// src/core/color.ts
|
|
541
|
+
var hash = (text) => {
|
|
542
|
+
let value = 2166136261;
|
|
543
|
+
for (let index = 0; index < text.length; index++) {
|
|
544
|
+
value ^= text.charCodeAt(index);
|
|
545
|
+
value = Math.imul(value, 16777619);
|
|
546
|
+
}
|
|
547
|
+
return value >>> 0;
|
|
548
|
+
};
|
|
549
|
+
var whiteContrast = (hue, saturation, lightness) => {
|
|
550
|
+
const s = saturation / 100;
|
|
551
|
+
const l = lightness / 100;
|
|
552
|
+
const channel = (n) => {
|
|
553
|
+
const k = (n + hue / 30) % 12;
|
|
554
|
+
const value = l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k - 3, 9 - k, 1));
|
|
555
|
+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
|
|
556
|
+
};
|
|
557
|
+
const luminance = 0.2126 * channel(0) + 0.7152 * channel(8) + 0.0722 * channel(4);
|
|
558
|
+
return 1.05 / (luminance + 0.05);
|
|
559
|
+
};
|
|
560
|
+
var hashColor = (input, { saturation = 55, lightness = 45, contrast = 4.5 } = {}) => {
|
|
561
|
+
const hue = hash(fold(String(input ?? "").trim())) % 360;
|
|
562
|
+
let light = Math.max(0, Math.min(100, lightness));
|
|
563
|
+
while (light > 0 && whiteContrast(hue, saturation, light) < contrast) light--;
|
|
564
|
+
return `hsl(${hue} ${saturation}% ${light}%)`;
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
// node_modules/.pnpm/@samline+internal@1.0.0/node_modules/@samline/internal/dist/index.js
|
|
568
|
+
var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
|
|
569
|
+
var noop = () => {
|
|
570
|
+
};
|
|
571
|
+
var isElement = (value) => typeof Element !== "undefined" && value instanceof Element;
|
|
572
|
+
var resolveElements = (target, root = isBrowser() ? document : null) => {
|
|
573
|
+
if (target == null) return [];
|
|
574
|
+
if (isElement(target)) return [target];
|
|
575
|
+
if (typeof target === "string") {
|
|
576
|
+
if (!root) return [];
|
|
577
|
+
return Array.from(root.querySelectorAll(target));
|
|
578
|
+
}
|
|
579
|
+
const source = typeof target[Symbol.iterator] === "function" ? Array.from(target) : Array.from(target);
|
|
580
|
+
return Array.from(new Set(source.filter(isElement)));
|
|
581
|
+
};
|
|
582
|
+
var read = (el, name) => el.hasAttribute(name) ? el.getAttribute(name) : null;
|
|
583
|
+
var readString = (el, name, fallback) => {
|
|
584
|
+
const raw = read(el, name);
|
|
585
|
+
return raw === null || raw.trim() === "" ? fallback : raw.trim();
|
|
586
|
+
};
|
|
587
|
+
var readBoolean = (el, name, fallback) => {
|
|
588
|
+
const raw = read(el, name);
|
|
589
|
+
if (raw === null) return fallback;
|
|
590
|
+
const value = raw.trim().toLowerCase();
|
|
591
|
+
if (value === "" || value === "true" || value === name.toLowerCase()) return true;
|
|
592
|
+
if (value === "false") return false;
|
|
593
|
+
return fallback;
|
|
594
|
+
};
|
|
595
|
+
var readNumber = (el, name, fallback) => {
|
|
596
|
+
const raw = read(el, name);
|
|
597
|
+
if (raw === null || raw.trim() === "") return fallback;
|
|
598
|
+
const value = Number(raw);
|
|
599
|
+
return Number.isFinite(value) ? value : fallback;
|
|
600
|
+
};
|
|
601
|
+
var readEnum = (el, name, allowed, fallback) => {
|
|
602
|
+
const raw = read(el, name)?.trim();
|
|
603
|
+
return raw !== void 0 && allowed.includes(raw) ? raw : fallback;
|
|
604
|
+
};
|
|
605
|
+
var autoInit = ({
|
|
606
|
+
selector,
|
|
607
|
+
init,
|
|
608
|
+
root,
|
|
609
|
+
observe = true
|
|
610
|
+
}) => {
|
|
611
|
+
if (!isBrowser()) return { refresh: noop, destroy: noop };
|
|
612
|
+
const scope = root ?? document;
|
|
613
|
+
const cleanups = /* @__PURE__ */ new Map();
|
|
614
|
+
const initOne = (element) => {
|
|
615
|
+
if (cleanups.has(element)) return;
|
|
616
|
+
cleanups.set(element, init(element) ?? noop);
|
|
617
|
+
};
|
|
618
|
+
const destroyOne = (element) => {
|
|
619
|
+
const cleanup = cleanups.get(element);
|
|
620
|
+
if (!cleanup) return;
|
|
621
|
+
cleanups.delete(element);
|
|
622
|
+
cleanup();
|
|
623
|
+
};
|
|
624
|
+
const forEachMatch = (node, callback) => {
|
|
625
|
+
if (!(node instanceof Element)) return;
|
|
626
|
+
if (node.matches(selector)) callback(node);
|
|
627
|
+
node.querySelectorAll(selector).forEach(callback);
|
|
628
|
+
};
|
|
629
|
+
const refresh = () => scope.querySelectorAll(selector).forEach(initOne);
|
|
630
|
+
let observer = null;
|
|
631
|
+
if (observe && typeof MutationObserver === "function") {
|
|
632
|
+
observer = new MutationObserver((mutations) => {
|
|
633
|
+
for (const mutation of mutations) {
|
|
634
|
+
mutation.removedNodes.forEach(
|
|
635
|
+
(node) => forEachMatch(node, (element) => {
|
|
636
|
+
if (!element.isConnected) destroyOne(element);
|
|
637
|
+
})
|
|
638
|
+
);
|
|
639
|
+
mutation.addedNodes.forEach((node) => forEachMatch(node, initOne));
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
const target = scope instanceof Document ? scope.documentElement : scope;
|
|
643
|
+
observer.observe(target, { childList: true, subtree: true });
|
|
644
|
+
}
|
|
645
|
+
refresh();
|
|
646
|
+
return {
|
|
647
|
+
refresh,
|
|
648
|
+
destroy: () => {
|
|
649
|
+
observer?.disconnect();
|
|
650
|
+
observer = null;
|
|
651
|
+
Array.from(cleanups.keys()).forEach(destroyOne);
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
// src/vanilla/string.ts
|
|
657
|
+
var ATTRIBUTE = "data-string";
|
|
658
|
+
var STATE_ATTRIBUTE = "data-string-state";
|
|
659
|
+
var CHANGE_EVENT = "string:change";
|
|
660
|
+
var COLOR_PROPERTY = "--string-color";
|
|
661
|
+
var POSITIONS = ["end", "middle", "start"];
|
|
662
|
+
var TYPING_SEPARATOR = {
|
|
663
|
+
kebab: "-",
|
|
664
|
+
snake: "_",
|
|
665
|
+
dot: ".",
|
|
666
|
+
path: "/",
|
|
667
|
+
constant: "_",
|
|
668
|
+
train: "-",
|
|
669
|
+
camel: " ",
|
|
670
|
+
pascal: " "
|
|
671
|
+
};
|
|
672
|
+
var isField = (element) => element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement;
|
|
673
|
+
var textOf = (element) => isField(element) ? element.value : element.textContent ?? "";
|
|
674
|
+
var parseReplacements = (raw) => {
|
|
675
|
+
try {
|
|
676
|
+
const value = JSON.parse(raw);
|
|
677
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
678
|
+
return Object.fromEntries(
|
|
679
|
+
Object.entries(value).filter(
|
|
680
|
+
(entry) => typeof entry[1] === "string"
|
|
681
|
+
)
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
} catch {
|
|
685
|
+
}
|
|
686
|
+
return void 0;
|
|
687
|
+
};
|
|
688
|
+
var readConfig = (element, options) => {
|
|
689
|
+
const raw = element.getAttribute(ATTRIBUTE)?.trim();
|
|
690
|
+
const to = isConversion(raw) ? raw : options.to;
|
|
691
|
+
if (!to) return null;
|
|
692
|
+
const { to: _to, source: sourceOption, live, onChange: _onChange, color, ...base } = options;
|
|
693
|
+
const converted = { ...base };
|
|
694
|
+
const attr = (name) => `${ATTRIBUTE}-${name}`;
|
|
695
|
+
const has = (name) => element.hasAttribute(attr(name));
|
|
696
|
+
const locale = readString(element, attr("locale"), "") || base.locale || element.closest("[lang]")?.getAttribute("lang") || "";
|
|
697
|
+
if (locale) converted.locale = locale;
|
|
698
|
+
else delete converted.locale;
|
|
699
|
+
if (has("separator")) converted.separator = element.getAttribute(attr("separator")) ?? "";
|
|
700
|
+
if (has("lower")) converted.lower = readBoolean(element, attr("lower"), base.lower ?? true);
|
|
701
|
+
if (has("max-length")) {
|
|
702
|
+
const maxLength = readNumber(element, attr("max-length"), NaN);
|
|
703
|
+
if (Number.isFinite(maxLength)) converted.maxLength = maxLength;
|
|
704
|
+
}
|
|
705
|
+
if (has("replacements")) {
|
|
706
|
+
const replacements = parseReplacements(element.getAttribute(attr("replacements")) ?? "");
|
|
707
|
+
if (replacements) converted.replacements = replacements;
|
|
708
|
+
}
|
|
709
|
+
if (has("preserve")) converted.preserve = element.getAttribute(attr("preserve")) ?? "";
|
|
710
|
+
if (has("transliterate")) {
|
|
711
|
+
const names = readString(element, attr("transliterate"), "").split(/[\s,]+/);
|
|
712
|
+
const maps = names.filter((name) => name in CHAR_MAPS).map((name) => CHAR_MAPS[name]);
|
|
713
|
+
if (maps.length) converted.charMap = Object.assign({}, base.charMap, ...maps);
|
|
714
|
+
}
|
|
715
|
+
if (has("taken")) {
|
|
716
|
+
const raw2 = element.getAttribute(attr("taken")) ?? "";
|
|
717
|
+
let taken;
|
|
718
|
+
try {
|
|
719
|
+
taken = JSON.parse(raw2);
|
|
720
|
+
} catch {
|
|
721
|
+
taken = raw2.split(/[\s,]+/);
|
|
722
|
+
}
|
|
723
|
+
if (Array.isArray(taken)) {
|
|
724
|
+
converted.taken = taken.filter(
|
|
725
|
+
(slug) => typeof slug === "string" && slug !== ""
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
if (has("small-words")) {
|
|
730
|
+
converted.smallWords = readString(element, attr("small-words"), "").split(/[\s,]+/).filter(Boolean);
|
|
731
|
+
}
|
|
732
|
+
if (has("length")) {
|
|
733
|
+
const length = readNumber(element, attr("length"), NaN);
|
|
734
|
+
if (Number.isFinite(length)) converted.length = length;
|
|
735
|
+
}
|
|
736
|
+
if (has("ellipsis")) converted.ellipsis = element.getAttribute(attr("ellipsis")) ?? "";
|
|
737
|
+
if (has("position"))
|
|
738
|
+
converted.position = readEnum(element, attr("position"), POSITIONS, base.position ?? "end");
|
|
739
|
+
if (has("word-boundary"))
|
|
740
|
+
converted.wordBoundary = readBoolean(element, attr("word-boundary"), true);
|
|
741
|
+
if (has("max")) {
|
|
742
|
+
const max = readNumber(element, attr("max"), NaN);
|
|
743
|
+
if (Number.isFinite(max)) converted.max = max;
|
|
744
|
+
}
|
|
745
|
+
const sourceSelector = readString(element, attr("source"), "");
|
|
746
|
+
const source = resolveElements(sourceSelector || sourceOption)[0] ?? null;
|
|
747
|
+
return {
|
|
748
|
+
to,
|
|
749
|
+
options: converted,
|
|
750
|
+
source: source === element ? null : source,
|
|
751
|
+
live: readBoolean(element, attr("live"), live ?? true),
|
|
752
|
+
color: readBoolean(element, attr("color"), Boolean(color)) ? typeof color === "object" ? color : {} : null
|
|
753
|
+
};
|
|
754
|
+
};
|
|
755
|
+
var convertWhileTyping = (value, { to, options }) => {
|
|
756
|
+
if (to === "slug") {
|
|
757
|
+
const { taken: _taken, ...typing } = options;
|
|
758
|
+
return convert(value, to, { ...typing, trim: false });
|
|
759
|
+
}
|
|
760
|
+
const separator = TYPING_SEPARATOR[to];
|
|
761
|
+
const result = convert(value, to, options);
|
|
762
|
+
return separator !== void 0 && result && /[^\p{L}\p{N}]$/u.test(value) ? result + separator : result;
|
|
763
|
+
};
|
|
764
|
+
var bind = (element, options) => {
|
|
765
|
+
let config = readConfig(element, options);
|
|
766
|
+
if (!config) return null;
|
|
767
|
+
const field = isField(element) ? element : null;
|
|
768
|
+
const originalNodes = Array.from(element.childNodes);
|
|
769
|
+
const originalText = textOf(element);
|
|
770
|
+
let detachSource = noop;
|
|
771
|
+
let state = "ready";
|
|
772
|
+
let composing = false;
|
|
773
|
+
const setState = (next) => {
|
|
774
|
+
state = next;
|
|
775
|
+
element.setAttribute(STATE_ATTRIBUTE, next);
|
|
776
|
+
};
|
|
777
|
+
const notify = (value) => {
|
|
778
|
+
options.onChange?.(value, element);
|
|
779
|
+
element.dispatchEvent(
|
|
780
|
+
new CustomEvent(CHANGE_EVENT, { bubbles: true, detail: { value, state } })
|
|
781
|
+
);
|
|
782
|
+
};
|
|
783
|
+
const authored = /* @__PURE__ */ new Map();
|
|
784
|
+
const describe = (name, value) => {
|
|
785
|
+
if (!authored.has(name)) authored.set(name, element.getAttribute(name));
|
|
786
|
+
if (authored.get(name) !== null) return;
|
|
787
|
+
if (value === null) element.removeAttribute(name);
|
|
788
|
+
else element.setAttribute(name, value);
|
|
789
|
+
};
|
|
790
|
+
const setFieldValue = (value) => {
|
|
791
|
+
if (!field || field.value === value) return;
|
|
792
|
+
const focused = field.ownerDocument.activeElement === field;
|
|
793
|
+
let fromEnd = 0;
|
|
794
|
+
try {
|
|
795
|
+
fromEnd = field.value.length - (field.selectionEnd ?? field.value.length);
|
|
796
|
+
} catch {
|
|
797
|
+
}
|
|
798
|
+
field.value = value;
|
|
799
|
+
if (focused) {
|
|
800
|
+
const caret = Math.max(0, value.length - fromEnd);
|
|
801
|
+
try {
|
|
802
|
+
field.setSelectionRange(caret, caret);
|
|
803
|
+
} catch {
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
notify(value);
|
|
807
|
+
};
|
|
808
|
+
const render = (text) => {
|
|
809
|
+
const current = config;
|
|
810
|
+
const output = convert(text, current.to, current.options);
|
|
811
|
+
if (field) return setFieldValue(output);
|
|
812
|
+
if (element.textContent !== output) {
|
|
813
|
+
element.textContent = output;
|
|
814
|
+
notify(output);
|
|
815
|
+
}
|
|
816
|
+
describe("title", current.to === "truncate" && output !== text ? text : null);
|
|
817
|
+
const label = current.to === "initials" ? text.trim() : "";
|
|
818
|
+
describe("role", label ? "img" : null);
|
|
819
|
+
describe("aria-label", label || null);
|
|
820
|
+
if (current.color) element.style.setProperty(COLOR_PROPERTY, hashColor(text, current.color));
|
|
821
|
+
else element.style.removeProperty(COLOR_PROPERTY);
|
|
822
|
+
};
|
|
823
|
+
const fromSource = () => config.source && render(textOf(config.source));
|
|
824
|
+
const onSourceInput = () => {
|
|
825
|
+
if (!field || state === "following") fromSource();
|
|
826
|
+
};
|
|
827
|
+
const onFieldInput = () => {
|
|
828
|
+
if (!field || composing) return;
|
|
829
|
+
if (config.source) {
|
|
830
|
+
if (field.value === "") return setState("following");
|
|
831
|
+
if (state !== "edited") setState("edited");
|
|
832
|
+
}
|
|
833
|
+
if (config.live && config.to !== "truncate" && config.to !== "initials") {
|
|
834
|
+
setFieldValue(convertWhileTyping(field.value, config));
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
const onFieldBlur = () => {
|
|
838
|
+
if (!field) return;
|
|
839
|
+
if (config.source && state === "following") return fromSource();
|
|
840
|
+
render(field.value);
|
|
841
|
+
};
|
|
842
|
+
const onCompositionStart = () => composing = true;
|
|
843
|
+
const onCompositionEnd = () => {
|
|
844
|
+
composing = false;
|
|
845
|
+
onFieldInput();
|
|
846
|
+
};
|
|
847
|
+
const attachSource = () => {
|
|
848
|
+
detachSource();
|
|
849
|
+
const source = config.source;
|
|
850
|
+
if (!source) return detachSource = noop;
|
|
851
|
+
source.addEventListener("input", onSourceInput);
|
|
852
|
+
source.addEventListener(CHANGE_EVENT, onSourceInput);
|
|
853
|
+
detachSource = () => {
|
|
854
|
+
source.removeEventListener("input", onSourceInput);
|
|
855
|
+
source.removeEventListener(CHANGE_EVENT, onSourceInput);
|
|
856
|
+
};
|
|
857
|
+
};
|
|
858
|
+
const start = () => {
|
|
859
|
+
const current = config;
|
|
860
|
+
attachSource();
|
|
861
|
+
if (field && current.source) {
|
|
862
|
+
const expected = convert(textOf(current.source), current.to, current.options);
|
|
863
|
+
setState(field.value === "" || field.value === expected ? "following" : "edited");
|
|
864
|
+
if (state === "following") fromSource();
|
|
865
|
+
else render(field.value);
|
|
866
|
+
} else {
|
|
867
|
+
setState("ready");
|
|
868
|
+
if (current.source) fromSource();
|
|
869
|
+
else render(field ? field.value : originalText);
|
|
870
|
+
}
|
|
871
|
+
};
|
|
872
|
+
if (field) {
|
|
873
|
+
field.addEventListener("input", onFieldInput);
|
|
874
|
+
field.addEventListener("blur", onFieldBlur);
|
|
875
|
+
field.addEventListener("compositionstart", onCompositionStart);
|
|
876
|
+
field.addEventListener("compositionend", onCompositionEnd);
|
|
877
|
+
}
|
|
878
|
+
start();
|
|
879
|
+
let active = true;
|
|
880
|
+
return {
|
|
881
|
+
refresh: () => {
|
|
882
|
+
if (!active) return;
|
|
883
|
+
const next = readConfig(element, options);
|
|
884
|
+
if (!next) return;
|
|
885
|
+
config = next;
|
|
886
|
+
if (field && config.source && state !== "ready") {
|
|
887
|
+
attachSource();
|
|
888
|
+
return state === "following" ? fromSource() : render(field.value);
|
|
889
|
+
}
|
|
890
|
+
start();
|
|
891
|
+
},
|
|
892
|
+
destroy: () => {
|
|
893
|
+
if (!active) return;
|
|
894
|
+
active = false;
|
|
895
|
+
detachSource();
|
|
896
|
+
if (field) {
|
|
897
|
+
field.removeEventListener("input", onFieldInput);
|
|
898
|
+
field.removeEventListener("blur", onFieldBlur);
|
|
899
|
+
field.removeEventListener("compositionstart", onCompositionStart);
|
|
900
|
+
field.removeEventListener("compositionend", onCompositionEnd);
|
|
901
|
+
} else {
|
|
902
|
+
element.replaceChildren(...originalNodes);
|
|
903
|
+
}
|
|
904
|
+
authored.forEach((before, name) => before === null && element.removeAttribute(name));
|
|
905
|
+
element.style.removeProperty(COLOR_PROPERTY);
|
|
906
|
+
if (element.getAttribute("style") === "") element.removeAttribute("style");
|
|
907
|
+
element.removeAttribute(STATE_ATTRIBUTE);
|
|
908
|
+
}
|
|
909
|
+
};
|
|
910
|
+
};
|
|
911
|
+
var string = (target = `[${ATTRIBUTE}]`, options = {}) => {
|
|
912
|
+
if (!isBrowser()) return { refresh: noop, destroy: noop };
|
|
913
|
+
const controllers = resolveElements(target).filter((element) => element instanceof HTMLElement).map((element) => bind(element, options)).filter((controller) => controller !== null);
|
|
914
|
+
return {
|
|
915
|
+
refresh: () => controllers.forEach((controller) => controller.refresh()),
|
|
916
|
+
destroy: () => controllers.forEach((controller) => controller.destroy())
|
|
917
|
+
};
|
|
918
|
+
};
|
|
919
|
+
var autoString = ({
|
|
920
|
+
selector = `[${ATTRIBUTE}]`,
|
|
921
|
+
scope,
|
|
922
|
+
observe = true,
|
|
923
|
+
...options
|
|
924
|
+
} = {}) => autoInit({
|
|
925
|
+
selector,
|
|
926
|
+
observe,
|
|
927
|
+
...scope && { root: scope },
|
|
928
|
+
init: (element) => {
|
|
929
|
+
const controller = string(element, options);
|
|
930
|
+
return controller.destroy;
|
|
931
|
+
}
|
|
932
|
+
});
|
|
933
|
+
|
|
934
|
+
// src/vanilla/search.ts
|
|
935
|
+
var SEARCH_ATTRIBUTE = "data-string-search";
|
|
936
|
+
var SEARCH_STATE_ATTRIBUTE = "data-string-search-state";
|
|
937
|
+
var SEARCH_EMPTY_ATTRIBUTE = "data-string-search-empty";
|
|
938
|
+
var SEARCH_EVENT = "string:search";
|
|
939
|
+
var textNodes = (root) => {
|
|
940
|
+
const walker = root.ownerDocument.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
941
|
+
const nodes = [];
|
|
942
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) nodes.push(node);
|
|
943
|
+
return nodes;
|
|
944
|
+
};
|
|
945
|
+
var bindSearch = (list, options) => {
|
|
946
|
+
const attr = (name) => `${SEARCH_ATTRIBUTE}-${name}`;
|
|
947
|
+
const selector = readString(list, SEARCH_ATTRIBUTE, "");
|
|
948
|
+
const input = resolveElements(selector || options.input)[0];
|
|
949
|
+
if (!(input instanceof HTMLInputElement || input instanceof HTMLTextAreaElement)) return null;
|
|
950
|
+
const doc = list.ownerDocument;
|
|
951
|
+
const authoredHidden = /* @__PURE__ */ new Map();
|
|
952
|
+
let marks = [];
|
|
953
|
+
let items = [];
|
|
954
|
+
let empty = null;
|
|
955
|
+
const settings = () => {
|
|
956
|
+
const locale = readString(list, attr("locale"), "") || options.locale || list.closest("[lang]")?.getAttribute("lang") || "";
|
|
957
|
+
const match = {
|
|
958
|
+
split: readBoolean(list, attr("split"), options.split ?? true),
|
|
959
|
+
...locale && { locale }
|
|
960
|
+
};
|
|
961
|
+
return {
|
|
962
|
+
match,
|
|
963
|
+
mark: readBoolean(list, attr("highlight"), options.highlight ?? true),
|
|
964
|
+
min: Math.max(1, readNumber(list, attr("min"), options.min ?? 1)),
|
|
965
|
+
items: readString(list, attr("items"), options.items ?? "")
|
|
966
|
+
};
|
|
967
|
+
};
|
|
968
|
+
const remember = (element) => {
|
|
969
|
+
if (!authoredHidden.has(element)) authoredHidden.set(element, element.hidden);
|
|
970
|
+
};
|
|
971
|
+
const collect = () => {
|
|
972
|
+
const { items: itemSelector } = settings();
|
|
973
|
+
const found = itemSelector ? Array.from(list.querySelectorAll(itemSelector)) : Array.from(list.children);
|
|
974
|
+
const emptyOption = resolveElements(options.empty)[0];
|
|
975
|
+
empty = list.querySelector(`[${SEARCH_EMPTY_ATTRIBUTE}]`) ?? (emptyOption instanceof HTMLElement ? emptyOption : null);
|
|
976
|
+
items = found.filter(
|
|
977
|
+
(item) => item instanceof HTMLElement && item !== empty && !item.contains(empty)
|
|
978
|
+
);
|
|
979
|
+
items.forEach(remember);
|
|
980
|
+
if (empty) remember(empty);
|
|
981
|
+
items = items.filter((item) => !authoredHidden.get(item));
|
|
982
|
+
};
|
|
983
|
+
const clearMarks = () => {
|
|
984
|
+
for (const { original, nodes } of marks) {
|
|
985
|
+
const first = nodes[0];
|
|
986
|
+
if (first?.parentNode) first.parentNode.insertBefore(original, first);
|
|
987
|
+
nodes.forEach((node) => node.parentNode?.removeChild(node));
|
|
988
|
+
}
|
|
989
|
+
marks = [];
|
|
990
|
+
};
|
|
991
|
+
const markItem = (item, query, match) => {
|
|
992
|
+
for (const node of textNodes(item)) {
|
|
993
|
+
const segments = highlight(node.data, query, match);
|
|
994
|
+
if (!segments.some((segment) => segment.match)) continue;
|
|
995
|
+
const nodes = segments.map((segment) => {
|
|
996
|
+
if (!segment.match) return doc.createTextNode(segment.text);
|
|
997
|
+
const mark = doc.createElement("mark");
|
|
998
|
+
mark.textContent = segment.text;
|
|
999
|
+
return mark;
|
|
1000
|
+
});
|
|
1001
|
+
const parent = node.parentNode;
|
|
1002
|
+
nodes.forEach((created) => parent.insertBefore(created, node));
|
|
1003
|
+
parent.removeChild(node);
|
|
1004
|
+
marks.push({ original: node, nodes });
|
|
1005
|
+
}
|
|
1006
|
+
};
|
|
1007
|
+
const search2 = () => {
|
|
1008
|
+
const { match, mark, min } = settings();
|
|
1009
|
+
const query = input.value;
|
|
1010
|
+
clearMarks();
|
|
1011
|
+
const active = Array.from(query.trim()).length >= min;
|
|
1012
|
+
const matches = items.filter((item) => {
|
|
1013
|
+
const visible = !active || contains(item.textContent ?? "", query, match);
|
|
1014
|
+
item.hidden = visible ? authoredHidden.get(item) : true;
|
|
1015
|
+
if (visible && active && mark) markItem(item, query, match);
|
|
1016
|
+
return visible && active;
|
|
1017
|
+
});
|
|
1018
|
+
const state = !active ? "idle" : matches.length > 0 ? "results" : "empty";
|
|
1019
|
+
list.setAttribute(SEARCH_STATE_ATTRIBUTE, state);
|
|
1020
|
+
if (empty) empty.hidden = state !== "empty";
|
|
1021
|
+
const result = {
|
|
1022
|
+
query,
|
|
1023
|
+
matches: active ? matches : [...items],
|
|
1024
|
+
total: items.length,
|
|
1025
|
+
state
|
|
1026
|
+
};
|
|
1027
|
+
options.onSearch?.(result, list);
|
|
1028
|
+
list.dispatchEvent(new CustomEvent(SEARCH_EVENT, { bubbles: true, detail: result }));
|
|
1029
|
+
};
|
|
1030
|
+
input.addEventListener("input", search2);
|
|
1031
|
+
collect();
|
|
1032
|
+
search2();
|
|
1033
|
+
let alive = true;
|
|
1034
|
+
return {
|
|
1035
|
+
refresh: () => {
|
|
1036
|
+
if (!alive) return;
|
|
1037
|
+
clearMarks();
|
|
1038
|
+
collect();
|
|
1039
|
+
search2();
|
|
1040
|
+
},
|
|
1041
|
+
destroy: () => {
|
|
1042
|
+
if (!alive) return;
|
|
1043
|
+
alive = false;
|
|
1044
|
+
input.removeEventListener("input", search2);
|
|
1045
|
+
clearMarks();
|
|
1046
|
+
authoredHidden.forEach((hidden, element) => element.hidden = hidden);
|
|
1047
|
+
list.removeAttribute(SEARCH_STATE_ATTRIBUTE);
|
|
1048
|
+
}
|
|
1049
|
+
};
|
|
1050
|
+
};
|
|
1051
|
+
var search = (target = `[${SEARCH_ATTRIBUTE}]`, options = {}) => {
|
|
1052
|
+
if (!isBrowser()) return { refresh: noop, destroy: noop };
|
|
1053
|
+
const controllers = resolveElements(target).filter((element) => element instanceof HTMLElement).map((element) => bindSearch(element, options)).filter((controller) => controller !== null);
|
|
1054
|
+
return {
|
|
1055
|
+
refresh: () => controllers.forEach((controller) => controller.refresh()),
|
|
1056
|
+
destroy: () => controllers.forEach((controller) => controller.destroy())
|
|
1057
|
+
};
|
|
1058
|
+
};
|
|
1059
|
+
var autoSearch = ({
|
|
1060
|
+
selector = `[${SEARCH_ATTRIBUTE}]`,
|
|
1061
|
+
scope,
|
|
1062
|
+
observe = true,
|
|
1063
|
+
...options
|
|
1064
|
+
} = {}) => autoInit({
|
|
1065
|
+
selector,
|
|
1066
|
+
observe,
|
|
1067
|
+
...scope && { root: scope },
|
|
1068
|
+
init: (element) => search(element, options).destroy
|
|
1069
|
+
});
|
|
1070
|
+
|
|
1071
|
+
// src/vanilla/controller.ts
|
|
1072
|
+
var bindAll = (selector, bind2) => (target = selector, options = {}) => {
|
|
1073
|
+
if (!isBrowser()) return { refresh: noop, destroy: noop };
|
|
1074
|
+
const controllers = resolveElements(target).filter((element) => element instanceof HTMLElement).map((element) => bind2(element, options)).filter((controller) => controller !== null);
|
|
1075
|
+
return {
|
|
1076
|
+
refresh: () => controllers.forEach((controller) => controller.refresh()),
|
|
1077
|
+
destroy: () => controllers.forEach((controller) => controller.destroy())
|
|
1078
|
+
};
|
|
1079
|
+
};
|
|
1080
|
+
var autoAll = (selector, run) => ({
|
|
1081
|
+
selector: chosen = selector,
|
|
1082
|
+
scope,
|
|
1083
|
+
observe = true,
|
|
1084
|
+
...options
|
|
1085
|
+
} = {}) => autoInit({
|
|
1086
|
+
selector: chosen,
|
|
1087
|
+
observe,
|
|
1088
|
+
...scope && { root: scope },
|
|
1089
|
+
init: (element) => run(element, options).destroy
|
|
1090
|
+
});
|
|
1091
|
+
var snapshot = (element) => {
|
|
1092
|
+
const nodes = Array.from(element.childNodes);
|
|
1093
|
+
return () => element.replaceChildren(...nodes);
|
|
1094
|
+
};
|
|
1095
|
+
var attributes = (element) => {
|
|
1096
|
+
const authored = /* @__PURE__ */ new Map();
|
|
1097
|
+
return {
|
|
1098
|
+
set: (name, value) => {
|
|
1099
|
+
if (!authored.has(name)) authored.set(name, element.getAttribute(name));
|
|
1100
|
+
if (authored.get(name) !== null) return;
|
|
1101
|
+
if (value === null) element.removeAttribute(name);
|
|
1102
|
+
else element.setAttribute(name, value);
|
|
1103
|
+
},
|
|
1104
|
+
restore: () => authored.forEach((before, name) => before === null && element.removeAttribute(name))
|
|
1105
|
+
};
|
|
1106
|
+
};
|
|
1107
|
+
var localeOf = (element, attribute, option) => element.getAttribute(attribute)?.trim() || option || element.closest("[lang]")?.getAttribute("lang") || void 0;
|
|
1108
|
+
var textOf2 = (element) => element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement ? element.value : element.textContent ?? "";
|
|
1109
|
+
|
|
1110
|
+
// src/vanilla/count.ts
|
|
1111
|
+
var COUNT_ATTRIBUTE = "data-string-count";
|
|
1112
|
+
var COUNT_STATE_ATTRIBUTE = "data-string-count-state";
|
|
1113
|
+
var COUNT_EVENT = "string:count";
|
|
1114
|
+
var UNITS = ["characters", "words"];
|
|
1115
|
+
var bindCount = (output, options) => {
|
|
1116
|
+
const attr = (name) => `${COUNT_ATTRIBUTE}-${name}`;
|
|
1117
|
+
const field = resolveElements(readString(output, COUNT_ATTRIBUTE, "") || options.field)[0];
|
|
1118
|
+
if (!(field instanceof HTMLInputElement || field instanceof HTMLTextAreaElement)) return null;
|
|
1119
|
+
const restoreChildren = snapshot(output);
|
|
1120
|
+
const update = () => {
|
|
1121
|
+
const unit = readEnum(output, attr("unit"), UNITS, options.unit ?? "characters");
|
|
1122
|
+
const locale = localeOf(output, attr("locale"), options.locale);
|
|
1123
|
+
const count2 = unit === "words" ? wordCount(field.value, locale ? { locale } : {}) : graphemes(field.value).length;
|
|
1124
|
+
const fallbackMax = options.max ?? (field.maxLength > 0 ? field.maxLength : NaN);
|
|
1125
|
+
const rawMax = readNumber(output, attr("max"), fallbackMax);
|
|
1126
|
+
const max = Number.isFinite(rawMax) && rawMax >= 0 ? rawMax : void 0;
|
|
1127
|
+
const remaining = max === void 0 ? void 0 : max - count2;
|
|
1128
|
+
const near = readNumber(output, attr("near"), options.near ?? Math.ceil((max ?? 0) * 0.1));
|
|
1129
|
+
const state = remaining === void 0 ? "ok" : remaining < 0 ? "over" : remaining <= near ? "near" : "ok";
|
|
1130
|
+
const info = { count: count2, max, remaining, state };
|
|
1131
|
+
const format = output.hasAttribute(attr("format")) ? output.getAttribute(attr("format")) ?? "" : options.format ?? (max === void 0 ? "{count}" : "{remaining}");
|
|
1132
|
+
output.textContent = typeof format === "function" ? format(info) : template(format, { count: count2, max, remaining }, { missing: "" });
|
|
1133
|
+
output.setAttribute(COUNT_STATE_ATTRIBUTE, state);
|
|
1134
|
+
options.onCount?.(info, output);
|
|
1135
|
+
output.dispatchEvent(new CustomEvent(COUNT_EVENT, { bubbles: true, detail: info }));
|
|
1136
|
+
};
|
|
1137
|
+
field.addEventListener("input", update);
|
|
1138
|
+
update();
|
|
1139
|
+
let alive = true;
|
|
1140
|
+
return {
|
|
1141
|
+
refresh: () => alive && update(),
|
|
1142
|
+
destroy: () => {
|
|
1143
|
+
if (!alive) return;
|
|
1144
|
+
alive = false;
|
|
1145
|
+
field.removeEventListener("input", update);
|
|
1146
|
+
restoreChildren();
|
|
1147
|
+
output.removeAttribute(COUNT_STATE_ATTRIBUTE);
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
};
|
|
1151
|
+
var count = bindAll(`[${COUNT_ATTRIBUTE}]`, bindCount);
|
|
1152
|
+
var autoCount = autoAll(`[${COUNT_ATTRIBUTE}]`, count);
|
|
1153
|
+
|
|
1154
|
+
// src/vanilla/fit.ts
|
|
1155
|
+
var FIT_ATTRIBUTE = "data-string-fit";
|
|
1156
|
+
var FIT_STATE_ATTRIBUTE = "data-string-fit-state";
|
|
1157
|
+
var POSITIONS2 = ["end", "middle", "start"];
|
|
1158
|
+
var bindFit = (element, options) => {
|
|
1159
|
+
const restoreChildren = snapshot(element);
|
|
1160
|
+
const managed = attributes(element);
|
|
1161
|
+
const text = element.textContent ?? "";
|
|
1162
|
+
const styleBefore = element.getAttribute("style");
|
|
1163
|
+
let alive = true;
|
|
1164
|
+
element.style.whiteSpace = "nowrap";
|
|
1165
|
+
element.style.overflow = "hidden";
|
|
1166
|
+
const fits = () => element.scrollWidth <= element.clientWidth;
|
|
1167
|
+
const fit2 = () => {
|
|
1168
|
+
const position = readEnum(element, FIT_ATTRIBUTE, POSITIONS2, options.position ?? "middle");
|
|
1169
|
+
const ellipsis = element.hasAttribute(`${FIT_ATTRIBUTE}-ellipsis`) ? element.getAttribute(`${FIT_ATTRIBUTE}-ellipsis`) ?? "" : options.ellipsis ?? "\u2026";
|
|
1170
|
+
if (element.textContent !== text) element.textContent = text;
|
|
1171
|
+
if (element.clientWidth === 0 || fits()) {
|
|
1172
|
+
element.setAttribute(FIT_STATE_ATTRIBUTE, "full");
|
|
1173
|
+
managed.set("title", null);
|
|
1174
|
+
return;
|
|
1175
|
+
}
|
|
1176
|
+
let low = 0;
|
|
1177
|
+
let high = graphemes(text).length - 1;
|
|
1178
|
+
let best = truncate(text, 0, { position, ellipsis, wordBoundary: false });
|
|
1179
|
+
while (low <= high) {
|
|
1180
|
+
const middle = Math.floor((low + high) / 2);
|
|
1181
|
+
const candidate = truncate(text, middle, { position, ellipsis, wordBoundary: false });
|
|
1182
|
+
element.textContent = candidate;
|
|
1183
|
+
if (fits()) {
|
|
1184
|
+
best = candidate;
|
|
1185
|
+
low = middle + 1;
|
|
1186
|
+
} else high = middle - 1;
|
|
1187
|
+
}
|
|
1188
|
+
element.textContent = best;
|
|
1189
|
+
element.setAttribute(FIT_STATE_ATTRIBUTE, "truncated");
|
|
1190
|
+
managed.set("title", text);
|
|
1191
|
+
};
|
|
1192
|
+
let frame = 0;
|
|
1193
|
+
const schedule = () => {
|
|
1194
|
+
cancelAnimationFrame(frame);
|
|
1195
|
+
frame = requestAnimationFrame(fit2);
|
|
1196
|
+
};
|
|
1197
|
+
let observer = null;
|
|
1198
|
+
if (typeof ResizeObserver === "function") {
|
|
1199
|
+
observer = new ResizeObserver(schedule);
|
|
1200
|
+
observer.observe(element);
|
|
1201
|
+
} else {
|
|
1202
|
+
window.addEventListener("resize", schedule);
|
|
1203
|
+
}
|
|
1204
|
+
void element.ownerDocument.fonts?.ready.then(() => alive && fit2());
|
|
1205
|
+
fit2();
|
|
1206
|
+
return {
|
|
1207
|
+
refresh: () => alive && fit2(),
|
|
1208
|
+
destroy: () => {
|
|
1209
|
+
if (!alive) return;
|
|
1210
|
+
alive = false;
|
|
1211
|
+
cancelAnimationFrame(frame);
|
|
1212
|
+
observer?.disconnect();
|
|
1213
|
+
window.removeEventListener("resize", schedule);
|
|
1214
|
+
restoreChildren();
|
|
1215
|
+
managed.restore();
|
|
1216
|
+
if (styleBefore === null) element.removeAttribute("style");
|
|
1217
|
+
else element.setAttribute("style", styleBefore);
|
|
1218
|
+
element.removeAttribute(FIT_STATE_ATTRIBUTE);
|
|
1219
|
+
}
|
|
1220
|
+
};
|
|
1221
|
+
};
|
|
1222
|
+
var fit = bindAll(`[${FIT_ATTRIBUTE}]`, bindFit);
|
|
1223
|
+
var autoFit = autoAll(`[${FIT_ATTRIBUTE}]`, fit);
|
|
1224
|
+
|
|
1225
|
+
// src/vanilla/copy.ts
|
|
1226
|
+
var COPY_ATTRIBUTE = "data-string-copy";
|
|
1227
|
+
var COPY_STATE_ATTRIBUTE = "data-string-copy-state";
|
|
1228
|
+
var COPY_EVENT = "string:copy";
|
|
1229
|
+
var bindCopy = (button, options) => {
|
|
1230
|
+
const {
|
|
1231
|
+
source: sourceOption,
|
|
1232
|
+
as: asOption,
|
|
1233
|
+
reset: resetOption,
|
|
1234
|
+
onCopy,
|
|
1235
|
+
...convertOptions
|
|
1236
|
+
} = options;
|
|
1237
|
+
let timer;
|
|
1238
|
+
const finish = (detail) => {
|
|
1239
|
+
clearTimeout(timer);
|
|
1240
|
+
button.setAttribute(COPY_STATE_ATTRIBUTE, detail.ok ? "copied" : "error");
|
|
1241
|
+
const reset = readNumber(button, `${COPY_ATTRIBUTE}-reset`, resetOption ?? 2e3);
|
|
1242
|
+
timer = setTimeout(() => button.removeAttribute(COPY_STATE_ATTRIBUTE), reset);
|
|
1243
|
+
onCopy?.(detail, button);
|
|
1244
|
+
button.dispatchEvent(new CustomEvent(COPY_EVENT, { bubbles: true, detail }));
|
|
1245
|
+
};
|
|
1246
|
+
const onClick = async () => {
|
|
1247
|
+
const source = resolveElements(readString(button, COPY_ATTRIBUTE, "") || sourceOption)[0];
|
|
1248
|
+
const raw = source ? textOf2(source) : "";
|
|
1249
|
+
const name = readString(button, `${COPY_ATTRIBUTE}-as`, "");
|
|
1250
|
+
const as = isConversion(name) ? name : asOption;
|
|
1251
|
+
const value = as ? convert(raw, as, convertOptions) : raw;
|
|
1252
|
+
try {
|
|
1253
|
+
if (!source || !navigator.clipboard) throw new Error("No source or clipboard");
|
|
1254
|
+
await navigator.clipboard.writeText(value);
|
|
1255
|
+
finish({ value, ok: true });
|
|
1256
|
+
} catch {
|
|
1257
|
+
finish({ value, ok: false });
|
|
1258
|
+
}
|
|
1259
|
+
};
|
|
1260
|
+
button.addEventListener("click", onClick);
|
|
1261
|
+
let alive = true;
|
|
1262
|
+
return {
|
|
1263
|
+
refresh: () => {
|
|
1264
|
+
},
|
|
1265
|
+
destroy: () => {
|
|
1266
|
+
if (!alive) return;
|
|
1267
|
+
alive = false;
|
|
1268
|
+
clearTimeout(timer);
|
|
1269
|
+
button.removeEventListener("click", onClick);
|
|
1270
|
+
button.removeAttribute(COPY_STATE_ATTRIBUTE);
|
|
1271
|
+
}
|
|
1272
|
+
};
|
|
102
1273
|
};
|
|
103
|
-
var
|
|
1274
|
+
var copy = bindAll(`[${COPY_ATTRIBUTE}]`, bindCopy);
|
|
1275
|
+
var autoCopy = autoAll(`[${COPY_ATTRIBUTE}]`, copy);
|
|
104
1276
|
|
|
1277
|
+
exports.ATTRIBUTE = ATTRIBUTE;
|
|
1278
|
+
exports.CHANGE_EVENT = CHANGE_EVENT;
|
|
1279
|
+
exports.CHAR_MAPS = CHAR_MAPS;
|
|
1280
|
+
exports.COLOR_PROPERTY = COLOR_PROPERTY;
|
|
1281
|
+
exports.CONVERSIONS = CONVERSIONS;
|
|
1282
|
+
exports.COPY_ATTRIBUTE = COPY_ATTRIBUTE;
|
|
1283
|
+
exports.COPY_EVENT = COPY_EVENT;
|
|
1284
|
+
exports.COPY_STATE_ATTRIBUTE = COPY_STATE_ATTRIBUTE;
|
|
1285
|
+
exports.COUNT_ATTRIBUTE = COUNT_ATTRIBUTE;
|
|
1286
|
+
exports.COUNT_EVENT = COUNT_EVENT;
|
|
1287
|
+
exports.COUNT_STATE_ATTRIBUTE = COUNT_STATE_ATTRIBUTE;
|
|
1288
|
+
exports.CYRILLIC = CYRILLIC;
|
|
1289
|
+
exports.FIT_ATTRIBUTE = FIT_ATTRIBUTE;
|
|
1290
|
+
exports.FIT_STATE_ATTRIBUTE = FIT_STATE_ATTRIBUTE;
|
|
1291
|
+
exports.GERMAN = GERMAN;
|
|
1292
|
+
exports.GREEK = GREEK;
|
|
1293
|
+
exports.SEARCH_ATTRIBUTE = SEARCH_ATTRIBUTE;
|
|
1294
|
+
exports.SEARCH_EMPTY_ATTRIBUTE = SEARCH_EMPTY_ATTRIBUTE;
|
|
1295
|
+
exports.SEARCH_EVENT = SEARCH_EVENT;
|
|
1296
|
+
exports.SEARCH_STATE_ATTRIBUTE = SEARCH_STATE_ATTRIBUTE;
|
|
1297
|
+
exports.STATE_ATTRIBUTE = STATE_ATTRIBUTE;
|
|
1298
|
+
exports.autoCopy = autoCopy;
|
|
1299
|
+
exports.autoCount = autoCount;
|
|
1300
|
+
exports.autoFit = autoFit;
|
|
1301
|
+
exports.autoSearch = autoSearch;
|
|
1302
|
+
exports.autoString = autoString;
|
|
105
1303
|
exports.camelCase = camelCase;
|
|
106
1304
|
exports.capitalize = capitalize;
|
|
1305
|
+
exports.closest = closest;
|
|
1306
|
+
exports.compareNatural = compareNatural;
|
|
107
1307
|
exports.constantCase = constantCase;
|
|
1308
|
+
exports.contains = contains;
|
|
1309
|
+
exports.convert = convert;
|
|
1310
|
+
exports.copy = copy;
|
|
1311
|
+
exports.count = count;
|
|
1312
|
+
exports.distance = distance;
|
|
108
1313
|
exports.dotCase = dotCase;
|
|
1314
|
+
exports.escapeHtml = escapeHtml;
|
|
1315
|
+
exports.fit = fit;
|
|
1316
|
+
exports.fold = fold;
|
|
1317
|
+
exports.graphemes = graphemes;
|
|
1318
|
+
exports.hashColor = hashColor;
|
|
1319
|
+
exports.highlight = highlight;
|
|
109
1320
|
exports.initials = initials;
|
|
1321
|
+
exports.isConversion = isConversion;
|
|
110
1322
|
exports.kebabCase = kebabCase;
|
|
111
1323
|
exports.pascalCase = pascalCase;
|
|
1324
|
+
exports.pathCase = pathCase;
|
|
1325
|
+
exports.pluralize = pluralize;
|
|
1326
|
+
exports.readingTime = readingTime;
|
|
112
1327
|
exports.removeAccents = removeAccents;
|
|
1328
|
+
exports.search = search;
|
|
113
1329
|
exports.sentenceCase = sentenceCase;
|
|
1330
|
+
exports.similarity = similarity;
|
|
114
1331
|
exports.slugify = slugify;
|
|
115
1332
|
exports.snakeCase = snakeCase;
|
|
1333
|
+
exports.string = string;
|
|
1334
|
+
exports.stripTags = stripTags;
|
|
1335
|
+
exports.template = template;
|
|
116
1336
|
exports.titleCase = titleCase;
|
|
1337
|
+
exports.trainCase = trainCase;
|
|
117
1338
|
exports.truncate = truncate;
|
|
118
1339
|
exports.uncapitalize = uncapitalize;
|
|
1340
|
+
exports.unescapeHtml = unescapeHtml;
|
|
1341
|
+
exports.uniqueSlug = uniqueSlug;
|
|
1342
|
+
exports.wordCount = wordCount;
|
|
119
1343
|
exports.words = words;
|
|
120
1344
|
//# sourceMappingURL=index.cjs.map
|
|
121
1345
|
//# sourceMappingURL=index.cjs.map
|