addTransform

Type: Function

How to get addTransform?

JavaScript

import addTransform from 'fontoxml-operations/src/addTransform.js'

Register a transform to the set of globally usable transforms. Transforms do not change application state, but do change the step data of the operation.

Examples of transforms include setting contextNodeId to various values, or encoding information in a URL.

A transform does not automatically cause the state of operations using it to be recomputed when the data it uses changes. While Fonto does automatically refresh some operation states in some cases, this behavior is subject to change. Therefore, we recommend not relying on this behavior to also recompute the state of custom transforms. Instead, developers should make sure to call OperationsManager#invalidateOperationStatesByStepType whenever the result of the transform may have changed. For example, a transform that uses the selection may invalidate corresponding operation states when the selection changes by subscribing to the selection change notifier. This is usually done in the same install.js that registers the transform:

Other

addTransform('setSomethingBasedOnSelection', stepData => {
    const selectedNode = selectionManager.getSelectedElement();
    if (selectedNode) {
        stepData.selectedNodeName = selectedNode.nodeName;
    }
    return stepData;
});
selectionManager.selectionChangeNotifier.addCallback(() => {
    operationsManager.invalidateOperationStatesByStepType(
        'transform',
        'setSomethingBasedOnSelection'
    );
});

Arguments

  1. name

    (Required)

    Type: String

    The name of the transform. Operations can invoke transform action using a step of type "transform/name".

  2. run

    (Required)

    Type: Function

    The callback receives the current operation StepData as the first and only argument. It should return the transformed StepData. If your transform is asynchronous, you can return a promise that eventually resolves into the transformed step data.

    Arguments

    Returns

  3. getState

    (Optional)

    Type: Function

    Setting this function is useful when:

    • the transform is not entirely without side-effects;

    • or getting the operation state can be simpified.

    The operation state can be changed by setting the operationState (OperationState) property on the StepData object. The step data object should either be returned from this callback function, or a promise returned from this function should resolve to it.

    This callback function receives the current operation StepData as the first and only argument.

    Arguments

    Returns