-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
108 lines (95 loc) · 3.27 KB
/
script.js
File metadata and controls
108 lines (95 loc) · 3.27 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
const inputField = document.getElementById('input');
const uploadIcon = document.getElementById('upload-icon');
const downloadIcon = document.getElementById('download-icon');
const fileUpload = document.getElementById('file-upload');
const uploadTooltip = document.getElementById('upload-tooltip');
const downloadTooltip = document.getElementById('download-tooltip');
// Handle input changes for paired characters
inputField.addEventListener('input', function(event) {
const text = inputField.innerText;
processInput(text);
});
// Handle tab key for indentation
inputField.addEventListener('keydown', function(event) {
if (event.key === 'Tab') {
event.preventDefault();
insertTab();
}
});
// Upload file functionality
uploadIcon.addEventListener('click', function() {
fileUpload.click();
});
fileUpload.addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
inputField.innerText = e.target.result;
};
reader.readAsText(file);
}
});
// Download file functionality
downloadIcon.addEventListener('click', function() {
const text = inputField.innerText;
const blob = new Blob([text], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'output.txt'; // Default download file name
link.click();
});
function processInput(input) {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
const textNode = range.startContainer;
if (textNode.nodeType === Node.TEXT_NODE) {
const text = textNode.nodeValue;
const cursorPosition = range.startOffset;
// Handle paired characters
let newText = handlePairedCharacters(text, cursorPosition);
// Update the text node with modified content
textNode.nodeValue = newText;
// Restore cursor position
range.setStart(textNode, cursorPosition);
range.setEnd(textNode, cursorPosition);
selection.removeAllRanges();
selection.addRange(range);
}
}
}
function handlePairedCharacters(text, cursorPosition) {
// Define pairs of characters to auto-insert
const pairs = {
'(': ')',
'{': '}',
'[': ']',
'"': '"',
'\'': '\''
};
// Iterate over each pair
for (let [openChar, closeChar] of Object.entries(pairs)) {
if (text[cursorPosition - 1] === openChar && text[cursorPosition] === closeChar) {
// If the cursor is between an opening and closing pair, skip insertion
cursorPosition++;
} else if (text[cursorPosition - 1] === openChar && text[cursorPosition] !== closeChar) {
// If the cursor is after an opening pair without a closing pair, insert the closing pair
text = text.substring(0, cursorPosition) + closeChar + text.substring(cursorPosition);
cursorPosition++;
}
}
return text;
}
function insertTab() {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
const tabNode = document.createTextNode(' '); // 4 spaces
range.insertNode(tabNode);
range.setStartAfter(tabNode);
range.setEndAfter(tabNode);
selection.removeAllRanges();
selection.addRange(range);
}
}