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

Fix issue with button JavaScript execution #19398

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
34 changes: 33 additions & 1 deletion gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import {
import { exec, execSync, spawn, spawnSync } from "child_process";
import autoprefixer from "autoprefixer";
import babel from "@babel/core";
import chokidar from "chokidar";
import crypto from "crypto";
import { fileURLToPath } from "url";
import fs from "fs";
import gulp from "gulp";
import hljs from "highlight.js";
import layouts from "@metalsmith/layouts";
import lodash from "lodash"; // Destructure the required function
import markdown from "@metalsmith/markdown";
import Metalsmith from "metalsmith";
import ordered from "ordered-read-streams";
Expand All @@ -45,6 +46,9 @@ import Vinyl from "vinyl";
import webpack2 from "webpack";
import webpackStream from "webpack-stream";
import zip from "gulp-zip";
// eslint-disable-next-line sort-imports
import { fileURLToPath } from "url"; // Import the default export
const { debounce } = lodash;

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -103,6 +107,27 @@ const BABEL_PRESET_ENV_OPTS = Object.freeze({
useBuiltIns: "usage",
});

// Task to rebuild the scripts
function rebuildScripts(done) {
console.log("Rebuilding scripts...");
exec("npx gulp generic", (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${stderr}`);
} else {
console.log(stdout);
}
done(); // Always call done() to ensure the watcher continues
});
}

// Debounced version of rebuildScripts
const debouncedRebuildScripts = debounce(rebuildScripts, 500); // 500ms debounce

// Task to watch for changes in the source files
chokidar
.watch("src/**/*.js", { ignoreInitial: true })
.on("all", debouncedRebuildScripts);

const DEFINES = Object.freeze({
SKIP_BABEL: true,
TESTING: undefined,
Expand Down Expand Up @@ -2539,3 +2564,10 @@ gulp.task("externaltest", function (done) {
});
done();
});

// Export the watch task
function watchFiles() {
gulp.watch("src/**/*.js", rebuildScripts);
}

export const watch = gulp.series(rebuildScripts, watchFiles);
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"autoprefixer": "^10.4.20",
"babel-loader": "^9.2.1",
"caniuse-lite": "^1.0.30001692",
"chokidar": "^4.0.3",
"core-js": "^3.40.0",
"eslint": "^9.18.0",
"eslint-plugin-import": "^2.31.0",
Expand Down
97 changes: 53 additions & 44 deletions src/scripting_api/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,57 +71,66 @@ function initSandbox(params) {
const appObjects = app._objects;

if (data.objects) {
const annotations = [];

for (const [name, objs] of Object.entries(data.objects)) {
annotations.length = 0;
let container = null;

for (const obj of objs) {
if (obj.type !== "") {
annotations.push(obj);
} else {
container = obj;
// Separate annotations and container
const annotations = objs.filter(obj => obj.type !== "");
const container = objs.find(obj => obj.type === "");

// Process each annotation (button, checkbox, etc.)
for (const obj of annotations) {
obj.send = send; // Assign the send function to each annotation
obj.globalEval = globalEval;
obj.doc = _document;
obj.fieldPath = name;
obj.appObjects = appObjects;

// Handle siblings (if any)
const otherFields = annotations.filter(x => x !== obj);
if (otherFields.length > 0) {
obj.siblings = otherFields.map(x => x.id);
}
}

let obj = container;
if (annotations.length > 0) {
obj = annotations[0];
obj.send = send;
}

obj.globalEval = globalEval;
obj.doc = _document;
obj.fieldPath = name;
obj.appObjects = appObjects;

const otherFields = annotations.slice(1);

let field;
switch (obj.type) {
case "radiobutton": {
field = new RadioButtonField(otherFields, obj);
break;
}
case "checkbox": {
field = new CheckboxField(otherFields, obj);
break;
// Create a Field instance based on the type
let field;
switch (obj.type) {
case "radiobutton":
field = new RadioButtonField(
otherFields.map(x => x.id),
obj
); // Pass sibling IDs
break;
case "checkbox":
field = new CheckboxField(
otherFields.map(x => x.id),
obj
); // Pass sibling IDs
break;
default:
field = new Field(obj);
}
default:
if (otherFields.length > 0) {
obj.siblings = otherFields.map(x => x.id);
}
field = new Field(obj);
}

const wrapped = new Proxy(field, proxyHandler);
const _object = { obj: field, wrapped };
doc._addField(name, _object);
for (const object of objs) {
appObjects[object.id] = _object;
// Wrap the field in a Proxy and add it to doc and appObjects
const wrapped = new Proxy(field, proxyHandler);
const _object = { obj: field, wrapped };
doc._addField(name, _object);
appObjects[obj.id] = _object;
}

// Handle the container (if it exists)
if (container) {
container.send = send;
container.globalEval = globalEval;
container.doc = _document;
container.fieldPath = name;
container.appObjects = appObjects;

// Create a Field instance for the container
const field = new Field(container);
const wrapped = new Proxy(field, proxyHandler);
const _object = { obj: field, wrapped };

// Add the container to doc and appObjects
doc._addField(name, _object);
appObjects[container.id] = _object;
}
}
Expand Down