-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee7cfe2
commit 5665f60
Showing
7 changed files
with
271 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<script setup lang="ts"> | ||
import { identity } from 'lodash-es' | ||
import type { FormInstance, FormItemRule, FormRules } from 'element-plus' | ||
const props = withDefaults( | ||
defineProps<{ | ||
transformer?:(v: string) => string | ||
inputValidationRules?: FormItemRule[] | ||
inputLabel?: string | ||
inputPlaceholder?: string | ||
inputDefault?: string | ||
outputLabel?: string | ||
}>(), | ||
{ | ||
transformer: identity, | ||
inputValidationRules: () => [], | ||
inputLabel: '输入', | ||
inputDefault: '', | ||
inputPlaceholder: '输入...', | ||
outputLabel: '输出' | ||
} | ||
) | ||
const { transformer, inputValidationRules, inputLabel, outputLabel, inputPlaceholder, inputDefault } = toRefs(props) | ||
const emits = defineEmits<{(e: 'format', data: string): void}>() | ||
const ruleFormRef = ref<FormInstance>() | ||
const form = reactive({ | ||
input: inputDefault.value | ||
}) | ||
watch(inputDefault, (val) => { | ||
form.input = val | ||
}) | ||
const output = computed(() => transformer.value(form.input)) | ||
const rules = reactive<FormRules<typeof form>>({ | ||
input: inputValidationRules.value | ||
}) | ||
onMounted(() => { | ||
emits('format', output.value) | ||
}) | ||
watch(output, () => { | ||
emits('format', output.value) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="formatter-container"> | ||
<el-form | ||
ref="ruleFormRef" | ||
:model="form" | ||
label-position="top" | ||
:rules="rules" | ||
> | ||
<el-form-item | ||
:label="inputLabel" | ||
prop="input" | ||
> | ||
<el-input | ||
v-model="form.input" | ||
:placeholder="inputPlaceholder" | ||
:rows="20" | ||
type="textarea" | ||
/> | ||
</el-form-item> | ||
<el-form-item | ||
:label="outputLabel" | ||
> | ||
<el-input | ||
:model-value="output" | ||
:rows="20" | ||
type="textarea" | ||
readonly | ||
/> | ||
</el-form-item> | ||
</el-form> | ||
</div> | ||
</template> | ||
|
||
<style scoped lang="scss"> | ||
.formatter-container { | ||
display: flex; | ||
gap: .8rem; | ||
.el-form { | ||
width: 100%; | ||
display: flex; | ||
gap: .8rem; | ||
.el-form-item { | ||
width: 50%; | ||
} | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<template> | ||
<div class="code-container"> | ||
<el-switch | ||
:model-value="advancedMode" | ||
size="large" | ||
inactive-text="简易模式" | ||
active-text="高级模式" | ||
@change="toggleMode" | ||
/> | ||
<div | ||
v-if="!advancedMode" | ||
flex | ||
gap-4 | ||
flex-col | ||
w-full | ||
items-center | ||
> | ||
<div | ||
flex | ||
gap-4 | ||
items-center | ||
> | ||
<el-switch | ||
v-model="collapseContent" | ||
size="large" | ||
inactive-text="折叠内容" | ||
/> | ||
<span> | ||
缩进大小 | ||
</span> | ||
<el-input-number | ||
v-model="indentValue" | ||
:min="0" | ||
:max="10" | ||
:step="1" | ||
:step-strictly="true" | ||
/> | ||
</div> | ||
<FormatTransformer | ||
w-full | ||
input-label="你的XML内容" | ||
input-placeholder="在这里粘贴XML内容..." | ||
output-label="格式化后的XML内容" | ||
:input-default="value" | ||
:transformer="transformer" | ||
:input-validation-rules="rules" | ||
@format="formattedValue = $event" | ||
/> | ||
</div> | ||
<full-editor | ||
v-else | ||
:value="value" | ||
@change="updateValue" | ||
/> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import xmlFormat from 'xml-formatter' | ||
import type { Ref } from 'vue' | ||
import FullEditor from './child/fullEditor.vue' | ||
const advancedMode: Ref<boolean> = ref(false) | ||
const value: Ref<string> = ref('<hello><world>foo</world><world>bar</world></hello>') | ||
const formattedValue: Ref<string> = ref('') | ||
const collapseContent: Ref<boolean> = ref(true) | ||
const indentValue: Ref<number> = ref(2) | ||
const rules = [{ | ||
validator: (rule: any, val: string, callback: any) => { | ||
if (!isValidXML(val)) { | ||
callback(new Error('请输入正确的XML内容')) | ||
} else { | ||
callback() | ||
} | ||
}, | ||
trigger: 'change' | ||
}] | ||
function toggleMode () { | ||
value.value = formattedValue.value || value.value | ||
advancedMode.value = !advancedMode.value | ||
} | ||
function updateValue (val: string) { | ||
formattedValue.value = val | ||
} | ||
function transformer (value: string) { | ||
try { | ||
return xmlFormat(value.trim(), { | ||
collapseContent: collapseContent.value, | ||
indentation: ' '.repeat(indentValue.value), | ||
lineSeparator: '\n' | ||
}) | ||
} catch (e) { | ||
return '' | ||
} | ||
} | ||
function isValidXML (val: string) { | ||
const tmp = val.trim() | ||
if (!tmp) { | ||
return true | ||
} | ||
try { | ||
xmlFormat(tmp) | ||
return true | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
</script> | ||
|
||
<style scoped lang="scss"> | ||
.code-container { | ||
height: 100%; | ||
width: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: flex-start; | ||
gap: .8rem; | ||
& > :last-child { | ||
flex: 1; | ||
width: 100%; | ||
} | ||
} | ||
</style> |