Skip to content

Commit 67a971d

Browse files
committed
use webview api instead of execute command
1 parent 57cd454 commit 67a971d

File tree

3 files changed

+67
-28
lines changed

3 files changed

+67
-28
lines changed

src/ConfluenceContentProvider.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ export class ConfluenceContentProvider implements vscode.TextDocumentContentProv
3232
private _onDidChange = new vscode.EventEmitter<vscode.Uri>();
3333
private _waiting: boolean = false;
3434

35-
constructor(private context: vscode.ExtensionContext) { }
35+
constructor(private context: vscode.ExtensionContext) {
36+
}
37+
38+
dispose() {
39+
this._onDidChange.dispose();
40+
}
3641

3742
public async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
3843
let document = await vscode.workspace.openTextDocument(unpackConfluenceUri(uri));

src/extension.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,84 @@ import { packConfluenceUri, unpackConfluenceUri, ConfluenceContentProvider } fro
77

88
const path = require('path');
99

10+
function getRenderedContent(contentProvider: ConfluenceContentProvider, uri: vscode.Uri, panel: vscode.WebviewPanel) {
11+
contentProvider.provideTextDocumentContent(packConfluenceUri(uri)).then((renderedContent) => {
12+
panel.webview.html = renderedContent;
13+
}, (reason) => {
14+
vscode.window.showErrorMessage(reason);
15+
});
16+
}
17+
18+
function createPanel(contentProvider: ConfluenceContentProvider, editor: vscode.TextEditor, viewColumn: vscode.ViewColumn) {
19+
20+
let title = 'Preview ' + path.basename(editor.document.uri.fsPath);
21+
22+
// Create and show panel
23+
const panel = vscode.window.createWebviewPanel(
24+
'confluencePreview',
25+
title,
26+
viewColumn,
27+
{
28+
retainContextWhenHidden: true
29+
}
30+
);
31+
getRenderedContent(contentProvider, editor.document.uri, panel)
32+
33+
return panel;
34+
}
35+
36+
function setDispose(panel: vscode.WebviewPanel, subscriptions: any){
37+
// Reset when the current panel is closed
38+
// Reset when the current panel is closed
39+
panel.onDidDispose(
40+
() => {},
41+
null,
42+
subscriptions
43+
);
44+
}
45+
1046
// this method is called when your extension is activated
1147
// your extension is activated the very first time the command is executed
1248
export function activate(context: vscode.ExtensionContext) {
1349

1450
const contentProvider = new ConfluenceContentProvider(context);
1551
const contentProviderRegistration = vscode.workspace.registerTextDocumentContentProvider(ConfluenceContentProvider.confluenceURI.scheme, contentProvider);
52+
let currentPanel: vscode.WebviewPanel;
1653

1754
// Show confluence
1855
let previewDisposable = vscode.commands.registerCommand('confluence.showPreview', () => {
1956
let editor = vscode.window.activeTextEditor;
20-
2157
if (typeof editor === 'undefined') {
2258
vscode.window.showErrorMessage('Please open a confluence file');
2359
return;
60+
} else {
61+
currentPanel = createPanel(contentProvider, editor, vscode.ViewColumn.Active);
62+
setDispose(currentPanel, context.subscriptions);
63+
return currentPanel;
2464
}
25-
26-
let title = 'Preview ' + path.basename(editor.document.uri.fsPath);
27-
28-
return vscode.commands.executeCommand('vscode.previewHtml', packConfluenceUri(editor.document.uri), vscode.ViewColumn.One, title).then((success) => {
29-
}, (reason) => {
30-
vscode.window.showErrorMessage(reason);
31-
});
3265
});
3366

3467
// Show confluence to the side
3568
let sidePreviewDisposable = vscode.commands.registerCommand('confluence.showPreviewToSide', () => {
3669
let editor = vscode.window.activeTextEditor;
37-
3870
if (typeof editor === 'undefined') {
3971
vscode.window.showErrorMessage('Please open a confluence file');
4072
return;
73+
} else {
74+
currentPanel = createPanel(contentProvider, editor, vscode.ViewColumn.Two);
75+
setDispose(currentPanel, context.subscriptions);
76+
return currentPanel;
4177
}
78+
});
4279

43-
let title = 'Preview ' + path.basename(editor.document.uri.fsPath);
4480

45-
return vscode.commands.executeCommand('vscode.previewHtml', packConfluenceUri(editor.document.uri), vscode.ViewColumn.Two, title).then((success) => {
46-
}, (reason) => {
47-
vscode.window.showErrorMessage(reason);
48-
});
49-
});
5081

