Skip to content

Commit 416938d

Browse files
authored
Merge pull request #2 from sparrow-js/part-edit
Part edit
2 parents c791c0a + acd61c6 commit 416938d

File tree

172 files changed

+16411
-6511
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+16411
-6511
lines changed

components/App.tsx

Lines changed: 247 additions & 255 deletions
Large diffs are not rendered by default.

components/compiler/index.ts

Lines changed: 452 additions & 0 deletions
Large diffs are not rendered by default.

components/compiler/uid-utils.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
export const MOCK_NEXT_GENERATED_UIDS: { current: string[] } = { current: [] };
2+
export const MOCK_NEXT_GENERATED_UIDS_IDX = { current: 0 };
3+
4+
export function generateMockNextGeneratedUID(): string | null {
5+
if (
6+
MOCK_NEXT_GENERATED_UIDS.current.length > 0 &&
7+
MOCK_NEXT_GENERATED_UIDS_IDX.current < MOCK_NEXT_GENERATED_UIDS.current.length
8+
) {
9+
const nextID = MOCK_NEXT_GENERATED_UIDS.current[MOCK_NEXT_GENERATED_UIDS_IDX.current];
10+
MOCK_NEXT_GENERATED_UIDS_IDX.current += 1;
11+
return nextID;
12+
} else {
13+
return null;
14+
}
15+
}
16+
17+
export const atoz = [
18+
'a',
19+
'b',
20+
'c',
21+
'd',
22+
'e',
23+
'f',
24+
'g',
25+
'h',
26+
'i',
27+
'j',
28+
'k',
29+
'l',
30+
'm',
31+
'n',
32+
'o',
33+
'p',
34+
'q',
35+
'r',
36+
's',
37+
't',
38+
'u',
39+
'v',
40+
'w',
41+
'x',
42+
'y',
43+
'z',
44+
];
45+
46+
export function generateConsistentUID(
47+
possibleStartingValue: string,
48+
...existingIDSets: Array<Set<string>>
49+
): string {
50+
function alreadyExistingID(idToCheck: string): boolean {
51+
return existingIDSets.some((s) => s.has(idToCheck));
52+
}
53+
const mockUID = generateMockNextGeneratedUID();
54+
if (mockUID == null) {
55+
if (possibleStartingValue.length >= 3) {
56+
const maxSteps = Math.floor(possibleStartingValue.length / 3);
57+
for (let step = 0; step < maxSteps; step++) {
58+
const possibleUID = possibleStartingValue.substring(step * 3, (step + 1) * 3);
59+
60+
if (!alreadyExistingID(possibleUID)) {
61+
return possibleUID;
62+
}
63+
}
64+
}
65+
66+
for (let firstChar of atoz) {
67+
for (let secondChar of atoz) {
68+
for (let thirdChar of atoz) {
69+
const possibleUID = `${firstChar}${secondChar}${thirdChar}`;
70+
71+
if (!alreadyExistingID(possibleUID)) {
72+
return possibleUID;
73+
}
74+
}
75+
}
76+
}
77+
throw new Error(`Unable to generate a UID from '${possibleStartingValue}'.`);
78+
} else {
79+
return mockUID;
80+
}
81+
}

components/components/CodePreview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function CodePreview({ code }: Props) {
1616
return (
1717
<div
1818
ref={scrollRef}
19-
className="w-full px-2 bg-black text-green-400 whitespace-nowrap flex
20-
overflow-x-auto font-mono text-[10px] my-4"
19+
className="w-full px-2 py-1 bg-black text-green-400 whitespace-nowrap flex
20+
overflow-x-auto font-mono text-[10px] my-2"
2121
>
2222
{code}
2323
</div>

components/components/CodeTab.tsx

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { Settings } from "../types";
44
import copy from "copy-to-clipboard";
55
import { useCallback } from "react";
66
import toast from "react-hot-toast";
7-
import Editor from '@monaco-editor/react';
7+
import * as monaco from 'monaco-editor';
8+
import Editor, { loader } from '@monaco-editor/react';
9+
10+
loader.config({ monaco });
811

912
interface Props {
1013
code: string;
@@ -13,73 +16,12 @@ interface Props {
1316
}
1417

1518
function CodeTab({ code, setCode, settings }: Props) {
16-
console.log(settings);
17-
const copyCode = useCallback(() => {
18-
copy(code);
19-
toast.success("Copied to clipboard");
20-
}, [code]);
21-
22-
const doOpenInCodepenio = useCallback(async () => {
23-
// TODO: Update CSS and JS external links depending on the framework being used
24-
const data = {
25-
html: code,
26-
editors: "100", // 1: Open HTML, 0: Close CSS, 0: Close JS
27-
layout: "left",
28-
css_external:
29-
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" +
30-
(code.includes("<ion-")
31-
? ",https://cdn.jsdelivr.net/npm/@ionic/core/css/ionic.bundle.css"
32-
: ""),
33-
js_external:
34-
"https://cdn.tailwindcss.com " +
35-
(code.includes("<ion-")
36-
? ",https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.esm.js,https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.js"
37-
: ""),
38-
};
39-
40-
// Create a hidden form and submit it to open the code in CodePen
41-
// Can't use fetch API directly because we want to open the URL in a new tab
42-
const input = document.createElement("input");
43-
input.setAttribute("type", "hidden");
44-
input.setAttribute("name", "data");
45-
input.setAttribute("value", JSON.stringify(data));
46-
47-
const form = document.createElement("form");
48-
form.setAttribute("method", "POST");
49-
form.setAttribute("action", "https://codepen.io/pen/define");
50-
form.setAttribute("target", "_blank");
51-
form.appendChild(input);
52-
53-
document.body.appendChild(form);
54-
form.submit();
55-
}, [code]);
56-
19+
5720
return (
58-
<div className="relative">
59-
<div className="flex justify-start items-center px-4 mb-2">
60-
<span
61-
title="Copy Code"
62-
className="bg-black text-white flex items-center justify-center hover:text-black hover:bg-gray-100 cursor-pointer rounded-lg text-sm p-2.5"
63-
onClick={copyCode}
64-
>
65-
Copy Code <FaCopy className="ml-2" />
66-
</span>
67-
<Button
68-
onClick={doOpenInCodepenio}
69-
className="bg-gray-100 text-black ml-2 py-2 px-4 border border-black rounded-md hover:bg-gray-400 focus:outline-none"
70-
>
71-
Open in{" "}
72-
<img
73-
src="https://assets.codepen.io/t-1/codepen-logo.svg"
74-
alt="codepen.io"
75-
className="h-4 ml-1"
76-
/>
77-
</Button>
78-
</div>
21+
<div className="relative h-full flex flex-col">
7922
<Editor
80-
height="70vh"
8123
defaultLanguage="html"
82-
defaultValue="// some comment"
24+
defaultValue=""
8325
value={code}
8426
onChange={(value) => {
8527
setCode(value as string)

0 commit comments

Comments
 (0)