Skip to content

Commit

Permalink
Update the events, used with scripting, to use lower-case names and a…
Browse files Browse the repository at this point in the history
…void using DOM events internally in the viewer

For DOM events all event names are lower-case, and the newly added PDF.js scripting-events thus "stick out" quite a bit. Even more so, considering that our internal `eventBus`-events follow the same naming convention.
Hence this patch, which changes the "updateFromSandbox"/"dispatchEventInSandbox" events to be lower-case instead.

Furthermore, using DOM events for communication *within* the PDF.js code itself (i.e. between code in `web/app.js` and `src/display/annotation_layer.js/`) feels *really* out of place.
That's exactly the reason that we have the `EventBus` abstraction, since it allowed us to remove prior use of DOM events, and this patch thus re-factors the code to make use of the `EventBus` instead for scripting-related events.
Obviously for events targeting a *specific element* using DOM events is still fine, but the "updatefromsandbox"/"dispatcheventinsandbox" ones should be using the `EventBus` internally.
  • Loading branch information
Snuffleupagus committed Dec 17, 2020
1 parent d5d215a commit 7cc7683
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 115 deletions.
6 changes: 3 additions & 3 deletions external/quickjs/quickjs-eval.js

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

171 changes: 81 additions & 90 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,13 @@ class LinkAnnotationElement extends AnnotationElement {
continue;
}
link[jsName] = () => {
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id: data.id,
name,
},
})
);
this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
source: this,
detail: {
id: data.id,
name,
},
});
return false;
};
}
Expand Down Expand Up @@ -551,30 +550,28 @@ class WidgetAnnotationElement extends AnnotationElement {
if (baseName.includes("mouse")) {
// Mouse events
element.addEventListener(baseName, event => {
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id: this.data.id,
name: eventName,
value: valueGetter(event),
shift: event.shiftKey,
modifier: this._getKeyModifier(event),
},
})
);
this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
source: this,
detail: {
id: this.data.id,
name: eventName,
value: valueGetter(event),
shift: event.shiftKey,
modifier: this._getKeyModifier(event),
},
});
});
} else {
// Non mouse event
element.addEventListener(baseName, event => {
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id: this.data.id,
name: eventName,
value: event.target.checked,
},
})
);
this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
source: this,
detail: {
id: this.data.id,
name: eventName,
value: event.target.checked,
},
});
});
}
}
Expand Down Expand Up @@ -650,7 +647,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
}
});

element.addEventListener("updateFromSandbox", function (event) {
element.addEventListener("updatefromsandbox", function (event) {
const { detail } = event;
const actions = {
value() {
Expand Down Expand Up @@ -721,39 +718,37 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
}
// Save the entered value
elementData.userValue = event.target.value;
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
source: this,
detail: {
id,
name: "Keystroke",
value: event.target.value,
willCommit: true,
commitKey,
selStart: event.target.selectionStart,
selEnd: event.target.selectionEnd,
},
});
});
const _blurListener = blurListener;
blurListener = null;
element.addEventListener("blur", event => {
if (this._mouseState?.isDown) {
// Focus out using the mouse: data are committed
elementData.userValue = event.target.value;
this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
source: this,
detail: {
id,
name: "Keystroke",
value: event.target.value,
willCommit: true,
commitKey,
commitKey: 1,
selStart: event.target.selectionStart,
selEnd: event.target.selectionEnd,
},
})
);
});
const _blurListener = blurListener;
blurListener = null;
element.addEventListener("blur", event => {
if (this._mouseState?.isDown) {
// Focus out using the mouse: data are committed
elementData.userValue = event.target.value;
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id,
name: "Keystroke",
value: event.target.value,
willCommit: true,
commitKey: 1,
selStart: event.target.selectionStart,
selEnd: event.target.selectionEnd,
},
})
);
});
}
_blurListener(event);
});
Expand Down Expand Up @@ -783,19 +778,18 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
if (elementData.beforeInputSelectionRange) {
[selStart, selEnd] = elementData.beforeInputSelectionRange;
}
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id,
name: "Keystroke",
value: elementData.beforeInputValue,
change: event.data,
willCommit: false,
selStart,
selEnd,
},
})
);
this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
source: this,
detail: {
id,
name: "Keystroke",
value: elementData.beforeInputValue,
change: event.data,
willCommit: false,
selStart,
selEnd,
},
});
});
}

Expand Down Expand Up @@ -929,7 +923,7 @@ class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
});

if (this.enableScripting && this.hasJSActions) {
element.addEventListener("updateFromSandbox", event => {
element.addEventListener("updatefromsandbox", event => {
const { detail } = event;
const actions = {
value() {
Expand Down Expand Up @@ -1010,7 +1004,7 @@ class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
});

if (this.enableScripting && this.hasJSActions) {
element.addEventListener("updateFromSandbox", event => {
element.addEventListener("updatefromsandbox", event => {
const { detail } = event;
const actions = {
value() {
Expand Down Expand Up @@ -1132,7 +1126,7 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
}

if (this.enableScripting && this.hasJSActions) {
selectElement.addEventListener("updateFromSandbox", event => {
selectElement.addEventListener("updatefromsandbox", event => {
const { detail } = event;
const actions = {
value() {
Expand Down Expand Up @@ -1166,18 +1160,17 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
const value = getValue(event);
storage.setValue(id, { value });

window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id,
name: "Keystroke",
changeEx: value,
willCommit: true,
commitKey: 1,
keyDown: false,
},
})
);
this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
source: this,
detail: {
id,
name: "Keystroke",
changeEx: value,
willCommit: true,
commitKey: 1,
keyDown: false,
},
});
});

this._setEventListeners(
Expand Down Expand Up @@ -1852,14 +1845,12 @@ class FileAttachmentAnnotationElement extends AnnotationElement {
this.filename = getFilenameFromUrl(filename);
this.content = content;

if (this.linkService.eventBus) {
this.linkService.eventBus.dispatch("fileattachmentannotation", {
source: this,
id: stringToPDFString(filename),
filename,
content,
});
}
this.linkService.eventBus?.dispatch("fileattachmentannotation", {
source: this,
id: stringToPDFString(filename),
filename,
content,
});
}

render() {
Expand Down
Loading

0 comments on commit 7cc7683

Please sign in to comment.