What type of proxy in vue-konva contains the getNode() method?

Currently, I am developing an application using Vue 3's Composition API with TypeScript.

Within my view, I aim to retrieve the new position of a dragged v-group. To achieve this, I need to obtain a reference to the v-group and remove it from the vue-konva proxy using the getNode() function. Since I am utilizing TypeScript, understanding the type of element obtained from the v-group reference is crucial.

Unfortunately, I have been unable to locate documentation regarding the vue-konva proxy utilized for each konva object.

Here is my existing code snippet:

<template>
  <v-stage :config="configKonva">
    <v-layer>
      <v-group ref="mainGroupProxy" :config="mainGroupConfig" @dragend="groupDragEnd">
        <v-rect :config="getPixelConfig" />
      </v-group>
    </v-layer>
  </v-stage>
</template>

<script setup lang="ts">
import Konva from "konva";
import { computed, ref } from "vue";

const configKonva = computed(() => ({
  width: 100,
  height: 100,
}));
const mainGroupConfig = computed(() => {
  return {
    x: 0,
    y: 0,
    draggable: true,
  };
});
function getPixelConfig() {
  return {
    x: 0,
    y: 0,
    width: 5,
    height: 5,
    fill: "black",
    stroke: "transparent",
  };
}

const mainGroupProxy = ref<Konva.Group | null>(null); // Encountering issue here
function groupDragEnd() {
  if (mainGroupProxy.value) {
    console.log("mainGroupProxy.value: ", mainGroupProxy.value); // returns proxy
    const mainGroup = mainGroupProxy.value.getNode(); // necessary to unwrap Konva node from Vue proxy
    console.log("X position: " + mainGroup.getX());
  }
}
</script>

The current implementation produces the following outcomes upon drag end:

mainGroupProxy.value: Proxy(Object) {__v_skip: true, getStage: ƒ, getNode: ƒ}

X position: 91.00000546773309

However, these results are inaccurate since the type of mainGroup should encompass the vue-konva type as well, like so:

const mainGroupProxy= ref<ProxyOfVueKonva<Konva.Group> | null>(null);

Where ProxyOfVueKonva denotes the specific proxy type. The absence of this type leads to TypeScript errors.

Answer №1

To address the issue, I had to generate the necessary type. As a result, the modified code snippet appears as follows:

