JSONLensRenderer.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <div>
  3. <div
  4. class="
  5. bg-primary
  6. border-dividerLight
  7. top-lowerSecondaryStickyFold
  8. sticky
  9. z-10
  10. flex
  11. items-center
  12. justify-between
  13. flex-1
  14. pl-4
  15. border-b
  16. "
  17. >
  18. <label class="font-semibold text-secondaryLight">{{
  19. t("response.body")
  20. }}</label>
  21. <div class="flex">
  22. <ButtonSecondary
  23. v-if="response.body"
  24. v-tippy="{ theme: 'tooltip' }"
  25. :title="t('state.linewrap')"
  26. :class="{ '!text-accent': linewrapEnabled }"
  27. svg="corner-down-left"
  28. @click.native.prevent="linewrapEnabled = !linewrapEnabled"
  29. />
  30. <ButtonSecondary
  31. v-if="response.body"
  32. ref="downloadResponse"
  33. v-tippy="{ theme: 'tooltip' }"
  34. :title="t('action.download_file')"
  35. :svg="downloadIcon"
  36. @click.native="downloadResponse"
  37. />
  38. <ButtonSecondary
  39. v-if="response.body"
  40. ref="copyResponse"
  41. v-tippy="{ theme: 'tooltip' }"
  42. :title="t('action.copy')"
  43. :svg="copyIcon"
  44. @click.native="copyResponse"
  45. />
  46. </div>
  47. </div>
  48. <div ref="jsonResponse"></div>
  49. <div
  50. v-if="outlinePath"
  51. class="
  52. flex
  53. bg-primaryLight
  54. border-dividerLight
  55. flex-nowrap
  56. hide-scrollbar
  57. sticky
  58. bottom-0
  59. z-10
  60. flex-1
  61. px-2
  62. overflow-auto
  63. border-t
  64. "
  65. >
  66. <div
  67. v-for="(item, index) in outlinePath"
  68. :key="`item-${index}`"
  69. class="flex items-center"
  70. >
  71. <tippy
  72. ref="outlineOptions"
  73. interactive
  74. trigger="click"
  75. theme="popover"
  76. arrow
  77. >
  78. <template #trigger>
  79. <div v-if="item.kind === 'RootObject'" class="outline">{}</div>
  80. <div v-if="item.kind === 'RootArray'" class="outline">[]</div>
  81. <div v-if="item.kind === 'ArrayMember'" class="outline">
  82. {{ item.index }}
  83. </div>
  84. <div v-if="item.kind === 'ObjectMember'" class="outline">
  85. {{ item.name }}
  86. </div>
  87. </template>
  88. <div
  89. v-if="item.kind === 'ArrayMember' || item.kind === 'ObjectMember'"
  90. >
  91. <div v-if="item.kind === 'ArrayMember'" class="flex flex-col">
  92. <SmartItem
  93. v-for="(arrayMember, astIndex) in item.astParent.values"
  94. :key="`ast-${astIndex}`"
  95. :label="`${astIndex}`"
  96. @click.native="
  97. () => {
  98. jumpCursor(arrayMember)
  99. outlineOptions[index].tippy().hide()
  100. }
  101. "
  102. />
  103. </div>
  104. <div v-if="item.kind === 'ObjectMember'" class="flex flex-col">
  105. <SmartItem
  106. v-for="(objectMember, astIndex) in item.astParent.members"
  107. :key="`ast-${astIndex}`"
  108. :label="objectMember.key.value"
  109. @click.native="
  110. () => {
  111. jumpCursor(objectMember)
  112. outlineOptions[index].tippy().hide()
  113. }
  114. "
  115. />
  116. </div>
  117. </div>
  118. <div v-if="item.kind === 'RootObject'" class="flex flex-col">
  119. <SmartItem
  120. label="{}"
  121. @click.native="
  122. () => {
  123. jumpCursor(item.astValue)
  124. outlineOptions[index].tippy().hide()
  125. }
  126. "
  127. />
  128. </div>
  129. <div v-if="item.kind === 'RootArray'" class="flex flex-col">
  130. <SmartItem
  131. label="[]"
  132. @click.native="
  133. () => {
  134. jumpCursor(item.astValue)
  135. outlineOptions[index].tippy().hide()
  136. }
  137. "
  138. />
  139. </div>
  140. </tippy>
  141. <i
  142. v-if="index + 1 !== outlinePath.length"
  143. class="opacity-50 text-secondaryLight material-icons"
  144. >chevron_right</i
  145. >
  146. </div>
  147. </div>
  148. </div>
  149. </template>
  150. <script setup lang="ts">
  151. import { computed, ref, reactive } from "@nuxtjs/composition-api"
  152. import { useCodemirror } from "~/helpers/editor/codemirror"
  153. import { copyToClipboard } from "~/helpers/utils/clipboard"
  154. import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
  155. import jsonParse, { JSONObjectMember, JSONValue } from "~/helpers/jsonParse"
  156. import { getJSONOutlineAtPos } from "~/helpers/newOutline"
  157. import {
  158. convertIndexToLineCh,
  159. convertLineChToIndex,
  160. } from "~/helpers/editor/utils"
  161. import { useI18n, useToast } from "~/helpers/utils/composables"
  162. const t = useI18n()
  163. const props = defineProps<{
  164. response: HoppRESTResponse
  165. }>()
  166. const toast = useToast()
  167. const responseBodyText = computed(() => {
  168. if (
  169. props.response.type === "loading" ||
  170. props.response.type === "network_fail"
  171. )
  172. return ""
  173. if (typeof props.response.body === "string") return props.response.body
  174. else {
  175. const res = new TextDecoder("utf-8").decode(props.response.body)
  176. // HACK: Temporary trailing null character issue from the extension fix
  177. return res.replace(/\0+$/, "")
  178. }
  179. })
  180. const downloadIcon = ref("download")
  181. const copyIcon = ref("copy")
  182. const jsonBodyText = computed(() => {
  183. try {
  184. return JSON.stringify(JSON.parse(responseBodyText.value), null, 2)
  185. } catch (e) {
  186. // Most probs invalid JSON was returned, so drop prettification (should we warn ?)
  187. return responseBodyText.value
  188. }
  189. })
  190. const ast = computed(() => {
  191. try {
  192. return jsonParse(jsonBodyText.value)
  193. } catch (_: any) {
  194. return null
  195. }
  196. })
  197. const outlineOptions = ref<any | null>(null)
  198. const jsonResponse = ref<any | null>(null)
  199. const linewrapEnabled = ref(true)
  200. const { cursor } = useCodemirror(
  201. jsonResponse,
  202. jsonBodyText,
  203. reactive({
  204. extendedEditorConfig: {
  205. mode: "application/ld+json",
  206. readOnly: true,
  207. lineWrapping: linewrapEnabled,
  208. },
  209. linter: null,
  210. completer: null,
  211. })
  212. )
  213. const jumpCursor = (ast: JSONValue | JSONObjectMember) => {
  214. const pos = convertIndexToLineCh(jsonBodyText.value, ast.start)
  215. pos.line--
  216. cursor.value = pos
  217. }
  218. const downloadResponse = () => {
  219. const dataToWrite = responseBodyText.value
  220. const file = new Blob([dataToWrite], { type: "application/json" })
  221. const a = document.createElement("a")
  222. const url = URL.createObjectURL(file)
  223. a.href = url
  224. // TODO get uri from meta
  225. a.download = `${url.split("/").pop().split("#")[0].split("?")[0]}`
  226. document.body.appendChild(a)
  227. a.click()
  228. downloadIcon.value = "check"
  229. toast.success(`${t("state.download_started")}`)
  230. setTimeout(() => {
  231. document.body.removeChild(a)
  232. URL.revokeObjectURL(url)
  233. downloadIcon.value = "download"
  234. }, 1000)
  235. }
  236. const outlinePath = computed(() => {
  237. if (ast.value) {
  238. return getJSONOutlineAtPos(
  239. ast.value,
  240. convertLineChToIndex(jsonBodyText.value, cursor.value)
  241. )
  242. } else return null
  243. })
  244. const copyResponse = () => {
  245. copyToClipboard(responseBodyText.value)
  246. copyIcon.value = "check"
  247. toast.success(`${t("state.copied_to_clipboard")}`)
  248. setTimeout(() => (copyIcon.value = "copy"), 1000)
  249. }
  250. </script>
  251. <style lang="scss" scoped>
  252. .outline {
  253. @apply cursor-pointer;
  254. @apply flex-grow-0 flex-shrink-0;
  255. @apply text-secondaryLight;
  256. @apply inline-flex;
  257. @apply items-center;
  258. @apply px-2;
  259. @apply py-1;
  260. @apply transition;
  261. @apply hover:text-secondary;
  262. }
  263. </style>