feat: fix outline
This commit is contained in:
@@ -465,9 +465,9 @@ input[type="checkbox"] {
|
|||||||
.CodeMirror {
|
.CodeMirror {
|
||||||
@apply !h-auto;
|
@apply !h-auto;
|
||||||
|
|
||||||
&:not(.CodeMirror-focused) .CodeMirror-activeline-background {
|
// &:not(.CodeMirror-focused) .CodeMirror-activeline-background {
|
||||||
background: transparent !important;
|
// background: transparent !important;
|
||||||
}
|
// }
|
||||||
|
|
||||||
.CodeMirror-dialog-top {
|
.CodeMirror-dialog-top {
|
||||||
@apply bg-primaryLight;
|
@apply bg-primaryLight;
|
||||||
|
|||||||
@@ -13,9 +13,9 @@
|
|||||||
justify-between
|
justify-between
|
||||||
"
|
"
|
||||||
>
|
>
|
||||||
<label class="font-semibold text-secondaryLight">
|
<label class="font-semibold text-secondaryLight">{{
|
||||||
{{ $t("response.body") }}
|
$t("response.body")
|
||||||
</label>
|
}}</label>
|
||||||
<div class="flex">
|
<div class="flex">
|
||||||
<ButtonSecondary
|
<ButtonSecondary
|
||||||
v-if="response.body"
|
v-if="response.body"
|
||||||
@@ -44,15 +44,89 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div ref="jsonResponse"></div>
|
<div ref="jsonResponse"></div>
|
||||||
|
<div
|
||||||
|
v-if="outlinePath"
|
||||||
|
class="
|
||||||
|
h-32
|
||||||
|
bg-primaryLight
|
||||||
|
border-t border-dividerLight
|
||||||
|
flex flex-nowrap flex-1
|
||||||
|
py-2
|
||||||
|
px-4
|
||||||
|
bottom-0
|
||||||
|
z-10
|
||||||
|
sticky
|
||||||
|
overflow-auto
|
||||||
|
hide-scrollbar
|
||||||
|
"
|
||||||
|
>
|
||||||
|
<div v-for="(item, index) in outlinePath" :key="`item-${index}`">
|
||||||
|
<tippy ref="options" interactive trigger="click" theme="popover" arrow>
|
||||||
|
<template #trigger>
|
||||||
|
<ButtonSecondary
|
||||||
|
v-if="item.kind === 'RootObject'"
|
||||||
|
:label="item.kind"
|
||||||
|
/>
|
||||||
|
<ButtonSecondary
|
||||||
|
v-if="item.kind === 'RootArray'"
|
||||||
|
:label="item.kind"
|
||||||
|
/>
|
||||||
|
<ButtonSecondary
|
||||||
|
v-if="item.kind === 'ArrayMember'"
|
||||||
|
:label="item.index.toString()"
|
||||||
|
/>
|
||||||
|
<ButtonSecondary
|
||||||
|
v-if="item.kind === 'ObjectMember'"
|
||||||
|
:label="item.name"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
<div
|
||||||
|
v-if="item.kind === 'ArrayMember' || item.kind === 'ObjectMember'"
|
||||||
|
>
|
||||||
|
<div v-if="item.kind === 'ArrayMember'">
|
||||||
|
<ButtonSecondary
|
||||||
|
v-for="(ast, astIndex) in item.astParent.values"
|
||||||
|
:key="`ast-${astIndex}`"
|
||||||
|
:label="astIndex.toString()"
|
||||||
|
@click.native="jumpCursor(ast)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div v-if="item.kind === 'ObjectMember'">
|
||||||
|
<ButtonSecondary
|
||||||
|
v-for="(ast, astIndex) in item.astParent.members"
|
||||||
|
:key="`ast-${astIndex}`"
|
||||||
|
:label="ast.key.value"
|
||||||
|
@click.native="jumpCursor(ast)"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</tippy>
|
||||||
|
<i v-if="index + 1 !== outlinePath.length" class="mx-2 material-icons"
|
||||||
|
>chevron_right</i
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, ref, useContext, reactive } from "@nuxtjs/composition-api"
|
import {
|
||||||
|
computed,
|
||||||
|
ref,
|
||||||
|
watchEffect,
|
||||||
|
useContext,
|
||||||
|
reactive,
|
||||||
|
} from "@nuxtjs/composition-api"
|
||||||
import { useCodemirror } from "~/helpers/editor/codemirror"
|
import { useCodemirror } from "~/helpers/editor/codemirror"
|
||||||
import { copyToClipboard } from "~/helpers/utils/clipboard"
|
import { copyToClipboard } from "~/helpers/utils/clipboard"
|
||||||
import "codemirror/mode/javascript/javascript"
|
import "codemirror/mode/javascript/javascript"
|
||||||
import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
|
import { HoppRESTResponse } from "~/helpers/types/HoppRESTResponse"
|
||||||
|
import jsonParse, { JSONObjectMember, JSONValue } from "~/helpers/jsonParse"
|
||||||
|
import { getJSONOutlineAtPos } from "~/helpers/newOutline"
|
||||||
|
import {
|
||||||
|
convertIndexToLineCh,
|
||||||
|
convertLineChToIndex,
|
||||||
|
} from "~/helpers/editor/utils"
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
response: HoppRESTResponse
|
response: HoppRESTResponse
|
||||||
@@ -90,10 +164,18 @@ const jsonBodyText = computed(() => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const ast = computed(() => {
|
||||||
|
try {
|
||||||
|
return jsonParse(jsonBodyText.value)
|
||||||
|
} catch (_: any) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const jsonResponse = ref<any | null>(null)
|
const jsonResponse = ref<any | null>(null)
|
||||||
const linewrapEnabled = ref(true)
|
const linewrapEnabled = ref(true)
|
||||||
|
|
||||||
useCodemirror(
|
const { cursor } = useCodemirror(
|
||||||
jsonResponse,
|
jsonResponse,
|
||||||
jsonBodyText,
|
jsonBodyText,
|
||||||
reactive({
|
reactive({
|
||||||
@@ -107,6 +189,13 @@ useCodemirror(
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const jumpCursor = (ast: JSONValue | JSONObjectMember) => {
|
||||||
|
console.log(jsonBodyText.value)
|
||||||
|
console.log(ast.start)
|
||||||
|
console.log(convertIndexToLineCh(jsonBodyText.value, ast.start))
|
||||||
|
cursor.value = convertIndexToLineCh(jsonBodyText.value, ast.start)
|
||||||
|
}
|
||||||
|
|
||||||
const downloadResponse = () => {
|
const downloadResponse = () => {
|
||||||
const dataToWrite = responseBodyText.value
|
const dataToWrite = responseBodyText.value
|
||||||
const file = new Blob([dataToWrite], { type: "application/json" })
|
const file = new Blob([dataToWrite], { type: "application/json" })
|
||||||
@@ -128,6 +217,19 @@ const downloadResponse = () => {
|
|||||||
}, 1000)
|
}, 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const outlinePath = computed(() => {
|
||||||
|
if (ast.value) {
|
||||||
|
return getJSONOutlineAtPos(
|
||||||
|
ast.value,
|
||||||
|
convertLineChToIndex(jsonBodyText.value, cursor.value)
|
||||||
|
)
|
||||||
|
} else return null
|
||||||
|
})
|
||||||
|
|
||||||
|
watchEffect(() => {
|
||||||
|
console.log(outlinePath.value)
|
||||||
|
})
|
||||||
|
|
||||||
const copyResponse = () => {
|
const copyResponse = () => {
|
||||||
copyToClipboard(responseBodyText.value)
|
copyToClipboard(responseBodyText.value)
|
||||||
copyIcon.value = "check"
|
copyIcon.value = "check"
|
||||||
|
|||||||
@@ -61,10 +61,11 @@ export function useCodemirror(
|
|||||||
el: Ref<any | null>,
|
el: Ref<any | null>,
|
||||||
value: Ref<string>,
|
value: Ref<string>,
|
||||||
options: CodeMirrorOptions
|
options: CodeMirrorOptions
|
||||||
) {
|
): { cm: Ref<CodeMirror.Position | null>; cursor: Ref<CodeMirror.Position> } {
|
||||||
const { $colorMode } = useContext() as any
|
const { $colorMode } = useContext() as any
|
||||||
|
|
||||||
const cm = ref<CodeMirror.Editor | null>(null)
|
const cm = ref<CodeMirror.Editor | null>(null)
|
||||||
|
const cursor = ref<CodeMirror.Position>({ line: 0, ch: 0 })
|
||||||
|
|
||||||
const updateEditorConfig = () => {
|
const updateEditorConfig = () => {
|
||||||
Object.keys(options.extendedEditorConfig).forEach((key) => {
|
Object.keys(options.extendedEditorConfig).forEach((key) => {
|
||||||
@@ -135,6 +136,10 @@ export function useCodemirror(
|
|||||||
value.value = instance.getValue()
|
value.value = instance.getValue()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
cm.value.on("cursorActivity", (instance) => {
|
||||||
|
cursor.value = instance.getCursor()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Boot-up CodeMirror, set the value and listeners
|
// Boot-up CodeMirror, set the value and listeners
|
||||||
@@ -192,10 +197,20 @@ export function useCodemirror(
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Push cursor updates
|
||||||
|
watch(cursor, (value) => {
|
||||||
|
if (value !== cm.value?.getCursor()) {
|
||||||
|
cm.value?.focus()
|
||||||
|
console.log(value)
|
||||||
|
cm.value?.setCursor(value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Watch color mode updates and update theme
|
// Watch color mode updates and update theme
|
||||||
watch(() => $colorMode.value, setTheme)
|
watch(() => $colorMode.value, setTheme)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cm,
|
cm,
|
||||||
|
cursor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ export function convertIndexToLineCh(
|
|||||||
text: string,
|
text: string,
|
||||||
i: number
|
i: number
|
||||||
): { line: number; ch: number } {
|
): { line: number; ch: number } {
|
||||||
const lines = text.split("/n")
|
const lines = text.split("\n")
|
||||||
|
|
||||||
let line = 0
|
let line = 0
|
||||||
let counter = 0
|
let counter = 0
|
||||||
@@ -21,3 +21,18 @@ export function convertIndexToLineCh(
|
|||||||
|
|
||||||
throw new Error("Invalid input")
|
throw new Error("Invalid input")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function convertLineChToIndex(
|
||||||
|
text: string,
|
||||||
|
lineCh: { line: number; ch: number }
|
||||||
|
): number {
|
||||||
|
const textSplit = text.split("\n")
|
||||||
|
|
||||||
|
if (textSplit.length < lineCh.line) throw new Error("Invalid position")
|
||||||
|
|
||||||
|
const tillLineIndex = textSplit
|
||||||
|
.slice(0, lineCh.line)
|
||||||
|
.reduce((acc, line) => acc + line.length + 1, 0)
|
||||||
|
|
||||||
|
return tillLineIndex + lineCh.ch
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,7 +19,75 @@
|
|||||||
* - end: int - the end exclusive offset of the syntax error
|
* - end: int - the end exclusive offset of the syntax error
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export default function jsonParse(str) {
|
type JSONEOFValue = {
|
||||||
|
kind: "EOF"
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
}
|
||||||
|
|
||||||
|
type JSONNullValue = {
|
||||||
|
kind: "Null"
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
}
|
||||||
|
|
||||||
|
type JSONNumberValue = {
|
||||||
|
kind: "Number"
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
value: number
|
||||||
|
}
|
||||||
|
|
||||||
|
type JSONStringValue = {
|
||||||
|
kind: "String"
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
value: string
|
||||||
|
}
|
||||||
|
|
||||||
|
type JSONBooleanValue = {
|
||||||
|
kind: "Boolean"
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
value: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
type JSONPrimitiveValue =
|
||||||
|
| JSONNullValue
|
||||||
|
| JSONEOFValue
|
||||||
|
| JSONStringValue
|
||||||
|
| JSONNumberValue
|
||||||
|
| JSONBooleanValue
|
||||||
|
|
||||||
|
export type JSONObjectValue = {
|
||||||
|
kind: "Object"
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
// eslint-disable-next-line no-use-before-define
|
||||||
|
members: JSONObjectMember[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export type JSONArrayValue = {
|
||||||
|
kind: "Array"
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
// eslint-disable-next-line no-use-before-define
|
||||||
|
values: JSONValue[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export type JSONValue = JSONObjectValue | JSONArrayValue | JSONPrimitiveValue
|
||||||
|
|
||||||
|
export type JSONObjectMember = {
|
||||||
|
kind: "Member"
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
key: JSONStringValue
|
||||||
|
value: JSONValue
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function jsonParse(
|
||||||
|
str: string
|
||||||
|
): JSONObjectValue | JSONArrayValue {
|
||||||
string = str
|
string = str
|
||||||
strLen = str.length
|
strLen = str.length
|
||||||
start = end = lastEnd = -1
|
start = end = lastEnd = -1
|
||||||
@@ -37,15 +105,15 @@ export default function jsonParse(str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let string
|
let string: string
|
||||||
let strLen
|
let strLen: number
|
||||||
let start
|
let start: number
|
||||||
let end
|
let end: number
|
||||||
let lastEnd
|
let lastEnd: number
|
||||||
let code
|
let code: number
|
||||||
let kind
|
let kind: string
|
||||||
|
|
||||||
function parseObj() {
|
function parseObj(): JSONObjectValue {
|
||||||
const nodeStart = start
|
const nodeStart = start
|
||||||
const members = []
|
const members = []
|
||||||
expect("{")
|
expect("{")
|
||||||
@@ -63,9 +131,9 @@ function parseObj() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseMember() {
|
function parseMember(): JSONObjectMember {
|
||||||
const nodeStart = start
|
const nodeStart = start
|
||||||
const key = kind === "String" ? curToken() : null
|
const key = kind === "String" ? (curToken() as JSONStringValue) : null
|
||||||
expect("String")
|
expect("String")
|
||||||
expect(":")
|
expect(":")
|
||||||
const value = parseVal()
|
const value = parseVal()
|
||||||
@@ -73,14 +141,14 @@ function parseMember() {
|
|||||||
kind: "Member",
|
kind: "Member",
|
||||||
start: nodeStart,
|
start: nodeStart,
|
||||||
end: lastEnd,
|
end: lastEnd,
|
||||||
key,
|
key: key!,
|
||||||
value,
|
value,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseArr() {
|
function parseArr(): JSONArrayValue {
|
||||||
const nodeStart = start
|
const nodeStart = start
|
||||||
const values = []
|
const values: JSONValue[] = []
|
||||||
expect("[")
|
expect("[")
|
||||||
if (!skip("]")) {
|
if (!skip("]")) {
|
||||||
do {
|
do {
|
||||||
@@ -96,7 +164,7 @@ function parseArr() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseVal() {
|
function parseVal(): JSONValue {
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
case "[":
|
case "[":
|
||||||
return parseArr()
|
return parseArr()
|
||||||
@@ -111,14 +179,19 @@ function parseVal() {
|
|||||||
lex()
|
lex()
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
return expect("Value")
|
return expect("Value") as never
|
||||||
}
|
}
|
||||||
|
|
||||||
function curToken() {
|
function curToken(): JSONPrimitiveValue {
|
||||||
return { kind, start, end, value: JSON.parse(string.slice(start, end)) }
|
return {
|
||||||
|
kind: kind as any,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
value: JSON.parse(string.slice(start, end)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function expect(str) {
|
function expect(str: string) {
|
||||||
if (kind === str) {
|
if (kind === str) {
|
||||||
lex()
|
lex()
|
||||||
return
|
return
|
||||||
@@ -137,11 +210,17 @@ function expect(str) {
|
|||||||
throw syntaxError(`Expected ${str} but found ${found}.`)
|
throw syntaxError(`Expected ${str} but found ${found}.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
function syntaxError(message) {
|
type SyntaxError = {
|
||||||
|
message: string
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
}
|
||||||
|
|
||||||
|
function syntaxError(message: string): SyntaxError {
|
||||||
return { message, start, end }
|
return { message, start, end }
|
||||||
}
|
}
|
||||||
|
|
||||||
function skip(k) {
|
function skip(k: string) {
|
||||||
if (kind === k) {
|
if (kind === k) {
|
||||||
lex()
|
lex()
|
||||||
return true
|
return true
|
||||||
@@ -227,7 +306,7 @@ function lex() {
|
|||||||
function readString() {
|
function readString() {
|
||||||
ch()
|
ch()
|
||||||
while (code !== 34 && code > 31) {
|
while (code !== 34 && code > 31) {
|
||||||
if (code === 92) {
|
if (code === (92 as any)) {
|
||||||
// \
|
// \
|
||||||
ch()
|
ch()
|
||||||
switch (code) {
|
switch (code) {
|
||||||
@@ -299,7 +378,7 @@ function readNumber() {
|
|||||||
if (code === 69 || code === 101) {
|
if (code === 69 || code === 101) {
|
||||||
// E e
|
// E e
|
||||||
ch()
|
ch()
|
||||||
if (code === 43 || code === 45) {
|
if (code === (43 as any) || code === (45 as any)) {
|
||||||
// + -
|
// + -
|
||||||
ch()
|
ch()
|
||||||
}
|
}
|
||||||
100
helpers/newOutline.ts
Normal file
100
helpers/newOutline.ts
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
import {
|
||||||
|
JSONArrayValue,
|
||||||
|
JSONObjectMember,
|
||||||
|
JSONObjectValue,
|
||||||
|
JSONValue,
|
||||||
|
} from "./jsonParse"
|
||||||
|
|
||||||
|
type RootEntry =
|
||||||
|
| {
|
||||||
|
kind: "RootObject"
|
||||||
|
astValue: JSONObjectValue
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
kind: "RootArray"
|
||||||
|
astValue: JSONArrayValue
|
||||||
|
}
|
||||||
|
|
||||||
|
type ObjectMemberEntry = {
|
||||||
|
kind: "ObjectMember"
|
||||||
|
name: string
|
||||||
|
astValue: JSONObjectMember
|
||||||
|
astParent: JSONObjectValue
|
||||||
|
}
|
||||||
|
|
||||||
|
type ArrayMemberEntry = {
|
||||||
|
kind: "ArrayMember"
|
||||||
|
index: number
|
||||||
|
astValue: JSONValue
|
||||||
|
astParent: JSONArrayValue
|
||||||
|
}
|
||||||
|
|
||||||
|
type PathEntry = RootEntry | ObjectMemberEntry | ArrayMemberEntry
|
||||||
|
|
||||||
|
export function getJSONOutlineAtPos(
|
||||||
|
jsonRootAst: JSONObjectValue | JSONArrayValue,
|
||||||
|
posIndex: number
|
||||||
|
): PathEntry[] | null {
|
||||||
|
try {
|
||||||
|
const rootObj = jsonRootAst
|
||||||
|
|
||||||
|
if (posIndex > rootObj.end || posIndex < rootObj.start)
|
||||||
|
throw new Error("Invalid position")
|
||||||
|
|
||||||
|
let current: JSONValue = rootObj
|
||||||
|
|
||||||
|
const path: PathEntry[] = []
|
||||||
|
|
||||||
|
if (rootObj.kind === "Object") {
|
||||||
|
path.push({
|
||||||
|
kind: "RootObject",
|
||||||
|
astValue: rootObj,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
path.push({
|
||||||
|
kind: "RootArray",
|
||||||
|
astValue: rootObj,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
while (current.kind === "Object" || current.kind === "Array") {
|
||||||
|
if (current.kind === "Object") {
|
||||||
|
const next: JSONObjectMember | undefined = current.members.find(
|
||||||
|
(member) => member.start <= posIndex && member.end >= posIndex
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!next) throw new Error("Couldn't find child")
|
||||||
|
|
||||||
|
path.push({
|
||||||
|
kind: "ObjectMember",
|
||||||
|
name: next.key.value,
|
||||||
|
astValue: next,
|
||||||
|
astParent: current,
|
||||||
|
})
|
||||||
|
|
||||||
|
current = next.value
|
||||||
|
} else {
|
||||||
|
const nextIndex = current.values.findIndex(
|
||||||
|
(value) => value.start <= posIndex && value.end >= posIndex
|
||||||
|
)
|
||||||
|
|
||||||
|
if (nextIndex < 0) throw new Error("Couldn't find child")
|
||||||
|
|
||||||
|
const next: JSONValue = current.values[nextIndex]
|
||||||
|
|
||||||
|
path.push({
|
||||||
|
kind: "ArrayMember",
|
||||||
|
index: nextIndex,
|
||||||
|
astValue: next,
|
||||||
|
astParent: current,
|
||||||
|
})
|
||||||
|
|
||||||
|
current = next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return path
|
||||||
|
} catch (e: any) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user