Compare commits
No commits in common. "65743d85627ca910ac57dffb61a389b402fa7d85" and "123bf5100193afda6e6d337fb469a16a80c524c3" have entirely different histories.
65743d8562
...
123bf51001
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,5 +4,3 @@
|
|||||||
|
|
||||||
log.txt
|
log.txt
|
||||||
log
|
log
|
||||||
|
|
||||||
test2.pdf
|
|
@ -1,196 +0,0 @@
|
|||||||
interface EventMap {
|
|
||||||
keypress: CLIKeypressEvent;
|
|
||||||
char: CLICharEvent;
|
|
||||||
activate: Event;
|
|
||||||
deactivate: Event;
|
|
||||||
exit: Event;
|
|
||||||
enter: Event;
|
|
||||||
backspace: Event;
|
|
||||||
delete: Event;
|
|
||||||
"arrow-left": Event;
|
|
||||||
"arrow-right": Event;
|
|
||||||
"arrow-up": Event;
|
|
||||||
"arrow-down": Event;
|
|
||||||
[key: string]: Event;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface EventDetailMap {
|
|
||||||
keypress: {
|
|
||||||
key: number;
|
|
||||||
sequence?: Uint8Array;
|
|
||||||
};
|
|
||||||
char: EventDetailMap["keypress"] & {
|
|
||||||
char: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export type TypedEventTarget<EventMap extends object> = {
|
|
||||||
new (): IntermediateEventTarget<EventMap>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface IntermediateEventTarget<EventMap> extends EventTarget {
|
|
||||||
addEventListener<K extends keyof EventMap>(
|
|
||||||
type: K,
|
|
||||||
listener: (
|
|
||||||
event: EventMap[K] extends Event ? EventMap[K] : Event,
|
|
||||||
) => EventMap[K] extends Event ? void : never,
|
|
||||||
options?: boolean | AddEventListenerOptions,
|
|
||||||
): void;
|
|
||||||
|
|
||||||
addEventListener(
|
|
||||||
type: string,
|
|
||||||
listener: EventListenerOrEventListenerObject,
|
|
||||||
options?: boolean | AddEventListenerOptions,
|
|
||||||
): void;
|
|
||||||
|
|
||||||
removeEventListener<K extends keyof EventMap>(
|
|
||||||
type: K,
|
|
||||||
listener: (
|
|
||||||
event: EventMap[K] extends Event ? EventMap[K] : Event,
|
|
||||||
) => EventMap[K] extends Event ? void : never,
|
|
||||||
options?: boolean | AddEventListenerOptions,
|
|
||||||
): void;
|
|
||||||
|
|
||||||
removeEventListener(
|
|
||||||
type: string,
|
|
||||||
listener: EventListenerOrEventListenerObject,
|
|
||||||
options?: boolean | AddEventListenerOptions,
|
|
||||||
): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ManagerEventTarget = EventTarget as TypedEventTarget<EventMap>;
|
|
||||||
|
|
||||||
export class CLICharEvent extends CustomEvent<EventDetailMap["char"]> {
|
|
||||||
constructor(detail: EventDetailMap["char"]) {
|
|
||||||
super("char", { detail, cancelable: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class CLIKeypressEvent extends CustomEvent<EventDetailMap["keypress"]> {
|
|
||||||
constructor(detail: EventDetailMap["keypress"]) {
|
|
||||||
super("keypress", { detail, cancelable: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class InputManager extends ManagerEventTarget {
|
|
||||||
private static instance = new InputManager();
|
|
||||||
private active = false;
|
|
||||||
|
|
||||||
static getInstance(): InputManager {
|
|
||||||
return this.instance ??= new InputManager();
|
|
||||||
}
|
|
||||||
|
|
||||||
static addEventListener = InputManager.prototype.addEventListener.bind(
|
|
||||||
this.instance,
|
|
||||||
);
|
|
||||||
static removeEventListener = InputManager.prototype.removeEventListener.bind(
|
|
||||||
this.instance,
|
|
||||||
);
|
|
||||||
static dispatchEvent = InputManager.prototype.dispatchEvent.bind(
|
|
||||||
this.instance,
|
|
||||||
);
|
|
||||||
|
|
||||||
activate({ raw = true }: { raw?: boolean } = {}) {
|
|
||||||
if (this.active) return;
|
|
||||||
this.active = true;
|
|
||||||
this.dispatchEvent(new Event("activate"));
|
|
||||||
this.listen(raw);
|
|
||||||
}
|
|
||||||
|
|
||||||
deactivate({ dactivateRaw = true }: { dactivateRaw?: boolean } = {}) {
|
|
||||||
if (!this.active) return;
|
|
||||||
this.active = false;
|
|
||||||
this.dispatchEvent(new Event("deactivate"));
|
|
||||||
if (dactivateRaw) Deno.stdin.setRaw(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
once<T extends string>(type: T): Promise<Event> {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
const handler = (event: Event) => {
|
|
||||||
this.removeEventListener(type, handler);
|
|
||||||
resolve(event);
|
|
||||||
};
|
|
||||||
this.addEventListener(type, handler);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private async listen(raw: boolean) {
|
|
||||||
if (raw) await Deno.stdin.setRaw(true);
|
|
||||||
const buf = new Uint8Array(64);
|
|
||||||
|
|
||||||
while (this.active) {
|
|
||||||
const n = await Deno.stdin.read(buf);
|
|
||||||
if (n === null) break;
|
|
||||||
|
|
||||||
let i = 0;
|
|
||||||
while (i < n) {
|
|
||||||
const byte = buf[i];
|
|
||||||
|
|
||||||
// Ctrl+C
|
|
||||||
if (byte === 3) {
|
|
||||||
this.dispatchEvent(new Event("exit"));
|
|
||||||
await Deno.stdin.setRaw(false);
|
|
||||||
Deno.exit(130);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enter
|
|
||||||
if (byte === 13) {
|
|
||||||
this.dispatchEvent(new Event("enter"));
|
|
||||||
i++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Backspace
|
|
||||||
if (byte === 127 || byte === 8) {
|
|
||||||
this.dispatchEvent(new Event("backspace"));
|
|
||||||
i++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Escape sequences
|
|
||||||
if (byte === 27 && i + 1 < n && buf[i + 1] === 91) {
|
|
||||||
const code = buf[i + 2];
|
|
||||||
switch (code) {
|
|
||||||
case 65:
|
|
||||||
this.dispatchEvent(new Event("arrow-up"));
|
|
||||||
break;
|
|
||||||
case 66:
|
|
||||||
this.dispatchEvent(new Event("arrow-down"));
|
|
||||||
break;
|
|
||||||
case 67:
|
|
||||||
this.dispatchEvent(new Event("arrow-right"));
|
|
||||||
break;
|
|
||||||
case 68:
|
|
||||||
this.dispatchEvent(new Event("arrow-left"));
|
|
||||||
break;
|
|
||||||
case 51:
|
|
||||||
if (i + 3 < n && buf[i + 3] === 126) {
|
|
||||||
this.dispatchEvent(new Event("delete"));
|
|
||||||
i += 4;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
i += 3;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Printable ASCII
|
|
||||||
if (byte >= 32 && byte <= 126) {
|
|
||||||
this.dispatchEvent(
|
|
||||||
new CLICharEvent({ key: byte, char: String.fromCharCode(byte) }),
|
|
||||||
);
|
|
||||||
i++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unknown
|
|
||||||
this.dispatchEvent(
|
|
||||||
new CLIKeypressEvent({ key: byte, sequence: buf.slice(i, i + 1) }),
|
|
||||||
);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (raw) await Deno.stdin.setRaw(false);
|
|
||||||
}
|
|
||||||
}
|
|
@ -22,7 +22,7 @@ export class TerminalLayout {
|
|||||||
|
|
||||||
Deno.addSignalListener("SIGINT", () => {
|
Deno.addSignalListener("SIGINT", () => {
|
||||||
this.clearAll();
|
this.clearAll();
|
||||||
// Deno.exit(0);
|
Deno.exit(0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,8 +121,7 @@ export class TerminalBlock {
|
|||||||
|
|
||||||
private preserveHistory = false;
|
private preserveHistory = false;
|
||||||
|
|
||||||
constructor(private prepend: string = "") {
|
constructor(private prepend: string = "") {}
|
||||||
}
|
|
||||||
|
|
||||||
setPreserveHistory(preserveHistory: boolean) {
|
setPreserveHistory(preserveHistory: boolean) {
|
||||||
this.preserveHistory = preserveHistory;
|
this.preserveHistory = preserveHistory;
|
||||||
|
@ -6,7 +6,6 @@ import { selectMenuInteractive } from "./selectMenu.ts";
|
|||||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||||
import { cliAlert, cliLog } from "./prompts.ts";
|
import { cliAlert, cliLog } from "./prompts.ts";
|
||||||
import type { ITool } from "../types.ts";
|
import type { ITool } from "../types.ts";
|
||||||
import { InputManager } from "./InputManager.ts";
|
|
||||||
|
|
||||||
// Register tools here (filename, no extension)
|
// Register tools here (filename, no extension)
|
||||||
const toolRegistry: [string, Promise<{ default: ITool }>][] = [
|
const toolRegistry: [string, Promise<{ default: ITool }>][] = [
|
||||||
@ -56,12 +55,6 @@ export class PdfToolsCli {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async run() {
|
public async run() {
|
||||||
const im = InputManager.getInstance();
|
|
||||||
im.activate();
|
|
||||||
im.addEventListener("exit", () => {
|
|
||||||
this.closeMessage = "Exiting...";
|
|
||||||
this.cleanup();
|
|
||||||
});
|
|
||||||
try {
|
try {
|
||||||
await this.importTools();
|
await this.importTools();
|
||||||
const titleBlock = new TerminalBlock();
|
const titleBlock = new TerminalBlock();
|
||||||
@ -85,13 +78,11 @@ export class PdfToolsCli {
|
|||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
this.cleanup();
|
this.cleanup();
|
||||||
Deno.exit(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private cleanup() {
|
private cleanup() {
|
||||||
this.terminalLayout.clearAll();
|
this.terminalLayout.clearAll();
|
||||||
InputManager.getInstance().deactivate();
|
|
||||||
Deno.stdin.setRaw(false);
|
Deno.stdin.setRaw(false);
|
||||||
if (this.closeMessage) console.log(this.closeMessage);
|
if (this.closeMessage) console.log(this.closeMessage);
|
||||||
}
|
}
|
||||||
|
318
cli/prompts.ts
318
cli/prompts.ts
@ -1,117 +1,8 @@
|
|||||||
// deno-lint-disable-must-await-calls
|
// deno-lint-disable-must-await-calls
|
||||||
|
import { log } from "util/logfile.ts";
|
||||||
import { Cursor } from "./cursor.ts";
|
import { Cursor } from "./cursor.ts";
|
||||||
import { colorize } from "./style.ts";
|
import { colorize } from "./style.ts";
|
||||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||||
import { type CLICharEvent, InputManager } from "./InputManager.ts";
|
|
||||||
|
|
||||||
// export async function cliPrompt(
|
|
||||||
// message: string,
|
|
||||||
// block?: TerminalBlock,
|
|
||||||
// ): Promise<string> {
|
|
||||||
// const encoder = new TextEncoder();
|
|
||||||
// const input: string[] = [];
|
|
||||||
// let cursorPos = 0;
|
|
||||||
|
|
||||||
// await Deno.stdin.setRaw(true);
|
|
||||||
|
|
||||||
// const cursorVisible = Cursor["visible"];
|
|
||||||
// Cursor.show();
|
|
||||||
|
|
||||||
// let range: [number, number] = [0, 1];
|
|
||||||
// if (block) {
|
|
||||||
// range = block.setLines([message + " "]);
|
|
||||||
// } else {
|
|
||||||
// Deno.stdout.writeSync(encoder.encode(message + " "));
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const render = () => {
|
|
||||||
// const line = message + " " + input.join("");
|
|
||||||
// const moveTo = `\x1b[${message.length + 2 + cursorPos}G`;
|
|
||||||
|
|
||||||
// if (block) {
|
|
||||||
// block.setPostRenderAction(function () {
|
|
||||||
// Deno.stdout.writeSync(
|
|
||||||
// encoder.encode(`\x1b[${this["lastRenderRow"]};1H`),
|
|
||||||
// );
|
|
||||||
// Deno.stdout.writeSync(encoder.encode(moveTo));
|
|
||||||
// });
|
|
||||||
// range = block.setLines([line], range);
|
|
||||||
// } else {
|
|
||||||
// Deno.stdout.writeSync(encoder.encode("\x1b[K" + line + moveTo));
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// render();
|
|
||||||
|
|
||||||
// const buf = new Uint8Array(64); // large enough for most pastes
|
|
||||||
// inputLoop:
|
|
||||||
// while (true) {
|
|
||||||
// const n = await Deno.stdin.read(buf);
|
|
||||||
// if (n === null) break;
|
|
||||||
|
|
||||||
// for (let i = 0; i < n; i++) {
|
|
||||||
// const byte = buf[i];
|
|
||||||
|
|
||||||
// // Ctrl+C
|
|
||||||
// if (byte === 3) {
|
|
||||||
// block?.clear();
|
|
||||||
// block?.["layout"]?.clearAll();
|
|
||||||
// await Deno.stdin.setRaw(false);
|
|
||||||
// Deno.exit(130);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (byte === 13) { // Enter
|
|
||||||
// break inputLoop;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Escape sequence?
|
|
||||||
// if (byte === 27 && i + 1 < n && buf[i + 1] === 91) {
|
|
||||||
// const third = buf[i + 2];
|
|
||||||
// if (third === 68 && cursorPos > 0) cursorPos--; // Left
|
|
||||||
// else if (third === 67 && cursorPos < input.length) cursorPos++; // Right
|
|
||||||
// else if (third === 51 && i + 3 < n && buf[i + 3] === 126) { // Delete
|
|
||||||
// if (cursorPos < input.length) input.splice(cursorPos, 1);
|
|
||||||
// i += 1; // consume tilde
|
|
||||||
// }
|
|
||||||
// i += 2; // consume ESC [ X
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Backspace
|
|
||||||
// if (byte === 127 || byte === 8) {
|
|
||||||
// if (cursorPos > 0) {
|
|
||||||
// input.splice(cursorPos - 1, 1);
|
|
||||||
// cursorPos--;
|
|
||||||
// }
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Delete (ASCII 46)
|
|
||||||
// if (byte === 46 && cursorPos < input.length) {
|
|
||||||
// input.splice(cursorPos, 1);
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Printable
|
|
||||||
// if (byte >= 32 && byte <= 126) {
|
|
||||||
// input.splice(cursorPos, 0, String.fromCharCode(byte));
|
|
||||||
// cursorPos++;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Other cases: ignore
|
|
||||||
// }
|
|
||||||
|
|
||||||
// render();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await Deno.stdin.setRaw(false);
|
|
||||||
// if (!cursorVisible) {
|
|
||||||
// Cursor.hide();
|
|
||||||
// }
|
|
||||||
// Deno.stdout.writeSync(encoder.encode("\n"));
|
|
||||||
|
|
||||||
// return input.join("");
|
|
||||||
// }
|
|
||||||
|
|
||||||
export async function cliPrompt(
|
export async function cliPrompt(
|
||||||
message: string,
|
message: string,
|
||||||
@ -120,140 +11,121 @@ export async function cliPrompt(
|
|||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
const input: string[] = [];
|
const input: string[] = [];
|
||||||
let cursorPos = 0;
|
let cursorPos = 0;
|
||||||
let range: [number, number] | undefined;
|
|
||||||
|
|
||||||
|
await Deno.stdin.setRaw(true);
|
||||||
|
|
||||||
|
const cursorVisible = Cursor["visible"];
|
||||||
Cursor.show();
|
Cursor.show();
|
||||||
|
|
||||||
const im = InputManager.getInstance();
|
let range: [number, number] = [0, 1];
|
||||||
im.activate();
|
if (block) {
|
||||||
|
range = block.setLines([message + " "]);
|
||||||
|
} else {
|
||||||
|
Deno.stdout.writeSync(encoder.encode(message + " "));
|
||||||
|
}
|
||||||
|
|
||||||
const render = () => {
|
const render = () => {
|
||||||
const line = message + " " + input.join("");
|
const line = message + " " + input.join("");
|
||||||
const moveTo = `\x1b[${message.length + 2 + cursorPos}G`;
|
const moveTo = `\x1b[${message.length + 2 + cursorPos}G`;
|
||||||
|
|
||||||
if (block) {
|
if (block) {
|
||||||
block.setPostRenderAction(() => {
|
block.setPostRenderAction(function () {
|
||||||
|
Deno.stdout.writeSync(
|
||||||
|
encoder.encode(`\x1b[${this["lastRenderRow"]};1H`),
|
||||||
|
);
|
||||||
Deno.stdout.writeSync(encoder.encode(moveTo));
|
Deno.stdout.writeSync(encoder.encode(moveTo));
|
||||||
});
|
});
|
||||||
range = block.setLines([line], range);
|
range = block.setLines([line], range);
|
||||||
} else {
|
} else {
|
||||||
Deno.stdout.writeSync(encoder.encode("\r\x1b[K" + line + moveTo));
|
Deno.stdout.writeSync(encoder.encode("\x1b[K" + line + moveTo));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const exit = () => {
|
|
||||||
im.removeEventListener("enter", onEnter);
|
|
||||||
im.removeEventListener("backspace", onBackspace);
|
|
||||||
im.removeEventListener("delete", onDelete);
|
|
||||||
im.removeEventListener("arrow-left", onLeft);
|
|
||||||
im.removeEventListener("arrow-right", onRight);
|
|
||||||
im.removeEventListener("char", onKey);
|
|
||||||
Cursor.hide();
|
|
||||||
};
|
|
||||||
|
|
||||||
let resolve: null | ((value: string) => void) = null;
|
|
||||||
|
|
||||||
const onEnter = () => {
|
|
||||||
exit();
|
|
||||||
resolve?.(input.join(""));
|
|
||||||
};
|
|
||||||
|
|
||||||
const onBackspace = () => {
|
|
||||||
if (cursorPos > 0) {
|
|
||||||
input.splice(cursorPos - 1, 1);
|
|
||||||
cursorPos--;
|
|
||||||
render();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const onDelete = () => {
|
|
||||||
if (cursorPos < input.length) {
|
|
||||||
input.splice(cursorPos, 1);
|
|
||||||
render();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const onLeft = () => {
|
|
||||||
if (cursorPos > 0) {
|
|
||||||
cursorPos--;
|
|
||||||
render();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const onRight = () => {
|
|
||||||
if (cursorPos < input.length) {
|
|
||||||
cursorPos++;
|
|
||||||
render();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const onKey = (e: Event) => {
|
|
||||||
const ke = (e as CLICharEvent).detail;
|
|
||||||
input.splice(cursorPos, 0, ke.char);
|
|
||||||
cursorPos++;
|
|
||||||
render();
|
|
||||||
};
|
|
||||||
|
|
||||||
render();
|
render();
|
||||||
|
|
||||||
return await new Promise<string>((res) => {
|
const buf = new Uint8Array(64); // large enough for most pastes
|
||||||
resolve = res;
|
inputLoop:
|
||||||
im.addEventListener("enter", onEnter);
|
while (true) {
|
||||||
im.addEventListener("backspace", onBackspace);
|
const n = await Deno.stdin.read(buf);
|
||||||
im.addEventListener("delete", onDelete);
|
if (n === null) break;
|
||||||
im.addEventListener("arrow-left", onLeft);
|
|
||||||
im.addEventListener("arrow-right", onRight);
|
for (let i = 0; i < n; i++) {
|
||||||
im.addEventListener("char", onKey);
|
const byte = buf[i];
|
||||||
});
|
|
||||||
|
// Ctrl+C
|
||||||
|
if (byte === 3) {
|
||||||
|
block?.clear();
|
||||||
|
block?.["layout"]?.clearAll();
|
||||||
|
await Deno.stdin.setRaw(false);
|
||||||
|
Deno.exit(130);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (byte === 13) { // Enter
|
||||||
|
break inputLoop;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escape sequence?
|
||||||
|
if (byte === 27 && i + 1 < n && buf[i + 1] === 91) {
|
||||||
|
const third = buf[i + 2];
|
||||||
|
if (third === 68 && cursorPos > 0) cursorPos--; // Left
|
||||||
|
else if (third === 67 && cursorPos < input.length) cursorPos++; // Right
|
||||||
|
else if (third === 51 && i + 3 < n && buf[i + 3] === 126) { // Delete
|
||||||
|
if (cursorPos < input.length) input.splice(cursorPos, 1);
|
||||||
|
i += 1; // consume tilde
|
||||||
|
}
|
||||||
|
i += 2; // consume ESC [ X
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backspace
|
||||||
|
if (byte === 127 || byte === 8) {
|
||||||
|
if (cursorPos > 0) {
|
||||||
|
input.splice(cursorPos - 1, 1);
|
||||||
|
cursorPos--;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete (ASCII 46)
|
||||||
|
if (byte === 46 && cursorPos < input.length) {
|
||||||
|
input.splice(cursorPos, 1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Printable
|
||||||
|
if (byte >= 32 && byte <= 126) {
|
||||||
|
input.splice(cursorPos, 0, String.fromCharCode(byte));
|
||||||
|
cursorPos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Other cases: ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
await Deno.stdin.setRaw(false);
|
||||||
|
if (!cursorVisible) {
|
||||||
|
Cursor.hide();
|
||||||
|
}
|
||||||
|
Deno.stdout.writeSync(encoder.encode("\n"));
|
||||||
|
|
||||||
|
return input.join("");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function cliConfirm(message: string, block?: TerminalBlock) {
|
export function cliConfirm(message: string, block?: TerminalBlock) {
|
||||||
const im = InputManager.getInstance();
|
return cliPrompt(message + " (y/n)", block).then((v) =>
|
||||||
let inpout = "";
|
v.toLowerCase() === "y"
|
||||||
function isValidInput(input: string) {
|
|
||||||
switch (input) {
|
|
||||||
case "y":
|
|
||||||
case "n":
|
|
||||||
return inpout.length === 0;
|
|
||||||
case "e":
|
|
||||||
return inpout === "y";
|
|
||||||
case "s":
|
|
||||||
return inpout === "ye";
|
|
||||||
case "o":
|
|
||||||
return inpout === "n";
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function onKey(e: CLICharEvent) {
|
|
||||||
const ke = e.detail;
|
|
||||||
const char = String.fromCharCode(ke.key);
|
|
||||||
if (isValidInput(char)) {
|
|
||||||
inpout += char;
|
|
||||||
} else {
|
|
||||||
e.stopImmediatePropagation();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
im.addEventListener("char", onKey);
|
|
||||||
const value = await cliPrompt(message + " (y/n)", block).then((v) =>
|
|
||||||
v.charAt(0).toLowerCase() === "y"
|
|
||||||
);
|
);
|
||||||
im.removeEventListener("char", onKey);
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function cliAlert(message: string, block?: TerminalBlock) {
|
export function cliAlert(message: string, block?: TerminalBlock) {
|
||||||
const im = InputManager.getInstance();
|
return cliPrompt(
|
||||||
const onKey = (e: CLICharEvent) => {
|
|
||||||
e.stopImmediatePropagation();
|
|
||||||
};
|
|
||||||
im.addEventListener("char", onKey);
|
|
||||||
await cliPrompt(
|
|
||||||
message + colorize(" Press Enter to continue", "gray"),
|
message + colorize(" Press Enter to continue", "gray"),
|
||||||
block,
|
block,
|
||||||
);
|
).then((v) => {
|
||||||
im.removeEventListener("char", onKey);
|
return v;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function cliLog(
|
export function cliLog(
|
||||||
@ -286,16 +158,10 @@ if (import.meta.main) {
|
|||||||
layout.register("block", block);
|
layout.register("block", block);
|
||||||
layout.register("footer", footer);
|
layout.register("footer", footer);
|
||||||
|
|
||||||
InputManager.addEventListener("exit", () => {
|
|
||||||
layout.clearAll();
|
|
||||||
// console.clear();
|
|
||||||
Deno.exit(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
Deno.addSignalListener("SIGINT", () => {
|
Deno.addSignalListener("SIGINT", () => {
|
||||||
layout.clearAll();
|
layout.clearAll();
|
||||||
// console.clear();
|
// console.clear();
|
||||||
// Deno.exit(0);
|
Deno.exit(0);
|
||||||
});
|
});
|
||||||
const name = await cliPrompt("Enter your name:", block);
|
const name = await cliPrompt("Enter your name:", block);
|
||||||
cliLog(`Hello, ${name}!`, block);
|
cliLog(`Hello, ${name}!`, block);
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import type { callback } from "../types.ts";
|
import type { callback } from "../types.ts";
|
||||||
import { type CLICharEvent, InputManager } from "./InputManager.ts";
|
|
||||||
import { cliLog } from "./prompts.ts";
|
|
||||||
import { colorize } from "./style.ts";
|
import { colorize } from "./style.ts";
|
||||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
import { TerminalBlock } from "./TerminalLayout.ts";
|
||||||
|
|
||||||
interface ISelectMenuConfig {
|
interface ISelectMenuConfig {
|
||||||
terminalBlock?: TerminalBlock;
|
terminalBlock?: TerminalBlock;
|
||||||
@ -64,65 +62,50 @@ export async function selectMenuInteractive(
|
|||||||
|
|
||||||
let inputBuffer = "";
|
let inputBuffer = "";
|
||||||
|
|
||||||
const im = InputManager.getInstance();
|
// Function to handle input
|
||||||
im.activate();
|
async function handleInput() {
|
||||||
|
const buf = new Uint8Array(3); // arrow keys send 3 bytes
|
||||||
|
while (true) {
|
||||||
|
renderMenu();
|
||||||
|
const n = await Deno.stdin.read(buf);
|
||||||
|
if (n === null) break;
|
||||||
|
|
||||||
const onUp = (e: Event) => {
|
const [a, b, c] = buf;
|
||||||
e.stopImmediatePropagation();
|
|
||||||
selected = (selected - 1 + options.length) % options.length;
|
|
||||||
renderMenu();
|
|
||||||
};
|
|
||||||
|
|
||||||
const onDown = (e: Event) => {
|
if (a === 3) {
|
||||||
e.stopImmediatePropagation();
|
Deno.stdin.setRaw(false);
|
||||||
selected = (selected + 1) % options.length;
|
terminalBlock?.["layout"]?.clearAll();
|
||||||
renderMenu();
|
Deno.exit(130);
|
||||||
};
|
}
|
||||||
|
|
||||||
const onKey = (e: CLICharEvent) => {
|
if (a === 13) { // Enter key
|
||||||
e.stopImmediatePropagation();
|
if (inputBuffer) {
|
||||||
const ke = e.detail;
|
const parsed = parseInt(inputBuffer);
|
||||||
const char = String.fromCharCode(ke.key);
|
if (!isNaN(parsed)) {
|
||||||
inputBuffer += char;
|
selected = parsed - 1;
|
||||||
};
|
}
|
||||||
|
inputBuffer = "";
|
||||||
const onBackspace = (e: Event) => {
|
}
|
||||||
e.stopImmediatePropagation();
|
break;
|
||||||
inputBuffer = inputBuffer.slice(0, -1);
|
} else if (a === 27 && b === 91) { // Arrow keys
|
||||||
};
|
inputBuffer = "";
|
||||||
|
if (c === 65) { // Up
|
||||||
let resolve: null | ((value: string) => void) = null;
|
selected = (selected - 1 + options.length) % options.length;
|
||||||
|
} else if (c === 66) { // Down
|
||||||
const onEnter = (e: Event) => {
|
selected = (selected + 1) % options.length;
|
||||||
e.stopImmediatePropagation();
|
}
|
||||||
if (inputBuffer) {
|
} else if (a >= 48 && a <= 57) {
|
||||||
const parsed = parseInt(inputBuffer);
|
inputBuffer += String.fromCharCode(a);
|
||||||
if (!isNaN(parsed)) {
|
} else if (a === 8) {
|
||||||
selected = parsed - 1;
|
inputBuffer = inputBuffer.slice(0, -1);
|
||||||
}
|
}
|
||||||
inputBuffer = "";
|
|
||||||
}
|
}
|
||||||
im.removeEventListener("arrow-up", onUp);
|
|
||||||
im.removeEventListener("arrow-down", onDown);
|
|
||||||
im.removeEventListener("char", onKey);
|
|
||||||
im.removeEventListener("backspace", onBackspace);
|
|
||||||
im.removeEventListener("enter", onEnter);
|
|
||||||
resolve?.(options[selected]);
|
|
||||||
};
|
|
||||||
|
|
||||||
renderMenu();
|
|
||||||
await new Promise<string>((res) => {
|
|
||||||
resolve = res;
|
|
||||||
im.addEventListener("char", onKey);
|
|
||||||
im.addEventListener("backspace", onBackspace);
|
|
||||||
im.addEventListener("enter", onEnter);
|
|
||||||
im.addEventListener("arrow-up", onUp);
|
|
||||||
im.addEventListener("arrow-down", onDown);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
Deno.stdin.setRaw(false);
|
||||||
|
return options[selected];
|
||||||
|
}
|
||||||
terminalBlock.setLines(["Selected: " + options[selected]], range);
|
terminalBlock.setLines(["Selected: " + options[selected]], range);
|
||||||
|
return await handleInput();
|
||||||
return options[selected];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function multiSelectMenuInteractive(
|
export async function multiSelectMenuInteractive(
|
||||||
@ -134,7 +117,9 @@ export async function multiSelectMenuInteractive(
|
|||||||
let selected = 0;
|
let selected = 0;
|
||||||
let selectedOptions: number[] = config?.initialSelections || [];
|
let selectedOptions: number[] = config?.initialSelections || [];
|
||||||
|
|
||||||
const rawValues = options.map((i) => typeof i === "string" ? i : i[0]);
|
const rawValues = new Set(
|
||||||
|
options.map((i) => typeof i === "string" ? i : i[0]),
|
||||||
|
).values().toArray();
|
||||||
|
|
||||||
if (rawValues.length !== options.length) {
|
if (rawValues.length !== options.length) {
|
||||||
throw new Error("Duplicate options in multi-select menu");
|
throw new Error("Duplicate options in multi-select menu");
|
||||||
@ -173,52 +158,44 @@ export async function multiSelectMenuInteractive(
|
|||||||
range = terminalBlock.setLines(lines, range);
|
range = terminalBlock.setLines(lines, range);
|
||||||
}
|
}
|
||||||
|
|
||||||
const im = InputManager.getInstance();
|
// Function to handle input
|
||||||
im.activate();
|
async function handleInput() {
|
||||||
|
const buf = new Uint8Array(3); // arrow keys send 3 bytes
|
||||||
|
while (true) {
|
||||||
|
renderMenu();
|
||||||
|
const n = await Deno.stdin.read(buf);
|
||||||
|
if (n === null) break;
|
||||||
|
|
||||||
let resolve = null as null | ((value: number[]) => void);
|
const [a, b, c] = buf;
|
||||||
|
|
||||||
const onUp = (e: Event) => {
|
if (a === 3) {
|
||||||
e.stopImmediatePropagation();
|
Deno.stdin.setRaw(false);
|
||||||
selected = (selected - 1 + options.length) % options.length;
|
terminalBlock?.["layout"]?.clearAll();
|
||||||
renderMenu();
|
Deno.exit(130);
|
||||||
};
|
}
|
||||||
|
|
||||||
const onDown = (e: Event) => {
|
if (a === 13) { // Enter key
|
||||||
e.stopImmediatePropagation();
|
break;
|
||||||
selected = (selected + 1) % options.length;
|
} else if (a === 27 && b === 91) { // Arrow keys
|
||||||
renderMenu();
|
if (c === 65) { // Up
|
||||||
};
|
selected = (selected - 1 + options.length) % options.length;
|
||||||
|
} else if (c === 66) { // Down
|
||||||
const onSpace = (e: CLICharEvent) => {
|
selected = (selected + 1) % options.length;
|
||||||
if (e.detail.char !== " ") return;
|
}
|
||||||
e.stopImmediatePropagation();
|
} else if (a === 32) { // Space
|
||||||
if (selectedOptions.includes(selected)) {
|
Deno.stdout.writeSync(new TextEncoder().encode("\x07"));
|
||||||
selectedOptions = selectedOptions.filter((i) => i !== selected);
|
if (selectedOptions.includes(selected)) {
|
||||||
} else {
|
selectedOptions = selectedOptions.filter((i) => i !== selected);
|
||||||
selectedOptions.push(selected);
|
} else {
|
||||||
|
selectedOptions.push(selected);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
renderMenu();
|
|
||||||
};
|
|
||||||
|
|
||||||
const onEnter = (e: Event) => {
|
Deno.stdin.setRaw(false);
|
||||||
e.stopImmediatePropagation();
|
return selectedOptions;
|
||||||
resolve?.(selectedOptions);
|
}
|
||||||
im.removeEventListener("arrow-up", onUp);
|
const selections = await handleInput();
|
||||||
im.removeEventListener("arrow-down", onDown);
|
|
||||||
im.removeEventListener("char", onSpace);
|
|
||||||
im.removeEventListener("enter", onEnter);
|
|
||||||
};
|
|
||||||
|
|
||||||
renderMenu();
|
|
||||||
|
|
||||||
const selections = await new Promise<number[]>((res) => {
|
|
||||||
resolve = res;
|
|
||||||
im.addEventListener("arrow-up", onUp);
|
|
||||||
im.addEventListener("arrow-down", onDown);
|
|
||||||
im.addEventListener("char", onSpace);
|
|
||||||
im.addEventListener("enter", onEnter);
|
|
||||||
});
|
|
||||||
for (const optionI of selections) {
|
for (const optionI of selections) {
|
||||||
const option = options[optionI];
|
const option = options[optionI];
|
||||||
if (Array.isArray(option)) {
|
if (Array.isArray(option)) {
|
||||||
@ -231,17 +208,17 @@ export async function multiSelectMenuInteractive(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (import.meta.main) {
|
if (import.meta.main) {
|
||||||
const layout = new TerminalLayout();
|
// const layout = new TerminalLayout();
|
||||||
const block = new TerminalBlock();
|
// const block = new TerminalBlock();
|
||||||
const titleBlock = new TerminalBlock();
|
// const titleBlock = new TerminalBlock();
|
||||||
const postBlock = new TerminalBlock();
|
// const postBlock = new TerminalBlock();
|
||||||
InputManager.addEventListener("exit", () => layout.clearAll());
|
// titleBlock.setLines(["An incredible fruit menu!"]);
|
||||||
titleBlock.setLines(["An incredible fruit menu!"]);
|
// postBlock.setLines(["I'm here too!"]);
|
||||||
postBlock.setLines(["I'm here too!"]);
|
// titleBlock.setFixedHeight(1);
|
||||||
titleBlock.setFixedHeight(1);
|
// postBlock.setFixedHeight(1);
|
||||||
postBlock.setFixedHeight(1);
|
// layout.register("title", titleBlock);
|
||||||
layout.register("title", titleBlock);
|
// layout.register("block", block);
|
||||||
layout.register("block", block);
|
// layout.register("post", postBlock);
|
||||||
|
|
||||||
// const val = await selectMenuInteractive("choose a fruit", [
|
// const val = await selectMenuInteractive("choose a fruit", [
|
||||||
// "apple",
|
// "apple",
|
||||||
@ -299,8 +276,8 @@ if (import.meta.main) {
|
|||||||
"ximenia",
|
"ximenia",
|
||||||
"yuzu",
|
"yuzu",
|
||||||
"zucchini",
|
"zucchini",
|
||||||
], { terminalBlock: block });
|
]);
|
||||||
cliLog(val || "No value");
|
console.log(val);
|
||||||
|
|
||||||
// Deno.stdout.writeSync(new TextEncoder().encode("\x07"));
|
// Deno.stdout.writeSync(new TextEncoder().encode("\x07"));
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@bearmetal/pdf-tools",
|
"name": "@bearmetal/pdf-tools",
|
||||||
"version": "1.0.8-l",
|
"version": "1.0.7",
|
||||||
"license": "GPL 3.0",
|
"license": "GPL 3.0",
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"dev": "deno run -A --env-file=.env main.ts",
|
"dev": "deno run -A --env-file=.env main.ts",
|
||||||
|
5
main.ts
5
main.ts
@ -1,10 +1,5 @@
|
|||||||
/// <reference types="./types.ts" />
|
/// <reference types="./types.ts" />
|
||||||
import { PdfToolsCli } from "./cli/index.ts";
|
import { PdfToolsCli } from "./cli/index.ts";
|
||||||
// import { log } from "util/logfile.ts";
|
|
||||||
|
|
||||||
// try {
|
|
||||||
const app = new PdfToolsCli();
|
const app = new PdfToolsCli();
|
||||||
app.run();
|
app.run();
|
||||||
// } catch (e) {
|
|
||||||
// // log(e);
|
|
||||||
// }
|
|
||||||
|
BIN
testing/test.pdf
BIN
testing/test.pdf
Binary file not shown.
@ -1,23 +1,6 @@
|
|||||||
import {
|
import { type PDFAcroField, type PDFField, PDFName, PDFString } from "pdf-lib";
|
||||||
PDFAcroField,
|
import { loadPdf, loadPdfForm, savePdf } from "util/saveLoadPdf.ts";
|
||||||
PDFAcroTerminal,
|
import { callWithArgPrompt } from "util/call.ts";
|
||||||
PDFArray,
|
|
||||||
PDFCheckBox,
|
|
||||||
PDFContext,
|
|
||||||
PDFDict,
|
|
||||||
type PDFDocument,
|
|
||||||
type PDFField,
|
|
||||||
PDFHexString,
|
|
||||||
PDFName,
|
|
||||||
PDFNumber,
|
|
||||||
type PDFObject,
|
|
||||||
PDFRadioGroup,
|
|
||||||
PDFRef,
|
|
||||||
PDFString,
|
|
||||||
PDFTextField,
|
|
||||||
type PDFWidgetAnnotation,
|
|
||||||
} from "pdf-lib";
|
|
||||||
import { loadPdf, savePdf } from "util/saveLoadPdf.ts";
|
|
||||||
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
import { TerminalBlock } from "../cli/TerminalLayout.ts";
|
||||||
import { forceArgs } from "../cli/forceArgs.ts";
|
import { forceArgs } from "../cli/forceArgs.ts";
|
||||||
import { colorize } from "../cli/style.ts";
|
import { colorize } from "../cli/style.ts";
|
||||||
@ -25,680 +8,65 @@ import { cliAlert, cliLog, cliPrompt } from "../cli/prompts.ts";
|
|||||||
import { multiSelectMenuInteractive } from "../cli/selectMenu.ts";
|
import { multiSelectMenuInteractive } from "../cli/selectMenu.ts";
|
||||||
import type { callback, ITool } from "../types.ts";
|
import type { callback, ITool } from "../types.ts";
|
||||||
import { toCase } from "util/caseManagement.ts";
|
import { toCase } from "util/caseManagement.ts";
|
||||||
import { log } from "util/logfile.ts";
|
|
||||||
|
|
||||||
function removeWidgetFromOldField(
|
async function renameFields(
|
||||||
doc: PDFDocument,
|
path: string,
|
||||||
field: PDFField,
|
pattern: string | RegExp,
|
||||||
widget: PDFWidgetAnnotation,
|
change: string,
|
||||||
) {
|
) {
|
||||||
const maybeKids = field.acroField.dict.get(PDFName.of("Kids"));
|
if (typeof pattern === "string") pattern = new RegExp(pattern);
|
||||||
if (!maybeKids || !(maybeKids instanceof PDFArray)) return;
|
const form = await loadPdfForm(path);
|
||||||
const kids = maybeKids;
|
const fields = form.getFields();
|
||||||
if (!kids) return;
|
let changesMade = false;
|
||||||
|
for (const field of fields) {
|
||||||
const widgetRef = getWidgetRef(widget, doc);
|
const name = field.getName();
|
||||||
if (!widgetRef) return;
|
if (pattern.test(name)) {
|
||||||
|
console.log(name + " %cfound", "color: red");
|
||||||
const updatedKids = kids.asArray().filter((ref) => {
|
const segments = name.split(".");
|
||||||
const dict = doc.context.lookup(ref);
|
const matchingSegments = segments.filter((s) => pattern.test(s));
|
||||||
return dict !== widget.dict;
|
let cField: PDFAcroField | undefined = field.acroField;
|
||||||
});
|
while (cField) {
|
||||||
|
if (
|
||||||
if (updatedKids.length === 0) {
|
cField.getPartialName() &&
|
||||||
// Field is now empty, remove it from the AcroForm
|
matchingSegments.includes(cField.getPartialName()!)
|
||||||
const acroForm = doc.catalog.lookup(PDFName.of("AcroForm"), PDFDict);
|
) {
|
||||||
const fields = acroForm.lookup(PDFName.of("Fields"), PDFArray);
|
const mName = cField.getPartialName()?.replace(pattern, change);
|
||||||
const fieldRef = field.acroField.ref;
|
if (mName) {
|
||||||
const newFields = fields.asArray().filter((ref) => ref !== fieldRef);
|
changesMade = true;
|
||||||
acroForm.set(PDFName.of("Fields"), doc.context.obj(newFields));
|
cField.dict.set(PDFName.of("T"), PDFString.of(mName));
|
||||||
} else {
|
}
|
||||||
field.acroField.dict.set(PDFName.of("Kids"), doc.context.obj(updatedKids));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function moveWidgetToFlatField(
|
|
||||||
doc: PDFDocument,
|
|
||||||
field: PDFField,
|
|
||||||
widget: PDFWidgetAnnotation,
|
|
||||||
newName: string,
|
|
||||||
) {
|
|
||||||
const form = doc.getForm();
|
|
||||||
const page = findPageForWidget(doc, widget);
|
|
||||||
if (!page) throw new Error("Widget's page not found");
|
|
||||||
|
|
||||||
const rect = widget.getRectangle();
|
|
||||||
if (!rect) throw new Error("Widget has no rectangle");
|
|
||||||
|
|
||||||
const fieldType = detectFieldType(field);
|
|
||||||
const widgetRef = getWidgetRef(widget, doc);
|
|
||||||
if (!widgetRef) throw new Error("Widget ref not found");
|
|
||||||
|
|
||||||
// 🔒 Extract value + style before any destructive ops
|
|
||||||
let value: string | undefined;
|
|
||||||
try {
|
|
||||||
if (fieldType === "/Tx" && field instanceof PDFTextField) {
|
|
||||||
value = field.getText();
|
|
||||||
}
|
|
||||||
} catch (_) {
|
|
||||||
log("Failed to extract value from field");
|
|
||||||
}
|
|
||||||
|
|
||||||
const sourceFieldDict = field.acroField.dict;
|
|
||||||
const sourceWidgetDict = widget.dict;
|
|
||||||
|
|
||||||
// 🔥 Remove widget from page + field
|
|
||||||
removeWidgetFromPage(widget, doc);
|
|
||||||
removeWidgetCompletely(doc, widget, field);
|
|
||||||
|
|
||||||
// 🔥 Carefully remove field + parents
|
|
||||||
try {
|
|
||||||
fullyDeleteFieldHierarchy(doc, field);
|
|
||||||
} catch (_) {
|
|
||||||
// fallback
|
|
||||||
log("Failed to remove field hierarchy");
|
|
||||||
removeFieldIfEmpty(doc, field);
|
|
||||||
}
|
|
||||||
|
|
||||||
sanitizeFieldsTree(doc);
|
|
||||||
removeDanglingParents(doc);
|
|
||||||
removeEmptyAncestors(doc, field);
|
|
||||||
|
|
||||||
// 🔁 Create replacement field
|
|
||||||
let newField: PDFField;
|
|
||||||
|
|
||||||
switch (fieldType) {
|
|
||||||
case "/Tx": {
|
|
||||||
const tf = form.createTextField(newName);
|
|
||||||
if (value) tf.setText(value);
|
|
||||||
tf.addToPage(page, rect);
|
|
||||||
newField = tf;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case "/Btn": {
|
|
||||||
const isRadio = getFlag(field, 15);
|
|
||||||
if (isRadio) {
|
|
||||||
const rg = form.createRadioGroup(newName);
|
|
||||||
rg.addOptionToPage(newName, page, rect);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
const cb = form.createCheckBox(newName);
|
|
||||||
cb.addToPage(page, rect);
|
|
||||||
if (field instanceof PDFCheckBox && field.isChecked()) {
|
|
||||||
cb.check();
|
|
||||||
}
|
}
|
||||||
return;
|
cField = cField.getParent();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case "/Ch": {
|
|
||||||
const ff = sourceFieldDict.get(PDFName.of("Ff"));
|
|
||||||
const isCombo = ff instanceof PDFNumber &&
|
|
||||||
((ff.asNumber() & (1 << 17)) !== 0);
|
|
||||||
const opts = sourceFieldDict.lookupMaybe(PDFName.of("Opt"), PDFArray);
|
|
||||||
const values =
|
|
||||||
opts?.asArray().map((opt) =>
|
|
||||||
opt instanceof PDFString || opt instanceof PDFHexString
|
|
||||||
? opt.decodeText()
|
|
||||||
: ""
|
|
||||||
) ?? [];
|
|
||||||
|
|
||||||
if (isCombo) {
|
|
||||||
const dd = form.createDropdown(newName);
|
|
||||||
dd.addOptions(values);
|
|
||||||
dd.addToPage(page, rect);
|
|
||||||
newField = dd;
|
|
||||||
} else {
|
|
||||||
const ol = form.createOptionList(newName);
|
|
||||||
ol.addOptions(values);
|
|
||||||
ol.addToPage(page, rect);
|
|
||||||
newField = ol;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new Error(`Unsupported field type: ${fieldType}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 🔧 Apply styles *after creation*
|
|
||||||
const targetWidgetDict = newField.acroField.getWidgets()[0].dict;
|
|
||||||
copyFieldAndWidgetStyles(
|
|
||||||
sourceFieldDict,
|
|
||||||
sourceWidgetDict,
|
|
||||||
newField.acroField.dict,
|
|
||||||
targetWidgetDict,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeDanglingParents(doc: PDFDocument) {
|
|
||||||
const context = doc.context;
|
|
||||||
const acroForm = doc.catalog.lookup(PDFName.of("AcroForm"), PDFDict);
|
|
||||||
const fields = acroForm.lookupMaybe(PDFName.of("Fields"), PDFArray);
|
|
||||||
if (!(fields instanceof PDFArray)) return;
|
|
||||||
|
|
||||||
function fixFieldDict(dict: PDFDict) {
|
|
||||||
const parentRef = dict.get(PDFName.of("Parent"));
|
|
||||||
if (!parentRef || !(parentRef instanceof PDFRef)) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const parentDict = context.lookup(parentRef, PDFDict);
|
|
||||||
if (!parentDict) throw new Error("Missing parent");
|
|
||||||
} catch {
|
|
||||||
// Parent is broken — remove reference
|
|
||||||
dict.delete(PDFName.of("Parent"));
|
|
||||||
log("Broken parent reference removed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const visited = new Set<string>();
|
|
||||||
|
|
||||||
function recurseKids(dict: PDFDict) {
|
|
||||||
const kids = dict.lookupMaybe(PDFName.of("Kids"), PDFArray);
|
|
||||||
if (!(kids instanceof PDFArray)) return;
|
|
||||||
|
|
||||||
for (const kidRef of kids.asArray()) {
|
|
||||||
if (!(kidRef instanceof PDFRef)) continue;
|
|
||||||
const key = kidRef.toString();
|
|
||||||
if (visited.has(key)) continue;
|
|
||||||
visited.add(key);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const kidDict = context.lookup(kidRef, PDFDict);
|
|
||||||
fixFieldDict(kidDict);
|
|
||||||
recurseKids(kidDict);
|
|
||||||
} catch (e) {
|
|
||||||
context.delete(kidRef); // nuke broken reference
|
|
||||||
log("Broken kid reference removed");
|
|
||||||
log(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (changesMade) {
|
||||||
for (const ref of fields.asArray()) {
|
savePdf(form.doc, path);
|
||||||
if (!(ref instanceof PDFRef)) continue;
|
|
||||||
try {
|
|
||||||
const dict = context.lookup(ref, PDFDict);
|
|
||||||
fixFieldDict(dict);
|
|
||||||
recurseKids(dict);
|
|
||||||
} catch {
|
|
||||||
context.delete(ref); // broken root
|
|
||||||
log("Broken root reference removed");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeFieldByName(doc: PDFDocument, fieldName: string) {
|
function applyRename(
|
||||||
const form = doc.getForm();
|
|
||||||
const acroForm = doc.catalog.lookup(PDFName.of("AcroForm"), PDFDict);
|
|
||||||
const fields = acroForm.lookup(PDFName.of("Fields"), PDFArray);
|
|
||||||
const context = doc.context;
|
|
||||||
|
|
||||||
const remainingFields = fields.asArray().filter((ref) => {
|
|
||||||
const dict = context.lookup(ref, PDFDict);
|
|
||||||
const name = dict?.get(PDFName.of("T"));
|
|
||||||
|
|
||||||
if (name && (name.decodeText?.() === fieldName)) {
|
|
||||||
context.delete(ref as PDFRef);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
acroForm.set(PDFName.of("Fields"), context.obj(remainingFields));
|
|
||||||
}
|
|
||||||
|
|
||||||
function sanitizeFieldsTree(doc: PDFDocument) {
|
|
||||||
const context = doc.context;
|
|
||||||
const acroForm = doc.catalog.lookup(PDFName.of("AcroForm"), PDFDict);
|
|
||||||
const fields = acroForm.lookupMaybe(PDFName.of("Fields"), PDFArray);
|
|
||||||
if (!(fields instanceof PDFArray)) return;
|
|
||||||
|
|
||||||
function pruneInvalidKids(dict: PDFDict, context: PDFContext) {
|
|
||||||
const kids = dict.lookupMaybe(PDFName.of("Kids"), PDFArray);
|
|
||||||
if (!(kids instanceof PDFArray)) return;
|
|
||||||
|
|
||||||
const validKids: PDFRef[] = [];
|
|
||||||
|
|
||||||
for (const ref of kids.asArray()) {
|
|
||||||
// 💥 Defensive: skip anything that's not a real PDFRef
|
|
||||||
if (!ref || !(ref instanceof PDFRef)) continue;
|
|
||||||
|
|
||||||
let child: PDFDict | undefined;
|
|
||||||
try {
|
|
||||||
child = context.lookup(ref, PDFDict);
|
|
||||||
} catch (e) {
|
|
||||||
context.delete(ref);
|
|
||||||
log("Broken kid reference removed");
|
|
||||||
log(e);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!child) {
|
|
||||||
context.delete(ref);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const t = child.get(PDFName.of("T"));
|
|
||||||
if (!(t instanceof PDFString || t instanceof PDFHexString)) {
|
|
||||||
context.delete(ref);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recurse, but protect inner layers too
|
|
||||||
pruneInvalidKids(child, context);
|
|
||||||
validKids.push(ref);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (validKids.length > 0) {
|
|
||||||
dict.set(PDFName.of("Kids"), context.obj(validKids));
|
|
||||||
} else {
|
|
||||||
dict.delete(PDFName.of("Kids"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const validFields: PDFRef[] = [];
|
|
||||||
|
|
||||||
for (const ref of fields.asArray()) {
|
|
||||||
if (!ref || !(ref instanceof PDFRef)) continue;
|
|
||||||
|
|
||||||
let dict: PDFDict | undefined;
|
|
||||||
try {
|
|
||||||
dict = context.lookup(ref, PDFDict);
|
|
||||||
} catch {
|
|
||||||
context.delete(ref);
|
|
||||||
log("Broken field reference removed");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!dict) {
|
|
||||||
context.delete(ref);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const t = dict.get(PDFName.of("T"));
|
|
||||||
if (!(t instanceof PDFString || t instanceof PDFHexString)) {
|
|
||||||
context.delete(ref);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
pruneInvalidKids(dict, context);
|
|
||||||
validFields.push(ref);
|
|
||||||
}
|
|
||||||
|
|
||||||
acroForm.set(PDFName.of("Fields"), context.obj(validFields));
|
|
||||||
}
|
|
||||||
|
|
||||||
function fullyDeleteFieldHierarchy(doc: PDFDocument, rootField: PDFField) {
|
|
||||||
const context = doc.context;
|
|
||||||
const acroForm = doc.catalog.lookup(PDFName.of("AcroForm"), PDFDict);
|
|
||||||
const fields = acroForm.lookup(PDFName.of("Fields"), PDFArray);
|
|
||||||
|
|
||||||
function recurseDelete(dict: PDFDict, ref: PDFRef) {
|
|
||||||
const kids = dict.lookupMaybe(PDFName.of("Kids"), PDFArray);
|
|
||||||
|
|
||||||
if (kids instanceof PDFArray) {
|
|
||||||
for (const kidRef of kids.asArray()) {
|
|
||||||
const kidDict = context.lookup(kidRef, PDFDict);
|
|
||||||
if (kidDict) {
|
|
||||||
recurseDelete(kidDict, kidRef as PDFRef);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
context.delete(ref);
|
|
||||||
}
|
|
||||||
|
|
||||||
recurseDelete(rootField.acroField.dict, rootField.acroField.ref);
|
|
||||||
|
|
||||||
// Remove root from AcroForm.Fields
|
|
||||||
const newFields = fields
|
|
||||||
.asArray()
|
|
||||||
.filter((ref) => ref !== rootField.acroField.ref);
|
|
||||||
|
|
||||||
acroForm.set(PDFName.of("Fields"), context.obj(newFields));
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeEmptyAncestors(doc: PDFDocument, field: PDFField) {
|
|
||||||
let current: PDFAcroField | undefined = field.acroField;
|
|
||||||
const context = doc.context;
|
|
||||||
|
|
||||||
while (current) {
|
|
||||||
const parent = current.getParent();
|
|
||||||
|
|
||||||
const kids = parent?.dict.lookupMaybe(PDFName.of("Kids"), PDFArray);
|
|
||||||
if (kids instanceof PDFArray) {
|
|
||||||
const remaining = kids.asArray().filter((ref) => {
|
|
||||||
try {
|
|
||||||
const kidDict = context.lookup(ref, PDFDict);
|
|
||||||
return kidDict !== current?.dict;
|
|
||||||
} catch (e) {
|
|
||||||
log("Broken kid reference removed");
|
|
||||||
log(e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (remaining.length > 0) {
|
|
||||||
parent.dict.set(PDFName.of("Kids"), context.obj(remaining));
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
parent.dict.delete(PDFName.of("Kids"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
context.delete(current.ref);
|
|
||||||
current = parent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeWidgetCompletely(
|
|
||||||
doc: PDFDocument,
|
|
||||||
widget: PDFWidgetAnnotation,
|
|
||||||
field: PDFField,
|
field: PDFField,
|
||||||
) {
|
name: string,
|
||||||
const widgetRef = getWidgetRef(widget, doc);
|
|
||||||
if (!widgetRef) return;
|
|
||||||
|
|
||||||
// 1. Remove from field's /Kids array
|
|
||||||
const kidsRaw = field.acroField.dict.get(PDFName.of("Kids"));
|
|
||||||
if (kidsRaw instanceof PDFArray) {
|
|
||||||
const updatedKids = kidsRaw.asArray().filter((ref) => {
|
|
||||||
const dict = doc.context.lookup(ref);
|
|
||||||
return dict !== widget.dict;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (updatedKids.length > 0) {
|
|
||||||
field.acroField.dict.set(
|
|
||||||
PDFName.of("Kids"),
|
|
||||||
doc.context.obj(updatedKids),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
field.acroField.dict.delete(PDFName.of("Kids"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Remove from page /Annots
|
|
||||||
for (const page of doc.getPages()) {
|
|
||||||
const annotsRaw = page.node.Annots()?.asArray();
|
|
||||||
if (!annotsRaw) continue;
|
|
||||||
|
|
||||||
const remainingAnnots = annotsRaw.filter((ref) => {
|
|
||||||
const dict = doc.context.lookup(ref);
|
|
||||||
return dict !== widget.dict;
|
|
||||||
});
|
|
||||||
|
|
||||||
page.node.set(PDFName.of("Annots"), doc.context.obj(remainingAnnots));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Optional: delete the widget from the context
|
|
||||||
doc.context.delete(widgetRef);
|
|
||||||
}
|
|
||||||
function removeFieldIfEmpty(doc: PDFDocument, field: PDFField) {
|
|
||||||
const kids = field.acroField.getWidgets();
|
|
||||||
if (kids.length > 0) return;
|
|
||||||
|
|
||||||
const acroForm = doc.catalog.lookup(PDFName.of("AcroForm"), PDFDict);
|
|
||||||
const fieldsArray = acroForm.lookup(PDFName.of("Fields"), PDFArray);
|
|
||||||
const ref = field.acroField.ref;
|
|
||||||
|
|
||||||
const updatedFields = fieldsArray.asArray().filter((f) => f !== ref);
|
|
||||||
acroForm.set(PDFName.of("Fields"), doc.context.obj(updatedFields));
|
|
||||||
|
|
||||||
// Optional: remove field object entirely
|
|
||||||
doc.context.delete(ref);
|
|
||||||
}
|
|
||||||
|
|
||||||
function copyFieldAndWidgetStyles(
|
|
||||||
sourceFieldDict: PDFDict,
|
|
||||||
sourceWidgetDict: PDFDict,
|
|
||||||
targetFieldDict: PDFDict,
|
|
||||||
targetWidgetDict: PDFDict,
|
|
||||||
) {
|
|
||||||
const fieldKeys = ["DA", "DR", "Q"];
|
|
||||||
const widgetKeys = ["MK", "BS", "Border"];
|
|
||||||
|
|
||||||
// Copy from field dict → field dict
|
|
||||||
for (const key of fieldKeys) {
|
|
||||||
const val = sourceFieldDict.get(PDFName.of(key));
|
|
||||||
if (val) {
|
|
||||||
targetFieldDict.set(PDFName.of(key), val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy from widget dict → widget dict
|
|
||||||
for (const key of widgetKeys) {
|
|
||||||
const val = sourceWidgetDict.get(PDFName.of(key));
|
|
||||||
if (val) {
|
|
||||||
targetWidgetDict.set(PDFName.of(key), val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function findPageForWidget(
|
|
||||||
doc: PDFDocument,
|
|
||||||
widget: PDFWidgetAnnotation,
|
|
||||||
) {
|
|
||||||
const pages = doc.getPages();
|
|
||||||
for (const page of pages) {
|
|
||||||
const annots = page.node.Annots();
|
|
||||||
if (!annots) continue;
|
|
||||||
|
|
||||||
const annotRefs = annots.asArray();
|
|
||||||
for (const ref of annotRefs) {
|
|
||||||
const annot = doc.context.lookup(ref);
|
|
||||||
if (annot === widget.dict) {
|
|
||||||
return page;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function detectFieldType(field: PDFField): string | undefined {
|
|
||||||
const ft = field.acroField.dict.get(PDFName.of("FT"));
|
|
||||||
return ft instanceof PDFName ? ft.asString() : undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getFlag(field: PDFField, bit: number): boolean {
|
|
||||||
const ff = field.acroField.dict.get(PDFName.of("Ff"));
|
|
||||||
return ff instanceof PDFNumber ? (ff.asNumber() & (1 << bit)) !== 0 : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getWidgetRef(
|
|
||||||
widget: PDFWidgetAnnotation,
|
|
||||||
doc: PDFDocument,
|
|
||||||
): PDFRef | undefined {
|
|
||||||
for (const page of doc.getPages()) {
|
|
||||||
const annots = page.node.Annots()?.asArray() ?? [];
|
|
||||||
for (const ref of annots) {
|
|
||||||
const maybeDict = doc.context.lookup(ref);
|
|
||||||
if (maybeDict === widget.dict) {
|
|
||||||
return ref as PDFRef;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function applyWidgetRename(
|
|
||||||
doc: PDFDocument,
|
|
||||||
field: PDFField,
|
|
||||||
widget: PDFWidgetAnnotation,
|
|
||||||
newName: string,
|
|
||||||
pattern: RegExp,
|
pattern: RegExp,
|
||||||
change: string,
|
change: string,
|
||||||
) {
|
) {
|
||||||
try {
|
const segments = name.split(".");
|
||||||
const form = doc.getForm();
|
const matchingSegments = segments.filter((s) => pattern.test(s));
|
||||||
const widgets = field.acroField.getWidgets();
|
let cField: PDFAcroField | undefined = field.acroField;
|
||||||
const widgetDict = widget.dict;
|
while (cField) {
|
||||||
const widgetIndex = widgets.findIndex((w) => w.dict === widgetDict);
|
if (
|
||||||
if (widgetIndex === -1) return;
|
cField.getPartialName() &&
|
||||||
|
matchingSegments.includes(cField.getPartialName()!)
|
||||||
const widgetRef = getWidgetRef(widget, doc);
|
) {
|
||||||
if (!widgetRef) return;
|
const mName = cField.getPartialName()?.replace(pattern, change);
|
||||||
|
if (mName) {
|
||||||
// Remove widget from internal widgets list
|
cField.dict.set(PDFName.of("T"), PDFString.of(mName));
|
||||||
widgets.splice(widgetIndex, 1);
|
// console.log(cField.getPartialName())
|
||||||
|
}
|
||||||
// Remove from /Kids
|
|
||||||
const maybeKids = field.acroField.dict.get(PDFName.of("Kids"));
|
|
||||||
if (maybeKids instanceof PDFArray) {
|
|
||||||
const updatedKids = maybeKids.asArray().filter((ref) => {
|
|
||||||
const maybeDict = doc.context.lookup(ref);
|
|
||||||
return maybeDict !== widgetDict;
|
|
||||||
});
|
|
||||||
field.acroField.dict.set(
|
|
||||||
PDFName.of("Kids"),
|
|
||||||
doc.context.obj(updatedKids),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
cField = cField.getParent();
|
||||||
const page = findPageForWidget(doc, widget);
|
// console.log(cField?.getPartialName())
|
||||||
if (!page) throw new Error("Widget's page not found");
|
|
||||||
|
|
||||||
const rect = widget.getRectangle();
|
|
||||||
if (!rect) throw new Error("Widget has no rectangle");
|
|
||||||
|
|
||||||
const finalName = newName.replace(pattern, change);
|
|
||||||
const fieldType = detectFieldType(field);
|
|
||||||
|
|
||||||
// Attempt to find an existing field with the new name
|
|
||||||
let targetField: PDFField | undefined;
|
|
||||||
try {
|
|
||||||
targetField = form.getField(finalName);
|
|
||||||
} catch {
|
|
||||||
//
|
|
||||||
log("Failed to find existing field");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (targetField) {
|
|
||||||
const sourceType = detectFieldType(field);
|
|
||||||
const targetType = detectFieldType(targetField);
|
|
||||||
if (sourceType !== targetType) {
|
|
||||||
throw new Error(
|
|
||||||
`Field "${finalName}" already exists with a different type (${targetType} vs ${sourceType})`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add widget to existing field
|
|
||||||
widget.dict.set(PDFName.of("Parent"), targetField.acroField.ref);
|
|
||||||
|
|
||||||
const kids = targetField.acroField.dict.lookup(
|
|
||||||
PDFName.of("Kids"),
|
|
||||||
PDFArray,
|
|
||||||
);
|
|
||||||
if (kids) {
|
|
||||||
kids.push(widgetRef);
|
|
||||||
} else {
|
|
||||||
targetField.acroField.dict.set(
|
|
||||||
PDFName.of("Kids"),
|
|
||||||
doc.context.obj([widgetRef]),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const annots = page.node.Annots()?.asArray() ?? [];
|
|
||||||
if (!annots.includes(widgetRef)) {
|
|
||||||
annots.push(widgetRef);
|
|
||||||
page.node.set(PDFName.of("Annots"), doc.context.obj(annots));
|
|
||||||
}
|
|
||||||
|
|
||||||
removeWidgetFromPage(widget, doc);
|
|
||||||
removeWidgetCompletely(doc, widget, field);
|
|
||||||
removeFieldIfEmpty(doc, field);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// No existing field — create new one and move widget
|
|
||||||
removeWidgetFromPage(widget, doc);
|
|
||||||
removeWidgetCompletely(doc, widget, field);
|
|
||||||
removeFieldIfEmpty(doc, field);
|
|
||||||
|
|
||||||
let newField: PDFField;
|
|
||||||
|
|
||||||
switch (fieldType) {
|
|
||||||
case "/Tx": {
|
|
||||||
const tf = form.createTextField(finalName);
|
|
||||||
if (field instanceof PDFTextField) {
|
|
||||||
const val = field.getText();
|
|
||||||
if (val) tf.setText(val);
|
|
||||||
}
|
|
||||||
tf.addToPage(page, {
|
|
||||||
x: rect.x,
|
|
||||||
y: rect.y,
|
|
||||||
width: rect.width,
|
|
||||||
height: rect.height,
|
|
||||||
});
|
|
||||||
newField = tf;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case "/Btn": {
|
|
||||||
const isRadio = getFlag(field, 15);
|
|
||||||
if (isRadio) {
|
|
||||||
const radio = form.createRadioGroup(finalName);
|
|
||||||
radio.addOptionToPage(finalName, page, {
|
|
||||||
x: rect.x,
|
|
||||||
y: rect.y,
|
|
||||||
width: rect.width,
|
|
||||||
height: rect.height,
|
|
||||||
});
|
|
||||||
if (field instanceof PDFRadioGroup) {
|
|
||||||
const selected = field.getSelected();
|
|
||||||
if (selected) radio.select(selected);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
const cb = form.createCheckBox(finalName);
|
|
||||||
cb.addToPage(page, {
|
|
||||||
x: rect.x,
|
|
||||||
y: rect.y,
|
|
||||||
width: rect.width,
|
|
||||||
height: rect.height,
|
|
||||||
});
|
|
||||||
if (field instanceof PDFCheckBox && field.isChecked()) {
|
|
||||||
cb.check();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new Error(`Unsupported field type: ${fieldType}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply styles from old field/widget after creation
|
|
||||||
copyFieldAndWidgetStyles(
|
|
||||||
field.acroField.dict,
|
|
||||||
widget.dict,
|
|
||||||
newField.acroField.dict,
|
|
||||||
newField.acroField.getWidgets()[0].dict,
|
|
||||||
);
|
|
||||||
} catch (e) {
|
|
||||||
log("applyWidgetRename error:", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeWidgetFromPage(widget: PDFWidgetAnnotation, doc: PDFDocument) {
|
|
||||||
const pages = doc.getPages();
|
|
||||||
|
|
||||||
for (const page of pages) {
|
|
||||||
const annotsArray = page.node.Annots();
|
|
||||||
if (!annotsArray) continue;
|
|
||||||
|
|
||||||
const refs = annotsArray.asArray();
|
|
||||||
const newRefs = refs.filter((ref) => {
|
|
||||||
const maybeDict = doc.context.lookup(ref);
|
|
||||||
return maybeDict !== widget.dict;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Replace /Annots with updated array
|
|
||||||
if (newRefs.length === refs.length) continue;
|
|
||||||
page.node.set(PDFName.of("Annots"), doc.context.obj(newRefs));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -715,7 +83,7 @@ function removeWidgetFromPage(widget: PDFWidgetAnnotation, doc: PDFDocument) {
|
|||||||
* - $<int>u - capture groups, indexed from 1, transforming a string to upper case
|
* - $<int>u - capture groups, indexed from 1, transforming a string to upper case
|
||||||
* - $<int>t - capture groups, indexed from 1, transforming a string to title case
|
* - $<int>t - capture groups, indexed from 1, transforming a string to title case
|
||||||
*/
|
*/
|
||||||
function evaluateChange(change: string, match: RegExpExecArray, index: number) {
|
function evaluateChange(change: string, match: RegExpExecArray) {
|
||||||
return change.replace(
|
return change.replace(
|
||||||
/\$(\d+)([icslut]?)/g,
|
/\$(\d+)([icslut]?)/g,
|
||||||
(_, i, indexed) => {
|
(_, i, indexed) => {
|
||||||
@ -738,19 +106,7 @@ function evaluateChange(change: string, match: RegExpExecArray, index: number) {
|
|||||||
return match[i];
|
return match[i];
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
);
|
||||||
.replace(
|
|
||||||
/\$I{((\w+,?)+)}/,
|
|
||||||
(_, offset) => {
|
|
||||||
const options = offset.split(",");
|
|
||||||
return options[index % options.length];
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.replace(
|
|
||||||
/\$I(-?\d+)?/,
|
|
||||||
(_, offset) =>
|
|
||||||
(parseInt(offset) ? index + parseInt(offset) : index).toString(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class RenameFields implements ITool {
|
class RenameFields implements ITool {
|
||||||
@ -794,97 +150,29 @@ class RenameFields implements ITool {
|
|||||||
|
|
||||||
const pdf = await loadPdf(pdfPath);
|
const pdf = await loadPdf(pdfPath);
|
||||||
const form = pdf.getForm();
|
const form = pdf.getForm();
|
||||||
const fields = form.getFields().sort((a, b) => {
|
const fields = form.getFields();
|
||||||
const aWidgets = a.acroField.getWidgets();
|
|
||||||
const bWidgets = b.acroField.getWidgets();
|
|
||||||
const aWidget = aWidgets[0];
|
|
||||||
const bWidget = bWidgets[0];
|
|
||||||
|
|
||||||
const aPage = a.doc.findPageForAnnotationRef(a.acroField.ref);
|
|
||||||
const bPage = b.doc.findPageForAnnotationRef(b.acroField.ref);
|
|
||||||
|
|
||||||
if (aPage && bPage && aPage !== bPage) {
|
|
||||||
const pages = a.doc.getPages();
|
|
||||||
const aPageIndex = pages.indexOf(aPage);
|
|
||||||
const bPageIndex = pages.indexOf(bPage);
|
|
||||||
|
|
||||||
if (aPageIndex !== bPageIndex) return aPageIndex - bPageIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
const aRect = aWidget.Rect()?.asRectangle();
|
|
||||||
const bRect = bWidget.Rect()?.asRectangle();
|
|
||||||
|
|
||||||
if (aRect && bRect) {
|
|
||||||
const dy = bRect.y - aRect.y;
|
|
||||||
if (Math.abs(dy) > 5) return dy;
|
|
||||||
|
|
||||||
return aRect.x - bRect.x;
|
|
||||||
}
|
|
||||||
|
|
||||||
return a.getName().localeCompare(b.getName());
|
|
||||||
});
|
|
||||||
let badFields = 0;
|
|
||||||
|
|
||||||
for (const field of fields) {
|
|
||||||
if (field.acroField.getWidgets().length > 1) {
|
|
||||||
badFields++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
badFields && await cliLog(
|
|
||||||
colorize(
|
|
||||||
`Warning, ${badFields} fields with shared widgets found`,
|
|
||||||
"yellow",
|
|
||||||
),
|
|
||||||
this.block,
|
|
||||||
);
|
|
||||||
|
|
||||||
const foundUpdates: [string, callback][] = [];
|
const foundUpdates: [string, callback][] = [];
|
||||||
let changesMade = false;
|
let changesMade = false;
|
||||||
|
|
||||||
let i = 0;
|
|
||||||
for (const field of fields) {
|
for (const field of fields) {
|
||||||
const name = field.getName();
|
const name = field.getName();
|
||||||
const match = patternRegex.exec(name);
|
const match = patternRegex.exec(name);
|
||||||
if (match) {
|
if (match) {
|
||||||
foundUpdates.push(
|
const toChange = evaluateChange(change, match);
|
||||||
...field.acroField.getWidgets()?.map<[string, callback]>((
|
const preview = name.replace(new RegExp(patternRegex), toChange);
|
||||||
widget,
|
foundUpdates.push([
|
||||||
) => {
|
`${colorize(name, "red")} -> ${colorize(preview, "green")}`,
|
||||||
const toChange = evaluateChange(change, match, i);
|
() => {
|
||||||
const preview = name.replace(
|
applyRename(field, name, patternRegex, toChange);
|
||||||
new RegExp(patternRegex),
|
changesMade = true;
|
||||||
toChange,
|
},
|
||||||
);
|
]);
|
||||||
i++;
|
|
||||||
return [
|
|
||||||
`${colorize(name, "red")} -> ${colorize(preview, "green")}`,
|
|
||||||
() => {
|
|
||||||
field.acroField.getWidgets().length > 1
|
|
||||||
? applyWidgetRename(
|
|
||||||
pdf,
|
|
||||||
field,
|
|
||||||
widget,
|
|
||||||
name,
|
|
||||||
new RegExp(patternRegex),
|
|
||||||
toChange,
|
|
||||||
)
|
|
||||||
: moveWidgetToFlatField(
|
|
||||||
pdf,
|
|
||||||
field,
|
|
||||||
field.acroField.getWidgets()[0],
|
|
||||||
preview,
|
|
||||||
);
|
|
||||||
changesMade = true;
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (foundUpdates.length) {
|
if (foundUpdates.length) {
|
||||||
await cliLog("Found updates:", this.block);
|
cliLog("Found updates:", this.block);
|
||||||
await multiSelectMenuInteractive(
|
await multiSelectMenuInteractive(
|
||||||
"Please select an option to apply",
|
"Please select an option to apply",
|
||||||
foundUpdates,
|
foundUpdates,
|
||||||
@ -897,11 +185,7 @@ class RenameFields implements ITool {
|
|||||||
"Save to path (or hit enter to keep current):",
|
"Save to path (or hit enter to keep current):",
|
||||||
this.block,
|
this.block,
|
||||||
);
|
);
|
||||||
try {
|
await savePdf(pdf, path || pdfPath);
|
||||||
await savePdf(pdf, path || pdfPath);
|
|
||||||
} catch {
|
|
||||||
log(e);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
cliLog("No changes made, skipping", this.block);
|
cliLog("No changes made, skipping", this.block);
|
||||||
}
|
}
|
||||||
@ -910,14 +194,14 @@ class RenameFields implements ITool {
|
|||||||
}
|
}
|
||||||
export default new RenameFields();
|
export default new RenameFields();
|
||||||
|
|
||||||
// if (import.meta.main) {
|
if (import.meta.main) {
|
||||||
// // await call(renameFields)
|
// await call(renameFields)
|
||||||
// // while (!path || !path.endsWith('.pdf')) path = prompt("Please provide path to PDF:") || '';
|
// while (!path || !path.endsWith('.pdf')) path = prompt("Please provide path to PDF:") || '';
|
||||||
// // while (!pattern) pattern = prompt("Please provide search string:") || '';
|
// while (!pattern) pattern = prompt("Please provide search string:") || '';
|
||||||
// // while (!change) change = prompt("Please provide requested change:") || '';
|
// while (!change) change = prompt("Please provide requested change:") || '';
|
||||||
// await callWithArgPrompt(renameFields, [
|
await callWithArgPrompt(renameFields, [
|
||||||
// ["Please provide path to PDF:", (p) => !!p && p.endsWith(".pdf")],
|
["Please provide path to PDF:", (p) => !!p && p.endsWith(".pdf")],
|
||||||
// "Please provide search string:",
|
"Please provide search string:",
|
||||||
// "Please provide requested change:",
|
"Please provide requested change:",
|
||||||
// ]);
|
]);
|
||||||
// }
|
}
|
||||||
|
@ -20,10 +20,31 @@ export class ListFormFields implements ITool {
|
|||||||
|
|
||||||
const form = await loadPdfForm(pdfPath);
|
const form = await loadPdfForm(pdfPath);
|
||||||
const fields = form.getFields();
|
const fields = form.getFields();
|
||||||
const fieldNames = fields.map((f) => f.getName());
|
const height = this.block.getRenderHeight() - 1;
|
||||||
|
const fieldNames = fields.sort((a, b) => {
|
||||||
|
const aRect = a.acroField.getWidgets().find((e) => e.Rect())?.Rect()
|
||||||
|
?.asRectangle();
|
||||||
|
const bRect = b.acroField.getWidgets().find((e) => e.Rect())?.Rect()
|
||||||
|
?.asRectangle();
|
||||||
|
|
||||||
|
if (aRect && bRect) {
|
||||||
|
if (aRect.x !== bRect.x) {
|
||||||
|
return aRect.x - bRect.x; // Sort left to right
|
||||||
|
} else {
|
||||||
|
return bRect.y - aRect.y; // If x is equal, sort top to bottom
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return a.getName().localeCompare(b.getName());
|
||||||
|
}).map((f) => f.getName());
|
||||||
|
const maxLength = Math.max(...fieldNames.map((f) => f.length)) + 4;
|
||||||
const lines = [];
|
const lines = [];
|
||||||
for (const fieldName of fieldNames) {
|
for (let i = 0; i < height; i++) {
|
||||||
lines.push(fieldName);
|
let line = "";
|
||||||
|
for (let j = 0; j < fieldNames.length; j += height) {
|
||||||
|
const fieldName = fieldNames[i + j] ?? "";
|
||||||
|
line += fieldName.padEnd(maxLength, " ");
|
||||||
|
}
|
||||||
|
lines.push(line);
|
||||||
}
|
}
|
||||||
this.block.setLines(lines, [0, 1]);
|
this.block.setLines(lines, [0, 1]);
|
||||||
await cliAlert("", this.block);
|
await cliAlert("", this.block);
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { log } from "./logfile.ts";
|
||||||
import { join } from "@std/path";
|
import { join } from "@std/path";
|
||||||
|
|
||||||
export async function getAsciiArt(art: string) {
|
export async function getAsciiArt(art: string) {
|
||||||
|
@ -7,9 +7,9 @@ const logFile = Deno.openSync("./log.txt", {
|
|||||||
|
|
||||||
logFile.truncateSync(0);
|
logFile.truncateSync(0);
|
||||||
|
|
||||||
export function log(...message: any) {
|
export function log(message: any) {
|
||||||
if (typeof message === "object") {
|
if (typeof message === "object") {
|
||||||
message = Deno.inspect(message);
|
message = JSON.stringify(message);
|
||||||
}
|
}
|
||||||
logFile.writeSync(new TextEncoder().encode(message + "\n"));
|
logFile.writeSync(new TextEncoder().encode(message + "\n"));
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,10 @@ export async function loadPdf(path: string) {
|
|||||||
export async function savePdf(doc: PDFDocument, path: string) {
|
export async function savePdf(doc: PDFDocument, path: string) {
|
||||||
doc.getForm().getFields().forEach((field) => {
|
doc.getForm().getFields().forEach((field) => {
|
||||||
if (field instanceof PDFTextField) {
|
if (field instanceof PDFTextField) {
|
||||||
field.disableRichFormatting?.();
|
field.disableRichFormatting();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const pdfBytes = await doc.save({ updateFieldAppearances: true });
|
const pdfBytes = await doc.save();
|
||||||
if (Deno.env.get("DRYRUN") || path.includes("dryrun")) return;
|
if (Deno.env.get("DRYRUN") || path.includes("dryrun")) return;
|
||||||
await Deno.writeFile(path, pdfBytes);
|
await Deno.writeFile(path, pdfBytes);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user