@atlaskit/editor-plugin-find-replace 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/.eslintrc.js +26 -0
  2. package/CHANGELOG.md +11 -0
  3. package/LICENSE.md +13 -0
  4. package/README.md +30 -0
  5. package/dist/cjs/FindReplaceToolbarButtonWithState.js +166 -0
  6. package/dist/cjs/actions.js +19 -0
  7. package/dist/cjs/commands-with-analytics.js +101 -0
  8. package/dist/cjs/commands.js +255 -0
  9. package/dist/cjs/index.js +12 -0
  10. package/dist/cjs/plugin.js +93 -0
  11. package/dist/cjs/pm-plugins/keymap.js +24 -0
  12. package/dist/cjs/pm-plugins/main.js +39 -0
  13. package/dist/cjs/pm-plugins/plugin-factory.js +109 -0
  14. package/dist/cjs/pm-plugins/plugin-key.js +8 -0
  15. package/dist/cjs/reducer.js +61 -0
  16. package/dist/cjs/styles.js +17 -0
  17. package/dist/cjs/types.js +5 -0
  18. package/dist/cjs/ui/Find.js +309 -0
  19. package/dist/cjs/ui/FindReplace.js +104 -0
  20. package/dist/cjs/ui/FindReplaceToolbarButton.js +133 -0
  21. package/dist/cjs/ui/FindReplaceTooltipButton.js +77 -0
  22. package/dist/cjs/ui/Replace.js +176 -0
  23. package/dist/cjs/ui/styles.js +46 -0
  24. package/dist/cjs/utils/array.js +13 -0
  25. package/dist/cjs/utils/batch-decorations.js +310 -0
  26. package/dist/cjs/utils/commands.js +16 -0
  27. package/dist/cjs/utils/index.js +290 -0
  28. package/dist/es2019/FindReplaceToolbarButtonWithState.js +153 -0
  29. package/dist/es2019/actions.js +13 -0
  30. package/dist/es2019/commands-with-analytics.js +72 -0
  31. package/dist/es2019/commands.js +240 -0
  32. package/dist/es2019/index.js +1 -0
  33. package/dist/es2019/plugin.js +88 -0
  34. package/dist/es2019/pm-plugins/keymap.js +16 -0
  35. package/dist/es2019/pm-plugins/main.js +30 -0
  36. package/dist/es2019/pm-plugins/plugin-factory.js +91 -0
  37. package/dist/es2019/pm-plugins/plugin-key.js +2 -0
  38. package/dist/es2019/reducer.js +56 -0
  39. package/dist/es2019/styles.js +18 -0
  40. package/dist/es2019/types.js +1 -0
  41. package/dist/es2019/ui/Find.js +286 -0
  42. package/dist/es2019/ui/FindReplace.js +81 -0
  43. package/dist/es2019/ui/FindReplaceToolbarButton.js +122 -0
  44. package/dist/es2019/ui/FindReplaceTooltipButton.js +51 -0
  45. package/dist/es2019/ui/Replace.js +155 -0
  46. package/dist/es2019/ui/styles.js +50 -0
  47. package/dist/es2019/utils/array.js +3 -0
  48. package/dist/es2019/utils/batch-decorations.js +189 -0
  49. package/dist/es2019/utils/commands.js +6 -0
  50. package/dist/es2019/utils/index.js +249 -0
  51. package/dist/esm/FindReplaceToolbarButtonWithState.js +157 -0
  52. package/dist/esm/actions.js +13 -0
  53. package/dist/esm/commands-with-analytics.js +95 -0
  54. package/dist/esm/commands.js +248 -0
  55. package/dist/esm/index.js +1 -0
  56. package/dist/esm/plugin.js +86 -0
  57. package/dist/esm/pm-plugins/keymap.js +18 -0
  58. package/dist/esm/pm-plugins/main.js +33 -0
  59. package/dist/esm/pm-plugins/plugin-factory.js +104 -0
  60. package/dist/esm/pm-plugins/plugin-key.js +2 -0
  61. package/dist/esm/reducer.js +54 -0
  62. package/dist/esm/styles.js +11 -0
  63. package/dist/esm/types.js +1 -0
  64. package/dist/esm/ui/Find.js +304 -0
  65. package/dist/esm/ui/FindReplace.js +100 -0
  66. package/dist/esm/ui/FindReplaceToolbarButton.js +126 -0
  67. package/dist/esm/ui/FindReplaceTooltipButton.js +70 -0
  68. package/dist/esm/ui/Replace.js +171 -0
  69. package/dist/esm/ui/styles.js +39 -0
  70. package/dist/esm/utils/array.js +7 -0
  71. package/dist/esm/utils/batch-decorations.js +304 -0
  72. package/dist/esm/utils/commands.js +10 -0
  73. package/dist/esm/utils/index.js +280 -0
  74. package/dist/types/FindReplaceToolbarButtonWithState.d.ts +4 -0
  75. package/dist/types/actions.d.ts +64 -0
  76. package/dist/types/commands-with-analytics.d.ts +27 -0
  77. package/dist/types/commands.d.ts +12 -0
  78. package/dist/types/index.d.ts +2 -0
  79. package/dist/types/plugin.d.ts +2 -0
  80. package/dist/types/pm-plugins/keymap.d.ts +4 -0
  81. package/dist/types/pm-plugins/main.d.ts +5 -0
  82. package/dist/types/pm-plugins/plugin-factory.d.ts +2 -0
  83. package/dist/types/pm-plugins/plugin-key.d.ts +3 -0
  84. package/dist/types/reducer.d.ts +4 -0
  85. package/dist/types/styles.d.ts +3 -0
  86. package/dist/types/types.d.ts +76 -0
  87. package/dist/types/ui/Find.d.ts +71 -0
  88. package/dist/types/ui/FindReplace.d.ts +43 -0
  89. package/dist/types/ui/FindReplaceToolbarButton.d.ts +21 -0
  90. package/dist/types/ui/FindReplaceTooltipButton.d.ts +18 -0
  91. package/dist/types/ui/Replace.d.ts +27 -0
  92. package/dist/types/ui/styles.d.ts +6 -0
  93. package/dist/types/utils/array.d.ts +1 -0
  94. package/dist/types/utils/batch-decorations.d.ts +36 -0
  95. package/dist/types/utils/commands.d.ts +2 -0
  96. package/dist/types/utils/index.d.ts +49 -0
  97. package/dist/types-ts4.5/FindReplaceToolbarButtonWithState.d.ts +4 -0
  98. package/dist/types-ts4.5/actions.d.ts +64 -0
  99. package/dist/types-ts4.5/commands-with-analytics.d.ts +27 -0
  100. package/dist/types-ts4.5/commands.d.ts +12 -0
  101. package/dist/types-ts4.5/index.d.ts +2 -0
  102. package/dist/types-ts4.5/plugin.d.ts +2 -0
  103. package/dist/types-ts4.5/pm-plugins/keymap.d.ts +4 -0
  104. package/dist/types-ts4.5/pm-plugins/main.d.ts +5 -0
  105. package/dist/types-ts4.5/pm-plugins/plugin-factory.d.ts +2 -0
  106. package/dist/types-ts4.5/pm-plugins/plugin-key.d.ts +3 -0
  107. package/dist/types-ts4.5/reducer.d.ts +4 -0
  108. package/dist/types-ts4.5/styles.d.ts +3 -0
  109. package/dist/types-ts4.5/types.d.ts +76 -0
  110. package/dist/types-ts4.5/ui/Find.d.ts +71 -0
  111. package/dist/types-ts4.5/ui/FindReplace.d.ts +43 -0
  112. package/dist/types-ts4.5/ui/FindReplaceToolbarButton.d.ts +21 -0
  113. package/dist/types-ts4.5/ui/FindReplaceTooltipButton.d.ts +18 -0
  114. package/dist/types-ts4.5/ui/Replace.d.ts +27 -0
  115. package/dist/types-ts4.5/ui/styles.d.ts +6 -0
  116. package/dist/types-ts4.5/utils/array.d.ts +1 -0
  117. package/dist/types-ts4.5/utils/batch-decorations.d.ts +36 -0
  118. package/dist/types-ts4.5/utils/commands.d.ts +2 -0
  119. package/dist/types-ts4.5/utils/index.d.ts +49 -0
  120. package/package.json +117 -0
  121. package/styles/package.json +17 -0
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.findReplacePlugin = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
9
+ var _FindReplaceToolbarButtonWithState = _interopRequireDefault(require("./FindReplaceToolbarButtonWithState"));
10
+ var _keymap = _interopRequireDefault(require("./pm-plugins/keymap"));
11
+ var _main = require("./pm-plugins/main");
12
+ var _pluginKey = require("./pm-plugins/plugin-key");
13
+ var findReplacePlugin = exports.findReplacePlugin = function findReplacePlugin(_ref) {
14
+ var _api$featureFlags;
15
+ var props = _ref.config,
16
+ api = _ref.api;
17
+ var featureFlags = (api === null || api === void 0 || (_api$featureFlags = api.featureFlags) === null || _api$featureFlags === void 0 ? void 0 : _api$featureFlags.sharedState.currentState()) || {};
18
+ return {
19
+ name: 'findReplace',
20
+ pmPlugins: function pmPlugins() {
21
+ return [{
22
+ name: 'findReplace',
23
+ plugin: function plugin(_ref2) {
24
+ var dispatch = _ref2.dispatch;
25
+ return (0, _main.createPlugin)(dispatch);
26
+ }
27
+ }, {
28
+ name: 'findReplaceKeymap',
29
+ plugin: function plugin() {
30
+ var _api$analytics;
31
+ return (0, _keymap.default)(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions);
32
+ }
33
+ }];
34
+ },
35
+ getSharedState: function getSharedState(editorState) {
36
+ if (!editorState) {
37
+ return undefined;
38
+ }
39
+ return _pluginKey.findReplacePluginKey.getState(editorState) || undefined;
40
+ },
41
+ actions: {
42
+ getToolbarButton: function getToolbarButton(_ref3) {
43
+ var popupsBoundariesElement = _ref3.popupsBoundariesElement,
44
+ popupsMountPoint = _ref3.popupsMountPoint,
45
+ popupsScrollableElement = _ref3.popupsScrollableElement,
46
+ editorView = _ref3.editorView,
47
+ containerElement = _ref3.containerElement,
48
+ dispatchAnalyticsEvent = _ref3.dispatchAnalyticsEvent,
49
+ isToolbarReducedSpacing = _ref3.isToolbarReducedSpacing;
50
+ return /*#__PURE__*/_react.default.createElement(_FindReplaceToolbarButtonWithState.default, {
51
+ popupsBoundariesElement: popupsBoundariesElement,
52
+ popupsMountPoint: popupsMountPoint,
53
+ popupsScrollableElement: popupsScrollableElement,
54
+ editorView: editorView,
55
+ containerElement: containerElement,
56
+ dispatchAnalyticsEvent: dispatchAnalyticsEvent
57
+ // `allowMatchCase` comes through the preset, but not the feature flags
58
+ // prop with the `ComposableEditor` - grab the FFs from the editor API
59
+ // instead until we clean this up.
60
+ ,
61
+ featureFlags: featureFlags,
62
+ isToolbarReducedSpacing: isToolbarReducedSpacing,
63
+ api: api
64
+ });
65
+ }
66
+ },
67
+ primaryToolbarComponent: function primaryToolbarComponent(_ref4) {
68
+ var popupsBoundariesElement = _ref4.popupsBoundariesElement,
69
+ popupsMountPoint = _ref4.popupsMountPoint,
70
+ popupsScrollableElement = _ref4.popupsScrollableElement,
71
+ isToolbarReducedSpacing = _ref4.isToolbarReducedSpacing,
72
+ editorView = _ref4.editorView,
73
+ containerElement = _ref4.containerElement,
74
+ dispatchAnalyticsEvent = _ref4.dispatchAnalyticsEvent;
75
+ if (props !== null && props !== void 0 && props.twoLineEditorToolbar) {
76
+ return null;
77
+ } else {
78
+ return /*#__PURE__*/_react.default.createElement(_FindReplaceToolbarButtonWithState.default, {
79
+ popupsBoundariesElement: popupsBoundariesElement,
80
+ popupsMountPoint: popupsMountPoint,
81
+ popupsScrollableElement: popupsScrollableElement,
82
+ isToolbarReducedSpacing: isToolbarReducedSpacing,
83
+ editorView: editorView,
84
+ containerElement: containerElement,
85
+ dispatchAnalyticsEvent: dispatchAnalyticsEvent,
86
+ takeFullWidth: props === null || props === void 0 ? void 0 : props.takeFullWidth,
87
+ featureFlags: featureFlags,
88
+ api: api
89
+ });
90
+ }
91
+ }
92
+ };
93
+ };
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _analytics = require("@atlaskit/editor-common/analytics");
8
+ var _keymaps = require("@atlaskit/editor-common/keymaps");
9
+ var _keymap = require("@atlaskit/editor-prosemirror/keymap");
10
+ var _commandsWithAnalytics = require("../commands-with-analytics");
11
+ var activateFindReplace = function activateFindReplace(editorAnalyticsAPI) {
12
+ return function (state, dispatch) {
13
+ (0, _commandsWithAnalytics.activateWithAnalytics)(editorAnalyticsAPI)({
14
+ triggerMethod: _analytics.TRIGGER_METHOD.SHORTCUT
15
+ })(state, dispatch);
16
+ return true;
17
+ };
18
+ };
19
+ var keymapPlugin = function keymapPlugin(editorAnalyticsAPI) {
20
+ var list = {};
21
+ (0, _keymaps.bindKeymapWithCommand)(_keymaps.find.common, activateFindReplace(editorAnalyticsAPI), list);
22
+ return (0, _keymap.keymap)(list);
23
+ };
24
+ var _default = exports.default = keymapPlugin;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.initialState = exports.createPlugin = void 0;
7
+ var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
8
+ var _view = require("@atlaskit/editor-prosemirror/view");
9
+ var _pluginFactory = require("./plugin-factory");
10
+ var _pluginKey = require("./plugin-key");
11
+ var initialState = exports.initialState = {
12
+ isActive: false,
13
+ shouldFocus: false,
14
+ findText: '',
15
+ replaceText: '',
16
+ index: 0,
17
+ matches: [],
18
+ decorationSet: _view.DecorationSet.empty,
19
+ shouldMatchCase: false
20
+ };
21
+ var createPlugin = exports.createPlugin = function createPlugin(dispatch) {
22
+ return new _safePlugin.SafePlugin({
23
+ key: _pluginKey.findReplacePluginKey,
24
+ state: (0, _pluginFactory.createPluginState)(dispatch, function () {
25
+ return initialState;
26
+ }),
27
+ props: {
28
+ decorations: function decorations(state) {
29
+ var _getPluginState = (0, _pluginFactory.getPluginState)(state),
30
+ isActive = _getPluginState.isActive,
31
+ findText = _getPluginState.findText,
32
+ decorationSet = _getPluginState.decorationSet;
33
+ if (isActive && findText) {
34
+ return decorationSet;
35
+ }
36
+ }
37
+ }
38
+ });
39
+ };
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.getPluginState = exports.createPluginState = exports.createCommand = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
+ var _utils = require("@atlaskit/editor-common/utils");
11
+ var _view = require("@atlaskit/editor-prosemirror/view");
12
+ var _reducer = _interopRequireDefault(require("../reducer"));
13
+ var _utils2 = require("../utils");
14
+ var _array = require("../utils/array");
15
+ var _main = require("./main");
16
+ var _pluginKey = require("./plugin-key");
17
+ 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; }
18
+ 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; } // TODO: move into index export
19
+ var handleDocChanged = function handleDocChanged(tr, pluginState) {
20
+ var isActive = pluginState.isActive,
21
+ findText = pluginState.findText;
22
+ if (!isActive || !findText) {
23
+ return pluginState;
24
+ }
25
+ if (!tr.steps.find(_utils.stepHasSlice)) {
26
+ return pluginState;
27
+ }
28
+ var index = pluginState.index,
29
+ decorationSet = pluginState.decorationSet,
30
+ matches = pluginState.matches,
31
+ shouldMatchCase = pluginState.shouldMatchCase;
32
+ var newMatches = (0, _utils2.findMatches)(tr.doc, findText, shouldMatchCase);
33
+ decorationSet = decorationSet.map(tr.mapping, tr.doc);
34
+ var numDecorations = decorationSet.find().length;
35
+ var mappedMatches = matches.map(function (match) {
36
+ return {
37
+ start: tr.mapping.map(match.start),
38
+ end: tr.mapping.map(match.end)
39
+ };
40
+ });
41
+ var matchesToAdd = [];
42
+ var matchesToDelete = [];
43
+ if (newMatches.length > 0 && numDecorations === 0) {
44
+ matchesToAdd = newMatches;
45
+ } else if (newMatches.length === 0 && numDecorations > 0) {
46
+ decorationSet = _view.DecorationSet.empty;
47
+ } else if (newMatches.length > 0 || numDecorations > 0) {
48
+ // go through tr steps and find any new matches from user adding content or
49
+ // any dead matches from user deleting content
50
+ tr.steps.forEach(function (step) {
51
+ if ((0, _utils.stepHasSlice)(step)) {
52
+ // add all matches that are between the affected positions and don't already have
53
+ // corresponding decorations
54
+ matchesToAdd = [].concat((0, _toConsumableArray2.default)(matchesToAdd), (0, _toConsumableArray2.default)(newMatches.filter(function (match) {
55
+ return (0, _utils2.isMatchAffectedByStep)(match, step, tr) && !(0, _utils2.findDecorationFromMatch)(decorationSet, match);
56
+ })));
57
+
58
+ // delete any matches that are missing from the newMatches array and have a
59
+ // corresponding decoration
60
+ matchesToDelete = [].concat((0, _toConsumableArray2.default)(matchesToDelete), (0, _toConsumableArray2.default)((0, _array.findUniqueItemsIn)(mappedMatches.filter(function (match) {
61
+ return (0, _utils2.isMatchAffectedByStep)(match, step, tr) && !!(0, _utils2.findDecorationFromMatch)(decorationSet, match);
62
+ }), newMatches, function (firstMatch, secondMatch) {
63
+ return firstMatch.start === secondMatch.start && firstMatch.end === secondMatch.end;
64
+ })));
65
+ }
66
+ });
67
+ }
68
+
69
+ // update decorations if matches changed following document update
70
+ if (matchesToDelete.length > 0) {
71
+ var decorationsToDelete = matchesToDelete.reduce(function (decorations, match) {
72
+ return [].concat((0, _toConsumableArray2.default)(decorations), (0, _toConsumableArray2.default)(decorationSet.find(match.start, match.end)));
73
+ }, []);
74
+ decorationSet = (0, _utils2.removeDecorationsFromSet)(decorationSet, decorationsToDelete, tr.doc);
75
+ }
76
+ if (matchesToAdd.length > 0) {
77
+ decorationSet = decorationSet.add(tr.doc, (0, _utils2.createDecorations)(tr.selection.from, matchesToAdd));
78
+ }
79
+
80
+ // update selected match if it has changed
81
+ var newIndex = index;
82
+ var selectedMatch = mappedMatches[index];
83
+ if (selectedMatch) {
84
+ newIndex = newMatches.findIndex(function (match) {
85
+ return match.start === selectedMatch.start;
86
+ });
87
+ }
88
+ if (newIndex === undefined || newIndex === -1) {
89
+ newIndex = (0, _utils2.findSearchIndex)(tr.selection.from, newMatches);
90
+ }
91
+ var newSelectedMatch = newMatches[newIndex];
92
+ decorationSet = (0, _utils2.removeMatchesFromSet)(decorationSet, [selectedMatch, newSelectedMatch], tr.doc);
93
+ if (newSelectedMatch) {
94
+ decorationSet = decorationSet.add(tr.doc, (0, _utils2.createDecorations)(0, [newSelectedMatch]));
95
+ }
96
+ return _objectSpread(_objectSpread({}, pluginState), {}, {
97
+ matches: newMatches,
98
+ index: newIndex,
99
+ decorationSet: decorationSet
100
+ });
101
+ };
102
+ var _pluginFactory = (0, _utils.pluginFactory)(_pluginKey.findReplacePluginKey, (0, _reducer.default)(function () {
103
+ return _main.initialState;
104
+ }), {
105
+ onDocChanged: handleDocChanged
106
+ }),
107
+ createCommand = exports.createCommand = _pluginFactory.createCommand,
108
+ getPluginState = exports.getPluginState = _pluginFactory.getPluginState,
109
+ createPluginState = exports.createPluginState = _pluginFactory.createPluginState;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.findReplacePluginKey = void 0;
7
+ var _state = require("@atlaskit/editor-prosemirror/state");
8
+ var findReplacePluginKey = exports.findReplacePluginKey = new _state.PluginKey('findReplace');
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _actions = require("./actions");
10
+ 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; }
11
+ 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; }
12
+ var reducer = function reducer(getInitialState) {
13
+ return function (state, action) {
14
+ switch (action.type) {
15
+ case _actions.FindReplaceActionTypes.ACTIVATE:
16
+ case _actions.FindReplaceActionTypes.FIND:
17
+ return _objectSpread(_objectSpread({}, state), {}, {
18
+ isActive: true,
19
+ shouldFocus: action.type === _actions.FindReplaceActionTypes.ACTIVATE,
20
+ findText: action.findText !== undefined ? action.findText : state.findText,
21
+ matches: action.matches || state.matches,
22
+ index: action.index !== undefined ? action.index : state.index
23
+ });
24
+ case _actions.FindReplaceActionTypes.UPDATE_DECORATIONS:
25
+ return _objectSpread(_objectSpread({}, state), {}, {
26
+ decorationSet: action.decorationSet
27
+ });
28
+ case _actions.FindReplaceActionTypes.FIND_NEXT:
29
+ return _objectSpread(_objectSpread({}, state), {}, {
30
+ index: action.index,
31
+ decorationSet: action.decorationSet
32
+ });
33
+ case _actions.FindReplaceActionTypes.FIND_PREVIOUS:
34
+ return _objectSpread(_objectSpread({}, state), {}, {
35
+ index: action.index,
36
+ decorationSet: action.decorationSet
37
+ });
38
+ case _actions.FindReplaceActionTypes.REPLACE:
39
+ case _actions.FindReplaceActionTypes.REPLACE_ALL:
40
+ return _objectSpread(_objectSpread({}, state), {}, {
41
+ replaceText: action.replaceText,
42
+ decorationSet: action.decorationSet,
43
+ matches: action.matches,
44
+ index: action.index
45
+ });
46
+ case _actions.FindReplaceActionTypes.CANCEL:
47
+ return getInitialState();
48
+ case _actions.FindReplaceActionTypes.BLUR:
49
+ return _objectSpread(_objectSpread({}, state), {}, {
50
+ shouldFocus: false
51
+ });
52
+ case _actions.FindReplaceActionTypes.TOGGLE_MATCH_CASE:
53
+ return _objectSpread(_objectSpread({}, state), {}, {
54
+ shouldMatchCase: !state.shouldMatchCase
55
+ });
56
+ default:
57
+ return state;
58
+ }
59
+ };
60
+ };
61
+ var _default = exports.default = reducer;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.selectedSearchMatchClass = exports.searchMatchClass = exports.findReplaceStyles = void 0;
8
+ var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteral"));
9
+ var _react = require("@emotion/react");
10
+ var _colors = require("@atlaskit/theme/colors");
11
+ var _templateObject;
12
+ /* eslint-disable @atlaskit/design-system/ensure-design-token-usage/preview */
13
+ /* eslint-disable @atlaskit/design-system/ensure-design-token-usage */
14
+ // TODO: https://product-fabric.atlassian.net/browse/DSP-4290
15
+ var searchMatchClass = exports.searchMatchClass = 'search-match';
16
+ var selectedSearchMatchClass = exports.selectedSearchMatchClass = 'selected-search-match';
17
+ var findReplaceStyles = exports.findReplaceStyles = (0, _react.css)(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2.default)(["\n .", " {\n background-color: ", ";\n }\n\n .", " {\n background-color: ", ";\n color: white;\n }\n"])), searchMatchClass, _colors.B75, selectedSearchMatchClass, _colors.B200);
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,309 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = exports.FIND_DEBOUNCE_MS = void 0;
8
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
9
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
10
+ var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
11
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
12
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
13
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
14
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
+ var _react = _interopRequireDefault(require("react"));
16
+ var _react2 = require("@emotion/react");
17
+ var _debounce = _interopRequireDefault(require("lodash/debounce"));
18
+ var _rafSchd = _interopRequireDefault(require("raf-schd"));
19
+ var _reactIntlNext = require("react-intl-next");
20
+ var _analytics = require("@atlaskit/editor-common/analytics");
21
+ var _close = _interopRequireDefault(require("@atlaskit/icon/glyph/editor/close"));
22
+ var _keyboard = _interopRequireDefault(require("@atlaskit/icon/glyph/emoji/keyboard"));
23
+ var _chevronDown = _interopRequireDefault(require("@atlaskit/icon/glyph/hipchat/chevron-down"));
24
+ var _chevronUp = _interopRequireDefault(require("@atlaskit/icon/glyph/hipchat/chevron-up"));
25
+ var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
26
+ var _textfield = _interopRequireDefault(require("@atlaskit/textfield"));
27
+ var _FindReplaceTooltipButton = require("./FindReplaceTooltipButton");
28
+ var _styles = require("./styles");
29
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
30
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } /* eslint-disable @atlaskit/design-system/consistent-css-prop-usage */ /** @jsx jsx */
31
+ var FIND_DEBOUNCE_MS = exports.FIND_DEBOUNCE_MS = 100;
32
+ var messages = (0, _reactIntlNext.defineMessages)({
33
+ find: {
34
+ id: 'fabric.editor.find',
35
+ defaultMessage: 'Find',
36
+ description: 'The word or phrase to search for on the document'
37
+ },
38
+ matchCase: {
39
+ id: 'fabric.editor.matchCase',
40
+ defaultMessage: 'Match case',
41
+ description: 'Toggle whether should also match case when searching for text'
42
+ },
43
+ findNext: {
44
+ id: 'fabric.editor.findNext',
45
+ defaultMessage: 'Find next',
46
+ description: 'Locate the next occurrence of the word or phrase that was searched for'
47
+ },
48
+ findPrevious: {
49
+ id: 'fabric.editor.findPrevious',
50
+ defaultMessage: 'Find previous',
51
+ description: 'Locate the previous occurrence of the word or phrase that was searched for'
52
+ },
53
+ closeFindReplaceDialog: {
54
+ id: 'fabric.editor.closeFindReplaceDialog',
55
+ defaultMessage: 'Close',
56
+ description: 'Cancel search and close the "Find and Replace" dialog'
57
+ },
58
+ noResultsFound: {
59
+ id: 'fabric.editor.noResultsFound',
60
+ defaultMessage: 'No results',
61
+ description: 'No matches were found for the word or phrase that was searched for'
62
+ },
63
+ resultsCount: {
64
+ id: 'fabric.editor.resultsCount',
65
+ description: 'Text for selected search match position and total results count',
66
+ defaultMessage: '{selectedMatchPosition} of {totalResultsCount}'
67
+ }
68
+ });
69
+ // eslint-disable-next-line @repo/internal/react/no-class-components
70
+ var Find = /*#__PURE__*/function (_React$Component) {
71
+ (0, _inherits2.default)(Find, _React$Component);
72
+ var _super = _createSuper(Find);
73
+ function Find(props) {
74
+ var _this;
75
+ (0, _classCallCheck2.default)(this, Find);
76
+ _this = _super.call(this, props);
77
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "findTextfieldRef", /*#__PURE__*/_react.default.createRef());
78
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "isComposing", false);
79
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "syncFindText", function (onSynced) {
80
+ var _this$state;
81
+ // If the external prop findText changes and we aren't in a composition we should update to
82
+ // use the external prop value.
83
+ //
84
+ // An example of where this may happen is when a find occurs through the user selecting some text
85
+ // and pressing Mod-f.
86
+ if (!_this.isComposing && _this.props.findText !== ((_this$state = _this.state) === null || _this$state === void 0 ? void 0 : _this$state.localFindText)) {
87
+ _this.updateFindValue(_this.props.findText || '', onSynced);
88
+ }
89
+ });
90
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "focusFindTextfield", function () {
91
+ var input = _this.findTextfieldRef.current;
92
+ if (_this.props.shouldFocus && input) {
93
+ input.select();
94
+ }
95
+ });
96
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleFindChange", function (event) {
97
+ _this.updateFindValue(event.target.value);
98
+ });
99
+ // debounce (vs throttle) to not block typing inside find input while onFind runs
100
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "debouncedFind", (0, _debounce.default)(function (value) {
101
+ _this.props.onFind(value);
102
+ }, FIND_DEBOUNCE_MS));
103
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "updateFindValue", function (value, onSynced) {
104
+ _this.setState({
105
+ localFindText: value
106
+ }, function () {
107
+ if (_this.isComposing) {
108
+ return;
109
+ }
110
+ onSynced && onSynced();
111
+ _this.debouncedFind(value);
112
+ });
113
+ });
114
+ // throtlle between animation frames gives better experience on Enter compared to arbitrary value
115
+ // it adjusts based on performance (and document size)
116
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleFindKeyDownThrottled", (0, _rafSchd.default)(function (event) {
117
+ if (event.key === 'Enter') {
118
+ if (event.shiftKey) {
119
+ _this.props.onFindPrev({
120
+ triggerMethod: _analytics.TRIGGER_METHOD.KEYBOARD
121
+ });
122
+ } else {
123
+ _this.props.onFindNext({
124
+ triggerMethod: _analytics.TRIGGER_METHOD.KEYBOARD
125
+ });
126
+ }
127
+ } else if (event.key === 'ArrowDown') {
128
+ // we want to move focus between find & replace texfields when user hits up/down arrows
129
+ _this.props.onArrowDown();
130
+ }
131
+ }));
132
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleFindKeyDown", function (event) {
133
+ if (_this.isComposing) {
134
+ return;
135
+ }
136
+ event.persist();
137
+ _this.handleFindKeyDownThrottled(event);
138
+ });
139
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleFindKeyUp", function () {
140
+ _this.handleFindKeyDownThrottled.cancel();
141
+ });
142
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleFindNextClick", function (ref) {
143
+ if (_this.isComposing) {
144
+ return;
145
+ }
146
+ _this.props.onFindNext({
147
+ triggerMethod: _analytics.TRIGGER_METHOD.BUTTON
148
+ });
149
+ });
150
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleFindPrevClick", function (ref) {
151
+ if (_this.isComposing) {
152
+ return;
153
+ }
154
+ _this.props.onFindPrev({
155
+ triggerMethod: _analytics.TRIGGER_METHOD.BUTTON
156
+ });
157
+ });
158
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleCompositionStart", function () {
159
+ _this.isComposing = true;
160
+ });
161
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleCompositionEnd", function (event) {
162
+ _this.isComposing = false;
163
+ // type for React.CompositionEvent doesn't set type for target correctly
164
+ _this.updateFindValue(event.target.value);
165
+ });
166
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "clearSearch", function () {
167
+ _this.props.onCancel({
168
+ triggerMethod: _analytics.TRIGGER_METHOD.BUTTON
169
+ });
170
+ });
171
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleMatchCaseClick", function (buttonRef) {
172
+ if (_this.props.allowMatchCase && _this.props.onToggleMatchCase) {
173
+ _this.props.onToggleMatchCase();
174
+ _this.props.onFind(_this.props.findText);
175
+ }
176
+ });
177
+ var formatMessage = props.intl.formatMessage;
178
+ _this.find = formatMessage(messages.find);
179
+ _this.closeFindReplaceDialog = formatMessage(messages.closeFindReplaceDialog);
180
+ _this.noResultsFound = formatMessage(messages.noResultsFound);
181
+ _this.findNext = formatMessage(messages.findNext);
182
+ _this.findPrevious = formatMessage(messages.findPrevious);
183
+ _this.matchCase = formatMessage(messages.matchCase);
184
+ _this.matchCaseIcon = (0, _react2.jsx)(_keyboard.default, {
185
+ label: _this.matchCase
186
+ });
187
+ _this.findNextIcon = (0, _react2.jsx)(_chevronDown.default, {
188
+ label: _this.findNext
189
+ });
190
+ _this.findPrevIcon = (0, _react2.jsx)(_chevronUp.default, {
191
+ label: _this.findPrevious
192
+ });
193
+ _this.closeIcon = (0, _react2.jsx)(_close.default, {
194
+ label: _this.closeFindReplaceDialog
195
+ });
196
+
197
+ // We locally manage the value of the input inside this component in order to support compositions.
198
+ // This requires some additional work inside componentDidUpdate to ensure we support changes that
199
+ // occur to this value which do not originate from this component.
200
+ _this.state = {
201
+ localFindText: ''
202
+ };
203
+ return _this;
204
+ }
205
+ (0, _createClass2.default)(Find, [{
206
+ key: "componentDidMount",
207
+ value: function componentDidMount() {
208
+ var _this2 = this;
209
+ this.props.onFindTextfieldRefSet(this.findTextfieldRef);
210
+
211
+ // focus initially on dialog mount if there is no find text provided
212
+ if (!this.props.findText) {
213
+ this.focusFindTextfield();
214
+ }
215
+ this.syncFindText(function () {
216
+ // focus after input is synced if find text provided
217
+ if (_this2.props.findText) {
218
+ _this2.focusFindTextfield();
219
+ }
220
+ });
221
+ }
222
+ }, {
223
+ key: "componentDidUpdate",
224
+ value: function componentDidUpdate(prevProps) {
225
+ var _this3 = this;
226
+ // focus on update if find text did not change
227
+ if (!(0, _platformFeatureFlags.getBooleanFF)('platform.editor.a11y-find-replace')) {
228
+ var _this$state2;
229
+ if (this.props.findText === ((_this$state2 = this.state) === null || _this$state2 === void 0 ? void 0 : _this$state2.localFindText)) {
230
+ this.focusFindTextfield();
231
+ }
232
+ if (this.props.findText !== prevProps.findText) {
233
+ this.syncFindText(function () {
234
+ // focus after input is synced if find text provided
235
+ if (_this3.props.findText) {
236
+ _this3.focusFindTextfield();
237
+ }
238
+ });
239
+ }
240
+ }
241
+ }
242
+ }, {
243
+ key: "componentWillUnmount",
244
+ value: function componentWillUnmount() {
245
+ this.debouncedFind.cancel();
246
+ this.handleFindKeyDownThrottled.cancel();
247
+ }
248
+ }, {
249
+ key: "render",
250
+ value: function render() {
251
+ var _this$props = this.props,
252
+ findText = _this$props.findText,
253
+ count = _this$props.count,
254
+ allowMatchCase = _this$props.allowMatchCase,
255
+ shouldMatchCase = _this$props.shouldMatchCase,
256
+ formatMessage = _this$props.intl.formatMessage;
257
+ var resultsCount = formatMessage(messages.resultsCount, {
258
+ selectedMatchPosition: count.index + 1,
259
+ totalResultsCount: count.total
260
+ });
261
+ return (0, _react2.jsx)("div", {
262
+ css: _styles.sectionWrapperStyles
263
+ }, (0, _react2.jsx)(_textfield.default, {
264
+ name: "find",
265
+ appearance: "none",
266
+ placeholder: this.find,
267
+ value: this.state.localFindText,
268
+ ref: this.findTextfieldRef,
269
+ autoComplete: "off",
270
+ onChange: this.handleFindChange,
271
+ onKeyDown: this.handleFindKeyDown,
272
+ onKeyUp: this.handleFindKeyUp,
273
+ onBlur: this.props.onFindBlur,
274
+ onCompositionStart: this.handleCompositionStart,
275
+ onCompositionEnd: this.handleCompositionEnd
276
+ }), (0, _react2.jsx)("div", {
277
+ css: _styles.countWrapperStyles,
278
+ "aria-live": "polite"
279
+ }, findText && (0, _react2.jsx)("span", {
280
+ "data-testid": "textfield-count",
281
+ css: _styles.countStyles
282
+ }, count.total === 0 ? this.noResultsFound : resultsCount)), allowMatchCase && (0, _react2.jsx)(_FindReplaceTooltipButton.FindReplaceTooltipButton, {
283
+ title: this.matchCase,
284
+ icon: this.matchCaseIcon,
285
+ onClick: this.handleMatchCaseClick,
286
+ isPressed: shouldMatchCase
287
+ }), (0, _react2.jsx)(_FindReplaceTooltipButton.FindReplaceTooltipButton, {
288
+ title: this.findNext,
289
+ icon: this.findNextIcon,
290
+ keymapDescription: 'Enter',
291
+ onClick: this.handleFindNextClick,
292
+ disabled: count.total <= 1
293
+ }), (0, _react2.jsx)(_FindReplaceTooltipButton.FindReplaceTooltipButton, {
294
+ title: this.findPrevious,
295
+ icon: this.findPrevIcon,
296
+ keymapDescription: 'Shift Enter',
297
+ onClick: this.handleFindPrevClick,
298
+ disabled: count.total <= 1
299
+ }), (0, _react2.jsx)(_FindReplaceTooltipButton.FindReplaceTooltipButton, {
300
+ title: this.closeFindReplaceDialog,
301
+ icon: this.closeIcon,
302
+ keymapDescription: 'Escape',
303
+ onClick: this.clearSearch
304
+ }));
305
+ }
306
+ }]);
307
+ return Find;
308
+ }(_react.default.Component);
309
+ var _default = exports.default = (0, _reactIntlNext.injectIntl)(Find);