@we-debug/miniprogram 0.41.0 → 0.42.0

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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [0.42.0](https://github.com/dlhandsome/we-debug/compare/v0.41.0...v0.42.0) (2025-03-09)
7
+
8
+
9
+ ### Features
10
+
11
+ * **miniprogram:** support jsonview ([09cf21c](https://github.com/dlhandsome/we-debug/commit/09cf21cf6d65af71c5bb823fc2ebe63e3c1c1d76))
12
+
13
+
14
+
15
+
16
+
6
17
  # [0.41.0](https://github.com/dlhandsome/we-debug/compare/v0.40.2...v0.41.0) (2025-03-09)
7
18
 
8
19
  **Note:** Version bump only for package @we-debug/miniprogram
@@ -60,6 +60,7 @@ Component({
60
60
  data: {
61
61
  sys: store.sys.get(),
62
62
  rules: store.rules.get(),
63
+ jsonviews: store.jsonviews.get(),
63
64
  badges: store.badges.get(),
64
65
  modal: {
65
66
  logo: LOGO_BASE64,
@@ -78,12 +79,22 @@ Component({
78
79
  setGroups() {
79
80
  this.setData({ 'group.keys': setGroup() });
80
81
  },
81
- setRules() {
82
- const rules = !store.group.get(currentGroupCache.get() || DEFAULT_GROUP.ALL)
82
+ getGroupStores() {
83
+ return !store.group.get(currentGroupCache.get() || DEFAULT_GROUP.ALL)
83
84
  ? store.group.get(DEFAULT_GROUP.ALL).get()
84
85
  : store.group.get(currentGroupCache.get() || DEFAULT_GROUP.ALL).get();
85
-
86
- this.setData({ rules });
86
+ },
87
+ setJsonViews() {
88
+ const groupStores = this.getGroupStores();
89
+ this.setData({
90
+ jsonviews: groupStores.filter(i => i.isJsonView)
91
+ });
92
+ },
93
+ setRules() {
94
+ const groupStores = this.getGroupStores();
95
+ this.setData({
96
+ rules: groupStores.filter(i => !i.isJsonView)
97
+ });
87
98
  },
88
99
  setBadges() {
89
100
  this.setData({ badges: store.badges.get() });
@@ -119,22 +130,23 @@ Component({
119
130
  },
120
131
  groupTapHandler(e) {
121
132
  const group = e.currentTarget.dataset.item;
122
- const rules = store.group.get(group).get();
133
+ // 设置当前分组 key 缓存
134
+ currentGroupCache.set(group);
123
135
 
124
136
  this.setData({
125
- 'group.actived': group,
126
- rules
137
+ 'group.actived': group
127
138
  });
128
- // 设置当前分组 key 缓存
129
- currentGroupCache.set(group);
139
+ this.setRules();
130
140
  },
131
141
  addListeners() {
132
142
  store.event.on('rule:update', this.setRules.bind(this));
143
+ store.event.on('jsonview:update', this.setJsonViews.bind(this));
133
144
  store.event.on('badge:update', this.setBadges.bind(this));
134
145
  store.event.on('group:update', this.setGroups.bind(this));
135
146
  },
136
147
  removeListeners() {
137
148
  store.event.off('rule:update', this.setRules.bind(this));
149
+ store.event.on('jsonview:update', this.setJsonViews.bind(this));
138
150
  store.event.off('badge:update', this.setBadges.bind(this));
139
151
  store.event.off('group:update', this.setGroups.bind(this));
140
152
  }
@@ -11,6 +11,7 @@
11
11
  "w-cell": "../cell/index",
12
12
  "w-rule": "../rule/index",
13
13
  "w-badge": "../badge/index",
14
+ "w-json-view": "../json-view/index",
14
15
  "w-ui-check": "@we-debug/plugin-ui-check/index"
15
16
  }
16
17
  }
@@ -5,22 +5,30 @@
5
5
  <view class="w-mask__modal-header">
6
6
  <image class="w-mask__modal-logo" src="{{modal.logo}}" mode="aspectFit" lazy-load="false" binderror="" bindload=""></image>
7
7
  </view>
8
- <view class="w-mask__modal-search">
9
- <input value="{{searchStr}}" placeholder="搜索" catchinput="searchInputHandler"/>
10
- <image class="search-icon--search" />
11
- <image class="search-icon--close" src="../icon/close.svg" wx:if="{{searchStr}}" catchtap="searchCleanHandler"/>
12
- </view>
13
- <scroll-view class="w-mask__modal-content" scroll-y="true" style="height:{{modal.scrollHeight}}rpx;">
14
- <w-cell wx:if="{{rules.length > 0}}">
15
- <w-rule
16
- wx:for="{{rules}}"
17
- wx:key="id"
18
- config="{{item}}"
19
- />
20
- </w-cell>
21
- <view wx:else class="w-mask__modal-content--empty">暂无搜索内容</view>
22
- <slot name="modal-body"></slot>
23
- </scroll-view>
8
+
9
+ <block wx:if="{{jsonviews.length > 0}}">
10
+ <view style="height:{{modal.scrollHeight}}rpx;">
11
+ <json-viewer json-data="{{jsonviews[0].data}}" editable="{{jsonviews[0].editable}}" expand-level="{{jsonviews[0].expandLevel}}" bind:change="handleJsonViewChange"/>
12
+ </view>
13
+ </block>
14
+ <block wx:else>
15
+ <view class="w-mask__modal-search">
16
+ <input value="{{searchStr}}" placeholder="搜索" catchinput="searchInputHandler"/>
17
+ <image class="search-icon--search" />
18
+ <image class="search-icon--close" src="../icon/close.svg" wx:if="{{searchStr}}" catchtap="searchCleanHandler"/>
19
+ </view>
20
+ <scroll-view class="w-mask__modal-content" scroll-y="true" style="height:{{modal.scrollHeight}}rpx;">
21
+ <w-cell wx:if="{{rules.length > 0}}">
22
+ <w-rule
23
+ wx:for="{{rules}}"
24
+ wx:key="id"
25
+ config="{{item}}"
26
+ />
27
+ </w-cell>
28
+ <view wx:else class="w-mask__modal-content--empty">暂无搜索内容</view>
29
+ <slot name="modal-body"></slot>
30
+ </scroll-view>
31
+ </block>
24
32
  <scroll-view class="w-mask__modal-group" scroll-x="true">
25
33
  <view class="group-item {{item === group.actived ? 'actived': ''}}" style=""
26
34
  wx:for="{{group.keys}}"
@@ -0,0 +1,686 @@
1
+ Component({
2
+ properties: {
3
+ jsonData: {
4
+ type: null,
5
+ value: null,
6
+ observer: function (newVal) {
7
+ if (newVal) {
8
+ const formatted = this.formatJson(newVal);
9
+ this.setData({ formattedData: formatted });
10
+ if (this.data.expandLevel >= 0) {
11
+ this.updateExpandLevel(this.data.expandLevel);
12
+ }
13
+ }
14
+ }
15
+ },
16
+ editable: {
17
+ type: Boolean,
18
+ value: false
19
+ },
20
+ expandLevel: {
21
+ type: Number,
22
+ value: -1, // -1 表示全部展开,0 表示全部折叠,正数表示展开的层级数
23
+ observer: function (newVal) {
24
+ if (this.data.formattedData.length > 0) {
25
+ this.updateExpandLevel(newVal);
26
+ }
27
+ }
28
+ }
29
+ },
30
+
31
+ data: {
32
+ formattedData: [],
33
+ editingIndex: -1,
34
+ editValue: '',
35
+ editKey: '',
36
+ editType: 0,
37
+ typeOptions: ['string', 'number', 'boolean', 'null'],
38
+ scrollLeft: 0,
39
+ touchStartX: 0,
40
+ lastScrollLeft: 0,
41
+ isDragging: false
42
+ },
43
+
44
+ methods: {
45
+ updateExpandLevel(level) {
46
+ const { formattedData } = this.data;
47
+ const stack = [];
48
+
49
+ // 首先重置所有项的状态
50
+ formattedData.forEach(item => {
51
+ item.hidden = false;
52
+ if (item.isOpening) {
53
+ item.collapsed = level >= 0 && item.level >= level;
54
+ }
55
+ });
56
+
57
+ // 处理隐藏状态
58
+ for (let i = 0; i < formattedData.length; i++) {
59
+ const item = formattedData[i];
60
+
61
+ // 处理开始括号
62
+ if (item.isOpening) {
63
+ if (item.collapsed) {
64
+ stack.push({ level: item.level, index: i });
65
+ }
66
+ // 如果超出展开层级,隐藏该项
67
+ if (level >= 0 && item.level > level) {
68
+ item.hidden = true;
69
+ }
70
+ continue;
71
+ }
72
+
73
+ // 处理结束括号
74
+ if (item.isClosing) {
75
+ if (stack.length > 0 && item.level === stack[stack.length - 1].level) {
76
+ stack.pop();
77
+ }
78
+ // 如果超出展开层级,隐藏该项
79
+ if (level >= 0 && item.level > level) {
80
+ item.hidden = true;
81
+ }
82
+ continue;
83
+ }
84
+
85
+ // 处理普通值
86
+ if (!item.isOpening && !item.isClosing) {
87
+ // 如果在任何折叠块内,隐藏该项
88
+ if (stack.length > 0) {
89
+ item.hidden = true;
90
+ continue;
91
+ }
92
+
93
+ // 如果超出展开层级,隐藏该项
94
+ if (level >= 0 && item.level > level) {
95
+ item.hidden = true;
96
+ }
97
+ }
98
+ }
99
+
100
+ this.setData({ formattedData });
101
+ },
102
+
103
+ handleTouchStart(e) {
104
+ this.setData({
105
+ touchStartX: e.touches[0].clientX,
106
+ lastScrollLeft: this.data.scrollLeft,
107
+ isDragging: true
108
+ });
109
+ },
110
+
111
+ handleTouchMove(e) {
112
+ if (!this.data.isDragging) return;
113
+
114
+ const deltaX = this.data.touchStartX - e.touches[0].clientX;
115
+ const newScrollLeft = this.data.lastScrollLeft + deltaX;
116
+
117
+ this.setData({
118
+ scrollLeft: Math.max(0, newScrollLeft)
119
+ });
120
+ },
121
+
122
+ handleTouchEnd() {
123
+ this.setData({
124
+ isDragging: false
125
+ });
126
+ },
127
+
128
+ formatJson(data, level = 0, path = []) {
129
+ const result = [];
130
+
131
+ if (typeof data !== 'object' || data === null) {
132
+ return result;
133
+ }
134
+
135
+ const isArray = Array.isArray(data);
136
+ const entries = isArray ? data.map((val, idx) => [idx, val]) : Object.entries(data);
137
+
138
+ // 添加开始的括号
139
+ result.push({
140
+ key: null,
141
+ value: null,
142
+ level,
143
+ isArray,
144
+ isOpening: true,
145
+ isClosing: false,
146
+ hasChildren: entries.length > 0,
147
+ path: [...path],
148
+ collapsed: false
149
+ });
150
+
151
+ entries.forEach(([key, value], index) => {
152
+ const valueType = typeof value;
153
+ const valueIsObject = valueType === 'object' && value !== null;
154
+ const currentPath = [...path, key];
155
+
156
+ if (!valueIsObject) {
157
+ // 处理基本类型值
158
+ result.push({
159
+ key: isArray ? null : key,
160
+ value: String(value),
161
+ rawValue: value,
162
+ type: valueType,
163
+ level: level + 1,
164
+ isLast: index === entries.length - 1,
165
+ isArray: false,
166
+ isOpening: false,
167
+ isClosing: false,
168
+ hasChildren: false,
169
+ path: currentPath,
170
+ hidden: false
171
+ });
172
+ } else {
173
+ // 递归处理对象或数组
174
+ const children = this.formatJson(value, level + 1, currentPath);
175
+ if (children.length > 0) {
176
+ // 如果是对象的属性,添加键名
177
+ if (!isArray) {
178
+ children[0].key = key;
179
+ }
180
+ result.push(...children);
181
+ }
182
+ }
183
+ });
184
+
185
+ // 添加结束的括号
186
+ result.push({
187
+ key: null,
188
+ value: null,
189
+ level,
190
+ isArray,
191
+ isOpening: false,
192
+ isClosing: true,
193
+ isLast: true,
194
+ path: [...path],
195
+ hidden: false
196
+ });
197
+
198
+ return result;
199
+ },
200
+
201
+ toggleCollapse(e) {
202
+ const { index } = e.currentTarget.dataset;
203
+ const { formattedData } = this.data;
204
+ const currentItem = formattedData[index];
205
+
206
+ if (!currentItem.hasChildren) return;
207
+
208
+ // 切换当前项的折叠状态
209
+ currentItem.collapsed = !currentItem.collapsed;
210
+
211
+ let level = currentItem.level;
212
+ let i = index + 1;
213
+ let bracketCount = 1;
214
+
215
+ // 保存子对象的状态
216
+ const childStates = new Map();
217
+ if (!currentItem.collapsed) {
218
+ // 展开时,先保存所有子对象的状态
219
+ while (i < formattedData.length && bracketCount > 0) {
220
+ const item = formattedData[i];
221
+ if (item.level === level) {
222
+ if (item.isOpening) bracketCount++;
223
+ if (item.isClosing) bracketCount--;
224
+ }
225
+ if (item.isOpening) {
226
+ childStates.set(i, {
227
+ collapsed: true, // 默认设置子对象为折叠状态
228
+ hidden: item.hidden
229
+ });
230
+ }
231
+ i++;
232
+ }
233
+ }
234
+
235
+ // 重置计数器和索引
236
+ i = index + 1;
237
+ bracketCount = 1;
238
+
239
+ while (i < formattedData.length && bracketCount > 0) {
240
+ const item = formattedData[i];
241
+
242
+ // 更新括号计数
243
+ if (item.level === level) {
244
+ if (item.isOpening) bracketCount++;
245
+ if (item.isClosing) bracketCount--;
246
+ }
247
+
248
+ if (currentItem.collapsed) {
249
+ // 折叠时:隐藏所有子项
250
+ if (bracketCount > 0) {
251
+ item.hidden = true;
252
+ }
253
+ } else {
254
+ // 展开时的处理逻辑
255
+ if (item.level === level + 1) {
256
+ // 显示直接子级
257
+ item.hidden = false;
258
+ // 如果是对象或数组的开始,设置为折叠状态
259
+ if (item.isOpening) {
260
+ item.collapsed = true;
261
+ }
262
+ } else if (item.level > level + 1) {
263
+ // 更深层级的项保持隐藏
264
+ item.hidden = true;
265
+ }
266
+
267
+ // 如果是子对象的开始括号,恢复其状态
268
+ if (item.isOpening) {
269
+ const savedState = childStates.get(i);
270
+ if (savedState) {
271
+ item.collapsed = savedState.collapsed;
272
+ // 如果子对象是折叠的,确保其结束括号可见
273
+ if (item.collapsed) {
274
+ let subLevel = item.level;
275
+ let j = i + 1;
276
+ let subBracketCount = 1;
277
+
278
+ while (j < formattedData.length && subBracketCount > 0) {
279
+ const subItem = formattedData[j];
280
+ if (subItem.level === subLevel) {
281
+ if (subItem.isOpening) subBracketCount++;
282
+ if (subItem.isClosing) {
283
+ subBracketCount--;
284
+ // 确保结束括号可见
285
+ if (subBracketCount === 0) {
286
+ subItem.hidden = false;
287
+ }
288
+ }
289
+ }
290
+ // 折叠块内的其他项都隐藏
291
+ if (subBracketCount > 0) {
292
+ subItem.hidden = true;
293
+ }
294
+ j++;
295
+ }
296
+ }
297
+ }
298
+ }
299
+ }
300
+
301
+ i++;
302
+ }
303
+
304
+ // 确保当前对象的结束括号可见
305
+ if (i < formattedData.length) {
306
+ formattedData[i - 1].hidden = false;
307
+ }
308
+
309
+ this.setData({ formattedData });
310
+ },
311
+
312
+ startEdit(e) {
313
+ if (!this.data.editable) return;
314
+
315
+ const { index } = e.currentTarget.dataset;
316
+ const item = this.data.formattedData[index];
317
+
318
+ if (item.isOpening || item.isClosing) return;
319
+
320
+ // 设置编辑类型
321
+ let editType = 0; // 默认为字符串类型
322
+ if (item.type === 'number') editType = 1;
323
+ else if (item.type === 'boolean') editType = 2;
324
+ else if (item.value === 'null') editType = 3;
325
+
326
+ this.setData({
327
+ editingIndex: index,
328
+ editValue: item.rawValue !== undefined ? String(item.rawValue) : item.value,
329
+ editKey: item.key || '',
330
+ editType
331
+ });
332
+ },
333
+
334
+ handleTypeChange(e) {
335
+ const type = parseInt(e.detail.value);
336
+ let value = this.data.editValue;
337
+
338
+ // 根据选择的类型转换值的显示
339
+ switch (type) {
340
+ case 0: // string
341
+ // 如果之前是布尔值,保持 "true" 或 "false"
342
+ if (this.data.editType === 2) {
343
+ value = value.toLowerCase();
344
+ } else {
345
+ value = String(value);
346
+ }
347
+ break;
348
+ case 1: // number
349
+ value = isNaN(Number(value)) ? '0' : String(Number(value));
350
+ break;
351
+ case 2: // boolean
352
+ value = value === 'true' || value === '1' || value === 'yes' ? 'true' : 'false';
353
+ break;
354
+ case 3: // null
355
+ value = 'null';
356
+ break;
357
+ }
358
+
359
+ this.setData({
360
+ editType: type,
361
+ editValue: value
362
+ });
363
+ },
364
+
365
+ handleKeyEdit(e) {
366
+ this.setData({
367
+ editKey: e.detail.value
368
+ });
369
+ },
370
+
371
+ handleValueEdit(e) {
372
+ this.setData({
373
+ editValue: e.detail.value
374
+ });
375
+ },
376
+
377
+ getFirstLevelPath(path) {
378
+ return path[0];
379
+ },
380
+
381
+ collectChangedData(data, changedPaths) {
382
+ const changes = {};
383
+ const firstLevelChanges = new Set();
384
+
385
+ // 收集所有变更的一级字段
386
+ changedPaths.forEach(path => {
387
+ const firstLevel = this.getFirstLevelPath(path);
388
+ if (firstLevel !== undefined) {
389
+ firstLevelChanges.add(firstLevel);
390
+ }
391
+ });
392
+
393
+ // 获取变更字段的完整数据
394
+ firstLevelChanges.forEach(field => {
395
+ changes[field] = data[field];
396
+ });
397
+
398
+ return Object.entries(changes).map(([key, value]) => ({
399
+ field: key,
400
+ value: value
401
+ }));
402
+ },
403
+
404
+ // 添加新方法:保存折叠状态
405
+ saveCollapseState(formattedData) {
406
+ const collapseState = new Map();
407
+
408
+ formattedData.forEach((item, index) => {
409
+ // 为每个项保存完整状态
410
+ const pathKey = item.path.join('.');
411
+ collapseState.set(pathKey + '_' + index, {
412
+ collapsed: item.collapsed,
413
+ hidden: item.hidden,
414
+ level: item.level,
415
+ isOpening: item.isOpening,
416
+ isClosing: item.isClosing
417
+ });
418
+ });
419
+
420
+ return collapseState;
421
+ },
422
+
423
+ // 添加新方法:恢复折叠状态
424
+ restoreCollapseState(formattedData, collapseState) {
425
+ formattedData.forEach((item, index) => {
426
+ const pathKey = item.path.join('.');
427
+ const savedState = collapseState.get(pathKey + '_' + index);
428
+
429
+ if (savedState) {
430
+ item.collapsed = savedState.collapsed;
431
+ item.hidden = savedState.hidden;
432
+ }
433
+ });
434
+
435
+ return formattedData;
436
+ },
437
+
438
+ saveEdit() {
439
+ const { editingIndex, editValue, editKey, editType, formattedData } = this.data;
440
+ if (editingIndex === -1) return;
441
+
442
+ const item = formattedData[editingIndex];
443
+ let newValue = editValue;
444
+
445
+ // 保存当前的折叠状态
446
+ const collapseState = this.saveCollapseState(formattedData);
447
+
448
+ // 根据选择的类型转换值
449
+ switch (editType) {
450
+ case 1: // number
451
+ newValue = Number(editValue);
452
+ break;
453
+ case 2: // boolean
454
+ newValue = editValue.toLowerCase() === 'true';
455
+ break;
456
+ case 3: // null
457
+ newValue = null;
458
+ break;
459
+ default: // string
460
+ newValue = editValue;
461
+ }
462
+
463
+ // 更新数据
464
+ const updatedData = this.updateJsonByPath(this.data.jsonData, item.path, newValue, editKey);
465
+
466
+ // 收集变更数据
467
+ const changedData = this.collectChangedData(updatedData, [item.path]);
468
+
469
+ // 格式化新数据并恢复折叠状态
470
+ const newFormattedData = this.formatJson(updatedData);
471
+ const restoredData = this.restoreCollapseState(newFormattedData, collapseState);
472
+
473
+ // 通知父组件数据已更新
474
+ this.triggerEvent('change', {
475
+ value: updatedData,
476
+ changes: changedData
477
+ });
478
+
479
+ // 更新显示
480
+ this.setData({
481
+ editingIndex: -1,
482
+ formattedData: restoredData
483
+ });
484
+ },
485
+
486
+ cancelEdit() {
487
+ this.setData({
488
+ editingIndex: -1
489
+ });
490
+ },
491
+
492
+ updateJsonByPath(data, path, value, newKey) {
493
+ const result = JSON.parse(JSON.stringify(data));
494
+ let current = result;
495
+
496
+ for (let i = 0; i < path.length - 1; i++) {
497
+ current = current[path[i]];
498
+ }
499
+
500
+ const lastKey = path[path.length - 1];
501
+ if (newKey && newKey !== lastKey) {
502
+ // 如果键名改变了
503
+ delete current[lastKey];
504
+ current[newKey] = value;
505
+ } else {
506
+ current[lastKey] = value;
507
+ }
508
+
509
+ return result;
510
+ },
511
+
512
+ addItem(e) {
513
+ const { index } = e.currentTarget.dataset;
514
+ const item = this.data.formattedData[index];
515
+
516
+ if (!item.isOpening) return;
517
+
518
+ // 保存当前的折叠状态,使用不包含索引的路径作为key
519
+ const collapseState = new Map();
520
+ this.data.formattedData.forEach(item => {
521
+ if (item.isOpening) {
522
+ const pathKey = item.path.join('.');
523
+ collapseState.set(pathKey, {
524
+ collapsed: item.collapsed,
525
+ hidden: item.hidden,
526
+ level: item.level
527
+ });
528
+ }
529
+ });
530
+
531
+ const newData = JSON.parse(JSON.stringify(this.data.jsonData));
532
+ let current = newData;
533
+
534
+ // 定位到当前对象
535
+ for (const key of item.path) {
536
+ current = current[key];
537
+ }
538
+
539
+ // 添加新项
540
+ const newKey = item.isArray ? current.length : 'newKey';
541
+ if (item.isArray) {
542
+ current.push(null);
543
+ } else {
544
+ current['newKey'] = null;
545
+ }
546
+
547
+ // 收集变更数据
548
+ const changedData = this.collectChangedData(newData, [item.path]);
549
+
550
+ // 格式化新数据
551
+ const newFormattedData = this.formatJson(newData);
552
+
553
+ // 恢复折叠状态
554
+ newFormattedData.forEach(newItem => {
555
+ if (newItem.isOpening) {
556
+ const pathKey = newItem.path.join('.');
557
+ const savedState = collapseState.get(pathKey);
558
+
559
+ if (savedState) {
560
+ // 恢复保存的状态
561
+ newItem.collapsed = savedState.collapsed;
562
+
563
+ // 如果是折叠状态,处理子项的隐藏状态
564
+ if (newItem.collapsed) {
565
+ let level = newItem.level;
566
+ let i = newFormattedData.indexOf(newItem) + 1;
567
+ let bracketCount = 1;
568
+
569
+ while (i < newFormattedData.length && bracketCount > 0) {
570
+ const child = newFormattedData[i];
571
+
572
+ // 更新括号计数
573
+ if (child.level === level) {
574
+ if (child.isOpening) bracketCount++;
575
+ if (child.isClosing) bracketCount--;
576
+ }
577
+
578
+ // 在折叠块内的项都要隐藏
579
+ if (bracketCount > 0) {
580
+ child.hidden = true;
581
+ }
582
+
583
+ i++;
584
+ }
585
+ }
586
+ }
587
+ }
588
+ });
589
+
590
+ // 确保新项的父节点是展开的,并且新项可见
591
+ const parentPath = item.path.join('.');
592
+ newFormattedData.forEach((newItem, idx) => {
593
+ if (newItem.isOpening && newItem.path.join('.') === parentPath) {
594
+ newItem.collapsed = false;
595
+
596
+ // 确保直接子项可见
597
+ let i = idx + 1;
598
+ let level = newItem.level;
599
+ let bracketCount = 1;
600
+
601
+ while (i < newFormattedData.length && bracketCount > 0) {
602
+ const child = newFormattedData[i];
603
+
604
+ // 更新括号计数
605
+ if (child.level === level) {
606
+ if (child.isOpening) bracketCount++;
607
+ if (child.isClosing) bracketCount--;
608
+ }
609
+
610
+ // 显示直接子级
611
+ if (child.level === level + 1) {
612
+ child.hidden = false;
613
+ }
614
+
615
+ i++;
616
+ }
617
+ }
618
+ });
619
+
620
+ // 找到新添加的项并添加高亮
621
+ const newItemPath = [...item.path, newKey];
622
+ newFormattedData.forEach(item => {
623
+ if (!item.isOpening && !item.isClosing && JSON.stringify(item.path) === JSON.stringify(newItemPath)) {
624
+ item.highlight = true;
625
+ // 3秒后取消高亮
626
+ setTimeout(() => {
627
+ item.highlight = false;
628
+ this.setData({ formattedData: this.data.formattedData });
629
+ }, 3000);
630
+ }
631
+ });
632
+
633
+ // 通知更新
634
+ this.triggerEvent('change', {
635
+ value: newData,
636
+ changes: changedData
637
+ });
638
+
639
+ this.setData({
640
+ formattedData: newFormattedData
641
+ });
642
+ },
643
+
644
+ deleteItem(e) {
645
+ const { index } = e.currentTarget.dataset;
646
+ const item = this.data.formattedData[index];
647
+
648
+ if (item.isOpening || item.isClosing) return;
649
+
650
+ // 保存当前的折叠状态
651
+ const collapseState = this.saveCollapseState(this.data.formattedData);
652
+
653
+ const newData = JSON.parse(JSON.stringify(this.data.jsonData));
654
+ let current = newData;
655
+
656
+ // 定位到父对象
657
+ for (let i = 0; i < item.path.length - 1; i++) {
658
+ current = current[item.path[i]];
659
+ }
660
+
661
+ const lastKey = item.path[item.path.length - 1];
662
+ if (Array.isArray(current)) {
663
+ current.splice(lastKey, 1);
664
+ } else {
665
+ delete current[lastKey];
666
+ }
667
+
668
+ // 收集变更数据
669
+ const changedData = this.collectChangedData(newData, [item.path]);
670
+
671
+ // 格式化新数据并恢复折叠状态
672
+ const newFormattedData = this.formatJson(newData);
673
+ const restoredData = this.restoreCollapseState(newFormattedData, collapseState);
674
+
675
+ // 通知更新
676
+ this.triggerEvent('change', {
677
+ value: newData,
678
+ changes: changedData
679
+ });
680
+
681
+ this.setData({
682
+ formattedData: restoredData
683
+ });
684
+ }
685
+ }
686
+ });
@@ -0,0 +1,4 @@
1
+ {
2
+ "component": true,
3
+ "usingComponents": {}
4
+ }
@@ -0,0 +1 @@
1
+ @import '../less/json-view.less';
@@ -0,0 +1,66 @@
1
+ <view class="json-viewer-container">
2
+ <scroll-view
3
+ class="json-viewer"
4
+ scroll-x="{{true}}"
5
+ scroll-left="{{scrollLeft}}"
6
+ bindtouchstart="handleTouchStart"
7
+ bindtouchmove="handleTouchMove"
8
+ bindtouchend="handleTouchEnd"
9
+ enhanced="{{true}}"
10
+ show-scrollbar="{{false}}">
11
+ <view class="json-content">
12
+ <block wx:for="{{formattedData}}" wx:key="index">
13
+ <view
14
+ class="json-item {{item.hidden ? 'hidden' : ''}} {{item.highlight ? 'highlight' : ''}}"
15
+ style="padding-left: {{item.level * 20}}px; transform: translateX(-{{scrollLeft}}px);"
16
+ >
17
+ <view class="content">
18
+ <!-- 编辑状态 -->
19
+ <block wx:if="{{editingIndex === index}}">
20
+ <view class="edit-group">
21
+ <block wx:if="{{!item.inArray && item.key !== null}}">
22
+ <input class="edit-input key"
23
+ value="{{editKey}}"
24
+ bind:input="handleKeyEdit" />
25
+ <text>:</text>
26
+ </block>
27
+ <picker class="type-picker"
28
+ bindchange="handleTypeChange"
29
+ value="{{editType}}"
30
+ range="{{typeOptions}}">
31
+ <view class="type-selector">{{typeOptions[editType]}}</view>
32
+ </picker>
33
+ <input class="edit-input value"
34
+ value="{{editValue}}"
35
+ bind:input="handleValueEdit" />
36
+ <view class="edit-actions">
37
+ <text class="action save" bind:tap="saveEdit">✓</text>
38
+ <text class="action cancel" bind:tap="cancelEdit">✗</text>
39
+ </view>
40
+ </view>
41
+ </block>
42
+
43
+ <!-- 显示状态 -->
44
+ <block wx:else>
45
+ <view class="item-content" bind:tap="{{editable && !item.isOpening && !item.isClosing ? 'startEdit' : 'toggleCollapse'}}" data-index="{{index}}">
46
+ <text wx:if="{{!item.inArray && item.key !== null}}" class="key">{{item.key}}: </text>
47
+ <block wx:if="{{item.isOpening}}">
48
+ <text class="bracket">{{item.isArray ? '[' : '{'}}{{item.collapsed ? '...' : ''}}</text>
49
+ <text wx:if="{{editable}}" class="action add" catchtap="addItem" data-index="{{index}}">+</text>
50
+ </block>
51
+ <block wx:elif="{{!item.isClosing}}">
52
+ <text class="value {{item.type}}">{{item.value}}</text>
53
+ <text wx:if="{{editable}}" class="action delete" catchtap="deleteItem" data-index="{{index}}">×</text>
54
+ </block>
55
+ <block wx:if="{{item.isClosing}}">
56
+ <text class="bracket">{{item.isArray ? ']' : '}'}}</text>
57
+ </block>
58
+ <text wx:if="{{!item.isLast && !item.isOpening}}">,</text>
59
+ </view>
60
+ </block>
61
+ </view>
62
+ </view>
63
+ </block>
64
+ </view>
65
+ </scroll-view>
66
+ </view>
@@ -0,0 +1,169 @@
1
+ .json-viewer-container {
2
+ position: relative;
3
+ width: 100%;
4
+ height: 100%;
5
+ overflow: hidden;
6
+ }
7
+ .json-viewer {
8
+ width: 100%;
9
+ height: 100%;
10
+ }
11
+ .json-content {
12
+ display: inline-block;
13
+ min-width: 100%;
14
+ padding: 10px;
15
+ font-family: monospace;
16
+ font-size: 14px;
17
+ line-height: 1.5;
18
+ }
19
+ .json-item {
20
+ white-space: nowrap;
21
+ }
22
+ .json-item.hidden {
23
+ display: none;
24
+ }
25
+ .content {
26
+ display: inline-block;
27
+ min-height: 24px;
28
+ line-height: 24px;
29
+ }
30
+ .item-content {
31
+ display: inline-block;
32
+ padding: 2px 4px;
33
+ border-radius: 4px;
34
+ }
35
+ .item-content:hover {
36
+ background-color: rgba(0, 0, 0, 0.05);
37
+ }
38
+ .key {
39
+ color: #881391;
40
+ }
41
+ .value {
42
+ color: #1A1AA6;
43
+ }
44
+ .value.string {
45
+ color: #C41A16;
46
+ }
47
+ .value.number {
48
+ color: #1C00CF;
49
+ }
50
+ .value.boolean {
51
+ color: #0000FF;
52
+ }
53
+ .bracket {
54
+ color: #000000;
55
+ cursor: pointer;
56
+ }
57
+ .bracket:hover {
58
+ opacity: 0.7;
59
+ }
60
+ .edit-group {
61
+ display: inline-flex;
62
+ align-items: center;
63
+ gap: 4px;
64
+ background: #f8f9fa;
65
+ padding: 2px 4px;
66
+ border-radius: 4px;
67
+ border: 1px solid #e9ecef;
68
+ }
69
+ .edit-input {
70
+ height: 28px;
71
+ line-height: 28px;
72
+ padding: 0 8px;
73
+ border: 1px solid #ced4da;
74
+ border-radius: 4px;
75
+ font-family: monospace;
76
+ font-size: 14px;
77
+ background: #fff;
78
+ transition: all 0.2s;
79
+ }
80
+ .edit-input:focus {
81
+ border-color: #80bdff;
82
+ box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
83
+ outline: none;
84
+ }
85
+ .edit-input.key {
86
+ color: #881391;
87
+ width: 120px;
88
+ }
89
+ .edit-input.value {
90
+ color: #1A1AA6;
91
+ width: 200px;
92
+ }
93
+ .type-picker {
94
+ display: inline-block;
95
+ vertical-align: middle;
96
+ }
97
+ .type-selector {
98
+ height: 28px;
99
+ line-height: 28px;
100
+ padding: 0 8px;
101
+ border: 1px solid #ced4da;
102
+ border-radius: 4px;
103
+ font-size: 13px;
104
+ color: #495057;
105
+ background-color: #fff;
106
+ min-width: 80px;
107
+ text-align: center;
108
+ transition: all 0.2s;
109
+ }
110
+ .type-selector:hover {
111
+ border-color: #80bdff;
112
+ cursor: pointer;
113
+ }
114
+ .edit-actions {
115
+ display: inline-flex;
116
+ align-items: center;
117
+ gap: 4px;
118
+ margin-left: 8px;
119
+ }
120
+ .action {
121
+ display: inline-flex;
122
+ align-items: center;
123
+ justify-content: center;
124
+ width: 24px;
125
+ height: 24px;
126
+ border-radius: 4px;
127
+ cursor: pointer;
128
+ transition: all 0.2s;
129
+ }
130
+ .action.save {
131
+ color: #fff;
132
+ background-color: #28a745;
133
+ margin-right: 0;
134
+ }
135
+ .action.save:hover {
136
+ background-color: #218838;
137
+ }
138
+ .action.cancel {
139
+ color: #fff;
140
+ background-color: #dc3545;
141
+ }
142
+ .action.cancel:hover {
143
+ background-color: #c82333;
144
+ }
145
+ .action.add {
146
+ color: #28a745;
147
+ font-weight: bold;
148
+ }
149
+ .action.add:hover {
150
+ background-color: rgba(40, 167, 69, 0.1);
151
+ }
152
+ .action.delete {
153
+ color: #dc3545;
154
+ font-weight: bold;
155
+ }
156
+ .action.delete:hover {
157
+ background-color: rgba(220, 53, 69, 0.1);
158
+ }
159
+ @keyframes highlight {
160
+ 0% {
161
+ background-color: rgba(255, 255, 0, 0.5);
162
+ }
163
+ 100% {
164
+ background-color: transparent;
165
+ }
166
+ }
167
+ .json-item.highlight {
168
+ animation: highlight 3s ease-out;
169
+ }
@@ -0,0 +1,203 @@
1
+ .json-viewer-container {
2
+ position: relative;
3
+ width: 100%;
4
+ height: 100%;
5
+ overflow: hidden;
6
+ }
7
+
8
+ .json-viewer {
9
+ width: 100%;
10
+ height: 100%;
11
+ }
12
+
13
+ .json-content {
14
+ display: inline-block;
15
+ min-width: 100%;
16
+ padding: 10px;
17
+ font-family: monospace;
18
+ font-size: 14px;
19
+ line-height: 1.5;
20
+ }
21
+
22
+ .json-item {
23
+ white-space: nowrap;
24
+ }
25
+
26
+ .json-item.hidden {
27
+ display: none;
28
+ }
29
+
30
+ .content {
31
+ display: inline-block;
32
+ min-height: 24px;
33
+ line-height: 24px;
34
+ }
35
+
36
+ .item-content {
37
+ display: inline-block;
38
+ padding: 2px 4px;
39
+ border-radius: 4px;
40
+ }
41
+
42
+ .item-content:hover {
43
+ background-color: rgba(0, 0, 0, 0.05);
44
+ }
45
+
46
+ .key {
47
+ color: #881391;
48
+ }
49
+
50
+ .value {
51
+ color: #1A1AA6;
52
+ }
53
+
54
+ .value.string {
55
+ color: #C41A16;
56
+ }
57
+
58
+ .value.number {
59
+ color: #1C00CF;
60
+ }
61
+
62
+ .value.boolean {
63
+ color: #0000FF;
64
+ }
65
+
66
+ .bracket {
67
+ color: #000000;
68
+ cursor: pointer;
69
+ }
70
+
71
+ .bracket:hover {
72
+ opacity: 0.7;
73
+ }
74
+
75
+ .edit-group {
76
+ display: inline-flex;
77
+ align-items: center;
78
+ gap: 4px;
79
+ background: #f8f9fa;
80
+ padding: 2px 4px;
81
+ border-radius: 4px;
82
+ border: 1px solid #e9ecef;
83
+ }
84
+
85
+ .edit-input {
86
+ height: 28px;
87
+ line-height: 28px;
88
+ padding: 0 8px;
89
+ border: 1px solid #ced4da;
90
+ border-radius: 4px;
91
+ font-family: monospace;
92
+ font-size: 14px;
93
+ background: #fff;
94
+ transition: all 0.2s;
95
+ }
96
+
97
+ .edit-input:focus {
98
+ border-color: #80bdff;
99
+ box-shadow: 0 0 0 2px rgba(0,123,255,.25);
100
+ outline: none;
101
+ }
102
+
103
+ .edit-input.key {
104
+ color: #881391;
105
+ width: 120px;
106
+ }
107
+
108
+ .edit-input.value {
109
+ color: #1A1AA6;
110
+ width: 200px;
111
+ }
112
+
113
+ .type-picker {
114
+ display: inline-block;
115
+ vertical-align: middle;
116
+ }
117
+
118
+ .type-selector {
119
+ height: 28px;
120
+ line-height: 28px;
121
+ padding: 0 8px;
122
+ border: 1px solid #ced4da;
123
+ border-radius: 4px;
124
+ font-size: 13px;
125
+ color: #495057;
126
+ background-color: #fff;
127
+ min-width: 80px;
128
+ text-align: center;
129
+ transition: all 0.2s;
130
+ }
131
+
132
+ .type-selector:hover {
133
+ border-color: #80bdff;
134
+ cursor: pointer;
135
+ }
136
+
137
+ .edit-actions {
138
+ display: inline-flex;
139
+ align-items: center;
140
+ gap: 4px;
141
+ margin-left: 8px;
142
+ }
143
+
144
+ .action {
145
+ display: inline-flex;
146
+ align-items: center;
147
+ justify-content: center;
148
+ width: 24px;
149
+ height: 24px;
150
+ border-radius: 4px;
151
+ cursor: pointer;
152
+ transition: all 0.2s;
153
+ }
154
+
155
+ .action.save {
156
+ color: #fff;
157
+ background-color: #28a745;
158
+ margin-right: 0;
159
+ }
160
+
161
+ .action.save:hover {
162
+ background-color: #218838;
163
+ }
164
+
165
+ .action.cancel {
166
+ color: #fff;
167
+ background-color: #dc3545;
168
+ }
169
+
170
+ .action.cancel:hover {
171
+ background-color: #c82333;
172
+ }
173
+
174
+ .action.add {
175
+ color: #28a745;
176
+ font-weight: bold;
177
+ }
178
+
179
+ .action.add:hover {
180
+ background-color: rgba(40, 167, 69, 0.1);
181
+ }
182
+
183
+ .action.delete {
184
+ color: #dc3545;
185
+ font-weight: bold;
186
+ }
187
+
188
+ .action.delete:hover {
189
+ background-color: rgba(220, 53, 69, 0.1);
190
+ }
191
+
192
+ @keyframes highlight {
193
+ 0% {
194
+ background-color: rgba(255, 255, 0, 0.5);
195
+ }
196
+ 100% {
197
+ background-color: transparent;
198
+ }
199
+ }
200
+
201
+ .json-item.highlight {
202
+ animation: highlight 3s ease-out;
203
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@we-debug/miniprogram",
3
- "version": "0.41.0",
3
+ "version": "0.42.0",
4
4
  "description": "we-debug miniprogram component",
5
5
  "author": {
6
6
  "name": "dlhandsome"
@@ -26,10 +26,10 @@
26
26
  },
27
27
  "license": "MIT",
28
28
  "dependencies": {
29
- "@we-debug/core": "0.41.0"
29
+ "@we-debug/core": "0.42.0"
30
30
  },
31
31
  "readme": "ERROR: No README data found!",
32
32
  "_id": "@we-debug/core@0.3.5",
33
33
  "_commitid": "6c3f04b",
34
- "gitHead": "cd8770ba0a849a994e9f4e75e8622fb4972643f8"
34
+ "gitHead": "1bd39c3c4bb27512bb95da3c1468f70ef1f7c185"
35
35
  }