Skip to content

Commit c177f09

Browse files
Merge pull request #308 from RSE-Sheffield/feat/multi-line-options
Survey configuration: Convert multiline paste into new options
2 parents 63cb3a6 + 7b5fc9c commit c177f09

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

ui_components/src/lib/components/input/OptionsList.svelte

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,38 @@
88
function deleteOption(index){
99
options.splice(index, 1);
1010
}
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+
}
1139
</script>
1240
{#each options as option, index (index)}
1341
<div class="input-group mb-1">
42+
<!-- Icon (based on question type) -->
1443
<span class="input-group-text">
1544
{#if type === "radio"}
1645
<i class='bx bx-radio-circle-marked' ></i>
@@ -22,7 +51,8 @@
2251
<i class='bx bxs-grid'></i>
2352
{/if}
2453
</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)} />
2656
{#if readonly}
2757
<button class="btn btn-outline-secondary" aria-label="Delete option" title="Delete option"><i class='bx bx-x' ></i></button>
2858
{:else}

0 commit comments

Comments
 (0)