``, and ````.
The optional ``style`` attribute may contain a string of CSS declarations. CSS properties that can be applied are:
- ``align*``
- ``background*`` (``background-image`` only allows ``data:`` URLs)
- ``border*``
- ``box*``
- ``clear``
- ``color``
- ``cursor``
- ``display``
- ``float``
- ``font*``
- ``justify*``
- ``line*``
- ``margin*``
- ``padding*``
- ``position`` (only the values ``static`` and ``relative`` are accepted)
- ``text*``
- ``transition*``
- ``outline*``
- ``vertical-align``
- ``white-space``
- ``word*``
- ``writing*``
- ``width``
- ``min-width``
- ``max-width``
- ``height``
- ``min-height``
- ``max-height``
A child can be another element, a string, or an object reference.
.. _generating-child-elements:
Generating child elements
-------------------------
Child elements can be created by defining a special ``object`` template. The format of this template is:
.. code-block:: javascript
["object", {"object": objectToInspect, "config": configObject}]
Examples
********
Simple example
--------------
Let's take a look at a simple example to illustrate the concept of custom formatters:
.. code-block:: javascript
window.devtoolsFormatters = [
{
header: variable => {
if (variable.hasOwnProperty('foo')) {
return [
'span', {
'style': `
font-family: "Comic Sans MS", fantasy;
font-size: 3rem;
color: green;
`
},
'foo'
];
}
return null;
}
}
];
In the example above, a custom formatter is defined for a variable. The `header` property of the formatter is a function that determines how the variable is displayed. In this case, if the variable has a property named `foo`, it will be rendered as a `` element with a specific style. It will be logged to the Web Console like this:
.. image:: simple-custom-formatter-example.png
Complete example
----------------
For a more complex example, let's consider displaying a `Date` object:
.. code-block:: html
Custom formatter for dates
With this custom formatter, a ``Date`` object logged to the console will display the date and time in a formatted manner, along with separate breakdowns of its individual components. Within the console, the object will be logged like this:
.. image:: date-custom-formatter-example.png
Example with object references
------------------------------
.. code-block:: html
Menu custom formatter
This example displays a menu object with nested subitems. The custom formatter is recursive, so it will display all subitems as well. The output of this example looks like this:
.. image:: menu.png
Debugging Custom Formatters
***************************
If a custom formatter contains an error, an error message is logged to the console, explaining the issue. Whenever possible, the error message also includes a source link pointing to the exact location of the error within the code.
.. image:: custom-formatter-error.png
Further tips
************
- Analyze the structure and behavior of the variables you intend to format, understanding the key properties or class hierarchy that differentiate them.
- When testing for an object's type, use ``instanceof`` if the objects are instances of a specific class. If the objects are plain objects, use ``hasOwnProperty`` to check for particular properties.
- Test your formatters with different types of variables to ensure they function as expected and handle various cases accurately.
- Nest formatters by :ref:`generating-child-elements` to display complex objects in a more readable manner.
- Use the ``config`` parameter to pass additional information to the formatter, such as the current level of recursion.
- If you have multiple formatters, keep in mind to check for the type of the object in each formatter, and return ``null`` if the object is not of the expected type. Otherwise, the formatter will be applied to all objects, which may result in unexpected behavior.
- Choose your formatting wisely. For large objects it may be better to display only a summary of the object, and allow the user to expand it if needed.
- Each logged object will call the formatters hooks, which can have an impact on performance. So you should aim for small and fast hooks.
Existing Formatters
*******************
There are existing formatters available that cover different needs. Some examples include:
- `andrewdavey/immutable-devtools `_: Custom formatter for Immutable-js values
- `disjukr/vector-devtools `_: Custom formatter for vector values
- `binaryage/cljs-devtools `_: Collection of DevTools enhancements for ClojureScript developers
- Three.js object formatters:
- `twitter.com/thespite/status/656585905151545344 `_
- `twitter.com/thespite/status/656499298230734849 `_
|