Namespace util

Utilities that are useful when extending the editor or integrating it with other elements of a Web app.

Index

Functions

compareMathContent

  • compareMathContent(document1: string, document2: string): boolean
  • Compares the math content of two documents stored in the MICD document format. Only math content is compared; metadata is ignored. If either document is stored in an old version of the document format, the comparison will be performed as if it was first upgraded to the current version.

    This can be used to help implement automated testing by checking whether some code produces an expected result. It can also be used as a first pass to check whether a submitted answer matches an accepted solution. Note, though, that it does not attempt to test whether the two documents are mathematically equivalent, but only whether their contents are identical.

    throws

    TypeError If either argument is not a valid MICD document.

    see

    Editor.value

    Parameters

    • document1: string

      A document in the MICD document format.

    • document2: string

      The document to be compared to the first document, also in the MICD document format.

    Returns boolean

    If the documents have the same math content, true; otherwise false.

completeConfig

  • Completes a partial configuration by filling in default values for any missing properties. This can be used to determine what the default value of one or more configuration options would be given the specified basis. For example, the following statement evaluates to what the default radix point character would be if the locale was Canadian French: micd.util.completeConfig({ locale: "fr-CA" }).radixPoint.

    Parameters

    • Optional config: ConfigOptions | null

      An object that may define configuration options to constrain the completed result.

    Returns ConfigOptions

compress

  • compress(toCompress: string | null, optimizeForUtf8?: boolean): string | null
  • Compresses a string. The returned string is usually not longer than (and often significantly shorter than) the original string. The original string can be recovered by calling decompress with the compressed string as an argument.

    By default, compressed strings use the entire range of character values. This improves compression effectiveness when the compressed string will be kept in memory or otherwise stored as 16-bit character values. However, if encoded as UTF-8, the encoded representation of the compressed string will typically require more bytes than the uncompressed string would have. Passing true for optimizeForUtf8 limits compression efficiency but ensures that the compressed string uses only single-byte UTF-8 characters. The compressed result will generally require more characters than the standard method, but those characters will use fewer actual bytes when encoded.

    If null or an empty string is passed in, it is returned unchanged. The original string may also be returned if the compressed result would be longer. This is transparent to the caller: decompress handles such strings correctly.

    Parameters

    • toCompress: string | null

      The string to compress.

    • Optional optimizeForUtf8: boolean

      If true, the compressor will minimize the byte length of the string's UTF-8 encoding instead of minimizing the number of characters in the compressed string object.

    Returns string | null

    The compressed string, or null if null was passed in.

createOperator

  • Creates a new operator which is returned as a Clip. The clip can then be applied to an editor, saved in a memory cell, or made into an Action and assigned a keystroke. To create an operator you must specify a symbol, such as "⨂" or "jne", and optionally a placement.

    While the resulting operator can always be used in the editor, you may run into other issues:

    • Views may display a placeholder symbol when a requested symbol is not supported by the view's math fonts. The end result will vary by platform.
    • Conversion to other formats may not work as expected.
    • Other advanced features may not work; for example, computation may fail since the editor may not know how to apply the operator.
    • Custom operators that effectively reproduce an existing operator should not expect the new operator to behave differently.
    throws

    ReferenceError If the symbol is missing.

    throws

    TypeError If the symbol is too long or the placement is invalid.

    Parameters

    • symbol: string

      A non-empty string containing the symbol or name to use for the operator.

    • Optional placement: OperatorPlacement | keyof typeof OperatorPlacement

      A string naming the placement of the operator; if not specified an appropriate default is chosen based on the symbol.

    Returns Clip

createSafeSpace

  • createSafeSpace(source: Editor, caretPosition?: string, selection?: string): Editor
  • Returns a new editor that contains a copy of the line containing the specified caret position and selection. The caret position and selection will be translated to the same relative position in the copy. If a caret position and selection are not provided, the current position and selection in the source editor are used.

    A safe space can be used to safely test a complex, multistep edit operation without affecting the undo history of the source editor. If the operation succeeds in the safe space, the same steps can be repeated in the source editor.

    Parameters

    • source: Editor

      The editor to create a safe copy from.

    • Optional caretPosition: string

      The document caret position to be recreated; the default uses the source editor caret position.

    • Optional selection: string

      The selection to be recreated, or null for no selection. If caretPosition is null, this argument is ignored and the source editor selection is used instead.

    Returns Editor

    A new, historyless editor that containing the copied context and translated caret position and selection.

decompress

  • decompress(toDecompress: string | null): string | null
  • Decompresses a previously compressed string, returning the original text.

    Parameters

    • toDecompress: string | null

      The string to decompress, as previously returned from compress.

    Returns string | null

isValidClip

  • isValidClip(clip: any): boolean
  • Returns whether the given string value describes a valid clip in MICD format. If this returns true, actions such as creating a new Clip using the value will not throw an error.

    Parameters

    • clip: any

      The potential clip data to test.

    Returns boolean

    True if the data is a valid clip.

isValidDocument

  • isValidDocument(document: any): boolean
  • Returns whether the given string value describes a valid document in MICD format. If this returns true, actions such as setting an editor's value to the string will not throw an error.

    Parameters

    • document: any

      The potential document data to test.

    Returns boolean

    True if the data is a valid document.

normalizeSemanticObject

  • Given a semantic object from any source, returns a semantic object that describes the same content in the standard form returned by the API.

    Parameters

    Returns object

    A structure containing the normalized object and an array of any errors found in the source object.

printDocument

  • printDocument(document: string, options?: PrintOptions): Promise<void>
  • Asynchronously prints the specified document.

    Parameters

    • document: string

      The document to print, in the MICD document format.

    • Optional options: PrintOptions

      Options that affect the print layout.

    Returns Promise<void>

    A Promise that resolves after document layout is complete, once the print operation has begun.

supportedCommands

  • Returns a new array in which each element is an object that describes basic information about a built-in command. This can be used for purposes such as allowing users to customize key gestures, or to create a custom replacement for the built-in help table feature.

    see

    InputMap

    see

    createHelpTable

    Returns CommandInfo[]

    A new array with an element for each command.

supportedImageFormats

  • Returns a new array in which each element describes one of the image formats supported on the current device when exporting math content to an image. Support for "png" and "svg" is guaranteed. Other entries depend on the platform and/or browser, but may include "bmp", "gif", "jpeg", "webp", and "avif". The array can be iterated over to list supported formats, or specific formats can be looked up by name (as in formats["png"]).

    Example: Create a glob pattern for accepted files

    const filters = micd.util.supportedImageFormats()
      .map(fmt => fmt.fileExtension)
      .join();
    console.log(`*.{${filters}}`);
    
    see

    Editor.toImage

    Returns ImageFormatInfo[]

    An array with an element for each supported format.

This API is still under development and is subject to change. Copyright © Math I Can Do Solutions Incorporated and/or its licensors.