import { Transformer } from "konva/lib/shapes/Transformer";
type VueKonvaProxy<Group> = { getNode(): Group }; // This solution resolved the issue
const mainGroupProxy = ref<VueKonvaProxy<Konva.Group> | null>(null);
function groupDragEnd() {
    if (mainGroupProxy.value) {
        console.log("mainGroupProxy.value: " + mainGroupProxy.value);
        const mainGroup = mainGroupProxy.value.getNode() as Transformer; // The type Transformer must be specified for TypeScript to enable the use of required function
        console.log("X position: " + mainGroup.getX());
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular vs. Typescript: What gets assigned to the $scope variable?

As I work on integrating trNgGrid into my angular app, the challenge arises when attempting to bind a function from the controller to an ng-click on a row. This difficulty stems from the fact that the table body is built by itself. The solution proposed in ...

What is the best way to focus on a particular version of TypeScript?

After upgrading my Angular 2 project to RC1 and the router to v3 alpha3, I encountered the following errors: node_modules/@angular/router/directives/router_outlet.d.ts(10,14): error TS1005: '=' expected. It appears to be a TypeScript version is ...

An error occurred: The property 'toUpperCase' cannot be read of an undefined Observable in an uncaught TypeError

Encountering an issue during the development of a mobile app using the ionic framework for a movie application. After finding an example on Github, I attempted to integrate certain functions and designs into my project. The problem arises with the 'S ...

At compile time, Typescript runs smoothly, but errors may arise during runtime

Currently, I am delving into learning Typescript and have encountered a snag in my code. Despite searching extensively for a solution, I have been unable to find any relevant material pertaining to my issue. Below is the code snippet in question: <code ...

Struggling to combine interface with import and local variables - any solutions?

Although examples have demonstrated the merging of interfaces in a single file, I am facing challenges when trying to merge interfaces that are located in different files. I want to clarify that I am not extending any modules, just interfaces. /types/ind ...

Injecting live data into an input field within a table using Angular 4

Trying to create a dynamic row table with input fields in all cells. The loaded data is static, and I'm facing issues adding more data in the view. However, the functionality to add and delete rows is working fine. I have experimented with ngModel and ...

Setting the sidebar width for Nebular with two sidebars in the layout: A step-by-step guide

Having two sidebars (identified as left and right) in my page layout, I initially set both sidebars to a width of 400px using the custom theme method with sidebar-width: 400px. However, I now need to change the width of the right sidebar to 700px. Is the ...

Retrieving attributes from a reactive object in TypeScript

I have a question regarding accessing values in Typescript. Whenever I load my website, I make a call to a service that fetches user data. private currentUserSource = new ReplaySubject<IUser>(1); currentUser$ = this.currentUserSource.asObservable ...

An unexpected error occurred while running ng lint in an Angular project

I've encountered an issue while trying to run ng lint on my Angular project. After migrating from TSLint to ESLint, I'm getting the following error message when running ng lint: An unhandled exception occurred: Invalid lint configuration. Nothing ...

Step-by-step guide for adding an object to a Material UI select component

I am in the process of incorporating a Select component with reactjs material ui and typescript. Yet, I encounter this typing error: Property 'id' does not exist on type 'string'. Property 'name' does not exist on type ' ...

`Why does the npm test command in vue2 source code fail with the error "Type ... is not assignable to type ..."?`

After cloning the Vue source code from github using git clone, I proceeded to install dependencies by running yarn. However, when I ran npm test, the test failed. https://i.sstatic.net/aZXBg.png Does anyone have insight on why this error occurred and how ...

Retrieve a variable in a child component by passing it down from the parent component and triggering it from the parent

I'm struggling to grasp this concept. In my current scenario, I pass two variables to a component like this: <app-selectcomp [plid]="plid" [codeId]="selectedCode" (notify)="getCompFromChild($event)"></app-select ...

Issue encountered while initializing a fresh project with Angular CLI version 13.1.0

I encountered an issue while trying to create a new project with Angular CLI v13.1.0 \ Installing packages (npm)...npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/ema ...

Idiosyncratic TypeScript behavior: Extending a class with comparable namespace structure

Lately, I've been working on creating a type library for a JavaScript written library. As I was defining all the namespaces, classes, and interfaces, I encountered an error TS2417 with some of the classes. I double-checked for any issues with method o ...

What is the best approach to incorporate a refresh feature for a data stream?

I need to implement a function that updates the current list of items$ and allows for awaiting refreshItems(). This is my working implementation: private readStream = new Subject<T[]>(); readStream$ = this.readStream.asObservable(); getItems = (): ...

What is the correct way to include a new property in the MUI Link component using TypeScript?

Currently, within my mui-theme.ts configuration file, I have the following setup: const theme = createTheme(globalTheme, { components: { MuiLink: { variants: [ { props: { hover: 'lightup' }, style: { ...

Having difficulties utilizing React Syntax Highlighter in Next.js 13 with Sanity version 3

Hello there, I've encountered a problem with my project while using Sanity v3 and React Syntax Highlighter. Initially, I had success displaying my code in the browser by utilizing the Refactor library after following a tutorial on the Code Input by Sa ...

Exclude promises from a type in TypeScript

Here is my TypeScript snippet: async function get_string(){ return "hello" } var the_string = get_string() In the code, the type of the_string is Promise<string> I am interested in removing the Promise from the type of the_string and m ...

Having trouble running Vue component tests with Vitest

I need help with my project setup which includes: Using Vue 3 Webpack as the development server Vitest and Vue Test Utils for testing utilities While I can write and run tests for simple JavaScript functions without issues, I encounter an error when tryi ...

Form validation errors were detected

Currently, I am working with a formgroup that contains input fields with validations set up in the following manner: <mat-form-field class="mat-width-98" appearance="outline"> <mat-label>Profession Oc ...