From 86591cd69b62235a001d4840068abb6ccbda9ba9 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Tue, 15 Feb 2022 17:42:37 +0100 Subject: [PATCH] Change content block rendering of single list items, refactor code --- .../read-only/assignment.student.spec.js | 2 +- client/src/components/ContentBlock.vue | 45 +--- .../content-block-form/ContentBlockForm.vue | 1 + .../content-blocks/ContentListBlock.vue | 3 +- client/tests/unit/content-block.spec.js | 240 ------------------ 5 files changed, 8 insertions(+), 283 deletions(-) delete mode 100644 client/tests/unit/content-block.spec.js diff --git a/client/cypress/integration/frontend/read-only/assignment.student.spec.js b/client/cypress/integration/frontend/read-only/assignment.student.spec.js index 28b9ee21..3d27c261 100644 --- a/client/cypress/integration/frontend/read-only/assignment.student.spec.js +++ b/client/cypress/integration/frontend/read-only/assignment.student.spec.js @@ -62,7 +62,7 @@ const getOperations = ({final, readOnly, classReadOnly = false}) => ({ describe('Assignments read-only - Student', () => { beforeEach(() => { cy.setup(); - }); +}); it('can edit and turn in', () => { cy.mockGraphqlOps({ diff --git a/client/src/components/ContentBlock.vue b/client/src/components/ContentBlock.vue index 2e6c3d8b..293c7c4a 100644 --- a/client/src/components/ContentBlock.vue +++ b/client/src/components/ContentBlock.vue @@ -155,34 +155,21 @@ content_list: [content_list_item: [contents...], content_list_item: [contents...]], text_block } - if there's only a single content_list_item it should not be displayed as list like so - { - text_block, - content_list_item: [text_block, image_block], - } becomes - { - text_block, - text_block, - image_block - } */ let contentList = []; - let startingIndex = 0; let newContent = this.contentBlock.contents.reduce((newContents, content, index) => { // collect content_list_items if (content.type === 'content_list_item') { contentList = [...contentList, content]; if (index === this.contentBlock.contents.length - 1) { // content is last element of contents array - let updatedContent = [...newContents, ...this.createContentListOrBlocks(contentList, startingIndex)]; - startingIndex = this.updateStartingIndex(startingIndex, contentList); + let updatedContent = [...newContents, ...this.createContentListOrBlocks(contentList)]; return updatedContent; } return newContents; } else { // handle all other items and reset current content_list if necessary if (contentList.length !== 0) { - newContents = [...newContents, ...this.createContentListOrBlocks(contentList, startingIndex), content]; - startingIndex = this.updateStartingIndex(startingIndex, contentList); + newContents = [...newContents, ...this.createContentListOrBlocks(contentList), content]; contentList = []; return newContents; } else { @@ -191,7 +178,7 @@ } }, []); return Object.assign({}, this.contentBlock, { - contents: this.removeSingleContentListItem(newContent, startingIndex) + contents: newContent }); }, hidden() { @@ -246,35 +233,13 @@ } }); }, - createContentListOrBlocks(contentList, startingIndex) { + createContentListOrBlocks(contentList) { return [{ type: 'content_list', contents: contentList, - id: contentList[0].id, - startingIndex + id: contentList[0].id }]; }, - updateStartingIndex(startingIndex, contentList) { - return contentList.length > 0 ? startingIndex + contentList.length : startingIndex; - }, - removeSingleContentListItem(content, index) { - // just handle the case where we have one contentlistItem ( no index like "a)"" will be shown) - - if (index === 0) { - return content; - } - - const contentLists = content.filter(contentItem => contentItem.type === 'content_list'); - if (contentLists.length !== 1) { - return content; - } - - const listIndex = content.findIndex(contentItem => contentItem.type === 'content_list'); - if (content[listIndex].contents.length !== 1) { - return content; - } - return [...content.slice(0, listIndex), ...content[listIndex].contents[0].value, ...content.slice(listIndex + 1)]; - } } }; diff --git a/client/src/components/content-block-form/ContentBlockForm.vue b/client/src/components/content-block-form/ContentBlockForm.vue index 7fa553f1..d8f2504d 100644 --- a/client/src/components/content-block-form/ContentBlockForm.vue +++ b/client/src/components/content-block-form/ContentBlockForm.vue @@ -126,6 +126,7 @@ import {CHOOSER, transformInnerContents} from '@/components/content-block-form/helpers'; import ContentElementActions from '@/components/content-block-form/ContentElementActions'; + // TODO: refactor this file, it's huuuuuge! export default { props: { diff --git a/client/src/components/content-blocks/ContentListBlock.vue b/client/src/components/content-blocks/ContentListBlock.vue index 5738e09b..ab0e5de5 100644 --- a/client/src/components/content-blocks/ContentListBlock.vue +++ b/client/src/components/content-blocks/ContentListBlock.vue @@ -1,7 +1,6 @@