Skip to content

Commit

Permalink
Open DICOM folder from tree view
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbinian Eckstein committed Jan 26, 2024
1 parent 3a8c8d3 commit 9d8e4b7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
42 changes: 28 additions & 14 deletions src/editorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ export class NiiVueEditorProvider implements vscode.CustomReadonlyEditorProvider
})
}

public static async createOrShowDcmFolder(context: vscode.ExtensionContext, uri: vscode.Uri) {
const name = vscode.Uri.parse(uri.toString()).path.split('/').pop()
const tabName = name ? name : 'NiiVue DICOM'
this.createPanel(context, 'niivue.webview', tabName, uri).then((panel) => {
panel.webview.onDidReceiveMessage(async (e) => {
if (e.type === 'ready') {
NiiVueEditorProvider.openDcmFolder(uri, panel)
}
})
})
}

public static async createCompareView(context: vscode.ExtensionContext, items: any) {
const uris = items.map((item: any) => vscode.Uri.parse(item))
this.createPanel(context, 'niivue.compare', 'NiiVue Compare Panel', uris[0]).then((panel) => {
Expand Down Expand Up @@ -162,27 +174,29 @@ export class NiiVueEditorProvider implements vscode.CustomReadonlyEditorProvider
})
.then(async (folderUri) => {
if (folderUri) {
const files = await vscode.workspace.fs.readDirectory(folderUri[0])
const fileUris = files.map((file) => vscode.Uri.joinPath(folderUri[0], file[0]))
const data = await Promise.all(
fileUris.map((uri) =>
vscode.workspace.fs.readFile(uri).then((data) => data.buffer),
),
)
panel.webview.postMessage({
type: 'addImage',
body: {
data: data,
uri: fileUris[0].toString(),
},
})
NiiVueEditorProvider.openDcmFolder(folderUri[0], panel)
}
})
return
}
})
}

public static async openDcmFolder(folderUri: vscode.Uri, panel: vscode.WebviewPanel) {
const files = await vscode.workspace.fs.readDirectory(folderUri)
const fileUris = files.map((file) => vscode.Uri.joinPath(folderUri, file[0]))
const data = await Promise.all(
fileUris.map((uri) => vscode.workspace.fs.readFile(uri).then((data) => data.buffer)),
)
panel.webview.postMessage({
type: 'addImage',
body: {
data: data,
uri: fileUris[0].toString(),
},
})
}

async resolveCustomEditor(
document: NiiVueDocument,
webviewPanel: vscode.WebviewPanel,
Expand Down
48 changes: 22 additions & 26 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import { LinkHoverProvider } from './HoverProvider'

export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(NiiVueEditorProvider.register(context))
context.subscriptions.push(
vscode.languages.registerHoverProvider('*', new LinkHoverProvider()),
)
context.subscriptions.push(vscode.languages.registerHoverProvider('*', new LinkHoverProvider()))
context.subscriptions.push(
vscode.commands.registerCommand('niivue.openWebLink', async () => {
vscode.window
.showInputBox({
prompt: 'File Path',
placeHolder:
'https://niivue.github.io/niivue-demo-images/mni152.nii.gz',
placeHolder: 'https://niivue.github.io/niivue-demo-images/mni152.nii.gz',
})
.then((input) => {
if (input) {
Expand All @@ -30,11 +27,7 @@ export function activate(context: vscode.ExtensionContext) {
)
context.subscriptions.push(
vscode.commands.registerCommand('niiVue.openLocal', async (args: any) => {
vscode.commands.executeCommand(
'vscode.openWith',
args.resourceUri,
'niiVue.default',
)
vscode.commands.executeCommand('vscode.openWith', args.resourceUri, 'niiVue.default')
}),
)
context.subscriptions.push(
Expand All @@ -49,23 +42,26 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand(
'niiVue.openFromExplorer',
async (_activeItem: any, items: any) => {
if (items && items.length >= 1) {
vscode.commands.executeCommand(
'vscode.openWith',
vscode.Uri.parse(items[0]),
'niiVue.default',
)
const uri = vscode.Uri.parse(items[0])
const stat = await vscode.workspace.fs.stat(uri)

if ((stat.type & vscode.FileType.Directory) !== 0) {
NiiVueEditorProvider.createOrShowDcmFolder(context, uri)
} else {
vscode.window
.showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: true,
openLabel: 'Open Image or Mesh',
})
.then((uris) => {
NiiVueEditorProvider.createCompareView(context, uris)
})
if (items && items.length >= 1) {
vscode.commands.executeCommand('vscode.openWith', uri, 'niiVue.default')
} else {
vscode.window
.showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: true,
openLabel: 'Open Image or Mesh',
})
.then((uris) => {
NiiVueEditorProvider.createCompareView(context, uris)
})
}
}
},
),
Expand Down

0 comments on commit 9d8e4b7

Please sign in to comment.