-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.html
194 lines (189 loc) · 8.78 KB
/
demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no">
<title>CodeCake Demo</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM+Plex+Serif:wght@700&display=swap">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Lato:wght@400;500;700&display=swap">
<link rel="stylesheet" href="./node_modules/lowcss/dist/low.css">
<link rel="stylesheet" href="./codecake.css">
</head>
<body class="m-0 font-lato text-gray-900 leading-normal">
<!-- Main content -->
<div class="main w-full max-w-4xl mx-auto px-6">
<!-- Header section -->
<div class="w-full mt-4 md:mt-12 mb-6">
<div class="flex justify-between items-center w-full">
<div class="text-xl md:text-2xl font-bold font-plex-serif tracking-tight select-none">
<a href="./" class="text-gray-950">CodeCake</a>
</div>
<div class="flex gap-4">
<a href="./demo.html" class="font-medium text-gray-900 hover:text-gray-800 hover:underline">Demo</a>
<a href="https://github.com/jmjuanes/codecake#api" class="font-medium text-gray-900 hover:text-gray-800 hover:underline">API</a>
<a href="https://github.com/jmjuanes/codecake" class="font-medium text-gray-900 hover:text-gray-800 hover:underline">GitHub</a>
</div>
</div>
</div>
<!-- Hero section -->
<div class="w-full pt-6 pb-6">
<div class="text-4xl font-bold font-plex-serif tracking-tight">
<strong>Demo</strong>
</div>
</div>
<div class="w-full">
<!-- Demo options -->
<div class="rounded-md bg-neutral-100 p-4 grid grid-cols-1 md:grid-cols-2 mb-4 gap-4 select-none">
<div class="">
<div class="font-bold mb-1">Language:</div>
<select class="w-full bg-neutral-200 rounded-md" id="select-language">
<option value="javascript">JavaScript</option>
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="markdown">MarkDown</option>
</select>
</div>
<div class="">
<div class="font-bold mb-1">Theme</div>
<select class="w-full bg-neutral-200 rounded-md" id="select-theme">
<option value="dark">Dark (codecake-dark)</option>
<option value="light">Light (codecake-light)</option>
<option value="monoblue">Mono Blue (codecake-monoblue)</option>
<option value="one-dark">One Dark (codecake-one-dark)</option>
<option value="one-light">One Light (codecake-one-light)</option>
</select>
</div>
</div>
<!-- Demo editor-->
<div id="editor" class="shadow-lg rounded-md overflow-hidden flex"></div>
</div>
<!-- Footer section -->
<div class="pb-16 pt-16 text-sm text-gray-700">
Crafted with love by <a href="https://josemi.xyz" class="font-medium text-gray-900 hover:underline">Josemi</a>.
</div>
</div>
<!-- Editor code -->
<script type="module">
import * as CodeCake from "./codecake.js";
const codes = {
javascript: [
`import * as CodeCake from "codecake";`,
``,
`// First get reference to the target element`,
`const target = document.getElementById("editor");`,
``,
`// Initialize codecake editor`,
`const cake = CodeCake.create(target, {`,
` code: "",`,
` className: "codecake-{theme}",`,
` readOnly: false,`,
` lineNumbers: true,`,
` highlight: code => CodeCake.highlight(code, "javascript"),`,
`});`,
``,
`// You can use the API methods to access the code`,
`cake.setCode("Hello world!");`,
``,
`// Register events listeners`,
`cake.onChange(code => console.log("New code: ", code));`,
],
html: [
`<html lang="en">`,
`<head>`,
` <meta charset="utf-8">`,
` <title>CodeCake demo</title>`,
`</head>`,
`<body>`,
` <h1>CodeCake - Tiny code editor</h1>`,
` <div class="description">`,
` <b>CodeCake</b> is a tiny code editor written in less than 200 lines of code!`,
` </div>`,
`</body>`,
`</html>`,
],
css: [
`@font-face {`,
` font-family: Lato;`,
` src: url("./lato.ttf");`,
`}`,
``,
`html {`,
` background-color: #fafafa;`,
` margin: 0;`,
` padding: 0;`,
`}`,
`/* Style body element */`,
`body {`,
` border: 2px solid #000;`,
` color: #333;`,
` font-size: 16px;`,
` font-family: "Lato", sans-serif;`,
` margin: 0;`,
` padding: 0;`,
`}`,
``,
`a:visited,`,
`a:hover {`,
` color: #444;`,
` outline: none;`,
` text-decoration: none; `,
`}`,
``,
`.link[data-active="true"] {`,
` /* color: currentColor; */`,
` color: gray;`,
`}`,
],
markdown: [
`# CodeCake`,
``,
`> A tiny and lightweight code editor for web applications.`,
``,
`## Features`,
``,
`* Less than 200 lines of code. Easy to understand, customize, and integrate into your projects.`,
`* Automatically indents new lines.`,
`* Lightweight syntax highlighting with support for *JavaScript*, *HTML*, *CSS*, and *Markdown*.`,
``,
`## Installation`,
``,
`Install **CodeCake** in your project with just one command:`,
``,
"```bash",
`npm install --save codecake`,
"```",
],
};
const parent = document.getElementById("editor");
const options = {
theme: "dark",
highlight: true,
linenumbers: true,
language: "javascript",
};
const reloadEditor = () => {
// Remove parent childs
while(parent.firstChild) {
parent.removeChild(parent.firstChild);
}
// Initialize cake editor
CodeCake.create(parent, {
code: codes[options.language].join("\n").replace("{theme}", options.theme),
className: `codecake-${options.theme} h-full`,
readOnly: false,
lineNumbers: options.linenumbers,
lineWrap: true,
highlight: options.highlight && (code => CodeCake.highlight(code, options.language)),
});
};
// Register listeners to language and theme
["language", "theme"].forEach(type => {
document.querySelector(`select#select-${type}`).addEventListener("change", event => {
options[type] = event.target.value;
reloadEditor();
});
});
// Init editor
reloadEditor();
</script>
</body>
</html>