dlsjs 0.1.25 → 1.0.3

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "林川",
4
4
  "license": "MIT",
5
5
  "private": false,
6
- "version": "0.1.25",
6
+ "version": "1.0.3",
7
7
  "description": "domain-language for js",
8
8
  "main": "dist/dlsjs.esm.js",
9
9
  "module": "dist/dlsjs.esm.js",
@@ -125,7 +125,3 @@ function getImageSizeByUrl(url) {
125
125
  image.src = url;
126
126
  });
127
127
  }
128
-
129
- export default {
130
- movePanel, moveRelate, getTextWidth, getImageSizeByUrl
131
- }
@@ -172,7 +172,3 @@ export function toBase64(str){
172
172
  let encode = encodeURI(str);
173
173
  return btoa(encode);
174
174
  }
175
-
176
- export default {
177
- chooseFile, requestFile, requestJSON, downloadBlob, file2Json, json2Blob, toBase64, downloadByUrl
178
- }
@@ -59,12 +59,3 @@ export function sessionSet (key, value) {
59
59
  export function sessionRemove (key) {
60
60
  window.sessionStorage.removeItem(key)
61
61
  }
62
-
63
- export default {
64
- localGet,
65
- localSet,
66
- localRemove,
67
- sessionGet,
68
- sessionSet,
69
- sessionRemove
70
- }
@@ -0,0 +1,208 @@
1
+ /**
2
+ * 检测是否为移动设备
3
+ * 综合使用User-Agent、屏幕尺寸和触摸支持进行判断
4
+ * 时间复杂度O(1):直接访问浏览器属性和正则匹配
5
+ *
6
+ * @returns {boolean} 是否为移动设备
7
+ *
8
+ * @example
9
+ * if (isMobileDevice()) {
10
+ * console.log('当前在移动设备上');
11
+ * // 加载移动端优化样式
12
+ * }
13
+ */
14
+ export function isMobileDevice() {
15
+ // 1. 检查User-Agent
16
+ const mobileUserAgent = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
17
+ const isMobileUA = mobileUserAgent.test(navigator.userAgent);
18
+
19
+ // 2. 检查屏幕尺寸(移动设备通常宽度小于768px)
20
+ const isSmallScreen = window.innerWidth <= 768;
21
+
22
+ // 3. 检查触摸支持
23
+ const hasTouchSupport = 'ontouchstart' in window ||
24
+ navigator.maxTouchPoints > 0 ||
25
+ navigator.msMaxTouchPoints > 0;
26
+
27
+ // 4. 检查设备像素比(移动设备通常有较高的DPI)
28
+ const isHighDPI = window.devicePixelRatio >= 1.5;
29
+
30
+ // 综合判断:User-Agent匹配或(小屏幕且支持触摸)
31
+ return isMobileUA || (isSmallScreen && hasTouchSupport);
32
+ }
33
+
34
+ /**
35
+ * 获取设备类型详细信息
36
+ * 时间复杂度O(1):直接访问浏览器属性和正则匹配
37
+ *
38
+ * @returns {Object} 设备信息对象,包含类型、屏幕尺寸等
39
+ *
40
+ * @example
41
+ * const deviceInfo = getDeviceInfo();
42
+ * console.log(deviceInfo.type); // 'mobile', 'tablet', 'desktop'
43
+ * console.log(deviceInfo.screenWidth); // 屏幕宽度
44
+ */
45
+ export function getDeviceInfo() {
46
+ const userAgent = navigator.userAgent.toLowerCase();
47
+ const screenWidth = window.innerWidth;
48
+
49
+ let type = 'desktop';
50
+ let subType = 'unknown';
51
+
52
+ // 检测设备类型
53
+ if (/mobile|android|iphone|ipod/.test(userAgent)) {
54
+ type = 'mobile';
55
+ if (/iphone|ipod/.test(userAgent)) subType = 'iphone';
56
+ else if (/android/.test(userAgent)) subType = 'android';
57
+ } else if (/ipad/.test(userAgent) || (screenWidth <= 1024 && screenWidth > 768)) {
58
+ type = 'tablet';
59
+ if (/ipad/.test(userAgent)) subType = 'ipad';
60
+ }
61
+
62
+ // 根据屏幕尺寸进一步调整
63
+ if (type === 'desktop' && screenWidth <= 768) {
64
+ type = 'mobile';
65
+ }
66
+
67
+ return {
68
+ type,
69
+ subType,
70
+ screenWidth,
71
+ screenHeight: window.innerHeight,
72
+ pixelRatio: window.devicePixelRatio,
73
+ userAgent: navigator.userAgent
74
+ };
75
+ }
76
+
77
+ /**
78
+ * 复制文本到剪贴板
79
+ * 优先使用现代Clipboard API,降级到传统execCommand方法
80
+ * 时间复杂度O(1):直接调用浏览器API
81
+ *
82
+ * @param {string} text - 要复制的文本内容
83
+ * @returns {Promise<boolean>} 复制是否成功
84
+ *
85
+ * @example
86
+ * // 使用async/await
87
+ * const success = await copyText('Hello World');
88
+ * if (success) {
89
+ * console.log('复制成功');
90
+ * }
91
+ *
92
+ * // 使用Promise
93
+ * copyText('Hello World').then(success => {
94
+ * if (success) {
95
+ * console.log('复制成功');
96
+ * }
97
+ * });
98
+ */
99
+ export function copyText(text) {
100
+ // 输入验证
101
+ if (typeof text !== 'string') {
102
+ console.warn('dlsjs: copyText 参数必须是字符串');
103
+ return Promise.resolve(false);
104
+ }
105
+
106
+ if (text === '') {
107
+ console.warn('dlsjs: copyText 不能复制空字符串');
108
+ return Promise.resolve(false);
109
+ }
110
+
111
+ // 优先使用现代Clipboard API(需要安全上下文)
112
+ if (navigator.clipboard && window.isSecureContext) {
113
+ return navigator.clipboard.writeText(text)
114
+ .then(() => true)
115
+ .catch(error => {
116
+ console.error('dlsjs: 使用Clipboard API复制失败:', error);
117
+ return fallbackCopyText(text);
118
+ });
119
+ }
120
+
121
+ // 降级到传统方法
122
+ return Promise.resolve(fallbackCopyText(text));
123
+ }
124
+
125
+ /**
126
+ * 传统复制文本方法(降级方案)
127
+ * 时间复杂度O(1):DOM操作和execCommand调用
128
+ *
129
+ * @param {string} text - 要复制的文本
130
+ * @returns {boolean} 复制是否成功
131
+ */
132
+ function fallbackCopyText(text) {
133
+ try {
134
+ // 创建临时textarea元素(比input更适合多行文本)
135
+ const textarea = document.createElement('textarea');
136
+ textarea.value = text;
137
+ textarea.style.position = 'fixed';
138
+ textarea.style.left = '-9999px';
139
+ textarea.style.top = '0';
140
+ textarea.setAttribute('readonly', '');
141
+
142
+ document.body.appendChild(textarea);
143
+
144
+ // 选择文本(兼容不同浏览器)
145
+ if (textarea.select) {
146
+ textarea.select();
147
+ } else if (textarea.setSelectionRange) {
148
+ textarea.setSelectionRange(0, textarea.value.length);
149
+ }
150
+
151
+ // 尝试复制
152
+ const successful = document.execCommand('copy');
153
+ document.body.removeChild(textarea);
154
+
155
+ if (!successful) {
156
+ console.warn('dlsjs: 传统复制方法执行失败');
157
+ }
158
+
159
+ return successful;
160
+ } catch (error) {
161
+ console.error('dlsjs: 传统复制方法出错:', error);
162
+ return false;
163
+ }
164
+ }
165
+
166
+ /**
167
+ * 检查剪贴板API是否可用
168
+ * 时间复杂度O(1):直接检查浏览器API支持
169
+ *
170
+ * @returns {boolean} 剪贴板API是否可用
171
+ *
172
+ * @example
173
+ * if (isClipboardSupported()) {
174
+ * // 可以使用现代剪贴板API
175
+ * } else {
176
+ * // 需要使用传统方法
177
+ * }
178
+ */
179
+ export function isClipboardSupported() {
180
+ return !!(navigator.clipboard && window.isSecureContext);
181
+ }
182
+
183
+ /**
184
+ * 读取剪贴板文本内容
185
+ * 需要用户授权,只能在安全上下文(HTTPS)中使用
186
+ * 时间复杂度O(1):直接调用浏览器API
187
+ *
188
+ * @returns {Promise<string>} 剪贴板中的文本内容
189
+ *
190
+ * @example
191
+ * try {
192
+ * const text = await readClipboardText();
193
+ * console.log('剪贴板内容:', text);
194
+ * } catch (error) {
195
+ * console.error('读取剪贴板失败:', error);
196
+ * }
197
+ */
198
+ export function readClipboardText() {
199
+ if (!isClipboardSupported()) {
200
+ return Promise.reject(new Error('dlsjs: 当前环境不支持剪贴板读取'));
201
+ }
202
+
203
+ return navigator.clipboard.readText()
204
+ .catch(error => {
205
+ console.error('dlsjs: 读取剪贴板失败:', error);
206
+ throw error;
207
+ });
208
+ }
@@ -0,0 +1,175 @@
1
+ /**
2
+ * 切换全屏状态
3
+ * 如果当前处于全屏模式则退出,否则进入全屏模式
4
+ * 时间复杂度O(1):直接调用浏览器API,无循环操作
5
+ *
6
+ * @param {HTMLElement} [element=document.documentElement] - 要设置为全屏的元素,默认为整个文档
7
+ * @returns {boolean} 操作是否成功执行
8
+ *
9
+ * @example
10
+ * // 切换整个文档的全屏状态
11
+ * toggleFullscreen();
12
+ *
13
+ * // 切换指定元素的全屏状态
14
+ * toggleFullscreen(document.getElementById('myElement'));
15
+ */
16
+ export function toggleFullscreen(element = document.documentElement) {
17
+ if (!element || !(element instanceof HTMLElement)) {
18
+ console.warn('dlsjs: toggleFullscreen 参数必须是有效的HTML元素');
19
+ return false;
20
+ }
21
+
22
+ if (isFullscreenActive()) {
23
+ return exitFullscreen();
24
+ } else {
25
+ return requestFullscreen(element);
26
+ }
27
+ }
28
+
29
+ /**
30
+ * 请求进入全屏模式
31
+ * 时间复杂度O(1):直接调用浏览器API,无循环操作
32
+ *
33
+ * @param {HTMLElement} element - 要设置为全屏的元素
34
+ * @returns {boolean} 操作是否成功发起
35
+ *
36
+ * @example
37
+ * requestFullscreen(document.getElementById('myElement'));
38
+ */
39
+ export function requestFullscreen(element) {
40
+ if (!element || !(element instanceof HTMLElement)) {
41
+ console.warn('dlsjs: requestFullscreen 参数必须是有效的HTML元素');
42
+ return false;
43
+ }
44
+
45
+ try {
46
+ // 处理不同浏览器的全屏API
47
+ const requestMethod =
48
+ element.requestFullscreen ||
49
+ element.webkitRequestFullscreen ||
50
+ element.mozRequestFullScreen ||
51
+ element.msRequestFullscreen;
52
+
53
+ if (requestMethod) {
54
+ requestMethod.call(element);
55
+ return true;
56
+ } else {
57
+ console.warn('dlsjs: 当前浏览器不支持全屏API');
58
+ return false;
59
+ }
60
+ } catch (error) {
61
+ console.error('dlsjs: 全屏请求失败:', error);
62
+ return false;
63
+ }
64
+ }
65
+
66
+ /**
67
+ * 退出全屏模式
68
+ * 时间复杂度O(1):直接调用浏览器API,无循环操作
69
+ *
70
+ * @returns {boolean} 操作是否成功发起
71
+ *
72
+ * @example
73
+ * exitFullscreen();
74
+ */
75
+ export function exitFullscreen() {
76
+ try {
77
+ // 处理不同浏览器的退出全屏API
78
+ const exitMethod =
79
+ document.exitFullscreen ||
80
+ document.webkitExitFullscreen ||
81
+ document.mozCancelFullScreen ||
82
+ document.msExitFullscreen;
83
+
84
+ if (exitMethod) {
85
+ exitMethod.call(document);
86
+ return true;
87
+ } else {
88
+ console.warn('dlsjs: 当前浏览器不支持退出全屏API');
89
+ return false;
90
+ }
91
+ } catch (error) {
92
+ console.error('dlsjs: 退出全屏失败:', error);
93
+ return false;
94
+ }
95
+ }
96
+
97
+ /**
98
+ * 检查当前是否处于全屏模式
99
+ * 时间复杂度O(1):直接访问浏览器属性,无循环操作
100
+ *
101
+ * @returns {boolean} 当前是否处于全屏模式
102
+ *
103
+ * @example
104
+ * if (isFullscreenActive()) {
105
+ * console.log('当前处于全屏模式');
106
+ * }
107
+ */
108
+ export function isFullscreenActive() {
109
+ return !!(
110
+ document.fullscreenElement ||
111
+ document.webkitFullscreenElement ||
112
+ document.mozFullScreenElement ||
113
+ document.msFullscreenElement
114
+ );
115
+ }
116
+
117
+ /**
118
+ * 获取当前全屏元素
119
+ * 时间复杂度O(1):直接访问浏览器属性,无循环操作
120
+ *
121
+ * @returns {HTMLElement|null} 当前全屏元素,如果没有则返回null
122
+ *
123
+ * @example
124
+ * const fullscreenElement = getFullscreenElement();
125
+ */
126
+ export function getFullscreenElement() {
127
+ return (
128
+ document.fullscreenElement ||
129
+ document.webkitFullscreenElement ||
130
+ document.mozFullScreenElement ||
131
+ document.msFullscreenElement ||
132
+ null
133
+ );
134
+ }
135
+
136
+ /**
137
+ * 添加全屏状态变化监听器
138
+ * 时间复杂度O(1):直接添加事件监听器,无循环操作
139
+ *
140
+ * @param {Function} callback - 状态变化时的回调函数
141
+ * @returns {Function} 移除监听器的函数
142
+ *
143
+ * @example
144
+ * const removeListener = addFullscreenChangeListener(() => {
145
+ * console.log('全屏状态发生变化');
146
+ * });
147
+ *
148
+ * // 稍后移除监听器
149
+ * removeListener();
150
+ */
151
+ export function addFullscreenChangeListener(callback) {
152
+ if (typeof callback !== 'function') {
153
+ console.warn('dlsjs: addFullscreenChangeListener 参数必须是函数');
154
+ return () => {};
155
+ }
156
+
157
+ const eventName =
158
+ 'fullscreenchange' in document ? 'fullscreenchange' :
159
+ 'webkitfullscreenchange' in document ? 'webkitfullscreenchange' :
160
+ 'mozfullscreenchange' in document ? 'mozfullscreenchange' :
161
+ 'MSFullscreenChange' in document ? 'MSFullscreenChange' : null;
162
+
163
+ if (!eventName) {
164
+ console.warn('dlsjs: 当前浏览器不支持全屏变化事件');
165
+ return () => {};
166
+ }
167
+
168
+ const handler = () => callback(isFullscreenActive());
169
+ document.addEventListener(eventName, handler);
170
+
171
+ // 返回移除监听器的函数
172
+ return () => {
173
+ document.removeEventListener(eventName, handler);
174
+ };
175
+ }
@@ -97,7 +97,3 @@ export class Poll {
97
97
  this.timer = null
98
98
  }
99
99
  }
100
-
101
- export default {
102
- waitValue, Poll, lazyChange, throttleAsync, debounceAsync
103
- }