Skip to content

Commit

Permalink
fix: "Show Model Preview" menu was not working
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeUtan committed Apr 20, 2021
1 parent 63375d2 commit e2155fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as vscode from 'vscode';
import { MCModelPanel } from './MCModelPanel';
import * as config from './config';
import * as utils from './utils';

let textEditorController;

export async function activate(context: vscode.ExtensionContext) {
textEditorController = new TextEditorController();
context.subscriptions.push(textEditorController);
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(onChangedActiveTextEditor)
);

// Listen to configuration changes
context.subscriptions.push(...config.createConfigurationListeners());
Expand All @@ -33,19 +34,13 @@ export async function activate(context: vscode.ExtensionContext) {

export function deactivate() {}

class TextEditorController {
private _disposable: vscode.Disposable;

constructor() {
let subscriptions: vscode.Disposable[] = [];
vscode.window.onDidChangeActiveTextEditor(this._onChangedActiveTextEditor, this, subscriptions);
this._disposable = vscode.Disposable.from(...subscriptions);
}

dispose() {
this._disposable.dispose();
}
function onChangedActiveTextEditor(editor?: vscode.TextEditor) {
const val = editor ? isModelFile(editor) : false;
vscode.commands.executeCommand("setContext", "mcmodel-viewer.activeTextEditorIsModel", val);
}

private _onChangedActiveTextEditor(editor?: vscode.TextEditor) {
}
}
function isModelFile(editor: vscode.TextEditor) {
return editor
&& editor.document.languageId === "json"
&& config.modelAssetsRoots.some(root => utils.isParentDir(root.path, editor.document.uri.path));
}
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as path from 'path';

export function isParentDir(parent: string, file: string) {
const relativePath = path.relative(parent, file);
return relativePath && relativePath.split(path.sep)[0] !== '..' && !path.isAbsolute(relativePath);
}

0 comments on commit e2155fb

Please sign in to comment.