Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [1.3.0] - 2024-03-10
### Added
- TXT files can now be right-clicked and opened like directories in file explorer.

## [1.2.1] - 2022-09-28
### Fixed
- Supported file extensions are now consistent between [`package.json`](package.json) and [`src/utils.ts`](src/utils.ts).
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-image-gallery",
"displayName": "Image Gallery",
"description": "Improve image browsing experience, especially for remote development.",
"version": "1.2.1",
"version": "1.3.0",
"publisher": "GeriYoco",
"repository": {
"type": "git",
Expand Down Expand Up @@ -101,6 +101,11 @@
"command": "gryc.openGallery",
"group": "2_grycImageGallery@1",
"when": "explorerResourceIsFolder && !virtualWorkspace"
},
{
"command": "gryc.openGallery",
"group": "2_grycImageGallery@1",
"when": "resourceFilename =~ /.*\\.txt/"
}
]
}
Expand Down
44 changes: 34 additions & 10 deletions src/gallery/gallery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TFolder } from 'custom_typings';
import CustomSorter from './sorter';
import HTMLProvider from '../html_provider';
import { reporter } from '../telemetry';
import { minimatch } from 'minimatch';

export let disposable: vscode.Disposable;

Expand Down Expand Up @@ -42,37 +43,60 @@ class GalleryWebview {

constructor(private readonly context: vscode.ExtensionContext) { }

private async getImageUris(galleryFolder?: vscode.Uri | string) {
private async parseTxt(path: vscode.Uri, globPattern: string) {
/**
* Parse the TXT file and extract the URIs of the images from lines matching the glob pattern.
*
* @param path The path to the TXT file.
* @param globPattern The glob pattern to match the lines.
*/
let fileContents = (await vscode.workspace.openTextDocument(path)).getText();
let fileLines = fileContents.split('\n');
let validPaths = fileLines.filter((line) => minimatch(line, globPattern));
let validUris = validPaths.map((path) => vscode.Uri.file(path));
return validUris;
}

private async getImageUris(galleryTarget?: vscode.Uri) {
/**
* Recursively get the URIs of all the images within the folder.
*
* @param galleryFolder The folder to search. If not provided, the
* @param galleryTarget The TXT/folder to search. If not provided, the
* workspace folder will be used.
*/
vscode.window.showInformationMessage("Gallery target:"+galleryTarget);
let globPattern = utils.getGlob();
let imgUris = await vscode.workspace.findFiles(
galleryFolder ? new vscode.RelativePattern(galleryFolder, globPattern) : globPattern
);
return imgUris;
if (galleryTarget?.toString().endsWith('.txt')) {
return await this.parseTxt(galleryTarget, globPattern);
}
else { // assume the target is a folder
return await vscode.workspace.findFiles(
galleryTarget ? new vscode.RelativePattern(galleryTarget, globPattern) : globPattern
);
}
}

public async createPanel(galleryFolder?: vscode.Uri) {
const startTime = Date.now();
vscode.commands.executeCommand('setContext', 'ext.viewType', 'gryc.gallery');
const imageUris = await this.getImageUris(galleryFolder);
this.gFolders = await utils.getFolders(imageUris);
this.gFolders = this.customSorter.sort(this.gFolders);
const panel = vscode.window.createWebviewPanel(
'gryc.gallery',
`Image Gallery${galleryFolder ? ': ' + utils.getFilename(galleryFolder.path) : ''}`,
vscode.ViewColumn.One,
{
enableScripts: true,
retainContextWhenHidden: true,
localResourceRoots: [vscode.Uri.file('/'),
// adding all directories is not needded
// ideally would only add the ones containing images
]
}
);

panel.webview.options.localResourceRoots
const htmlProvider = new HTMLProvider(this.context, panel.webview);
const imageUris = await this.getImageUris(galleryFolder);
this.gFolders = await utils.getFolders(imageUris);
this.gFolders = this.customSorter.sort(this.gFolders);
panel.webview.html = htmlProvider.fullHTML();

const imageSizeStat = utils.getImageSizeStat(this.gFolders);
Expand Down
2 changes: 1 addition & 1 deletion src/gallery/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class DOMManager {
})
);
if (content.childElementCount === 0) {
content.innerHTML = "<p>No image found in this folder.</p>";
content.innerHTML = "<p>No image found in this gallery target.</p>";
}
}
}
Expand Down