JSONLensRenderer.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <template>
  2. <div class="flex flex-col flex-1">
  3. <div
  4. class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-lowerSecondaryStickyFold"
  5. >
  6. <label class="font-semibold text-secondaryLight">
  7. {{ t("response.body") }}
  8. </label>
  9. <div class="flex">
  10. <ButtonSecondary
  11. v-if="response.body"
  12. v-tippy="{ theme: 'tooltip' }"
  13. :title="t('state.linewrap')"
  14. :class="{ '!text-accent': linewrapEnabled }"
  15. svg="wrap-text"
  16. @click.native.prevent="linewrapEnabled = !linewrapEnabled"
  17. />
  18. <ButtonSecondary
  19. v-if="response.body"
  20. ref="downloadResponse"
  21. v-tippy="{ theme: 'tooltip' }"
  22. :title="t('action.download_file')"
  23. :svg="downloadIcon"
  24. @click.native="downloadResponse"
  25. />
  26. <ButtonSecondary
  27. v-if="response.body"
  28. ref="copyResponse"
  29. v-tippy="{ theme: 'tooltip' }"
  30. :title="t('action.copy')"
  31. :svg="copyIcon"
  32. @click.native="copyResponse"
  33. />
  34. </div>
  35. </div>
  36. <div ref="jsonResponse" class="flex flex-col flex-1"></div>
  37. <div
  38. v-if="outlinePath"
  39. class="sticky bottom-0 z-10 flex px-2 overflow-auto border-t bg-primaryLight border-dividerLight flex-nowrap hide-scrollbar"
  40. >
  41. <div
  42. v-for="(item, index) in outlinePath"
  43. :key="`item-${index}`"
  44. class="flex items-center"
  45. >
  46. <tippy
  47. ref="outlineOptions"
  48. interactive
  49. trigger="click"
  50. theme="popover"
  51. arrow
  52. >
  53. <template #trigger>
  54. <div v-if="item.kind === 'RootObject'" class="outline-item">{}</div>
  55. <div v-if="item.kind === 'RootArray'" class="outline-item">[]</div>
  56. <div v-if="item.kind === 'ArrayMember'" class="outline-item">
  57. {{ item.index }}
  58. </div>
  59. <div v-if="item.kind === 'ObjectMember'" class="outline-item">
  60. {{ item.name }}
  61. </div>
  62. </template>
  63. <div
  64. v-if="item.kind === 'ArrayMember' || item.kind === 'ObjectMember'"
  65. >
  66. <div
  67. v-if="item.kind === 'ArrayMember'"
  68. class="flex flex-col"
  69. role="menu"
  70. >
  71. <SmartItem
  72. v-for="(arrayMember, astIndex) in item.astParent.values"
  73. :key="`ast-${astIndex}`"
  74. :label="`${astIndex}`"
  75. @click.native="
  76. () => {
  77. jumpCursor(arrayMember)
  78. outlineOptions[index].tippy().hide()
  79. }
  80. "
  81. />
  82. </div>
  83. <div
  84. v-if="item.kind === 'ObjectMember'"
  85. class="flex flex-col"
  86. role="menu"
  87. >
  88. <SmartItem
  89. v-for="(objectMember, astIndex) in item.astParent.members"
  90. :key="`ast-${astIndex}`"
  91. :label="objectMember.key.value"
  92. @click.native="
  93. () => {
  94. jumpCursor(objectMember)
  95. outlineOptions[index].tippy().hide()
  96. }
  97. "
  98. />
  99. </div>
  100. </div>
  101. <div
  102. v-if="item.kind === 'RootObject'"
  103. class="flex flex-col"
  104. role="menu"
  105. >
  106. <SmartItem
  107. label="{}"
  108. @click.native="
  109. () => {
  110. jumpCursor(item.astValue)
  111. outlineOptions[index].tippy().hide()
  112. }
  113. "
  114. />
  115. </div>
  116. <div
  117. v-if="item.kind === 'RootArray'"
  118. class="flex flex-col"
  119. role="menu"
  120. >
  121. <SmartItem
  122. label="[]"
  123. @click.native="
  124. () => {
  125. jumpCursor(item.astValue)
  126. outlineOptions[index].tippy().hide()
  127. }
  128. "
  129. />
  130. </div>
  131. </tippy>
  132. <i
  133. v-if="index + 1 !== outlinePath.length"
  134. class="opacity-50 text-secondaryLight material-icons"
  135. >chevron_right</i
  136. >
  137. </div>
  138. </div>
  139. </div>
  140. </template>
  141. <script setup lang="ts">
  142. import * as LJSON from "lossless-json"
  143. import * as O from "fp-ts/Option"
  144. import { pipe } from "fp-ts/function"
  145. import { computed, ref, reactive } from "@nuxtjs/composition-api"
  146. import { useCodemirror } from "~/helpers/editor/codemirror"
  147. import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
  148. import jsonParse, { JSONObjectMember, JSONValue } from "~/helpers/jsonParse"
  149. import { getJSONOutlineAtPos } from "~/helpers/newOutline"
  150. import {
  151. convertIndexToLineCh,
  152. convertLineChToIndex,
  153. } from "~/helpers/editor/utils"
  154. import { useI18n } from "~/helpers/utils/composables"
  155. import useCopyResponse from "~/helpers/lenses/composables/useCopyResponse"
  156. import useResponseBody from "~/helpers/lenses/composables/useResponseBody"
  157. import useDownloadResponse from "~/helpers/lenses/composables/useDownloadResponse"
  158. const t = useI18n()
  159. const props = defineProps<{
  160. response: HoppRESTResponse
  161. }>()
  162. const { responseBodyText } = useResponseBody(props.response)
  163. const { copyIcon, copyResponse } = useCopyResponse(responseBodyText)
  164. const { downloadIcon, downloadResponse } = useDownloadResponse(
  165. "application/json",
  166. responseBodyText
  167. )
  168. const jsonBodyText = computed(() =>
  169. pipe(
  170. responseBodyText.value,
  171. O.tryCatchK(LJSON.parse),
  172. O.map((val) => LJSON.stringify(val, undefined, 2)),
  173. O.getOrElse(() => responseBodyText.value)
  174. )
  175. )
  176. const ast = computed(() =>
  177. pipe(
  178. jsonBodyText.value,
  179. O.tryCatchK(jsonParse),
  180. O.getOrElseW(() => null)
  181. )
  182. )
  183. const outlineOptions = ref<any | null>(null)
  184. const jsonResponse = ref<any | null>(null)
  185. const linewrapEnabled = ref(true)
  186. const { cursor } = useCodemirror(
  187. jsonResponse,
  188. jsonBodyText,
  189. reactive({
  190. extendedEditorConfig: {
  191. mode: "application/ld+json",
  192. readOnly: true,
  193. lineWrapping: linewrapEnabled,
  194. },
  195. linter: null,
  196. completer: null,
  197. environmentHighlights: true,
  198. })
  199. )
  200. const jumpCursor = (ast: JSONValue | JSONObjectMember) => {
  201. const pos = convertIndexToLineCh(jsonBodyText.value, ast.start)
  202. pos.line--
  203. cursor.value = pos
  204. }
  205. const outlinePath = computed(() =>
  206. pipe(
  207. ast.value,
  208. O.fromNullable,
  209. O.map((ast) =>
  210. getJSONOutlineAtPos(
  211. ast,
  212. convertLineChToIndex(jsonBodyText.value, cursor.value)
  213. )
  214. ),
  215. O.getOrElseW(() => null)
  216. )
  217. )
  218. </script>
  219. <style lang="scss" scoped>
  220. .outline-item {
  221. @apply cursor-pointer;
  222. @apply flex-grow-0 flex-shrink-0;
  223. @apply text-secondaryLight;
  224. @apply inline-flex;
  225. @apply items-center;
  226. @apply px-2;
  227. @apply py-1;
  228. @apply transition;
  229. @apply hover:text-secondary;
  230. }
  231. </style>