addTransform

How to get addTransform

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

Type: Function

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:

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

Arguments

# Name Type Description
1. name String

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

2. run Function

Asynchronous callback function which defines the actual behavior of the transform.

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
# Name Type Description
1. stepData StepData
  Returns

Type: StepData | Promise<StepData>

3. [getState] Function

Optional asynchronous callback function which can be implemented to calculate the current state of operations using this transform.

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
# Name Type Description
1. stepData StepData
  Returns

Type: StepData | Promise<StepData>

Was this page helpful?