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

refactor: change syntax to svelte 5 #972

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 6 additions & 6 deletions packages/svelte-vega-sample/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import type { View, VisualizationSpec } from 'svelte-vega';
import { Vega, VegaLite } from 'svelte-vega';
import type { VisualizationSpec, View } from 'svelte-vega';

const data = {
table: [
Expand All @@ -15,8 +15,8 @@
]
};

let viewVL: View;
let specVL: VisualizationSpec = {
let viewVL: View | undefined = $state(undefined);
Sparkier marked this conversation as resolved.
Show resolved Hide resolved
const specVL: VisualizationSpec = {
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
description: 'A simple bar chart with embedded data.',
data: {
Expand All @@ -29,7 +29,7 @@
}
};

let viewV: View;
let viewV: View | undefined = $state(undefined);
const specV: VisualizationSpec = {
$schema: 'https://vega.github.io/schema/vega/v5.json',
width: 400,
Expand Down Expand Up @@ -111,8 +111,8 @@
]
};

$: viewVL ? console.log('Vega-Lite view: ', viewVL.data('table')) : '';
$: viewV ? console.log('Vega view: ', viewV.data('table')) : '';
$inspect('Vega-Lite view: ', viewVL ? viewVL.data('table') : '');
$inspect('Vega view: ', viewV ? viewV.data('table') : '');
</script>

<main>
Expand Down
23 changes: 15 additions & 8 deletions packages/svelte-vega/src/lib/Vega.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<script lang="ts">
import type { EmbedOptions, Mode } from 'vega-embed';
import type { SignalListeners, VisualizationSpec, View } from './types';
import type { SignalListeners, View, VisualizationSpec } from './types';
import VegaEmbed from './VegaEmbed.svelte';

export let spec: VisualizationSpec;
export let options: EmbedOptions = {};
export let data: Record<string, unknown> = {};
export let signalListeners: SignalListeners = {};
export let view: View | undefined = undefined;
let {
spec,
options,
data,
signalListeners,
view = $bindable()
}: {
spec: VisualizationSpec;
options: EmbedOptions;
data: Record<string, unknown>;
signalListeners: SignalListeners;
view: View | undefined;
} = $props();

const mode = 'vega' as Mode;
$: vegaOptions = { ...options, mode: mode };
const vegaOptions = $derived({ ...options, mode: 'vega' as Mode });
</script>

<VegaEmbed
Expand Down
45 changes: 28 additions & 17 deletions packages/svelte-vega/src/lib/VegaEmbed.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,44 @@
import { WIDTH_HEIGHT } from './constants';
import {
addSignalListenersToView,
updateMultipleDatasetsInView,
combineSpecWithDimension,
computeSpecChanges,
removeSignalListenersFromView,
shallowEqual
shallowEqual,
updateMultipleDatasetsInView
} from './utils';

export let options: EmbedOptions;
export let spec: VisualizationSpec;
export let view: View | undefined;
export let signalListeners: SignalListeners = {};
export let data: Record<string, unknown> = {};
let {
options,
spec,
view = $bindable(),
signalListeners,
data
}: {
options: EmbedOptions;
spec: VisualizationSpec;
view: View | undefined;
signalListeners: SignalListeners;
data: Record<string, unknown>;
} = $props();

const dispatch = createEventDispatcher();

let result: Result | undefined = undefined;
let prevOptions: EmbedOptions = {};
let prevSignalListeners: SignalListeners = {};
let prevSpec: VisualizationSpec = {};
let prevData: Record<string, unknown> = {};
let chartContainer: HTMLElement;
let result: Result | undefined = $state(undefined);
let prevOptions: EmbedOptions = $state({});
let prevSignalListeners: SignalListeners = $state({});
let prevSpec: VisualizationSpec = $state({});
let prevData: Record<string, unknown> = $state({});
let chartContainer: HTMLElement | undefined = $state(undefined);

Check warning on line 39 in packages/svelte-vega/src/lib/VegaEmbed.svelte

View workflow job for this annotation

GitHub Actions / Node

'HTMLElement' is not defined

$: {
$effect(() => {
if (!shallowEqual(data, prevData)) {
update();
}
prevData = data;
}
});

