Skip to content

Commit

Permalink
refactor: Refactors match types (#244)
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
ParisaTork committed Nov 10, 2022
1 parent b24daa1 commit cbeb4d4
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 47 deletions.
6 changes: 3 additions & 3 deletions src/ts/components/FilterResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
} from "../utils/decoration";

const filterOrder = Object.values([
MatchType.CORRECT,
MatchType.DEFAULT,
MatchType.HAS_REPLACEMENT
MatchType.OK,
MatchType.REVIEW,
MatchType.AMEND
]);

interface IProps {
Expand Down
6 changes: 3 additions & 3 deletions src/ts/components/SidebarMatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const getSidebarMatchStyles = (
: "";

switch (matchType) {
case MatchType.CORRECT:
case MatchType.OK:
return css`
&:after {
position: absolute;
Expand All @@ -53,7 +53,7 @@ export const getSidebarMatchStyles = (
background-size: 2px 5px;
}
`;
case MatchType.DEFAULT:
case MatchType.REVIEW:
return css`
&:after {
position: absolute;
Expand All @@ -67,7 +67,7 @@ export const getSidebarMatchStyles = (
background-image: url('${getSquiggleAsUri(color, 'VERTICAL')}');
}
`;
case MatchType.HAS_REPLACEMENT:
case MatchType.AMEND:
return css`
border-left: 2px solid ${color};
`;
Expand Down
6 changes: 3 additions & 3 deletions src/ts/components/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ export const tickIcon = (colour: string) => (
);

export const iconMap = {
CORRECT: {
OK: {
icon: tickIcon(neutral[100]),
description: "OK",
tooltip: "Typerighter thinks these matches are correct."
},
HAS_REPLACEMENT: {
AMEND: {
icon: warningIcon(neutral[100]),
description: "Amend",
tooltip: "Typerighter doesn’t think these matches are correct."
},
DEFAULT: {
REVIEW: {
icon: infoIcon(neutral[100]),
description: "Review",
tooltip: "Typerighter can’t figure out whether these matches are correct or not. They might be worth reviewing."
Expand Down
4 changes: 2 additions & 2 deletions src/ts/state/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ export const selectHasMatches = (state: IPluginState): boolean =>

const getSortOrderForMatchType = (match: IMatch) => {
const matchType = getMatchType(match);
if (matchType === MatchType.HAS_REPLACEMENT) {
if (matchType === MatchType.AMEND) {
return 0;
} else if (matchType === MatchType.CORRECT) {
} else if (matchType === MatchType.OK) {
return 2;
} else {
return 1;
Expand Down
14 changes: 7 additions & 7 deletions src/ts/state/test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("State helpers", () => {
});
it("should report stale when the filter state changes", () => {
const oldFilterState = [] as MatchType[];
const newFilterState = [MatchType.CORRECT];
const newFilterState = [MatchType.OK];
const matches = [] as IMatch[];
const { state: oldState } = getState(matches, oldFilterState);
const { state: newState } = getState(matches, newFilterState);
Expand Down Expand Up @@ -108,7 +108,7 @@ describe("State helpers", () => {
});
it("should remove matches when they don't pass the filter", () => {
const matches = [createMatch(1, 4), createMatch(4, 7)];
const { tr, state } = getState(matches, [MatchType.DEFAULT]);
const { tr, state } = getState(matches, [MatchType.REVIEW]);
const {
currentMatches,
filteredMatches,
Expand All @@ -125,7 +125,7 @@ describe("State helpers", () => {
});

it("should not touch non-match-related decorations", () => {
const { tr, state } = getState([], [MatchType.DEFAULT]);
const { tr, state } = getState([], [MatchType.REVIEW]);
const debugDecos = DecorationSet.create(tr.doc, [
createDebugDecorationFromRange({ from: 0, to: 1 })
]);
Expand All @@ -149,7 +149,7 @@ describe("State helpers", () => {
const deleteRange = 1;
const deleteFrom = 2;
const matches = [createMatch(1, 4), createMatch(4, 7)];
const { tr, state } = getState(matches, [MatchType.DEFAULT]);
const { tr, state } = getState(matches, [MatchType.REVIEW]);

tr.delete(deleteFrom, deleteFrom + deleteRange);
const newState = getNewStateFromTransaction(tr, state);
Expand All @@ -165,7 +165,7 @@ describe("State helpers", () => {
it("should map dirtied ranges through the transaction mapping", () => {
const deleteRange = 1;
const deleteFrom = 2;
const { tr, state } = getState([], [MatchType.DEFAULT]);
const { tr, state } = getState([], [MatchType.REVIEW]);
const dirtiedRange = { from: 0, to: 4 };
const initState = {
...state,
Expand All @@ -184,7 +184,7 @@ describe("State helpers", () => {
it("should add mapping to the requests in flight", () => {
const deleteRange = 1;
const deleteFrom = 2;
const { tr, state } = getState([], [MatchType.DEFAULT]);
const { tr, state } = getState([], [MatchType.REVIEW]);
const initState = {
...state,
requestsInFlight: createRequestInFlight([
Expand All @@ -203,7 +203,7 @@ describe("State helpers", () => {
it("should map requestsInFlight blocks through the incoming transaction mapping", () => {
const deleteRange = 1;
const deleteFrom = 2;
const { tr, state } = getState([], [MatchType.DEFAULT]);
const { tr, state } = getState([], [MatchType.REVIEW]);
const requestsInFlight = createRequestInFlight([
createBlock(1, 23, "Example text to check")
]);
Expand Down
4 changes: 2 additions & 2 deletions src/ts/test/createTyperighterPlugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe("createTyperighterPlugin", () => {
describe("filtering matchers", () => {
const filterOptions = {
filterMatches: filterByMatchState,
initialFilterState: [MatchType.CORRECT]
initialFilterState: [MatchType.OK]
};
it("should filter matches with the supplied predicate when the plugin initialises – remove matches", () => {
const correctMatches = [{ ...createMatch(1), markAsCorrect: true }];
Expand Down Expand Up @@ -270,7 +270,7 @@ describe("createTyperighterPlugin", () => {
filterOptions
});

commands.setFilterState([MatchType.DEFAULT]);
commands.setFilterState([MatchType.REVIEW]);

const decorationSpecs = getDecorationSpecsFromDoc(view);
const decorationsSpecsToExpect = getDecorationSpecs(
Expand Down
42 changes: 21 additions & 21 deletions src/ts/utils/decoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { IRange, IMatch } from "../interfaces/IMatch";
import { getSquiggleAsUri } from "./squiggle";

export enum MatchType {
HAS_REPLACEMENT = "HAS_REPLACEMENT",
DEFAULT = "DEFAULT",
CORRECT = "CORRECT"
AMEND = "AMEND",
REVIEW = "REVIEW",
OK = "OK"
}

export interface IMatchTypeToColourMap {
Expand Down Expand Up @@ -42,9 +42,9 @@ export const DecorationClassMap = {
[DECORATION_MATCH]: "MatchDecoration",
[DECORATION_MATCH_HEIGHT_MARKER]: "MatchDecoration__height-marker",
[DECORATION_MATCH_IS_SELECTED]: "MatchDecoration--is-selected",
[MatchType.CORRECT]: "MatchDecoration--is-correct",
[MatchType.DEFAULT]: "MatchDecoration--default",
[MatchType.HAS_REPLACEMENT]: "MatchDecoration--has-replacement"
[MatchType.OK]: "MatchDecoration--is-correct",
[MatchType.REVIEW]: "MatchDecoration--default",
[MatchType.AMEND]: "MatchDecoration--has-replacement"
};

export const DECORATION_ATTRIBUTE_ID = "data-match-id";
Expand Down Expand Up @@ -147,12 +147,12 @@ export const createDecorationSpecFromMatch = (match: IMatch) => ({

export const getMatchType = (match: IMatch): MatchType => {
if (match.markAsCorrect) {
return MatchType.CORRECT;
return MatchType.OK;
}
if (match.replacement) {
return MatchType.HAS_REPLACEMENT;
return MatchType.AMEND;
}
return MatchType.DEFAULT;
return MatchType.REVIEW;
};

export const getColourForMatch = (
Expand Down Expand Up @@ -183,13 +183,13 @@ export const getColourForMatchType = (
const backgroundOpacitySelected = "50";
const backgroundOpacity = "07";
switch (matchType) {
case MatchType.CORRECT:
case MatchType.OK:
return {
backgroundColour: `${matchColours.correct}${backgroundOpacity}`,
backgroundColourSelected: `${matchColours.correct}${backgroundOpacitySelected}`,
borderColour: `${matchColours.correct}${matchColours.correctOpacity}`
};
case MatchType.HAS_REPLACEMENT:
case MatchType.AMEND:
return {
backgroundColour: `${matchColours.hasSuggestion}${backgroundOpacity}`,
backgroundColourSelected: `${matchColours.hasSuggestion}${backgroundOpacitySelected}`,
Expand Down Expand Up @@ -273,20 +273,20 @@ export const GLOBAL_DECORATION_STYLE_ID = "prosemirror-typerighter-global-styles
export const createGlobalDecorationStyleTag = (
matchColours: IMatchTypeToColourMap
): HTMLStyleElement => {
const correctColours = getColourForMatchType(MatchType.CORRECT, matchColours);
const hasReplacementColours = getColourForMatchType(MatchType.HAS_REPLACEMENT, matchColours);
const defaultColours = getColourForMatchType(MatchType.DEFAULT, matchColours);
const correctColours = getColourForMatchType(MatchType.OK, matchColours);
const hasReplacementColours = getColourForMatchType(MatchType.AMEND, matchColours);
const defaultColours = getColourForMatchType(MatchType.REVIEW, matchColours);
const styleContent = `
.${DecorationClassMap.HAS_REPLACEMENT} {
.${DecorationClassMap.AMEND} {
background-color: ${hasReplacementColours.backgroundColour};
border-bottom: 2px solid ${hasReplacementColours.borderColour};
}
.${DecorationClassMap.HAS_REPLACEMENT}.MatchDecoration--is-selected {
.${DecorationClassMap.AMEND}.MatchDecoration--is-selected {
background-color: ${hasReplacementColours.backgroundColourSelected};
}
.${DecorationClassMap.DEFAULT} {
.${DecorationClassMap.REVIEW} {
position: relative;
background-color: ${defaultColours.backgroundColour};
border-image-source: url('${getSquiggleAsUri(defaultColours.borderColour)}');
Expand All @@ -297,12 +297,12 @@ export const createGlobalDecorationStyleTag = (
border-width: 0 0 2px 0;
}
.${DecorationClassMap.DEFAULT}.MatchDecoration--is-selected {
.${DecorationClassMap.REVIEW}.MatchDecoration--is-selected {
background-color: ${defaultColours.backgroundColourSelected};
border-image-source: linear-gradient(0deg, ${defaultColours.backgroundColourSelected} 3px, transparent 3px), url('${getSquiggleAsUri(defaultColours.borderColour)}');
}
.${DecorationClassMap.CORRECT} {
.${DecorationClassMap.OK} {
position: relative;
box-decoration-break: clone;
-webkit-box-decoration-break: clone;
Expand All @@ -314,8 +314,8 @@ export const createGlobalDecorationStyleTag = (
border-width: 0 0 2px 0;
}
.${DecorationClassMap.CORRECT}.MatchDecoration--is-selected,
.${DecorationClassMap.CORRECT}.MatchDecoration--is-selected:after {
.${DecorationClassMap.OK}.MatchDecoration--is-selected,
.${DecorationClassMap.OK}.MatchDecoration--is-selected:after {
background-color: ${correctColours.backgroundColourSelected};
}
`;
Expand Down
12 changes: 6 additions & 6 deletions src/ts/utils/test/decoration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ describe("Decoration utils", () => {
});
describe("getMatchType", () => {
const defaultMatch = createMatch(0, 5);
it("gives a MatchType of CORRECT when markAsCorrect is set", () => {
it("gives a MatchType of OK when markAsCorrect is set", () => {
const match = { ...defaultMatch, markAsCorrect: true };
expect(getMatchType(match)).toBe(MatchType.CORRECT);
expect(getMatchType(match)).toBe(MatchType.OK);
});
it("gives a matchType of HAS_REPLACEMENT when a replacement is available", () => {
it("gives a matchType of AMEND when a replacement is available", () => {
const match = {
...defaultMatch,
replacement: { text: "u r wrong", type: "TEXT_SUGGESTION" as const }
};
expect(getMatchType(match)).toBe(MatchType.HAS_REPLACEMENT);
expect(getMatchType(match)).toBe(MatchType.AMEND);
});
it("gives a match type of DEFAULT when none of the above apply", () => {
expect(getMatchType(defaultMatch)).toBe(MatchType.DEFAULT);
it("gives a match type of REVIEW when none of the above apply", () => {
expect(getMatchType(defaultMatch)).toBe(MatchType.REVIEW);
});
});
});

0 comments on commit cbeb4d4

Please sign in to comment.