Files
test-repo/website/playground/PrettierFormat.js
T
d9bd8abfeb Add playground for current version (#18830)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: fisker <lionkay@gmail.com>
2026-02-05 22:29:59 +08:00

62 lines
1.4 KiB
JavaScript

import { onMounted, reactive, toRaw, watch } from "vue";
import { settings } from "./composables/playground-settings.js";
import { worker } from "./composables/prettier-worker.js";
import { version } from "./composables/use-version.js";
function setup(props, { slots }) {
const state = reactive({ formatted: "", debug: {} });
const componentDidMount = () => {
format();
};
const format = async () => {
const result = await worker.format(
props.code,
toRaw(props.options),
toRaw(settings),
version.value,
);
Object.assign(state, result);
};
const render = () => slots.default(state);
onMounted(componentDidMount);
watch(() => props.code, format);
watch(() => props.options, format, { deep: true });
watch(() => version.value, format);
// Should not trigger format when these changes
const ignoredKeys = new Set(["showSidebar", "showInput"]);
for (const key of Object.keys(settings).filter(
(key) => !ignoredKeys.has(key),
)) {
watch(
() => settings[key],
(newValue, oldValue) => {
// Always triggers format
if (key === "rethrowEmbedErrors" && newValue !== oldValue) {
return format();
}
// Only if set to true
if (newValue) {
return format();
}
},
);
}
return render;
}
export default {
name: "PrettierFormat",
props: {
code: String,
options: Object,
},
setup,
};