$: {
$effect(() => {
if (chartContainer !== undefined) {
// only create a new view if neccessary
if (!shallowEqual(options, prevOptions, WIDTH_HEIGHT)) {
Expand Down Expand Up @@ -87,13 +95,16 @@
prevSignalListeners = signalListeners;
prevSpec = spec;
}
}
});

onDestroy(() => {
clearView();
});

async function createView() {
if (chartContainer === undefined) {
return;
}
clearView();
try {
result = await vegaEmbed(chartContainer, spec, options);
Expand All @@ -119,7 +130,7 @@
dispatch('onError', {
error: error
});
console.warn(error);

Check warning on line 133 in packages/svelte-vega/src/lib/VegaEmbed.svelte

View workflow job for this annotation

GitHub Actions / Node

'console' is not defined
}

function onNewView(view: View) {
Expand Down
23 changes: 15 additions & 8 deletions packages/svelte-vega/src/lib/VegaLite.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<script lang="ts">
import type { EmbedOptions, Mode } from 'vega-embed';
import type { SignalListeners, VisualizationSpec, View } from './types';
import type { SignalListeners, View, VisualizationSpec } from './types';
import VegaEmbed from './VegaEmbed.svelte';

export let spec: VisualizationSpec;
export let options: EmbedOptions = {};
export let data: Record<string, unknown> = {};
export let signalListeners: SignalListeners = {};
export let view: View | undefined = undefined;
let {
spec,
options,
data,
signalListeners,
view = $bindable()
}: {
spec: VisualizationSpec;
options: EmbedOptions;
data: Record<string, unknown>;
signalListeners: SignalListeners;
view: View | undefined;
} = $props();

const mode = 'vega-lite' as Mode;
$: vegaLiteOptions = { ...options, mode: mode };
const vegaLiteOptions = $derived({ ...options, mode: 'vega-lite' as Mode });
</script>

<VegaEmbed
Expand Down
12 changes: 6 additions & 6 deletions packages/sveltekit-vega-sample/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import type { View, VisualizationSpec } from 'svelte-vega';
import { Vega, VegaLite } from 'svelte-vega';
import type { VisualizationSpec, View } from 'svelte-vega';

const data = {
table: [
Expand All @@ -15,8 +15,8 @@
]
};

let viewVL: View;
let specVL: VisualizationSpec = {
let viewVL: View | undefined = $state(undefined);
const specVL: VisualizationSpec = {
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
description: 'A simple bar chart with embedded data.',
data: {
Expand All @@ -29,7 +29,7 @@
}
};

let viewV: View;
let viewV: View | undefined = $state(undefined);
const specV: VisualizationSpec = {
$schema: 'https://vega.github.io/schema/vega/v5.json',
width: 400,
Expand Down Expand Up @@ -111,8 +111,8 @@
]
};

$: viewVL ? console.log('Vega-Lite view: ', viewVL.data('table')) : '';
$: viewV ? console.log('Vega view: ', viewV.data('table')) : '';
$inspect('Vega-Lite view: ', viewVL ? viewVL.data('table') : '');
$inspect('Vega view: ', viewV ? viewV.data('table') : '');
</script>

<main>
Expand Down
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5973,6 +5973,14 @@ svelte-preprocess@^6.0.3:
resolved "https://registry.yarnpkg.com/svelte-preprocess/-/svelte-preprocess-6.0.3.tgz#fdc1f9dc41b6f22bf8b1f059e9f21eaaae181eeb"
integrity sha512-PLG2k05qHdhmRG7zR/dyo5qKvakhm8IJ+hD2eFRQmMLHp7X3eJnjeupUtvuRpbNiF31RjVw45W+abDwHEmP5OA==

svelte-vega@^1.2.0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird. Probably shouldn't be here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm thats's what I get when I run yarn on the monorepo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to indicate that we don't use the dependency from this repo somehow.

version "1.2.0"
resolved "https://registry.yarnpkg.com/svelte-vega/-/svelte-vega-1.2.0.tgz#ec8c835837a15198b1ac26c90abd07ef41bc4647"
integrity sha512-MsDdO+l7o/d9d4mVkh8MBDhqZvJ45lpuprBaTj0V/ZilIG902QERHFQlam3ZFcR9C9OIKSpmPqINssWNPkDdcA==
dependencies:
fast-deep-equal "^3.1.3"
vega-embed "^6.21.0"

svelte2tsx@~0.7.16:
version "0.7.22"
resolved "https://registry.yarnpkg.com/svelte2tsx/-/svelte2tsx-0.7.22.tgz#2051d61d8581c538b39ce7c029ca9a4680113a83"
Expand Down Expand Up @@ -6409,7 +6417,7 @@ vega-dataflow@^5.7.6, vega-dataflow@~5.7.6:
vega-loader "^4.5.2"
vega-util "^1.17.2"

vega-embed@^6.28.0:
vega-embed@^6.21.0, vega-embed@^6.28.0:
version "6.28.0"
resolved "https://registry.yarnpkg.com/vega-embed/-/vega-embed-6.28.0.tgz#18ea7ea24e12df4d6075496ce4dd601fca23ce0d"
integrity sha512-QCjrNCDZPrSOZPG3UmfFZsd95mUQEZSYAWdoi2TOEnzBv/NzB+BX+Fc6jdpcAHsORn3TqxL0um/jktyjnV88zg==
Expand Down
Loading