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