|
| 1 | +import { Action } from "sprotty-protocol"; |
| 2 | +import { FileData, LoadJsonCommand } from "./loadJson"; |
| 3 | +import { inject } from "inversify"; |
| 4 | +import { TYPES, ILogger, ActionDispatcher } from "sprotty"; |
| 5 | +import { EditorModeController } from "../settings/editorMode"; |
| 6 | +import { LabelTypeRegistry } from "../labels/LabelTypeRegistry"; |
| 7 | +import { SavedDiagram } from "./SavedDiagram"; |
| 8 | +import { FileName } from "../fileName/fileName"; |
| 9 | +import { SETTINGS } from "../settings/Settings"; |
| 10 | +import { ConstraintRegistry } from "../constraint/constraintRegistry"; |
| 11 | +import { LoadingIndicator } from "../loadingIndicator/loadingIndicator"; |
| 12 | + |
| 13 | +interface LoadFromUrlAction extends Action { |
| 14 | + url: string; |
| 15 | +} |
| 16 | + |
| 17 | +export namespace LoadFromUrlAction { |
| 18 | + export const KIND = "loadUrl"; |
| 19 | + |
| 20 | + export function create(url: string): LoadFromUrlAction { |
| 21 | + return { kind: KIND, url }; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +export class LoadFromUrlCommand extends LoadJsonCommand { |
| 26 | + static readonly KIND = LoadFromUrlAction.KIND; |
| 27 | + |
| 28 | + constructor( |
| 29 | + @inject(TYPES.Action) private readonly action: LoadFromUrlAction, |
| 30 | + @inject(TYPES.ILogger) logger: ILogger, |
| 31 | + @inject(LabelTypeRegistry) labelTypeRegistry: LabelTypeRegistry, |
| 32 | + @inject(ConstraintRegistry) constraintRegistry: ConstraintRegistry, |
| 33 | + @inject(SETTINGS.Mode) editorModeController: EditorModeController, |
| 34 | + @inject(TYPES.IActionDispatcher) actionDispatcher: ActionDispatcher, |
| 35 | + @inject(FileName) fileName: FileName, |
| 36 | + @inject(LoadingIndicator) loadingIndicator: LoadingIndicator, |
| 37 | + ) { |
| 38 | + super( |
| 39 | + logger, |
| 40 | + labelTypeRegistry, |
| 41 | + constraintRegistry, |
| 42 | + editorModeController, |
| 43 | + actionDispatcher, |
| 44 | + fileName, |
| 45 | + loadingIndicator, |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + protected async getFile(): Promise<FileData<SavedDiagram> | undefined> { |
| 50 | + const content = await fetch(this.action.url).then((response) => response.json() as Promise<SavedDiagram>); |
| 51 | + const urlParts = this.action.url.split("/"); |
| 52 | + const fileName = urlParts[urlParts.length - 1]; |
| 53 | + return { |
| 54 | + content, |
| 55 | + fileName, |
| 56 | + }; |
| 57 | + } |
| 58 | +} |
0 commit comments