blob: b15865aba5624df47dac18b7e3b9fbc814b0328e [file] [log] [blame]
bankseand52d39d2025-07-20 14:57:38 -07001/* eslint-disable @typescript-eslint/no-explicit-any */
2import { DemoModule } from "./demo-framework/types";
3import { demoUtils } from "./demo-fixtures/index";
4
5const demo: DemoModule = {
6 title: "Sketch Monaco Viewer Demo",
7 description:
8 "Monaco editor with code comparison functionality for different languages",
9 imports: ["../sketch-monaco-view.ts"],
10
11 customStyles: `
12 button {
13 padding: 8px 12px;
14 background-color: #4285f4;
15 color: white;
16 border: none;
17 border-radius: 4px;
18 cursor: pointer;
19 margin-right: 8px;
20 }
21
22 button:hover {
23 background-color: #3367d6;
24 }
25
26 sketch-monaco-view {
27 margin-top: 20px;
28 height: 500px;
29 }
30 `,
31
32 setup: async (container: HTMLElement) => {
33 const section = demoUtils.createDemoSection(
34 "Monaco Code Viewer",
35 "Demonstrates the Monaco editor component with side-by-side code comparison for different programming languages.",
36 );
37
38 // Create control panel
39 const controlPanel = document.createElement("div");
40 controlPanel.style.marginBottom = "2rem";
41 controlPanel.style.padding = "1rem";
42 controlPanel.style.backgroundColor = "#f0f0f0";
43 controlPanel.style.borderRadius = "4px";
44
45 const buttonsContainer = document.createElement("div");
46 buttonsContainer.style.marginTop = "1rem";
47
48 // Create example buttons
49 const jsButton = demoUtils.createButton("Example 1: JavaScript", () => {
50 diffEditor.setOriginalCode(
51 `function calculateTotal(items) {
52 return items
53 .map(item => item.price * item.quantity)
54 .reduce((a, b) => a + b, 0);
55}`,
56 "original.js",
57 );
58
59 diffEditor.setModifiedCode(
60 `function calculateTotal(items) {
61 // Apply discount if available
62 return items
63 .map(item => {
64 const price = item.discount ?
65 item.price * (1 - item.discount) :
66 item.price;
67 return price * item.quantity;
68 })
69 .reduce((a, b) => a + b, 0);
70}`,
71 "modified.js",
72 );
73 });
74
75 const htmlButton = demoUtils.createButton("Example 2: HTML", () => {
76 diffEditor.setOriginalCode(
77 `<!DOCTYPE html>
78<html>
79<head>
80 <title>Demo Page</title>
81</head>
82<body>
83 <h1>Hello World</h1>
84 <p>This is a paragraph.</p>
85</body>
86</html>`,
87 "original.html",
88 );
89
90 diffEditor.setModifiedCode(
91 `<!DOCTYPE html>
92<html>
93<head>
94 <title>Demo Page</title>
95 <meta name="viewport" content="width=device-width, initial-scale=1.0">
96 <link rel="stylesheet" href="styles.css">
97</head>
98<body>
99 <header>
100 <h1>Hello World</h1>
101 </header>
102 <main>
103 <p>This is a paragraph with some <strong>bold</strong> text.</p>
104 </main>
105 <footer>
106 <p>&copy; 2025</p>
107 </footer>
108</body>
109</html>`,
110 "modified.html",
111 );
112 });
113
114 const goButton = demoUtils.createButton("Example 3: Go", () => {
115 diffEditor.setOriginalCode(
116 `package main
117
118import "fmt"
119
120func main() {
121 fmt.Println("Hello, world!")
122}`,
123 "original.go",
124 );
125
126 diffEditor.setModifiedCode(
127 `package main
128
129import (
130 "fmt"
131 "time"
132)
133
134func main() {
135 fmt.Println("Hello, world!")
136 fmt.Printf("The time is %s\n", time.Now().Format(time.RFC3339))
137}`,
138 "modified.go",
139 );
140 });
141
142 buttonsContainer.appendChild(jsButton);
143 buttonsContainer.appendChild(htmlButton);
144 buttonsContainer.appendChild(goButton);
145
146 controlPanel.innerHTML = `<p>Select an example to see code comparison in different languages:</p>`;
147 controlPanel.appendChild(buttonsContainer);
148
149 // Create the Monaco view component
150 const diffEditor = document.createElement("sketch-monaco-view") as any;
151 diffEditor.id = "diffEditor";
152
153 // Set initial example
154 diffEditor.originalCode = `function hello() {
155 console.log("Hello World");
156 return true;
157}`;
158
159 diffEditor.modifiedCode = `function hello() {
160 // Add a comment
161 console.log("Hello Updated World");
162 return true;
163}`;
164
165 section.appendChild(controlPanel);
166 section.appendChild(diffEditor);
167 container.appendChild(section);
168 },
169};
170
171export default demo;