-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
194 lines (171 loc) · 7.98 KB
/
main.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
let currPageIdx = 0;
let textBlocks = [[]];
const pagesList = document.querySelector('.pages');
const addNewPageBtn = document.querySelector('.add-new-page-btn');
const textBlockForm = document.querySelector('.text-block-form');
const textBlockInp = document.querySelector('.text-block-inp');
const textBlockSubmitBtn = document.querySelector('.text-block-submit-btn');
const indexesList = document.querySelector('.indexes-list');
const textBlocksList = document.querySelector('.text-blocks-list');
function addNewPageHandler(e) {
const existingPagesCount = pagesList.querySelectorAll('.page[data-page-idx]').length;
const newPageEl = document.createElement('li');
newPageEl.className = 'page';
newPageEl.innerText = existingPagesCount + 1;
newPageEl.setAttribute('data-page-idx', existingPagesCount);
newPageEl.setAttribute('onclick', 'changePageHandler(event)');
const deleteBtn = document.createElement('button');
deleteBtn.className = 'delete-page-btn';
deleteBtn.innerHTML = '<i class="bi bi-x"></i>';
deleteBtn.onclick = function(event) {
deletePageHandler(event);
};
newPageEl.appendChild(deleteBtn);
pagesList.insertBefore(newPageEl, addNewPageBtn);
textBlocks.push([]);
changePageHandler({target: newPageEl});
}
function deletePageHandler(event) {
const pageToDelete = event.target.closest('.page');
const pageIdxToDelete = parseInt(pageToDelete.dataset.pageIdx);
pageToDelete.remove();
textBlocks.splice(pageIdxToDelete, 1);
if (currPageIdx === pageIdxToDelete) {
currPageIdx = 0;
const firstPage = pagesList.querySelector('.page[data-page-idx="0"]');
if (firstPage) firstPage.classList.add('active');
}
renderIndexes();
renderTextBlocks();
}
function changePageHandler(e) {
const currPageEl = pagesList.querySelector(`.page[data-page-idx="${currPageIdx}"]`);
if (currPageEl) currPageEl.classList.remove('active');
e.target.classList.add('active');
currPageIdx = Number(e.target.getAttribute('data-page-idx'));
renderIndexes();
renderTextBlocks();
}
function ctc(text) {
const textareaInp = document.createElement('textarea');
textareaInp.value = text;
document.body.appendChild(textareaInp);
textareaInp.select();
navigator.clipboard.writeText(textareaInp.value);
textareaInp.remove();
}
function ctcHandler() {
const appendedText = textBlocks[currPageIdx].reduce((text, ele) => text + `[code]<pre>\n${ele.text}\n</pre>[/code]\n`, '');
ctc(appendedText);
}
function renderIndexes() {
indexesList.innerHTML = '';
textBlocks[currPageIdx].forEach(function(textBlock, idx) {
indexesList.innerHTML += `<li draggable="true" ondragstart="onDragStartItem(event)" ondragover="onDragOverItem(event)" ondrop="onDropItem(event)" class="indexes-item" data-idx="${idx}"><div class="move-up-down-icons"><i class="move-up-icon bi bi-arrow-up-circle-fill" onclick=reorderTextBlocksHandler(event)></i><i class="move-down-icon bi bi-arrow-down-circle-fill" onclick=reorderTextBlocksHandler(event)></i></div><span class="indexes-item-content">${truncateText(textBlock.text, 40)}</span><i class="delete-icon bi bi-x-circle-fill" onclick="deleteTextBlockHandler(event)"></i></li>`;
});
if (textBlocks[currPageIdx].length) {
indexesList.innerHTML += `<li class="indexes-item ctc-item"><button onclick="ctcHandler()" class="ctc-btn">Copy to Clipboard</button></li>`;
}
}
function renderTextBlocks() {
textBlocksList.innerHTML = '';
textBlocks[currPageIdx].forEach(function(textBlock, idx) {
textBlocksList.innerHTML += `<li draggable="true" ondragstart="onDragStartItem(event)" ondragover="onDragOverItem(event)" ondrop="onDropItem(event)" class="text-blocks-item" data-idx="${idx}"><div class="move-up-down-icons"><i class="move-up-icon bi bi-arrow-up-circle-fill" onclick=reorderTextBlocksHandler(event)></i><i class="move-down-icon bi bi-arrow-down-circle-fill" onclick=reorderTextBlocksHandler(event)></i></div><span class="text-blocks-item-content">${textBlock.text}</span><i class="delete-icon bi bi-x-circle-fill" onclick="deleteTextBlockHandler(event)"></i></li>`;
});
}
function addTextBlockHandler(e) {
e.preventDefault();
const text = textBlockInp.value.trim();
if (!text.length) return;
const index = text.split(/\s+/).length === 1 ? text.split(/\s+/).slice(0, 1) : text.split(/\s+/).slice(0, 2).join(' ');
let duplicateIndexId = 0;
textBlocks[currPageIdx].forEach((ele) => {
if (ele.index == index) duplicateIndexId = ele.duplicateIndexId + 1;
});
textBlocks[currPageIdx].push({ index, duplicateIndexId, text });
renderIndexes();
renderTextBlocks();
textBlockInp.value = '';
}
function deleteTextBlockHandler(e) {
const li = e.target.closest('li');
const dataIdx = li.getAttribute('data-idx');
textBlocks[currPageIdx] = textBlocks[currPageIdx].filter((ele, idx) => idx != dataIdx);
renderIndexes();
renderTextBlocks();
}
function reorderTextBlocksHandler(e) {
const li = e.target.closest('li');
const upIconClicked = e.target.className.indexOf('up') != -1;
const clickedIdx = li.getAttribute('data-idx');
let exchangeIdx = clickedIdx;
if (upIconClicked && clickedIdx != 0) exchangeIdx--;
else if (!upIconClicked && clickedIdx != textBlocks[currPageIdx].length - 1) exchangeIdx++;
const temp = textBlocks[currPageIdx][clickedIdx];
textBlocks[currPageIdx][clickedIdx] = textBlocks[currPageIdx][exchangeIdx];
textBlocks[currPageIdx][exchangeIdx] = temp;
renderIndexes();
renderTextBlocks();
}
function onDragStartItem(e) {
e.dataTransfer.setData('idx', e.target.getAttribute('data-idx'));
}
function onDragOverItem(e) {
e.preventDefault();
}
function onDropItem(e) {
e.preventDefault();
const dragItemIdx = Number(e.dataTransfer.getData('idx'));
const dropItemIdx = Number(e.target.closest('[draggable="true"]').getAttribute('data-idx'));
const dragItem = textBlocks[currPageIdx][dragItemIdx];
textBlocks[currPageIdx].splice(dragItemIdx, 1);
textBlocks[currPageIdx].splice(dropItemIdx, 0, dragItem);
renderIndexes();
renderTextBlocks();
}
function truncateText(text, maxLength) {
return text.length > maxLength ? text.substring(0, maxLength) + '...' : text;
}
document.addEventListener('DOMContentLoaded', function() {
const clearAllBtn = document.getElementById('clearAllBtn'); // Select the Clear All button
clearAllBtn.addEventListener('click', function() { // Add click event listener
location.reload(); // Reload the page
});
});
document.addEventListener('DOMContentLoaded', function() {
const updateTimeZones = () => {
const timeZones = [
{ elementId: 'time-US-West', timeZone: 'America/Los_Angeles' },
{ elementId: 'time-Denver', timeZone: 'America/Denver' },
{ elementId: 'time-UTC', timeZone: 'UTC' },
{ elementId: 'time-Amsterdam', timeZone: 'Europe/Amsterdam' },
{ elementId: 'time-India', timeZone: 'Asia/Kolkata' },
{ elementId: 'time-Korea', timeZone: 'Asia/Seoul' },
{ elementId: 'time-Singapore', timeZone: 'Asia/Singapore' },
];
timeZones.forEach(({ elementId, timeZone }) => {
const now = new Date();
const timeFormatOptions = {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZone: timeZone,
hour12: false // Adjust based on preference
};
const dateFormatOptions = {
weekday: 'short',
day: '2-digit',
month: 'long',
year: 'numeric',
timeZone: timeZone
};
const timeFormatter = new Intl.DateTimeFormat('en-US', timeFormatOptions);
const dateFormatter = new Intl.DateTimeFormat('en-US', dateFormatOptions);
const timeString = timeFormatter.format(now);
const dateString = dateFormatter.format(now);
document.getElementById(elementId).textContent = `${timeString} on ${dateString}`;
});
};
const timeZonesBtn = document.getElementById('timeZonesBtn');
timeZonesBtn.addEventListener('mouseenter', updateTimeZones);
});