From d2e23d6b15f4e93c17aa804653f2b487328319c6 Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Mon, 24 Oct 2022 20:02:49 -0700 Subject: [PATCH] Clean up Vue component refs (#813) * Clean up Vue component refs * Second pass of code improvements --- .../components/floating-menus/ColorPicker.vue | 2 +- .../components/floating-menus/DialogModal.vue | 4 +- .../floating-menus/EyedropperPreview.vue | 4 +- .../components/floating-menus/MenuList.vue | 13 ++-- .../src/components/layout/FloatingMenu.vue | 63 +++++++-------- frontend/src/components/panels/Document.vue | 30 ++++---- frontend/src/components/panels/LayerTree.vue | 33 ++++---- frontend/src/components/panels/NodeGraph.vue | 76 ++++++++++--------- .../widgets/inputs/CheckboxInput.vue | 7 +- .../widgets/inputs/DropdownInput.vue | 11 +-- .../components/widgets/inputs/FieldInput.vue | 22 ++++++ .../components/widgets/inputs/FontInput.vue | 31 ++++---- .../widgets/inputs/MenuBarInput.vue | 16 ++-- .../components/widgets/inputs/NumberInput.vue | 15 ++-- .../widgets/inputs/TextAreaInput.vue | 14 ++-- .../components/widgets/inputs/TextInput.vue | 19 ++--- .../widgets/metrics/CanvasRuler.vue | 8 +- .../widgets/metrics/PersistentScrollbar.vue | 35 ++++++--- .../src/components/window/workspace/Panel.vue | 15 +++- .../components/window/workspace/Workspace.vue | 28 ++++--- frontend/src/wasm-communication/messages.ts | 2 +- 21 files changed, 253 insertions(+), 195 deletions(-) diff --git a/frontend/src/components/floating-menus/ColorPicker.vue b/frontend/src/components/floating-menus/ColorPicker.vue index 9e2cf743..236938d2 100644 --- a/frontend/src/components/floating-menus/ColorPicker.vue +++ b/frontend/src/components/floating-menus/ColorPicker.vue @@ -135,7 +135,7 @@ export default defineComponent({ const hsva = this.color.toHSVA(); return { - draggingPickerTrack: undefined as HTMLElement | undefined, + draggingPickerTrack: undefined as HTMLDivElement | undefined, hue: hsva.h, saturation: hsva.s, value: hsva.v, diff --git a/frontend/src/components/floating-menus/DialogModal.vue b/frontend/src/components/floating-menus/DialogModal.vue index 6d79e042..1d9a58df 100644 --- a/frontend/src/components/floating-menus/DialogModal.vue +++ b/frontend/src/components/floating-menus/DialogModal.vue @@ -84,8 +84,8 @@ export default defineComponent({ }, mounted() { // Focus the first button in the popup - const element = this.$el as Element | undefined; - const emphasizedOrFirstButton = (element?.querySelector("[data-emphasized]") || element?.querySelector("[data-text-button]") || undefined) as HTMLButtonElement | undefined; + const dialogModal: HTMLDivElement | undefined = this.$el; + const emphasizedOrFirstButton = (dialogModal?.querySelector("[data-emphasized]") || dialogModal?.querySelector("[data-text-button]") || undefined) as HTMLButtonElement | undefined; emphasizedOrFirstButton?.focus(); }, components: { diff --git a/frontend/src/components/floating-menus/EyedropperPreview.vue b/frontend/src/components/floating-menus/EyedropperPreview.vue index c6a9ab75..cc2352c4 100644 --- a/frontend/src/components/floating-menus/EyedropperPreview.vue +++ b/frontend/src/components/floating-menus/EyedropperPreview.vue @@ -113,7 +113,9 @@ export default defineComponent({ }, methods: { displayImageDataPreview(imageData: ImageData | undefined) { - const canvas = this.$refs.zoomPreviewCanvas as HTMLCanvasElement; + const canvas = this.$refs.zoomPreviewCanvas as HTMLCanvasElement | undefined; + if (!canvas) return; + canvas.width = ZOOM_WINDOW_DIMENSIONS; canvas.height = ZOOM_WINDOW_DIMENSIONS; const context = canvas.getContext("2d"); diff --git a/frontend/src/components/floating-menus/MenuList.vue b/frontend/src/components/floating-menus/MenuList.vue index 4b2802f6..565e5e54 100644 --- a/frontend/src/components/floating-menus/MenuList.vue +++ b/frontend/src/components/floating-menus/MenuList.vue @@ -46,7 +46,7 @@ :direction="'TopRight'" :entries="entry.children" v-bind="{ minWidth, drawIcon, scrollableY }" - :ref="(ref: MenuListInstance) => ref && (entry.ref = ref)" + :ref="(ref: MenuListInstance): void => (ref && (entry.ref = ref), undefined)" /> @@ -204,15 +204,17 @@ const MenuList = defineComponent({ this.$emit("update:open", newIsOpen); }, entries() { - const floatingMenu = this.$refs.floatingMenu as typeof FloatingMenu; - floatingMenu.measureAndEmitNaturalWidth(); + (this.$refs.floatingMenu as typeof FloatingMenu | undefined)?.measureAndEmitNaturalWidth(); }, drawIcon() { - const floatingMenu = this.$refs.floatingMenu as typeof FloatingMenu; - floatingMenu.measureAndEmitNaturalWidth(); + (this.$refs.floatingMenu as typeof FloatingMenu | undefined)?.measureAndEmitNaturalWidth(); }, }, methods: { + scrollViewTo(distanceDown: number): void { + const scroller: HTMLDivElement | undefined = (this.$refs.scroller as typeof LayoutCol | undefined)?.$el; + scroller?.scrollTo(0, distanceDown); + }, onEntryClick(menuListEntry: MenuListEntry): void { // Call the action if available if (menuListEntry.action) menuListEntry.action(); @@ -242,7 +244,6 @@ const MenuList = defineComponent({ return this.open; }, - /// Handles keyboard navigation for the menu. Returns if the entire menu stack should be dismissed keydown(e: KeyboardEvent, submenu: boolean): boolean { // Interactive menus should keep the active entry the same as the highlighted one diff --git a/frontend/src/components/layout/FloatingMenu.vue b/frontend/src/components/layout/FloatingMenu.vue index 522389cd..aaeb1dfd 100644 --- a/frontend/src/components/layout/FloatingMenu.vue +++ b/frontend/src/components/layout/FloatingMenu.vue @@ -1,6 +1,6 @@