vite-uni-dev-tool 0.0.20 → 0.0.21

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/README.md CHANGED
@@ -146,6 +146,7 @@ optimizeDeps: {
146
146
  ![image.png](https://gitee.com/cloud_l/vite-uni-dev-tool/raw/master/packages/docs/public/network2.jpg)
147
147
  ![image.png](https://gitee.com/cloud_l/vite-uni-dev-tool/raw/master/packages/docs/public/network3.jpg)
148
148
  ![image.png](https://gitee.com/cloud_l/vite-uni-dev-tool/raw/master/packages/docs/public/network4.jpg)
149
+ ![image.png](https://gitee.com/cloud_l/vite-uni-dev-tool/raw/master/packages/docs/public/network5.jpg)
149
150
 
150
151
  - 支持 upload 监控
151
152
 
@@ -197,6 +198,11 @@ optimizeDeps: {
197
198
 
198
199
  ## 更新日志
199
200
 
201
+ ### 0.0.21
202
+
203
+ - 增加 source code 部分语法高亮
204
+ - 补充 network detail 字段
205
+
200
206
  ### 0.0.20
201
207
 
202
208
  - 修复 storage 调用异常
@@ -1230,6 +1230,16 @@ function onOpenCode(value?: string) {
1230
1230
  --dev-tool-symbol-color: bisque;
1231
1231
  --dev-tool-active-bg-color: #f0f0f0;
1232
1232
  --dev-tool-time-line-color: #9254de;
1233
+
1234
+ --dev-tool-note-color: #9c9c9c;
1235
+
1236
+ --dev-tool-keyword-color: #9254de;
1237
+ --dev-tool-bracket-color: #ffa940;
1238
+ --dev-tool-quotation-color: #91e76a;
1239
+ --dev-tool-string-color: #91e76a;
1240
+ --dev-tool-function-color: #1d8ce0;
1241
+ --dev-tool-attribute-color: #ef66ba;
1242
+ --dev-tool-operator-color: #50d3eb;
1233
1243
  }
1234
1244
  .dev-tool-window-dark {
1235
1245
  --dev-tool-bg-color: rgba(0, 0, 0, 0.8);
@@ -1243,6 +1253,16 @@ function onOpenCode(value?: string) {
1243
1253
  --dev-tool-active-bg-color: #3d3d3d;
1244
1254
 
1245
1255
  --dev-tool-time-line-color: #f9f9f9;
1256
+
1257
+ --dev-tool-note-color: #9c9c9c;
1258
+
1259
+ --dev-tool-keyword-color: #9254de;
1260
+ --dev-tool-bracket-color: #ffa940;
1261
+ --dev-tool-quotation-color: #91e76a;
1262
+ --dev-tool-string-color: #91e76a;
1263
+ --dev-tool-function-color: #1d8ce0;
1264
+ --dev-tool-attribute-color: #ef66ba;
1265
+ --dev-tool-operator-color: #50d3eb;
1246
1266
  }
1247
1267
 
1248
1268
  .dev-tool-window-bg {
@@ -16,7 +16,7 @@
16
16
  <view class="network-detail-header" v-if="currentSelect === 'header'">
17
17
  <DevToolTitle>常规</DevToolTitle>
18
18
  <view class="net-detail-item">
19
- <view>请求url:</view>
19
+ <view>请求地址:</view>
20
20
  <view>{{ network.url }}</view>
21
21
  </view>
22
22
  <view class="net-detail-item">
@@ -27,31 +27,65 @@
27
27
  <view>状态代码:</view>
28
28
  {{ network.status }}
29
29
  </view>
30
+ <view class="net-detail-item">
31
+ <view>开始时间:</view>
32
+ {{
33
+ network.startTime
34
+ ? formatDate(network.startTime, 'HH:mm:ss:SS')
35
+ : '--:--:--'
36
+ }}
37
+ </view>
38
+ <view class="net-detail-item">
39
+ <view>结束时间:</view>
40
+ {{
41
+ network.endTime
42
+ ? formatDate(network.endTime, 'HH:mm:ss:SS')
43
+ : '--:--:--'
44
+ }}
45
+ </view>
46
+ <view class="net-detail-item">
47
+ <view>请求耗时:</view>
48
+ {{ network.time ?? '--' }}
49
+ </view>
50
+ <view class="net-detail-item">
51
+ <view>请求大小:</view>
52
+ {{ network.size ?? '--' }}
53
+ </view>
30
54
 
31
55
  <DevToolTitle>请求头</DevToolTitle>
32
- <view
33
- class="net-detail-item"
34
- v-for="item in network.headers.requestHeader"
35
- :key="item.key"
36
- >
37
- <view>{{ item.key }}:</view>
38
- {{ item.value }}
56
+ <template v-if="network.headers.requestHeader.length > 0">
57
+ <view
58
+ class="net-detail-item"
59
+ v-for="item in network.headers.requestHeader"
60
+ :key="item.key"
61
+ >
62
+ <view>{{ item.key }}:</view>
63
+ {{ item.value }}
64
+ </view>
65
+ </template>
66
+ <view v-else>
67
+ <Empty text="暂无请求头" />
39
68
  </view>
40
69
 
41
70
  <DevToolTitle>响应头</DevToolTitle>
42
- <view
43
- class="net-detail-item"
44
- v-for="item in network.headers.responseHeader"
45
- :key="item.key"
46
- >
47
- <view>{{ item.key }}:</view>
48
- {{ item.value }}
71
+ <template v-if="network.headers.responseHeader.length > 0">
72
+ <view
73
+ class="net-detail-item"
74
+ v-for="item in network.headers.responseHeader"
75
+ :key="item.key"
76
+ >
77
+ <view>{{ item.key }}:</view>
78
+ {{ item.value }}
79
+ </view>
80
+ </template>
81
+ <view v-else>
82
+ <Empty text="暂无响应头" />
49
83
  </view>
50
84
  </view>
51
85
 
52
86
  <template v-if="currentSelect === 'payload'">
53
87
  <template v-if="payload?.type === 'object'">
54
- <JsonPretty :data="payload.value" />
88
+ <JsonPretty :data="payload.value" virtual :height="height" />
55
89
  </template>
56
90
  <view
57
91
  v-else-if="payload?.type === 'string'"
@@ -64,7 +98,7 @@
64
98
 
65
99
  <template v-if="currentSelect === 'response'">
66
100
  <template v-if="response?.type === 'object'">
67
- <JsonPretty :data="response.value" virtual />
101
+ <JsonPretty :data="response.value" virtual :height="height" />
68
102
  </template>
69
103
  <view
70
104
  v-else-if="response?.type === 'string'"
@@ -77,13 +111,14 @@
77
111
  </view>
78
112
  </template>
79
113
  <script lang="ts" setup>
80
- import { ref, computed } from 'vue';
114
+ import { ref, computed, onMounted } from 'vue';
81
115
  import JsonPretty from '../JsonPretty/index.vue';
82
116
  import Tag from '../Tag/index.vue';
83
117
  import Empty from '../Empty/index.vue';
84
118
  import CircularButton from '../CircularButton/index.vue';
85
119
  import DevToolTitle from '../DevToolTitle/index.vue';
86
120
  import type { DevTool } from '../../type';
121
+ import { formatDate } from '../../utils';
87
122
  const props = defineProps<{ network: DevTool.NetworkItem; zIndex?: number }>();
88
123
  const emit = defineEmits<{ (e: 'close'): void }>();
89
124
  const selectItems = [
@@ -100,6 +135,9 @@ const selectItems = [
100
135
  value: 'response',
101
136
  },
102
137
  ];
138
+
139
+ const height = ref(0);
140
+
103
141
  const currentSelect = ref('header');
104
142
 
105
143
  const payload = computed(() => {
@@ -148,6 +186,12 @@ function onSelect(item: { label: string; value: string }) {
148
186
  function onClose() {
149
187
  emit('close');
150
188
  }
189
+
190
+ onMounted(() => {
191
+ const { windowHeight } = uni.getWindowInfo();
192
+
193
+ height.value = windowHeight - 32 - 32 - 2;
194
+ });
151
195
  </script>
152
196
  <style scoped>
153
197
  .network-detail {
@@ -166,7 +210,7 @@ function onClose() {
166
210
  .network-detail .network-detail-control {
167
211
  display: flex;
168
212
  align-items: center;
169
- margin-bottom: 4px;
213
+ /* margin-bottom: 4px; */
170
214
  gap: 12px;
171
215
  height: 32px;
172
216
  border-bottom: 1px solid transparent;
@@ -1,7 +1,7 @@
1
1
  <template>
2
2
  <view class="setting-content">
3
3
  <view class="setting-item">
4
- <DevToolTitle>DevTool(v0.0.20-vue3)</DevToolTitle>
4
+ <DevToolTitle>DevTool(v0.0.21-vue3)</DevToolTitle>
5
5
  <view class="setting-item-content">
6
6
  <view class="setting-row">
7
7
  <view>显示调试按钮:</view>
@@ -0,0 +1,101 @@
1
+ <template>
2
+ <view
3
+ :class="['source-code-line', active ? 'source-code-line-active' : '']"
4
+ :id="id"
5
+ >
6
+ <view class="source-code-line-num" v-if="showNum">{{ num }}</view>
7
+ <view :class="['source-code-line-content', line.class]">
8
+ <view
9
+ v-for="item in line.words"
10
+ :class="['source-code-line-word', item.class]"
11
+ >
12
+ {{ item.word }}
13
+ </view>
14
+ </view>
15
+ </view>
16
+ </template>
17
+ <script lang="ts" setup>
18
+ import type { LineInfo } from './parseCode';
19
+
20
+ const props = withDefaults(
21
+ defineProps<{
22
+ line: LineInfo;
23
+
24
+ id: string;
25
+ active?: boolean;
26
+ num?: number;
27
+ showNum?: boolean;
28
+ }>(),
29
+ {
30
+ showNum: true,
31
+ },
32
+ );
33
+ </script>
34
+ <style lang="scss" scoped>
35
+ .source-code-line {
36
+ display: flex;
37
+ align-items: center;
38
+ height: 24px;
39
+ }
40
+
41
+ .source-code-line-active {
42
+ color: #fff;
43
+ background-color: var(--dev-tool-main-color);
44
+ }
45
+
46
+ .source-code-line-num {
47
+ flex-shrink: 0;
48
+ width: 30px;
49
+ padding: 0 6px;
50
+ margin-right: 6px;
51
+ text-align: right;
52
+ height: 24px;
53
+ border-right: 1px solid var(--dev-tool-border-color);
54
+ }
55
+
56
+ .source-code-line-content {
57
+ display: flex;
58
+ white-space: nowrap;
59
+ height: 24px;
60
+ }
61
+
62
+ .source-code-line-word {
63
+ white-space: nowrap;
64
+ }
65
+
66
+ .source-code-pre-note {
67
+ color: var(--dev-tool-note-color);
68
+ }
69
+
70
+ .source-code-pre-space {
71
+ width: 8px;
72
+ }
73
+
74
+ .source-code-pre-keyword {
75
+ color: var(--dev-tool-keyword-color);
76
+ }
77
+
78
+ .source-code-pre-bracket {
79
+ color: var(--dev-tool-bracket-color);
80
+ }
81
+
82
+ .source-code-pre-quotation {
83
+ color: var(--dev-tool-quotation-color);
84
+ }
85
+
86
+ .source-code-pre-string {
87
+ color: var(--dev-tool-string-color);
88
+ }
89
+
90
+ .source-code-pre-function {
91
+ color: var(--dev-tool-function-color);
92
+ }
93
+
94
+ .source-code-pre-attribute {
95
+ color: var(--dev-tool-attribute-color);
96
+ }
97
+
98
+ .source-code-pre-operator {
99
+ color: var(--dev-tool-operator-color);
100
+ }
101
+ </style>
@@ -1,14 +1,14 @@
1
1
  <template>
2
2
  <view class="dev-tool-code" :style="{ zIndex: zIndex }">
3
3
  <view class="dev-tool-code-control">
4
- <FilterInput
4
+ <!-- <FilterInput
5
5
  v-model="modelValue"
6
6
  style="width: 100%"
7
7
  @search="onSearch"
8
- />
8
+ /> -->
9
+ <view class="dev-tool-code-title">{{ fileName }}</view>
9
10
  <CircularButton style="margin-left: auto" text="×" @click="onClose" />
10
11
  </view>
11
- <view class="dev-tool-code-title">{{ fileName }}</view>
12
12
 
13
13
  <VirtualListPro
14
14
  v-if="codes.length > 0"
@@ -17,8 +17,6 @@
17
17
  :height="height"
18
18
  :data="codes"
19
19
  :scrollWithAnimation="true"
20
- :defaultCurrent="defaultCurrent"
21
- :scrollIntoView="scrollIntoView"
22
20
  >
23
21
  <template #default="{ list, current, start }">
24
22
  <AutoSize
@@ -26,18 +24,12 @@
26
24
  :key="start + index"
27
25
  :index="start + index"
28
26
  >
29
- <view
27
+ <Line
28
+ :line="code"
30
29
  :id="`dev-code-${start + index}`"
31
- :class="`dev-tool-code-item ${start + index === activeRowCol.row ? 'dev-tool-code-item-active' : ''}`"
32
- >
33
- <view class="dev-tool-code-item-index">
34
- {{ start + index + 1 }}
35
- </view>
36
-
37
- <view class="dev-tool-code-item-content">
38
- {{ code }}
39
- </view>
40
- </view>
30
+ :num="start + index + 1"
31
+ :active="start + index === activeRowCol.row"
32
+ />
41
33
  </AutoSize>
42
34
  </template>
43
35
  </VirtualListPro>
@@ -46,13 +38,14 @@
46
38
  </template>
47
39
  <script lang="ts" setup>
48
40
  import { computed, ref, onMounted } from 'vue';
49
- import FilterInput from '../FilterInput/index.vue';
41
+ // import FilterInput from '../FilterInput/index.vue';
50
42
  import CircularButton from '../CircularButton/index.vue';
51
43
  import VirtualListPro from '../VirtualListPro/index.vue';
52
44
  import Empty from '../Empty/index.vue';
53
45
  import AutoSize from '../VirtualListPro/AutoSize.vue';
54
- import { hightLight, isAndroid, parseStock } from '../../utils';
55
-
46
+ import Line from './Line.vue';
47
+ import { isAndroid, parseStock } from '../../utils';
48
+ import { parseCode, type LineInfo } from './parseCode';
56
49
  const props = defineProps<{
57
50
  url: string;
58
51
  sourceFileServers?: string[];
@@ -62,7 +55,7 @@ const props = defineProps<{
62
55
 
63
56
  const emit = defineEmits<{ (e: 'close'): void }>();
64
57
 
65
- const modelValue = ref('');
58
+ // const modelValue = ref('');
66
59
  const height = ref(0);
67
60
  const fileName = computed(() => {
68
61
  const name =
@@ -73,20 +66,20 @@ const fileName = computed(() => {
73
66
  const activeRowCol = ref({ row: -1, col: -1, activeRow: -1 });
74
67
  const scrollIntoView = ref('');
75
68
 
76
- let backupCodes: string[] = [];
69
+ // let backupCodes: string[] = [];
77
70
 
78
- const codes = ref<string[]>([]);
79
- const defaultCurrent = ref(1);
80
- const pageSize = 25;
71
+ const codes = ref<LineInfo[]>([]);
72
+ // const defaultCurrent = ref(1);
73
+ const pageSize = 50;
81
74
 
82
75
  function onClose() {
83
76
  emit('close');
84
77
  }
85
78
 
86
79
  function onSearch(value: string) {
87
- codes.value = backupCodes.map((code) => {
88
- return hightLight(code, value);
89
- });
80
+ // codes.value = backupCodes.map((code) => {
81
+ // return hightLight(code, value);
82
+ // });
90
83
  }
91
84
 
92
85
  let timer: number;
@@ -139,24 +132,26 @@ function getCode(url: string, i: number = 0) {
139
132
  url: allUrl,
140
133
  success: (res) => {
141
134
  if (typeof res.data === 'string') {
142
- codes.value = res.data?.split('\n') ?? [];
135
+ codes.value = parseCode(res.data ?? '');
136
+
137
+ // res.data?.split('\n') ?? [];
143
138
 
144
139
  // 缓慢显示
145
140
  const row = activeRowCol.value.row;
146
141
  // defaultCurrent.value = Math.ceil(activeRowCol.value.row / pageSize);
147
142
  const targetCurrent = Math.ceil(row / pageSize);
148
143
 
149
- startCounter({
150
- start: 1,
151
- target: targetCurrent,
152
- duration: 500,
153
- onExecute(current) {
154
- scrollIntoView.value = `dev-code-${current * pageSize - 1}`;
155
- },
156
- onSuccess() {
157
- // scrollIntoView.value = `dev-code-${row}`;
158
- },
159
- });
144
+ // startCounter({
145
+ // start: 1,
146
+ // target: targetCurrent,
147
+ // duration: 500,
148
+ // onExecute(current) {
149
+ // scrollIntoView.value = `dev-code-${current * pageSize - 1}`;
150
+ // },
151
+ // onSuccess() {
152
+ // // scrollIntoView.value = `dev-code-${row}`;
153
+ // },
154
+ // });
160
155
  }
161
156
  },
162
157
  fail: (err) => {
@@ -185,7 +180,7 @@ function getSourceCodeDev(url: string) {
185
180
  onMounted(() => {
186
181
  const { windowHeight } = uni.getWindowInfo();
187
182
 
188
- height.value = windowHeight - 32 - 32 - 32;
183
+ height.value = windowHeight - 32 - 32;
189
184
 
190
185
  let url = props?.url;
191
186
 
@@ -253,25 +248,4 @@ onMounted(() => {
253
248
  .dev-tool-code-list {
254
249
  height: calc(100% - 68px);
255
250
  }
256
-
257
- .dev-tool-code-item {
258
- display: flex;
259
- align-items: center;
260
- height: 28px;
261
- }
262
- .dev-tool-code-item-active {
263
- color: #fff;
264
- background-color: var(--dev-tool-main-color);
265
- }
266
-
267
- .dev-tool-code-item-index {
268
- flex-shrink: 0;
269
- width: 20px;
270
- margin-right: 8px;
271
- text-align: right;
272
- }
273
-
274
- .dev-tool-code-item-content {
275
- white-space: pre;
276
- }
277
251
  </style>
@@ -0,0 +1,27 @@
1
+ type Word = {
2
+ /** 字符 */
3
+ word: string;
4
+ /** css 样式 */
5
+ class: string;
6
+ /** 分类
7
+ * - space: 空格
8
+ * - bracket: 括号
9
+ * - quotation: 引号
10
+ * - string: 字符串
11
+ * - function: 函数
12
+ * - attribute: 属性
13
+ * - operator: 操作符
14
+ * - symbol: 符号
15
+ * - keyword: 关键字
16
+ * - other: 其他
17
+ */
18
+ type: 'space' | 'bracket' | 'quotation' | 'string' | 'function' | 'attribute' | 'operator' | 'symbol' | 'keyword' | 'other';
19
+ };
20
+ export type LineInfo = {
21
+ type: 'empty' | 'tag' | 'script' | 'style' | 'note';
22
+ class: string;
23
+ words: Word[];
24
+ };
25
+ export declare function parseCode(code: string): LineInfo[];
26
+ export {};
27
+ //# sourceMappingURL=parseCode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseCode.d.ts","sourceRoot":"","sources":["../../../dev/v3/SourceCode/parseCode.ts"],"names":[],"mappings":"AA0IA,KAAK,IAAI,GAAG;IACV,SAAS;IACT,IAAI,EAAE,MAAM,CAAC;IACb,aAAa;IACb,KAAK,EAAE,MAAM,CAAC;IACd;;;;;;;;;;;OAWG;IACH,IAAI,EACA,OAAO,GACP,SAAS,GACT,WAAW,GACX,QAAQ,GACR,UAAU,GACV,WAAW,GACX,UAAU,GACV,QAAQ,GACR,SAAS,GACT,OAAO,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IACrB,IAAI,EAAE,OAAO,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,GAAG,MAAM,CAAC;IACpD,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,IAAI,EAAE,CAAC;CACf,CAAC;AAobF,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,EAAE,CAkClD"}
@@ -0,0 +1,643 @@
1
+ /** 行前空格匹配 */
2
+ const startSpaceReg = /^(\s*)/;
3
+
4
+ /** 括号 */
5
+ const bracketList = ['<', '>', '(', ')', '{', '}', '[', ']'];
6
+
7
+ /** 引号 */
8
+ const quotationList = ["'", '"', '‘', '’', '`'];
9
+
10
+ /** 操作符 */
11
+ const operatorList = ['+', '-', '*', '/', '%', '=', '!', '&'];
12
+
13
+ /** 符号 */
14
+ const symbolList = [
15
+ ' ',
16
+ '<',
17
+ '>',
18
+ '(',
19
+ ')',
20
+ '{',
21
+ '}',
22
+ '[',
23
+ ']',
24
+ ';',
25
+ ':',
26
+ ',',
27
+ '.',
28
+ '=',
29
+ '?',
30
+ '+',
31
+ '-',
32
+ '*',
33
+ '/',
34
+ '\\',
35
+ "'",
36
+ '"',
37
+ '@',
38
+ '#',
39
+ '$',
40
+ ];
41
+
42
+ /** 关键字 */
43
+ const keywordList = [
44
+ // JavaScript 关键字
45
+ 'var',
46
+ 'let',
47
+ 'const',
48
+ 'function',
49
+ 'class',
50
+ 'import',
51
+ 'export',
52
+ 'from',
53
+ 'as',
54
+ 'this',
55
+ 'super',
56
+ 'console',
57
+ 'window',
58
+ 'document',
59
+ 'if',
60
+ 'else',
61
+ 'for',
62
+ 'while',
63
+ 'do',
64
+ 'switch',
65
+ 'case',
66
+ 'default',
67
+ 'break',
68
+ 'continue',
69
+ 'return',
70
+ 'try',
71
+ 'catch',
72
+ 'finally',
73
+ 'throw',
74
+ 'new',
75
+ 'delete',
76
+ 'typeof',
77
+ 'instanceof',
78
+ 'in',
79
+ 'of',
80
+ 'void',
81
+ 'yield',
82
+ 'await',
83
+ 'async',
84
+ 'extends',
85
+ 'implements',
86
+ 'interface',
87
+ 'package',
88
+ 'private',
89
+ 'protected',
90
+ 'public',
91
+ 'static',
92
+ 'yield',
93
+ 'null',
94
+ 'undefined',
95
+ 'true',
96
+ 'false',
97
+ 'get',
98
+
99
+ // Vue 特定关键字
100
+ // 'ref',
101
+ // 'reactive',
102
+ // 'computed',
103
+ // 'watch',
104
+ // 'watchEffect',
105
+ // 'provide',
106
+ // 'inject',
107
+ // 'setup',
108
+ // 'onMounted',
109
+ // 'onUnmounted',
110
+ // 'onUpdated',
111
+ // 'onBeforeMount',
112
+ // 'onBeforeUpdate',
113
+ // 'onBeforeUnmount',
114
+ // 'nextTick',
115
+ // 'defineComponent',
116
+ // 'defineProps',
117
+ // 'defineEmits',
118
+ // 'defineExpose',
119
+ // 'withDefaults',
120
+
121
+ // // UniApp 特定关键字
122
+ // 'uni',
123
+ // 'getCurrentInstance',
124
+ // 'onLoad',
125
+ // 'onShow',
126
+ // 'onReady',
127
+ // 'onHide',
128
+ // 'onUnload',
129
+ // 'onPullDownRefresh',
130
+ // 'onReachBottom',
131
+ // 'onShareAppMessage',
132
+ // 'onShareTimeline',
133
+ // 'onAddToFavorites',
134
+ // 'onPageScroll',
135
+ // 'onResize',
136
+ // 'onTabItemTap',
137
+ ];
138
+
139
+ type Word = {
140
+ /** 字符 */
141
+ word: string;
142
+ /** css 样式 */
143
+ class: string;
144
+ /** 分类
145
+ * - space: 空格
146
+ * - bracket: 括号
147
+ * - quotation: 引号
148
+ * - string: 字符串
149
+ * - function: 函数
150
+ * - attribute: 属性
151
+ * - operator: 操作符
152
+ * - symbol: 符号
153
+ * - keyword: 关键字
154
+ * - other: 其他
155
+ */
156
+ type:
157
+ | 'space'
158
+ | 'bracket'
159
+ | 'quotation'
160
+ | 'string'
161
+ | 'function'
162
+ | 'attribute'
163
+ | 'operator'
164
+ | 'symbol'
165
+ | 'keyword'
166
+ | 'other';
167
+ };
168
+
169
+ export type LineInfo = {
170
+ type: 'empty' | 'tag' | 'script' | 'style' | 'note';
171
+ class: string;
172
+ words: Word[];
173
+ };
174
+
175
+ const scriptStartReg = /<script(\s[^>]*)?>/i;
176
+ const scriptEndReg = /<\/script>/i;
177
+
178
+ const styleStartReg = /<style(\s[^>]*)?>/i;
179
+ const styleEndReg = /<\/style>/i;
180
+
181
+ const preClass = 'source-code-pre';
182
+
183
+ /**
184
+ * 获取代码中所有 <script> 块的起止行号(不包含标签本身)
185
+ * @param lines 源码按行分割后的字符串数组
186
+ * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
187
+ */
188
+ function getScriptStartAndEndRow(lines: string[]): number[][] {
189
+ // 如果没有找到任何<script>标签,则认为整段代码都是script代码
190
+ let hasScriptTags = false;
191
+ const groups: number[][] = [];
192
+ let currentStart: number | null = null; // 记录当前 <script> 开始行号
193
+
194
+ lines.forEach((line, index) => {
195
+ if (scriptStartReg.test(line)) {
196
+ hasScriptTags = true;
197
+ // 遇到开始标签,记录下一行作为内容开始位置
198
+ currentStart = index + 1;
199
+ } else if (scriptEndReg.test(line) && currentStart !== null) {
200
+ // 遇到结束标签且已有开始位置,则记录为一个有效块
201
+ // 如果开始行大于等于结束行(即<script></script>在同一行或相邻行),则不构成有效内容区间
202
+ if (currentStart <= index - 1) {
203
+ groups.push([currentStart, index - 1]); // 结束行为上一行(排除 </script>)
204
+ }
205
+ currentStart = null; // 重置等待下一个 <script>
206
+ }
207
+ });
208
+
209
+ // 如果完全没有<script>标签,则整段代码都算作script内容
210
+ if (!hasScriptTags && lines.length > 0) {
211
+ // 整个文件都是script代码
212
+ groups.push([0, lines.length - 1]);
213
+ }
214
+
215
+ return groups;
216
+ }
217
+
218
+ /**
219
+ * 获取代码中所有 <style> 块的起止行号(不包含标签本身)
220
+ * @param lines 源码按行分割后的字符串数组
221
+ * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
222
+ */
223
+
224
+ function getStyleStartAndEndRow(lines: string[]): number[][] {
225
+ const groups: number[][] = [];
226
+ let currentStart: number | null = null; // 记录当前 <style> 开始行号
227
+
228
+ lines.forEach((line, index) => {
229
+ if (styleStartReg.test(line)) {
230
+ // 遇到开始标签,记录下一行作为内容开始位置
231
+ currentStart = index + 1;
232
+ } else if (styleEndReg.test(line) && currentStart !== null) {
233
+ // 遇到结束标签且已有开始位置,则记录为一个有效块
234
+ // 如果开始行大于等于结束行(即<style></style>在同一行或相邻行),则不构成有效内容区间
235
+ if (currentStart <= index - 1) {
236
+ groups.push([currentStart, index - 1]); // 结束行为上一行(排除 </style>)
237
+ }
238
+ currentStart = null; // 重置等待下一个 <style>
239
+ }
240
+ });
241
+
242
+ return groups;
243
+ }
244
+
245
+ /**
246
+ * 获取代码中所有注释块的起止行号
247
+ * @param lines 源码按行分割后的字符串数组
248
+ * @returns 起止行号组成的二维数组,每个子数组为 [startRow, endRow]
249
+ */
250
+ function getNoteStartAndEndRow(lines: string[]): number[][] {
251
+ const groups: number[][] = [];
252
+ let currentStart: number | null = null; // 记录当前注释块开始行号
253
+ let inMultiLineComment = false; // 是否在多行注释 /* */ 中
254
+ let inHtmlComment = false; // 是否在HTML注释 <!-- --> 中
255
+
256
+ lines.forEach((line, index) => {
257
+ const trimLine = line.trim();
258
+
259
+ // 处理 /* */ 多行注释
260
+ if (!inMultiLineComment && !inHtmlComment && trimLine.startsWith('/*')) {
261
+ inMultiLineComment = true;
262
+ currentStart = index;
263
+
264
+ // 检查是否在同一行内结束多行注释
265
+ if (
266
+ trimLine.includes('*/') &&
267
+ trimLine.indexOf('*/') > trimLine.indexOf('/*')
268
+ ) {
269
+ groups.push([currentStart, index]);
270
+ inMultiLineComment = false;
271
+ currentStart = null;
272
+ }
273
+ } else if (inMultiLineComment && trimLine.includes('*/')) {
274
+ // 结束多行注释
275
+ groups.push([currentStart!, index]);
276
+ inMultiLineComment = false;
277
+ currentStart = null;
278
+ }
279
+ // 处理 <!-- --> HTML注释
280
+ else if (
281
+ !inMultiLineComment &&
282
+ !inHtmlComment &&
283
+ trimLine.startsWith('<!--')
284
+ ) {
285
+ inHtmlComment = true;
286
+ currentStart = index;
287
+
288
+ // 检查是否在同一行内结束HTML注释
289
+ if (
290
+ trimLine.includes('-->') &&
291
+ trimLine.indexOf('-->') > trimLine.indexOf('<!--')
292
+ ) {
293
+ groups.push([currentStart, index]);
294
+ inHtmlComment = false;
295
+ currentStart = null;
296
+ }
297
+ } else if (inHtmlComment && trimLine.includes('-->')) {
298
+ // 结束HTML注释
299
+ groups.push([currentStart!, index]);
300
+ inHtmlComment = false;
301
+ currentStart = null;
302
+ }
303
+ // 处理单行注释 // 或 /** */
304
+ else if (
305
+ !inMultiLineComment &&
306
+ !inHtmlComment &&
307
+ trimLine.startsWith('//')
308
+ ) {
309
+ groups.push([index, index]);
310
+ }
311
+ });
312
+
313
+ return groups;
314
+ }
315
+
316
+ /**
317
+ * 获取代码中的 标签起始行,不包含script 中的内容,不包含style中的内容
318
+ * @param lines 源码按行分割后的字符串数组
319
+ * @returns 包含标签、脚本、样式和注释范围的对象
320
+ */
321
+ function getTagStartAndEndRow(lines: string[]): {
322
+ tagRanges: number[][];
323
+ scriptRanges: number[][];
324
+ styleRanges: number[][];
325
+ noteRanges: number[][];
326
+ } {
327
+ // 先找出 script 和 style 的内容区域
328
+ const scriptRanges = getScriptStartAndEndRow(lines);
329
+ const styleRanges = getStyleStartAndEndRow(lines);
330
+ const noteRanges = getNoteStartAndEndRow(lines);
331
+
332
+ // 合并并排序所有需要排除的区间
333
+ const excludeRanges = [...scriptRanges, ...styleRanges, ...noteRanges].sort(
334
+ (a, b) => a[0] - b[0],
335
+ );
336
+
337
+ const tagRanges: number[][] = [];
338
+
339
+ if (excludeRanges.length === 0) {
340
+ // 如果没有 script 或 style 块,则整个文件都是 html 区域
341
+ if (lines.length > 0) {
342
+ tagRanges.push([0, lines.length - 1]);
343
+ }
344
+ } else {
345
+ let currentStart = 0;
346
+
347
+ for (let i = 0; i < excludeRanges.length; i++) {
348
+ const [start, end] = excludeRanges[i];
349
+
350
+ // 检查当前起始点到这个排除区间的开始是否有内容
351
+ if (currentStart <= start - 1) {
352
+ tagRanges.push([currentStart, start - 1]);
353
+ }
354
+
355
+ // 更新下一个可能的起始点为排除区间之后
356
+ currentStart = end + 1;
357
+ }
358
+
359
+ // 检查最后一个排除区间结束后是否还有剩余内容
360
+ if (currentStart < lines.length) {
361
+ tagRanges.push([currentStart, lines.length - 1]);
362
+ }
363
+ }
364
+
365
+ return {
366
+ tagRanges,
367
+ scriptRanges,
368
+ styleRanges,
369
+ noteRanges,
370
+ };
371
+ }
372
+
373
+ /**
374
+ * 获取指定行号对应的代码块类型
375
+ *
376
+ * @param {number} index
377
+ * @param {{
378
+ * tagRanges: number[][];
379
+ * scriptRanges: number[][];
380
+ * styleRanges: number[][];
381
+ * }} {
382
+ * tagRanges,
383
+ * scriptRanges,
384
+ * styleRanges,
385
+ * }
386
+ * @return {*}
387
+ */
388
+ function getLineIndexType(
389
+ index: number,
390
+ {
391
+ tagRanges,
392
+ scriptRanges,
393
+ styleRanges,
394
+ noteRanges,
395
+ }: {
396
+ tagRanges: number[][];
397
+ scriptRanges: number[][];
398
+ styleRanges: number[][];
399
+ noteRanges: number[][];
400
+ },
401
+ ) {
402
+ // 检查是否在注释区间内
403
+ for (const [start, end] of noteRanges) {
404
+ if (index >= start && index <= end) {
405
+ return 'note';
406
+ }
407
+ }
408
+
409
+ // 检查是否在 script 区间内
410
+ for (const [start, end] of scriptRanges) {
411
+ if (index >= start && index <= end) {
412
+ return 'script';
413
+ }
414
+ }
415
+
416
+ // 检查是否在 style 区间内
417
+ for (const [start, end] of styleRanges) {
418
+ if (index >= start && index <= end) {
419
+ return 'style';
420
+ }
421
+ }
422
+
423
+ // 检查是否在 tag 区间内
424
+ for (const [start, end] of tagRanges) {
425
+ if (index >= start && index <= end) {
426
+ return 'tag';
427
+ }
428
+ }
429
+
430
+ return 'empty';
431
+ }
432
+
433
+ /**
434
+ * 获取代码中的单词信息
435
+ *
436
+ * TODO: 属性 , 方法
437
+ *
438
+ * @param {string[]} words
439
+ * @return {*} {Word[]}
440
+ */
441
+ function getWordsInfo(words: string[]): Word[] {
442
+ let quotation = '';
443
+ return words.map((w, index) => {
444
+ // 空格
445
+ const isSpace = w === ' ';
446
+ if (isSpace) {
447
+ return {
448
+ word: w,
449
+ type: 'space',
450
+ class: `${preClass}-space`,
451
+ };
452
+ }
453
+
454
+ // 箭头函数
455
+ const isArrow =
456
+ (w === '=' && words?.[index + 1] === '>') ||
457
+ (w === '>' && words?.[index - 1] === '=');
458
+ if (isArrow) {
459
+ return {
460
+ word: w,
461
+ type: 'keyword',
462
+ class: `${preClass}-keyword`,
463
+ };
464
+ }
465
+
466
+ // 括号
467
+ const isBracket = bracketList.includes(w);
468
+ if (isBracket) {
469
+ return {
470
+ word: w,
471
+ type: 'bracket',
472
+ class: `${preClass}-bracket`,
473
+ };
474
+ }
475
+
476
+ // 引号
477
+ const isQuotation = quotationList.includes(w);
478
+ if (isQuotation) {
479
+ quotation = quotation !== '' && quotation === w ? '' : w;
480
+ return {
481
+ word: w,
482
+ type: 'quotation',
483
+ class: `${preClass}-quotation`,
484
+ };
485
+ }
486
+
487
+ if (quotation != '') {
488
+ return {
489
+ word: w,
490
+ type: 'string',
491
+ class: `${preClass}-string`,
492
+ };
493
+ }
494
+
495
+ // 字符串
496
+ const isStr = quotationList.includes(words?.[index - 1]);
497
+ if (isStr) {
498
+ return {
499
+ word: w,
500
+ type: 'string',
501
+ class: `${preClass}-string`,
502
+ };
503
+ }
504
+
505
+ // 函数
506
+ const isFunction =
507
+ (words?.[index - 1] === ' ' && words?.[index - 2] === 'function') ||
508
+ words?.[index + 1] === '(';
509
+ if (isFunction) {
510
+ return {
511
+ word: w,
512
+ type: 'function',
513
+ class: `${preClass}-function`,
514
+ };
515
+ }
516
+
517
+ const preW = symbolList.includes(words?.[index - 1]);
518
+ const isAttribute = words?.[index + 1] === '.' && !preW;
519
+ if (isAttribute) {
520
+ return {
521
+ word: w,
522
+ type: 'attribute',
523
+ class: `${preClass}-attribute`,
524
+ };
525
+ }
526
+
527
+ // 操作符
528
+ const isOperator = operatorList.includes(w);
529
+ if (isOperator) {
530
+ return {
531
+ word: w,
532
+ type: 'operator',
533
+ class: `${preClass}-operator`,
534
+ };
535
+ }
536
+
537
+ // 符号
538
+ const isSymbol = symbolList.includes(w);
539
+ if (isSymbol) {
540
+ return {
541
+ word: w,
542
+ type: 'symbol',
543
+ class: `${preClass}-symbol`,
544
+ };
545
+ }
546
+
547
+ // 关键字
548
+ const isKeyword =
549
+ keywordList.includes(w) &&
550
+ (words?.[index + 1] === ' ' || words?.[index + 1] === ';');
551
+ if (isKeyword) {
552
+ return {
553
+ word: w,
554
+ type: 'keyword',
555
+ class: `${preClass}-keyword`,
556
+ };
557
+ }
558
+
559
+ // 其他
560
+ return {
561
+ word: w,
562
+ type: 'other',
563
+ class: `${preClass}-other`,
564
+ };
565
+ });
566
+ }
567
+
568
+ /**
569
+ * 按符号列表分割字符串
570
+ * @param lineTrim 需要分割的字符串
571
+ * @param symbols 符号列表
572
+ * @returns 分割后的词元数组
573
+ /**
574
+ *
575
+ *
576
+ * @param {string} lineTrim
577
+ * @param {string[]} symbols
578
+ * @return {*} {string[]}
579
+ */
580
+ function splitBySymbols(lineTrim: string, symbols: string[]): string[] {
581
+ if (!lineTrim) return [];
582
+
583
+ // 跳过注释行的处理
584
+ if (
585
+ lineTrim.startsWith('/*') ||
586
+ lineTrim.startsWith('*') ||
587
+ lineTrim.startsWith('//') ||
588
+ lineTrim.startsWith('<!--') ||
589
+ lineTrim.startsWith('-->') ||
590
+ lineTrim.length > 5000
591
+ ) {
592
+ return [lineTrim];
593
+ }
594
+
595
+ // 转义特殊字符并构建正则表达式
596
+ const escapedSymbols = symbols.map((s) =>
597
+ s.replace(/[.*+?^${}()|[\]\\@\$]/g, '\\$&'),
598
+ );
599
+ const regexPattern = `(${escapedSymbols.join('|')})`;
600
+ const regex = new RegExp(regexPattern, 'g');
601
+
602
+ // 使用正则表达式分割字符串并保留分隔符
603
+ const parts = lineTrim.split(regex);
604
+
605
+ // 过滤掉空字符串并返回结果
606
+ return parts.filter((part) => part !== '');
607
+ }
608
+
609
+ export function parseCode(code: string): LineInfo[] {
610
+ // 按行进行重新解析
611
+ const lines = code.split('\n');
612
+
613
+ const { tagRanges, scriptRanges, styleRanges, noteRanges } =
614
+ getTagStartAndEndRow(lines);
615
+
616
+ const parseLine = lines.map((line, index) => {
617
+ // 解析行之前的空格
618
+ const space =
619
+ line?.length < 500
620
+ ? (line.match(startSpaceReg)?.[1] || '')?.split('')
621
+ : [];
622
+ // 获取行类型
623
+
624
+ const type = getLineIndexType(index, {
625
+ tagRanges,
626
+ scriptRanges,
627
+ styleRanges,
628
+ noteRanges,
629
+ });
630
+
631
+ const lineTrim = line.trim();
632
+
633
+ // 按符号分割行内容
634
+ const segments = splitBySymbols(lineTrim, symbolList);
635
+
636
+ const words = getWordsInfo([...space, ...segments]);
637
+
638
+ const lineInfo: LineInfo = { type, words, class: `${preClass}-${type}` };
639
+ return lineInfo;
640
+ });
641
+
642
+ return parseLine;
643
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-uni-dev-tool",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "description": "vite-uni-dev-tool, debug, uni-app, 一处编写,到处调试",
5
5
  "keywords": [
6
6
  "vite",