-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathmain.js
More file actions
344 lines (285 loc) · 12 KB
/
main.js
File metadata and controls
344 lines (285 loc) · 12 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import "@ui5/webcomponents-ai/dist/TextArea.js";
import "@ui5/webcomponents/dist/Menu.js";
import "@ui5/webcomponents/dist/MenuItem.js";
import "@ui5/webcomponents/dist/Button.js";
// Sample AI responses for different actions
const SAMPLE_TEXTS = {
en: "Innovation managers operate with both creativity and business acumen, driving initiatives that cultivate an innovation-friendly culture, streamline the execution of new ideas, and ultimately unlock value for the organization and its customers.",
bg: "Мениджърите по иновации действат с креативност и бизнес усет, като насърчават инициативи, които изграждат култура, благоприятстваща иновациите, улесняват реализирането на нови идеи и в крайна сметка създават стойност за организацията и нейните клиенти.",
de: "Innovationsmanager agieren mit sowohl Kreativität als auch unternehmerischem Geschick. Sie treiben Initiativen voran, die eine innovationsfreundliche Unternehmenskultur fördern, die Umsetzung neuer Ideen optimieren und letztlich Mehrwert für das Unternehmen und seine Kunden schaffen.",
bulleted: `Innovation managers:
• Operate with creativity and business acumen
• Drive initiatives that cultivate an innovation-friendly culture
• Streamline the execution of new ideas
• Unlock value for the organization and its customers`,
expanded: "Innovation managers play a pivotal role in organizations by blending creativity with strategic business acumen. They are not just responsible for generating innovative ideas but also for fostering a culture that encourages experimentation, collaboration, and out-of-the-box thinking among teams. These professionals work tirelessly to identify opportunities for improvement, anticipate market trends, and develop solutions that align with the organization's goals and customer needs. In their efforts to promote an innovation-friendly environment, they implement frameworks and processes that remove barriers to creativity, making it easier for teams to brainstorm, prototype, and refine new concepts. Beyond idea generation, innovation managers ensure that these ideas are systematically evaluated and effectively executed, transforming them into tangible value propositions. This requires them to bridge the gap between visionary thinking and practical implementation, often by collaborating with cross-functional teams, securing resources, and navigating organizational dynamics. Ultimately, the work of innovation managers unlocks significant value, driving growth, enhancing customer satisfaction, and ensuring the organization remains competitive in a rapidly evolving landscape. Their dual focus on creativity and business results positions them as key drivers of sustainable innovation and long-term success.",
simplified: "Innovation managers use creativity and business skills to promote innovation, simplify the development of new ideas, and deliver value to the organization and its customers.",
summarized: "Innovation managers blend creativity and business skills to foster innovation, simplify idea implementation, and create value for organizations and customers."
};
const TIMING_CONFIG = {
processingDelay: 3000, // Milliseconds to simulate AI processing time
typingSpeed: 10 // Milliseconds between each character in typing animation
};
// Configuration for AI action menu - customize this to add/remove AI capabilities
const MENU_CONFIG = [
{
text: "Regenerate",
action: "regenerate",
processingLabel: "Regenerating text...",
completedLabel: "Regenerated text",
textKey: "en",
replaces: "generate"
},
{
text: "Fix spelling and grammar",
action: "fixSpelling",
processingLabel: "Fixing spelling and grammar...",
completedLabel: "Fixed spelling and grammar",
textKey: "en",
startsSection: true
},
{
text: "Rewrite text",
children: [
{ text: "Simplify", action: "simplify", processingLabel: "Simplifying text...", completedLabel: "Simplified text", textKey: "simplified" },
{ text: "Expand", action: "expand", processingLabel: "Expanding text...", completedLabel: "Expanded text", textKey: "expanded" },
{ text: "Summarize", action: "summarize", processingLabel: "Summarizing text...", completedLabel: "Summarized text", textKey: "summarized" }
]
},
{
text: "Make bulleted list",
action: "bullets",
processingLabel: "Making bulleted list...",
completedLabel: "Made bulleted list",
textKey: "bulleted"
},
{
text: "Translate",
children: [
{ text: "English", action: "translateEN", processingLabel: "Translating to English...", completedLabel: "Translated to English", textKey: "en" },
{ text: "German", action: "translateDE", processingLabel: "Translating to German...", completedLabel: "Translated to German", textKey: "de" },
{ text: "Bulgarian", action: "translateBG", processingLabel: "Translating to Bulgarian...", completedLabel: "Translated to Bulgarian", textKey: "bg" }
]
}
];
// State management for version control and operations
let versionHistory = [];
let currentIndexHistory = 0;
let currentActionInProgress = null;
let typingInterval = null;
let currentGenerationIndex = 0;
let contentBeforeGeneration = "";
const textarea = document.getElementById('ai-textarea');
const menu = document.getElementById('ai-menu');
const resetBtn = document.getElementById('reset-btn');
const readonlyToggle = document.getElementById('readonly-toggle');
const disabledToggle = document.getElementById('disabled-toggle');
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function saveCurrentVersion() {
if (versionHistory.length > 0 && versionHistory[currentIndexHistory]) {
versionHistory[currentIndexHistory].value = textarea.value;
}
}
function updateComponentState(versionIndex = null) {
if (versionIndex !== null && versionHistory[versionIndex]) {
currentIndexHistory = versionIndex;
textarea.value = versionHistory[versionIndex].value;
}
textarea.currentVersion = currentIndexHistory + 1;
textarea.totalVersions = versionHistory.length;
if (versionHistory[currentIndexHistory]) {
textarea.promptDescription = versionHistory[currentIndexHistory].endAction;
} else {
textarea.promptDescription = "";
}
}
function createMenuItem(configItem) {
const item = document.createElement('ui5-menu-item');
item.setAttribute('text', configItem.text || '');
if (configItem.action) {
item.dataset.action = configItem.action;
if (configItem.processingLabel) {
item.dataset.processingLabel = configItem.processingLabel;
}
if (configItem.completedLabel) {
item.dataset.completedLabel = configItem.completedLabel;
}
if (configItem.textKey) {
item.dataset.textKey = configItem.textKey;
}
}
if (configItem.startsSection) {
item.setAttribute('starts-section', '');
}
return item;
}
function buildMenuFromConfig() {
const hasHistory = versionHistory.length > 0;
menu.innerHTML = '';
// Add initial "Generate text" option if no history
if (!hasHistory) {
const generateItem = document.createElement('ui5-menu-item');
generateItem.setAttribute('text', 'Generate text');
generateItem.dataset.action = 'generate';
generateItem.dataset.processingLabel = 'Generating text...';
generateItem.dataset.completedLabel = 'Generated text';
generateItem.dataset.textKey = 'en';
menu.appendChild(generateItem);
}
MENU_CONFIG.forEach(configItem => {
if (configItem.replaces && !hasHistory) {
return;
}
if (configItem.children && Array.isArray(configItem.children)) {
const group = createMenuItem(configItem);
configItem.children.forEach(child => {
const childItem = createMenuItem(child);
group.appendChild(childItem);
});
menu.appendChild(group);
} else {
menu.appendChild(createMenuItem(configItem));
}
});
}
function stopTypingAnimation() {
if (typingInterval) {
clearInterval(typingInterval);
typingInterval = null;
}
}
function completeGeneration(action, menuItem) {
stopTypingAnimation();
const completedLabel = (menuItem && menuItem.dataset.completedLabel) ? menuItem.dataset.completedLabel : 'Action completed';
versionHistory.push({
value: textarea.value,
action,
endAction: completedLabel,
timestamp: new Date().toISOString()
});
currentIndexHistory = versionHistory.length - 1;
currentActionInProgress = null;
buildMenuFromConfig();
updateComponentState();
textarea.loading = false;
}
function animateTextGeneration(text, action, menuItem) {
return new Promise(resolve => {
const chars = text.split('');
let i = 0;
textarea.value = "";
textarea.loading = true;
typingInterval = setInterval(() => {
if (i < chars.length) {
textarea.value += chars[i++];
} else {
completeGeneration(action, menuItem);
resolve();
}
}, TIMING_CONFIG.typingSpeed);
});
}
function setLoadingState(promptDescription) {
textarea.loading = true;
textarea.value = "Analyzing request...";
textarea.promptDescription = promptDescription || '';
}
function resetGenerationState() {
stopTypingAnimation();
currentActionInProgress = null;
textarea.loading = false;
}
function findMenuItemByAction(action) {
return menu.querySelector(`ui5-menu-item[data-action="${action}"]`);
}
// Main function to execute AI actions - replace the delay/sample logic with actual API calls
async function executeAction(action) {
if (textarea.loading) {
return;
}
const menuItem = findMenuItemByAction(action);
if (!menuItem) {
return;
}
const processingLabel = menuItem.dataset.processingLabel || 'Processing...';
const textKey = menuItem.dataset.textKey || 'en';
saveCurrentVersion();
contentBeforeGeneration = textarea.value;
currentActionInProgress = action;
currentGenerationIndex += 1;
const generationIdForThisRun = currentGenerationIndex;
setLoadingState(processingLabel);
// Replace this delay with actual API call
await delay(TIMING_CONFIG.processingDelay);
if (!textarea.loading || generationIdForThisRun !== currentGenerationIndex) {
resetGenerationState();
return;
}
const text = SAMPLE_TEXTS[textKey] || SAMPLE_TEXTS.en;
await animateTextGeneration(text, action, menuItem);
}
function stopGeneration() {
if (!textarea.loading) {
return;
}
stopTypingAnimation();
currentGenerationIndex += 1;
const stoppedValue = textarea.value;
if (stoppedValue.trim()) {
const action = currentActionInProgress || 'generate';
const menuItem = findMenuItemByAction(action);
const completedLabel = (menuItem && menuItem.dataset.completedLabel) ? menuItem.dataset.completedLabel : 'Action completed';
versionHistory.push({
value: stoppedValue,
action,
endAction: completedLabel + " (stopped)",
timestamp: new Date().toISOString()
});
currentIndexHistory = versionHistory.length - 1;
buildMenuFromConfig();
updateComponentState();
}
currentActionInProgress = null;
textarea.loading = false;
}
function handleVersionChange(event) {
const { backwards } = event.detail || {};
if (backwards && currentIndexHistory > 0) {
saveCurrentVersion();
updateComponentState(currentIndexHistory - 1);
} else if (!backwards && currentIndexHistory < versionHistory.length - 1) {
saveCurrentVersion();
updateComponentState(currentIndexHistory + 1);
}
}
async function handleMenuItemClick(event) {
const action = event?.detail?.item?.dataset?.action;
if (!action) {
return;
}
await executeAction(action);
}
function handleStopGeneration() {
stopGeneration();
}
function handleKeydown(event) {
if (event.key === 'Escape' && textarea.loading) {
event.preventDefault();
stopGeneration();
}
}
function init() {
buildMenuFromConfig();
menu.addEventListener('item-click', handleMenuItemClick);
textarea.addEventListener('version-change', handleVersionChange);
textarea.addEventListener('stop-generation', handleStopGeneration);
document.addEventListener('keydown', handleKeydown);
// Prevent memory leaks on page unload
window.addEventListener('beforeunload', () => {
stopTypingAnimation();
resetGenerationState();
});
}
document.addEventListener('DOMContentLoaded', init);