From 749775c2b1f70ac176426dd85b8d6bd8eefd97af Mon Sep 17 00:00:00 2001 From: Tom Norris Date: Thu, 7 Sep 2023 20:03:31 -0700 Subject: [PATCH] Image info block now tries to auto resolve file moves Updated readme --- README.md | 28 +++++++++++++++++++++- src/block.ts | 64 ++++++++++++++++++++++++++++++++++++++++++------- src/main.ts | 13 +--------- src/settings.ts | 1 - src/utils.ts | 32 ++++++++++++++++++++++--- 5 files changed, 113 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 65876a8..e9610f0 100644 --- a/README.md +++ b/README.md @@ -52,4 +52,30 @@ Argument Info: ## Settings: -![](https://raw.githubusercontent.com/TomNCatz/obsidian-gallery/main/images/Gallery_Settings.png) \ No newline at end of file +![](https://raw.githubusercontent.com/TomNCatz/obsidian-gallery/main/images/Gallery_Settings.png) + + +# Release Notes +## v0.7.0 +- Total image count now only count file the gallery thinks it can display in the first place +- More large gallery optimization +- Added a setting for default hidden info +- Added the option to set your own template file for the meta files + +## v0.6.2 +- Tag filter support added to gallery block +- Tag filter support is more stable and consistent now +- added support for .gif and .webm support +- indicator in filter of how many files are being shown verses the total(this currently show all file types for the total, not just supported image file types, will fix later) +- some work done to improve handling of exceptionally large image collections(over 14000) +- moved the reverse sorting checkbox into the filter header so it can be changed on the fly +- cleaned up some packages that might have had vulnerabilities +- fixed issue with supporting multiple spaces in a file name + +## V0.6.1 +- when desktop version parses color information for an image is saves it and mobile loads it from that save (work around for not being able to get palette information on mobile) +- tag filter field added +- can filter by multiple tags if separated by spaces +- putting a '-' at the front of a tag such as '-photos' will exclude all results with that tag +- tag filter mode checkbox added, when checked only shows results that match ALL tags, when off includes result that match ANY tags +- Fixed MP$ display on desktop(still broken on mobile) diff --git a/src/block.ts b/src/block.ts index fb27297..7402d52 100644 --- a/src/block.ts +++ b/src/block.ts @@ -1,12 +1,12 @@ -import type { Vault, MetadataCache, FrontMatterCache } from 'obsidian' -import { MarkdownRenderer, TFile, getAllTags, Platform } from 'obsidian' +import type { Vault, MetadataCache, FrontMatterCache, EditorPosition } from 'obsidian' +import { MarkdownRenderer, TFile, getAllTags, Platform, MarkdownView } from 'obsidian' import { extractColors } from '../node_modules/extract-colors' import type { GalleryBlockArgs, InfoBlockArgs } from './utils' import { EXTENSIONS, GALLERY_DISPLAY_USAGE, EXTRACT_COLORS_OPTIONS, OB_GALLERY_INFO, VIDEO_REGEX, - getImgInfo, updateFocus + getImgInfo, updateFocus, GALLERY_INFO_USAGE, searchForFile } from './utils' import { GalleryInfoView } from './view' import type GalleryTagsPlugin from './main' @@ -191,7 +191,7 @@ export class GalleryProcessor } } - async galleryImageInfo(source: string, el: HTMLElement, vault: Vault, metadata: MetadataCache, plugin: GalleryTagsPlugin) + async galleryImageInfo(source: string, el: HTMLElement, sourcePath: string, vault: Vault, metadata: MetadataCache, plugin: GalleryTagsPlugin) { const args: InfoBlockArgs = { imgPath: '', @@ -219,16 +219,64 @@ export class GalleryProcessor attr: { style: 'width: 100%; height: auto; float: left' } }) - const imgTFile = vault.getAbstractFileByPath(args.imgPath) - const imgURL = vault.adapter.getResourcePath(args.imgPath) + let imgTFile = vault.getAbstractFileByPath(args.imgPath) + let imgURL = vault.adapter.getResourcePath(args.imgPath) // Handle problematic arg - if (!args.imgPath || !imgTFile) + if(!args.imgPath) { - MarkdownRenderer.renderMarkdown('GALLERY_INFO_USAGE', elCanvas, '/', plugin) + MarkdownRenderer.render(plugin.app, GALLERY_INFO_USAGE, elCanvas, '/', plugin) return; } + if (!imgTFile) + { + const found = await searchForFile(args.imgPath, plugin); + + if(found.length == 0) + { + MarkdownRenderer.render(plugin.app,GALLERY_INFO_USAGE, elCanvas, '/', plugin) + return; + } + + if(found.length == 1) + { + // set file and path for current usage + imgTFile = vault.getAbstractFileByPath(found[0]) + imgURL = vault.adapter.getResourcePath(found[0]) + + // replace file path for future usage + const view = plugin.app.workspace.getActiveViewOfType(MarkdownView); + + if (view) + { + for(let i = 0; i < view.editor.lineCount(); i++) + { + let line = view.editor.getLine(i); + if(line.contains(args.imgPath)) + { + const from: EditorPosition = {line: i, ch: line.indexOf(args.imgPath)}; + const to: EditorPosition = {line: i, ch: line.indexOf(args.imgPath)+args.imgPath.length}; + view.editor.replaceRange(found[0], from, to); + } + } + } + + } + else + { + // too many options, tell the user about it + let output = "### File path not found. Were you looking for one of these?\n" + for(let i = 0; i < found.length; i++) + { + output += "- "+found[i]+"\n"; + } + + MarkdownRenderer.render(plugin.app,output, elCanvas, '/', plugin) + return; + } + } + let measureEl, isVideo let hexList: string[] = []; // Get image dimensions diff --git a/src/main.ts b/src/main.ts index 577168c..440921e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,13 +8,11 @@ export default class GalleryTagsPlugin extends Plugin { settings!: GallerySettings; containerEl!: HTMLElement; - currentMetaTemplate: string; async onload() { // Load message await this.loadSettings(); - await this.loadMetaTemplate(); console.log('Loaded Gallery Tags Plugin'); // Register gallery display block renderer @@ -28,7 +26,7 @@ export default class GalleryTagsPlugin extends Plugin this.registerMarkdownCodeBlockProcessor('gallery-info', async (source, el, ctx) => { const proc = new GalleryProcessor() - await proc.galleryImageInfo(source, el, this.app.vault, this.app.metadataCache, this) + await proc.galleryImageInfo(source, el, ctx.sourcePath, this.app.vault, this.app.metadataCache, this) }); // Add Gallery Icon @@ -76,15 +74,6 @@ export default class GalleryTagsPlugin extends Plugin this.app.workspace.detachLeavesOfType(OB_GALLERY_INFO) console.log('unloading Gallery Plugin') } - - async loadMetaTemplate() - { - const imgTFile = this.app.vault.getAbstractFileByPath(this.settings.imgmetaTemplatePath+".md") as TFile; - if(imgTFile) - { - this.currentMetaTemplate = await this.app.vault.read(imgTFile); - } - } async loadSettings() { diff --git a/src/settings.ts b/src/settings.ts index c9d0884..9d75975 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -100,7 +100,6 @@ export class GallerySettingTab extends PluginSettingTab this.plugin.settings.imgmetaTemplatePath = imgmetaPathInput imgmetaPathInput = '' this.plugin.saveSettings() - await this.plugin.loadMetaTemplate(); })) .addText(text => text .setPlaceholder(this.plugin.settings.imgmetaTemplatePath) diff --git a/src/utils.ts b/src/utils.ts index 8d1332f..c5d0605 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -102,7 +102,7 @@ e.g. Input: \`\`\` imgPath=Resources/Images/Image_example_1.png -infoList=Name;tags;size;backlinks +ignoreInfo=Name;tags;size;backlinks \`\`\` ---- @@ -111,11 +111,12 @@ infoList=Name;tags;size;backlinks - Path is the relative path of the file withing the obsidian vault. - Make sure the image exists!! - It is case sensitive! +- IgnoreInfo is a list of fields NOT to display(if not included uses Default Hidden Info setting) ---- Please Check Release Notes for plugin changes:
-https://github.com/Darakah/obsidian-gallery#release-notes +https://github.com/TomNCatz/obsidian-gallery#release-notes ` export const GALLERY_RESOURCES_MISSING = ` @@ -202,7 +203,15 @@ export const getImgInfo = async (imgPath: string, vault: Vault, metadata: Metada counter++; } - await vault.adapter.write(`${plugin.settings.imgDataFolder}/${fileName}.md`, initializeInfo(plugin.currentMetaTemplate, imgPath, imgName)) + + const templateTFile = plugin.app.vault.getAbstractFileByPath(plugin.settings.imgmetaTemplatePath+".md") as TFile; + let template = defaultTemplate; + if(templateTFile) + { + template = await plugin.app.vault.read(templateTFile); + } + + await vault.adapter.write(`${plugin.settings.imgDataFolder}/${fileName}.md`, initializeInfo(template, imgPath, imgName)) infoFile = (vault.getAbstractFileByPath(`${plugin.settings.imgDataFolder}/${fileName}.md`) as TFile) } return infoFile @@ -212,6 +221,23 @@ export const getImgInfo = async (imgPath: string, vault: Vault, metadata: Metada return null }; +export const searchForFile = async (path: string, plugin: GalleryTagsPlugin): Promise => +{ + const foundPaths: string[] = [] + const vaultFiles: TFile[] = plugin.app.vault.getFiles(); + const fileName: string = path.substring(path.lastIndexOf('/')); + + for (const file of vaultFiles) + { + if (EXTENSIONS.contains(file.extension.toLowerCase()) && file.path.contains(fileName) ) + { + foundPaths.push(file.path); + } + } + + return foundPaths; +} + /** * Return images in the specified directory * @param path - path to project e.g. 'Test Project/First Sub Project'