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 2 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
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
8 changes: 1 addition & 7 deletions packages/svelte-vega-sample/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,5 @@ import { sveltePreprocess } from 'svelte-preprocess';
export default {
// Consult https://github.com/sveltejs/svelte-preprocess
// for more information about preprocessors
preprocess: sveltePreprocess(),

compilerOptions: {
compatibility: {
componentApi: 4
}
}
preprocess: sveltePreprocess()
};
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
6 changes: 0 additions & 6 deletions packages/svelte-vega/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ const config = {
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter()
},

compilerOptions: {
compatibility: {
componentApi: 4
}
}
};

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
6 changes: 0 additions & 6 deletions packages/sveltekit-vega-sample/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ const config = {
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter()
},

compilerOptions: {
compatibility: {
componentApi: 4
}
}
};

Expand Down
Loading
Loading