Updated APi_Methods, Config, and README Updated content and formatting.
diff --git a/README.md b/README.md index 2b51817..f7afc72 100644 --- a/README.md +++ b/README.md
@@ -19,21 +19,21 @@ MathQuill has a simple interface: ```javascript -var domObject = document.getElementById('#some_id'); +var htmlElement = document.getElementById('some_id'); var config = { handlers: {edit: myEditHandler}, restrictMismatchedBrackets: true -} -var mathField = MQ.MathField(domObject, config); +}; +var mathField = MQ.MathField(htmlElement, config); -mathField.latex('2^{\frac{3}{2}}') // Renders the given LaTeX in the MathQuill field -console.log(mathField.latex()) // Shows '2^{\frac{3}{2}}' +mathField.latex('2^{\\frac{3}{2}}'); // Renders the given LaTeX in the MathQuill field +console.log(mathField.latex()); // Shows '2^{\frac{3}{2}}' ``` ## Open-Source License The Source Code Form of MathQuill is subject to the terms of the Mozilla Public -License, v. 2.0: http://mozilla.org/MPL/2.0/ +License, v. 2.0: [http://mozilla.org/MPL/2.0/](http://mozilla.org/MPL/2.0/) The quick-and-dirty is you can do whatever if modifications to MathQuill are in public GitHub forks. (Other ways to publicize modifications are also fine, as
diff --git a/docs/Api_Methods.md b/docs/Api_Methods.md index a7b4ea3..a7625f3 100644 --- a/docs/Api_Methods.md +++ b/docs/Api_Methods.md
@@ -1,4 +1,4 @@ -# Api Methods +# Api Interface To use the MathQuill API, first get the latest version of the interface: @@ -26,21 +26,21 @@ math fields, but using different copies on the same DOM element won't work. Anyway, .noConflict() is primarily to help you reduce globals.) -## Constructors +# Constructors -### MQ.StaticMath(DOM_OBJECT) +## MQ.StaticMath(html_element) Creates a non-editable MathQuill with the contents of the HTML element and returns a [static MathField object](http://mathquill.readthedocs.org/en/latest/Api_Methods/#mathfield-methods). If the element is already a static MathField, this will return a new static Mathfield object with the same ID. If the element is a different type of MathQuill element, this will return null. -### MQ.MathField(DOM_OBJECT, CONFIG) +## MQ.MathField(html_element, config) Creates an editable MathQuill with the contents of the HTML element and returns an [editable MathField object](http://mathquill.readthedocs.org/en/latest/Api_Methods/#editable-mathfield-methods). If the element is already an editable MathField, this will return a new editable Mathfield object with the same ID. If the element is a different type of MathQuill element, this will return null. -### MQ(DOM_OBJECT) +## MQ(html_element) `MQ` itself is a function that takes an HTML element and, if it's the root HTML element of a static math or math field, returns an API object for it @@ -51,9 +51,9 @@ MQ(otherSpan) // => null ``` -## Comparing MathFields +# Comparing MathFields -### Checking Type +## Checking Type ```js var staticMath = MQ.StaticMath(staticMathSpan); mathField instanceof MQ.StaticMath // => true @@ -67,7 +67,7 @@ mathField instanceof MathQuill // => true ``` -### ID +## Comparing IDs API objects for the same MathQuill instance have the same `.id`, which will always be a unique truthy primitive value that can be used as an object key (like an ad hoc [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) or [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)): @@ -81,7 +81,7 @@ staticMath.id in setOfMathFields // => false ``` -### Data Object +## Data Object Similarly, API objects for the same MathQuill instance share a `.data` object (which can be used like an ad hoc [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) or [`WeakSet`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet)): @@ -91,9 +91,11 @@ MQ(mathFieldSpan).data.foo // => 'bar' ``` -## MathField Methods +# MathField Methods -These are methods that every MathField has. +These are methods that every MathField has. These are the only methods that static fields have and a subset of the methods that editable fields have. + +## revert() Any element that has been MathQuill-ified can be reverted: ```html @@ -105,6 +107,8 @@ MQ($('#revert-me')[0]).revert().html(); // => 'some <code>HTML</code>' ``` +## reflow() + MathQuill uses computed dimensions, so if they change (because an element was mathquill-ified before it was in the visible HTML DOM, or the font size changed), then you'll need to tell MathQuill to recompute: @@ -116,46 +120,108 @@ mathField.reflow(); ``` -MathQuill API objects further expose the following public methods: +## el() -* `.el()` returns the root HTML element -* `.html()` returns the contents as static HTML -* `.latex()` returns the contents as LaTeX -* `.latex('a_n x^n')` will render the argument as LaTeX +Returns the root HTML element. -## Editable MathField Methods +## html() + +Returns the contents of the MathField as static HTML + +## latex() + +Returns the contents as LaTeX + +## latex(latex_string) + +This will render the argument as LaTeX + +# Editable MathField Methods Editable MathFields have all of the [above](http://mathquill.readthedocs.org/en/latest/Api_Methods/#mathfield-methods) methods in addition to the ones listed here. -Additionally, descendants of `MQ.EditableField` (currently only `MQ.MathField`) -expose: +## focus() -* `.focus()`, `.blur()` focuses or defocuses the editable field -* `.write(' - 1')` will write some LaTeX at the current cursor position -* `.cmd('\\sqrt')` will enter a LaTeX command at the current cursor position or - with the current selection -* `.select()` selects the contents (just like [on `textarea`s][] and [on - `input`s][]) -* `.clearSelection()` clears the current selection -* `.moveTo{Left,Right,Dir}End()` move the cursor to the left/right end of the - editable field, respectively. (The first two are implemented in terms of - `.moveToDirEnd(dir)` where `dir` is one of `MQ.L` or `MQ.R`, constants that - obey the contract that `MQ.L === -MQ.R` and vice versa.) -* `.keystroke(keys)` simulates keystrokes given a string like `"Ctrl-Home Del"`, - a whitespace-delimited list of [key values][] with optional prefixes -* `.typedText(text)` simulates typing text, one character at a time -* `ᴇxᴘᴇʀɪᴍᴇɴᴛᴀʟ` `.dropEmbedded(pageX, pageY, options)` insert a custom - embedded element at the given coordinates, where `options` is an object like: +Puts the focus on the editable field. - ```js - { - htmlString: '<span class="custom-embed"></span>', - text: function() { return 'custom_embed'; }, - latex: function() { return '\customEmbed'; } +## blur() + +Removes focus from the editable field. + +## write(latex_string) + +Write the given LaTeX at the current cursor position. If the cursor does not have focus, it writes it to last position the cursor occupied in the mathField. + +```javascript +mathField.write(' - 1'); // writes ' - 1' to mathField after the cursor position. +``` + +## cmd(latex_string) + +Enter a LaTeX command at the current cursor position or with the current selection. If the cursor does not have focus, it writes it to last position the cursor occupied in the mathField. + +```javascript +mathField.cmd('\\sqrt'); // writes a square root character after the cursor position. +``` + +## select() + +Selects the contents (just like [on `textarea`s](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-48880622) and [on `input`s](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-34677168)). + +## clearSelection() + +Clears the current selection. + +## moveToLeftEnd(), moveToRightEnd() + +Move the cursor to the left/right end of the editable field, respectively. These are implemented in terms of [`moveToDirEnd`, shown below](). + +## movetoDirEnd(direction) + +Moves the cursor to the end of the mathfield in the direction specified. The direction can be one of `MQ.L` or `MQ.R`. These are constants that obey the contract that `MQ.L === -MQ.R` and vice versa. This function may be easier to use than the functions above it if used in the [`moveOutOf` handler]() when setting the [configuration of a mathField](http://mathquill.readthedocs.org/en/latest/Config/#setting-configuration). + +```javascript +var mathFieldConfig = { + handlers: { + moveOutOf: function(direction, mathField) { + mathField.movetoDirEnd(direction); + } } - ``` -* `ᴇxᴘᴇʀɪᴍᴇɴᴛᴀʟ` `.registerEmbed('name', function(id){return options})` allows MathQuill to parse custom embedded objects from latex, where `options` is an object like the one defined above in `.dropEmbedded`. This will parse the following latex into the embedded object you defined: `\embed{name}[id]}` +}); +``` -[on `textarea`s]: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-48880622 -[on `input`s]: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-34677168 -[key values]: http://www.w3.org/TR/2012/WD-DOM-Level-3-Events-20120614/#fixed-virtual-key-codes +## keystroke(keys) + +Simulates keystrokes given a string like `"Ctrl-Home Del"`, a whitespace-delimited list of [key inputs](http://www.w3.org/TR/2012/WD-DOM-Level-3-Events-20120614/#fixed-virtual-key-codes) with optional prefixes. + +```javascript +mathField.keystroke('Shift-Left'); // Selects character before the current cursor position +``` + +## typedText(text) + +Simulates typing text, one character at a time. + +```javascript +// Types part of the demo from mathquill.com without delays between keystrokes +mathField.typedText('x=-b\\pm \\sqrt b^2 -4ac'); +``` + +## dropEmbedded(pageX, pageY, options) **[Experimental]()** + +Insert a custom embedded element at the given coordinates, where `options` is an object like: +```js +{ + htmlString: '<span class="custom-embed"></span>', + text: function() { return 'custom_embed'; }, + latex: function() { return '\customEmbed'; } +} +``` + +## registerEmbed('name', function(id){return options}) **[Experimental]()** + +Allows MathQuill to parse custom embedded objects from latex, where `options` is an object like the one defined above in `.dropEmbedded`. This will parse the following latex into the embedded object you defined: `\embed{name}[id]}`. + +## Note on Experimental Features + +Methods marked as experimental may be altered drastically or removed in future versions. They may also receive less maintance than other non-experimental features.
diff --git a/docs/Config.md b/docs/Config.md index a09aee7..a8836b5 100644 --- a/docs/Config.md +++ b/docs/Config.md
@@ -1,6 +1,4 @@ -# MathField Config - -## Setting Configuration +# Setting Configuration The configuration options for a given mathField has the following structure and [options](http://mathquill.readthedocs.org/en/latest/Config/#configuration-options): ```js @@ -31,15 +29,15 @@ Global defaults for a page may be set with `MQ.config(NEW_CONFIG)`. -## Configuration Options +# Configuration Options -### spacesBehavesLikeTab +## spacesBehavesLikeTab If `spaceBehavesLikeTab` is true the keystrokes {Shift-,}Spacebar will behave like {Shift-,}Tab escaping from the current block (as opposed to the default behavior of inserting a Space character). -### leftRightIntoCmdGoes +## leftRightIntoCmdGoes By default, the Left and Right keys move the cursor through all possible cursor positions in a particular order: right into a fraction puts the cursor at the @@ -60,26 +58,26 @@ numerator that is always skipped, which is the same behavior as the Mac OS X built-in app Grapher. -### restrictMismatchedBrackets +## restrictMismatchedBrackets If `restrictMismatchedBrackets` is true then you can type [a,b) and [a,b), but if you try typing `[x}` or `\langle x|`, you'll get `[{x}]` or `\langle|x|\rangle` instead. This lets you type `(|x|+1)` normally; otherwise, you'd get `\left( \right| x \left| + 1 \right)`. -### sumStartsWithNEquals +## sumStartsWithNEquals If `sumStartsWithNEquals` is true then when you type `\sum`, `\prod`, or `\coprod`, the lower limit starts out with `n=`, e.g. you get the LaTeX `\sum_{n=}^{ }`, rather than empty by default. -### supSubsRequireOperand +## supSubsRequireOperand `supSubsRequireOperand` disables typing of superscripts and subscripts when there's nothing to the left of the cursor to be exponentiated or subscripted. Averts the especially confusing typo `x^^2`, which looks much like `x^2`. -### charsThatBreakOutOfSupSub +## charsThatBreakOutOfSupSub `charsThatBreakOutOfSupSub` sets the chars that when typed, "break out" of superscripts and subscripts: for example, typing `x^2n+y` normally results in @@ -91,14 +89,14 @@ (Doesn't apply to the first character in a superscript or subscript, so typing `x^-6` still results in `x^{-6}`.) -### autoCommands +## autoCommands `autoCommands`, a space-delimited list of LaTeX control words (no backslash, letters only, min length 2), defines the (default empty) set of "auto-commands", commands automatically rendered by just typing the letters without typing a backslash first. -### autoOperatorNames +## autoOperatorNames `autoOperatorNames`, a list of the same form (space-delimited letters-only each length>=2), and overrides the set of operator names that automatically become @@ -106,7 +104,7 @@ `sin`, `log`, etc. (Defaults to [the LaTeX built-in operator names](http://latex.wikia.com/wiki/List_of_LaTeX_symbols#Named_operators:_sin.2C_cos.2C_etc.), but with additional trig operators like `sech`, `arcsec`, `arsinh`, etc.) -### substituteTextarea +## substituteTextarea `substituteTextarea`, a function that creates a focusable DOM element, called when setting up a math field. It defaults to `<textarea autocorrect=off .../>`, @@ -120,30 +118,9 @@ virtual keyboard. Desmos currently sniffs the user agent for iOS, so Bluetooth keyboards just don't work in Desmos on iOS, the tradeoffs are up to you. -### Handlers +# Handlers -Supported handlers: -- `moveOutOf`, `deleteOutOf`, and `selectOutOf` are called with `dir` and the - math field API object as arguments -- `upOutOf`, `downOutOf`, `enter`, and `edit` are called with just the API - object as the argument - -The `*OutOf` handlers are called when Left/Right/Up/Down/Backspace/Del/ -Shift-Left/Shift-Right is pressed but the cursor is at the left/right/top/bottom -edge and so nothing happens within the math field. For example, when the cursor -is at the left edge, pressing the Left key causes the `moveOutOf` handler (if -provided) to be called with `MQ.L` and the math field API object as arguments, -and Backspace causes `deleteOutOf` (if provided) to be called with `MQ.L` and -the API object as arguments, etc. - -The `enter` handler is called whenever Enter is pressed. - -The `edit` handler is called when the contents of the field might have been -changed by stuff being typed, or deleted, or written with the API, etc. -(Deprecated aliases: `edited`, `reflow`.) - -Handlers are always called directly on the `handlers` object passed in, -preserving the `this` value, so you can do stuff like: +Handlers are called directly on the `handlers` object passed in, preserving the `this` value, so you can do stuff like: ```js var MathList = P(function(_) { _.init = function() { @@ -164,8 +141,8 @@ ... }); ``` -Of course you can always ignore the last argument, like when the handlers close -over the math field: + +You can always ignore the last argument, like when the handlers close over the math field: ```js var latex = ''; var mathField = MQ.MathField($('#mathfield')[0], { @@ -175,18 +152,25 @@ } }); ``` -### Changing Colors -To change the foreground color, don't just set the `color`, also set -the `border-color`, because the cursor, fraction bar, and square root -overline are all borders, not text. (Example below.) +## *OutOf handlers -Due to technical limitations of IE8, if you support it, and want to give -a MathQuill editable a background color other than white, and support -square roots, parentheses, square brackets, or curly braces, you will -need to, in addition to of course setting the background color on the -editable itself, set it on elements with class `mq-matrixed`, and then set -a Chroma filter on elements with class `mq-matrixed-container`. +moveOutOf(direction, mathField), deleteOutOf(direction, mathField), selectOutOf(direction, mathField), upOutOf(mathField), downOutOf(mathField) + +The `*OutOf` handlers are called when Left/Right/Up/Down/Backspace/Del/Shift-Left/Shift-Right is pressed but the cursor is at the left/right/top/bottom edge and so nothing happens within the math field. For example, when the cursor is at the left edge, pressing the Left key causes the `moveOutOf` handler (if +provided) to be called with `MQ.L` and the math field API object as arguments, and Backspace causes `deleteOutOf` (if provided) to be called with `MQ.L` and the API object as arguments, etc. + +## enter(mathField) + +Called whenever Enter is pressed. + +## edit(mathField) + +This is called when the contents of the field might have been changed by stuff being typed, or deleted, or written with the API, etc. (Deprecated aliases: `edited`, `reflow`.) Note that this may be called when nothing has actually changed. + +# Changing Colors + +To change the foreground color, set both `color` and the `border-color` because some MathQuill symbols are implemented with borders, not text. For example, to style as white-on-black instead of black-on-white: @@ -202,9 +186,6 @@ filter: progid:DXImageTransform.Microsoft.Chroma(color='black'); } -(This is because almost all math rendered by MathQuill has a transparent -background, so for them it's sufficient to set the background color on -the editable itself. The exception is, IE8 doesn't support CSS -transforms, so MathQuill uses a matrix filter to stretch parens etc, -which [anti-aliases wrongly without an opaque background](http://github.com/mathquill/mathquill/wiki/Transforms), -so MathQuill defaults to white.) +## Color Changes Support IE8 + +To support a MathQuill editable background color other than white in IE8, set the background color on both the editable mathField and on elements with class `mq-matrixed`. Then set a Chroma filter on elements with class `mq-matrixed-container`.
diff --git a/docs/index.md b/docs/index.md index 2b51817..f7afc72 100644 --- a/docs/index.md +++ b/docs/index.md
@@ -19,21 +19,21 @@ MathQuill has a simple interface: ```javascript -var domObject = document.getElementById('#some_id'); +var htmlElement = document.getElementById('some_id'); var config = { handlers: {edit: myEditHandler}, restrictMismatchedBrackets: true -} -var mathField = MQ.MathField(domObject, config); +}; +var mathField = MQ.MathField(htmlElement, config); -mathField.latex('2^{\frac{3}{2}}') // Renders the given LaTeX in the MathQuill field -console.log(mathField.latex()) // Shows '2^{\frac{3}{2}}' +mathField.latex('2^{\\frac{3}{2}}'); // Renders the given LaTeX in the MathQuill field +console.log(mathField.latex()); // Shows '2^{\frac{3}{2}}' ``` ## Open-Source License The Source Code Form of MathQuill is subject to the terms of the Mozilla Public -License, v. 2.0: http://mozilla.org/MPL/2.0/ +License, v. 2.0: [http://mozilla.org/MPL/2.0/](http://mozilla.org/MPL/2.0/) The quick-and-dirty is you can do whatever if modifications to MathQuill are in public GitHub forks. (Other ways to publicize modifications are also fine, as