Skip to content

Commit

Permalink
fix: respect highlightCompleteColor when finishing quiz (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanind authored Feb 8, 2023
1 parent c0c08b0 commit 99c6f93
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
1 change: 1 addition & 0 deletions demo/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function updateCharacter() {
height: 400,
renderer: 'svg',
radicalColor: '#166E16',
highlightCompleteColor: '#FF0000',
onCorrectStroke: printStrokePoints,
onMistake: printStrokePoints,
showCharacter: false,
Expand Down
2 changes: 0 additions & 2 deletions src/Mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export interface GenericMutation<
cancel(renderState: TRenderStateClass): void;
}

export type MutationChain = GenericMutation<any>[];

class Delay implements GenericMutation {
scope: string;
_runningPromise: Promise<void> | undefined;
Expand Down
7 changes: 5 additions & 2 deletions src/Quiz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as characterActions from './characterActions';
import Character from './models/Character';
import { ParsedHanziWriterOptions, Point, StrokeData } from './typings/types';
import RenderState from './RenderState';
import { MutationChain } from './Mutation';
import { GenericMutation } from './Mutation';

const getDrawnPath = (userStroke: UserStroke) => ({
pathString: geometry.getPathString(userStroke.externalPoints),
Expand Down Expand Up @@ -184,14 +184,16 @@ export default class Quiz {
onComplete,
highlightOnComplete,
strokeFadeDuration,
highlightCompleteColor,
highlightColor,
strokeHighlightDuration,
} = this._options;

onCorrectStroke?.({
...this._getStrokeData({ isCorrect: true, meta }),
});

let animation: MutationChain = characterActions.showStroke(
let animation: GenericMutation[] = characterActions.showStroke(
'main',
this._currentStrokeIndex,
strokeFadeDuration,
Expand All @@ -212,6 +214,7 @@ export default class Quiz {
animation = animation.concat(
quizActions.highlightCompleteChar(
this._character,
colorStringToVals(highlightCompleteColor || highlightColor),
(strokeHighlightDuration || 0) * 2,
),
);
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/Quiz-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Positioner from '../Positioner';
import { resolvePromises } from '../testUtils';
import strokeMatches from '../strokeMatches';
import { Point } from '../typings/types';
import { colorStringToVals } from '../utils';

(Positioner as any).mockImplementation(() => ({
convertExternalPoint: (point: Point) => ({ x: point.x + 5, y: point.y + 5 }),
Expand Down Expand Up @@ -807,6 +808,7 @@ describe('Quiz', () => {
clock.tick(1000);
await resolvePromises();

expect(renderState.state.options.highlightColor).toEqual(colorStringToVals('#0F0'));
expect(renderState.state.character.highlight.opacity).toBe(1);
clock.tick(1000);
await resolvePromises();
Expand Down
24 changes: 12 additions & 12 deletions src/characterActions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Stroke from './models/Stroke';
import { ColorObject, RecursivePartial } from './typings/types';
import Character from './models/Character';
import Mutation, { MutationChain } from './Mutation';
import Mutation, { GenericMutation } from './Mutation';
import { objRepeat } from './utils';
import { CharacterName, CharacterRenderState, RenderStateObject } from './RenderState';

export const showStrokes = (
charName: CharacterName,
character: Character,
duration: number,
): MutationChain => {
): GenericMutation[] => {
return [
new Mutation(
`character.${charName}.strokes`,
Expand All @@ -26,7 +26,7 @@ export const showCharacter = (
charName: CharacterName,
character: Character,
duration: number,
): MutationChain => {
): GenericMutation[] => {
return [
new Mutation(
`character.${charName}`,
Expand All @@ -43,7 +43,7 @@ export const hideCharacter = (
charName: CharacterName,
character: Character,
duration?: number,
): MutationChain => {
): GenericMutation[] => {
return [
new Mutation(`character.${charName}.opacity`, 0, { duration, force: true }),
...showStrokes(charName, character, 0),
Expand All @@ -62,11 +62,11 @@ export const highlightStroke = (
stroke: Stroke,
color: ColorObject | null,
speed: number,
): MutationChain => {
): GenericMutation[] => {
const strokeNum = stroke.strokeNum;
const duration = (stroke.getLength() + 600) / (3 * speed);
return [
new Mutation('character.highlight.strokeColor', color),
new Mutation('options.highlightColor', color),
new Mutation('character.highlight', {
opacity: 1,
strokes: {
Expand All @@ -92,7 +92,7 @@ export const animateStroke = (
charName: CharacterName,
stroke: Stroke,
speed: number,
): MutationChain => {
): GenericMutation[] => {
const strokeNum = stroke.strokeNum;
const duration = (stroke.getLength() + 600) / (3 * speed);
return [
Expand All @@ -116,7 +116,7 @@ export const animateSingleStroke = (
character: Character,
strokeNum: number,
speed: number,
): MutationChain => {
): GenericMutation[] => {
const mutationStateFunc = (state: RenderStateObject) => {
const curCharState = state.character[charName];
const mutationState: RecursivePartial<CharacterRenderState> = {
Expand All @@ -141,7 +141,7 @@ export const showStroke = (
charName: CharacterName,
strokeNum: number,
duration: number,
): MutationChain => {
): GenericMutation[] => {
return [
new Mutation(
`character.${charName}.strokes.${strokeNum}`,
Expand All @@ -160,8 +160,8 @@ export const animateCharacter = (
fadeDuration: number,
speed: number,
delayBetweenStrokes: number,
): MutationChain => {
let mutations: MutationChain = hideCharacter(charName, character, fadeDuration);
): GenericMutation[] => {
let mutations: GenericMutation[] = hideCharacter(charName, character, fadeDuration);
mutations = mutations.concat(showStrokes(charName, character, 0));
mutations.push(
new Mutation(
Expand All @@ -187,7 +187,7 @@ export const animateCharacterLoop = (
speed: number,
delayBetweenStrokes: number,
delayBetweenLoops: number,
): MutationChain => {
): GenericMutation[] => {
const mutations = animateCharacter(
charName,
character,
Expand Down
16 changes: 9 additions & 7 deletions src/quizActions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Mutation, { MutationChain } from './Mutation';
import Mutation, { GenericMutation } from './Mutation';
import * as characterActions from './characterActions';
import { objRepeat, objRepeatCb } from './utils';
import Character from './models/Character';
import { Point } from './typings/types';
import { ColorObject, Point } from './typings/types';

export const startQuiz = (
character: Character,
fadeDuration: number,
startStrokeNum: number,
): MutationChain => {
): GenericMutation[] => {
return [
...characterActions.hideCharacter('main', character, fadeDuration),
new Mutation(
Expand All @@ -32,7 +32,7 @@ export const startQuiz = (
];
};

export const startUserStroke = (id: string | number, point: Point): MutationChain => {
export const startUserStroke = (id: string | number, point: Point): GenericMutation[] => {
return [
new Mutation('quiz.activeUserStrokeId', id, { force: true }),
new Mutation(
Expand All @@ -49,14 +49,14 @@ export const startUserStroke = (id: string | number, point: Point): MutationChai
export const updateUserStroke = (
userStrokeId: string | number,
points: Point[],
): MutationChain => {
): GenericMutation[] => {
return [new Mutation(`userStrokes.${userStrokeId}.points`, points, { force: true })];
};

export const removeUserStroke = (
userStrokeId: string | number,
duration: number,
): MutationChain => {
): GenericMutation[] => {
return [
new Mutation(`userStrokes.${userStrokeId}.opacity`, 0, { duration }),
new Mutation(`userStrokes.${userStrokeId}`, null, { force: true }),
Expand All @@ -65,9 +65,11 @@ export const removeUserStroke = (

export const highlightCompleteChar = (
character: Character,
color: ColorObject | null,
duration: number,
): MutationChain => {
): GenericMutation[] => {
return [
new Mutation('options.highlightColor', color),
...characterActions.hideCharacter('highlight', character),
...characterActions.showCharacter('highlight', character, duration / 2),
...characterActions.hideCharacter('highlight', character, duration / 2),
Expand Down

0 comments on commit 99c6f93

Please sign in to comment.