Skip to content

Commit

Permalink
Moved sequence adaptation loading code into SequenceEditor to include… (
Browse files Browse the repository at this point in the history
#1347)

* Moved sequence adaptation loading code into SequenceEditor to include it in the sequence preview
  • Loading branch information
cohansen authored Jun 25, 2024
1 parent c773b11 commit cf295d3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
40 changes: 39 additions & 1 deletion src/components/sequencing/SequenceEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -71,6 +72,12 @@
let selectedNode: SyntaxNode | null;
let toggleSeqJsonPreview: boolean = false;
$: {
if ($parcel?.sequence_adaptation_id) {
loadSequenceAdaptation($parcel?.sequence_adaptation_id);
}
}
$: {
if (editorSequenceView) {
editorSequenceView.dispatch({
Expand Down Expand Up @@ -178,6 +185,37 @@
});
});
onDestroy(() => {
resetSequenceAdaptation();
});
async function loadSequenceAdaptation(id: number | null | undefined): Promise<void> {
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();
Expand Down
35 changes: 0 additions & 35 deletions src/components/sequencing/SequenceForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,7 +19,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';
Expand Down Expand Up @@ -75,39 +73,6 @@
$: {
if (sequenceParcelId) {
$parcelId = sequenceParcelId;
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<void> {
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();
}
}
Expand Down

0 comments on commit cf295d3

Please sign in to comment.