@hoci/core 0.2.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Chizuki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,597 @@
1
+ 'use strict';
2
+
3
+ const vue = require('vue');
4
+ const core = require('@vueuse/core');
5
+ const shared = require('@hoci/shared');
6
+ const tslx = require('tslx');
7
+
8
+ var __defProp$1 = Object.defineProperty;
9
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
10
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues$1 = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp$1.call(b, prop))
16
+ __defNormalProp$1(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols$1)
18
+ for (var prop of __getOwnPropSymbols$1(b)) {
19
+ if (__propIsEnum$1.call(b, prop))
20
+ __defNormalProp$1(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __async = (__this, __arguments, generator) => {
25
+ return new Promise((resolve, reject) => {
26
+ var fulfilled = (value) => {
27
+ try {
28
+ step(generator.next(value));
29
+ } catch (e) {
30
+ reject(e);
31
+ }
32
+ };
33
+ var rejected = (value) => {
34
+ try {
35
+ step(generator.throw(value));
36
+ } catch (e) {
37
+ reject(e);
38
+ }
39
+ };
40
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
41
+ step((generator = generator.apply(__this, __arguments)).next());
42
+ });
43
+ };
44
+ const affixProps = shared.defineHookProps(
45
+ {
46
+ fixedClass: {
47
+ type: String,
48
+ default: ""
49
+ },
50
+ /**
51
+ * @zh 距离窗口顶部达到指定偏移量后触发
52
+ * @en Triggered when the specified offset is reached from the top of the window
53
+ */
54
+ offset: {
55
+ type: Number,
56
+ default: 0
57
+ },
58
+ offsetType: {
59
+ type: String,
60
+ default: "top"
61
+ },
62
+ /**
63
+ * @zh 滚动容器,默认是 `window`
64
+ * @en Scroll container, default is `window`
65
+ */
66
+ target: {
67
+ type: [String, Object, Function]
68
+ },
69
+ /**
70
+ * @zh z-index 值
71
+ * @en Z index value
72
+ */
73
+ zIndex: {
74
+ type: Number,
75
+ default: 998
76
+ }
77
+ }
78
+ );
79
+ const affixEmits = shared.defineHookEmits(["scroll", "change"]);
80
+ const AFFIX_TARGET_KEY = Symbol("AFFIX_TARGET_KEY");
81
+ function getTargetRect(target) {
82
+ return shared.isWindow(target) ? {
83
+ top: 0,
84
+ bottom: window.innerHeight
85
+ } : target.getBoundingClientRect();
86
+ }
87
+ const useAffix = shared.defineHookComponent({
88
+ props: affixProps,
89
+ setup(props, { emit }) {
90
+ const wrapperRef = vue.ref(null);
91
+ const parentRef = vue.inject(AFFIX_TARGET_KEY, window);
92
+ const targetRef = shared.useElement(props.target, parentRef);
93
+ const isFixed = vue.ref(false);
94
+ const placeholderStyle = vue.ref({});
95
+ const fixedStyle = vue.ref({});
96
+ const classNames = vue.computed(() => {
97
+ return isFixed.value ? props.fixedClass : "";
98
+ });
99
+ const wrapperVisible = core.useElementVisibility(wrapperRef);
100
+ const container = vue.computed(() => {
101
+ var _a;
102
+ if (!targetRef.value || !wrapperVisible.value) {
103
+ return null;
104
+ }
105
+ return (_a = targetRef.value) != null ? _a : window;
106
+ });
107
+ const updatePosition = shared.throttleByRaf(() => __async(this, null, function* () {
108
+ if (!wrapperRef.value || !targetRef.value) {
109
+ return;
110
+ }
111
+ yield vue.nextTick();
112
+ const wrapperRect = wrapperRef.value.getBoundingClientRect();
113
+ const targetRect = getTargetRect(targetRef.value);
114
+ let newIsFixed = false;
115
+ let newFixedStyles = {};
116
+ const newPlaceholderStyles = {
117
+ width: `${wrapperRef.value.offsetWidth}px`,
118
+ height: `${wrapperRef.value.offsetHeight}px`
119
+ };
120
+ const offset = props.offset;
121
+ if (props.offsetType === "top") {
122
+ newIsFixed = wrapperRect.top - targetRect.top < offset && offset >= 0;
123
+ newFixedStyles = newIsFixed ? {
124
+ position: "fixed",
125
+ zIndex: props.zIndex,
126
+ top: `${targetRect.top + (offset || 0)}px`
127
+ } : {};
128
+ } else {
129
+ newIsFixed = targetRect.bottom - wrapperRect.bottom < (offset || 0);
130
+ newFixedStyles = newIsFixed ? {
131
+ position: "fixed",
132
+ bottom: `${window.innerHeight - targetRect.bottom + (offset || 0)}px`
133
+ } : {};
134
+ }
135
+ if (newIsFixed !== isFixed.value) {
136
+ isFixed.value = newIsFixed;
137
+ emit("change", newIsFixed);
138
+ }
139
+ placeholderStyle.value = newPlaceholderStyles;
140
+ fixedStyle.value = __spreadValues$1(__spreadValues$1({}, newFixedStyles), newIsFixed ? newPlaceholderStyles : {});
141
+ }));
142
+ core.useEventListener(container, "scroll", () => {
143
+ emit("scroll");
144
+ updatePosition();
145
+ });
146
+ core.useEventListener(container, "resize", updatePosition);
147
+ vue.watchPostEffect(updatePosition);
148
+ return {
149
+ classNames,
150
+ wrapperRef,
151
+ isFixed,
152
+ placeholderStyle,
153
+ fixedStyle,
154
+ updatePosition
155
+ };
156
+ }
157
+ });
158
+ function provideAffixTarget(target) {
159
+ vue.provide(AFFIX_TARGET_KEY, target);
160
+ }
161
+
162
+ const selectionProps = shared.defineHookProps({
163
+ modelValue: {
164
+ type: shared.valuePropType,
165
+ default: () => null
166
+ },
167
+ /**
168
+ * 选中时的 class
169
+ */
170
+ activeClass: {
171
+ type: shared.classPropType,
172
+ default: "active"
173
+ },
174
+ /**
175
+ * 每个选项的 class
176
+ */
177
+ itemClass: {
178
+ type: shared.classPropType,
179
+ default: ""
180
+ },
181
+ disabledClass: {
182
+ type: shared.classPropType,
183
+ default: "disabled"
184
+ },
185
+ unactiveClass: {
186
+ type: shared.classPropType,
187
+ default: ""
188
+ },
189
+ label: {
190
+ type: shared.labelPropType
191
+ },
192
+ /**
193
+ * 多选模式
194
+ */
195
+ multiple: {
196
+ type: [Boolean, Number],
197
+ default: () => false
198
+ },
199
+ /**
200
+ * 可清除
201
+ */
202
+ clearable: {
203
+ type: Boolean
204
+ },
205
+ defaultValue: {
206
+ type: shared.valuePropType,
207
+ default: () => null
208
+ },
209
+ activateEvent: {
210
+ type: String,
211
+ default: () => "click"
212
+ }
213
+ });
214
+ const selectionEmits = shared.defineHookEmits([
215
+ "update:modelValue",
216
+ "change",
217
+ "load",
218
+ "unload"
219
+ ]);
220
+ const HiSelectionContextSymbol = Symbol("[hi-selection]context");
221
+ function useSelectionContext() {
222
+ return vue.inject(HiSelectionContextSymbol, {
223
+ isActive: () => false,
224
+ changeActive: () => {
225
+ },
226
+ activeClass: "active",
227
+ unactiveClass: "unactive",
228
+ disabledClass: "disabled",
229
+ itemClass: "",
230
+ activateEvent: "click",
231
+ label: null,
232
+ multiple: false
233
+ });
234
+ }
235
+ const useSelectionList = shared.defineHookComponent({
236
+ props: selectionProps,
237
+ emits: selectionEmits,
238
+ setup(props, { emit }) {
239
+ var _a;
240
+ const options = vue.reactive([]);
241
+ function toArray(value) {
242
+ if (!core.isDefined(value)) {
243
+ return [];
244
+ }
245
+ if (props.multiple && Array.isArray(value)) {
246
+ return value.filter((v) => v != null || v !== void 0);
247
+ }
248
+ return [value];
249
+ }
250
+ const actives = vue.reactive([
251
+ ...toArray((_a = props.modelValue) != null ? _a : props.defaultValue)
252
+ ]);
253
+ const currentValue = vue.computed({
254
+ get() {
255
+ return props.multiple ? actives : actives[0];
256
+ },
257
+ set(val) {
258
+ actives.splice(0, actives.length, ...toArray(val));
259
+ }
260
+ });
261
+ const modelValue = vue.computed({
262
+ get() {
263
+ var _a2;
264
+ return (_a2 = props.modelValue) != null ? _a2 : props.defaultValue;
265
+ },
266
+ set(val) {
267
+ emit("update:modelValue", val);
268
+ }
269
+ });
270
+ core.syncRef(currentValue, modelValue, { immediate: true, deep: true });
271
+ const emitChange = () => emit("change", currentValue.value);
272
+ function isActive(value) {
273
+ return actives.includes(value);
274
+ }
275
+ function changeActive(value) {
276
+ if (isActive(value)) {
277
+ if (props.multiple || props.clearable) {
278
+ actives.splice(actives.indexOf(value), 1);
279
+ emitChange();
280
+ }
281
+ } else {
282
+ if (props.multiple) {
283
+ const limit = typeof props.multiple === "number" ? props.multiple : Number.POSITIVE_INFINITY;
284
+ if (actives.length < limit) {
285
+ actives.push(value);
286
+ emitChange();
287
+ }
288
+ } else {
289
+ actives.splice(0, actives.length, value);
290
+ emitChange();
291
+ }
292
+ }
293
+ }
294
+ const init = (option) => {
295
+ function remove() {
296
+ const index = options.findIndex((e) => e.id === option.id);
297
+ if (index > -1) {
298
+ options.splice(index, 1);
299
+ emit("unload", option);
300
+ }
301
+ }
302
+ for (let i = 0; i < options.length; i++) {
303
+ if (options[i].value === option.value) {
304
+ options.splice(i, 1);
305
+ i--;
306
+ }
307
+ }
308
+ options.push(option);
309
+ emit("load", option);
310
+ return remove;
311
+ };
312
+ vue.provide(HiSelectionContextSymbol, core.toReactive({
313
+ activeClass: vue.computed(() => tslx.cls(props.activeClass)),
314
+ unactiveClass: vue.computed(() => tslx.cls(props.unactiveClass)),
315
+ disabledClass: vue.computed(() => tslx.cls(props.disabledClass)),
316
+ itemClass: vue.computed(() => tslx.cls(props.itemClass)),
317
+ label: vue.computed(() => props.label),
318
+ multiple: vue.computed(() => props.multiple),
319
+ clearable: vue.computed(() => props.clearable),
320
+ defaultValue: vue.computed(() => props.defaultValue),
321
+ activateEvent: vue.computed(() => props.activateEvent),
322
+ active: currentValue,
323
+ changeActive,
324
+ isActive,
325
+ init
326
+ }));
327
+ function renderItem() {
328
+ const children = options.filter((e) => actives.includes(e.value)).map((e) => e.render());
329
+ return props.multiple ? children : children[0];
330
+ }
331
+ return {
332
+ options,
333
+ actives,
334
+ isActive,
335
+ changeActive,
336
+ renderItem
337
+ };
338
+ }
339
+ });
340
+
341
+ const itemProps = shared.defineHookProps({
342
+ value: {
343
+ type: shared.valuePropType,
344
+ default() {
345
+ return Math.random().toString(16).slice(2);
346
+ }
347
+ },
348
+ label: {
349
+ type: [Function, String]
350
+ },
351
+ keepAlive: {
352
+ type: Boolean,
353
+ default: () => true
354
+ },
355
+ key: {
356
+ type: [String, Number, Symbol]
357
+ },
358
+ activateEvent: {
359
+ type: String
360
+ },
361
+ disabled: {
362
+ type: Boolean,
363
+ default: false
364
+ }
365
+ });
366
+ const useSelectionItem = shared.defineHookComponent({
367
+ props: itemProps,
368
+ setup(props, { slots }) {
369
+ const context = useSelectionContext();
370
+ const activate = () => {
371
+ if (props.disabled) {
372
+ return;
373
+ }
374
+ context.changeActive(props.value);
375
+ };
376
+ function render() {
377
+ return vue.renderSlot(slots, "default", {
378
+ active: context.isActive(props.value),
379
+ activate
380
+ }, () => {
381
+ var _a;
382
+ let label = (_a = props.label) != null ? _a : context.label;
383
+ if (label && typeof label == "function") {
384
+ label = label(props.value);
385
+ }
386
+ return Array.isArray(label) ? label : [label];
387
+ });
388
+ }
389
+ let remove = () => {
390
+ };
391
+ const init = context.init;
392
+ if (init) {
393
+ vue.watch(
394
+ () => props.value,
395
+ (value) => {
396
+ remove();
397
+ remove = init({
398
+ id: Math.random().toString(16).slice(2),
399
+ label: typeof props.label == "string" ? props.label : void 0,
400
+ value,
401
+ render
402
+ });
403
+ },
404
+ { immediate: true }
405
+ );
406
+ core.tryOnScopeDispose(remove);
407
+ }
408
+ const isActive = vue.computed(() => context.isActive(props.value));
409
+ const isDisabled = vue.computed(() => props.disabled);
410
+ const className = vue.computed(() => {
411
+ const array = [context.itemClass];
412
+ if (!isDisabled.value) {
413
+ array.push(context.isActive(props.value) ? context.activeClass : context.unactiveClass);
414
+ } else {
415
+ array.push(context.disabledClass);
416
+ }
417
+ return tslx.cls(array);
418
+ });
419
+ const activateEvent = vue.computed(() => context.activateEvent);
420
+ return {
421
+ activate,
422
+ render,
423
+ isActive,
424
+ isDisabled,
425
+ className,
426
+ activateEvent
427
+ };
428
+ }
429
+ });
430
+
431
+ const switchProps = shared.defineHookProps({
432
+ modelValue: {
433
+ type: Boolean,
434
+ default: false
435
+ },
436
+ class: {
437
+ type: shared.classPropType,
438
+ required: true
439
+ },
440
+ activeClass: {
441
+ type: shared.classPropType,
442
+ default: "checked"
443
+ },
444
+ unactiveClass: {
445
+ type: shared.classPropType,
446
+ default: "unchecked"
447
+ },
448
+ activateEvent: {
449
+ type: String,
450
+ default: "click"
451
+ },
452
+ disabled: {
453
+ type: Boolean,
454
+ default: false
455
+ },
456
+ disabledClass: {
457
+ type: shared.classPropType,
458
+ default: ""
459
+ }
460
+ });
461
+ const switchEmits = shared.defineHookEmits(["update:modelValue", "change"]);
462
+ const useSwitch = shared.defineHookComponent({
463
+ props: switchProps,
464
+ emits: switchEmits,
465
+ setup(props, context) {
466
+ const modelValue = core.useVModel(props, "modelValue", context.emit, {
467
+ passive: true,
468
+ defaultValue: false
469
+ });
470
+ const toggle = function(value) {
471
+ if (props.disabled) {
472
+ return;
473
+ }
474
+ const oldValue = modelValue.value;
475
+ const newValue = typeof value === "boolean" ? value : !oldValue;
476
+ if (newValue !== oldValue) {
477
+ modelValue.value = newValue;
478
+ context.emit("change", newValue);
479
+ }
480
+ };
481
+ const isDisabled = vue.computed(() => props.disabled);
482
+ const className = vue.computed(() => {
483
+ return tslx.cls([
484
+ props.class,
485
+ modelValue.value ? props.activeClass : props.unactiveClass,
486
+ isDisabled.value ? props.disabledClass : ""
487
+ ]);
488
+ });
489
+ return {
490
+ toggle,
491
+ modelValue,
492
+ className,
493
+ isDisabled
494
+ };
495
+ }
496
+ });
497
+
498
+ var __defProp = Object.defineProperty;
499
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
500
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
501
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
502
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
503
+ var __spreadValues = (a, b) => {
504
+ for (var prop in b || (b = {}))
505
+ if (__hasOwnProp.call(b, prop))
506
+ __defNormalProp(a, prop, b[prop]);
507
+ if (__getOwnPropSymbols)
508
+ for (var prop of __getOwnPropSymbols(b)) {
509
+ if (__propIsEnum.call(b, prop))
510
+ __defNormalProp(a, prop, b[prop]);
511
+ }
512
+ return a;
513
+ };
514
+ const iconProps = shared.defineHookProps({
515
+ src: {
516
+ type: String,
517
+ required: true
518
+ },
519
+ size: {
520
+ type: [Number, String],
521
+ default: "1rem"
522
+ },
523
+ width: {
524
+ type: [Number, String]
525
+ },
526
+ height: {
527
+ type: [Number, String]
528
+ },
529
+ color: {
530
+ type: String
531
+ },
532
+ mask: {
533
+ type: [Boolean, String],
534
+ default: () => "auto"
535
+ }
536
+ });
537
+ const useIcon = shared.defineHookComponent({
538
+ props: iconProps,
539
+ setup(props, context) {
540
+ const style = vue.computed(() => {
541
+ var _a, _b, _c, _d, _e, _f;
542
+ const icon = props.src;
543
+ const propSize = (_a = props.size) != null ? _a : "16px";
544
+ const size = typeof propSize === "number" ? `${propSize}px` : propSize;
545
+ const propWidth = (_b = props.width) != null ? _b : size;
546
+ const width = typeof propWidth === "number" ? `${propWidth}px` : propWidth;
547
+ const propHeight = (_c = props.height) != null ? _c : size;
548
+ const height = typeof propHeight === "number" ? `${propHeight}px` : propHeight;
549
+ const color = (_d = props.color) != null ? _d : "currentColor";
550
+ const mask = props.mask === "auto" ? icon.endsWith(".svg") : props.mask;
551
+ if (!mask) {
552
+ return __spreadValues({
553
+ "--icon-url": `url('${icon}')`,
554
+ "background-image": "var(--icon-url)",
555
+ "background-size": "100% 100%",
556
+ "height": height,
557
+ "width": width,
558
+ "display": "inline-block"
559
+ }, (_e = context.attrs.style) != null ? _e : {});
560
+ }
561
+ return __spreadValues({
562
+ "--icon-url": `url('${icon}')`,
563
+ "mask": "var(--icon-url) no-repeat",
564
+ "mask-size": "100% 100%",
565
+ "-webkit-mask": "var(--icon-url) no-repeat",
566
+ "-webkit-mask-size": "100% 100%",
567
+ "background-color": color,
568
+ "display": "inline-block",
569
+ "width": width,
570
+ "height": height
571
+ }, (_f = context.attrs.style) != null ? _f : {});
572
+ });
573
+ return {
574
+ style
575
+ };
576
+ }
577
+ });
578
+
579
+ exports.AFFIX_TARGET_KEY = AFFIX_TARGET_KEY;
580
+ exports.affixEmits = affixEmits;
581
+ exports.affixProps = affixProps;
582
+ exports.iconProps = iconProps;
583
+ exports.itemProps = itemProps;
584
+ exports.provideAffixTarget = provideAffixTarget;
585
+ exports.selectionEmits = selectionEmits;
586
+ exports.selectionProps = selectionProps;
587
+ exports.switchEmits = switchEmits;
588
+ exports.switchProps = switchProps;
589
+ exports.useAffix = useAffix;
590
+ exports.useIcon = useIcon;
591
+ exports.useSelectionContext = useSelectionContext;
592
+ exports.useSelectionItem = useSelectionItem;
593
+ exports.useSelectionList = useSelectionList;
594
+ exports.useSwitch = useSwitch;
595
+ Object.keys(shared).forEach(function (k) {
596
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = shared[k];
597
+ });