diff --git a/src/components/equative-explorer/EquativeDisplay.tsx b/src/components/equative-explorer/EquativeDisplay.tsx index 0938b02..83f0d51 100644 --- a/src/components/equative-explorer/EquativeDisplay.tsx +++ b/src/components/equative-explorer/EquativeDisplay.tsx @@ -15,41 +15,120 @@ import { equativeMachine, assembleEquativeOutput, SubjectInput, + PredicateInput, + isParticipleInput, + ParticipleInput, } from "../../lib/equative-machine"; -import { isNoun, isPluralEntry, isUnisexNoun } from "../../lib/type-predicates"; +import { isPluralEntry, isUnisexNoun, isAdjective, isSingularEntry } from "../../lib/type-predicates"; export function chooseLength(o: T.SingleOrLengthOpts, length: "short" | "long"): O { return ("long" in o) ? o[length] : o; } function SingleItemDisplay({ state }: { state: ExplorerState }) { - if (state.subjectType === "pronouns") { + if (state.subject.type === "pronouns") { return
ERROR: Wrong display being used
; } - const entry = state.subjectsSelected[state.subjectType]; - // @ts-ignore - TODO: safer and for use with unisex nouns - const subjInput: SubjectInput = isNoun(entry) ? { - entry, - plural: isPluralEntry(entry) ? true : state.subjectsSelected.info.plural, - ...isUnisexNoun(entry) ? { - gender: state.subjectsSelected.info.gender, - } : {}, - } : entry; + try { + const subjInput = makeSubjectInput(state.subject[state.subject.type], state); + const predInput = makePredicateInput(state.predicate[state.predicate.type], state); + const block = assembleEquativeOutput( + equativeMachine(subjInput, predInput, state.tense) + ); + return
+ +
; + } catch (e) { + console.error(e); + return
Error making equative sentence
+ } +} - const block = assembleEquativeOutput( - equativeMachine(subjInput, state.predicatesSelected[state.predicateType], state.tense) - ); - return
- -
; +function makeSubjectInput(entry: Noun | ParticipleInput | UnisexNoun, state: ExplorerState): SubjectInput { + if (isParticipleInput(entry)) { + return entry; + } + const isUnisex = isUnisexNoun(entry); + if (isUnisex && isSingularEntry(entry)) { + return { + ...state.subject.info, + entry, + }; + } + if (isUnisex && isPluralEntry(entry)) { + return { + ...state.subject.info, + plural: true, + entry, + }; + } + if (isUnisex) { + throw new Error("improper unisex noun"); + } + if (isPluralEntry(entry)) { + return { + plural: true, + entry, + } + } + if (isSingularEntry(entry)) { + return { + entry, + plural: state.subject.info.plural, + }; + } + throw new Error("unable to make subject input from entry"); +} + +function makePredicateInput(entry: Noun | ParticipleInput | UnisexNoun | Adjective, state: ExplorerState): PredicateInput { + if (isParticipleInput(entry) || isAdjective(entry)) { + return entry; + } + const isUnisex = isUnisexNoun(entry); + if (isUnisex && state.subject.type === "pronouns") { + return entry; + } + if (isUnisex && isSingularEntry(entry)) { + return { + ...state.predicate.info, + entry, + }; + } + if (isUnisex && isPluralEntry(entry)) { + return { + ...state.predicate.info, + plural: true, + entry, + }; + } + if (isUnisex) { + throw new Error("improper unisex noun"); + } + if (isPluralEntry(entry)) { + return { + plural: true, + entry, + } + } + if (isSingularEntry(entry)) { + return { + entry, + plural: state.predicate.info.plural, + }; + } + throw new Error("unable to make predicate input from entry"); } function PronounBlockDisplay({ state }: { state: ExplorerState }) { - const block = makeBlockWPronouns(state.predicatesSelected[state.predicateType], state.tense); - return ; + const pred = state.predicate[state.predicate.type]; + if (!isParticipleInput(pred) && (isAdjective(pred) || isUnisexNoun(pred))) { + const block = makeBlockWPronouns(pred, state.tense); + return ; + } + return
Invalid combination
} function EquativeDisplay({ state, dispatch }: { state: ExplorerState, dispatch: (action: ExplorerReducerAction) => void }) { @@ -65,7 +144,7 @@ function EquativeDisplay({ state, dispatch }: { state: ExplorerState, dispatch: handleChange={(p) => dispatch({ type: "setLength", payload: p as "long" | "short" })} /> } - {state.subjectType === "pronouns" + {state.subject.type === "pronouns" ? : } diff --git a/src/components/equative-explorer/EquativeExplorer.tsx b/src/components/equative-explorer/EquativeExplorer.tsx index bb58f3d..267301c 100644 --- a/src/components/equative-explorer/EquativeExplorer.tsx +++ b/src/components/equative-explorer/EquativeExplorer.tsx @@ -22,11 +22,19 @@ import EquativeDisplay from "./EquativeDisplay"; const defaultState: ExplorerState = { tense: "present", length: "short", - predicatesSelected: { + predicate: { + type: "adjective", adjective: defaultAdjective, unisexNoun: defaultUnisexNoun, + participle: defaultParticiple, + noun: defaultNoun, + info: { + plural: false, + gender: "masc", + }, }, - subjectsSelected: { + subject: { + type: "pronouns", noun: defaultNoun, participle: defaultParticiple, unisexNoun: defaultUnisexNoun, @@ -35,8 +43,6 @@ const defaultState: ExplorerState = { gender: "masc", }, }, - predicateType: "adjective", - subjectType: "pronouns", }; function EquativeExplorer() { @@ -46,8 +52,10 @@ function EquativeExplorer() { unsafeSetState(newState); } return <> -
+
+ +
diff --git a/src/components/equative-explorer/explorer-reducer.ts b/src/components/equative-explorer/explorer-reducer.ts index 8980c2b..e00abaa 100644 --- a/src/components/equative-explorer/explorer-reducer.ts +++ b/src/components/equative-explorer/explorer-reducer.ts @@ -3,13 +3,13 @@ import { ExplorerState, ExplorerReducerAction } from "./explorer-types"; export function reducer(state: ExplorerState, action: ExplorerReducerAction): ExplorerState { if (action.type === "setPredicate") { - const pile = inputs[state.predicateType] as (UnisexNoun | Adjective)[]; + const pile = inputs[state.predicate.type] as (UnisexNoun | Adjective)[]; const predicate = (pile.find(p => p.ts === action.payload) || pile[0]); return { ...state, - predicatesSelected: { - ...state.predicatesSelected, - [state.predicateType]: predicate, + predicate: { + ...state.predicate, + [state.predicate.type]: predicate, }, }; } @@ -17,37 +17,46 @@ export function reducer(state: ExplorerState, action: ExplorerReducerAction): Ex const predicateType = action.payload; return { ...state, - predicateType: (predicateType === "unisexNoun" && state.subjectType === "noun") ? "adjective" : predicateType, + predicate: { + ...state.predicate, + type: (predicateType === "unisexNoun" && state.subject.type === "noun") ? "adjective" : predicateType, + }, }; } if (action.type === "setSubjectType") { const subjectType = action.payload; return { ...state, - predicateType: state.predicateType === "unisexNoun" ? "adjective" : state.predicateType, - subjectType, + predicate: { + ...state.predicate, + type: state.predicate.type === "unisexNoun" ? "adjective" : state.predicate.type, + }, + subject: { + ...state.subject, + type: subjectType, + } }; } if (action.type === "setSubject") { - if (state.subjectType === "pronouns") return state; - const pile = inputs[state.subjectType]; + if (state.subject.type === "pronouns") return state; + const pile = inputs[state.subject.type]; // @ts-ignore const subject = (pile.find(p => p.ts === action.payload) || pile[0]); return { ...state, - subjectsSelected: { - ...state.subjectsSelected, - [state.subjectType]: subject, + subject: { + ...state.subject, + [state.subject.type]: subject, }, }; } if (action.type === "setSubjectPlural") { return { ...state, - subjectsSelected: { - ...state.subjectsSelected, + subject: { + ...state.subject, info: { - ...state.subjectsSelected.info, + ...state.subject.info, plural: action.payload, }, }, @@ -56,10 +65,10 @@ export function reducer(state: ExplorerState, action: ExplorerReducerAction): Ex if (action.type === "setSubjectGender") { return { ...state, - subjectsSelected: { - ...state.subjectsSelected, + subject: { + ...state.subject, info: { - ...state.subjectsSelected.info, + ...state.subject.info, gender: action.payload, }, }, diff --git a/src/components/equative-explorer/explorer-selectors.tsx b/src/components/equative-explorer/explorer-selectors.tsx index ef6f082..7ad7d83 100644 --- a/src/components/equative-explorer/explorer-selectors.tsx +++ b/src/components/equative-explorer/explorer-selectors.tsx @@ -30,17 +30,17 @@ export function SubjectSelector({ state, dispatch }: { dispatch({ type: "setSubject", payload: parseInt(value) }); } const pluralNounSelected = ( - state.subjectType === "noun" && isPluralEntry(state.subjectsSelected.noun) + state.subject.type === "noun" && isPluralEntry(state.subject.noun) ); - const options = state.subjectType === "pronouns" + const options = state.subject.type === "pronouns" ? [] - : inputs[state.subjectType].map(e => ({ + : inputs[state.subject.type].map(e => ({ value: e.ts.toString(), label: makeOptionLabel(e), })); - const subject = state.subjectType === "pronouns" + const subject = state.subject.type === "pronouns" ? undefined - : state.subjectsSelected[state.subjectType]; + : state.subject[state.subject.type]; return
@@ -50,7 +50,7 @@ export function SubjectSelector({ state, dispatch }: { name="pronounsSubjectRadio" id="pronounsSubjectRadio" value="pronouns" - checked={state.subjectType === "pronouns"} + checked={state.subject.type === "pronouns"} onChange={onTypeSelect} />
- {state.subjectType !== "pronouns" && + {state.subject.type !== "pronouns" && <>