From 6185e9181f4a35094c3e061bfed60c74225686a3 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Tue, 24 Nov 2026 10:39:43 +0100 Subject: [PATCH] feat(section): reorder sections in a dialog "Move section" in the menu of a section opens a dialog listing the sections in their order, as in Google Forms. Up and down buttons change the order there, and saving moves each section with its questions in one reorder request; cancelling keeps the form as it was. Questions before the first section stay first. Section cards have no drag handle or move buttons any more, so a section is never dragged alone away from its questions. Jumps that the new order would turn backward or into another section are handled as for any reorder. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- playwright/e2e/sections.spec.ts | 167 +++++++++++++ src/components/Questions/Question.vue | 14 +- src/components/ReorderSectionsDialog.vue | 306 +++++++++++++++++++++++ src/composables/useQuestion.ts | 2 + src/utils/Sections.ts | 33 +++ src/views/Create.vue | 148 +++++++++-- 6 files changed, 648 insertions(+), 22 deletions(-) create mode 100644 src/components/ReorderSectionsDialog.vue diff --git a/playwright/e2e/sections.spec.ts b/playwright/e2e/sections.spec.ts index d48ace861..833c27201 100644 --- a/playwright/e2e/sections.spec.ts +++ b/playwright/e2e/sections.spec.ts @@ -9,6 +9,7 @@ import { expect, mergeTests } from '@playwright/test' import { getQuestionIdsInSections, isEmptySection, + reorderSections, } from '../../src/utils/Sections.ts' import { test as randomUserTest } from '../support/fixtures/random-user.ts' import { test as submitTest } from '../support/fixtures/submit.ts' @@ -623,6 +624,172 @@ test.describe('Sections', () => { expect(questions).toEqual([]) }) + test('Sections are reordered with their questions', () => { + const items = [ + { id: 1, type: 'short' }, + { id: 2, type: 'section' }, + { id: 3, type: 'short' }, + { id: 4, type: 'section' }, + { id: 5, type: 'short' }, + { id: 6, type: 'short' }, + { id: 7, type: 'section' }, + ] + const ids = (questions: typeof items) => questions.map(({ id }) => id) + expect(ids(reorderSections(items, [4, 7, 2]))).toEqual([1, 4, 5, 6, 7, 2, 3]) + expect(ids(reorderSections(items, [7, 2]))).toEqual([1, 7, 2, 3, 4, 5, 6]) + expect(ids(reorderSections(items.slice(0, 1), [2]))).toEqual([1]) + }) + + test('The reorder sections dialog moves sections with their questions on save, by button or by dragging', async ({ + page, + }) => { + const form = await api(page.request, 'POST', '/forms') + const ids: number[] = [] + for (const [type, text] of [ + ['short', 'Intro'], + ['section', 'Part one'], + ['short', 'First'], + ['section', 'Part two'], + ['short', 'Second'], + ['short', 'Third'], + ]) { + ids.push( + ( + await api(page.request, 'POST', `/forms/${form.id}/questions`, { + type, + text, + }) + ).id, + ) + } + await page.goto(`apps/forms/${form.hash}/edit`) + + const firstSection = page.getByRole('listitem', { name: 'Section number 1' }) + await expect( + firstSection.getByRole('button', { name: /^Move/ }), + ).toHaveCount(0) + await firstSection + .getByRole('button', { name: 'Actions', exact: true }) + .click() + await page.getByRole('menuitem', { name: 'Move section' }).click() + + const dialog = page.getByRole('dialog', { name: 'Reorder sections' }) + const items = dialog.getByRole('listitem') + await expect(items).toHaveText([ + /Part one\s*Section 1 of 2/, + /Part two\s*Section 2 of 2/, + ]) + const save = dialog.getByRole('button', { name: 'Save' }) + await expect(save).toBeDisabled() + await expect( + dialog.getByRole('button', { name: 'Move section Part one up' }), + ).toBeDisabled() + + const down = dialog.getByRole('button', { + name: 'Move section Part one down', + }) + await down.click() + await expect(items).toHaveText([/Part two/, /Part one/]) + await expect( + dialog.getByRole('button', { name: 'Move section Part one up' }), + ).toBeFocused() + await dialog.getByRole('button', { name: 'Cancel' }).click() + await expect(dialog).toHaveCount(0) + await expect( + firstSection.getByRole('textbox', { name: 'Title of section number 1' }), + ).toHaveValue('Part one') + + await firstSection + .getByRole('button', { name: 'Actions', exact: true }) + .click() + await page.getByRole('menuitem', { name: 'Move section' }).click() + await expect(items).toHaveText([/Part one/, /Part two/]) + await items + .nth(1) + .locator('.reorder-sections__handle') + .dragTo(items.nth(0), { targetPosition: { x: 20, y: 5 } }) + await expect(items).toHaveText([/Part two/, /Part one/]) + const order = page.waitForResponse( + (response) => + response.request().method() === 'PATCH' + && response.url().endsWith(`/forms/${form.id}/questions`), + ) + await save.click() + await order + + const { questions } = await api(page.request, 'GET', `/forms/${form.id}`) + expect(questions.map(({ id }: { id: number }) => id)).toEqual([ + ids[0], + ids[3], + ids[4], + ids[5], + ids[1], + ids[2], + ]) + await expect( + page.getByRole('textbox', { name: 'Title of section number 1' }), + ).toHaveValue('Part two') + }) + + test('Reordering sections names the moved section when a jump would go back', async ({ + page, + }) => { + const form = await api(page.request, 'POST', '/forms') + const ids: Record = {} + for (const [type, text] of [ + ['section', 'Part one'], + ['multiple_unique', 'Skip ahead?'], + ['section', 'Part two'], + ['short', 'Second'], + ]) { + ids[text] = ( + await api(page.request, 'POST', `/forms/${form.id}/questions`, { + type, + text, + }) + ).id + } + const options = await api( + page.request, + 'POST', + `/forms/${form.id}/questions/${ids['Skip ahead?']}/options`, + { optionTexts: ['Yes', 'No'] }, + ) + await api( + page.request, + 'PATCH', + `/forms/${form.id}/questions/${ids['Skip ahead?']}`, + { + keyValuePairs: { + extraSettings: { + goTo: [{ optionId: options[0].id, target: ids['Part two'] }], + }, + }, + }, + ) + await page.goto(`apps/forms/${form.hash}/edit`) + + await page + .getByRole('listitem', { name: 'Section number 1' }) + .getByRole('button', { name: 'Actions', exact: true }) + .click() + await page.getByRole('menuitem', { name: 'Move section' }).click() + const reorder = page.getByRole('dialog', { name: 'Reorder sections' }) + await reorder + .getByRole('button', { name: 'Move section Part two up' }) + .click() + await reorder.getByRole('button', { name: 'Save' }).click() + + const dialog = page.getByRole('dialog', { name: 'Jumps cannot go back' }) + await expect(dialog).toContainText( + 'Move the section "Part two" back, or remove these jumps?', + ) + await dialog.getByRole('button', { name: 'Move back' }).click() + await expect( + page.getByRole('textbox', { name: 'Title of section number 1' }), + ).toHaveValue('Part one') + }) + test('Each page checks its required questions, an empty section is an info page', async ({ page, submitView, diff --git a/src/components/Questions/Question.vue b/src/components/Questions/Question.vue index d34aa030b..0d386c8ba 100644 --- a/src/components/Questions/Question.vue +++ b/src/components/Questions/Question.vue @@ -21,7 +21,7 @@
+ + + {{ t('forms', 'Move section') }} + + + + + + + diff --git a/src/composables/useQuestion.ts b/src/composables/useQuestion.ts index 0417ce91e..3dd584dae 100644 --- a/src/composables/useQuestion.ts +++ b/src/composables/useQuestion.ts @@ -31,6 +31,7 @@ export const QUESTION_EMITS = [ 'clone', 'moveDown', 'moveUp', + 'moveSection', ] export const QUESTION_PROPS = { @@ -445,6 +446,7 @@ export function useQuestion(props: QuestionPropsLike, options: UseQuestionOption 'update:name': onNameChange, moveDown: (...args: unknown[]) => options.emit('moveDown', ...args), moveUp: (...args: unknown[]) => options.emit('moveUp', ...args), + moveSection: () => options.emit('moveSection'), } }) diff --git a/src/utils/Sections.ts b/src/utils/Sections.ts index 382d116f8..590364adf 100644 --- a/src/utils/Sections.ts +++ b/src/utils/Sections.ts @@ -122,6 +122,39 @@ export function isAllowedTarget( ) } +/** + * The questions with the sections in a new order, each section followed by its questions; questions before the first section stay first, sections missing from the order keep their place after the others + * + * @param questions the questions and sections in their order + * @param sectionIds the ids of the sections in their new order + */ +export function reorderSections( + questions: T[], + sectionIds: number[], +): T[] { + const first = questions.findIndex( + (question) => question.type === QUESTION_TYPE_SECTION, + ) + if (first === -1) { + return questions + } + const blocks: T[][] = [] + for (const question of questions.slice(first)) { + if (question.type === QUESTION_TYPE_SECTION) { + blocks.push([]) + } + blocks[blocks.length - 1].push(question) + } + const rank = (block: T[]): number => { + const position = sectionIds.indexOf(block[0].id) + return position === -1 ? sectionIds.length : position + } + return [ + ...questions.slice(0, first), + ...blocks.sort((a, b) => rank(a) - rank(b)).flat(), + ] +} + /** * The pages of the questions on a path: a page starts at each section, questions before the first section form the first page * diff --git a/src/views/Create.vue b/src/views/Create.vue index c80c58dc2..419da8288 100644 --- a/src/views/Create.vue +++ b/src/views/Create.vue @@ -147,11 +147,8 @@ :ref="registerQuestionRef(question)" :class="sectionFrames[index]" v-bind="form.questions[index]" - :canMoveDown=" - !isChangingQuestions - && index < form.questions.length - 1 - " - :canMoveUp="!isChangingQuestions && index > 0" + :canMoveDown="!isChangingQuestions && canMoveDown(index)" + :canMoveUp="!isChangingQuestions && canMoveUp(index)" :answerType="answerTypes[question.type]" :index="questionNumbers.get(question.id)" :maxStringLengths="maxStringLengths" @@ -170,7 +167,8 @@ @clone="cloneQuestion(question, index)" @delete="onDelete(question.id)" @moveDown="onMoveDown(index)" - @moveUp="onMoveUp(index)"> + @moveUp="onMoveUp(index)" + @moveSection="reorderSectionsFrom = question.id"> @@ -277,6 +280,7 @@ import Question from '../components/Questions/Question.vue' import QuestionLong from '../components/Questions/QuestionLong.vue' import QuestionMultiple from '../components/Questions/QuestionMultiple.vue' import QuestionShort from '../components/Questions/QuestionShort.vue' +import ReorderSectionsDialog from '../components/ReorderSectionsDialog.vue' import TopBar from '../components/TopBar.vue' import { FORM_JUMP_ISSUES_KEY, FORM_QUESTIONS_KEY } from '../composables/useJumps.ts' import { queueQuestionRequest } from '../composables/useQuestion.ts' @@ -293,7 +297,11 @@ import { } from '../utils/Jumps.ts' import logger from '../utils/Logger.ts' import OcsResponse2Data from '../utils/OcsResponse2Data.ts' -import { getNumbers, QUESTION_TYPE_SECTION } from '../utils/Sections.ts' +import { + getNumbers, + QUESTION_TYPE_SECTION, + reorderSections, +} from '../utils/Sections.ts' import SetWindowTitle from '../utils/SetWindowTitle.ts' const formsAppName = 'forms' @@ -311,6 +319,7 @@ export default defineComponent({ Draggable, NcIconSvgWrapper, AddQuestionMenu, + ReorderSectionsDialog, NcAppContent, NcEmptyContent, NcLoadingIcon, @@ -411,6 +420,7 @@ export default defineComponent({ const questionMenuOpened = ref(false) const activeQuestionType = ref(null) const questionRefsMap = ref>({}) + const reorderSectionsFrom = ref(null) // when set to a number, the next created question will be inserted at this index const insertMenuOpenedIndex = ref(null) // controls per-question insert menu visibility @@ -765,26 +775,45 @@ export default defineComponent({ } /** - * The closing question of a move confirmation, naming the moved question or section + * The closing question of a move confirmation, naming the moved question, section or sections * - * @param moved the moved question or section + * @param moved the moved question, section or sections */ - const moveBackText = (moved: FormsQuestion | undefined): string => - moved?.type === QUESTION_TYPE_SECTION + const moveBackText = (moved: FormsQuestion[]): string => { + const options = { escape: false, sanitize: false } + if (moved.length > 1) { + return t( + 'forms', + 'Move the sections {sections} back, or remove these jumps?', + { + sections: formatList( + moved.map( + ({ text }) => + `"${text || t('forms', 'Untitled section')}"`, + ), + getLanguage(), + ), + }, + undefined, + options, + ) + } + return moved[0]?.type === QUESTION_TYPE_SECTION ? t( 'forms', 'Move the section "{title}" back, or remove these jumps?', - { title: moved.text || t('forms', 'Untitled section') }, + { title: moved[0].text || t('forms', 'Untitled section') }, undefined, - { escape: false, sanitize: false }, + options, ) : t( 'forms', 'Move the question "{title}" back, or remove these jumps?', - { title: moved?.text || t('forms', 'Untitled question') }, + { title: moved[0]?.text || t('forms', 'Untitled question') }, undefined, - { escape: false, sanitize: false }, + options, ) + } /** * Ask whether to remove the jumps that a new order of the questions would turn into jumps to an earlier question or into the middle of another section, or to move the moved question back @@ -792,14 +821,14 @@ export default defineComponent({ * @param questions the questions in the new order * @param ids the ids of the questions with jumps to an earlier question * @param intoSectionIds the ids of the other questions with jumps into the middle of another section - * @param moved the moved question or section + * @param moved the moved question, section or sections * @return whether to remove the jumps */ const confirmRemovingBackwardJumps = async ( questions: FormsQuestion[], ids: number[], intoSectionIds: number[], - moved: FormsQuestion | undefined, + moved: FormsQuestion[], ): Promise => { try { if (ids.length === 0) { @@ -868,11 +897,11 @@ export default defineComponent({ * Reorder questions on dragEnd * * @param questions the freshly reordered questions (avoids reading the not-yet-updated `form` prop) - * @param moved the question the user moved + * @param moved the question, section or sections the user moved */ const onQuestionOrderChange = async ( questions: FormsQuestion[], - moved?: FormsQuestion, + moved: FormsQuestion[] = [], ): Promise => { const formId = props.form.id const oldQuestions = props.form.questions @@ -1000,7 +1029,78 @@ export default defineComponent({ questions[index - 1], ] emit('update:form', { ...props.form, questions }) - onQuestionOrderChange(questions, moved) + onQuestionOrderChange(questions, [moved]) + } + + /** + * Whether the question at a position can move up; a section, whether another section is before it + * + * @param index the position of the question + */ + const canMoveUp = (index: number): boolean => + props.form.questions[index]?.type === QUESTION_TYPE_SECTION + ? props.form.questions + .slice(0, index) + .some((question) => question.type === QUESTION_TYPE_SECTION) + : index > 0 + + /** + * Whether the question at a position can move down; a section, whether another section is after it + * + * @param index the position of the question + */ + const canMoveDown = (index: number): boolean => + props.form.questions[index]?.type === QUESTION_TYPE_SECTION + ? props.form.questions + .slice(index + 1) + .some((question) => question.type === QUESTION_TYPE_SECTION) + : index < props.form.questions.length - 1 + + const sections = computed(() => + props.form.questions.filter( + (question) => question.type === QUESTION_TYPE_SECTION, + ), + ) + + /** + * Focus the title of a question or section + * + * @param id the id of the question or section + */ + const focusQuestion = (id: number | null): void => { + nextTick(() => { + if (id !== null) { + questionRefsMap.value[id]?.focus?.() + } + }) + } + + /** + * Close the reorder sections dialog and focus the section it was opened from + */ + const closeReorderSections = (): void => { + focusQuestion(reorderSectionsFrom.value) + reorderSectionsFrom.value = null + } + + /** + * Move the sections with their questions into a new order, then focus the section the dialog was opened from + * + * @param sectionIds the ids of the sections in their new order + * @param movedIds the ids of the sections the user moved + */ + const onReorderSections = async ( + sectionIds: number[], + movedIds: number[], + ): Promise => { + const id = reorderSectionsFrom.value + const questions = reorderSections(props.form.questions, sectionIds) + emit('update:form', { ...props.form, questions }) + await onQuestionOrderChange( + questions, + sections.value.filter((section) => movedIds.includes(section.id)), + ) + focusQuestion(id) } const onMoveUp = (index: number): void => { @@ -1332,7 +1432,7 @@ export default defineComponent({ emit('update:form', { ...props.form, questions }) onQuestionOrderChange( questions, - questions.find(({ id }) => id === draggedId), + questions.filter(({ id }) => id === draggedId), ) } @@ -1410,6 +1510,12 @@ export default defineComponent({ resizeTitle, resizeDescription, addQuestion, + canMoveDown, + canMoveUp, + closeReorderSections, + onReorderSections, + reorderSectionsFrom, + sections, onDelete, insertQuestion, cloneQuestion,