An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @typedef {import("./event_utils.js").EventBus} EventBus */
/** @typedef {import("./overlay_manager.js").OverlayManager} OverlayManager */
// eslint-disable-next-line max-len
/** @typedef {import("../src/display/api.js").PDFDocumentProxy} PDFDocumentProxy */
import { getPageSizeInches, isPortraitOrientation } from "./ui_utils.js";
import { internalOpt } from "./internal_evt.js";
import { PDFDateString } from "./pdfjs";
// See https://en.wikibooks.org/wiki/Lentis/Conversion_to_the_Metric_Standard_in_the_United_States
const NON_METRIC_LOCALES = ["en-us", "en-lr", "my"];
// Should use the format: `width x height`, in portrait orientation. The names,
// which are l10n-ids, should be lowercase.
// See https://en.wikipedia.org/wiki/Paper_size
const US_PAGE_NAMES = {
"8.5x11": "pdfjs-document-properties-page-size-name-letter",
"8.5x14": "pdfjs-document-properties-page-size-name-legal",
};
const METRIC_PAGE_NAMES = {
"297x420": "pdfjs-document-properties-page-size-name-a-three",
"210x297": "pdfjs-document-properties-page-size-name-a-four",
};
function getPageName(size, isPortrait, pageNames) {
const width = isPortrait ? size.width : size.height;
const height = isPortrait ? size.height : size.width;
return pageNames[`${width}x${height}`];
}
/**
* @typedef {object} PDFDocumentPropertiesOptions
* @property {HTMLDialogElement} dialog - The overlay's DOM element.
* @property {object} fields - Names and elements of the overlay's fields.
* @property {HTMLButtonElement} closeButton - Button for closing the overlay.
*/
class PDFDocumentProperties {
#fieldData = null;
/**
* @param {PDFDocumentPropertiesOptions} options
* @param {OverlayManager} overlayManager - Manager for the viewer overlays.
* @param {EventBus} eventBus - The application event bus.
* @param {L10n} l10n - Localization service.
* @param {Function} fileNameLookup - The function that is used to lookup
* the document fileName.
*/
constructor(
{ dialog, fields, closeButton },
overlayManager,
eventBus,
l10n,
fileNameLookup,
titleLookup
) {
this.dialog = dialog;
this.fields = fields;
this.overlayManager = overlayManager;
this.l10n = l10n;
this._fileNameLookup = fileNameLookup;
this._titleLookup = titleLookup;
this.#reset();
// Bind the event listener for the Close button.
closeButton.addEventListener("click", this.close.bind(this));
this.overlayManager.register(this.dialog);
eventBus.on(
"pagechanging",
evt => {
this._currentPageNumber = evt.pageNumber;
},
internalOpt
);
eventBus.on(
"rotationchanging",
evt => {
this._pagesRotation = evt.pagesRotation;
},
internalOpt
);
}
/**
* Open the document properties overlay.
*/
async open() {
await Promise.all([
this.overlayManager.open(this.dialog),
this._dataAvailableCapability.promise,
]);
const currentPageNumber = this._currentPageNumber;
const pagesRotation = this._pagesRotation;
// If the document properties were previously fetched (for this PDF file),
// just update the dialog immediately to avoid redundant lookups.
if (
this.#fieldData &&
currentPageNumber === this.#fieldData._currentPageNumber &&
pagesRotation === this.#fieldData._pagesRotation
) {
this.#updateUI();
return;
}
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
this._fieldDataLastUpdated = Date.now();
}
// Get the document properties.
const [
{ info, metadata, /* contentDispositionFilename, */ contentLength },
pageSize,
] = await Promise.all([
this.pdfDocument.getMetadata(),
this.pdfDocument.getPage(currentPageNumber).then(
pdfPage => this.#parsePageSize(pdfPage, pagesRotation),
reason => {
console.error(
`PDFDocumentProperties - unable to get page ${currentPageNumber}.`,
reason
);
return undefined;
}
),
]);
this.#fieldData = Object.freeze({
fileName: this._fileNameLookup(),
fileSize: this.#parseFileSize(contentLength),
title: this._titleLookup(),
author: metadata?.get("dc:creator")?.join("\n") || info.Author,
subject: metadata?.get("dc:subject")?.join("\n") || info.Subject,
keywords: metadata?.get("pdf:keywords") || info.Keywords,
creationDate: this.#parseDate(
metadata?.get("xmp:createdate"),
info.CreationDate
),
modificationDate: this.#parseDate(
metadata?.get("xmp:modifydate"),
info.ModDate
),
creator: metadata?.get("xmp:creatortool") || info.Creator,
producer: metadata?.get("pdf:producer") || info.Producer,
version: info.PDFFormatVersion,
pageCount: this.pdfDocument.numPages,
pageSize,
linearized: this.#parseLinearization(info.IsLinearized),
_currentPageNumber: currentPageNumber,
_pagesRotation: pagesRotation,
});
this.#updateUI();
// Get the correct fileSize, since it may not have been available
// or could potentially be wrong.
const { length } = await this.pdfDocument.getDownloadInfo();
if (contentLength === length) {
return; // The fileSize has already been correctly set.
}
const data = Object.assign(Object.create(null), this.#fieldData);
data.fileSize = this.#parseFileSize(length);
this.#fieldData = Object.freeze(data);
this.#updateUI();
}
/**
* Close the document properties overlay.
*/
async close() {
this.overlayManager.close(this.dialog);
}
/**
* Set a reference to the PDF document in order to populate the dialog fields
* with the document properties. Note that the dialog will contain no
* information if this method is not called.
* @param {PDFDocumentProxy} pdfDocument - A reference to the PDF document.
*/
setDocument(pdfDocument) {
if (this.pdfDocument) {
this.#reset();
this.#updateUI();
}
if (!pdfDocument) {
return;
}
this.pdfDocument = pdfDocument;
this._dataAvailableCapability.resolve();
}
#reset() {
this.pdfDocument = null;
this.#fieldData = null;
this._dataAvailableCapability = Promise.withResolvers();
this._currentPageNumber = 1;
this._pagesRotation = 0;
}
/**
* Always updates all of the dialog fields, to prevent inconsistent UI state.
* NOTE: If the contents of a particular field is neither a non-empty string,
* nor a number, it will fall back to "-".
*/
#updateUI() {
if (this.#fieldData && this.overlayManager.active !== this.dialog) {
// Don't bother updating the dialog if it's already been closed,
// unless it's being reset (i.e. `this.#fieldData === null`),
// since it will be updated the next time `this.open` is called.
return;
}
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
this.dialog.dataset.fieldDataLastUpdated = this._fieldDataLastUpdated;
}
for (const id in this.fields) {
const field = this.fields[id],
data = this.#fieldData?.[id];
if (data?.id) {
field.setAttribute("data-l10n-id", data.id);
if (data.args) {
field.setAttribute("data-l10n-args", JSON.stringify(data.args));
}
} else {
field.removeAttribute("data-l10n-id");
field.removeAttribute("data-l10n-args");
field.textContent = data || data === 0 ? data : "-";
}
}
}
#parseFileSize(b = 0) {
const kb = b / 1024,
mb = kb / 1024;
return kb
// NOTE
? mb >= 1 ? `${mb.toPrecision(3)} MB ${b} bytes` : `${kb.toPrecision(3)} KB ${b} bytes`
: undefined;
}
async #parsePageSize(pdfPage, pagesRotation) {
let pageSizeInches = getPageSizeInches(pdfPage);
// Take the viewer rotation into account as well; compare with Adobe Reader.
if (pagesRotation % 180 !== 0) {
pageSizeInches = {
width: pageSizeInches.height,
height: pageSizeInches.width,
};
}
const isPortrait = isPortraitOrientation(pageSizeInches),
nonMetric = NON_METRIC_LOCALES.includes(this.l10n.getLanguage());
let sizeInches = {
width: Math.round(pageSizeInches.width * 100) / 100,
height: Math.round(pageSizeInches.height * 100) / 100,
};
// 1in == 25.4mm; no need to round to 2 decimals for millimeters.
let sizeMillimeters = {
width: Math.round(pageSizeInches.width * 25.4 * 10) / 10,
height: Math.round(pageSizeInches.height * 25.4 * 10) / 10,
};
let nameId =
getPageName(sizeInches, isPortrait, US_PAGE_NAMES) ||
getPageName(sizeMillimeters, isPortrait, METRIC_PAGE_NAMES);
if (
!nameId &&
!(
Number.isInteger(sizeMillimeters.width) &&
Number.isInteger(sizeMillimeters.height)
)
) {
// Attempt to improve the page name detection by falling back to fuzzy
// matching of the metric dimensions, to account for e.g. rounding errors
// and/or PDF files that define the page sizes in an imprecise manner.
const exactMillimeters = {
width: pageSizeInches.width * 25.4,
height: pageSizeInches.height * 25.4,
};
const intMillimeters = {
width: Math.round(sizeMillimeters.width),
height: Math.round(sizeMillimeters.height),
};
// Try to avoid false positives, by only considering "small" differences.
if (
Math.abs(exactMillimeters.width - intMillimeters.width) < 0.1 &&
Math.abs(exactMillimeters.height - intMillimeters.height) < 0.1
) {
nameId = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
if (nameId) {
// Update *both* sizes, computed above, to ensure that the displayed
// dimensions always correspond to the detected page name.
sizeInches = {
width: Math.round((intMillimeters.width / 25.4) * 100) / 100,
height: Math.round((intMillimeters.height / 25.4) * 100) / 100,
};
sizeMillimeters = intMillimeters;
}
}
}
const [{ width, height }, unit, name, orientation] = await Promise.all([
nonMetric ? sizeInches : sizeMillimeters,
// NOTE
this.l10n.get(
nonMetric
? "unitInches"
: "unitMillimeters"
),
nameId && this.l10n.get(nameId),
this.l10n.get(
isPortrait
? "document_properties_page_size_orientation_portrait"
: "document_properties_page_size_orientation_landscape"
),
]);
// NOTE
return name ?`${width.toLocaleString()} × ${height.toLocaleString()} ${unit} (${name}, ${orientation})`:
`${width.toLocaleString()} × ${height.toLocaleString()} ${unit} (${orientation})`;
}
#parseDate(metadataDate, infoDate) {
const dateObj =
(Date.parse(metadataDate) ? new Date(metadataDate) : PDFDateString.toDateObject(infoDate));
return dateObj
// NOTE
? `${dateObj.toLocaleDateString()}, ${dateObj.toLocaleTimeString()}`
: undefined;
}
#parseLinearization(isLinearized) {
return this.l10n.get(
isLinearized
// NOTE
? "Yes"
: "No"
);
}
}
export { PDFDocumentProperties };