-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
show-text-modal-directive.js
128 lines (116 loc) · 4.42 KB
/
show-text-modal-directive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright 2024 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
directive_module.directive('showtextmodal', showTextModalDirective)
.filter('jsonBigIntFilter', function () {
return function (text) {
if (typeof(text) === "undefined" || typeof JSON.parse(text) !== "object"
|| !text) {
return;
}
const numberRegex = /"\|+\d+\|+"/g;
const splitRegex = /"\|+\d+\|+"/;
const splitArray = text.split(splitRegex);
const matchResult = text.match(numberRegex);
const borderRegex = /"\|\d+\|"/;
if (!matchResult || 0 === splitArray.length) {
return text;
} else {
Object.keys(matchResult).forEach(function (key) {
if (matchResult[key].includes('"\|')) {
if (borderRegex.test(matchResult[key])) {
matchResult[key] = matchResult[key].replaceAll('"\|', '');
matchResult[key] = matchResult[key].replaceAll('|\"', '');
} else {
matchResult[key] = matchResult[key].replaceAll('"\|', '"');
matchResult[key] = matchResult[key].replaceAll('|\"', '"');
}
}
});
}
let resultStr = '';
let index = 0;
const isJsonText = resultStr => {
try {
return typeof JSON.parse(resultStr) === "object";
} catch (e) {
return false;
}
};
Object.keys(splitArray).forEach(function (key) {
resultStr = resultStr.concat(splitArray[key]);
if (typeof(matchResult[index]) !== "undefined"
&& !isJsonText(resultStr)) {
resultStr = resultStr.concat(matchResult[index++])
}
})
return resultStr;
}
});
function showTextModalDirective(AppUtil) {
return {
restrict: 'E',
templateUrl: AppUtil.prefixPath() + '/views/component/show-text-modal.html',
transclude: true,
replace: true,
scope: {
text: '=',
oldStr: '=',
newStr: '=',
enableTextDiff: '='
},
link: function (scope) {
scope.$watch('text', init);
function init() {
scope.jsonObject = undefined;
if (isJsonText(scope.text)) {
scope.jsonObject = parseBigInt(scope.text);
}
}
function isJsonText(text) {
try {
return typeof JSON.parse(text) === "object";
} catch (e) {
return false;
}
}
function parseBigInt(str) {
if (/\d+/.test(str)) {
let replaceMap = [];
let n = 0;
str = str.replace(/"\|+\d+\|+"/g, function (match) {
return match.replace('"\|', '"\||').replace('|"', '||"');
})
.replace(/"(\\?[\s\S])*?"/g, function (match) {
if (/\d+/.test(match)) {
replaceMap.push(match);
return '"""';
}
return match;
}).replace(/[+\-\d.eE]+/g, function (match) {
if (/^\d+$/.test(match)) {
return '"|' + match + '|"';
}
return match;
}).replace(/"""/g, function () {
return replaceMap[n++];
})
}
return JSON.parse(str);
}
}
}
}