Skip to content

Commit e0c2861

Browse files
committed
Add collapse functionality for response based on line indentation Huachao#157
1 parent 12f1a88 commit e0c2861

8 files changed

Lines changed: 205 additions & 7 deletions

File tree

‎scripts/main.js‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// @ts-check
2+
'use strict';
3+
4+
(function () {
5+
6+
function onLoad() {
7+
const code = document.getElementsByTagName('code')[0];
8+
let [...childs] = code.childNodes;
9+
childs.filter(n => n.nodeType === 1).forEach(n => {
10+
n.addEventListener('click', toggleLines);
11+
});
12+
}
13+
14+
function toggleLines(e) {
15+
const iconSpan = e.target;
16+
const lineSpan = iconSpan.parentNode;
17+
const blockEndNum = getFoldingRangeEnd(lineSpan);
18+
const isExpandAction = lineSpan.classList.contains('collapsed');
19+
if (isExpandAction) {
20+
lineSpan.classList.remove('collapsed');
21+
} else {
22+
lineSpan.classList.add('collapsed');
23+
}
24+
25+
let span = lineSpan;
26+
let excludeEndLineNum = -1;
27+
while (span = span.nextElementSibling) {
28+
const currentLineNum = getLineNum(span);
29+
if (currentLineNum > blockEndNum) {
30+
break;
31+
}
32+
33+
if (currentLineNum <= excludeEndLineNum) {
34+
continue;
35+
}
36+
37+
if (isCollapsedStartLineSpan(span)) {
38+
excludeEndLineNum = getFoldingRangeEnd(span);
39+
}
40+
41+
if (isExpandAction) {
42+
span.classList.remove('hidden-line');
43+
span.nextSibling.textContent = '\n';
44+
} else {
45+
span.classList.add('hidden-line');
46+
span.nextSibling.textContent = '';
47+
}
48+
}
49+
}
50+
51+
function getLineNum(element) {
52+
return parseInt(element.attributes.getNamedItem('start').value);
53+
}
54+
55+
function getFoldingRangeEnd(element) {
56+
return parseInt(element.attributes.getNamedItem('range-end').value);
57+
}
58+
59+
function isCollapsedStartLineSpan(element) {
60+
return element.classList.contains('collapsed');
61+
}
62+
63+
// function isFoldingRangeStartLine(node) {
64+
65+
// }
66+
67+
// function isTextLine(line) {
68+
// return line.nodeType === 3;
69+
// }
70+
71+
if (document.readyState === 'loading') {
72+
document.addEventListener('DOMContentLoaded', onLoad);
73+
} else {
74+
onLoad();
75+
}
76+
})()

‎src/constants.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export const ExtensionId: string = 'humao.rest-client';
44
export const AiKey: string = 'ad746e27-4a36-441c-8b94-5db178f81ae3';
55
export const CSSFileName: string = 'rest-client.css';
66
export const CSSFolderName: string = 'styles';
7+
export const ScriptFileName: string = 'main.js';
8+
export const ScriptsFolderName: string = 'scripts';
79
export const ExtensionFolderName: string = '.rest-client';
810
export const HistoryFileName: string = 'history.json';
911
export const CookieFileName: string = 'cookie.json';

‎src/views/httpResponseTextDocumentContentProvider.ts‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const autoLinker = require('autolinker');
1515
const hljs = require('highlight.js');
1616

1717
export class HttpResponseTextDocumentContentProvider extends BaseTextDocumentContentProvider {
18-
private static cssFilePath: string = path.join(extensions.getExtension(Constants.ExtensionId).extensionPath, Constants.CSSFolderName, Constants.CSSFileName);
18+
private static cssFilePath: string = Uri.file(path.join(extensions.getExtension(Constants.ExtensionId).extensionPath, Constants.CSSFolderName, Constants.CSSFileName)).toString();
19+
private static scriptFilePath: string = Uri.file(path.join(extensions.getExtension(Constants.ExtensionId).extensionPath, Constants.ScriptsFolderName, Constants.ScriptFileName)).toString();
1920

2021
public constructor(public settings: RestClientSettings) {
2122
super();
@@ -40,7 +41,7 @@ export class HttpResponseTextDocumentContentProvider extends BaseTextDocumentCon
4041
}
4142
return `
4243
<head>
43-
<link rel="stylesheet" href="${HttpResponseTextDocumentContentProvider.cssFilePath}">
44+
<link rel="stylesheet" type="text/css" href="${HttpResponseTextDocumentContentProvider.cssFilePath}">
4445
${this.getSettingsOverrideStyles(width)}
4546
</head>
4647
<body>
@@ -50,6 +51,7 @@ export class HttpResponseTextDocumentContentProvider extends BaseTextDocumentCon
5051
: this.addUrlLinks(innerHtml)}
5152
<a id="scroll-to-top" role="button" aria-label="scroll to top" onclick="scroll(0,0)"><span class="icon"></span></a>
5253
</div>
54+
<script type="text/javascript" src="${HttpResponseTextDocumentContentProvider.scriptFilePath}" charset="UTF-8"></script>
5355
</body>`;
5456
}
5557
}
@@ -116,11 +118,11 @@ ${HttpResponseTextDocumentContentProvider.formatHeaders(response.headers)}`;
116118
this.settings.fontWeight ? `font-weight: ${this.settings.fontWeight};` : '',
117119
'}',
118120
'code .line {',
119-
`padding-left: calc(${width}ch + 18px );`,
121+
`padding-left: calc(${width}ch + 20px );`,
120122
'}',
121123
'code .line:before {',
122124
`width: ${width}ch;`,
123-
`margin-left: calc(-${width}ch + -27px );`,
125+
`margin-left: calc(-${width}ch + -30px );`,
124126
'}',
125127
'</style>'].join('\n');
126128
}
@@ -133,9 +135,16 @@ ${HttpResponseTextDocumentContentProvider.formatHeaders(response.headers)}`;
133135
code = code.split(/\r\n|\r|\n/);
134136
let max = (1 + code.length).toString().length;
135137

138+
const foldingRanges = this.getFoldingRange(code);
139+
136140
code = code
137141
.map(function (line, i) {
138-
return '<span class="line width-' + max + '" start="' + (1 + i) + '">' + line + '</span>';
142+
const lineNum = i + 1;
143+
const range = foldingRanges.has(lineNum)
144+
? ` range-start="${foldingRanges.get(lineNum).start}" range-end="${foldingRanges.get(lineNum).end}"`
145+
: '';
146+
const folding = foldingRanges.has(lineNum) ? '<span class="icon"></span>' : '';
147+
return `<span class="line width-${max}" start="${lineNum}"${range}>${line}${folding}</span>`;
139148
})
140149
.join('\n');
141150
return code;
@@ -178,6 +187,32 @@ ${HttpResponseTextDocumentContentProvider.formatHeaders(response.headers)}`;
178187
});
179188
}
180189

190+
private getFoldingRange(lines: string[]): Map<number, FoldingRange> {
191+
const result = new Map<number, FoldingRange>();
192+
const stack = [];
193+
194+
const leadingSpaceCount = lines
195+
.map((line, index) => [index, line.search(/\S/)])
196+
.filter(([, num]) => num !== -1);
197+
for (const [index, [lineIndex, count]] of leadingSpaceCount.entries()) {
198+
if (index === 0) {
199+
continue;
200+
}
201+
202+
const [prevLineIndex, prevCount] = leadingSpaceCount[index - 1];
203+
if (prevCount < count) {
204+
stack.push([prevLineIndex, prevCount]);
205+
} else if (prevCount > count) {
206+
let prev;
207+
while ((prev = stack.slice(-1)[0]) && (prev[1] >= count)) {
208+
stack.pop();
209+
result.set(prev[0] + 1, new FoldingRange(prev[0] + 1, lineIndex));
210+
}
211+
}
212+
}
213+
return result;
214+
}
215+
181216
private static formatHeaders(headers: { [key: string]: string }): string {
182217
let headerString = '';
183218
for (let header in headers) {
@@ -212,4 +247,9 @@ ${HttpResponseTextDocumentContentProvider.formatHeaders(response.headers)}`;
212247
return null;
213248
}
214249
}
250+
}
251+
252+
class FoldingRange {
253+
public constructor(public start: number, public end: number) {
254+
}
215255
}

‎styles/collapse-dark.svg‎

Lines changed: 6 additions & 0 deletions
Loading

‎styles/collapse.svg‎

Lines changed: 6 additions & 0 deletions
Loading

‎styles/expand-dark.svg‎

Lines changed: 4 additions & 0 deletions
Loading

‎styles/expand.svg‎

Lines changed: 4 additions & 0 deletions
Loading

‎styles/rest-client.css‎

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@charset "UTF-8";
2+
13
body.vscode-light {
24
background-color: inherit;
35
}
@@ -96,16 +98,74 @@ code {
9698
color: white;
9799
}
98100

101+
.line .icon {
102+
position: absolute;
103+
display: inline-block;
104+
left: calc(2ch + 3px);
105+
top: 0.15em;
106+
width: 1em;
107+
height: 1em;
108+
cursor: pointer;
109+
}
110+
111+
.vscode-light .line .icon {
112+
background: url(expand.svg);
113+
background-size: 1.2em;
114+
background-repeat: no-repeat;
115+
}
116+
117+
.vscode-dark .line .icon,
118+
.vscode-high-contrast .line .icon {
119+
background: url(expand-dark.svg);
120+
background-size: 1.2em;
121+
background-repeat: no-repeat;
122+
}
123+
124+
.line.collapsed .icon {
125+
position: absolute;
126+
display: inline-block;
127+
left: calc(2ch + 3px);
128+
top: 0.15em;
129+
width: 1em;
130+
height: 1em;
131+
cursor: pointer;
132+
}
133+
134+
.vscode-light .line.collapsed .icon {
135+
background: url(collapse.svg);
136+
background-size: 1.2em;
137+
background-repeat: no-repeat;
138+
}
139+
140+
.vscode-dark .line.collapsed .icon,
141+
.vscode-high-contrast .line.collapsed .icon {
142+
background: url(collapse-dark.svg);
143+
background-size: 1.2em;
144+
background-repeat: no-repeat;
145+
}
146+
99147
code .line {
100148
display: inline-block;
101149
position: relative;
102-
padding-left: calc(2ch + 18px );
150+
padding-left: calc(2ch + 20px );
151+
}
152+
153+
code .line.hidden-line {
154+
display: none;
103155
}
104156

105157
code .line:after {
106158
content: ' ';
107159
}
108160

161+
code .line.collapsed:after {
162+
color: grey;
163+
margin: 0.1em 0.2em 0 0.2em;
164+
content: "⋯";
165+
display: inline;
166+
line-height: 1.5;
167+
}
168+
109169
code .line:before {
110170
box-sizing: content-box;
111171
display: inline-block;
@@ -117,7 +177,7 @@ code .line:before {
117177
content: attr(start);
118178
padding-right: 9px;
119179
padding-left: 9px;
120-
margin-left: calc(-2ch + -27px );
180+
margin-left: calc(-2ch + -30px );
121181
margin-right: 9px;
122182
color: #787878;
123183
background-color: inherit;

0 commit comments

Comments
 (0)