@mdui/shared 1.0.3 → 1.0.4

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.
@@ -16,17 +16,17 @@ export const observeResize = (target, callback) => {
16
16
  const result = {
17
17
  unobserve: () => {
18
18
  $target.each((_, target) => {
19
- const coArr = weakMap.get(target) ?? [];
20
- const index = coArr.findIndex((co) => co.key === key);
19
+ const options = weakMap.get(target);
20
+ const index = options.coArr.findIndex((co) => co.key === key);
21
21
  if (index !== -1) {
22
- coArr.splice(index, 1);
22
+ options.coArr.splice(index, 1);
23
23
  }
24
- if (!coArr.length) {
24
+ if (!options.coArr.length) {
25
25
  observer.unobserve(target);
26
26
  weakMap.delete(target);
27
27
  }
28
28
  else {
29
- weakMap.set(target, coArr);
29
+ weakMap.set(target, options);
30
30
  }
31
31
  });
32
32
  },
@@ -37,8 +37,9 @@ export const observeResize = (target, callback) => {
37
37
  observer = new ResizeObserver((entries) => {
38
38
  entries.forEach((entry) => {
39
39
  const target = entry.target;
40
- const coArr = weakMap.get(target);
41
- coArr.forEach((co) => {
40
+ const options = weakMap.get(target);
41
+ options.entry = entry;
42
+ options.coArr.forEach((co) => {
42
43
  co.callback.call(result, entry, result);
43
44
  });
44
45
  });
@@ -46,10 +47,14 @@ export const observeResize = (target, callback) => {
46
47
  }
47
48
  // 添加监听
48
49
  $target.each((_, target) => {
50
+ const options = weakMap.get(target) ?? { coArr: [] };
51
+ // 同一个元素已添加过监听后,再次添加新的监听时,不会立即执行回调函数,所以这里手动调用一次回调函数
52
+ if (options.coArr.length && options.entry) {
53
+ callback.call(result, options.entry, result);
54
+ }
55
+ options.coArr.push({ callback, key });
56
+ weakMap.set(target, options);
49
57
  observer.observe(target);
50
- const coArr = weakMap.get(target) ?? [];
51
- coArr.push({ callback, key });
52
- weakMap.set(target, coArr);
53
58
  });
54
59
  return result;
55
60
  };
@@ -10,15 +10,19 @@ import '@mdui/jq/methods/width.js';
10
10
  * @param fresh 是否重新计算
11
11
  */
12
12
  export declare const getScrollBarSize: (fresh?: boolean) => number;
13
+ /**
14
+ * 判断指定元素当前是否有滚动条
15
+ */
16
+ export declare const hasScrollbar: (target: HTMLElement) => boolean;
13
17
  /**
14
18
  * 锁定指定元素,禁止滚动。对同一个元素多次调用此方法,只会锁定一次
15
19
  * @param source 由该元素触发锁定
16
- * @param target 锁定该元素的滚动状态,默认为 body
20
+ * @param target 锁定该元素的滚动状态,默认为 html
17
21
  */
18
22
  export declare const lockScreen: (source: HTMLElement, target?: HTMLElement) => void;
19
23
  /**
20
24
  * 解除指定元素的滚动状态锁定。
21
25
  * @param source 由该元素触发锁定
22
- * @param target 锁定该元素的滚动状态,默认为 body
26
+ * @param target 锁定该元素的滚动状态,默认为 html
23
27
  */
24
28
  export declare const unlockScreen: (source: HTMLElement, target?: HTMLElement) => void;
package/helpers/scroll.js CHANGED
@@ -47,33 +47,41 @@ export const getScrollBarSize = (fresh) => {
47
47
  }
48
48
  return scrollBarSizeCached;
49
49
  };
50
+ /**
51
+ * 判断指定元素当前是否有滚动条
52
+ */
53
+ export const hasScrollbar = (target) => {
54
+ return target.scrollHeight > target.clientHeight;
55
+ };
50
56
  const lockMap = new WeakMap();
51
57
  const className = 'mdui-lock-screen';
52
58
  /**
53
59
  * 锁定指定元素,禁止滚动。对同一个元素多次调用此方法,只会锁定一次
54
60
  * @param source 由该元素触发锁定
55
- * @param target 锁定该元素的滚动状态,默认为 body
61
+ * @param target 锁定该元素的滚动状态,默认为 html
56
62
  */
57
63
  export const lockScreen = (source, target) => {
58
64
  const document = getDocument();
59
- target ??= document.body;
65
+ target ??= document.documentElement;
60
66
  if (!lockMap.has(target)) {
61
67
  lockMap.set(target, new Set());
62
68
  }
63
69
  const lock = lockMap.get(target);
64
70
  lock.add(source);
65
- $(target)
66
- .addClass(className)
67
- .css('width', `calc(100% - ${getScrollBarSize()}px)`);
71
+ const $target = $(target);
72
+ if (hasScrollbar(target)) {
73
+ $target.css('width', `calc(100% - ${getScrollBarSize()}px)`);
74
+ }
75
+ $target.addClass(className);
68
76
  };
69
77
  /**
70
78
  * 解除指定元素的滚动状态锁定。
71
79
  * @param source 由该元素触发锁定
72
- * @param target 锁定该元素的滚动状态,默认为 body
80
+ * @param target 锁定该元素的滚动状态,默认为 html
73
81
  */
74
82
  export const unlockScreen = (source, target) => {
75
83
  const document = getDocument();
76
- target ??= document.body;
84
+ target ??= document.documentElement;
77
85
  const lock = lockMap.get(target);
78
86
  if (!lock) {
79
87
  return;
@@ -24,7 +24,7 @@ export const FocusableMixin = (superclass) => {
24
24
  constructor() {
25
25
  super(...arguments);
26
26
  /**
27
- * 是否在页面加载完成后自动获得焦点
27
+ * 是否在页面加载完成后自动获取焦点
28
28
  */
29
29
  this.autofocus = false;
30
30
  /**
@@ -42,7 +42,7 @@ export const FocusableMixin = (superclass) => {
42
42
  this._tabIndex = 0;
43
43
  }
44
44
  /**
45
- * 通过 Tab 键在元素之间切换焦点时,tabIndex 属性指定了元素获取焦点的顺序
45
+ * 元素在使用 Tab 键切换焦点时的顺序
46
46
  */
47
47
  get tabIndex() {
48
48
  const $this = $(this);
@@ -131,11 +131,11 @@ export const FocusableMixin = (superclass) => {
131
131
  }
132
132
  }
133
133
  /**
134
- * 将焦点设置在当前元素。
134
+ * 将焦点设置到当前元素。
135
135
  *
136
- * 可传入一个对象作为参数。对象属性为:
136
+ * 可以传入一个对象作为参数,该对象的属性包括:
137
137
  *
138
- * * `preventScroll`:默认情况下,在聚焦后会滚动页面,以将聚焦的元素滚动到视图中。可将该属性设为 `true` 以阻止页面滚动。
138
+ * * `preventScroll`:默认情况下,元素获取焦点后,页面会滚动以将该元素滚动到视图中。如果不希望页面滚动,可以将此属性设置为 `true`。
139
139
  */
140
140
  focus(options) {
141
141
  if (this.focusDisabled || !this.focusElement) {
@@ -149,7 +149,7 @@ export const FocusableMixin = (superclass) => {
149
149
  }
150
150
  }
151
151
  /**
152
- * 从当前元素中移除焦点
152
+ * 移除当前元素的焦点
153
153
  */
154
154
  blur() {
155
155
  if (this.focusElement !== this) {
@@ -48,6 +48,7 @@ export const ScrollBehaviorMixin = (superclass) => {
48
48
  if ((oldValue && !newValue) || (!oldValue && newValue)) {
49
49
  this.updateContainerPadding();
50
50
  }
51
+ // @ts-ignore
51
52
  if (!this.scrollBehavior) {
52
53
  return;
53
54
  }
@@ -71,6 +72,7 @@ export const ScrollBehaviorMixin = (superclass) => {
71
72
  if (!listening) {
72
73
  return;
73
74
  }
75
+ // @ts-ignore
74
76
  if (this.scrollBehavior) {
75
77
  this.updateScrollTop(listening);
76
78
  listening.addEventListener('scroll', this.onListeningScroll);
@@ -98,6 +100,7 @@ export const ScrollBehaviorMixin = (superclass) => {
98
100
  * @param behavior 为数组时,只要其中一个行为在 scrollBehavior 中,即返回 `true`
99
101
  */
100
102
  hasScrollBehavior(behavior) {
103
+ // @ts-ignore
101
104
  const behaviors = (this.scrollBehavior?.split(' ') ??
102
105
  []);
103
106
  if (Array.isArray(behavior)) {
@@ -193,9 +196,6 @@ export const ScrollBehaviorMixin = (superclass) => {
193
196
  __decorate([
194
197
  property({ attribute: 'scroll-target' })
195
198
  ], ScrollBehaviorMixinClass.prototype, "scrollTarget", void 0);
196
- __decorate([
197
- property({ reflect: true, attribute: 'scroll-behavior' })
198
- ], ScrollBehaviorMixinClass.prototype, "scrollBehavior", void 0);
199
199
  __decorate([
200
200
  property({ type: Number, reflect: true, attribute: 'scroll-threshold' })
201
201
  ], ScrollBehaviorMixinClass.prototype, "scrollThreshold", void 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdui/shared",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "mdui 项目的公共部分",
5
5
  "type": "module",
6
6
  "files": [