Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
feat: use prettier as formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-para committed Aug 4, 2023
1 parent 9ffc8e6 commit 3d891b1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"naive-ui": "^2.34.4",
"npm-run-all": "^4.1.5",
"postcss": "^8.4.27",
"prettier": "^3.0.1",
"tailwindcss": "^3.3.3",
"typescript": "~5.1.6",
"vite": "^4.4.6",
Expand Down
42 changes: 34 additions & 8 deletions src/components/JsonEdit.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
<script setup lang="ts">
import { NButton, NCode, NInput, NIcon } from 'naive-ui'
import { CheckOutlined, CloseOutlined, EditOutlined } from '@vicons/material'
import { ref } from 'vue'
import { ref, watch } from 'vue'
import * as prettier from 'prettier/standalone'
import babel from 'prettier/plugins/babel'
import estree from 'prettier/plugins/estree'
const val = defineModel<unknown>('value', {
required: true
})
function stringify(v: unknown) {
return JSON.stringify(v, null, 4)
async function stringify(v: unknown) {
return await prettier.format(JSON.stringify(v), {
parser: 'json',
plugins: [babel, estree],
tabWidth: 4
})
}
const cache = ref(stringify(val.value))
const cache = ref('')
let counter = 0
let version = 0
watch(
val,
nv => {
let id = ++counter
stringify(nv).then(src => {
if (id > version) {
version = id
cache.value = src
}
})
},
{
immediate: true,
deep: true
}
)
const editing = ref(false)
function enterEdit() {
cache.value = stringify(val.value)
async function enterEdit() {
cache.value = await stringify(val.value)
editing.value = true
}
Expand All @@ -33,7 +59,7 @@ function cancelEdit() {
</script>

<template>
<div class="flex flex-col gap-2">
<div class="flex flex-col gap-2 p-2">
<template v-if="editing">
<div class="flex gap-2">
<NButton @click="trySave">
Expand Down Expand Up @@ -70,7 +96,7 @@ function cancelEdit() {
</template>
</NButton>
</div>
<NCode language="json" :code="stringify(val)"></NCode>
<NCode language="json" :code="cache" word-wrap></NCode>
</template>
</div>
</template>

0 comments on commit 3d891b1

Please sign in to comment.