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