Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2025-12-10 23:48:10 +08:00
import { onMounted, reactive, toRaw, watch } from "vue";
2026-01-27 18:52:05 +08:00
import { settings } from "./composables/playground-settings.js";
2026-01-05 17:51:47 +08:00
import { worker } from "./composables/prettier-worker.js";
2026-02-05 22:29:59 +08:00
import { version } from "./composables/use-version.js";
2018-04-11 22:28:50 -03:00
2025-12-10 23:48:10 +08:00
function setup(props, { slots }) {
const state = reactive({ formatted: "", debug: {} });
2018-04-11 22:28:50 -03:00
2025-12-10 23:48:10 +08:00
const componentDidMount = () => {
format();
};
2018-04-11 22:28:50 -03:00
2025-12-10 23:48:10 +08:00
const format = async () => {
const result = await worker.format(
props.code,
toRaw(props.options),
toRaw(settings),
2026-02-05 22:29:59 +08:00
version.value,
);
2022-08-01 11:27:34 +08:00
2025-12-10 23:48:10 +08:00
Object.assign(state, result);
};
2018-04-11 22:28:50 -03:00
2025-12-10 23:48:10 +08:00
const render = () => slots.default(state);
onMounted(componentDidMount);
2026-01-27 22:32:40 +08:00
watch(() => props.code, format);
watch(() => props.options, format, { deep: true });
2026-02-05 22:29:59 +08:00
watch(() => version.value, format);
2026-01-27 22:32:40 +08:00
// Should not trigger format when these changes
const ignoredKeys = new Set(["showSidebar", "showInput"]);
for (const key of Object.keys(settings)) {
if (ignoredKeys.has(key)) {
continue;
}
2026-01-27 22:32:40 +08:00
watch(
() => settings[key],
(newValue, oldValue) => {
// Always triggers format
if (key === "rethrowEmbedErrors" && newValue !== oldValue) {
return format();
}
// Only if set to true
if (newValue) {
return format();
}
},
);
}
2025-12-10 23:48:10 +08:00
return render;
2018-04-11 22:28:50 -03:00
}
2025-12-10 23:48:10 +08:00
export default {
name: "PrettierFormat",
props: {
code: String,
options: Object,
},
setup,
};