@farris/cli 2.0.3 → 2.1.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.
Files changed (125) hide show
  1. package/lib/commands/build.js +6 -7
  2. package/lib/commands/build.js.map +1 -1
  3. package/lib/commands/generate/api.js +188 -0
  4. package/lib/commands/generate/api.js.map +1 -0
  5. package/lib/commands/generate/common.js +101 -0
  6. package/lib/commands/generate/common.js.map +1 -0
  7. package/lib/commands/generate/component.js +164 -0
  8. package/lib/commands/generate/component.js.map +1 -0
  9. package/lib/commands/generate/view.js +195 -0
  10. package/lib/commands/generate/view.js.map +1 -0
  11. package/lib/commands/workspace/add-app.js +174 -0
  12. package/lib/commands/workspace/add-app.js.map +1 -0
  13. package/lib/commands/workspace/create.js +380 -0
  14. package/lib/commands/workspace/create.js.map +1 -0
  15. package/lib/commands/workspace/inspect.js +31 -0
  16. package/lib/commands/workspace/inspect.js.map +1 -0
  17. package/lib/commands/workspace/verify.js +39 -0
  18. package/lib/commands/workspace/verify.js.map +1 -0
  19. package/lib/common/constant.js +42 -1
  20. package/lib/common/constant.js.map +1 -1
  21. package/lib/common/get-vite-config.js +66 -9
  22. package/lib/common/get-vite-config.js.map +1 -1
  23. package/lib/common/output.js +95 -0
  24. package/lib/common/output.js.map +1 -0
  25. package/lib/common/safety.js +172 -0
  26. package/lib/common/safety.js.map +1 -0
  27. package/lib/common/template-renderer.js +115 -0
  28. package/lib/common/template-renderer.js.map +1 -0
  29. package/lib/common/transaction.js +84 -0
  30. package/lib/common/transaction.js.map +1 -0
  31. package/lib/common/verifier.js +147 -0
  32. package/lib/common/verifier.js.map +1 -0
  33. package/lib/config/vite-common.js +1 -1
  34. package/lib/config/vite-common.js.map +1 -1
  35. package/lib/index.js +124 -30
  36. package/lib/index.js.map +1 -1
  37. package/package.json +13 -9
  38. package/templates/workspace/app/farris.config.mjs +13 -0
  39. package/templates/{lib → workspace/app}/index.html +12 -12
  40. package/templates/workspace/app/package.json +22 -0
  41. package/templates/workspace/app/src/App.tsx +11 -0
  42. package/templates/workspace/app/src/main.ts +11 -0
  43. package/templates/workspace/app/src/router/index.ts +16 -0
  44. package/templates/workspace/app/src/styles/index.css +0 -0
  45. package/templates/workspace/app/src/styles/index.scss +0 -0
  46. package/templates/workspace/app/src/views/home/composables/use-home.ts +7 -0
  47. package/templates/workspace/app/src/views/home/home.component.tsx +15 -0
  48. package/templates/workspace/app/src/views/home/index.ts +4 -0
  49. package/templates/workspace/app/tsconfig.json +21 -0
  50. package/templates/workspace/root/README.md +28 -0
  51. package/templates/workspace/root/package.json +13 -0
  52. package/templates/workspace/root/pnpm-workspace.yaml +12 -0
  53. package/templates/lib/.eslintrc.cjs +0 -15
  54. package/templates/lib/.prettierrc.json +0 -8
  55. package/templates/lib/components/button/index.ts +0 -7
  56. package/templates/lib/components/button/src/button.component.tsx +0 -84
  57. package/templates/lib/components/button/src/button.props.ts +0 -55
  58. package/templates/lib/components/button/src/button.scss +0 -179
  59. package/templates/lib/components/common/index.ts +0 -6
  60. package/templates/lib/components/common/src/common.scss +0 -4
  61. package/templates/lib/components/common/src/compositions/index.ts +0 -19
  62. package/templates/lib/components/common/src/compositions/types.ts +0 -6
  63. package/templates/lib/components/common/src/compositions/use-bem/index.ts +0 -46
  64. package/templates/lib/components/common/src/compositions/use-click-away/index.ts +0 -40
  65. package/templates/lib/components/common/src/compositions/use-context/use-children-contexts.ts +0 -40
  66. package/templates/lib/components/common/src/compositions/use-context/use-parent-context.ts +0 -24
  67. package/templates/lib/components/common/src/compositions/use-event-listener/index.ts +0 -45
  68. package/templates/lib/components/common/src/compositions/use-expose/index.ts +0 -9
  69. package/templates/lib/components/common/src/compositions/use-lay-render/index.ts +0 -17
  70. package/templates/lib/components/common/src/compositions/use-link/index.ts +0 -14
  71. package/templates/lib/components/common/src/compositions/use-lock-scroll/index.ts +0 -25
  72. package/templates/lib/components/common/src/compositions/use-long-press/index.ts +0 -141
  73. package/templates/lib/components/common/src/compositions/use-momentum/index.ts +0 -82
  74. package/templates/lib/components/common/src/compositions/use-mount-component/index.ts +0 -48
  75. package/templates/lib/components/common/src/compositions/use-rect/index.ts +0 -31
  76. package/templates/lib/components/common/src/compositions/use-refs/index.ts +0 -21
  77. package/templates/lib/components/common/src/compositions/use-resize-observer/index.ts +0 -85
  78. package/templates/lib/components/common/src/compositions/use-resize-observer/utils.ts +0 -37
  79. package/templates/lib/components/common/src/compositions/use-scroll-parent/index.ts +0 -42
  80. package/templates/lib/components/common/src/compositions/use-touch/index.ts +0 -82
  81. package/templates/lib/components/common/src/compositions/use-touch-move/index.ts +0 -120
  82. package/templates/lib/components/common/src/entity/base-property.ts +0 -129
  83. package/templates/lib/components/common/src/entity/entity-schema.ts +0 -274
  84. package/templates/lib/components/common/src/entity/input-base-property.ts +0 -285
  85. package/templates/lib/components/common/src/style/animation/index.scss +0 -150
  86. package/templates/lib/components/common/src/style/base.scss +0 -63
  87. package/templates/lib/components/common/src/style/fonts/farris-mobile-icon.ttf +0 -0
  88. package/templates/lib/components/common/src/style/icon.scss +0 -6
  89. package/templates/lib/components/common/src/style/index.scss +0 -4
  90. package/templates/lib/components/common/src/style/mixins/bem.scss +0 -204
  91. package/templates/lib/components/common/src/style/mixins/border-radius.scss +0 -13
  92. package/templates/lib/components/common/src/style/mixins/ellipsis.scss +0 -15
  93. package/templates/lib/components/common/src/style/mixins/hairline.scss +0 -113
  94. package/templates/lib/components/common/src/style/mixins/index.scss +0 -6
  95. package/templates/lib/components/common/src/style/mixins/margin.scss +0 -10
  96. package/templates/lib/components/common/src/style/mixins/safe-area.scss +0 -9
  97. package/templates/lib/components/common/src/style/variables.scss +0 -113
  98. package/templates/lib/components/common/src/utils/index.ts +0 -15
  99. package/templates/lib/components/common/src/utils/src/common.ts +0 -83
  100. package/templates/lib/components/common/src/utils/src/date.ts +0 -134
  101. package/templates/lib/components/common/src/utils/src/dom/event.ts +0 -37
  102. package/templates/lib/components/common/src/utils/src/hook.ts +0 -18
  103. package/templates/lib/components/common/src/utils/src/number.ts +0 -26
  104. package/templates/lib/components/common/src/utils/src/query-filter.ts +0 -40
  105. package/templates/lib/components/common/src/utils/src/resove-asset.ts +0 -33
  106. package/templates/lib/components/common/src/utils/src/string.ts +0 -18
  107. package/templates/lib/components/common/src/utils/src/throttle.ts +0 -41
  108. package/templates/lib/components/common/src/utils/src/transition.ts +0 -14
  109. package/templates/lib/components/common/src/utils/src/type.ts +0 -105
  110. package/templates/lib/components/common/src/utils/src/use-appearance.ts +0 -33
  111. package/templates/lib/components/common/src/utils/src/with-install.ts +0 -16
  112. package/templates/lib/components/common/src/utils/src/with-register-designer.ts +0 -16
  113. package/templates/lib/components/common/src/utils/src/with-register.ts +0 -16
  114. package/templates/lib/components/common/types.ts +0 -131
  115. package/templates/lib/components/index.scss +0 -2
  116. package/templates/lib/components/index.ts +0 -21
  117. package/templates/lib/farris.config.mjs +0 -56
  118. package/templates/lib/package.json +0 -32
  119. package/templates/lib/src/App.vue +0 -80
  120. package/templates/lib/src/components/TheButton.vue +0 -3
  121. package/templates/lib/src/main.ts +0 -10
  122. package/templates/lib/src/router/index.ts +0 -23
  123. package/templates/lib/src/views/AboutView.vue +0 -15
  124. package/templates/lib/src/views/HomeView.vue +0 -9
  125. package/templates/lib/tsconfig.json +0 -20
