JSONLensRenderer.vue 7.5 KB

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