5182
vscode.workspace.onDidChangeTextDocument(e => {
5283
let editor = vscode.window.activeTextEditor;
5384
if (editor) {
5485
if (e.document === editor.document) {
5586
contentProvider.update(packConfluenceUri(e.document.uri));
87+
getRenderedContent(contentProvider, e.document.uri, currentPanel)
5688
}
5789
}
5890
});

src/markupParser.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ function imageUri(searchUri: vscode.Uri, imageLink: string) {
1313
if (imageLink.match(/^(ht)|(f)tps?:\/\//)) {
1414
imageUri = vscode.Uri.parse(imageLink);
1515
} else {
16-
let extPath = path.dirname(searchUri.fsPath);
17-
imageUri = vscode.Uri.file(path.join(extPath, imageLink));
16+
imageUri = vscode.Uri.file(path.join(searchUri.fsPath, imageLink)).with({ scheme: 'vscode-resource' });
1817
}
1918
return imageUri;
2019
}
2120

22-
function getUri(filepath: string, filename: string){
21+
function getUri(filepath: string, filename: string) {
2322
let extension = vscode.extensions.getExtension(EXTENTION_ID);
2423
if (extension) {
2524
let extPath = extension.extensionPath;
26-
let uri = vscode.Uri.file(path.join(extPath, filepath, filename));
25+
26+
// set special chema for resource:
27+
// https://code.visualstudio.com/api/extension-guides/webview#loading-local-content
28+
let uri = vscode.Uri.file(path.join(extPath, filepath, filename)).with({ scheme: 'vscode-resource' });
2729
return uri;
2830
}
2931
}
@@ -36,7 +38,7 @@ export function cssUri(cssFile: string) {
3638
return getUri(CSS_PATH, cssFile);
3739
}
3840

39-
export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
41+
export function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
4042
//TODO: use Tokenazer instead of line loop
4143

4244
var result = '';
@@ -92,7 +94,7 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
9294
html_tag = true;
9395
}
9496
//img
95-
let img = /!(.*)!/;
97+
let img = /!([^|]*)\|?.*!/;
9698
let match = tag.match(img);
9799
if (match) {
98100
tag = '<img src="' + imageUri(sourceUri, match[1]) + '"/>';
@@ -109,7 +111,7 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
109111
tag = tag.replace(/^\|/, '<td>');
110112
tag = tag.replace(/\|$/, '</td>');
111113
tag = tag.replace(/\|/gi, '</td><td>');
112-
if (tableFlag == false){
114+
if (tableFlag == false) {
113115
tag = '<table>' + tag;
114116
}
115117
tag = '<tr>' + tag + '</tr>';
@@ -147,7 +149,7 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
147149
listTag = 'ol';
148150
// reset ol after 3rd level
149151
// add count of non-ol elements for mixed lists
150-
if ( (match[1].length + (match[1].match(/[-|\*]/g) || []).length) % 3 === 1) {
152+
if ((match[1].length + (match[1].match(/[-|\*]/g) || []).length) % 3 === 1) {
151153
listStyle = ' class="initial"';
152154
}
153155
}
@@ -158,11 +160,11 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
158160
listStyle = ' class="alternate"';
159161
}
160162
if (match[1].length > listArr.length) {
161-
tag = '<'+ listTag + listStyle + '>';
163+
tag = '<' + listTag + listStyle + '>';
162164
listArr.push(listTag);
163165
}
164166
if (match[1].length < listArr.length) {
165-
tag = '</' + listArr.slice(match[1].length, listArr.length).reverse().join('></') +'>';
167+
tag = '</' + listArr.slice(match[1].length, listArr.length).reverse().join('></') + '>';
166168
listArr = listArr.slice(0, match[1].length);
167169
}
168170
tag += "<li>" + match[2] + "</li>";
@@ -188,12 +190,12 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
188190
tag = tag.replace(/_([\w ]*)_/g, "<i>$1</i>");
189191
}
190192
} else {
191-
if (tag !== '<pre><code>'){
193+
if (tag !== '<pre><code>') {
192194
tag = tag.replace(/</gi, '&lt;') + '<br />';
193195
}
194196
}
195197

196-
if (tag.match(/^s*$/)){
198+
if (tag.match(/^s*$/)) {
197199
tag = '<br />';
198200
}
199201

0 commit comments

Comments
 (0)