@@ -1,141 +0,0 @@
1
- /* eslint-disable no-use-before-define */
2
- import { ObjectDirective } from 'vue';
3
-
4
- const DEFAULT_DELAY = 600;
5
- const DEFAULT_THRESHOLD = 10;
6
-
7
- export interface OnLongPressModifiers {
8
- stop?: boolean;
9
- once?: boolean;
10
- prevent?: boolean;
11
- capture?: boolean;
12
- self?: boolean;
13
- interceptClick?: boolean;
14
- }
15
-
16
- export interface OnLongPressOptions {
17
-
18
- delay?: number;
19
-
20
- modifiers?: OnLongPressModifiers;
21
-
22
- distanceThreshold?: number | false;
23
- }
24
-
25
- interface Position {
26
- x: number;
27
- y: number;
28
- }
29
-
30
- function addEventListener(el: HTMLElement, event: string, listener: any, options: any): () => void {
31
- el.addEventListener(event, listener, options);
32
- return () => el.removeEventListener(event, listener, options);
33
- }
34
-
35
- export function onLongPress(
36
- target: HTMLElement,
37
- handler: (evt: TouchEvent) => void,
38
- options?: OnLongPressOptions,
39
- ) {
40
- let timeout: ReturnType<typeof setTimeout> | undefined;
41
- let posStart: Position | undefined;
42
-
43
- function clear() {
44
- if (timeout) {
45
- clearTimeout(timeout);
46
- timeout = undefined;
47
- }
48
- posStart = undefined;
49
- }
50
-
51
- function onStart(ev: TouchEvent) {
52
- if (options?.modifiers?.self && ev.target !== target) {
53
- return;
54
- }
55
-
56
- clear();
57
-
58
- options?.modifiers?.prevent && ev.preventDefault();
59
- options?.modifiers?.stop && ev.stopPropagation();
60
-
61
- posStart = {
62
- x: ev.touches[0].pageX,
63
- y: ev.touches[0].pageY,
64
- };
65
- timeout = setTimeout(
66
- () => {
67
- timeout = undefined;
68
- handler(ev);
69
- },
70
- options?.delay ?? DEFAULT_DELAY,
71
- );
72
- }
73
-
74
- function onMove(ev: TouchEvent) {
75
- if (options?.modifiers?.self && ev.target !== target) {
76
- return;
77
- }
78
-
79
- if (!posStart || options?.distanceThreshold === false) {
80
- return;
81
- }
82
-
83
- options?.modifiers?.prevent && ev.preventDefault();
84
- options?.modifiers?.stop && ev.stopPropagation();
85
-
86
- const dx = ev.touches[0].pageX - posStart.x;
87
- const dy = ev.touches[0].pageY - posStart.y;
88
- const distance = Math.sqrt(dx * dx + dy * dy);
89
- if (distance >= (options?.distanceThreshold ?? DEFAULT_THRESHOLD)) {
90
- clear();
91
- }
92
- }
93
-
94
- function onEnd() {
95
- if (options?.modifiers?.interceptClick && !timeout) {
96
- addAutoDisappearMask();
97
- }
98
- clear();
99
- }
100
-
101
- const listenerOptions = {
102
- capture: options?.modifiers?.capture,
103
- once: options?.modifiers?.once,
104
- };
105
- const cleanup = [
106
- addEventListener(target, 'touchstart', onStart, listenerOptions),
107
- addEventListener(target, 'touchmove', onMove, listenerOptions),
108
- addEventListener(target, 'touchend', onEnd, listenerOptions),
109
- addEventListener(target, 'touchcancel', clear, listenerOptions),
110
- ];
111
- const stop = () => cleanup.forEach(fn => fn());
112
- return stop;
113
- }
114
-
115
- type BindingValue = (evt: TouchEvent) => void;
116
-
117
- export const vOnLongPress: ObjectDirective<HTMLElement, BindingValue> = {
118
- mounted(el, binding) {
119
- const delayStr = binding.arg || '';
120
- let delay = parseInt(delayStr, 10) || DEFAULT_DELAY;
121
- if (delay <= 0) {
122
- delay = DEFAULT_DELAY;
123
- }
124
- onLongPress(el, binding.value, { delay, modifiers: binding.modifiers });
125
- }
126
- };
127
-
128
- function addAutoDisappearMask(duration?: number): void {
129
- console.log(duration);
130
- const mask = document.createElement('div');
131
- mask.style.position = 'fixed';
132
- mask.style.top = '0';
133
- mask.style.right = '0';
134
- mask.style.bottom = '0';
135
- mask.style.left = '0';
136
- mask.style.zIndex = '99999';
137
- document.body.appendChild(mask);
138
- setTimeout(() => {
139
- document.body.removeChild(mask);
140
- }, duration || 25);
141
- }
@@ -1,82 +0,0 @@
1
- type MonmentumOption = {
2
- maxOverflowY?: number,
3
- TimeThreshold?: number,
4
- YThreshold?: number
5
- }
6
-
7
- export const useMonmentum = (options: MonmentumOption) => {
8
- const { maxOverflowY = 0, TimeThreshold = 200, YThreshold = 88 } = options;
9
- let startTime = 0;
10
- let momentumStartY = 0;
11
- let deltaY = 0;
12
- const start = (y: number) => {
13
- startTime = new Date().getTime();
14
- momentumStartY = y;
15
- };
16
- const move = (y: number) => {
17
- deltaY = y;
18
- const now = new Date().getTime();
19
- const duration = now - startTime;
20
- if (duration > TimeThreshold) {
21
- momentumStartY = deltaY;
22
- startTime = now;
23
- }
24
- };
25
- const end = () => {
26
- const now = new Date().getTime();
27
- const duration = now - startTime;
28
- const absDeltaY = Math.abs(deltaY - momentumStartY);
29
- if (duration < TimeThreshold && absDeltaY > YThreshold) {
30
- return true;
31
- }
32
- return false;
33
- };
34
- const get = (current: number, minY: number, maxY: number) => {
35
- const durationMap = {
36
- noBounce: 2500,
37
- weekBounce: 800,
38
- strongBounce: 400
39
- };
40
- const bezierMap = {
41
- noBounce: 'cubic-bezier(.17, .89, .45, 1)',
42
- weekBounce: 'cubic-bezier(.25, .46, .45, .94)',
43
- strongBounce: 'cubic-bezier(.25, .46, .45, .94)'
44
- };
45
- let type = 'noBounce';
46
- // 惯性滑动加速度
47
- const deceleration = 0.003;
48
- // 回弹阻力
49
- const bounceRate = 10;
50
- // 强弱回弹的分割值
51
- const bounceThreshold = 300;
52
-
53
- let overflowY = 0;
54
- const now = new Date().getTime();
55
- const duration = now - startTime;
56
-
57
- const distance = current - momentumStartY;
58
- const speed = (2 * Math.abs(distance)) / duration;
59
- let destination = current + (speed / deceleration) * (distance < 0 ? -1 : 1);
60
- if (destination < minY) {
61
- overflowY = minY - destination;
62
- type = overflowY > bounceThreshold ? 'strongBounce' : 'weekBounce';
63
- destination = Math.max(minY - maxOverflowY, minY - overflowY / bounceRate);
64
- } else if (destination > maxY) {
65
- overflowY = destination - maxY;
66
- type = overflowY > bounceThreshold ? 'strongBounce' : 'weekBounce';
67
- destination = Math.min(maxY + maxOverflowY, maxY + overflowY / bounceRate);
68
- }
69
-
70
- return {
71
- destination,
72
- duration: durationMap[type],
73
- bezier: bezierMap[type]
74
- };
75
- };
76
- return {
77
- start,
78
- move,
79
- end,
80
- get
81
- };
82
- };
@@ -1,48 +0,0 @@
1
- import { useExpose } from "../use-expose";
2
- import { Component, createApp, nextTick, reactive } from "vue";
3
-
4
- export function usePopupState() {
5
- const state = reactive({
6
- show: false,
7
- });
8
-
9
- const toggle = (show: boolean) => {
10
- state.show = show;
11
- };
12
-
13
- const open = (props: Record<string, any>) => {
14
- Object.assign(state, props);
15
-
16
- nextTick(() => {
17
- toggle(true);
18
- });
19
- };
20
-
21
- const close = () => {
22
- toggle(false);
23
- };
24
-
25
- useExpose({ open, close, toggle });
26
-
27
- return {
28
- open,
29
- close,
30
- state,
31
- toggle,
32
- };
33
- }
34
-
35
- export function mountComponent(RootComponent: Component) {
36
- const app = createApp(RootComponent);
37
- const root = document.createElement('div');
38
-
39
- document.body.appendChild(root);
40
-
41
- return {
42
- instance: app.mount(root),
43
- unmount() {
44
- app.unmount();
45
- document.body.removeChild(root);
46
- },
47
- };
48
- }
@@ -1,31 +0,0 @@
1
- import { Ref, unref } from 'vue';
2
-
3
- const isWindow = (val: unknown): val is Window => val === window;
4
-
5
- const makeDOMRect = (width: number, height: number) =>
6
- ({
7
- top: 0,
8
- left: 0,
9
- right: width,
10
- bottom: height,
11
- width,
12
- height,
13
- }) as DOMRect;
14
-
15
- export const useRect = (
16
- elementOrRef: Element | Window | Ref<Element | Window | undefined>,
17
- ) => {
18
- const element = unref(elementOrRef);
19
-
20
- if (isWindow(element)) {
21
- const width = element.innerWidth;
22
- const height = element.innerHeight;
23
- return makeDOMRect(width, height);
24
- }
25
-
26
- if (element?.getBoundingClientRect) {
27
- return element.getBoundingClientRect();
28
- }
29
-
30
- return makeDOMRect(0, 0);
31
- };
@@ -1,21 +0,0 @@
1
- import { ref, Ref, onBeforeUpdate } from 'vue';
2
-
3
- export function useRefs<T = Element>() {
4
- const refs = ref([]) as Ref<T[]>;
5
- const cache: Array<(el: unknown) => void> = [];
6
-
7
- onBeforeUpdate(() => {
8
- refs.value = [];
9
- });
10
-
11
- const setRefs = (index: number) => {
12
- if (!cache[index]) {
13
- cache[index] = (el: unknown) => {
14
- refs.value[index] = el as T;
15
- };
16
- }
17
- return cache[index];
18
- };
19
-
20
- return [refs, setRefs] as const;
21
- }
@@ -1,85 +0,0 @@
1
- /* eslint-disable no-use-before-define */
2
- import { computed, watch } from 'vue';
3
- import { MaybeComputedElementRef, unrefElement, tryOnScopeDispose, defaultWindow } from './utils';
4
-
5
- export interface ResizeObserverSize {
6
- readonly inlineSize: number;
7
- readonly blockSize: number;
8
- }
9
-
10
- export interface ResizeObserverEntry {
11
- readonly target: Element;
12
- readonly contentRect: DOMRectReadOnly;
13
- readonly borderBoxSize?: ReadonlyArray<ResizeObserverSize>;
14
- readonly contentBoxSize?: ReadonlyArray<ResizeObserverSize>;
15
- readonly devicePixelContentBoxSize?: ReadonlyArray<ResizeObserverSize>;
16
- }
17
-
18
- export type ResizeObserverCallback = (entries: ReadonlyArray<ResizeObserverEntry>, observer: ResizeObserver) => void;
19
-
20
- export interface UseResizeObserverOptions {
21
-
22
- /** 监听的盒模型,默认为`content-box` */
23
- box?: ResizeObserverBoxOptions;
24
-
25
- /** 允许指定一个的`window` */
26
- window?: Window;
27
- }
28
-
29
- declare class ResizeObserver {
30
-
31
- constructor(callback: ResizeObserverCallback);
32
-
33
- disconnect(): void;
34
-
35
- observe(target: Element, options?: UseResizeObserverOptions): void;
36
-
37
- unobserve(target: Element): void;
38
- }
39
-
40
- /**
41
- * 监听元素的大小变化
42
- */
43
- export function useResizeObserver(
44
- target: MaybeComputedElementRef | MaybeComputedElementRef[],
45
- callback: ResizeObserverCallback,
46
- options: UseResizeObserverOptions = {},
47
- ) {
48
- const { window = defaultWindow, ...observerOptions } = options;
49
- let observer: ResizeObserver | undefined;
50
-
51
- const cleanup = () => {
52
- if (observer) {
53
- observer.disconnect();
54
- observer = undefined;
55
- }
56
- };
57
-
58
- const targets = computed(() =>
59
- Array.isArray(target)
60
- ? target.map(el => unrefElement(el))
61
- : [unrefElement(target)]);
62
-
63
- const stopWatch = watch(
64
- targets,
65
- (elements) => {
66
- cleanup();
67
- if (window && 'ResizeObserver' in window) {
68
- observer = new ResizeObserver(callback);
69
- elements.forEach((element) => {
70
- element && observer!.observe(element, observerOptions);
71
- });
72
- }
73
- },
74
- { immediate: true, flush: 'post' },
75
- );
76
-
77
- const stop = () => {
78
- cleanup();
79
- stopWatch();
80
- };
81
-
82
- tryOnScopeDispose(stop);
83
-
84
- return { stop };
85
- }
@@ -1,37 +0,0 @@
1
- /* eslint-disable no-use-before-define */
2
- import { ComponentPublicInstance, Ref, unref, getCurrentScope, onScopeDispose } from 'vue';
3
- import { inBrowser } from '../../utils';
4
-
5
- export const defaultWindow = inBrowser ? window : undefined;
6
-
7
- export type MaybeRef<T> = T | Ref<T>;
8
- export type MaybeRefOrGetter<T> = MaybeRef<T> | (() => T);
9
-
10
- export type VueInstance = ComponentPublicInstance;
11
- export type MaybeElementRef<T extends MaybeElement = MaybeElement> = MaybeRef<T>;
12
- export type MaybeComputedElementRef<T extends MaybeElement = MaybeElement> = MaybeRefOrGetter<T>;
13
- export type MaybeElement = HTMLElement | SVGElement | VueInstance | undefined | null;
14
-
15
- export type UnRefElementReturn<T extends MaybeElement = MaybeElement> =
16
- T extends VueInstance ? Exclude<MaybeElement, VueInstance> : T | undefined;
17
-
18
- export type AnyFn = (...args: any[]) => any;
19
-
20
- export function toValue<T>(r: MaybeRefOrGetter<T>): T {
21
- return typeof r === 'function'
22
- ? (r as AnyFn)()
23
- : unref(r);
24
- }
25
-
26
- export function unrefElement<T extends MaybeElement>(elRef: MaybeComputedElementRef<T>): UnRefElementReturn<T> {
27
- const plain = toValue(elRef);
28
- return (plain as VueInstance)?.$el ?? plain;
29
- }
30
-
31
- export function tryOnScopeDispose(fn: (() => void)) {
32
- if (getCurrentScope()) {
33
- onScopeDispose(fn);
34
- return true;
35
- }
36
- return false;
37
- }
@@ -1,42 +0,0 @@
1
- import { ref, Ref, onMounted } from 'vue';
2
- import { inBrowser } from '../../utils';
3
-
4
- type ScrollElement = HTMLElement | Window;
5
-
6
- const overflowScrollReg = /scroll|auto|overlay/i;
7
- const defaultRoot = inBrowser ? window : undefined;
8
-
9
- function isElement(node: Element) {
10
- const ELEMENT_NODE_TYPE = 1;
11
- return (
12
- node.tagName !== 'HTML' &&
13
- node.tagName !== 'BODY' &&
14
- node.nodeType === ELEMENT_NODE_TYPE
15
- );
16
- }
17
-
18
- export function getScrollParent(el: Element, root: ScrollElement | undefined = defaultRoot) {
19
- let node = el;
20
-
21
- while (node && node !== root && isElement(node)) {
22
- const { overflowY } = window.getComputedStyle(node);
23
- if (overflowScrollReg.test(overflowY)) {
24
- return node;
25
- }
26
- node = node.parentNode as Element;
27
- }
28
-
29
- return root;
30
- }
31
-
32
- export function useScrollParent(el: Ref<Element | undefined>, root: ScrollElement | undefined = defaultRoot) {
33
- const scrollParent = ref<Element | Window>();
34
-
35
- onMounted(() => {
36
- if (el.value) {
37
- scrollParent.value = getScrollParent(el.value, root);
38
- }
39
- });
40
-
41
- return scrollParent;
42
- }
@@ -1,82 +0,0 @@
1
- import { ref } from 'vue';
2
-
3
- export const TAP_OFFSET = 5;
4
-
5
- type Direction = '' | 'vertical' | 'horizontal';
6
-
7
- function getDirection(x: number, y: number) {
8
- if (x > y) {
9
- return 'horizontal';
10
- }
11
- if (y > x) {
12
- return 'vertical';
13
- }
14
- return '';
15
- }
16
-
17
- export function useTouch() {
18
- const startX = ref(0);
19
- const startY = ref(0);
20
- const deltaX = ref(0);
21
- const deltaY = ref(0);
22
- const offsetX = ref(0);
23
- const offsetY = ref(0);
24
- const direction = ref<Direction>('');
25
- const isTap = ref(true);
26
-
27
- const isVertical = () => direction.value === 'vertical';
28
- const isHorizontal = () => direction.value === 'horizontal';
29
-
30
- const reset = () => {
31
- deltaX.value = 0;
32
- deltaY.value = 0;
33
- offsetX.value = 0;
34
- offsetY.value = 0;
35
- direction.value = '';
36
- isTap.value = true;
37
- };
38
-
39
- const start = ((event: TouchEvent) => {
40
- reset();
41
- startX.value = event.touches[0].clientX;
42
- startY.value = event.touches[0].clientY;
43
- }) as EventListener;
44
-
45
- const move = ((event: TouchEvent) => {
46
- const touch = event.touches[0];
47
- // safari back will set clientX to negative number
48
- deltaX.value = (touch.clientX < 0 ? 0 : touch.clientX) - startX.value;
49
- deltaY.value = touch.clientY - startY.value;
50
- offsetX.value = Math.abs(deltaX.value);
51
- offsetY.value = Math.abs(deltaY.value);
52
-
53
- // lock direction when distance is greater than a certain value
54
- const LOCK_DIRECTION_DISTANCE = 10;
55
- if (
56
- !direction.value ||
57
- (offsetX.value < LOCK_DIRECTION_DISTANCE && offsetY.value < LOCK_DIRECTION_DISTANCE)
58
- ) {
59
- direction.value = getDirection(offsetX.value, offsetY.value);
60
- }
61
-
62
- if (isTap.value && (offsetX.value > TAP_OFFSET || offsetY.value > TAP_OFFSET)) {
63
- isTap.value = false;
64
- }
65
- }) as EventListener;
66
-
67
- return {
68
- move,
69
- start,
70
- reset,
71
- startX,
72
- startY,
73
- deltaX,
74
- deltaY,
75
- offsetX,
76
- offsetY,
77
- direction,
78
- isVertical,
79
- isHorizontal,
80
- isTap
81
- };
82
- }
@@ -1,120 +0,0 @@
1
- import { reactive, ref, Ref } from 'vue';
2
- import { throttle } from '../../utils';
3
- import { useEventListener } from '../use-event-listener';
4
-
5
- const MIN_DISTANCE = 10;
6
-
7
- type TouchDirection = 'horizontal' | 'vertical' | '';
8
-
9
- export type TouchOffset = {
10
- startX: number;
11
- startY: number;
12
- endX: number;
13
- endY: number;
14
- offsetX: number;
15
- offsetY: number;
16
- direction: TouchDirection;
17
- };
18
-
19
- type TouchListener = {
20
- onTouchStart?: (event: Event) => void;
21
- onTouchMove?: (event: Event) => void;
22
- onTouchEnd?: (event: Event) => void;
23
- onTouchCancel?: (event: Event) => void;
24
- };
25
-
26
- function getDirection(x: number, y: number): TouchDirection {
27
- if (x > y && x > MIN_DISTANCE) {
28
- return 'horizontal';
29
- }
30
-
31
- if (y > x && y > MIN_DISTANCE) {
32
- return 'vertical';
33
- }
34
- return '';
35
- }
36
-
37
- const defaultTouchOffset: () => TouchOffset = () => {
38
- return {
39
- startX: 0,
40
- startY: 0,
41
- endX: 0,
42
- endY: 0,
43
- offsetX: 0,
44
- offsetY: 0,
45
- direction: ''
46
- };
47
- };
48
-
49
- export enum DragStatus {
50
- NotStarted,
51
- Started,
52
- Dragging,
53
- End
54
- }
55
-
56
- export function useTouchMove(
57
- target: Ref<EventTarget | undefined>,
58
- listener?: TouchListener,
59
- wait = 0
60
- ) {
61
- let beginOffset = { offsetX: 0, offsetY: 0 };
62
- const offset: TouchOffset = reactive(defaultTouchOffset());
63
- const dragStatus = ref(DragStatus.NotStarted);
64
-
65
- const touchStart = (event: any) => {
66
- if (!event.touches || !event.touches[0]) {
67
- return;
68
- }
69
- offset.startX = event.touches[0].clientX;
70
- offset.startY = event.touches[0].clientY;
71
- listener?.onTouchStart && listener.onTouchStart(event);
72
- dragStatus.value = DragStatus.Started;
73
- };
74
-
75
- const touchMove = (event: any) => {
76
- if (!event.touches || !event.touches[0]) {
77
- return;
78
- }
79
- const touch = event.touches[0];
80
- offset.offsetX = touch.clientX - offset.startX + beginOffset.offsetX;
81
- offset.offsetY = touch.clientY - offset.startY + beginOffset.offsetY;
82
- offset.endX = event.touches[0].clientX;
83
- offset.endY = event.touches[0].clientY;
84
- offset.direction = getDirection(offset.offsetX, offset.offsetY);
85
- listener?.onTouchMove && listener.onTouchMove(event);
86
- dragStatus.value = DragStatus.Dragging;
87
- };
88
-
89
- const touchEnd = (event: Event) => {
90
- beginOffset = { offsetX: offset.offsetX, offsetY: offset.offsetY };
91
- listener?.onTouchEnd && listener.onTouchEnd(event);
92
- listener?.onTouchCancel && listener.onTouchCancel(event);
93
- dragStatus.value = DragStatus.End;
94
- };
95
-
96
- const resetTouchStatus = () => {
97
- offset.endX = 0;
98
- offset.endY = 0;
99
- offset.offsetX = 0;
100
- offset.offsetY = 0;
101
- offset.direction = '';
102
- beginOffset = { offsetX: 0, offsetY: 0 };
103
- dragStatus.value = DragStatus.NotStarted;
104
- };
105
-
106
- if (target) {
107
- useEventListener('touchstart', touchStart, { target });
108
- useEventListener('touchmove', wait > 0 ? throttle(touchMove, wait) : touchMove, {
109
- target,
110
- passive: false
111
- });
112
- useEventListener('touchend', touchEnd, { target });
113
- }
114
-
115
- return {
116
- offset,
117
- dragStatus,
118
- resetTouchStatus
119
- };
120
- }