Skip to content

Commit 12e6cd3

Browse files
committed
JS - Add the basic architecture to be able to execute embedded js
1 parent bdd3e01 commit 12e6cd3

16 files changed

+1335
-0
lines changed

gulpfile.js

+20
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,23 @@ function createWebBundle(defines) {
355355
return gulp.src("./web/viewer.js").pipe(webpack2Stream(viewerFileConfig));
356356
}
357357

358+
function createScriptingApiBundle(defines) {
359+
var scriptingApiAMDName = "pdfjs-dist/build/pdf.scripting_api";
360+
var scriptingApiOutputName = "pdf.scripting_api.js";
361+
362+
var scriptingApiFileConfig = createWebpackConfig(defines, {
363+
filename: scriptingApiOutputName,
364+
library: scriptingApiAMDName,
365+
libraryTarget: "umd",
366+
umdNamedDefine: true,
367+
});
368+
return gulp
369+
.src("./src/pdf.scripting_api.js")
370+
.pipe(webpack2Stream(scriptingApiFileConfig))
371+
.pipe(replaceWebpackRequire())
372+
.pipe(replaceJSRootName(scriptingApiAMDName, "pdfjsScriptingApi"));
373+
}
374+
358375
function createComponentsBundle(defines) {
359376
var componentsAMDName = "pdfjs-dist/web/pdf_viewer";
360377
var componentsOutputName = "pdf_viewer.js";
@@ -1052,6 +1069,9 @@ gulp.task(
10521069
createWorkerBundle(defines).pipe(
10531070
gulp.dest(MOZCENTRAL_CONTENT_DIR + "build")
10541071
),
1072+
createScriptingApiBundle(defines).pipe(
1073+
gulp.dest(MOZCENTRAL_CONTENT_DIR + "build")
1074+
),
10551075
createWebBundle(defines).pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")),
10561076
gulp
10571077
.src(MOZCENTRAL_COMMON_WEB_FILES, { base: "web/" })

src/display/annotation_layer.js

+35
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
462462
element.setAttribute("value", textContent);
463463
}
464464

465+
element.setAttribute("id", id);
466+
465467
element.addEventListener("input", function (event) {
466468
storage.setValue(id, event.target.value);
467469
});
@@ -470,6 +472,39 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
470472
event.target.setSelectionRange(0, 0);
471473
});
472474

475+
if (this.data.actions) {
476+
element.addEventListener("updateFromSandbox", function (event) {
477+
const data = event.detail;
478+
if ("value" in data) {
479+
event.target.value = event.detail.value;
480+
} else if ("focus" in data) {
481+
event.target.focus({ preventScroll: false });
482+
}
483+
});
484+
485+
for (const eventType of Object.keys(this.data.actions)) {
486+
switch (eventType) {
487+
case "Format":
488+
element.addEventListener("blur", function (event) {
489+
window.dispatchEvent(
490+
new CustomEvent("dispatchEventInSandbox", {
491+
detail: {
492+
field: "value",
493+
id,
494+
event: {
495+
type: "Field",
496+
name: "Format",
497+
value: event.target.value,
498+
},
499+
},
500+
})
501+
);
502+
});
503+
break;
504+
}
505+
}
506+
}
507+
473508
element.disabled = this.data.readOnly;
474509
element.name = this.data.fieldName;
475510

src/pdf.scripting_api.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright 2020 Mozilla Foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import { generateCode } from "./scripting_api/code_gen.js";
17+
18+
const Scripting = {
19+
getCode(objects) {
20+
return generateCode(objects);
21+
},
22+
};
23+
24+
export { Scripting };

0 commit comments

Comments
 (0)