From 4a822477cd22d5074eae800b94669c3e5a03b7ef Mon Sep 17 00:00:00 2001 From: Cody Hansen Date: Mon, 24 Jun 2024 06:11:53 -1000 Subject: [PATCH] Moved sequence adaptation loading code into SequenceEditor to include it in the sequence preview --- .../sequencing/SequenceEditor.svelte | 42 ++++++++++++++++--- src/components/sequencing/SequenceForm.svelte | 35 ---------------- 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/components/sequencing/SequenceEditor.svelte b/src/components/sequencing/SequenceEditor.svelte index 7e13ac6a31..46c04d640b 100644 --- a/src/components/sequencing/SequenceEditor.svelte +++ b/src/components/sequencing/SequenceEditor.svelte @@ -14,7 +14,7 @@ import SaveIcon from 'bootstrap-icons/icons/save.svg?component'; import { EditorView, basicSetup } from 'codemirror'; import { debounce } from 'lodash-es'; - import { createEventDispatcher, onMount } from 'svelte'; + import { createEventDispatcher, onDestroy, onMount } from 'svelte'; import { channelDictionaries, commandDictionaries, @@ -29,6 +29,7 @@ } from '../../stores/sequencing'; import type { User } from '../../types/app'; import { setupLanguageSupport } from '../../utilities/codemirror'; + import effects from '../../utilities/effects'; import { seqJsonLinter } from '../../utilities/new-sequence-editor/seq-json-linter'; import { sequenceCompletion } from '../../utilities/new-sequence-editor/sequence-completion'; import { sequenceLinter } from '../../utilities/new-sequence-editor/sequence-linter'; @@ -72,11 +73,9 @@ let toggleSeqJsonPreview: boolean = false; $: { - if (editorSequenceView) { - editorSequenceView.dispatch({ - changes: { from: 0, insert: sequenceDefinition, to: editorSequenceView.state.doc.length }, - }); - } + editorSequenceView.dispatch({ + changes: { from: 0, insert: sequenceDefinition, to: editorSequenceView.state.doc.length }, + }); } $: { @@ -178,6 +177,37 @@ }); }); + onDestroy(() => { + resetSequenceAdaptation(); + }); + + async function loadSequenceAdaptation(id: number | null | undefined): Promise { + if (id) { + const adaptation = await effects.getSequenceAdaptation(id, user); + + if (adaptation) { + try { + // This evaluates the custom sequence adaptation that is optionally provided by the user. + Function(adaptation.adaptation)(); + } catch (e) { + console.error(e); + showFailureToast('Invalid sequence adaptation'); + } + } + } else { + resetSequenceAdaptation(); + } + } + + function resetSequenceAdaptation(): void { + globalThis.CONDITIONAL_KEYWORDS = undefined; + globalThis.LOOP_KEYWORDS = undefined; + globalThis.GLOBALS = undefined; + globalThis.ARG_DELEGATOR = undefined; + globalThis.LINT = () => undefined; + globalThis.TO_SEQ_JSON = () => undefined; + } + function sequenceUpdateListener(viewUpdate: ViewUpdate) { const sequence = viewUpdate.state.doc.toString(); diff --git a/src/components/sequencing/SequenceForm.svelte b/src/components/sequencing/SequenceForm.svelte index a6e4dd4a4a..d4224e0770 100644 --- a/src/components/sequencing/SequenceForm.svelte +++ b/src/components/sequencing/SequenceForm.svelte @@ -4,7 +4,6 @@ import { goto } from '$app/navigation'; import { base } from '$app/paths'; import type { ParameterDictionary } from '@nasa-jpl/aerie-ampcs'; - import { onDestroy } from 'svelte'; import { parameterDictionaries as parameterDictionariesStore, parcel, @@ -19,7 +18,6 @@ import { parseSeqJsonFromFile, seqJsonToSequence } from '../../utilities/new-sequence-editor/from-seq-json'; import { permissionHandler } from '../../utilities/permissionHandler'; import { featurePermissions } from '../../utilities/permissions'; - import { showFailureToast } from '../../utilities/toast'; import PageTitle from '../app/PageTitle.svelte'; import CssGrid from '../ui/CssGrid.svelte'; import CssGridGutter from '../ui/CssGridGutter.svelte'; @@ -74,39 +72,6 @@ $: { if (sequenceParcelId) { $parcel = $parcels.find(p => p.id === sequenceParcelId) ?? null; - - loadSequenceAdaptation($parcel?.sequence_adaptation_id); - } - } - - onDestroy(() => { - resetSequenceAdaptation(); - }); - - function resetSequenceAdaptation(): void { - globalThis.CONDITIONAL_KEYWORDS = undefined; - globalThis.LOOP_KEYWORDS = undefined; - globalThis.GLOBALS = undefined; - globalThis.ARG_DELEGATOR = undefined; - globalThis.LINT = () => undefined; - globalThis.TO_SEQ_JSON = () => undefined; - } - - async function loadSequenceAdaptation(id: number | null | undefined): Promise { - if (id) { - const adaptation = await effects.getSequenceAdaptation(id, user); - - if (adaptation) { - try { - // This evaluates the custom sequence adaptation that is optionally provided by the user. - Function(adaptation.adaptation)(); - } catch (e) { - console.error(e); - showFailureToast('Invalid sequence adaptation'); - } - } - } else { - resetSequenceAdaptation(); } }