JSONLensRenderer.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
  150. import jsonParse, { JSONObjectMember, JSONValue } from "~/helpers/jsonParse"
  151. import { getJSONOutlineAtPos } from "~/helpers/newOutline"
  152. import {
  153. convertIndexToLineCh,
  154. convertLineChToIndex,
  155. } from "~/helpers/editor/utils"
  156. const props = defineProps<{
  157. response: HoppRESTResponse
  158. }>()
  159. const {
  160. $toast,
  161. app: { i18n },
  162. } = useContext()
  163. const t = i18n.t.bind(i18n)
  164. const responseBodyText = computed(() => {
  165. if (
  166. props.response.type === "loading" ||
  167. props.response.type === "network_fail"
  168. )
  169. return ""
  170. if (typeof props.response.body === "string") return props.response.body
  171. else {
  172. const res = new TextDecoder("utf-8").decode(props.response.body)
  173. // HACK: Temporary trailing null character issue from the extension fix
  174. return res.replace(/\0+$/, "")
  175. }
  176. })
  177. const downloadIcon = ref("download")
  178. const copyIcon = ref("copy")
  179. const jsonBodyText = computed(() => {
  180. try {
  181. return JSON.stringify(JSON.parse(responseBodyText.value), null, 2)
  182. } catch (e) {
  183. // Most probs invalid JSON was returned, so drop prettification (should we warn ?)
  184. return responseBodyText.value
  185. }
  186. })
  187. const ast = computed(() => {
  188. try {
  189. return jsonParse(jsonBodyText.value)
  190. } catch (_: any) {
  191. return null
  192. }
  193. })
  194. const outlineOptions = ref<any | null>(null)
  195. const jsonResponse = ref<any | null>(null)
  196. const linewrapEnabled = ref(true)
  197. const { cursor } = useCodemirror(
  198. jsonResponse,
  199. jsonBodyText,
  200. reactive({
  201. extendedEditorConfig: {
  202. mode: "application/ld+json",
  203. readOnly: true,
  204. lineWrapping: linewrapEnabled,
  205. },
  206. linter: null,
  207. completer: null,
  208. })
  209. )
  210. const jumpCursor = (ast: JSONValue | JSONObjectMember) => {
  211. const pos = convertIndexToLineCh(jsonBodyText.value, ast.start)
  212. pos.line--
  213. cursor.value = pos
  214. }
  215. const downloadResponse = () => {
  216. const dataToWrite = responseBodyText.value
  217. const file = new Blob([dataToWrite], { type: "application/json" })
  218. const a = document.createElement("a")
  219. const url = URL.createObjectURL(file)
  220. a.href = url
  221. // TODO get uri from meta
  222. a.download = `${url.split("/").pop().split("#")[0].split("?")[0]}`
  223. document.body.appendChild(a)
  224. a.click()
  225. downloadIcon.value = "check"
  226. $toast.success(`${t("state.download_started")}`, {
  227. icon: "downloading",
  228. })
  229. setTimeout(() => {
  230. document.body.removeChild(a)
  231. URL.revokeObjectURL(url)
  232. downloadIcon.value = "download"
  233. }, 1000)
  234. }
  235. const outlinePath = computed(() => {
  236. if (ast.value) {
  237. return getJSONOutlineAtPos(
  238. ast.value,
  239. convertLineChToIndex(jsonBodyText.value, cursor.value)
  240. )
  241. } else return null
  242. })
  243. const copyResponse = () => {
  244. copyToClipboard(responseBodyText.value)
  245. copyIcon.value = "check"
  246. $toast.success(`${t("state.copied_to_clipboard")}`, {
  247. icon: "content_paste",
  248. })
  249. setTimeout(() => (copyIcon.value = "copy"), 1000)
  250. }
  251. </script>
  252. <style lang="scss" scoped>
  253. .outline {
  254. @apply cursor-pointer;
  255. @apply flex-grow-0 flex-shrink-0;
  256. @apply text-secondaryLight;
  257. @apply inline-flex;
  258. @apply items-center;
  259. @apply px-2;
  260. @apply py-1;
  261. @apply transition;
  262. @apply hover:text-secondary;
  263. }
  264. </style>