|
8 | 8 | function deleteOption(index){
|
9 | 9 | options.splice(index, 1);
|
10 | 10 | }
|
| 11 | +
|
| 12 | + /** |
| 13 | + * Handle pasting text into an option text input. |
| 14 | + */ |
| 15 | + function handlePaste(event, index) { |
| 16 | + // Get the pasted text |
| 17 | + const pastedText = event.clipboardData.getData("text"); |
| 18 | +
|
| 19 | + // If the pasted text is multiline, process it to generate new options. |
| 20 | + // Otherwise, just paste it into field as normal. |
| 21 | + if (pastedText.includes("\n")) { |
| 22 | + // Prevent the default paste behavior |
| 23 | + event.preventDefault(); |
| 24 | +
|
| 25 | + // Split the text into lines and remove whitespace |
| 26 | + const lines = pastedText.split('\n').map(line => line.trim()) |
| 27 | + .filter(line => line.length > 0); // Remove empty lines |
| 28 | +
|
| 29 | + if (lines.length >= 1) { |
| 30 | + // Replace the current option with the first line |
| 31 | + options[index] = lines[0]; |
| 32 | +
|
| 33 | + // Insert the remaining lines as new options after the current one |
| 34 | + const newOptions = lines.slice(1); |
| 35 | + options.splice(index + 1, 0, ...newOptions); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
11 | 39 | </script>
|
12 | 40 | {#each options as option, index (index)}
|
13 | 41 | <div class="input-group mb-1">
|
| 42 | + <!-- Icon (based on question type) --> |
14 | 43 | <span class="input-group-text">
|
15 | 44 | {#if type === "radio"}
|
16 | 45 | <i class='bx bx-radio-circle-marked' ></i>
|
|
22 | 51 | <i class='bx bxs-grid'></i>
|
23 | 52 | {/if}
|
24 | 53 | </span>
|
25 |
| - <input class="form-control" type="text" bind:value={options[index]} readonly={readonly}/> |
| 54 | + <input class="form-control" type="text" bind:value={options[index]} readonly={readonly} |
| 55 | + onpaste={readonly ? undefined : (e) => handlePaste(e, index)} /> |
26 | 56 | {#if readonly}
|
27 | 57 | <button class="btn btn-outline-secondary" aria-label="Delete option" title="Delete option"><i class='bx bx-x' ></i></button>
|
28 | 58 | {:else}
|
|
0 commit comments