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

fix(core): first track ID should not be lost after compile #972

Merged
merged 3 commits into from
Sep 25, 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
24 changes: 24 additions & 0 deletions src/compiler/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { EX_SPEC_VISUAL_ENCODING } from '../../editor/example/json-spec/visual-e
import { compile } from './compile';
import { getTheme } from '../core/utils/theme';
import type { GoslingSpec } from '../index';
import { convertToFlatTracks } from './spec-preprocess';
import type { SingleView } from '@gosling-lang/gosling-schema';
import { spreadTracksByData } from '../core/utils/overlay';

describe('compile', () => {
it('compile should not touch the original spec of users', () => {
Expand Down Expand Up @@ -242,3 +245,24 @@ describe('Dummy track', () => {
);
});
});

describe('Maintain IDs', () => {
it('Overlaid tracks', () => {
const twoTracksWithDiffData: SingleView = {
alignment: 'overlay',
tracks: [
{
id: 'first',
data: { type: 'csv', url: 'http://abc' }
},
{
id: 'second',
data: { type: 'csv', url: 'http://def' }
}
]
};
const flattened = convertToFlatTracks(twoTracksWithDiffData);
const spread = spreadTracksByData(flattened);
expect(spread.map(d => d.id)).toEqual(['first', 'second']);
});
});
2 changes: 1 addition & 1 deletion src/core/gosling-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const GoslingComponent = forwardRef<GoslingRef, GoslingCompProps>((props,
* This makes sure that all the current zooming status is preserved when new tracks are added
*/
const preverseZoomStatus = (newSpec: gosling.HiGlassSpec, prevSpec: gosling.HiGlassSpec) => {
newSpec.views.forEach((view) => {
newSpec.views.forEach(view => {
const viewUid = view.uid!;
const newView = !prevSpec.views.find(v => v.uid === viewUid);
if (newView) {
Expand Down
5 changes: 5 additions & 0 deletions src/core/utils/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ export function spreadTracksByData(tracks: Track[]): Track[] {
original.data = subSpec.data;
}

// If the id is undefined, put the first spec to the parent
if (!original.id) {
original.id = subSpec.id;
}

// Determine if this `subSpec` should be added to `overlay` or become a separate track
if (!subSpec.data || isIdenticalDataSpec([original.data, subSpec.data])) {
original.overlay.push(subSpec);
Expand Down
Loading