import type PdfFile from "./PdfFile"; import type XRefEntry from "./XRefEntry"; import Primitive from "./Primitive.svelte"; import { invoke } from "@tauri-apps/api/core"; import TreeViewRequest from "./TreeViewRequest.svelte"; import type XRefTable from "./XRefTable"; import TreeViewState from "./TreeViewState.svelte"; import type { PathSelectedEvent } from "../events/PathSelectedEvent"; export default class FileViewState { public path: string[] = $state(["Trailer"]); public file: PdfFile; public prim: Primitive | undefined = $state(); public highlighted_prim: string | undefined = $state(); public xref_entries: XRefEntry[] = $state([]); public stream_data: string | undefined = $state(); constructor(file: PdfFile) { this.file = file; this.selectPath(this.path); this.loadXrefEntries() } getLastJump(): string | number | undefined { return this.prim?.getLastJump() } getFirstJump(): string | number | undefined { return this.prim?.getFirstJump() } public loadXrefEntries() { invoke("get_xref_table", { id: this.file.id }) .then(result => { this.xref_entries = result.entries; }) .catch(err => console.error(err)); } public async selectPath(newPath: string[]) { if (newPath.at(-1) === "Data") { let _path = newPath.slice(0, newPath.length - 1) const _prim = await invoke("get_prim_by_path", { id: this.file.id, path: this.formatPaths(_path) }) this.stream_data = await invoke("get_stream_data", { id: this.file.id, path: this.formatPaths(newPath) }) this.prim = new Primitive(_prim); this.path = _path; this.highlighted_prim = "Data"; return; } else { this.stream_data = undefined; } invoke("get_prim_by_path", { id: this.file.id, path: this.formatPaths(newPath) }) .then(result => { let _prim = new Primitive(result) if (_prim.isContainer()) { this.prim = _prim; this.path = newPath return; } else { this.highlighted_prim = _prim.key; this.selectPath(newPath.slice(0, newPath.length - 1)) } }) .catch(err => console.error(err)); } public selectPathHandler(event: PathSelectedEvent) { if (event.detail.file_id !== this.file.id) { return; } this.selectPath(event.detail.path); } public getMergedPath() { return this.formatPaths(this.path); } public popPath() { let path = this.copyPath(); if (path.length == 1) { return } path.pop() this.selectPath(path); } public copyPath() { const _path: string[] = []; for (let item of this.path) { _path.push(item); } return _path; } public selectXref(entry: XRefEntry | undefined) { if (!entry) { this.selectPath(["/"]) return; } this.selectPath([entry.obj_num.toString()]); } public formatPaths(paths: string[]) { if (paths.length == 0) { return "/"; } if (paths[0] === "/") { return "Trailer/" + paths.slice(1, paths.length).join("/") } return paths.join("/"); } }