UNPKG

@atlaskit/editor-common

Version:

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

44 lines (43 loc) 2.12 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } 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) { var storedMarks = tr.storedMarks; var 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(function (metadata, step) { if (!(step instanceof LinkMetaStep)) { return metadata; } return _objectSpread(_objectSpread({}, 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 var commandWithMetadata = function commandWithMetadata(command, metadata) { return function (state, dispatch, view) { if (!dispatch) { return command(state, dispatch, view); } return command(state, function (tr) { addLinkMetadata(state.selection, tr, metadata); dispatch(tr); }, view); }; };