Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: simple setup vite for playground #52

Merged
merged 38 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f920e96
build: simple setup vite for playground
igorwessel Apr 11, 2024
48245fd
build: add preview script to see production build
igorwessel Apr 11, 2024
5b5c3a4
chore: remove parcel deps
igorwessel Apr 11, 2024
900014c
build: use vite to bundle react, excalidraw
igorwessel Apr 11, 2024
e8037fc
refactor: typo fix for "definition" in playground
igorwessel Apr 12, 2024
b3505c0
build: basic support hmr for test cases
igorwessel Apr 12, 2024
7b71547
build: maintain same output build format which parcel uses
igorwessel Apr 12, 2024
88c08f0
fix: hmr testcase file update not get new definition for button
igorwessel Apr 12, 2024
ece4a0d
chore: some minor adjust in tsconfig
igorwessel Apr 13, 2024
8f149ae
refactor: use glob pattern for warm-up vite
igorwessel Apr 15, 2024
390f1d0
feat: transform playground in react
igorwessel Apr 16, 2024
266a594
build: add typecheck for playground
igorwessel Apr 16, 2024
9d3d496
build: fix typecheck for playground
igorwessel Apr 16, 2024
7cbec3a
refactor: use arrow function for testcases component
igorwessel Apr 23, 2024
8155f6d
refactor: use arrow function for mermaid component
igorwessel Apr 23, 2024
9ad7db4
refactor: use arrow function for excalidraw component
igorwessel Apr 23, 2024
799c472
refactor: use arrow function for customtest component
igorwessel Apr 23, 2024
ef2a7fa
refactor: use arrow function for app component
igorwessel Apr 23, 2024
84fa718
refactor: remove context, lifted state up for app
igorwessel Apr 23, 2024
006eb29
improve and reuse types
ad1992 Apr 25, 2024
3511403
rename onChangeDefination to onChange
ad1992 Apr 25, 2024
c87ee04
cleanup Testcases.tsx
ad1992 Apr 25, 2024
694fe96
rename syntax to definition to align with testcase
ad1992 Apr 25, 2024
29710e2
fix
ad1992 Apr 25, 2024
96f6505
refactor: rename mermaid to mermaid digram
igorwessel Apr 25, 2024
192f306
refactor: remove responsability to get mermaid data in excalidraw
igorwessel Apr 25, 2024
2475856
fix: minor fixes
igorwessel Apr 25, 2024
d3716a2
refactor: rename activeTestcase to activeTestcaseIndex
igorwessel Apr 29, 2024
e540933
refactor: improve readability for testcase error prop
igorwessel Apr 29, 2024
c0752ad
refactor: separate a single testcase component in own file
igorwessel Apr 29, 2024
2e09454
refactor: move error responsability to mermaid diagram
igorwessel Apr 29, 2024
378eaf9
refactor: better way to handle active testcase file
igorwessel Apr 29, 2024
51f080c
fix: force re-render when click in button
igorwessel Apr 30, 2024
b9cfd1c
feat: show error when occurs in parseMermaid
igorwessel Apr 30, 2024
e6ee857
rename the onchange handler to handleOnChange and use explicit types
ad1992 May 1, 2024
55ac328
Refactor Single Testcase to render a single testcase
ad1992 May 1, 2024
1fab572
add missing type to class testcase
ad1992 May 1, 2024
cf65699
fix typo for flowchart testcase
igorwessel May 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
"description": "Mermaid to Excalidraw Diagrams",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"//": "Set targets.main = false, this instructs Parcel to ignore the main parameters.",
"targets": {
"main": false,
"types": false
},
"files": [
"dist/*"
],
"type": "module",
"scripts": {
"build": "rimraf ./dist && cross-env tsc -b src",
"start": "rimraf .parcel-cache && parcel playground/index.html --open --dist-dir ./public",
"build:playground": "rimraf ./public && parcel build playground/index.html --no-scope-hoist --dist-dir ./public --public-url /",
"test:code": "eslint --max-warnings=0 --ext .js,.ts,.tsx ."
"build": "rimraf -rf ./dist && cross-env tsc -b src",
"test:code": "eslint --max-warnings=0 --ext .js,.ts,.tsx .",
"start": "vite playground",
"build:playground": "tsc --noEmit --project ./playground/tsconfig.json && vite build playground",
"preview": "npm run build:playground && vite preview --outDir ./public"
},
"dependencies": {
"@excalidraw/markdown-to-text": "0.1.2",
Expand All @@ -28,21 +24,24 @@
"@babel/core": "7.12.0",
"@excalidraw/eslint-config": "1.0.3",
"@excalidraw/excalidraw": "0.17.1-7381-cdf6d3e",
"@parcel/transformer-sass": "2.9.1",
"@types/mermaid": "9.2.0",
"@types/react": "18.2.14",
"@types/react-dom": "18.2.4",
"@typescript-eslint/eslint-plugin": "5.59.9",
"@typescript-eslint/parser": "5.59.9",
"cross-env": "7.0.3",
"@vitejs/plugin-react-swc": "3.6.0",
"eslint": "8.42.0",
"eslint-config-prettier": "8.8.0",
"eslint-plugin-prettier": "4.2.1",
"parcel": "2.9.1",
"prettier": "2.8.8",
"process": "0.11.10",
"react": "18.2.0",
"react-dom": "18.2.0",
"sass": "1.74.1",
"typescript": "5.2.2",
"rimraf": "5.0.5"
"rimraf": "5.0.5",
"vite": "5.2.8"
},
"resolutions": {
"@babel/preset-env": "7.13.8"
Expand Down
71 changes: 71 additions & 0 deletions playground/CustomTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { MermaidDiagram } from "./MermaidDiagram.tsx";
import type { ActiveTestCaseIndex, MermaidData } from "./index.tsx";

interface CustomTestProps {
onChange: (
definition: MermaidData["definition"],
activeTestCaseIndex: ActiveTestCaseIndex
) => void;
mermaidData: MermaidData;
activeTestCaseIndex: ActiveTestCaseIndex;
}

const CustomTest = ({
onChange,
mermaidData,
activeTestCaseIndex,
}: CustomTestProps) => {
const isActive = activeTestCaseIndex === "custom";
return (
<>
<form
onSubmit={(e) => {
e.preventDefault();

const formData = new FormData(e.target as HTMLFormElement);

onChange(formData.get("mermaid-input")?.toString() || "", "custom");
}}
>
<textarea
id="mermaid-input"
rows={10}
cols={50}
name="mermaid-input"
onChange={(e) => {
if (!isActive) {
return;
}

onChange(e.target.value, "custom");
}}
style={{ marginTop: "1rem" }}
placeholder="Input Mermaid Syntax"
/>
<br />
<button type="submit" id="render-excalidraw-btn">
{"Render to Excalidraw"}
</button>
</form>

{isActive && (
<>
<MermaidDiagram
definition={mermaidData.definition}
id="custom-diagram"
/>

<details id="parsed-data-details">
<summary>{"Parsed data from parseMermaid"}</summary>
<pre id="custom-parsed-data">
{JSON.stringify(mermaidData.output, null, 2)}
</pre>
{mermaidData.error && <div id="error">{mermaidData.error}</div>}
</details>
</>
)}
</>
);
};

export default CustomTest;
59 changes: 0 additions & 59 deletions playground/ExcalidrawWrapper.ts

This file was deleted.

64 changes: 64 additions & 0 deletions playground/ExcalidrawWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { useEffect, useState } from "react";
import {
Excalidraw,
convertToExcalidrawElements,
} from "@excalidraw/excalidraw";
import type { ExcalidrawImperativeAPI } from "@excalidraw/excalidraw/types/types.js";
import { graphToExcalidraw } from "../src/graphToExcalidraw";
import { DEFAULT_FONT_SIZE } from "../src/constants";
import type { MermaidData } from "./";

interface ExcalidrawWrapperProps {
mermaidDefinition: MermaidData["definition"];
mermaidOutput: MermaidData["output"];
}

const ExcalidrawWrapper = ({
mermaidDefinition,
mermaidOutput,
}: ExcalidrawWrapperProps) => {
const [excalidrawAPI, setExcalidrawAPI] =
useState<ExcalidrawImperativeAPI | null>(null);

useEffect(() => {
if (!excalidrawAPI) {
return;
}

if (mermaidDefinition === "" || mermaidOutput === null) {
excalidrawAPI.resetScene();
return;
}

const { elements, files } = graphToExcalidraw(mermaidOutput, {
fontSize: DEFAULT_FONT_SIZE,
});

excalidrawAPI.updateScene({
elements: convertToExcalidrawElements(elements),
});
excalidrawAPI.scrollToContent(excalidrawAPI.getSceneElements(), {
fitToContent: true,
});

if (files) {
excalidrawAPI.addFiles(Object.values(files));
}
}, [mermaidDefinition, mermaidOutput]);

return (
<div className="excalidraw-wrapper">
<Excalidraw
initialData={{
appState: {
viewBackgroundColor: "#fafafa",
currentItemFontFamily: 1,
},
}}
excalidrawAPI={(api) => setExcalidrawAPI(api)}
/>
</div>
);
};

export default ExcalidrawWrapper;
44 changes: 44 additions & 0 deletions playground/MermaidDiagram.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useState, useTransition, useEffect } from "react";
import mermaid from "mermaid";

