Skip to content
This repository was archived by the owner on Oct 21, 2025. It is now read-only.

Commit 91e1950

Browse files
authored
Merge pull request #142 from DataFlowAnalysis/load-from-url
Load diagram from URL
2 parents 04e4997 + a865d50 commit 91e1950

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/index.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ export function setModelSource(file: File): void {
112112
});
113113
}
114114

115+
function getQueryFileName(): string | null {
116+
const urlParams = new URLSearchParams(window.location.search);
117+
return urlParams.get("file");
118+
}
119+
115120
// Set empty model as starting point.
116121
// In contrast to the default diagram later this is not undoable which would bring the editor
117122
// into an invalid state where no root element is present.
@@ -121,7 +126,22 @@ modelSource
121126
id: "root",
122127
children: [],
123128
})
124-
.then(() =>
129+
.then(async () => {
130+
const queryFileName = getQueryFileName();
131+
let queryFile: File | null = null;
132+
if (queryFileName) {
133+
try {
134+
const response = await fetch(queryFileName);
135+
if (!response.ok) {
136+
throw new Error(`Failed to fetch file: ${response.statusText}`);
137+
}
138+
const blob = await response.blob();
139+
queryFile = new File([blob], queryFileName, { type: blob.type });
140+
} catch (error) {
141+
logger.error(null, `Failed to load file from query parameter: ${queryFileName}`, error);
142+
}
143+
}
144+
125145
dispatcher.dispatchAll([
126146
// Show the default uis after startup
127147
...defaultUIElements.map((uiElement) => {
@@ -130,11 +150,11 @@ modelSource
130150
visible: true,
131151
});
132152
}),
133-
// Then load the default diagram and commit the temporary model to the model source
134-
LoadDefaultDiagramAction.create(),
153+
// Then load the default diagram or query diagram and commit the temporary model to the model source
154+
queryFile ? LoadDiagramAction.create(queryFile) : LoadDefaultDiagramAction.create(),
135155
CommitModelAction.create(),
136-
]),
137-
)
156+
]);
157+
})
138158
.then(() => {
139159
// Focus the sprotty svg container to enable keyboard shortcuts
140160
// because those only work if the svg container is focused.

0 commit comments

Comments
 (0)