Skip to content

Commit

Permalink
JS - Add the basic architecture to be able to execute embedded js
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Oct 1, 2020
1 parent 53cbe33 commit 2832e17
Show file tree
Hide file tree
Showing 17 changed files with 1,320 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,9 +1257,9 @@ function buildLib(defines, dir) {
return merge([
gulp.src(
[
"src/{core,display,shared}/*.js",
"src/{core,display,scripting_api,shared}/*.js",
"!src/shared/{cffStandardStrings,fonts_utils}.js",
"src/{pdf,pdf.worker}.js",
"src/{pdf,pdf.scripting_api,pdf.worker}.js",
],
{ base: "src/" }
),
Expand Down
35 changes: 35 additions & 0 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
element.setAttribute("value", textContent);
}

element.setAttribute("id", id);

element.addEventListener("input", function (event) {
storage.setValue(id, event.target.value);
});
Expand All @@ -470,6 +472,39 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
event.target.setSelectionRange(0, 0);
});

if (this.data.actions) {
element.addEventListener("updateFromSandbox", function (event) {
const data = event.detail;
if ("value" in data) {
event.target.value = event.detail.value;
} else if ("focus" in data) {
event.target.focus({ preventScroll: false });
}
});

for (const eventType of Object.keys(this.data.actions)) {
switch (eventType) {
case "Format":
element.addEventListener("blur", function (event) {
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
field: "value",
id,
event: {
type: "Field",
name: "Format",
value: event.target.value,
},
},
})
);
});
break;
}
}
}

element.disabled = this.data.readOnly;
element.name = this.data.fieldName;

Expand Down
3 changes: 3 additions & 0 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { AnnotationLayer } from "./display/annotation_layer.js";
import { apiCompatibilityParams } from "./display/api_compatibility.js";
import { GlobalWorkerOptions } from "./display/worker_options.js";
import { renderTextLayer } from "./display/text_layer.js";
import { Scripting } from "./pdf.scripting_api.js";
import { SVGGraphics } from "./display/svg.js";

/* eslint-disable-next-line no-unused-vars */
Expand Down Expand Up @@ -164,4 +165,6 @@ export {
renderTextLayer,
// From "./display/svg.js":
SVGGraphics,
// From "./pdf.scripting_api.js"
Scripting,
};
24 changes: 24 additions & 0 deletions src/pdf.scripting_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright 2020 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.
*/

import { generateCode } from "./scripting_api/code_gen.js";

const Scripting = {
getCode(objects) {
return generateCode(objects);
},
};

export { Scripting };
Loading

0 comments on commit 2832e17

Please sign in to comment.