From 0f375a72b5891b085870cb802f857ccb04bb59e6 Mon Sep 17 00:00:00 2001 From: magonzalez9 Date: Thu, 2 Jan 2020 15:18:03 -0800 Subject: [PATCH] Add types for react-mathquill (#40861) * Add types for react-mathquill * Fix formatting issues * Fix typescript header version * Remove single quote character from typescript version header * Fix typing for function * Add type definitions for @edtr-io/mathquill and update react-mathquill to reflect that. * Add paths property to @edtr-io/mathquill tsconfig * Fix dt-header package version * Update types/react-mathquill/index.d.ts Co-Authored-By: Dmitry Demensky <10235949+demensky@users.noreply.github.com> * Update types/edtr-io__mathquill/edtr-io__mathquill-tests.ts Co-Authored-By: Dmitry Demensky <10235949+demensky@users.noreply.github.com> * Update types/react-mathquill/tsconfig.json Co-Authored-By: Dmitry Demensky <10235949+demensky@users.noreply.github.com> * Update types/react-mathquill/index.d.ts Co-Authored-By: Dmitry Demensky <10235949+demensky@users.noreply.github.com> * Update types/react-mathquill/index.d.ts Co-Authored-By: Dmitry Demensky <10235949+demensky@users.noreply.github.com> * Update types/react-mathquill/index.d.ts Co-Authored-By: Dmitry Demensky <10235949+demensky@users.noreply.github.com> * Update types/react-mathquill/index.d.ts Co-Authored-By: Dmitry Demensky <10235949+demensky@users.noreply.github.com> * Update types/react-mathquill/react-mathquill-tests.tsx Co-Authored-By: Dmitry Demensky <10235949+demensky@users.noreply.github.com> Co-authored-by: Dmitry Demensky <10235949+demensky@users.noreply.github.com> --- .../edtr-io__mathquill-tests.ts | 39 ++++++++++++++ types/edtr-io__mathquill/index.d.ts | 50 +++++++++++++++++ types/edtr-io__mathquill/tsconfig.json | 27 ++++++++++ types/edtr-io__mathquill/tslint.json | 1 + types/react-mathquill/index.d.ts | 21 ++++++++ .../react-mathquill/react-mathquill-tests.tsx | 54 +++++++++++++++++++ types/react-mathquill/tsconfig.json | 27 ++++++++++ types/react-mathquill/tslint.json | 1 + 8 files changed, 220 insertions(+) create mode 100644 types/edtr-io__mathquill/edtr-io__mathquill-tests.ts create mode 100644 types/edtr-io__mathquill/index.d.ts create mode 100644 types/edtr-io__mathquill/tsconfig.json create mode 100644 types/edtr-io__mathquill/tslint.json create mode 100644 types/react-mathquill/index.d.ts create mode 100644 types/react-mathquill/react-mathquill-tests.tsx create mode 100644 types/react-mathquill/tsconfig.json create mode 100644 types/react-mathquill/tslint.json diff --git a/types/edtr-io__mathquill/edtr-io__mathquill-tests.ts b/types/edtr-io__mathquill/edtr-io__mathquill-tests.ts new file mode 100644 index 0000000000..e8bd657de4 --- /dev/null +++ b/types/edtr-io__mathquill/edtr-io__mathquill-tests.ts @@ -0,0 +1,39 @@ +import { MQ, Config } from '@edtr-io/mathquill'; + +const Test = () => { + const config: Config = { + spaceBehavesLikeTab: true, + leftRightIntoCmdGoes: 'up', + autoSubscriptNumerals: true, + maxDepth: 10, + substituteTextarea() { + return document.createElement('textarea'); + }, + restrictMismatchedBrackets: true, + sumStartsWithNEquals: true, + supSubsRequireOperand: true, + charsThatBreakOutOfSupSub: '+-=<>', + autoCommands: 'pi theta sqrt', + autoOperatorNames: 'sin cos tan log', + handlers: { + edit(mathField: MQ) { + return mathField; + }, + enter(mathField: MQ) { + mathField.blur(); + mathField.focus(); + mathField.keystroke('Tab'); + mathField.typedText('test'); + mathField.moveToLeftEnd(); + mathField.moveToRightEnd(); + mathField.moveToDirEnd(1); + }, + upOutOf(mathField: MQ) { + mathField.keystroke('Enter'); + }, + moveOutOf(dir: number, mathField: MQ) { + if (dir === 1) mathField.clearSelection(); + }, + }, + }; +}; diff --git a/types/edtr-io__mathquill/index.d.ts b/types/edtr-io__mathquill/index.d.ts new file mode 100644 index 0000000000..dafc430d7f --- /dev/null +++ b/types/edtr-io__mathquill/index.d.ts @@ -0,0 +1,50 @@ +// Type definitions for @edtr-io/mathquill 0.11 +// Project: https://github.com/edtr-io/mathquill#readme +// Definitions by: Marco Gonzalez +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.5 + +export interface MathField { + focus(): MQ; + blur(): MQ; + write(latex: string): MQ; + cmd(latex: string): MQ; + select(): MQ; + clearSelection(): MQ; + moveToLeftEnd(): MQ; + moveToRightEnd(): MQ; + moveToDirEnd(direction: number): MQ; + keystroke(keys: string): MQ; + typedText(text: string): MQ; + config(config: Config): MQ; +} + +export interface MQ extends MathField { + L: number; + R: number; + revert(): MQ; + reflow(): MQ; + el(): HTMLElement; + latex(): string; + latex(latex: string): MQ; +} + +export interface Config { + spaceBehavesLikeTab?: boolean; + leftRightIntoCmdGoes?: string; + restrictMismatchedBrackets?: boolean; + sumStartsWithNEquals?: boolean; + supSubsRequireOperand?: boolean; + charsThatBreakOutOfSupSub?: string; + autoSubscriptNumerals?: boolean; + autoCommands?: string; + autoOperatorNames?: string; + maxDepth?: number; + substituteTextarea?(): HTMLTextAreaElement; + handlers?: { + enter?(mathField: MQ): any; + edit?(mathField: MQ): any; + upOutOf?(mathField: MQ): any; + moveOutOf?(direction: number, mathField: MQ): any; + }; +} diff --git a/types/edtr-io__mathquill/tsconfig.json b/types/edtr-io__mathquill/tsconfig.json new file mode 100644 index 0000000000..c73ecfab9a --- /dev/null +++ b/types/edtr-io__mathquill/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@edtr-io/mathquill": ["edtr-io__mathquill"] + } + }, + "files": [ + "index.d.ts", + "edtr-io__mathquill-tests.ts" + ] +} diff --git a/types/edtr-io__mathquill/tslint.json b/types/edtr-io__mathquill/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/edtr-io__mathquill/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/react-mathquill/index.d.ts b/types/react-mathquill/index.d.ts new file mode 100644 index 0000000000..49706bc0b9 --- /dev/null +++ b/types/react-mathquill/index.d.ts @@ -0,0 +1,21 @@ +// Type definitions for react-mathquill 0.1 +// Project: https://github.com/viktorstrate/react-mathquill#readme +// Definitions by: Marco Gonzalez +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.5 + +import { ComponentProps, Component } from 'react'; +import { MQ, Config } from '@edtr-io/mathquill'; + +export interface Props extends Omit, 'onChange'> { + latex?: string; + config?: Config; + onChange?(mathField: MQ): void; + mathquillDidMount?(mathField: MQ): void; +} + +export function addStyles(): void; + +declare class MathQuill extends Component {} + +export default MathQuill; diff --git a/types/react-mathquill/react-mathquill-tests.tsx b/types/react-mathquill/react-mathquill-tests.tsx new file mode 100644 index 0000000000..4d74820047 --- /dev/null +++ b/types/react-mathquill/react-mathquill-tests.tsx @@ -0,0 +1,54 @@ +import * as React from 'react'; +import MathQuill, { addStyles } from 'react-mathquill'; +import { Config, MQ } from '@edtr-io/mathquill'; + +addStyles(); + +const Test = () => { + const [latex, setLatex] = React.useState(''); + + const config: Config = { + spaceBehavesLikeTab: true, + leftRightIntoCmdGoes: 'up', + autoSubscriptNumerals: true, + maxDepth: 10, + substituteTextarea() { + return ''; + }, + restrictMismatchedBrackets: true, + sumStartsWithNEquals: true, + supSubsRequireOperand: true, + charsThatBreakOutOfSupSub: '+-=<>', + autoCommands: 'pi theta sqrt', + autoOperatorNames: 'sin cos tan log', + handlers: { + edit(mathField: MQ) { + return mathField; + }, + enter(mathField: MQ) { + mathField.blur(); + mathField.focus(); + mathField.keystroke('Tab'); + mathField.typedText('test'); + mathField.moveToLeftEnd(); + mathField.moveToRightEnd(); + mathField.moveToDirEnd(1); + }, + upOutOf(mathField: MQ) { + mathField.keystroke('Enter'); + }, + moveOutOf(dir: number, mathField: MQ) { + if (dir === 1) mathField.clearSelection(); + }, + }, + }; + + return ( + setLatex(MQ.latex())} + config={config} + mathquillDidMount={(MQ: MQ) => MQ.latex('\\sqrt{}')} + /> + ); +}; diff --git a/types/react-mathquill/tsconfig.json b/types/react-mathquill/tsconfig.json new file mode 100644 index 0000000000..05de0a8721 --- /dev/null +++ b/types/react-mathquill/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "jsx": "react", + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@edtr-io/mathquill": ["edtr-io__mathquill"] + } + }, + "files": [ + "index.d.ts", + "react-mathquill-tests.tsx" + ] +} diff --git a/types/react-mathquill/tslint.json b/types/react-mathquill/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-mathquill/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }