Skip to content

Commit 655301b

Browse files
committed
Add multiple options with one paste #337
Signed-off-by: hamza221 <[email protected]>
1 parent 350a2b6 commit 655301b

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

src/components/Questions/AnswerInput.vue

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div :is="pseudoIcon"
44
v-if="!isDropdown"
55
class="question__item__pseudoInput" />
6-
<input ref="input"
6+
<textarea ref="input"
77
:aria-label="t('forms', 'An answer for the {index} option', { index: index + 1 })"
88
:placeholder="t('forms', 'Answer number {index}', { index: index + 1 })"
99
:value="answer.text"
@@ -14,8 +14,7 @@
1414
type="text"
1515
@input="onInput"
1616
@keydown.delete="deleteEntry"
17-
@keydown.enter.prevent="addNewEntry">
18-
17+
@keydown.enter.prevent="addNewEntry" />
1918
<!-- Delete answer -->
2019
<NcActions>
2120
<NcActionButton @click="deleteEntry">
@@ -111,17 +110,23 @@ export default {
111110
// clone answer
112111
const answer = Object.assign({}, this.answer)
113112
answer.text = this.$refs.input.value
113+
if (!this.isUnique) {
114+
const multipleAnswers = answer.text.split(/\r?\n/g)
115+
if (multipleAnswers.length > 1) {
116+
// extract all answer entries except the first one to parent
117+
this.$emit('multiple-answers', multipleAnswers, this.answer)
118+
return
119+
}
120+
}
114121
115122
if (this.answer.local) {
116-
117123
// Dispatched for creation. Marked as synced
118124
// eslint-disable-next-line vue/no-mutating-props
119125
this.answer.local = false
120126
const newAnswer = await this.debounceCreateAnswer(answer)
121-
122127
// Forward changes, but use current answer.text to avoid erasing
123128
// any in-between changes while creating the answer
124-
Object.assign(newAnswer, { text: this.$refs.input.value })
129+
Object.assign(newAnswer, { text: answer.text })
125130
this.$emit('update:answer', answer.id, newAnswer)
126131
} else {
127132
this.debounceUpdateAnswer(answer)
@@ -221,6 +226,7 @@ export default {
221226
width: 100%;
222227
position: relative;
223228
margin-right: 2px !important;
229+
resize: none;
224230
225231
&--shifted {
226232
left: -30px;

src/components/Questions/QuestionMultiple.vue

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
:max-option-length="maxStringLengths.optionText"
6767
@add="addNewEntry"
6868
@delete="deleteOption"
69-
@update:answer="updateAnswer" />
69+
@update:answer="updateAnswer"
70+
@multiple-answers="handleMultipleOptions" />
7071
</template>
7172

7273
<li v-if="(edit && !isLastEmpty) || hasNoAnswer" class="question__item">
@@ -180,7 +181,58 @@ export default {
180181
// Radio: create array
181182
this.$emit('update:values', [this.questionValues])
182183
},
184+
async handleMultipleOptions(answers, answer) {
185+
this.edit = true
186+
const options = this.options.slice()
187+
if (answer.local) {
188+
await axios.post(generateOcsUrl('apps/forms/api/v2/option'), {
189+
questionId: this.id,
190+
text: answers[0],
191+
})
192+
answer.local = false
193+
194+
} else {
195+
try {
196+
await axios.post(generateOcsUrl('apps/forms/api/v2/option/update'), {
197+
id: answer.id,
198+
keyValuePairs: {
199+
text: answers[0],
200+
},
201+
})
202+
logger.debug('Updated answer', { answer })
203+
} catch (error) {
204+
logger.error('Error while saving answer', { answer, error })
205+
showError(t('forms', 'Error while saving the answer'))
206+
}
207+
}
208+
answer.text = answers[0]
209+
const answerIndex = options.findIndex(option => option.id === answer.id)
210+
options[answerIndex] = answer
211+
for (let i = 1; i < answers.length; i++) {
212+
213+
options.push({
214+
id: GenRandomId(),
215+
questionId: this.id,
216+
text: answers[i],
217+
local: false,
218+
})
183219
220+
await axios.post(generateOcsUrl('apps/forms/api/v2/option'), {
221+
questionId: this.id,
222+
text: answers[i],
223+
})
224+
}
225+
options.push({
226+
id: GenRandomId(),
227+
questionId: this.id,
228+
text: '',
229+
local: true,
230+
})
231+
this.updateOptions(options)
232+
this.$nextTick(() => {
233+
this.focusIndex(options.length - 1)
234+
})
235+
},
184236
/**
185237
* Is the provided answer required ?
186238
* This is needed for checkboxes as html5
@@ -244,7 +296,6 @@ export default {
244296
text: '',
245297
local: true,
246298
})
247-
248299
// Update question
249300
this.updateOptions(options)
250301

0 commit comments

Comments
 (0)