|
| 1 | +import "@ui5/webcomponents/dist/Card.js"; |
| 2 | +import "@ui5/webcomponents/dist/CardHeader.js"; |
| 3 | +import "@ui5/webcomponents/dist/Toast.js"; |
| 4 | +import "@ui5/webcomponents/dist/Token.js"; |
| 5 | +import "@ui5/webcomponents/dist/Title.js"; |
| 6 | +import "@ui5/webcomponents/dist/TextArea.js"; |
| 7 | +import "@ui5/webcomponents/dist/Link.js"; |
| 8 | +import "@ui5/webcomponents/dist/Label.js"; |
| 9 | +import "@ui5/webcomponents/dist/Select.js"; |
| 10 | +import "@ui5/webcomponents/dist/Option.js"; |
| 11 | +import "@ui5/webcomponents/dist/Popover.js"; |
| 12 | +import "@ui5/webcomponents/dist/Dialog.js"; |
| 13 | +import "@ui5/webcomponents/dist/RangeSlider.js"; |
| 14 | +import "@ui5/webcomponents/dist/SegmentedButton.js"; |
| 15 | +import "@ui5/webcomponents/dist/Bar.js"; |
| 16 | +import "@ui5/webcomponents/dist/Toolbar.js"; |
| 17 | +import "@ui5/webcomponents-fiori/dist/DynamicSideContent.js"; |
| 18 | +import "@ui5/webcomponents-ai/dist/Button.js"; |
| 19 | +import "@ui5/webcomponents-icons/dist/ai.js"; |
| 20 | +import "@ui5/webcomponents-icons/dist/stop.js"; |
| 21 | + |
| 22 | +let response = null; |
| 23 | +let responseContent = null; |
| 24 | + |
| 25 | +if (!response) { |
| 26 | + response = await fetch("../assets/data/predefinedTexts.json"); |
| 27 | + responseContent = await response.json(); |
| 28 | +} |
| 29 | + |
| 30 | +const dialog = document.getElementById("dialog"); |
| 31 | +const aiDialogButton = document.getElementById("aiDialogButton"); |
| 32 | +const openDialogButton = document.getElementById("openDialogButton"); |
| 33 | +const closeDialogButton = document.getElementById("closeDialogButton"); |
| 34 | +const output = document.getElementById("output"); |
| 35 | +const dialogOutput = document.getElementById("dialogOutput"); |
| 36 | +const structrureSelect = document.getElementById("structureSelect"); |
| 37 | +const languageSelect = document.getElementById("languageSelect"); |
| 38 | +const toneOfVoiceSelect = document.getElementById("toneOfVoiceSelect"); |
| 39 | +const sendButton = document.getElementById("sendButton"); |
| 40 | +const toast = document.getElementById("guidedPromptToast"); |
| 41 | + |
| 42 | +const texts = { |
| 43 | + paragraph: responseContent.predefinedTexts, |
| 44 | + bulleted: responseContent.predefinedTextsBulleted, |
| 45 | +}; |
| 46 | +let options = { |
| 47 | + structure: "paragraph", |
| 48 | + language: "en", |
| 49 | + toneOfVoice: 2, |
| 50 | +}; |
| 51 | +let text; |
| 52 | +let dialogGenerationId; |
| 53 | + |
| 54 | +function startGeneration() { |
| 55 | + console.warn("startGeneration"); |
| 56 | + dialogOutput.value = ""; |
| 57 | + text = texts[options.structure][options.language][options.toneOfVoice]; |
| 58 | + let generatedWordIndex = 0; |
| 59 | + const generationId = setInterval(function () { |
| 60 | + const words = text.split(" "); |
| 61 | + const maxWordIndex = words.length - 1; |
| 62 | + if (generatedWordIndex > maxWordIndex) { |
| 63 | + stopGeneration(generationId); |
| 64 | + aiDialogButton.state = "revise"; |
| 65 | + openDialogButton.innerText = "Revise"; |
| 66 | + closeDialogButton.disabled = false; |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + dialogOutput.value += words[generatedWordIndex] + " "; |
| 71 | + generatedWordIndex++; |
| 72 | + }, 75); |
| 73 | + return generationId; |
| 74 | +} |
| 75 | + |
| 76 | +function stopGeneration(generationId) { |
| 77 | + console.warn("stopGeneration"); |
| 78 | + closeDialogButton.disabled = false; |
| 79 | + clearTimeout(generationId); |
| 80 | +} |
| 81 | + |
| 82 | +function openDialogButtonClickHandler(evt) { |
| 83 | + openDialog(); |
| 84 | +} |
| 85 | + |
| 86 | +function aiDialogButtonClickHandler(evt) { |
| 87 | + var button = evt.target; |
| 88 | + switch (button.state) { |
| 89 | + case "": |
| 90 | + case "generate": |
| 91 | + startGenerationFromDialog(); |
| 92 | + break; |
| 93 | + case "revise": |
| 94 | + startGenerationFromDialog(); |
| 95 | + break; |
| 96 | + case "generating": |
| 97 | + button.state = "generate"; |
| 98 | + openDialogButton.innerText = "Generate"; |
| 99 | + closeDialogButton.disabled = false; |
| 100 | + stopGeneration(dialogGenerationId, closeDialogButton); |
| 101 | + break; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +function startGenerationFromDialog() { |
| 106 | + aiDialogButton.state = "generating"; |
| 107 | + closeDialogButton.disabled = true; |
| 108 | + dialogGenerationId = startGeneration(); |
| 109 | +} |
| 110 | + |
| 111 | +function openDialog() { |
| 112 | + dialog.open = true; |
| 113 | +} |
| 114 | + |
| 115 | +function closeDialog() { |
| 116 | + dialog.open = false; |
| 117 | + output.value = dialogOutput.value; |
| 118 | +} |
| 119 | + |
| 120 | +function structureSelectHandler(evt) { |
| 121 | + options.structure = evt.detail.selectedOption.value; |
| 122 | +} |
| 123 | + |
| 124 | +function languageSelectHandler(evt) { |
| 125 | + options.language = evt.detail.selectedOption.value; |
| 126 | +} |
| 127 | + |
| 128 | +function toneOfVoiceSelectHandler(evt) { |
| 129 | + const value = evt.detail.selectedItems[0].innerText; |
| 130 | + switch (value) { |
| 131 | + case "Formal": { |
| 132 | + options.toneOfVoice = 1; |
| 133 | + break; |
| 134 | + } |
| 135 | + case "Neutral": { |
| 136 | + options.toneOfVoice = 2; |
| 137 | + break; |
| 138 | + } |
| 139 | + case "Casual": { |
| 140 | + options.toneOfVoice = 3; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +sendButton.addEventListener("click", function() { |
| 146 | + const output = document.getElementById("output"); |
| 147 | + if (output.value) { |
| 148 | + toast.open = true; |
| 149 | + output.valueState = "None"; |
| 150 | + output.value = ""; |
| 151 | + aiDialogButton.state = "generate"; |
| 152 | + } |
| 153 | +}); |
| 154 | + |
| 155 | +structrureSelect.addEventListener("change", structureSelectHandler); |
| 156 | +openDialogButton.addEventListener("click", openDialogButtonClickHandler); |
| 157 | +aiDialogButton.addEventListener("click", aiDialogButtonClickHandler); |
| 158 | +closeDialogButton.addEventListener("click", closeDialog); |
| 159 | +languageSelect.addEventListener("change", languageSelectHandler); |
| 160 | +toneOfVoiceSelect.addEventListener( |
| 161 | + "selection-change", |
| 162 | + toneOfVoiceSelectHandler |
| 163 | +); |
0 commit comments