From 4f2478c8e5caacad153b4347376bc8a6bc0a32ab Mon Sep 17 00:00:00 2001 From: Mawoka Date: Sat, 17 Sep 2022 21:13:48 +0200 Subject: [PATCH] :sparkles: Added custom color for buttons --- classquiz/db/models.py | 1 + frontend/src/lib/editor/ABCDEditorPart.svelte | 22 ++++++++++++++----- frontend/src/lib/helpers.ts | 16 ++++++++++++++ frontend/src/lib/play/question.svelte | 4 +++- frontend/src/lib/quiz_types.ts | 1 + 5 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 frontend/src/lib/helpers.ts diff --git a/classquiz/db/models.py b/classquiz/db/models.py index 8ea1a7e..6482e41 100644 --- a/classquiz/db/models.py +++ b/classquiz/db/models.py @@ -65,6 +65,7 @@ class UserSession(ormar.Model): class ABCDQuizAnswer(BaseModel): right: bool answer: str + color: str | None class RangeQuizAnswer(BaseModel): diff --git a/frontend/src/lib/editor/ABCDEditorPart.svelte b/frontend/src/lib/editor/ABCDEditorPart.svelte index f847cd8..44a4e03 100644 --- a/frontend/src/lib/editor/ABCDEditorPart.svelte +++ b/frontend/src/lib/editor/ABCDEditorPart.svelte @@ -9,6 +9,7 @@ import { reach } from 'yup'; import { ABCDQuestionSchema } from '$lib/yupSchemas'; import { getLocalization } from '$lib/i18n'; + import { invertColor } from '$lib/helpers'; const { t } = getLocalization(); @@ -32,11 +33,6 @@ {#if Array.isArray(data.questions[selected_question].answers)} {#each data.questions[selected_question].answers as answer, index}
{ - data.questions[selected_question].answers.splice(index, 1); - data.questions[selected_question].answers = - data.questions[selected_question].answers; - }} out:fade={{ duration: 150 }} class="p-4 rounded-lg flex justify-center w-full transition" class:bg-red-500={!answer.right} @@ -48,7 +44,13 @@ { + data.questions[selected_question].answers.splice(index, 1); + data.questions[selected_question].answers = + data.questions[selected_question].answers; + }} + class="border-b-2 border-dotted w-5/6 text-center rounded-lg" + style="background-color: {answer.color ?? 'transparent'}" placeholder="Empty..." /> + { + answer.color = null; + }} + />
{/each} {/if} diff --git a/frontend/src/lib/helpers.ts b/frontend/src/lib/helpers.ts new file mode 100644 index 0000000..975662a --- /dev/null +++ b/frontend/src/lib/helpers.ts @@ -0,0 +1,16 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +export const invertColor = (hexTripletColor: string): string => { + let color = hexTripletColor; + color = color.substring(1); // remove # + let color_int = parseInt(color, 16); // convert to integer + color_int = 0xffffff ^ color_int; // invert three bytes + color = color_int.toString(16); // convert to hex + color = ('000000' + color).slice(-6); // pad with leading zeros + color = '#' + color; // prepend # + return color; +}; diff --git a/frontend/src/lib/play/question.svelte b/frontend/src/lib/play/question.svelte index 718f803..5db06f6 100644 --- a/frontend/src/lib/play/question.svelte +++ b/frontend/src/lib/play/question.svelte @@ -9,6 +9,7 @@ import { socket } from '$lib/socket'; import Spinner from '../Spinner.svelte'; import { getLocalization } from '$lib/i18n'; + import { invertColor } from '$lib/helpers'; const { t } = getLocalization(); @@ -86,7 +87,8 @@
{#each question.answers as answer} diff --git a/frontend/src/lib/quiz_types.ts b/frontend/src/lib/quiz_types.ts index 586d369..e158fe7 100644 --- a/frontend/src/lib/quiz_types.ts +++ b/frontend/src/lib/quiz_types.ts @@ -38,6 +38,7 @@ export interface Question { export interface Answer { right: boolean; answer: string; + color?: string; } export interface EditorData {