-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.html
More file actions
255 lines (214 loc) · 8.26 KB
/
code.html
File metadata and controls
255 lines (214 loc) · 8.26 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, interactive-widget=resizes-content">
<meta name="description" content="Simple Code Editor" />
<link rel="manifest" href="manifest.json">
<title>Code Viewer</title>
<style>
html, body {
height: 100%;
margin: 0;
overflow: hidden;
}
body {
display: flex;
flex-direction: row;
font-family: Arial, sans-serif;
background-color: black;
color: white;
height: 100%;
position: relative;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-touch-callout: none;
-webkit-tap-highlight-color: transparent;
}
#editorContainer { flex: 38; display: flex; position: relative; background-color: black; min-width: 0; }
#preview { flex: 62; border: none; background-color: #333333; overflow: auto; min-width: 0; }
#controlPanel {
flex: 0 0 auto;
background-color: black;
border-left: 1px solid #222;
}
#controlPanel > div {
display: flex;
flex-direction: column;
gap: 12px;
align-items: center;
justify-content: flex-start;
padding: 15px 10px;
box-sizing: border-box;
height: 100%;
}
#lineNumbering {
width: 40px; padding: 10px 5px; text-align: right; font-family: monospace;
font-size: 14px; line-height: 1.2; color: #666; background-color: #111;
user-select: none; white-space: pre; overflow: hidden; box-sizing: border-box; flex-shrink: 0;
}
#editor {
flex: 1; padding: 10px; border: none; resize: none; font-family: monospace;
font-size: 14px; line-height: 1.2; background-color: black; color: white;
outline: none; caret-color: white; position: relative; box-sizing: border-box;
overflow: auto; white-space: pre; min-width: 0;
}
#searchField {
padding: 5px; font-size: 14px; border: 1px solid #555; background-color: #333333;
color: white; height: 35px; box-sizing: border-box; border-radius: 5px; width: 100px }
#prevMatchButton, #nextMatchButton {
width: 40px;
height: 40px;
background-color: #333333;
color: white;
border: none;
cursor: pointer;
font-size: 18px;
border-radius: 5px;
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
}
#fileFormat { font-size: 14px; padding: 5px; width: 100px; height: 35px; }
#saveButton {
width: 80px; height: 40px; background-color: #007bff; color: white;
border: none; cursor: pointer; font-size: 14px; border-radius: 5px;
}
@media (orientation: portrait) {
body { flex-direction: column; }
#editorContainer { flex: 40; min-height: 0; }
#preview { flex: 50; min-height: 0; }
#controlPanel { border-left: none; border-top: 1px solid #222; }
#controlPanel > div {
flex-direction: row;
justify-content: space-evenly;
padding: 10px;
height: auto;
width: 100%;
flex-wrap: wrap;
}
#searchField { width: 100px; flex-grow: 1; }
}
</style>
</head>
<body>
<script src="pwa-init.js"></script>
<div id="editorContainer">
<div id="lineNumbering">1</div>
<textarea id="editor" placeholder="Type to see your code here..."></textarea>
</div>
<iframe id="preview" title="Preview"></iframe>
<div id="controlPanel">
<div>
<input type="text" id="searchField" placeholder="Search">
<button id="prevMatchButton" title="Previous Match">▲</button>
<button id="nextMatchButton" title="Next Match">▼</button>
<select id="fileFormat">
<option value="html">HTML</option>
<option value="json">JSON</option>
<option value="txt">TXT</option>
<option value="js">JS</option>
<option value="css">CSS</option>
</select>
<button id="saveButton">Save</button>
<a href="index.html" style="display: flex; align-items: center;">
<img src="logo.png" alt="Feedback" style="height: 40px; opacity: 0.9; width: auto;">
</a>
</div>
</div>
<script>
const editor = document.getElementById("editor");
const preview = document.getElementById("preview");
const saveButton = document.getElementById("saveButton");
const fileFormat = document.getElementById("fileFormat");
const searchField = document.getElementById("searchField");
const prevMatchButton = document.getElementById("prevMatchButton");
const nextMatchButton = document.getElementById("nextMatchButton");
const lineNumbering = document.getElementById("lineNumbering");
let searchMatches = [];
let currentMatchIndex = -1;
let currentSearchTerm = "";
const mimeTypes = {
html: "text/html", json: "application/json", txt: "text/plain",
js: "application/javascript", css: "text/css",
};
function updateLineNumbers() {
const lines = editor.value.split('\n');
lineNumbering.textContent = Array.from({ length: lines.length }, (_, i) => i + 1).join('\n');
}
editor.addEventListener('input', () => {
preview.srcdoc = editor.value;
updateLineNumbers();
});
editor.addEventListener('scroll', () => { lineNumbering.scrollTop = editor.scrollTop; });
updateLineNumbers();
saveButton.addEventListener("click", () => {
const format = fileFormat.value;
const blob = new Blob([editor.value], { type: mimeTypes[format] || "text/plain" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = `code.${format}`;
a.click();
});
function highlightMatch(index) {
if (index === -1 || searchMatches.length === 0) return;
const start = searchMatches[index];
const end = start + currentSearchTerm.length;
editor.focus();
editor.setSelectionRange(start, end);
const linesBefore = editor.value.substring(0, start).split('\n').length - 1;
editor.scrollTop = Math.max(0, (linesBefore - 2) * 16.8);
}
function startSearch(searchText) {
const trimmed = searchText.trim();
if (trimmed === "") { searchMatches = []; currentSearchTerm = ""; return; }
searchMatches = [];
currentSearchTerm = trimmed;
const content = editor.value.toLowerCase();
const term = trimmed.toLowerCase();
let index = content.indexOf(term, 0);
while (index !== -1) {
searchMatches.push(index);
index = content.indexOf(term, index + term.length);
}
if (searchMatches.length > 0) {
currentMatchIndex = 0;
highlightMatch(currentMatchIndex);
}
}
searchField.addEventListener("keydown", (e) => { if (e.key === "Enter") startSearch(searchField.value); });
nextMatchButton.addEventListener("click", () => {
if (searchField.value !== currentSearchTerm) startSearch(searchField.value);
else if (searchMatches.length) { currentMatchIndex = (currentMatchIndex + 1) % searchMatches.length; highlightMatch(currentMatchIndex); }
});
prevMatchButton.addEventListener("click", () => {
if (searchField.value !== currentSearchTerm) startSearch(searchField.value);
else if (searchMatches.length) { currentMatchIndex = (currentMatchIndex - 1 + searchMatches.length) % searchMatches.length; highlightMatch(currentMatchIndex); }
});
document.addEventListener('selectionchange', () => {
if (document.activeElement === editor) {
const rect = editor.getBoundingClientRect();
const cursor = window.getSelection();
}
});
editor.addEventListener('touchmove', (e) => {
const touchY = e.touches[0].clientY;
const rect = editor.getBoundingClientRect();
if (touchY > rect.top + 20 && touchY < rect.bottom - 20) {
e.stopPropagation();
}
}, { passive: false });
editor.addEventListener('pointerdown', () => {
window.addEventListener('wheel', stopGlobalScroll, { passive: false });
window.addEventListener('touchmove', stopGlobalScroll, { passive: false });
});
window.addEventListener('pointerup', () => {
window.removeEventListener('wheel', stopGlobalScroll);
window.removeEventListener('touchmove', stopGlobalScroll);
});
</script>
</body>
</html>