UNPKG

@atlaskit/editor-plugin-block-controls

Version:

Block controls plugin for @atlaskit/editor-core

78 lines 2.93 kB
import { mapSparseSurfaceCandidates } from './sparse-surface-candidates'; const mergeInvalidation = ({ current, incoming, tr }) => { var _current$ids, _incoming$ids, _current$positions, _incoming$positions, _current$ranges, _incoming$ranges; if (!current && !incoming) { return undefined; } if (!incoming && !tr.docChanged) { return current; } const ids = [...((_current$ids = current === null || current === void 0 ? void 0 : current.ids) !== null && _current$ids !== void 0 ? _current$ids : []), ...((_incoming$ids = incoming === null || incoming === void 0 ? void 0 : incoming.ids) !== null && _incoming$ids !== void 0 ? _incoming$ids : [])]; const positions = [...((_current$positions = current === null || current === void 0 ? void 0 : current.positions) !== null && _current$positions !== void 0 ? _current$positions : []).map(position => tr.mapping.map(position, 1)), ...((_incoming$positions = incoming === null || incoming === void 0 ? void 0 : incoming.positions) !== null && _incoming$positions !== void 0 ? _incoming$positions : [])]; const ranges = [...((_current$ranges = current === null || current === void 0 ? void 0 : current.ranges) !== null && _current$ranges !== void 0 ? _current$ranges : []).map(({ from, to }) => ({ from: tr.mapping.map(from, -1), to: tr.mapping.map(to, 1) })), ...((_incoming$ranges = incoming === null || incoming === void 0 ? void 0 : incoming.ranges) !== null && _incoming$ranges !== void 0 ? _incoming$ranges : [])]; return ids.length || positions.length || ranges.length ? { ids, positions, ranges } : undefined; }; const mapAnchors = (anchors, tr) => { if (!tr.docChanged || anchors.size === 0) { return anchors; } let changed = false; const mappedAnchors = new Map(); for (const [position, name] of anchors) { const mapped = tr.mapping.mapResult(position, 1); if (!mapped.deleted) { mappedAnchors.set(mapped.pos, name); changed ||= mapped.pos !== position; } else { changed = true; } } return changed ? mappedAnchors : anchors; }; export const reducePendingState = ({ invalidation, previous, tr }) => ({ invalidation: mergeInvalidation({ current: previous.invalidation, incoming: invalidation, tr }) }); export const isUnchangedTransaction = ({ pending, previous, tr }) => !tr.docChanged && pending.invalidation === previous.invalidation; export const mapTransactionState = ({ newState, pending, previous, tr }) => ({ ...previous, anchors: mapAnchors(previous.anchors, tr), candidates: tr.docChanged ? mapSparseSurfaceCandidates({ newState, previousState: previous.candidates, tr }) : previous.candidates, decorations: tr.docChanged ? previous.decorations.map(tr.mapping, tr.doc) : previous.decorations, documentChanged: previous.documentChanged || tr.docChanged, invalidation: pending.invalidation });