Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent user from overwriting read-only handle #136

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Unreleased

- -
- Improved handle prop safety by preventing the user from overwriting the internal handle of the svelte-konva components (see #136)

# v0.3.1

Expand Down
23 changes: 12 additions & 11 deletions src/lib/Arc.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,34 @@ Further information: [Konva API docs](https://konvajs.org/api/Konva.Arc.html), [
interface $$Events extends KonvaEvents {}

export let config: Konva.ArcConfig;
export let handle = new Konva.Arc(config);
const _handle = new Konva.Arc(config); // Hide inner handle behind a shadow variable to prevent users from overwriting it
export const handle = _handle;
export let staticConfig = false;

let parent: Writable<null | KonvaParent> = getParentContainer();
let dispatcher = createEventDispatcher();
const parent: Writable<null | KonvaParent> = getParentContainer();
const dispatcher = createEventDispatcher();

$: handle.setAttrs(config);
$: _handle.setAttrs(config);

onMount(() => {
$parent!.add(handle);
$parent!.add(_handle);

if (!staticConfig) {
handle.on('transformend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('transformend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});

handle.on('dragend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('dragend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});
}

registerEvents(dispatcher, handle);
registerEvents(dispatcher, _handle);
});

onDestroy(() => {
handle.destroy();
_handle.destroy();
});
</script>
23 changes: 12 additions & 11 deletions src/lib/Arrow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,34 @@ Further information: [Konva API docs](https://konvajs.org/api/Konva.Arrow.html),
interface $$Events extends KonvaEvents {}

export let config: Konva.ArrowConfig;
export let handle = new Konva.Arrow(config);
const _handle = new Konva.Arrow(config); // Hide inner handle behind a shadow variable to prevent users from overwriting it
export const handle = _handle;
export let staticConfig = false;

let parent: Writable<null | KonvaParent> = getParentContainer();
let dispatcher = createEventDispatcher();
const parent: Writable<null | KonvaParent> = getParentContainer();
const dispatcher = createEventDispatcher();

$: handle.setAttrs(config);
$: _handle.setAttrs(config);

onMount(() => {
$parent!.add(handle);
$parent!.add(_handle);

if (!staticConfig) {
handle.on('transformend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('transformend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});

handle.on('dragend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('dragend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});
}

registerEvents(dispatcher, handle);
registerEvents(dispatcher, _handle);
});

onDestroy(() => {
handle.destroy();
_handle.destroy();
});
</script>
23 changes: 12 additions & 11 deletions src/lib/Circle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,34 @@ Further information: [Konva API docs](https://konvajs.org/api/Konva.Circle.html)
interface $$Events extends KonvaEvents {}

export let config: Konva.CircleConfig;
export let handle = new Konva.Circle(config);
const _handle = new Konva.Circle(config); // Hide inner handle behind a shadow variable to prevent users from overwriting it
export const handle = _handle;
export let staticConfig = false;

let parent: Writable<null | KonvaParent> = getParentContainer();
let dispatcher = createEventDispatcher();
const parent: Writable<null | KonvaParent> = getParentContainer();
const dispatcher = createEventDispatcher();

$: handle.setAttrs(config);
$: _handle.setAttrs(config);

onMount(() => {
$parent!.add(handle);
$parent!.add(_handle);

if (!staticConfig) {
handle.on('transformend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('transformend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});

handle.on('dragend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('dragend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});
}

registerEvents(dispatcher, handle);
registerEvents(dispatcher, _handle);
});

onDestroy(() => {
handle.destroy();
_handle.destroy();
});
</script>
23 changes: 12 additions & 11 deletions src/lib/Ellipse.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,34 @@ Further information: [Konva API docs](https://konvajs.org/api/Konva.Ellipse.html
interface $$Events extends KonvaEvents {}

export let config: Konva.EllipseConfig;
export let handle = new Konva.Ellipse(config);
const _handle = new Konva.Ellipse(config); // Hide inner handle behind a shadow variable to prevent users from overwriting it
export const handle = _handle;
export let staticConfig = false;

let parent: Writable<null | KonvaParent> = getParentContainer();
let dispatcher = createEventDispatcher();
const parent: Writable<null | KonvaParent> = getParentContainer();
const dispatcher = createEventDispatcher();

$: handle.setAttrs(config);
$: _handle.setAttrs(config);

onMount(() => {
$parent!.add(handle);
$parent!.add(_handle);

if (!staticConfig) {
handle.on('transformend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('transformend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});

handle.on('dragend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('dragend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});
}

registerEvents(dispatcher, handle);
registerEvents(dispatcher, _handle);
});

