-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeep.js
More file actions
156 lines (124 loc) · 5.35 KB
/
keep.js
File metadata and controls
156 lines (124 loc) · 5.35 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
import { handleDelete } from './settings/delete_note.js';
import { handleToggle } from './settings/drop_down_up.js';
import { iconToggleUP, iconEllipsis, iconTrash } from './utils/icons.js';
import { changeColor, changeTextColor } from './settings/changecolor.js';
// Constants
export const container = document.getElementById('container');
// Variables for dragging functionality
let newPosX = 0, newPosY = 0, startPosX = JSON.parse(localStorage.getItem('startPosX')) ?? 0, startPosY = JSON.parse(localStorage.getItem('startPosY')) ?? 0;
let selectedElement;
console.log(startPosX);
const toBeStored = {
};
// Function to add a new item to the container
export async function addItem(elem) {
const uuid = uuidv4();
const element = `<div class="card" data-id="${uuid}">
<div class="card-title-bar" data-id="${uuid}">
<div id="toggle-${uuid}">${iconToggleUP}</div>
<div style="display: flex; gap: 15px">
<div id="settings-${uuid}">${iconEllipsis}</div>
<div id="remove-${uuid}">${iconTrash}</div>
</div>
</div>
<div class="card-body">
<textarea class="ss" data-id="${uuid}"></textarea>
</div>
<div class="card-footer">
<h4><i class="fa-solid fa-gear"></i> Settings</h4>
<div class="line"></div>
<div class="card-footer-items">
<div>
<label for="bgcolor">BG Color:</label>
<input id="input-${uuid}" type="color" value="#A6DCE9">
</div>
<div>
<label for="textcolor">Text Color:</label>
<input id="input-text-${uuid}" type="color" value="#3c3c3c">
</div>
</div>
</div>
</div>`;
elem.insertAdjacentHTML('beforeend', element);
// Event listeners
const cardTitleBar = document.querySelector(`.card-title-bar[data-id="${uuid}"]`);
cardTitleBar.addEventListener("mousedown", mouseDown);
const toggleBtn = document.getElementById(`toggle-${uuid}`);
toggleBtn.addEventListener('click', handleToggle);
const settingsBtn = document.getElementById(`settings-${uuid}`);
settingsBtn.addEventListener('click', openSettings);
const inputColor = document.getElementById(`input-${uuid}`);
inputColor.addEventListener('input', changeColor);
const inputTextColor = document.getElementById(`input-text-${uuid}`);
inputTextColor.addEventListener('input', changeTextColor);
const removeBtn = document.getElementById(`remove-${uuid}`);
removeBtn.addEventListener('click', handleDelete);
const textArea = document.querySelector(`.card-body textarea[data-id="${uuid}"]`);
textArea.addEventListener('input', autoGrow);
autoGrow.call(textArea);
}
// Function to automatically adjust textarea height
function autoGrow() {
this.style.height = 'auto';
this.style.height = this.scrollHeight + 'px';
}
// Function to handle mouse move during dragging
function mouseMove(e) {
newPosX = startPosX - e.clientX;
newPosY = startPosY - e.clientY;
startPosX = e.clientX;
startPosY = e.clientY;
const draggableRect = selectedElement.getBoundingClientRect();
const maxX = window.innerWidth - draggableRect.width;
const maxY = window.innerHeight - draggableRect.height;
let newX = selectedElement.offsetLeft - newPosX;
let newY = selectedElement.offsetTop - newPosY;
newX = Math.max(0, Math.min(newX, maxX));
newY = Math.max(0, Math.min(newY, maxY));
selectedElement.style.top = newY + "px";
selectedElement.style.left = newX + "px";
}
// Function to handle mouse down event for dragging
function mouseDown(e) {
e.preventDefault();
const items = document.querySelectorAll('.card-title-bar');
selectedElement = e.target.parentElement;
startPosX = e.clientX;
startPosY = e.clientY;
items.forEach(item => item.parentElement.style.zIndex = 0);
selectedElement.style.zIndex = 999;
selectedElement.classList.add('dragging');
selectedElement.classList.remove('dropped');
document.addEventListener('mousemove', mouseMove);
document.addEventListener('mouseup', () => {
const id = selectedElement.dataset.id;
selectedElement.classList.remove('dragging');
selectedElement.classList.add('dropped');
document.removeEventListener('mousemove', mouseMove);
toBeStored['startPosX'] = JSON.stringify((selectedElement.offsetLeft - newPosX) + 'px')
toBeStored['startPosY'] = JSON.stringify((selectedElement.offsetLeft - newPosY) + 'px')
});
}
function openSettings(event) {
// Find the closest ancestor element with the class "card"
const card = event.target.closest('.card');
// If a card element is found
if (card) {
// Find the settings bar within the card
const settingsBar = card.querySelector('.card-footer');
// If settings bar exists
if (settingsBar) {
// Toggle the visibility of the settings bar by toggling a CSS class
settingsBar.classList.toggle('visible');
}
}
}
function saveData(){
console.log('checking for data to be saved')
Object.keys(toBeStored).forEach(function(key){
localStorage.setItem(key, toBeStored[key]);
console.log(`updated ${key}`);
delete toBeStored[key];
});
}
setInterval(saveData, 3000)