Skip to content

Commit

Permalink
fix(core): first track ID should not be lost after compile (#972)
Browse files Browse the repository at this point in the history
* fix: first track ID should not be lost after compile

* chore: format

* chore: update a test
  • Loading branch information
sehilyi authored Sep 25, 2023
1 parent 98b5b88 commit f73410d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
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

0 comments on commit f73410d

Please sign in to comment.