|
| 1 | +import type { JsonValue } from '@aas-core-works/aas-core3.0-typescript/jsonization'; |
| 2 | +import { jsonization, types as aasTypes } from '@aas-core-works/aas-core3.0-typescript'; |
| 3 | +import { useRouter } from 'vue-router'; |
| 4 | +import { useAASRepositoryClient } from '@/composables/Client/AASRepositoryClient'; |
| 5 | +import { useSMRepositoryClient } from '@/composables/Client/SMRepositoryClient'; |
| 6 | +import { useIDUtils } from '@/composables/IDUtils'; |
| 7 | +import { useAASStore } from '@/store/AASDataStore'; |
| 8 | +import { useClipboardStore } from '@/store/ClipboardStore'; |
1 | 9 | import { useNavigationStore } from '@/store/NavigationStore';
|
| 10 | +import { extractEndpointHref } from '@/utils/AAS/DescriptorUtils'; |
| 11 | +import { base64Decode, base64Encode } from '@/utils/EncodeDecodeUtils'; |
2 | 12 |
|
3 | 13 | export function useClipboardUtil() {
|
| 14 | + // Vue Router |
| 15 | + const router = useRouter(); |
| 16 | + |
4 | 17 | // Store
|
| 18 | + const aasStore = useAASStore(); |
| 19 | + const clipboardStore = useClipboardStore(); |
5 | 20 | const navigationStore = useNavigationStore();
|
6 | 21 |
|
| 22 | + // composables |
| 23 | + const { postSubmodel, postSubmodelElement } = useSMRepositoryClient(); |
| 24 | + const { putAas } = useAASRepositoryClient(); |
| 25 | + const { generateIri } = useIDUtils(); |
| 26 | + |
| 27 | + // Computed properties |
| 28 | + const selectedAAS = computed(() => aasStore.getSelectedAAS); |
| 29 | + const submodelRepoUrl = computed(() => navigationStore.getSubmodelRepoURL); |
| 30 | + |
7 | 31 | function copyToClipboard(value: string, valueName: string, iconReference: { value: string }): void {
|
8 | 32 | if (!value) return;
|
9 | 33 |
|
@@ -78,6 +102,135 @@ export function useClipboardUtil() {
|
78 | 102 | });
|
79 | 103 | }
|
80 | 104 |
|
| 105 | + function pasteElement(item?: unknown): void { |
| 106 | + const clipboardElement = clipboardStore.getClipboardContent() as any; |
| 107 | + console.log('Paste element on item:', item, '. Element:', clipboardElement); |
| 108 | + |
| 109 | + if (!clipboardElement) return; |
| 110 | + |
| 111 | + if (clipboardElement.modelType === 'Submodel') { |
| 112 | + insertSubmodel(clipboardElement); |
| 113 | + } else { |
| 114 | + insertSubmodelElement(clipboardElement, item); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + async function insertSubmodel(json: JsonValue): Promise<void> { |
| 119 | + // Parse JSON to Submodel |
| 120 | + const instanceOrError = jsonization.submodelFromJsonable(json); |
| 121 | + if (instanceOrError.error !== null) { |
| 122 | + navigationStore.dispatchSnackbar({ |
| 123 | + status: true, |
| 124 | + timeout: 4000, |
| 125 | + color: 'error', |
| 126 | + btnColor: 'buttonText', |
| 127 | + text: 'Error parsing Submodel: ' + instanceOrError.error, |
| 128 | + }); |
| 129 | + return; |
| 130 | + } |
| 131 | + const submodel = instanceOrError.mustValue(); |
| 132 | + |
| 133 | + // Create new unique ID for the Submodel |
| 134 | + submodel.id = generateIri('Submodel'); |
| 135 | + |
| 136 | + // Create Submodel |
| 137 | + await postSubmodel(submodel); |
| 138 | + // Add Submodel Reference to AAS |
| 139 | + await addSubmodelReferenceToAas(submodel); |
| 140 | + // Fetch and dispatch Submodel |
| 141 | + const path = submodelRepoUrl.value + '/' + base64Encode(submodel.id); |
| 142 | + const aasEndpoint = extractEndpointHref(selectedAAS.value, 'AAS-3.0'); |
| 143 | + router.push({ query: { aas: aasEndpoint, path: path } }); |
| 144 | + |
| 145 | + navigationStore.dispatchTriggerTreeviewReload(); |
| 146 | + } |
| 147 | + |
| 148 | + async function insertSubmodelElement(json: JsonValue, parentElement: any): Promise<void> { |
| 149 | + if (!parentElement) return; |
| 150 | + |
| 151 | + const instanceOrError = jsonization.submodelElementFromJsonable(json); |
| 152 | + if (instanceOrError.error !== null) { |
| 153 | + navigationStore.dispatchSnackbar({ |
| 154 | + status: true, |
| 155 | + timeout: 4000, |
| 156 | + color: 'error', |
| 157 | + btnColor: 'buttonText', |
| 158 | + text: 'Error parsing SubmodelElement: ' + instanceOrError.error, |
| 159 | + }); |
| 160 | + return; |
| 161 | + } |
| 162 | + const submodelElement = instanceOrError.mustValue(); |
| 163 | + |
| 164 | + // In case the SubmodelElement has an idShort, add "_copy" to the end |
| 165 | + if (submodelElement.idShort) { |
| 166 | + submodelElement.idShort += '_copy'; |
| 167 | + } |
| 168 | + |
| 169 | + if (parentElement.modelType === 'Submodel') { |
| 170 | + // Create the property on the parent Submodel |
| 171 | + await postSubmodelElement(submodelElement, parentElement.id); |
| 172 | + |
| 173 | + const aasEndpoint = extractEndpointHref(selectedAAS.value, 'AAS-3.0'); |
| 174 | + |
| 175 | + // Navigate to the new property |
| 176 | + router.push({ |
| 177 | + query: { |
| 178 | + aas: aasEndpoint, |
| 179 | + path: parentElement.path + '/submodel-elements/' + submodelElement.idShort, |
| 180 | + }, |
| 181 | + }); |
| 182 | + } else { |
| 183 | + // Extract the submodel ID and the idShortPath from the parentElement path |
| 184 | + const splitted = parentElement.path.split('/submodel-elements/'); |
| 185 | + const submodelId = base64Decode(splitted[0].split('/submodels/')[1]); |
| 186 | + const idShortPath = splitted[1]; |
| 187 | + |
| 188 | + // Create the property on the parent element |
| 189 | + await postSubmodelElement(submodelElement, submodelId, idShortPath); |
| 190 | + |
| 191 | + const aasEndpoint = extractEndpointHref(selectedAAS.value, 'AAS-3.0'); |
| 192 | + |
| 193 | + // Navigate to the new property |
| 194 | + if (parentElement.modelType === 'SubmodelElementCollection') { |
| 195 | + router.push({ |
| 196 | + query: { |
| 197 | + aas: aasEndpoint, |
| 198 | + path: parentElement.path + '.' + submodelElement.idShort, |
| 199 | + }, |
| 200 | + }); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + navigationStore.dispatchTriggerTreeviewReload(); |
| 205 | + } |
| 206 | + |
| 207 | + async function addSubmodelReferenceToAas(submodel: aasTypes.Submodel): Promise<void> { |
| 208 | + if (selectedAAS.value === null) return; |
| 209 | + const localAAS = { ...selectedAAS.value }; |
| 210 | + const instanceOrError = jsonization.assetAdministrationShellFromJsonable(localAAS); |
| 211 | + if (instanceOrError.error !== null) { |
| 212 | + console.error('Error parsing AAS: ', instanceOrError.error); |
| 213 | + return; |
| 214 | + } |
| 215 | + const aas = instanceOrError.mustValue(); |
| 216 | + // Create new SubmodelReference |
| 217 | + const submodelReference = new aasTypes.Reference(aasTypes.ReferenceTypes.ExternalReference, [ |
| 218 | + new aasTypes.Key(aasTypes.KeyTypes.Submodel, submodel.id), |
| 219 | + ]); |
| 220 | + // Check if Submodels are null |
| 221 | + if (aas.submodels === null || aas.submodels === undefined) { |
| 222 | + aas.submodels = [submodelReference]; |
| 223 | + localAAS.submodels = [jsonization.toJsonable(submodelReference)]; |
| 224 | + } else { |
| 225 | + aas.submodels.push(submodelReference); |
| 226 | + localAAS.submodels.push(jsonization.toJsonable(submodelReference)); |
| 227 | + } |
| 228 | + await putAas(aas); |
| 229 | + |
| 230 | + // Update AAS in Store |
| 231 | + aasStore.dispatchSelectedAAS(localAAS); |
| 232 | + } |
| 233 | + |
81 | 234 | function cleanObjectRecursively(obj: unknown): unknown {
|
82 | 235 | if (obj === null || obj === undefined) {
|
83 | 236 | return obj;
|
@@ -144,5 +297,10 @@ export function useClipboardUtil() {
|
144 | 297 | return obj;
|
145 | 298 | }
|
146 | 299 |
|
147 |
| - return { copyToClipboard, copyJsonToClipboard, cleanObjectRecursively }; |
| 300 | + return { |
| 301 | + copyToClipboard, |
| 302 | + copyJsonToClipboard, |
| 303 | + cleanObjectRecursively, |
| 304 | + pasteElement, |
| 305 | + }; |
148 | 306 | }
|
0 commit comments