-
Notifications
You must be signed in to change notification settings - Fork 10
/
extension.ts
executable file
·127 lines (102 loc) · 4.34 KB
/
extension.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let previewUri = vscode.Uri.parse('html-escape://cfjedimaster/html-escape');
class TextDocumentContentProvider implements vscode.TextDocumentContentProvider {
private _onDidChange = new vscode.EventEmitter<vscode.Uri>();
public provideTextDocumentContent(uri: vscode.Uri): string {
return this.createEscapedHTML();
}
get onDidChange(): vscode.Event<vscode.Uri> {
return this._onDidChange.event;
}
public update(uri: vscode.Uri) {
this._onDidChange.fire(uri);
}
private createEscapedHTML() {
let editor = vscode.window.activeTextEditor;
return this.extractSnippet();
}
private extractSnippet(): string {
let editor = vscode.window.activeTextEditor;
//if we have a selection, use that, otherwise, whole document
console.log('is it empty? '+editor.selection.isEmpty);
if(editor.selection.isEmpty) {
return this.snippet(editor.document.getText());
} else {
console.log('return selection');
//there must be a better way of doing this
let selStart = editor.document.offsetAt(editor.selection.start);
let selEnd = editor.document.offsetAt(editor.selection.end);
return this.snippet(editor.document.getText().slice(selStart,selEnd));
}
}
private errorSnippet(error: string): string {
return `
<body>
${error}
</body>`;
}
private snippet(str: string): string {
str = str.replace(/&/g, "&");
str = str.replace(/</g, "<");
str = str.replace(/>/g, ">");
str = str.replace(/"/g, """);
str = str.replace(/'/g, "'");
str = str.replace(/\//g, "/");
//Required since we're rendering in HTML itself...
str = str.replace(/&/g, "&");
return `
<style>
body {
margin-top:10px;
margin-left:10px;
margin-right:10px;
}
textarea {
width:95%;
height:500px;
}
</style>
<body>
<textarea>${str}</textarea>
</body>
`
}
}
let provider = new TextDocumentContentProvider();
let registration = vscode.workspace.registerTextDocumentContentProvider('html-escape', provider);
vscode.workspace.onDidChangeTextDocument((e: vscode.TextDocumentChangeEvent) => {
if (e.document === vscode.window.activeTextEditor.document) {
provider.update(previewUri);
}
});
vscode.window.onDidChangeTextEditorSelection((e: vscode.TextEditorSelectionChangeEvent) => {
if (e.textEditor === vscode.window.activeTextEditor) {
provider.update(previewUri);
}
})
let disposable = vscode.commands.registerCommand('extension.showHtmlEscape', () => {
return vscode.commands.executeCommand('vscode.previewHtml', previewUri, vscode.ViewColumn.Two, 'Escaped HTML').then((success) => {
}, (reason) => {
vscode.window.showErrorMessage(reason);
});
});
let highlight = vscode.window.createTextEditorDecorationType({ backgroundColor: 'rgba(200,200,200,.35)' });
vscode.commands.registerCommand('extension.showHtmlEscape', (uri: vscode.Uri, propStart: number, propEnd: number) => {
for (let editor of vscode.window.visibleTextEditors) {
if (editor.document.uri.toString() === uri.toString()) {
let start = editor.document.positionAt(propStart);
let end = editor.document.positionAt(propEnd + 1);
editor.setDecorations(highlight, [new vscode.Range(start, end)]);
setTimeout(() => editor.setDecorations(highlight, []), 1500);
}
}
});
context.subscriptions.push(disposable, registration);
}
export function deactivate() {
}