From d256168b62d95d81db4bde718cbc1a28921bdb8e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 3 Apr 2023 08:57:45 +0200 Subject: [PATCH] Reduce duplication when dispatching the "switchannotationeditorparams" event Currently we repeat virtually the same code multiple times, which can be avoided by the introduction of a simple helper function. --- web/annotation_editor_params.js | 41 +++++++++++---------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/web/annotation_editor_params.js b/web/annotation_editor_params.js index c6a085ec3..60e3560f6 100644 --- a/web/annotation_editor_params.js +++ b/web/annotation_editor_params.js @@ -32,40 +32,27 @@ class AnnotationEditorParams { editorInkThickness, editorInkOpacity, }) { - editorFreeTextFontSize.addEventListener("input", evt => { + const dispatchEvent = (typeStr, value) => { this.eventBus.dispatch("switchannotationeditorparams", { source: this, - type: AnnotationEditorParamsType.FREETEXT_SIZE, - value: editorFreeTextFontSize.valueAsNumber, + type: AnnotationEditorParamsType[typeStr], + value, }); + }; + editorFreeTextFontSize.addEventListener("input", function () { + dispatchEvent("FREETEXT_SIZE", this.valueAsNumber); }); - editorFreeTextColor.addEventListener("input", evt => { - this.eventBus.dispatch("switchannotationeditorparams", { - source: this, - type: AnnotationEditorParamsType.FREETEXT_COLOR, - value: editorFreeTextColor.value, - }); + editorFreeTextColor.addEventListener("input", function () { + dispatchEvent("FREETEXT_COLOR", this.value); }); - editorInkColor.addEventListener("input", evt => { - this.eventBus.dispatch("switchannotationeditorparams", { - source: this, - type: AnnotationEditorParamsType.INK_COLOR, - value: editorInkColor.value, - }); + editorInkColor.addEventListener("input", function () { + dispatchEvent("INK_COLOR", this.value); }); - editorInkThickness.addEventListener("input", evt => { - this.eventBus.dispatch("switchannotationeditorparams", { - source: this, - type: AnnotationEditorParamsType.INK_THICKNESS, - value: editorInkThickness.valueAsNumber, - }); + editorInkThickness.addEventListener("input", function () { + dispatchEvent("INK_THICKNESS", this.valueAsNumber); }); - editorInkOpacity.addEventListener("input", evt => { - this.eventBus.dispatch("switchannotationeditorparams", { - source: this, - type: AnnotationEditorParamsType.INK_OPACITY, - value: editorInkOpacity.valueAsNumber, - }); + editorInkOpacity.addEventListener("input", function () { + dispatchEvent("INK_OPACITY", this.valueAsNumber); }); this.eventBus._on("annotationeditorparamschanged", evt => {