onDestroy(() => {
handle.destroy();
_handle.destroy();
});
</script>
31 changes: 14 additions & 17 deletions src/lib/Group.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,43 @@ Further information: [Konva API docs](https://konvajs.org/api/Konva.Group.html),
interface $$Events extends KonvaEvents {}

export let config: Konva.GroupConfig = {};
export let handle: Konva.Group = new Konva.Group(config);
const _handle = new Konva.Group(config); // Hide inner handle behind a shadow variable to prevent users from overwriting it
export const handle = _handle;
export let staticConfig = false;

let inner = writable<null | Konva.Group>(null);
const inner = writable<null | Konva.Group>(null);

let dispatcher = createEventDispatcher();
const dispatcher = createEventDispatcher();

let isReady = false;

$: if (handle) {
handle.setAttrs(config);
}
$: _handle.setAttrs(config);

let parent: Writable<null | KonvaParent> = getParentContainer();
const parent: Writable<null | KonvaParent> = getParentContainer();

onMount(() => {
$parent!.add(handle);
$parent!.add(_handle);

if (!staticConfig) {
handle.on('transformend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('transformend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});

handle.on('dragend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('dragend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});
}

registerEvents(dispatcher, handle);
registerEvents(dispatcher, _handle);

inner.set(handle);
inner.set(_handle);
isReady = true;
});

onDestroy(() => {
if (handle) {
handle.destroy();
}
_handle.destroy();
});

setContainerContext(Container.Group, inner);
Expand Down
23 changes: 12 additions & 11 deletions src/lib/Image.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,34 @@ Further information: [Konva API docs](https://konvajs.org/api/Konva.Image.html),
interface $$Events extends KonvaEvents {}

export let config: Konva.ImageConfig;
export let handle = new Konva.Image(config);
const _handle = new Konva.Image(config); // Hide inner handle behind a shadow variable to prevent users from overwriting it
export const handle = _handle;
export let staticConfig = false;

let parent: Writable<null | KonvaParent> = getParentContainer();
let dispatcher = createEventDispatcher();
const parent: Writable<null | KonvaParent> = getParentContainer();
const dispatcher = createEventDispatcher();

$: handle.setAttrs(config);
$: _handle.setAttrs(config);

onMount(() => {
$parent!.add(handle);
$parent!.add(_handle);

if (!staticConfig) {
handle.on('transformend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('transformend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});

handle.on('dragend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('dragend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});
}

registerEvents(dispatcher, handle);
registerEvents(dispatcher, _handle);
});

onDestroy(() => {
handle.destroy();
_handle.destroy();
});
</script>
27 changes: 14 additions & 13 deletions src/lib/Label.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,43 @@ Further information: [Konva API docs](https://konvajs.org/api/Konva.Label.html),
interface $$Events extends KonvaEvents {}

export let config: Konva.LabelConfig;
export let handle = new Konva.Label(config);
const _handle = new Konva.Label(config); // Hide inner handle behind a shadow variable to prevent users from overwriting it
export const handle = _handle;
export let staticConfig = false;

let inner = writable<null | Konva.Label>(null);
const inner = writable<null | Konva.Label>(null);

let dispatcher = createEventDispatcher();
const dispatcher = createEventDispatcher();

let isReady = false;

$: handle.setAttrs(config);
$: _handle.setAttrs(config);

let parent: Writable<null | KonvaParent> = getParentContainer();
const parent: Writable<null | KonvaParent> = getParentContainer();

onMount(() => {
$parent!.add(handle);
$parent!.add(_handle);

if (!staticConfig) {
handle.on('transformend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('transformend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});

handle.on('dragend', () => {
copyExistingKeys(config, handle.getAttrs());
_handle.on('dragend', () => {
copyExistingKeys(config, _handle.getAttrs());
config = config;
});
}

registerEvents(dispatcher, handle);
registerEvents(dispatcher, _handle);

inner.set(handle);
inner.set(_handle);
isReady = true;
});

onDestroy(() => {
handle.destroy();
_handle.destroy();
});

setContainerContext(Container.Label, inner);
Expand Down
Loading