Skip to content

Commit

Permalink
Fixed a bug where the StatusBar was constantly showing indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
MohrJonas committed Nov 5, 2022
1 parent 8c1cb04 commit a7f9ed4
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/Convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SettingsManager from "./Settings";
/**
* Convert a file from a pdf to a png
* @param file The file to convert
* @returns A list of absolute paths, each representing a page of the pdf
* @returns A list of absolute png-file paths, each representing a page of the pdf
*/
export async function convertPdfToPng(file: File): Promise<Array<string>> {
let platformSpecific: string;
Expand All @@ -34,6 +34,7 @@ export async function convertPdfToPng(file: File): Promise<Array<string>> {
const command = `${platformSpecific} -density ${SettingsManager.currentSettings.density} -quality ${SettingsManager.currentSettings.quality} -background white -alpha remove -alpha off ${SettingsManager.currentSettings.additionalImagemagickArgs} "${file.absPath}" "${join(randomFolderPath, "out.png")}"`;
const execResult = exec(command);
ObsidianOCRPlugin.children.push(execResult.execProcess);
await execResult.execPromise;
return await globby("*.png", {
cwd: randomFolderPath,
absolute: true
Expand Down
14 changes: 7 additions & 7 deletions src/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import SearchModal from "./modals/SearchModal";
import {areDepsMet} from "./Convert";
import {OcrQueue} from "./utils/OcrQueue";
import {ChildProcess} from "child_process";
import InstallationProviderManager from "./utils/InstallationProviderManager";
import WindowsInstallationProvider from "./utils/WindowsInstallationProvider";
import DebInstallationProvider from "./utils/DebInstallationProvider";
import InstallationProviderManager from "./utils/installation/InstallationProviderManager";
import WindowsInstallationProvider from "./utils/installation/WindowsInstallationProvider";
import DebInstallationProvider from "./utils/installation/DebInstallationProvider";

export default class ObsidianOCRPlugin extends Plugin {

Expand All @@ -37,7 +37,7 @@ export default class ObsidianOCRPlugin extends Plugin {
this.registerEvent(this.app.vault.on("create", async (tFile) => {
if (tFile instanceof TFolder) return;
const file = File.fromFile(tFile as TFile);
await OcrQueue.enqueueFile(file);
OcrQueue.enqueueFile(file);
}));
this.registerEvent(this.app.vault.on("delete", async (tFile) => {
if (tFile instanceof TFolder) {
Expand All @@ -47,7 +47,7 @@ export default class ObsidianOCRPlugin extends Plugin {
const file = File.fromFile(tFile as TFile);
if (file.jsonFile && existsSync(file.jsonFile.absPath)) {
TranscriptCache.remove(await Transcript.load(file.jsonFile.absPath));
await unlink(file.jsonFile.absPath);
unlink(file.jsonFile.absPath);
}
}));
this.registerEvent(this.app.vault.on("rename", async (file, oldPath) => {
Expand All @@ -59,12 +59,12 @@ export default class ObsidianOCRPlugin extends Plugin {
})[0];
transcript.originalFilePath = newFile.vaultRelativePath;
await rename(oldFile.jsonFile.absPath, newFile.jsonFile.absPath);
await writeFile(newFile.jsonFile.absPath, Transcript.encode(transcript));
writeFile(newFile.jsonFile.absPath, Transcript.encode(transcript));
}));
this.app.workspace.onLayoutReady(async () => {
if (!await areDepsMet()) new Notice("Dependecies aren't met");
if (SettingsManager.currentSettings.ocrProviderName == "NoOp") new Notice("Don't forget to select an OCR Provider in the settings.");
await TranscriptCache.populate();
TranscriptCache.populate();
processVault();
});
this.app.workspace.on("quit", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {OcrQueue} from "./utils/OcrQueue";
import TranscriptCache from "./TranscriptCache";
import {delimiter} from "path";
import {areDepsMet} from "./Convert";
import InstallationProviderManager from "./utils/InstallationProviderManager";
import InstallationProviderManager from "./utils/installation/InstallationProviderManager";
import TerminalModal from "./modals/TerminalModal";
import ObsidianOCRPlugin from "./Main";

Expand Down
5 changes: 2 additions & 3 deletions src/utils/FileOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {OcrQueue} from "./OcrQueue";
export async function removeAllJsonFiles() {
StatusBar.addStatusDeleting();
for (const jsonFile of (await getAllJsonFiles())) {
await unlink(jsonFile.absPath);
unlink(jsonFile.absPath);
}
StatusBar.removeStatusDeleting();
}
Expand Down Expand Up @@ -57,7 +57,6 @@ export async function processFile(file: File): Promise<Transcript | undefined> {
}

export function processVault() {

app.vault.getFiles()
.map((tFile) => {
return File.fromFile(tFile);
Expand All @@ -66,6 +65,6 @@ export function processVault() {
return await isFileValid(file);
})
.forEach(async (file) => {
OcrQueue.enqueueFile(file);
await OcrQueue.enqueueFile(file);
});
}
7 changes: 6 additions & 1 deletion src/utils/HocrUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Page from "../hocr/Page";
import HocrElement from "../hocr/HocrElement";
import Transcript from "../hocr/Transcript";
import Page from "../hocr/Page";

export function getTranscript(element: HocrElement): Transcript {
if (element.parent == undefined) return element as Transcript;
return getTranscript(element.parent);
}

//TODO change that ugly mess
//🚧 Do not look at this mess 🚧
export function flattenText(page: Page): string {
return page.children.map((child) => {
Expand All @@ -23,6 +24,10 @@ export function flattenText(page: Page): string {
}).flat().join(" ");
}

function getChildren(element: HocrElement): Array<HocrElement> {
return element.children;
}

export function parseTitle(title: string): Record<string, string> {
const titleParts = title.split("; ");
const record: Record<string, string> = {};
Expand Down
5 changes: 2 additions & 3 deletions src/utils/OcrQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export class OcrQueue {
const transcript = await processFile(file);
if (!transcript) return;
TranscriptCache.add(transcript);
await app.vault.create(file.jsonFile.vaultRelativePath, Transcript.encode(transcript));

app.vault.create(file.jsonFile.vaultRelativePath, Transcript.encode(transcript));
StatusBar.removeIndexingFile(file);
callback();
}, SettingsManager.currentSettings.concurrentIndexingProcesses);
Expand All @@ -29,7 +28,7 @@ export class OcrQueue {

public static async enqueueFile(file: File) {
if (!await isFileOCRable(file)) return;
await this.getQueue().push(file);
this.getQueue().push(file);
StatusBar.addIndexingFile(file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import InstallationProvider from "./InstallationProvider";
import {Terminal} from "xterm";
import ansiColors from "ansi-colors";
import {exec} from "sudo-prompt";
import {doesProgramExist} from "./Utils";
import {doesProgramExist} from "../Utils";

export default class DebInstallationProvider implements InstallationProvider {

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a7f9ed4

Please sign in to comment.