-
Notifications
You must be signed in to change notification settings - Fork 18
Adds JSON insert to editor #683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
226 changes: 226 additions & 0 deletions
226
aas-web-ui/src/components/EditorComponents/JsonInsert.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
<template> | ||
<v-dialog v-model="jsonInsertDialog" width="860" persistent> | ||
<v-card> | ||
<v-card-title>Insert {{ type }} from JSON</v-card-title> | ||
<v-divider></v-divider> | ||
<v-card-text class="bg-card pa-3"> | ||
<v-card> | ||
<v-textarea | ||
v-model="jsonInput" | ||
:error-messages="jsonInputErrors" | ||
variant="outlined" | ||
rows="10" | ||
hide-details | ||
@update:model-value="clearErrorMessages" /> | ||
</v-card> | ||
</v-card-text> | ||
<v-divider></v-divider> | ||
<v-card-actions> | ||
<v-spacer></v-spacer> | ||
<v-btn @click="closeDialog">Cancel</v-btn> | ||
<v-btn color="primary" @click="insertJson">Save</v-btn> | ||
</v-card-actions> | ||
</v-card> | ||
</v-dialog> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import type { JsonValue } from '@aas-core-works/aas-core3.0-typescript/jsonization'; | ||
import { jsonization, types as aasTypes } from '@aas-core-works/aas-core3.0-typescript'; | ||
import { computed, ref, watch } from 'vue'; | ||
import { useRouter } from 'vue-router'; | ||
import { useAASRepositoryClient } from '@/composables/Client/AASRepositoryClient'; | ||
import { useSMRepositoryClient } from '@/composables/Client/SMRepositoryClient'; | ||
import { useAASStore } from '@/store/AASDataStore'; | ||
import { useNavigationStore } from '@/store/NavigationStore'; | ||
import { extractEndpointHref } from '@/utils/AAS/DescriptorUtils'; | ||
import { base64Decode, base64Encode } from '@/utils/EncodeDecodeUtils'; | ||
|
||
const props = defineProps<{ | ||
modelValue: boolean; | ||
type: 'Submodel' | 'SubmodelElement'; | ||
parentElement?: any; | ||
}>(); | ||
|
||
const emit = defineEmits<{ | ||
(event: 'update:modelValue', value: boolean): void; | ||
}>(); | ||
|
||
// Vue Router | ||
const router = useRouter(); | ||
|
||
// Stores | ||
const aasStore = useAASStore(); | ||
const navigationStore = useNavigationStore(); | ||
|
||
// Composables | ||
const { postSubmodel, postSubmodelElement } = useSMRepositoryClient(); | ||
const { putAas } = useAASRepositoryClient(); | ||
|
||
// Data | ||
const jsonInsertDialog = ref(false); | ||
const jsonInput = ref<string | null>(null); | ||
const jsonInputErrors = ref<string[]>([]); | ||
|
||
// Computed Properties | ||
const selectedAAS = computed(() => aasStore.getSelectedAAS); // Get the selected AAS from Store | ||
const submodelRepoUrl = computed(() => navigationStore.getSubmodelRepoURL); | ||
|
||
watch( | ||
() => props.modelValue, | ||
(value) => { | ||
jsonInsertDialog.value = value; | ||
} | ||
); | ||
|
||
watch( | ||
() => jsonInsertDialog.value, | ||
(value) => { | ||
emit('update:modelValue', value); | ||
} | ||
); | ||
|
||
function insertJson(): void { | ||
if (!jsonInput.value || !isValidJson(jsonInput.value)) { | ||
jsonInputErrors.value = ['Invalid JSON input']; | ||
return; | ||
} | ||
|
||
// Parse JSON to Submodel/SubmodelElement | ||
if (props.type === 'Submodel') { | ||
insertSubmodel(JSON.parse(jsonInput.value)); | ||
} else { | ||
insertSubmodelElement(JSON.parse(jsonInput.value)); | ||
} | ||
} | ||
|
||
async function insertSubmodel(json: JsonValue): Promise<void> { | ||
// Parse JSON to Submodel | ||
const instanceOrError = jsonization.submodelFromJsonable(json); | ||
if (instanceOrError.error !== null) { | ||
navigationStore.dispatchSnackbar({ | ||
status: true, | ||
timeout: 4000, | ||
color: 'error', | ||
btnColor: 'buttonText', | ||
text: 'Error parsing Submodel: ' + instanceOrError.error, | ||
}); | ||
return; | ||
} | ||
const submodel = instanceOrError.mustValue(); | ||
|
||
// Create Submodel | ||
await postSubmodel(submodel); | ||
// Add Submodel Reference to AAS | ||
await addSubmodelReferenceToAas(submodel); | ||
// Fetch and dispatch Submodel | ||
const path = submodelRepoUrl.value + '/' + base64Encode(submodel.id); | ||
const aasEndpoint = extractEndpointHref(selectedAAS.value, 'AAS-3.0'); | ||
router.push({ query: { aas: aasEndpoint, path: path } }); | ||
|
||
closeDialog(); | ||
navigationStore.dispatchTriggerTreeviewReload(); | ||
} | ||
|
||
async function insertSubmodelElement(json: JsonValue): Promise<void> { | ||
const instanceOrError = jsonization.submodelElementFromJsonable(json); | ||
if (instanceOrError.error !== null) { | ||
navigationStore.dispatchSnackbar({ | ||
status: true, | ||
timeout: 4000, | ||
color: 'error', | ||
btnColor: 'buttonText', | ||
text: 'Error parsing SubmodelElement: ' + instanceOrError.error, | ||
}); | ||
return; | ||
} | ||
const submodelElement = instanceOrError.mustValue(); | ||
|
||
if (props.parentElement.modelType === 'Submodel') { | ||
// Create the property on the parent Submodel | ||
await postSubmodelElement(submodelElement, props.parentElement.id); | ||
|
||
const aasEndpoint = extractEndpointHref(selectedAAS.value, 'AAS-3.0'); | ||
|
||
// Navigate to the new property | ||
router.push({ | ||
query: { | ||
aas: aasEndpoint, | ||
path: props.parentElement.path + '/submodel-elements/' + submodelElement.idShort, | ||
}, | ||
}); | ||
} else { | ||
// Extract the submodel ID and the idShortPath from the parentElement path | ||
const splitted = props.parentElement.path.split('/submodel-elements/'); | ||
const submodelId = base64Decode(splitted[0].split('/submodels/')[1]); | ||
const idShortPath = splitted[1]; | ||
|
||
// Create the property on the parent element | ||
await postSubmodelElement(submodelElement, submodelId, idShortPath); | ||
|
||
const aasEndpoint = extractEndpointHref(selectedAAS.value, 'AAS-3.0'); | ||
|
||
// Navigate to the new property | ||
if (props.parentElement.modelType === 'SubmodelElementCollection') { | ||
router.push({ | ||
query: { | ||
aas: aasEndpoint, | ||
path: props.parentElement.path + '.' + submodelElement.idShort, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
closeDialog(); | ||
navigationStore.dispatchTriggerTreeviewReload(); | ||
} | ||
|
||
async function addSubmodelReferenceToAas(submodel: aasTypes.Submodel): Promise<void> { | ||
if (selectedAAS.value === null) return; | ||
const localAAS = { ...selectedAAS.value }; | ||
const instanceOrError = jsonization.assetAdministrationShellFromJsonable(localAAS); | ||
if (instanceOrError.error !== null) { | ||
console.error('Error parsing AAS: ', instanceOrError.error); | ||
return; | ||
} | ||
const aas = instanceOrError.mustValue(); | ||
// Create new SubmodelReference | ||
const submodelReference = new aasTypes.Reference(aasTypes.ReferenceTypes.ExternalReference, [ | ||
new aasTypes.Key(aasTypes.KeyTypes.Submodel, submodel.id), | ||
]); | ||
// Check if Submodels are null | ||
if (aas.submodels === null || aas.submodels === undefined) { | ||
aas.submodels = [submodelReference]; | ||
localAAS.submodels = [jsonization.toJsonable(submodelReference)]; | ||
} else { | ||
aas.submodels.push(submodelReference); | ||
localAAS.submodels.push(jsonization.toJsonable(submodelReference)); | ||
} | ||
await putAas(aas); | ||
|
||
// Update AAS in Store | ||
aasStore.dispatchSelectedAAS(localAAS); | ||
} | ||
|
||
function isValidJson(jsonString: string): boolean { | ||
try { | ||
JSON.parse(jsonString); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
function closeDialog(): void { | ||
clearForm(); | ||
jsonInsertDialog.value = false; | ||
} | ||
|
||
function clearForm(): void { | ||
jsonInput.value = null; | ||
} | ||
|
||
function clearErrorMessages(): void { | ||
jsonInputErrors.value = []; | ||
} | ||
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AAS parsing error is only logged to console. Consider showing a user-friendly error message or handling this error more gracefully since it affects the core functionality.
Copilot uses AI. Check for mistakes.