Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] XFA - Remove the xfaLayer from the DOM when resetting pages (bug 1721977, PR 13427 follow-up) #13782

Merged
merged 2 commits into from
Jul 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 55 additions & 28 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,42 +207,47 @@ class PDFPageView {
this.zoomLayer = null;
}

reset(keepZoomLayer = false, keepAnnotations = false) {
this.cancelRendering(keepAnnotations);
reset({
keepZoomLayer = false,
keepAnnotationLayer = false,
keepXfaLayer = false,
} = {}) {
this.cancelRendering({ keepAnnotationLayer, keepXfaLayer });
this.renderingState = RenderingStates.INITIAL;

const div = this.div;
div.style.width = Math.floor(this.viewport.width) + "px";
div.style.height = Math.floor(this.viewport.height) + "px";

const childNodes = div.childNodes;
const currentZoomLayerNode = (keepZoomLayer && this.zoomLayer) || null;
const currentAnnotationNode =
(keepAnnotations && this.annotationLayer?.div) || null;
const currentXfaLayerNode = this.xfaLayer?.div || null;
const childNodes = div.childNodes,
zoomLayerNode = (keepZoomLayer && this.zoomLayer) || null,
annotationLayerNode =
(keepAnnotationLayer && this.annotationLayer?.div) || null,
xfaLayerNode = (keepXfaLayer && this.xfaLayer?.div) || null;
for (let i = childNodes.length - 1; i >= 0; i--) {
const node = childNodes[i];
if (
currentZoomLayerNode === node ||
currentAnnotationNode === node ||
currentXfaLayerNode === node
) {
continue;
switch (node) {
case zoomLayerNode:
case annotationLayerNode:
case xfaLayerNode:
continue;
}
div.removeChild(node);
}
div.removeAttribute("data-loaded");

if (currentAnnotationNode) {
if (annotationLayerNode) {
// Hide the annotation layer until all elements are resized
// so they are not displayed on the already resized page.
this.annotationLayer.hide();
} else if (this.annotationLayer) {
this.annotationLayer.cancel();
this.annotationLayer = null;
}
if (xfaLayerNode) {
// Hide the XFA layer until all elements are resized
// so they are not displayed on the already resized page.
this.xfaLayer.hide();
}

if (!currentZoomLayerNode) {
if (!zoomLayerNode) {
if (this.canvas) {
this.paintedViewportMap.delete(this.canvas);
// Zeroing the width and height causes Firefox to release graphics
Expand Down Expand Up @@ -284,7 +289,11 @@ class PDFPageView {
});

if (this.svg) {
this.cssTransform(this.svg, true);
this.cssTransform({
target: this.svg,
redrawAnnotationLayer: true,
redrawXfaLayer: true,
});

this.eventBus.dispatch("pagerendered", {
source: this,
Expand Down Expand Up @@ -313,7 +322,11 @@ class PDFPageView {
this.useOnlyCssZoom ||
(this.hasRestrictedScaling && isScalingRestricted)
) {
this.cssTransform(this.canvas, true);
this.cssTransform({
target: this.canvas,
redrawAnnotationLayer: true,
redrawXfaLayer: true,
});

this.eventBus.dispatch("pagerendered", {
source: this,
Expand All @@ -330,16 +343,20 @@ class PDFPageView {
}
}
if (this.zoomLayer) {
this.cssTransform(this.zoomLayer.firstChild);
this.cssTransform({ target: this.zoomLayer.firstChild });
}
this.reset(/* keepZoomLayer = */ true, /* keepAnnotations = */ true);
this.reset({
keepZoomLayer: true,
keepAnnotationLayer: true,
keepXfaLayer: true,
});
}

/**
* PLEASE NOTE: Most likely you want to use the `this.reset()` method,
* rather than calling this one directly.
*/
cancelRendering(keepAnnotations = false) {
cancelRendering({ keepAnnotationLayer = false, keepXfaLayer = false } = {}) {
if (this.paintTask) {
this.paintTask.cancel();
this.paintTask = null;
Expand All @@ -350,17 +367,28 @@ class PDFPageView {
this.textLayer.cancel();
this.textLayer = null;
}
if (!keepAnnotations && this.annotationLayer) {
if (
this.annotationLayer &&
(!keepAnnotationLayer || !this.annotationLayer.div)
) {
this.annotationLayer.cancel();
this.annotationLayer = null;
}
if (this.xfaLayer && (!keepXfaLayer || !this.xfaLayer.div)) {
this.xfaLayer.cancel();
this.xfaLayer = null;
}
if (this._onTextLayerRendered) {
this.eventBus._off("textlayerrendered", this._onTextLayerRendered);
this._onTextLayerRendered = null;
}
}

cssTransform(target, redrawAnnotations = false) {
cssTransform({
target,
redrawAnnotationLayer = false,
redrawXfaLayer = false,
}) {
// Scale target (canvas or svg), its wrapper and page container.
const width = this.viewport.width;
const height = this.viewport.height;
Expand Down Expand Up @@ -429,11 +457,10 @@ class PDFPageView {
textLayerDiv.style.transformOrigin = "0% 0%";
}

if (redrawAnnotations && this.annotationLayer) {
if (redrawAnnotationLayer && this.annotationLayer) {
this._renderAnnotationLayer();
}

if (this.xfaLayer) {
if (redrawXfaLayer && this.xfaLayer) {
this._renderXfaLayer();
}
}
Expand Down