-
Notifications
You must be signed in to change notification settings - Fork 50
/
webviewcontentassembler.ts
110 lines (98 loc) · 3.63 KB
/
webviewcontentassembler.ts
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
'use strict';
import { WebviewContent } from './webviewcontent';
import { Context } from './context';
import { WebviewExtension } from './extensions/webview_extension';
enum ModuleType {
Insert,
Replace
}
type InsertModule = {
Type: ModuleType.Insert,
Extension: WebviewExtension
};
type ReplaceModule = {
Type: ModuleType.Replace,
ReplaceContent: string,
Extension: WebviewExtension,
};
type WebviewModule = {
Module: InsertModule | ReplaceModule,
LineNumber: number,
};
export class WebviewContentAssembler {
private webviewContent: WebviewContent;
private webviewModules: WebviewModule[];
private webviewContentLineMappings: Map<string, number[]>;
constructor(context: Context) {
this.webviewContent = new WebviewContent(context.getResourceUri('webview_base.html').fsPath);
this.webviewModules = [];
this.webviewContentLineMappings = new Map<string, number[]>();
let lineNumber = 1;
for (let line of this.webviewContent.getLines()) {
line = line.trim();
const lines = this.webviewContentLineMappings.get(line);
if (lines !== undefined) {
lines.push(lineNumber);
}
else {
this.webviewContentLineMappings.set(line, [ lineNumber ]);
}
lineNumber++;
}
}
public addWebviewModule(extension: WebviewExtension, originalLine: string) {
const insertModule: InsertModule = {
Type: ModuleType.Insert,
Extension: extension
};
originalLine = originalLine.trim();
const lines = this.webviewContentLineMappings.get(originalLine);
if (lines !== undefined) {
for (const lineNumber of lines) {
const webviewModule: WebviewModule = {
Module: insertModule,
LineNumber: lineNumber
};
this.insertModule(webviewModule);
}
}
}
public addReplaceModule(extension: WebviewExtension, originalLine: string, replaceContent: string) {
const replaceModule: ReplaceModule = {
Type: ModuleType.Replace,
ReplaceContent: replaceContent,
Extension: extension
};
originalLine = originalLine.trim();
const lines = this.webviewContentLineMappings.get(originalLine);
if (lines !== undefined) {
for (const lineNumber of lines) {
const webviewModule: WebviewModule = {
Module: replaceModule,
LineNumber: lineNumber
};
this.insertModule(webviewModule);
}
}
}
private insertModule(webviewModule: WebviewModule) {
const sortedIndex = this.webviewModules.findIndex((module: WebviewModule) => module.LineNumber < webviewModule.LineNumber);
if (sortedIndex < 0) {
this.webviewModules.push(webviewModule);
}
else {
this.webviewModules.splice(sortedIndex, 0, webviewModule);
}
}
public assembleWebviewContent() {
for (const webviewModule of this.webviewModules) {
if (webviewModule.Module.Type === ModuleType.Insert) {
this.webviewContent.insertAfterLine(webviewModule.Module.Extension.generateContent(), webviewModule.LineNumber);
}
else {
this.webviewContent.replaceWithinLine(webviewModule.Module.ReplaceContent, webviewModule.Module.Extension.generateContent(), webviewModule.LineNumber);
}
}
return this.webviewContent.getContent();
}
}