mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Merge pull request #16620 from Snuffleupagus/AnnotationStorage-transfers
Move the `transfers` computation into the `AnnotationStorage` class
This commit is contained in:
commit
ffa9795ca9
5 changed files with 66 additions and 75 deletions
|
@ -17,6 +17,12 @@ import { objectFromMap, unreachable } from "../shared/util.js";
|
|||
import { AnnotationEditor } from "./editor/editor.js";
|
||||
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
|
||||
|
||||
const SerializableEmpty = Object.freeze({
|
||||
map: null,
|
||||
hash: "",
|
||||
transfers: undefined,
|
||||
});
|
||||
|
||||
/**
|
||||
* Key/value storage for annotation data in forms.
|
||||
*/
|
||||
|
@ -171,34 +177,27 @@ class AnnotationStorage {
|
|||
*/
|
||||
get serializable() {
|
||||
if (this.#storage.size === 0) {
|
||||
return null;
|
||||
return SerializableEmpty;
|
||||
}
|
||||
const clone = new Map();
|
||||
|
||||
const map = new Map(),
|
||||
hash = new MurmurHash3_64(),
|
||||
transfers = [];
|
||||
for (const [key, val] of this.#storage) {
|
||||
const serialized =
|
||||
val instanceof AnnotationEditor ? val.serialize() : val;
|
||||
if (serialized) {
|
||||
clone.set(key, serialized);
|
||||
map.set(key, serialized);
|
||||
|
||||
hash.update(`${key}:${JSON.stringify(serialized)}`);
|
||||
|
||||
if (serialized.bitmap) {
|
||||
transfers.push(serialized.bitmap);
|
||||
}
|
||||
}
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* PLEASE NOTE: Only intended for usage within the API itself.
|
||||
* @ignore
|
||||
*/
|
||||
static getHash(map) {
|
||||
if (!map) {
|
||||
return "";
|
||||
}
|
||||
const hash = new MurmurHash3_64();
|
||||
|
||||
for (const [key, val] of map) {
|
||||
hash.update(`${key}:${JSON.stringify(val)}`);
|
||||
}
|
||||
return hash.hexdigest();
|
||||
return map.size > 0
|
||||
? { map, hash: hash.hexdigest(), transfers }
|
||||
: SerializableEmpty;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,12 +207,21 @@ class AnnotationStorage {
|
|||
* contents. (Necessary since printing is triggered synchronously in browsers.)
|
||||
*/
|
||||
class PrintAnnotationStorage extends AnnotationStorage {
|
||||
#serializable = null;
|
||||
#serializable;
|
||||
|
||||
constructor(parent) {
|
||||
super();
|
||||
const { map, hash, transfers } = parent.serializable;
|
||||
// Create a *copy* of the data, since Objects are passed by reference in JS.
|
||||
this.#serializable = structuredClone(parent.serializable);
|
||||
const clone = structuredClone(
|
||||
map,
|
||||
(typeof PDFJSDev === "undefined" ||
|
||||
PDFJSDev.test("SKIP_BABEL || TESTING")) &&
|
||||
transfers
|
||||
? { transfer: transfers }
|
||||
: null
|
||||
);
|
||||
this.#serializable = { map: clone, hash, transfers };
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -233,4 +241,4 @@ class PrintAnnotationStorage extends AnnotationStorage {
|
|||
}
|
||||
}
|
||||
|
||||
export { AnnotationStorage, PrintAnnotationStorage };
|
||||
export { AnnotationStorage, PrintAnnotationStorage, SerializableEmpty };
|
||||
|
|
|
@ -41,6 +41,7 @@ import {
|
|||
import {
|
||||
AnnotationStorage,
|
||||
PrintAnnotationStorage,
|
||||
SerializableEmpty,
|
||||
} from "./annotation_storage.js";
|
||||
import {
|
||||
deprecated,
|
||||
|
@ -1801,22 +1802,18 @@ class PDFPageProxy {
|
|||
/**
|
||||
* @private
|
||||
*/
|
||||
_pumpOperatorList({ renderingIntent, cacheKey, annotationStorageMap }) {
|
||||
_pumpOperatorList({
|
||||
renderingIntent,
|
||||
cacheKey,
|
||||
annotationStorageSerializable,
|
||||
}) {
|
||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
|
||||
assert(
|
||||
Number.isInteger(renderingIntent) && renderingIntent > 0,
|
||||
'_pumpOperatorList: Expected valid "renderingIntent" argument.'
|
||||
);
|
||||
}
|
||||
|
||||
const transfers = [];
|
||||
if (annotationStorageMap) {
|
||||
for (const annotation of annotationStorageMap.values()) {
|
||||
if (annotation.bitmap) {
|
||||
transfers.push(annotation.bitmap);
|
||||
}
|
||||
}
|
||||
}
|
||||
const { map, transfers } = annotationStorageSerializable;
|
||||
|
||||
const readableStream = this._transport.messageHandler.sendWithStream(
|
||||
"GetOperatorList",
|
||||
|
@ -1824,7 +1821,7 @@ class PDFPageProxy {
|
|||
pageIndex: this._pageIndex,
|
||||
intent: renderingIntent,
|
||||
cacheKey,
|
||||
annotationStorage: annotationStorageMap,
|
||||
annotationStorage: map,
|
||||
},
|
||||
transfers
|
||||
);
|
||||
|
@ -2449,7 +2446,7 @@ class WorkerTransport {
|
|||
isOpList = false
|
||||
) {
|
||||
let renderingIntent = RenderingIntentFlag.DISPLAY; // Default value.
|
||||
let annotationMap = null;
|
||||
let annotationStorageSerializable = SerializableEmpty;
|
||||
|
||||
switch (intent) {
|
||||
case "any":
|
||||
|
@ -2482,7 +2479,7 @@ class WorkerTransport {
|
|||
? printAnnotationStorage
|
||||
: this.annotationStorage;
|
||||
|
||||
annotationMap = annotationStorage.serializable;
|
||||
annotationStorageSerializable = annotationStorage.serializable;
|
||||
break;
|
||||
default:
|
||||
warn(`getRenderingIntent - invalid annotationMode: ${annotationMode}`);
|
||||
|
@ -2494,10 +2491,8 @@ class WorkerTransport {
|
|||
|
||||
return {
|
||||
renderingIntent,
|
||||
cacheKey: `${renderingIntent}_${AnnotationStorage.getHash(
|
||||
annotationMap
|
||||
)}`,
|
||||
annotationStorageMap: annotationMap,
|
||||
cacheKey: `${renderingIntent}_${annotationStorageSerializable.hash}`,
|
||||
annotationStorageSerializable,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -2905,22 +2900,15 @@ class WorkerTransport {
|
|||
"please use the getData-method instead."
|
||||
);
|
||||
}
|
||||
const annotationStorage = this.annotationStorage.serializable;
|
||||
const transfers = [];
|
||||
if (annotationStorage) {
|
||||
for (const annotation of annotationStorage.values()) {
|
||||
if (annotation.bitmap) {
|
||||
transfers.push(annotation.bitmap);
|
||||
}
|
||||
}
|
||||
}
|
||||
const { map, transfers } = this.annotationStorage.serializable;
|
||||
|
||||
return this.messageHandler
|
||||
.sendWithPromise(
|
||||
"SaveDocument",
|
||||
{
|
||||
isPureXfa: !!this._htmlForXfa,
|
||||
numPages: this._numPages,
|
||||
annotationStorage,
|
||||
annotationStorage: map,
|
||||
filename: this._fullReader?.filename ?? null,
|
||||
},
|
||||
transfers
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue