From 8fa7eacfb0050a92ffeb16350dc067dc815b7c6c Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Mon, 31 Jan 2022 17:18:06 +0530 Subject: [PATCH] refactor: cleanup env highlight tooltip calculation --- .../components/smart/EnvInput.vue | 74 ++++++++----------- 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/packages/hoppscotch-app/components/smart/EnvInput.vue b/packages/hoppscotch-app/components/smart/EnvInput.vue index 63509bd04..940562d48 100644 --- a/packages/hoppscotch-app/components/smart/EnvInput.vue +++ b/packages/hoppscotch-app/components/smart/EnvInput.vue @@ -29,6 +29,8 @@ import debounce from "lodash/debounce" import isUndefined from "lodash/isUndefined" import { tippy } from "vue-tippy" import * as E from "fp-ts/Either" +import { pipe } from "fp-ts/function" +import { Subject } from "rxjs" import { aggregateEnvs$ } from "~/newstore/environments" import { useReadonlyStream } from "~/helpers/utils/composables" import { parseTemplateStringE } from "~/helpers/templating" @@ -138,6 +140,33 @@ export default defineComponent({ }, 5) this.debouncedHandler() }, + getResultSpan(envVar, highlightPos, position) { + const env = this.aggregateEnvs.find((k) => k.key === envVar) + + const envName = env?.sourceEnv ?? "choose an environment" + const value = pipe( + parseTemplateStringE(env?.value ?? "", this.aggregateEnvs), + E.map((value) => value.replace(/"/g, """)) + ) + + const isError = env === undefined || E.isLeft(value) + + return `${ + E.isLeft(value) && value.left === "ENV_EXPAND_LOOP" + ? this.$t("environment.nested_overflow") + : value.right + }">${this.safe_tags_replace( + this.internalValue.substring(position.start, position.end + 1) + )}` + }, processHighlights() { if (!this.highlightEnabled) { this.htmlOutput = this.internalValue @@ -212,25 +241,7 @@ export default defineComponent({ const envVar = this.internalValue .substring(position.start, position.end + 1) .slice(2, -2) - result += `${ - this.getEnvValue(this.constructEnv(envVar)) === "ENV_EXPAND_LOOP" - ? this.$t("environment.nested_overflow") - : this.getEnvValue(this.constructEnv(envVar)) - }">${this.safe_tags_replace( - this.internalValue.substring(position.start, position.end + 1) - )}` + result += this.getResultSpan(envVar, highlightPositions[k], position) startingPosition = position.end + 1 } if (startingPosition < this.internalValue.length) @@ -481,31 +492,6 @@ export default defineComponent({ textRange.select() } }, - getEnvName(name) { - if (name) return name - return "choose an environment" - }, - getEnvValue(value) { - if (value) return value.replace(/"/g, """) - if (value === "ENV_EXPAND_LOOP") return "ENV_EXPAND_LOOP" - // it does not filter special characters before adding them to HTML. - return "not found" - }, - constructEnv(envVar) { - const result = parseTemplateStringE( - this.getEnvValue( - this.aggregateEnvs.find((k) => k.key === envVar)?.value - ), - this.aggregateEnvs - ) - - if (E.isLeft(result)) { - console.error("error", result.left) - return result.left - } else { - return result.right - } - }, }, })