UNPKG

@atlaskit/editor-plugin-find-replace

Version:

find replace plugin for @atlaskit/editor-core

127 lines (122 loc) 6.46 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getPluginState = exports.createPluginState = exports.createCommand = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _utils = require("@atlaskit/editor-common/utils"); var _view = require("@atlaskit/editor-prosemirror/view"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var _main = require("./main"); var _pluginKey = require("./plugin-key"); var _reducer = _interopRequireDefault(require("./reducer")); var _utils2 = require("./utils"); 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) { (0, _defineProperty2.default)(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; } var handleDocChanged = function handleDocChanged(tr, pluginState) { var _api$expand; var isActive = pluginState.isActive, findText = pluginState.findText; if (!isActive || !findText) { return pluginState; } if (!tr.steps.find(_utils.stepHasSlice)) { return pluginState; } // Ignored via go/ees005 // eslint-disable-next-line prefer-const var index = pluginState.index, decorationSet = pluginState.decorationSet, matches = pluginState.matches, shouldMatchCase = pluginState.shouldMatchCase, getIntl = pluginState.getIntl, api = pluginState.api; var newMatches = (0, _utils2.findMatches)({ content: tr.doc, searchText: findText, shouldMatchCase: shouldMatchCase, getIntl: getIntl, api: api }); decorationSet = decorationSet.map(tr.mapping, tr.doc); var numDecorations = decorationSet.find().length; var mappedMatches = matches.map(function (match) { return { start: tr.mapping.map(match.start), end: tr.mapping.map(match.end), canReplace: match.canReplace, nodeType: match.nodeType }; }); var matchesToAdd = []; var matchesToDelete = []; if (newMatches.length > 0 && numDecorations === 0) { matchesToAdd = newMatches; } else if (newMatches.length === 0 && numDecorations > 0) { decorationSet = _view.DecorationSet.empty; } else if (newMatches.length > 0 || numDecorations > 0) { // go through tr steps and find any new matches from user adding content or // any dead matches from user deleting content tr.steps.forEach(function (step) { if ((0, _utils.stepHasSlice)(step)) { // add all matches that are between the affected positions and don't already have // corresponding decorations matchesToAdd = [].concat((0, _toConsumableArray2.default)(matchesToAdd), (0, _toConsumableArray2.default)(newMatches.filter(function (match) { return (0, _utils2.isMatchAffectedByStep)(match, step, tr) && !(0, _utils2.findDecorationFromMatch)(decorationSet, match); }))); // delete any matches that are missing from the newMatches array and have a // corresponding decoration matchesToDelete = [].concat((0, _toConsumableArray2.default)(matchesToDelete), (0, _toConsumableArray2.default)((0, _utils2.findUniqueItemsIn)(mappedMatches.filter(function (match) { return (0, _utils2.isMatchAffectedByStep)(match, step, tr) && !!(0, _utils2.findDecorationFromMatch)(decorationSet, match); }), newMatches, function (firstMatch, secondMatch) { return firstMatch.start === secondMatch.start && firstMatch.end === secondMatch.end; }))); } }); } // update decorations if matches changed following document update if (matchesToDelete.length > 0) { var decorationsToDelete = matchesToDelete.reduce(function (decorations, match) { return [].concat((0, _toConsumableArray2.default)(decorations), (0, _toConsumableArray2.default)(decorationSet.find(match.start, match.end))); }, []); decorationSet = (0, _utils2.removeDecorationsFromSet)(decorationSet, decorationsToDelete, tr.doc); } if (matchesToAdd.length > 0) { decorationSet = decorationSet.add(tr.doc, (0, _utils2.createDecorations)(tr.selection.from, matchesToAdd)); } // update selected match if it has changed var newIndex = index; var selectedMatch = mappedMatches[index]; if (selectedMatch) { newIndex = newMatches.findIndex(function (match) { return match.start === selectedMatch.start; }); } if (newIndex === undefined || newIndex === -1) { newIndex = (0, _expValEquals.expValEquals)('platform_editor_find_and_replace_improvements', 'isEnabled', true) ? (0, _utils2.findClosestMatch)(tr.selection.from, newMatches) : (0, _utils2.findSearchIndex)(tr.selection.from, newMatches); } var newSelectedMatch = newMatches[newIndex]; decorationSet = (0, _utils2.removeMatchesFromSet)(decorationSet, [selectedMatch, newSelectedMatch], tr.doc); if (newSelectedMatch) { decorationSet = decorationSet.add(tr.doc, (0, _utils2.createDecorations)(0, [newSelectedMatch])); } var newSelection = (0, _utils2.getSelectionForMatch)(tr.selection, tr.doc, newIndex, newMatches); api === null || api === void 0 || (_api$expand = api.expand) === null || _api$expand === void 0 || _api$expand.commands.toggleExpandWithMatch(newSelection)({ tr: tr }); return _objectSpread(_objectSpread({}, pluginState), {}, { matches: newMatches, index: newIndex, decorationSet: decorationSet }); }; var dest = (0, _utils.pluginFactory)(_pluginKey.findReplacePluginKey, (0, _reducer.default)(function () { return _main.initialState; }), { onDocChanged: handleDocChanged }); var createCommand = exports.createCommand = dest.createCommand; var getPluginState = exports.getPluginState = dest.getPluginState; var createPluginState = exports.createPluginState = dest.createPluginState;