Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(smartAutocomplete): View data update on file change #103

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 50 additions & 19 deletions src/client/Preview/Register.ts
Expand Up @@ -3,47 +3,63 @@ import {TextDocumentContentProvider} from './TextDocumentContentProvider';

export function registerPreview(context, window, client) {

const aureliaViewDataPanelType = 'aureliaViewData';

let previewUri = vscode.Uri.parse('aurelia-preview://authority/aurelia-preview');

let provider = new TextDocumentContentProvider(client);
let registration = vscode.workspace.registerTextDocumentContentProvider('aurelia-preview', provider);
let isPanelVisible: boolean = false;
let panel: vscode.WebviewPanel;

function fillWebViewHtml(bodyContent: string): string {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cat Coding</title>
</head>
${bodyContent}
</html>
`;
}

vscode.workspace.onDidChangeTextDocument((e: vscode.TextDocumentChangeEvent) => {
if (!isPanelVisible) return;
if (e.document === vscode.window.activeTextEditor.document) {
provider.update(previewUri);
provider.update(previewUri).then(success => {
panel.webview.html = fillWebViewHtml(success);
});
}
});

vscode.window.onDidChangeTextEditorSelection((e: vscode.TextEditorSelectionChangeEvent) => {
if (e.textEditor === vscode.window.activeTextEditor) {
provider.update(previewUri);
}
vscode.window.onDidChangeActiveTextEditor((editor: vscode.TextEditor) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed onDidChangeTextEditorSelection to onDidChangeActiveTextEditor.

Looking at this again after a while, seems like the intent behind onDidChangeTextEditorSelection was to update the webview after changes as well.
I might revert this change

setTimeout(() => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setTimeout, because onDidChangeActiveTextEditor is called before panel.onDidDispose

I could not solve it another way atm.

if (!isPanelVisible) return;
if (editor === vscode.window.activeTextEditor) {
provider.update(previewUri).then(success => {
panel.webview.html = fillWebViewHtml(success);
});
}
}, 0);
});

context.subscriptions.push(vscode.commands.registerCommand('aurelia.showViewProperties', () => {

const smartAutocomplete = vscode.workspace.getConfiguration().get('aurelia.featureToggles.smartAutocomplete');
if (smartAutocomplete) {
const panel = vscode.window.createWebviewPanel(
'aureliaViewData',
panel = vscode.window.createWebviewPanel(
aureliaViewDataPanelType,
'Aurelia view data',
vscode.ViewColumn.Two,
);

provider.provideTextDocumentContent(previewUri)
.then(
(success) => {
panel.webview.html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cat Coding</title>
</head>
${success}
</html>
`
panel.webview.html = fillWebViewHtml(success);
},
(reason) => {
window.showErrorMessage(reason);
Expand All @@ -52,8 +68,23 @@ export function registerPreview(context, window, client) {
return vscode.window.showWarningMessage('This command requires the experimental feature "smartAutocomplete" to be enabled');
}

/**
* Set panel visible flag to true, if
* - we have the correct WebView type (multiple WebView types possible)
* - and panel itself is not active
*/
panel.onDidChangeViewState(event => {
const correctPanelType = (event.webviewPanel.viewType === aureliaViewDataPanelType);
/** Don't update panel if the panel itself is 'active' */
const panelNotActive = !event.webviewPanel.active
isPanelVisible = correctPanelType && panelNotActive;
});

panel.onDidDispose(event => {
isPanelVisible = false;
});

}));
}));

}

1 change: 1 addition & 0 deletions src/client/Preview/TextDocumentContentProvider.ts
Expand Up @@ -119,5 +119,6 @@

public update(uri: vscode.Uri) {
this._onDidChange.fire(uri);
return this.provideTextDocumentContent(uri);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to return content for webview

}
}