interface MermaidProps {
id: string;
definition: string;
}

export const MermaidDiagram = ({ definition, id }: MermaidProps) => {
const [svg, setSvg] = useState("");
const [error, setError] = useState<string | null>(null);
const [, startTransition] = useTransition();

useEffect(() => {
const render = async (id: string, definition: string) => {
try {
setError(null);

const { svg } = await mermaid.render(
`mermaid-diagram-${id}`,
definition
);
startTransition(() => {
setSvg(svg);
});
} catch (err) {
setError(String(err));
}
};

render(id, definition);
}, [definition, id]);

return (
<>
<div
style={{ width: "50%" }}
className="mermaid"
dangerouslySetInnerHTML={{ __html: svg }}
/>
{error && <div id="error">{error}</div>}
</>
);
};
39 changes: 39 additions & 0 deletions playground/SingleTestCase.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { MermaidDiagram } from "./MermaidDiagram";

export interface TestCase {
type: "class" | "flowchart" | "sequence" | "unsupported";
name: string;
definition: string;
}

export interface SingleTestCaseProps {
testcase: TestCase;
onChange: (activeTestcaseIndex: number) => void;
index: number;
activeTestcaseIndex?: number;
}

const SingleTestCase = ({ testcase, onChange, index }: SingleTestCaseProps) => {
const { name, definition, type } = testcase;
return (
<>
<h2 style={{ marginTop: "50px", color: "#f06595" }}>{name}</h2>
<pre>{definition}</pre>
<button
onClick={() => {
onChange(index);
}}
>
{"Render to Excalidraw"}
</button>

<MermaidDiagram
key={definition}
definition={definition}
id={`${type}-${index}`}
/>
</>
);
};

export default SingleTestCase;
Loading
Loading