Skip to content

Commit

Permalink
chore(playground): migrate formatting to editor component
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMcA committed Dec 11, 2024
1 parent c94cafa commit be7f2a2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
42 changes: 42 additions & 0 deletions client/src/playground/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,48 @@ export class PlayEditor extends LitElement {
];
}

async format() {
const prettier = await import("prettier/standalone");
const config = (() => {
switch (this.language) {
case "javascript":
return {
parser: "babel",
plugins: [
import("prettier/plugins/babel"),
// XXX Using .mjs until https://github.com/prettier/prettier/pull/15018 is deployed
import("prettier/plugins/estree.mjs"),
],
};
case "html":
return {
parser: "html",
plugins: [
import("prettier/plugins/html"),
import("prettier/plugins/postcss"),
import("prettier/plugins/babel"),
// XXX Using .mjs until https://github.com/prettier/prettier/pull/15018 is deployed
import("prettier/plugins/estree.mjs"),
],
};
case "css":
return {
parser: "css",
plugins: [import("prettier/plugins/postcss")],
};
default:
return undefined;
}
})();
if (config) {
const plugins = await Promise.all(config.plugins);
this.value = await prettier.format(this.value, {
parser: config.parser,
plugins,
});
}
}

/** @param {PropertyValues} changedProperties */
willUpdate(changedProperties) {
if (
Expand Down
35 changes: 6 additions & 29 deletions client/src/playground/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { useSearchParams } from "react-router-dom";
import useSWRImmutable from "swr/immutable";
import prettier from "prettier/standalone";
import prettierPluginBabel from "prettier/plugins/babel";
import prettierPluginCSS from "prettier/plugins/postcss";
// XXX Using .mjs until https://github.com/prettier/prettier/pull/15018 is deployed
import prettierPluginESTree from "prettier/plugins/estree.mjs";
import prettierPluginHTML from "prettier/plugins/html";

import { Button } from "../ui/atoms/button";
import { ReactPlayEditor, PlayEditor } from "./editor";
import { SidePlacement } from "../ui/organisms/placement";
Expand Down Expand Up @@ -296,33 +289,17 @@ export default function Playground() {
};

const format = async () => {
const { html, css, js } = getEditorContent();

try {
const formatted = {
html: await prettier.format(html, {
parser: "html",
plugins: [
prettierPluginHTML,
prettierPluginCSS,
prettierPluginBabel,
prettierPluginESTree,
],
}),
css: await prettier.format(css, {
parser: "css",
plugins: [prettierPluginCSS],
}),
js: await prettier.format(js, {
parser: "babel",
plugins: [prettierPluginBabel, prettierPluginESTree],
}),
};
setEditorContent(formatted);
await Promise.all([
htmlRef.current?.format(),
cssRef.current?.format(),
jsRef.current?.format(),
]);
} catch (e) {
console.error(e);
}
};

const share = useCallback(async () => {
const { url, id } = await save(getEditorContent());
setSearchParams([["id", id]], { replace: true });
Expand Down

0 comments on commit be7f2a2

Please sign in to comment.