-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogue_gen.js
139 lines (124 loc) · 5.14 KB
/
dialogue_gen.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
const messages = [];
function generateColor() {
const hue = Math.floor(Math.random() * 360);
const saturation = Math.floor(Math.random() * 30) + 70;
const lightness = Math.floor(Math.random() * 15) + 35;
return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
}
const speakerColors = {};
function getColorForSpeaker(speaker) {
if (!speakerColors[speaker]) {
speakerColors[speaker] = generateColor();
}
return speakerColors[speaker];
}
document.getElementById('messageForm').addEventListener('submit', function(event) {
event.preventDefault();
const speaker = document.getElementById('speaker').value;
const message = document.getElementById('message').value;
const messageId = messages.length + 1;
messages.push({ id: messageId, speaker, text: message });
displayMessages();
this.reset();
});
function displayMessages() {
const messagesContainer = document.getElementById('messages');
messagesContainer.innerHTML = '';
messages.forEach(msg => {
const messageElement = document.createElement('div');
messageElement.classList.add('message');
messageElement.setAttribute('data-id', msg.id);
messageElement.style.backgroundColor = getColorForSpeaker(msg.speaker);
messageElement.setAttribute('draggable', true);
messageElement.innerHTML = `<span class="message-info">${msg.speaker}: </span>${msg.text}<span class="delete-btn" onclick="deleteMessage(${msg.id})"> [Delete]</span>`;
messagesContainer.appendChild(messageElement);
});
setupDraggableItems();
}
function setupDraggableItems() {
const messagesContainer = document.getElementById('messages');
const draggables = document.querySelectorAll('.message');
draggables.forEach(draggable => {
draggable.addEventListener('dragstart', () => {
draggable.classList.add('dragging');
});
draggable.addEventListener('dragend', () => {
draggable.classList.remove('dragging');
});
});
messagesContainer.addEventListener('dragover', e => {
e.preventDefault();
const afterElement = getDragAfterElement(messagesContainer, e.clientY);
const draggable = document.querySelector('.dragging');
if (draggable && afterElement) {
messagesContainer.insertBefore(draggable, afterElement);
} else if (draggable) {
messagesContainer.appendChild(draggable);
}
});
}
function getDragAfterElement(container, y) {
const draggableElements = [...container.querySelectorAll('.message:not(.dragging)')];
return draggableElements.reduce((closest, child) => {
const box = child.getBoundingClientRect();
const offset = y - box.top - box.height / 2;
if (offset < 0 && offset > closest.offset) {
return { offset, element: child };
} else {
return closest;
}
}, { offset: Number.NEGATIVE_INFINITY }).element;
}
document.getElementById('exportBtn').addEventListener('click', function() {
regenerateMessagesFromDOM();
const exportData = messages.map(msg => ({ speaker: msg.speaker, text: msg.text.substring(0, msg.text.length - 8) }));
const jsonStr = JSON.stringify(exportData, null, 2);
const blob = new Blob([jsonStr], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'messages.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
function regenerateMessagesFromDOM() {
const messageElements = document.querySelectorAll('.message');
const newMessages = Array.from(messageElements).map(element => {
const id = parseInt(element.getAttribute('data-id'));
const speaker = element.querySelector('.message-info').textContent.split(': ')[0].trim();
const text = element.textContent.split(': ')[1].trim();
return { id, speaker, text };
});
messages.length = 0;
messages.push(...newMessages);
}
document.getElementById('importBtn').addEventListener('change', function(event) {
const file = event.target.files[0];
if (file && file.type === "application/json") {
const reader = new FileReader();
reader.onload = function(e) {
try {
const importedMessages = JSON.parse(e.target.result);
importedMessages.forEach(msg => {
getColorForSpeaker(msg.speaker); // Assign color on import
});
messages.splice(0, messages.length, ...importedMessages);
displayMessages();
} catch (error) {
alert('Error parsing JSON file.');
}
};
reader.readAsText(file);
} else {
alert('Please upload a valid JSON file.');
}
});
function deleteMessage(id) {
const index = messages.findIndex(msg => msg.id === id);
if (index > -1) {
messages.splice(index, 1);
displayMessages();
}
}