-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
269 lines (248 loc) · 9.8 KB
/
demo.js
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// demo.js
// Interactive logic for the Base437 demo page
import { createEncoder, CoreMapping } from './base437.js';
let currentMapping = CoreMapping;
let encoder = createEncoder(currentMapping);
let mappingStack = [currentMapping]; // Stack to store previous mappings
// Add at the top of demo.js, after imports
const mappingFileInput = document.createElement('input');
mappingFileInput.type = 'file';
mappingFileInput.accept = '.json';
mappingFileInput.style.marginBottom = '10px';
mappingFileInput.addEventListener('change', handleMappingImport);
const saveMappingBtn = document.createElement('button');
saveMappingBtn.textContent = 'Save Mapping as JSON';
saveMappingBtn.style.marginLeft = '10px';
saveMappingBtn.addEventListener('click', handleMappingExport);
function handleMappingImport(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = () => {
try {
const importedMapping = JSON.parse(reader.result);
// Validate the imported mapping
const newMapping = Object.assign({}, CoreMapping, importedMapping).validate();
currentMapping = newMapping;
mappingStack.push(newMapping);
encoder = createEncoder(currentMapping);
// Refresh the table
const table = document.getElementById('mappingTable');
table.innerHTML = '';
initMappingTable();
alert('Mapping imported successfully!');
} catch (e) {
alert(`Import Error: ${e.message}`);
}
};
reader.readAsText(file);
}
async function handleMappingExport() {
const mappingJson = JSON.stringify(currentMapping, null, 2);
if ('showSaveFilePicker' in window) {
// Use File System Access API if available
try {
const handle = await window.showSaveFilePicker({
suggestedName: 'base437_mapping.json',
types: [{
description: 'JSON File',
accept: { 'application/json': ['.json'] },
}],
});
const writable = await handle.createWritable();
await writable.write(mappingJson);
await writable.close();
alert('Mapping saved successfully!');
} catch (e) {
if (e.name !== 'AbortError') alert(`Save Error: ${e.message}`);
}
} else {
// Fallback to download link
const blob = new Blob([mappingJson], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'base437_mapping.json';
a.click();
URL.revokeObjectURL(url);
}
}
// Modify initMappingTable to add the file controls
function initMappingTable() {
const tableContainer = document.getElementById('mappingTable').parentElement;
const fileControls = document.createElement('div');
fileControls.appendChild(mappingFileInput);
fileControls.appendChild(saveMappingBtn);
tableContainer.insertBefore(fileControls, document.getElementById('mappingTable'));
const table = document.getElementById('mappingTable');
for (let i = 0; i < 256; i++) {
const cell = document.createElement('div');
cell.className = 'mapping-cell';
const byte = i.toString();
const unicode = currentMapping[byte];
cell.textContent = String.fromCodePoint(parseInt(unicode.slice(2), 16));
cell.title = `${byte} -> ${unicode}`;
cell.addEventListener('click', (e) => {
// Close any open dialog and open a new one for the clicked cell
const dialog = document.getElementById('editMappingDialog');
if (dialog.open) {
dialog.close();
}
editMapping(byte, cell);
});
table.appendChild(cell);
}
}
// Parse Unicode input (supports U+XXXX format only for now)
function parseUnicodeInput(input) {
if (input && input.match(/^U\+[0-9A-F]{4}$/i)) {
return input.toUpperCase();
}
return null;
}
// Edit a mapping using a dialog with enhanced UX
function editMapping(byte, cell) {
const dialog = document.getElementById('editMappingDialog');
const byteInfo = document.getElementById('byteInfo');
const originalMapping = document.getElementById('originalMapping');
const currentCharEl = document.getElementById('currentChar');
const newUnicodeInput = document.getElementById('newUnicode');
const livePreview = document.getElementById('livePreview').querySelector('span');
const saveBtn = document.getElementById('saveMappingBtn');
const unicode = currentMapping[byte];
const codePoint = parseInt(unicode.slice(2), 16);
const originalUnicode = CoreMapping[byte];
const originalCodePoint = parseInt(originalUnicode.slice(2), 16);
const byteNum = parseInt(byte);
const currentChar = String.fromCodePoint(codePoint);
// Display byte info with binary character (if displayable: ASCII 32–126)
let byteValue = String.fromCharCode(1);
if (byteNum >= 32 && byteNum <= 126) {
byteValue = `${String.fromCharCode(byteNum)}`;
}
let byteDisplay = `<div class=char-display>${byteValue} <span>${byteNum} (0x${byteNum.toString(16).padStart(2, '0').toUpperCase()})</span></div>`;
byteInfo.innerHTML = byteDisplay;
// Always display OG and current characters in the table
originalMapping.innerHTML = `<div class="char-display og">${String.fromCodePoint(originalCodePoint)} <span>(${originalUnicode})</span></div>`;
currentCharEl.innerHTML = `<div class="char-display current">${currentChar} <span>(${unicode})</span></div>`;
// Set initial input value
newUnicodeInput.value = unicode;
// Live preview on input
newUnicodeInput.oninput = () => {
const newUnicode = parseUnicodeInput(newUnicodeInput.value);
if (newUnicode) {
const newCodePoint = parseInt(newUnicode.slice(2), 16);
livePreview.textContent = String.fromCodePoint(newCodePoint);
} else {
livePreview.textContent = '-';
}
};
saveBtn.onclick = () => {
const newUnicode = parseUnicodeInput(newUnicodeInput.value);
if (newUnicode) {
try {
// Create a new mapping with the updated value
const newMapping = currentMapping.tr(currentChar, newUnicode).validate();
// If validation succeeds, update the current mapping and push to stack
currentMapping = newMapping;
mappingStack.push(newMapping);
encoder = createEncoder(currentMapping);
const newCodePoint = parseInt(newUnicode.slice(2), 16);
// Update the cell display and title
cell.textContent = String.fromCodePoint(newCodePoint);
cell.title = `${byte} -> ${newUnicode}`;
dialog.close();
console.log(mappingStack, {byte,newUnicode});
} catch (e) {
// If validation fails, revert to the previous good mapping
if (mappingStack.length > 0) {
currentMapping = mappingStack[mappingStack.length - 1];
encoder = createEncoder(currentMapping);
}
// Show a styled alert with Unicode warning symbol and formatted error
alert(
`⚠️ Validation Error\n\n\t${e.message}\n\nReverted to previous mapping.`
);
}
} else {
alert(
`⚠️ Input Error\n\n\tInvalid Unicode format.\n\tUse "U+XXXX" where XXXX is a 4-digit hex code.`
);
}
};
dialog.show();
newUnicodeInput.oninput();
}
// Handle input type switching
const inputText = document.getElementById('inputText');
const inputFile = document.getElementById('inputFile');
document.querySelectorAll('input[name="inputType"]').forEach(radio => {
radio.addEventListener('change', () => {
if (radio.value === 'text') {
inputText.style.display = 'block';
inputFile.style.display = 'none';
inputFile.value = '';
} else {
inputText.style.display = 'none';
inputFile.style.display = 'block';
inputText.value = '';
}
});
});
// Encode to Base437
document.getElementById('encodeBtn').addEventListener('click', () => {
const outputText = document.getElementById('outputText');
if (inputText.style.display !== 'none' && inputText.value) {
outputText.value = encoder.encode(inputText.value);
} else if (inputFile.files.length > 0) {
const file = inputFile.files[0];
const reader = new FileReader();
reader.onload = () => {
const bytes = new Uint8Array(reader.result);
outputText.value = encoder.encode(bytes);
};
reader.readAsArrayBuffer(file);
} else {
alert('Please enter text or select a file to encode.');
}
});
// Decode from Base437
document.getElementById('decodeBtn').addEventListener('click', () => {
const outputText = document.getElementById('outputText');
const outputType = document.getElementById('outputType').value;
const downloadLink = document.getElementById('downloadLink');
if (!outputText.value) {
alert('Please provide a Base437 string to decode.');
return;
}
try {
if (outputType === 'string') {
inputText.value = encoder.decode(outputText.value, 'string');
downloadLink.style.display = 'none';
} else {
const bytes = encoder.decode(outputText.value, 'uint8array');
const blob = new Blob([bytes], { type: 'application/octet-stream' });
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'decoded_output';
downloadLink.textContent = 'Download Decoded File';
downloadLink.style.display = 'block';
inputText.value = '[File decoded - click Download link]';
}
} catch (e) {
alert(`Decoding error: ${e.message}`);
}
});
// Convert demo image to Base64 if present
const demoImage = document.getElementById('demoImage');
if (demoImage) {
const htmlSafeMapping = CoreMapping; // CoreMapping includes this by default, but if it didn't, you'd call
// .tr('"', 'U+201C').validate();
const htmlEncoder = createEncoder(htmlSafeMapping);
window.onload = () => {
const base64Src = htmlEncoder.toBase64Url(demoImage.getAttribute('src'));
demoImage.setAttribute('src', base64Src);
console.log('Demo image converted to Base64:', base64Src);
};
}
// Initialize the demo
initMappingTable();