AsyncSchemaLocationToSchemaExperienceResolver

Type: Class

How to get AsyncSchemaLocationToSchemaExperienceResolver?

JavaScript

import AsyncSchemaLocationToSchemaExperienceResolver from 'fontoxml-schema-experience-resolver/src/AsyncSchemaLocationToSchemaExperienceResolver.js'

An asynchronous version of the SchemaLocationToSchemaExperienceResolver, which can be used to asynchronously register schema experiences. This can be useful for defining (parts of) schema experience configuration at runtime.

It requires an asynchronous callback function in which the following can be achieved:

  • Add operations.

  • Add schema experience modules.

  • Add schema experience configuration.

  • Define and register schema experiences.

There are some caveats:

  • Any schema experience configuration added within the callback function is processed after all other schema experience configuration, meaning it will have the highest precedence.

  • Localization: Texts in added schema experience configuration might not have localized counterparts in included messages bundles.

  • Performance: The startup time of the editor will be directly affected by the time it takes to resolve the asynchronous callback function.

Example: Create a schema experience module, add operations, and register a schema experience (shell).

JavaScript

import AsyncSchemaLocationToSchemaExperienceResolver from 'fontoxml-schema-experience-resolver/src/AsyncSchemaLocationToSchemaExperienceResolver.js';
import configurationManager from 'fontoxml-configuration/src/configurationManager.js';
import configureAsInlineFormatting from 'fontoxml-families/src/configureAsInlineFormatting.js';

configurationManager.set(
	'schema-experience-resolver',
	new AsyncSchemaLocationToSchemaExperienceResolver(async asyncRegistrationApi => {
		// Create a module to add the configuration on.
		const sxModule = asyncRegistrationApi.addSchemaExperienceModule('some-sx-module', []);

		// Configure the element(s).
		configureAsInlineFormatting(sxModule, 'self::em', 'Emphasis', { slant: 'italic' });

		// Add additional operation(s).
		asyncRegistrationApi.addOperations({
			'toggle-em': {
				label: 'Emphasis',
				steps: [
					{
						type: 'operation/toggle-inline-formatting-element',
						data: {
							nodeName: 'em'
						}
					}
				]
			}
		});

		// Define and register the schema experience for a schema and schema locations.
		asyncRegistrationApi.defineAndRegisterSchemaExperience(
			'assets/schemas/some-schema.json',
			'urn:fontoxml:example:some:schema.xsd',
			['some-sx-module']
		);
	})
);

Example: Dynamically create a schema experience module, add operations, and register a schema experience (shell), depending on a built-in schema experience (shell).

JavaScript

import AsyncSchemaLocationToSchemaExperienceResolver from 'fontoxml-schema-experience-resolver/src/AsyncSchemaLocationToSchemaExperienceResolver.js';
import configurationManager from 'fontoxml-configuration/src/configurationManager.js';
import configureAsInlineFormatting from 'fontoxml-families/src/configureAsInlineFormatting.js';
import customLoadDynamicConfiguration from 'example-package/src/customLoadDynamicConfiguration.js'
import customMastheadConfiguration from 'example-package/src/customMastheadConfiguration.js'

configurationManager.set(
	'schema-experience-resolver',
	new AsyncSchemaLocationToSchemaExperienceResolver(async asyncRegistrationApi => {
		const dynamicConfiguration = await customLoadDynamicConfiguration();
		dynamicConfiguration.shells.forEach(shell => {
			// Create a module to add the configuration on.
			const sxModule = asyncRegistrationApi.addSchemaExperienceModule(
				shell.name,
				[shell.builtInSxShellName]
			);

			shell.inlineElementConfigurations.forEach(inlineElementConfiguration => {
				configureAsInlineFormatting(
					sxModule,
					inlineElementConfiguration.selector,
					inlineElementConfiguration.markupLabel,
					inlineElementConfiguration.options
				);
			});

			// Add additional operations.
			asyncRegistrationApi.addOperations(shell.operationsByName);
			customMastheadConfiguration.addOperationNames(shell.mastheadOperationNames);

			// Define and register the schema experience using the built-in schema experience (shell).
			asyncRegistrationApi.defineAndRegisterSchemaExperience(
				'assets/schemas/' + shell.builtInSxShellName + '.json',
				shell.locations,
				[sxModule.moduleName]
			);
		});
	})
);

Example: Dynamically create schema experience modules, create a schema experience (shell), and register the schema experience (shell).

JavaScript

import AsyncSchemaLocationToSchemaExperienceResolver from 'fontoxml-schema-experience-resolver/src/AsyncSchemaLocationToSchemaExperienceResolver.js';
import configurationManager from 'fontoxml-configuration/src/configurationManager.js';
import configureAsInlineFormatting from 'fontoxml-families/src/configureAsInlineFormatting.js';
import customLoadDynamicConfiguration from 'example-package/src/customLoadDynamicConfiguration.js'

configurationManager.set(
	'schema-experience-resolver',
	new AsyncSchemaLocationToSchemaExperienceResolver(async asyncRegistrationApi => {
		const dynamicConfiguration = await customLoadDynamicConfiguration();
		const sxModuleNames = dynamicConfiguration.modules.map(module => {
			// Create a new schema experience module.
			const sxModule = asyncRegistrationApi.addSchemaExperienceModule(module.name, []);

			// Add new schema experience configuration using the new module.
			module.inlineElementConfigurations.forEach(inlineElementConfiguration => {
				configureAsInlineFormatting(
					sxModule,
					inlineElementConfiguration.selector,
					inlineElementConfiguration.markupLabel,
					inlineElementConfiguration.options
				);
			});

			return module.name;
		});

		// Create a new bundle of schema experience modules (shell).
		const sxShellModule = addSchemaExperienceModule('some-sx-shell', sxModuleNames);

		// Define and register the schema experience for a schema and schema locations.
		asyncRegistrationApi.defineAndRegisterSchemaExperience(
			'assets/schemas/some-schema.json',
			'urn:fontoxml:example:some:schema.xsd',
			['some-sx-shell']
		);
	})
);

Constructor arguments

  1. callback

    (Required)

    Type: Function

    Asynchronous callback function in which schema experiences can be registered.

    Arguments

    Returns