UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

46 lines (45 loc) 1.31 kB
import { LinkMetaStep } from '@atlaskit/adf-schema/steps'; /** * Records metadata about the user action and input method relating to a transaction * as a custom LinkStepMetadata prosemirror step so that it is preserved in * the history for undo/redo. */ export function addLinkMetadata(initialSelection, tr, metadata) { const { storedMarks } = tr; const pos = tr.mapping.map(initialSelection.$from.pos); tr.step(new LinkMetaStep(pos, metadata)); // When you add a new step all the storedMarks are removed it if (storedMarks) { tr.setStoredMarks(storedMarks); } return tr; } export function getLinkMetadataFromTransaction(tr) { return tr.steps.reduce((metadata, step) => { if (!(step instanceof LinkMetaStep)) { return metadata; } return { ...metadata, ...step.getMetadata() }; }, {}); } /** * Adds metadata to the transaction created from a command * The metadata describes the user intent and input method * for executing the command */ export const commandWithMetadata = (command, metadata) => { return (state, dispatch, view) => { if (!dispatch) { return command(state, dispatch, view); } return command(state, tr => { addLinkMetadata(state.selection, tr, metadata); dispatch(tr); }, view); }; };