Files
2026-09-21 15:32:12 +08:00

91 lines
2.2 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="demo">
<section class="block">
<h4 v-text="'toFormData:嵌套对象扁平化'" />
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr v-for="row in formRows" :key="row.key">
<td><code v-text="row.key" /></td>
<td v-text="row.value" />
</tr>
</tbody>
</table>
</section>
<section class="block">
<h4 v-text="'base64ToFile:还原为 File'" />
<p class="line" v-text="fileInfo" />
</section>
<section class="block">
<h4 v-text="'isFile / getAttribute'" />
<p class="line" v-text="fileCheck" />
<p class="line" v-text="attributeCheck" />
</section>
</div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { WebToolkit } from "timi-web";
/** toFormData 会把嵌套对象展开为点号连接的键。 */
const source = {
name: "timi",
version: 1,
author: { first: "Ti", last: "mi" },
tags: ["a", "b"]
};
const formRows = [...WebToolkit.toFormData(source).entries()].map(([key, item]) => ({
key,
value: typeof item === "string" ? item : `${item.constructor.name}(${(item as Blob).size} 字节)`
}));
const fileInfo = ref("");
const fileCheck = ref("");
const attributeCheck = ref("");
onMounted(() => {
// 1×1 的透明 PNG
const dataUrl = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";
const file = WebToolkit.base64ToFile(dataUrl, "pixel.png");
fileInfo.value = `name="${file.name}" size=${file.size} type="${file.type}"`;
fileCheck.value = `isFile(file) = ${WebToolkit.isFile(file)}|isFile({}) = ${WebToolkit.isFile({})}`;
const element = document.createElement("div");
element.setAttribute("data-role", "demo");
attributeCheck.value = `getAttribute(el, "data-role") = ${WebToolkit.getAttribute(element, "data-role")}|getAttribute(el, "missing") = ${WebToolkit.getAttribute(element, "missing")}`;
});
</script>
<style lang="less" scoped>
.demo {
.block {
margin-bottom: 1.5rem;
&:last-child {
margin-bottom: 0;
}
}
h4 {
margin: 0 0 .5rem;
font-size: .8125rem;
}
.line {
margin: 0 0 .25rem;
font-size: .75rem;
font-family: monospace;
color: var(--vp-c-text-2);
}
}
</style>