1- import ShareLinkState from '../ShareLinkState' ;
1+ import { Chapter , Variant } from 'js-slang/dist/types' ;
2+ import { decompressFromEncodedURIComponent } from 'lz-string' ;
3+ import { getDefaultFilePath } from 'src/commons/application/ApplicationTypes' ;
4+ import { convertParamToBoolean , convertParamToInt } from 'src/commons/utils/ParamParseHelper' ;
5+ import { parseQuery } from 'src/commons/utils/QueryHelper' ;
6+ import { WorkspaceLocation } from 'src/commons/workspace/WorkspaceTypes' ;
7+
8+ import { ShareLinkState } from '../ShareLinkState' ;
29import DecoderDelegate from './delegates/DecoderDelegate' ;
310
411/**
@@ -11,8 +18,34 @@ class ShareLinkStateDecoder {
1118 this . encodedString = encodedString ;
1219 }
1320
14- decodeWith ( decoderDelegate : DecoderDelegate ) : ShareLinkState {
15- return decoderDelegate . decode ( this . encodedString ) ;
21+ decodeWith (
22+ decoderDelegate : DecoderDelegate ,
23+ workspaceLocation : WorkspaceLocation
24+ ) : ShareLinkState {
25+ const parsedObject = decoderDelegate . decode ( this . encodedString ) ;
26+
27+ // For backward compatibility with old share links - 'prgrm' is no longer used.
28+ const program =
29+ parsedObject . prgrm === undefined ? '' : decompressFromEncodedURIComponent ( parsedObject . prgrm ) ;
30+
31+ // By default, create just the default file.
32+ const defaultFilePath = getDefaultFilePath ( workspaceLocation ) ;
33+ const filesObject : Record < string , string > =
34+ parsedObject . files === undefined
35+ ? {
36+ [ defaultFilePath ] : program
37+ }
38+ : parseQuery ( decompressFromEncodedURIComponent ( parsedObject . files ) ) ;
39+
40+ return {
41+ chap : convertParamToInt ( parsedObject . chap ) ?? Chapter . SOURCE_1 ,
42+ exec : Math . max ( convertParamToInt ( parsedObject . exec ) || 1000 , 1000 ) ,
43+ files : filesObject ,
44+ isFolder : convertParamToBoolean ( parsedObject . isFolder ) ?? false ,
45+ tabIdx : convertParamToInt ( parsedObject . tabIdx ) ?? 0 , // By default, use the first editor tab.
46+ tabs : parsedObject . tabs ?. map ( decompressFromEncodedURIComponent ) ?? [ defaultFilePath ] , // By default, open a single editor tab containing the default playground file.
47+ variant : parsedObject . variant as Variant
48+ } ;
1649 }
1750}
1851
0 commit comments