Skip to content

Commit bdca050

Browse files
dani-polaniclaude
andcommitted
fix(share): load link settings before pruning its connections
Hydration loaded the project before the link's settings, so connections were pruned against tokens built with the default separators. A diagram that split a line with a custom separator such as `-` lost every link past that line's first token. Add projectStore.loadState, which loads settings first, and route the home page, examples, and gallery previews through it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent c794a8b commit bdca050

4 files changed

Lines changed: 76 additions & 11 deletions

File tree

‎bitext/src/lib/components/examples/ExamplePreviewEmbed.svelte‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import { findExample, type ExampleId } from '$lib/state/examples.js';
66
import { layoutExportStore } from '$lib/state/layoutExport.svelte.js';
77
import { projectStore } from '$lib/state/project.svelte.js';
8-
import { settingsStore } from '$lib/state/settings.svelte.js';
98
109
let {
1110
exampleId,
@@ -20,10 +19,9 @@
2019
2120
onMount(() => {
2221
void (async () => {
23-
const state = buildAppStateFromExample(findExample(exampleId), { previewHideChrome: true });
24-
settingsStore.load(state.settings);
25-
projectStore.loadSnapshotV2(state.project);
26-
projectStore.retokenizeFromSettings();
22+
projectStore.loadState(
23+
buildAppStateFromExample(findExample(exampleId), { previewHideChrome: true })
24+
);
2725
layoutExportStore.requestRemeasureAfterLayout();
2826
await document.fonts.ready;
2927
await tick();
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { beforeEach, describe, expect, it } from 'vitest';
2+
import { projectStore } from './project.svelte.js';
3+
import { settingsStore } from './settings.svelte.js';
4+
import { decodeState } from '$lib/serialization/decode.js';
5+
import { encodeState } from '$lib/serialization/encode.js';
6+
import {
7+
SCHEMA_VERSION,
8+
defaultVisualSettingsV2,
9+
type AppStateV2,
10+
type LineV2
11+
} from '$lib/serialization/schema.js';
12+
13+
function line(id: string, rawText: string): LineV2 {
14+
return {
15+
id,
16+
rawText,
17+
font: { family: 'Inter', source: 'google' },
18+
textSizePx: 36,
19+
gapWordPx: 14
20+
};
21+
}
22+
23+
/** Middle line only splits into three tokens with the custom `-` separator. */
24+
function stateWithCustomSeparator(): AppStateV2 {
25+
return {
26+
v: SCHEMA_VERSION,
27+
settings: { ...defaultVisualSettingsV2(), tokenSplitChars: '|-' },
28+
project: {
29+
lines: [line('s', 'a b c'), line('t', 'x-y-z'), line('u', 'd e f')],
30+
connections: [
31+
{ id: 'c1', upperTokenId: 's-0', lowerTokenId: 't-0' },
32+
{ id: 'c2', upperTokenId: 's-1', lowerTokenId: 't-1' },
33+
{ id: 'c3', upperTokenId: 's-2', lowerTokenId: 't-2' },
34+
{ id: 'c4', upperTokenId: 't-0', lowerTokenId: 'u-2' },
35+
{ id: 'c5', upperTokenId: 't-1', lowerTokenId: 'u-1' },
36+
{ id: 'c6', upperTokenId: 't-2', lowerTokenId: 'u-0' }
37+
],
38+
pairControls: [],
39+
linePairGaps: []
40+
}
41+
};
42+
}
43+
44+
describe('project store: loadState', () => {
45+
beforeEach(() => {
46+
settingsStore.reset();
47+
projectStore.loadSnapshotV2({
48+
lines: [line('s', 'a'), line('t', 'b')],
49+
connections: [],
50+
pairControls: [],
51+
linePairGaps: []
52+
});
53+
});
54+
55+
it('keeps links on tokens produced by the loaded tokenization settings', () => {
56+
const shared = decodeState(encodeState(stateWithCustomSeparator()));
57+
expect(shared.project.connections).toHaveLength(6);
58+
59+
projectStore.loadState(shared);
60+
61+
expect(settingsStore.settings.tokenSplitChars).toBe('|-');
62+
expect(projectStore.tokensOnLine('t').map((t) => t.text)).toEqual(['x', 'y', 'z']);
63+
expect(projectStore.connections).toHaveLength(6);
64+
});
65+
});

‎bitext/src/lib/state/project.svelte.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
MAX_LINES,
2626
NEW_LINE_HINT_TEXT,
2727
normalizeProjectSnapshotV2,
28+
type AppStateV2,
2829
type LinePairGapV2,
2930
type LineV2,
3031
type PairControlV2,
@@ -363,11 +364,14 @@ class ProjectStore {
363364
this.pruneInvalidConnections();
364365
}
365366

366-
loadExample(kind: ExampleId = 'simple') {
367-
const state = buildAppStateFromExample(findExample(kind));
367+
/** Settings load first: connections are pruned against tokens built from them. */
368+
loadState(state: AppStateV2) {
368369
settingsStore.load(state.settings);
369370
this.loadSnapshotV2(state.project);
370-
this.retokenizeFromSettings();
371+
}
372+
373+
loadExample(kind: ExampleId = 'simple') {
374+
this.loadState(buildAppStateFromExample(findExample(kind)));
371375
layoutExportStore.requestRemeasureAfterLayout();
372376
}
373377
}

‎bitext/src/routes/+page.svelte‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@
7070
$effect(() => {
7171
if (hydrated) return;
7272
if (data.initialState) {
73-
projectStore.loadSnapshotV2(data.initialState.project);
74-
settingsStore.load(data.initialState.settings);
75-
projectStore.retokenizeFromSettings();
73+
projectStore.loadState(data.initialState);
7674
}
7775
hydrated = true;
7876
});

0 commit comments

Comments
 (0)