Skip to content

Commit

Permalink
Make stage x and y props bindable. Fix stage bindable tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TeyKey1 committed Jul 31, 2024
1 parent 839841e commit 3ffb059
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
18 changes: 8 additions & 10 deletions src/lib/Stage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ Further information: [Konva API docs](https://konvajs.org/api/Konva.Stage.html),
ontransformend,
ontransformstart,
onwheel,
y,
x,
width,
visible,
skewY,
skewX,
scaleY,
scaleX,
x = $bindable(),
y = $bindable(),
scale,
scaleX,
scaleY,
rotation,
skewX,
skewY,
width,
visible,
preventDefault,
opacity,
offsetY,
Expand Down Expand Up @@ -104,7 +104,6 @@ Further information: [Konva API docs](https://konvajs.org/api/Konva.Stage.html),
let isReady = $state(false);
onMount(() => {
console.log(x, y);
_handle = new Konva.Stage({
container: stage,
y,
Expand Down Expand Up @@ -156,7 +155,6 @@ Further information: [Konva API docs](https://konvajs.org/api/Konva.Stage.html),
});
$effect(() => {
_handle!.setAttr('width', width);
console.log('stage change width', width);
});
$effect(() => {
_handle!.setAttr('visible', visible);
Expand Down
6 changes: 3 additions & 3 deletions src/tests/stage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import './mocks/mouse';
import type { MockStage } from './mocks/mouse';

// Test Component Wrappers
import ConfigBinding from './wrappers/ConfigBinding.test.svelte';
import ConfigBindingStage from './wrappers/ConfigBindingStage.test.svelte';
import ContainerContext from './wrappers/ContainerContext.test.svelte';

test('creates a div container and forwards rest props to div', () => {
Expand Down Expand Up @@ -96,7 +96,7 @@ test('Correctly updates bound config on dragend', () => {
const yWritable = writable(CONFIG.y);
let handle: MockStage | null = null;

render(ConfigBinding, {
render(ConfigBindingStage, {
props: {
component: Stage,
...CONFIG,
Expand All @@ -121,7 +121,7 @@ test('Does not update config if instantiated with staticConfig prop', async () =
const yWritable = writable(CONFIG.y);
let handle: MockStage | null = null;

render(ConfigBinding, {
render(ConfigBindingStage, {
props: {
component: Stage,
...CONFIG,
Expand Down
48 changes: 48 additions & 0 deletions src/tests/wrappers/ConfigBindingStage.test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--
@component
Wraps the to be tested svelte-konva component so that the binding of the config prop can be tested
Special component for svelte-konva stage component, as it does only support binding on `x` and `y` props.
-->
<script lang="ts">
import { onMount } from 'svelte';
import type { Writable } from 'svelte/store';
let {
component,
getHandle,
staticConfig = false,
x,
y,
scale,
scaleX,
scaleY,
rotation,
skewX,
skewY,
...restProps
}: {
component: any;
x: Writable<any>;
y: Writable<any>;
getHandle: (handle: any) => void;
staticConfig?: boolean;
[key: string]: any;
} = $props();
let boundComponent: any;
onMount(() => {
// Once we have the inner component handle of the svelte-konva component we pass it to the callback to get access to it in the test function
getHandle(boundComponent.handle);
});
</script>

<svelte:component
this={component}
bind:this={boundComponent}
bind:x={$x}
bind:y={$y}
{staticConfig}
{...restProps}
></svelte:component>

0 comments on commit 3ffb059

Please sign in to comment.