@atlaskit/editor-plugin-base 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 (84) hide show
  1. package/.eslintrc.js +14 -0
  2. package/CHANGELOG.md +11 -0
  3. package/LICENSE.md +13 -0
  4. package/README.md +33 -0
  5. package/dist/cjs/commands.js +20 -0
  6. package/dist/cjs/index.js +33 -0
  7. package/dist/cjs/plugin.js +143 -0
  8. package/dist/cjs/pm-plugins/context-identifier.js +58 -0
  9. package/dist/cjs/pm-plugins/disable-spell-checking.js +63 -0
  10. package/dist/cjs/pm-plugins/filter-steps.js +34 -0
  11. package/dist/cjs/pm-plugins/fix-chrome-88-selection.js +34 -0
  12. package/dist/cjs/pm-plugins/frozen-editor.js +265 -0
  13. package/dist/cjs/pm-plugins/inline-cursor-target.js +184 -0
  14. package/dist/cjs/pm-plugins/newline-preserve-marks.js +34 -0
  15. package/dist/cjs/pm-plugins/scroll-gutter/index.js +264 -0
  16. package/dist/cjs/pm-plugins/scroll-gutter/plugin-key.js +15 -0
  17. package/dist/cjs/types.js +5 -0
  18. package/dist/cjs/utils/frozen-editor.js +11 -0
  19. package/dist/cjs/utils/input-latency-tracking.js +134 -0
  20. package/dist/es2019/commands.js +13 -0
  21. package/dist/es2019/index.js +4 -0
  22. package/dist/es2019/plugin.js +113 -0
  23. package/dist/es2019/pm-plugins/context-identifier.js +43 -0
  24. package/dist/es2019/pm-plugins/disable-spell-checking.js +54 -0
  25. package/dist/es2019/pm-plugins/filter-steps.js +23 -0
  26. package/dist/es2019/pm-plugins/fix-chrome-88-selection.js +24 -0
  27. package/dist/es2019/pm-plugins/frozen-editor.js +258 -0
  28. package/dist/es2019/pm-plugins/inline-cursor-target.js +187 -0
  29. package/dist/es2019/pm-plugins/newline-preserve-marks.js +20 -0
  30. package/dist/es2019/pm-plugins/scroll-gutter/index.js +244 -0
  31. package/dist/es2019/pm-plugins/scroll-gutter/plugin-key.js +7 -0
  32. package/dist/es2019/types.js +1 -0
  33. package/dist/es2019/utils/frozen-editor.js +2 -0
  34. package/dist/es2019/utils/input-latency-tracking.js +103 -0
  35. package/dist/esm/commands.js +13 -0
  36. package/dist/esm/index.js +4 -0
  37. package/dist/esm/plugin.js +131 -0
  38. package/dist/esm/pm-plugins/context-identifier.js +48 -0
  39. package/dist/esm/pm-plugins/disable-spell-checking.js +56 -0
  40. package/dist/esm/pm-plugins/filter-steps.js +27 -0
  41. package/dist/esm/pm-plugins/fix-chrome-88-selection.js +26 -0
  42. package/dist/esm/pm-plugins/frozen-editor.js +251 -0
  43. package/dist/esm/pm-plugins/inline-cursor-target.js +175 -0
  44. package/dist/esm/pm-plugins/newline-preserve-marks.js +26 -0
  45. package/dist/esm/pm-plugins/scroll-gutter/index.js +256 -0
  46. package/dist/esm/pm-plugins/scroll-gutter/plugin-key.js +7 -0
  47. package/dist/esm/types.js +1 -0
  48. package/dist/esm/utils/frozen-editor.js +4 -0
  49. package/dist/esm/utils/input-latency-tracking.js +127 -0
  50. package/dist/types/commands.d.ts +2 -0
  51. package/dist/types/index.d.ts +7 -0
  52. package/dist/types/plugin.d.ts +24 -0
  53. package/dist/types/pm-plugins/context-identifier.d.ts +12 -0
  54. package/dist/types/pm-plugins/disable-spell-checking.d.ts +4 -0
  55. package/dist/types/pm-plugins/filter-steps.d.ts +4 -0
  56. package/dist/types/pm-plugins/fix-chrome-88-selection.d.ts +5 -0
  57. package/dist/types/pm-plugins/frozen-editor.d.ts +12 -0
  58. package/dist/types/pm-plugins/inline-cursor-target.d.ts +17 -0
  59. package/dist/types/pm-plugins/newline-preserve-marks.d.ts +5 -0
  60. package/dist/types/pm-plugins/scroll-gutter/index.d.ts +22 -0
  61. package/dist/types/pm-plugins/scroll-gutter/plugin-key.d.ts +7 -0
  62. package/dist/types/types.d.ts +4 -0
  63. package/dist/types/utils/frozen-editor.d.ts +2 -0
  64. package/dist/types/utils/input-latency-tracking.d.ts +42 -0
  65. package/dist/types-ts4.5/commands.d.ts +2 -0
  66. package/dist/types-ts4.5/index.d.ts +7 -0
  67. package/dist/types-ts4.5/plugin.d.ts +26 -0
  68. package/dist/types-ts4.5/pm-plugins/context-identifier.d.ts +12 -0
  69. package/dist/types-ts4.5/pm-plugins/disable-spell-checking.d.ts +4 -0
  70. package/dist/types-ts4.5/pm-plugins/filter-steps.d.ts +4 -0
  71. package/dist/types-ts4.5/pm-plugins/fix-chrome-88-selection.d.ts +5 -0
  72. package/dist/types-ts4.5/pm-plugins/frozen-editor.d.ts +12 -0
  73. package/dist/types-ts4.5/pm-plugins/inline-cursor-target.d.ts +17 -0
  74. package/dist/types-ts4.5/pm-plugins/newline-preserve-marks.d.ts +5 -0
  75. package/dist/types-ts4.5/pm-plugins/scroll-gutter/index.d.ts +22 -0
  76. package/dist/types-ts4.5/pm-plugins/scroll-gutter/plugin-key.d.ts +7 -0
  77. package/dist/types-ts4.5/types.d.ts +4 -0
  78. package/dist/types-ts4.5/utils/frozen-editor.d.ts +2 -0
  79. package/dist/types-ts4.5/utils/input-latency-tracking.d.ts +42 -0
  80. package/package.json +92 -0
  81. package/report.api.md +89 -0
  82. package/tmp/api-report-tmp.d.ts +64 -0
  83. package/tsconfig.app.json +45 -0
  84. package/tsconfig.dev.json +81 -0
@@ -0,0 +1,265 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.frozenEditorPluginKey = exports.default = exports.NORMAL_SEVERITY_THRESHOLD = exports.DEGRADED_SEVERITY_THRESHOLD = exports.DEFAULT_TRACK_SEVERITY_THRESHOLD_NORMAL = exports.DEFAULT_TRACK_SEVERITY_THRESHOLD_DEGRADED = exports.DEFAULT_FREEZE_THRESHOLD = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _analytics = require("@atlaskit/editor-common/analytics");
10
+ var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
11
+ var _ufo = require("@atlaskit/editor-common/ufo");
12
+ var _utils = require("@atlaskit/editor-common/utils");
13
+ var _state = require("@atlaskit/editor-prosemirror/state");
14
+ var _frozenEditor = require("../utils/frozen-editor");
15
+ var _inputLatencyTracking = _interopRequireDefault(require("../utils/input-latency-tracking"));
16
+ var _contextIdentifier = require("./context-identifier");
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; }
19
+ var frozenEditorPluginKey = new _state.PluginKey('frozenEditor');
20
+ exports.frozenEditorPluginKey = frozenEditorPluginKey;
21
+ var DEFAULT_KEYSTROKE_SAMPLING_LIMIT = 100;
22
+ var DEFAULT_SLOW_THRESHOLD = 300;
23
+ var DEFAULT_FREEZE_THRESHOLD = 600;
24
+ exports.DEFAULT_FREEZE_THRESHOLD = DEFAULT_FREEZE_THRESHOLD;
25
+ var NORMAL_SEVERITY_THRESHOLD = 2000;
26
+ exports.NORMAL_SEVERITY_THRESHOLD = NORMAL_SEVERITY_THRESHOLD;
27
+ var DEGRADED_SEVERITY_THRESHOLD = 3000;
28
+ exports.DEGRADED_SEVERITY_THRESHOLD = DEGRADED_SEVERITY_THRESHOLD;
29
+ var DEFAULT_TRACK_SEVERITY_ENABLED = false;
30
+ var DEFAULT_TRACK_SEVERITY_THRESHOLD_NORMAL = 100;
31
+ exports.DEFAULT_TRACK_SEVERITY_THRESHOLD_NORMAL = DEFAULT_TRACK_SEVERITY_THRESHOLD_NORMAL;
32
+ var DEFAULT_TRACK_SEVERITY_THRESHOLD_DEGRADED = 500;
33
+ exports.DEFAULT_TRACK_SEVERITY_THRESHOLD_DEGRADED = DEFAULT_TRACK_SEVERITY_THRESHOLD_DEGRADED;
34
+ var dispatchLongTaskEvent = function dispatchLongTaskEvent(dispatchAnalyticsEvent, view, time, getNodeCount, interactionType, severity) {
35
+ var _getContextIdentifier;
36
+ var state = view.state;
37
+ var nodesCount = getNodeCount(state);
38
+ return dispatchAnalyticsEvent({
39
+ action: _analytics.ACTION.BROWSER_FREEZE,
40
+ actionSubject: _analytics.ACTION_SUBJECT.EDITOR,
41
+ attributes: _objectSpread(_objectSpread({
42
+ objectId: (_getContextIdentifier = (0, _contextIdentifier.getContextIdentifier)(state)) === null || _getContextIdentifier === void 0 ? void 0 : _getContextIdentifier.objectId,
43
+ freezeTime: time,
44
+ nodeSize: state.doc.nodeSize
45
+ }, nodesCount), {}, {
46
+ interactionType: interactionType,
47
+ severity: severity
48
+ }),
49
+ eventType: _analytics.EVENT_TYPE.OPERATIONAL
50
+ });
51
+ };
52
+ var _default = function _default(dispatchAnalyticsEvent, inputTracking, browserFreezeTracking, ufo) {
53
+ var interactionType;
54
+ var inputLatencyTracker = null;
55
+ var inputLatencySingleKeyTracker = null;
56
+ var inputLatencyRenderedTracker = null;
57
+ if (browserFreezeTracking !== null && browserFreezeTracking !== void 0 && browserFreezeTracking.trackInteractionType) {
58
+ interactionType = (0, _frozenEditor.setInteractionType)(_analytics.BROWSER_FREEZE_INTERACTION_TYPE.LOADING);
59
+ }
60
+ var samplingRate = inputTracking && typeof inputTracking.samplingRate === 'number' ? inputTracking.samplingRate : DEFAULT_KEYSTROKE_SAMPLING_LIMIT;
61
+ var slowThreshold = inputTracking && typeof inputTracking.slowThreshold === 'number' ? inputTracking.slowThreshold : DEFAULT_SLOW_THRESHOLD;
62
+ var freezeThreshold = inputTracking && typeof inputTracking.freezeThreshold === 'number' ? inputTracking.freezeThreshold : DEFAULT_FREEZE_THRESHOLD;
63
+ var allowCountNodes = inputTracking && inputTracking.countNodes;
64
+ var prevNodeCountState = null;
65
+ var prevNodeCount = {};
66
+
67
+ // Cache the result as we were calling this multiple times
68
+ // and has potential to be expensive
69
+ var getNodeCount = function getNodeCount(state) {
70
+ if (state === prevNodeCountState) {
71
+ return prevNodeCount;
72
+ }
73
+ prevNodeCount = allowCountNodes ? (0, _utils.countNodes)(state) : {};
74
+ prevNodeCountState = state;
75
+ return prevNodeCount;
76
+ };
77
+ var shouldTrackSeverity = (inputTracking === null || inputTracking === void 0 ? void 0 : inputTracking.trackSeverity) || DEFAULT_TRACK_SEVERITY_ENABLED;
78
+ var severityThresholdNormal = (inputTracking === null || inputTracking === void 0 ? void 0 : inputTracking.severityNormalThreshold) || DEFAULT_TRACK_SEVERITY_THRESHOLD_NORMAL;
79
+ var severityThresholdDegraded = (inputTracking === null || inputTracking === void 0 ? void 0 : inputTracking.severityDegradedThreshold) || DEFAULT_TRACK_SEVERITY_THRESHOLD_DEGRADED;
80
+ var createDispatchSample = function createDispatchSample(action, view) {
81
+ return function (time, severity) {
82
+ var _getContextIdentifier2;
83
+ var state = view.state;
84
+ var nodesCount = getNodeCount(state);
85
+ var samplePayload = {
86
+ action: action,
87
+ actionSubject: _analytics.ACTION_SUBJECT.EDITOR,
88
+ attributes: _objectSpread(_objectSpread({
89
+ time: time,
90
+ nodeSize: state.doc.nodeSize
91
+ }, nodesCount), {}, {
92
+ objectId: (_getContextIdentifier2 = (0, _contextIdentifier.getContextIdentifier)(state)) === null || _getContextIdentifier2 === void 0 ? void 0 : _getContextIdentifier2.objectId,
93
+ severity: shouldTrackSeverity ? severity : undefined
94
+ }),
95
+ eventType: _analytics.EVENT_TYPE.OPERATIONAL
96
+ };
97
+ dispatchAnalyticsEvent(samplePayload);
98
+ };
99
+ };
100
+ var createDispatchAverage = function createDispatchAverage(action, view) {
101
+ return function (_ref, severity) {
102
+ var _getContextIdentifier3;
103
+ var mean = _ref.mean,
104
+ median = _ref.median,
105
+ sampleSize = _ref.sampleSize;
106
+ var state = view.state;
107
+ var nodeCount = getNodeCount(state);
108
+ var averagePayload = {
109
+ action: action,
110
+ actionSubject: _analytics.ACTION_SUBJECT.EDITOR,
111
+ attributes: _objectSpread(_objectSpread({
112
+ mean: mean,
113
+ median: median,
114
+ sampleSize: sampleSize
115
+ }, nodeCount), {}, {
116
+ nodeSize: state.doc.nodeSize,
117
+ severity: shouldTrackSeverity ? severity : undefined,
118
+ objectId: (_getContextIdentifier3 = (0, _contextIdentifier.getContextIdentifier)(state)) === null || _getContextIdentifier3 === void 0 ? void 0 : _getContextIdentifier3.objectId
119
+ }),
120
+ eventType: _analytics.EVENT_TYPE.OPERATIONAL
121
+ };
122
+ dispatchAnalyticsEvent(averagePayload);
123
+ };
124
+ };
125
+ return new _safePlugin.SafePlugin({
126
+ key: frozenEditorPluginKey,
127
+ props: (0, _utils.isPerformanceAPIAvailable)() ? {
128
+ handleTextInput: function handleTextInput(view) {
129
+ if (browserFreezeTracking !== null && browserFreezeTracking !== void 0 && browserFreezeTracking.trackInteractionType) {
130
+ interactionType = _analytics.BROWSER_FREEZE_INTERACTION_TYPE.TYPING;
131
+ }
132
+ if (inputLatencyTracker) {
133
+ var end = inputLatencyTracker.start();
134
+
135
+ // This is called after all handleTextInput events are executed which means first handleTextInput time incorporates following handleTextInput processing time
136
+ // Also this is called before browser rendering so it doesn't count it.
137
+ requestAnimationFrame(end);
138
+ }
139
+ if (inputLatencySingleKeyTracker) {
140
+ var _end = inputLatencySingleKeyTracker.start();
141
+
142
+ // This is executed before next handleTextInput when multiple keypress events are in one animation frame
143
+ // so it tracks individual keypress processing time
144
+ Promise.resolve().then(_end);
145
+ }
146
+ if (inputLatencyRenderedTracker) {
147
+ var _end2 = inputLatencyRenderedTracker.start();
148
+
149
+ // This is called at the next event loop so it counts browser rendering time.
150
+ setTimeout(_end2);
151
+ }
152
+ return false;
153
+ },
154
+ handleDOMEvents: browserFreezeTracking !== null && browserFreezeTracking !== void 0 && browserFreezeTracking.trackInteractionType ? {
155
+ click: function click() {
156
+ interactionType = (0, _frozenEditor.setInteractionType)(_analytics.BROWSER_FREEZE_INTERACTION_TYPE.CLICKING);
157
+ return false;
158
+ },
159
+ paste: function paste() {
160
+ interactionType = (0, _frozenEditor.setInteractionType)(_analytics.BROWSER_FREEZE_INTERACTION_TYPE.PASTING);
161
+ return false;
162
+ }
163
+ } : undefined
164
+ } : undefined,
165
+ view: function view(_view) {
166
+ if (!(0, _utils.isPerformanceObserverAvailable)()) {
167
+ return {};
168
+ }
169
+ var experienceStore = ufo ? _ufo.ExperienceStore.getInstance(_view) : undefined;
170
+ if (inputTracking !== null && inputTracking !== void 0 && inputTracking.enabled) {
171
+ inputLatencyTracker = new _inputLatencyTracking.default({
172
+ samplingRate: samplingRate,
173
+ slowThreshold: slowThreshold,
174
+ normalThreshold: severityThresholdNormal,
175
+ degradedThreshold: severityThresholdDegraded,
176
+ onSampleStart: function onSampleStart() {
177
+ experienceStore === null || experienceStore === void 0 ? void 0 : experienceStore.start(_ufo.EditorExperience.typing);
178
+ },
179
+ onSampleEnd: function onSampleEnd(time, _ref2) {
180
+ var _getContextIdentifier4;
181
+ var isSlow = _ref2.isSlow,
182
+ severity = _ref2.severity;
183
+ var state = _view.state;
184
+ var nodesCount = getNodeCount(state);
185
+ if (isSlow) {
186
+ experienceStore === null || experienceStore === void 0 ? void 0 : experienceStore.addMetadata(_ufo.EditorExperience.typing, {
187
+ slowInput: true
188
+ });
189
+ }
190
+ experienceStore === null || experienceStore === void 0 ? void 0 : experienceStore.success(_ufo.EditorExperience.typing, _objectSpread(_objectSpread({
191
+ nodeSize: state.doc.nodeSize
192
+ }, nodesCount), {}, {
193
+ objectId: (_getContextIdentifier4 = (0, _contextIdentifier.getContextIdentifier)(state)) === null || _getContextIdentifier4 === void 0 ? void 0 : _getContextIdentifier4.objectId,
194
+ time: time,
195
+ severity: shouldTrackSeverity ? severity : undefined
196
+ }));
197
+ },
198
+ dispatchSample: createDispatchSample(_analytics.ACTION.INPUT_PERF_SAMPLING, _view),
199
+ dispatchAverage: createDispatchAverage(_analytics.ACTION.INPUT_PERF_SAMPLING_AVG, _view),
200
+ onSlowInput: function onSlowInput(time) {
201
+ var _getContextIdentifier5;
202
+ var state = _view.state;
203
+ var nodesCount = getNodeCount(state);
204
+ dispatchAnalyticsEvent({
205
+ action: _analytics.ACTION.SLOW_INPUT,
206
+ actionSubject: _analytics.ACTION_SUBJECT.EDITOR,
207
+ attributes: _objectSpread(_objectSpread({
208
+ time: time,
209
+ nodeSize: state.doc.nodeSize
210
+ }, nodesCount), {}, {
211
+ objectId: (_getContextIdentifier5 = (0, _contextIdentifier.getContextIdentifier)(state)) === null || _getContextIdentifier5 === void 0 ? void 0 : _getContextIdentifier5.objectId
212
+ }),
213
+ eventType: _analytics.EVENT_TYPE.OPERATIONAL
214
+ });
215
+ }
216
+ });
217
+ }
218
+ if (inputTracking !== null && inputTracking !== void 0 && inputTracking.trackSingleKeypress) {
219
+ inputLatencySingleKeyTracker = new _inputLatencyTracking.default({
220
+ samplingRate: samplingRate,
221
+ slowThreshold: slowThreshold,
222
+ normalThreshold: severityThresholdNormal,
223
+ degradedThreshold: severityThresholdDegraded,
224
+ dispatchSample: createDispatchSample(_analytics.ACTION.INPUT_PERF_SAMPLING_SINGLE_KEYPRESS, _view),
225
+ dispatchAverage: createDispatchAverage(_analytics.ACTION.INPUT_PERF_SAMPLING_SINGLE_KEYPRESS_AVG, _view)
226
+ });
227
+ }
228
+ if (inputTracking !== null && inputTracking !== void 0 && inputTracking.trackRenderingTime) {
229
+ inputLatencyRenderedTracker = new _inputLatencyTracking.default({
230
+ samplingRate: samplingRate,
231
+ slowThreshold: slowThreshold,
232
+ normalThreshold: severityThresholdNormal,
233
+ degradedThreshold: severityThresholdDegraded,
234
+ dispatchSample: createDispatchSample(_analytics.ACTION.INPUT_PERF_SAMPLING_RENDERED, _view),
235
+ dispatchAverage: createDispatchAverage(_analytics.ACTION.INPUT_PERF_SAMPLING_RENDERED_AVG, _view)
236
+ });
237
+ }
238
+ var observer;
239
+ try {
240
+ var _observer = new PerformanceObserver(function (list) {
241
+ var perfEntries = list.getEntries();
242
+ for (var i = 0; i < perfEntries.length; i++) {
243
+ var duration = perfEntries[i].duration;
244
+ if (duration > freezeThreshold) {
245
+ dispatchLongTaskEvent(dispatchAnalyticsEvent, _view, duration, getNodeCount, browserFreezeTracking !== null && browserFreezeTracking !== void 0 && browserFreezeTracking.trackInteractionType ? interactionType : undefined, browserFreezeTracking !== null && browserFreezeTracking !== void 0 && browserFreezeTracking.trackSeverity ? (0, _utils.getAnalyticsEventSeverity)(duration, browserFreezeTracking.severityNormalThreshold || NORMAL_SEVERITY_THRESHOLD, browserFreezeTracking.severityDegradedThreshold || DEGRADED_SEVERITY_THRESHOLD) : undefined);
246
+ }
247
+ }
248
+ });
249
+
250
+ // register observer for long task notifications
251
+ _observer.observe({
252
+ entryTypes: ['longtask']
253
+ });
254
+ } catch (e) {}
255
+ return {
256
+ destroy: function destroy() {
257
+ var _inputLatencyTracker;
258
+ (_inputLatencyTracker = inputLatencyTracker) === null || _inputLatencyTracker === void 0 ? void 0 : _inputLatencyTracker.flush();
259
+ observer === null || observer === void 0 ? void 0 : observer.disconnect();
260
+ }
261
+ };
262
+ }
263
+ });
264
+ };
265
+ exports.default = _default;
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isInlineNodeView = exports.inlineCursorTargetStateKey = exports.default = void 0;
7
+ var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
8
+ var _utils = require("@atlaskit/editor-common/utils");
9
+ var _state = require("@atlaskit/editor-prosemirror/state");
10
+ var _view = require("@atlaskit/editor-prosemirror/view");
11
+ var inlineCursorTargetStateKey = new _state.PluginKey('inlineCursorTargetPlugin');
12
+ exports.inlineCursorTargetStateKey = inlineCursorTargetStateKey;
13
+ var isInlineNodeView = function isInlineNodeView(node) {
14
+ return node && node.type.isInline && !node.type.isText;
15
+ };
16
+ exports.isInlineNodeView = isInlineNodeView;
17
+ var _default = function _default() {
18
+ return new _safePlugin.SafePlugin({
19
+ key: inlineCursorTargetStateKey,
20
+ state: {
21
+ init: function init() {
22
+ return {
23
+ cursorTarget: undefined
24
+ };
25
+ },
26
+ apply: function apply(tr) {
27
+ var selection = tr.selection,
28
+ doc = tr.doc;
29
+ var $from = selection.$from,
30
+ $to = selection.$to;
31
+ // In Safari, if the cursor target is to the right of the cursor it will block selections
32
+ // made with shift + arrowRight and vice versa for shift + arrowLeft. This is due to a
33
+ // contenteditable bug in safari, where editable elements block the selection but we need
34
+ // the cursor target to be editable for the following:
35
+ // - Ability to navigate with down/up arrows when between inline nodes
36
+ // - Ability to navigate with down/up arrows when between the start of a paragraph & an inline node
37
+ // - Ability to click and drag to select an inline node if it is the first node
38
+ // To prevent blocking the selection, we check handleDOMEvents and add meta to
39
+ // the transaction to prevent the plugin from making cursor target decorations.
40
+ var safariShiftSelection = tr.getMeta(inlineCursorTargetStateKey);
41
+ if (selection && (0, _utils.isTextSelection)(selection) && !safariShiftSelection) {
42
+ var hasInlineNodeViewAfter = isInlineNodeView($from.nodeAfter);
43
+ var hasInlineNodeViewBefore = isInlineNodeView($from.nodeBefore);
44
+ var isAtStartAndInlineNodeViewAfter = $from.parentOffset === 0 && isInlineNodeView($from.nodeAfter);
45
+ var isAtEndAndInlineNodeViewBefore = doc.resolve($from.pos).node().lastChild === $from.nodeBefore && isInlineNodeView($from.nodeBefore);
46
+ var createWidget = function createWidget(side) {
47
+ var node = document.createElement('span');
48
+ node.contentEditable = 'true';
49
+ node.setAttribute('aria-hidden', 'true');
50
+ node.appendChild(document.createTextNode(_utils.ZERO_WIDTH_SPACE));
51
+ node.className = 'cursor-target';
52
+ var $from = selection.$from;
53
+ var rightPosition = $from.pos;
54
+ var leftPosition = $from.posAtIndex(Math.max($from.index() - 1, 0));
55
+ var widgetPos = side === 'left' ? leftPosition : rightPosition;
56
+ return _view.Decoration.widget(widgetPos, node, {
57
+ raw: true,
58
+ key: 'inlineCursor'
59
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
+ });
61
+ };
62
+
63
+ // Create editable decoration widgets around the current inline node to allow proper cursor navigation.
64
+ if ((hasInlineNodeViewAfter || isAtEndAndInlineNodeViewBefore) && (hasInlineNodeViewBefore || isAtStartAndInlineNodeViewAfter)) {
65
+ return {
66
+ cursorTarget: {
67
+ decorations: [createWidget('left'), createWidget('right')],
68
+ positions: {
69
+ from: $from.pos,
70
+ to: $to.pos
71
+ }
72
+ }
73
+ };
74
+ }
75
+ }
76
+ return {
77
+ cursorTarget: undefined
78
+ };
79
+ }
80
+ },
81
+ props: {
82
+ decorations: function decorations(state) {
83
+ var doc = state.doc;
84
+ var _ref = inlineCursorTargetStateKey.getState(state),
85
+ cursorTarget = _ref.cursorTarget;
86
+ if (cursorTargetHasValidDecorations(cursorTarget)) {
87
+ return _view.DecorationSet.create(doc, cursorTarget.decorations);
88
+ }
89
+ return null;
90
+ },
91
+ handleDOMEvents: {
92
+ // Workaround to prevent the decorations created by the plugin from
93
+ // blocking shift + arrow left/right selections in safari. When
94
+ // a shift + arrow left/right event is detected, send meta data to the
95
+ // plugin to prevent it from creating decorations.
96
+ // TODO We may be able to remove this when playing the following ticket:
97
+ // https://product-fabric.atlassian.net/browse/ED-14938
98
+ keydown: function keydown(view, event) {
99
+ if (_utils.browser.safari && event instanceof KeyboardEvent && event.shiftKey && (event.key === 'ArrowLeft' || event.key === 'ArrowRight')) {
100
+ view.dispatch(view.state.tr.setMeta(inlineCursorTargetStateKey, {
101
+ cursorTarget: undefined
102
+ }));
103
+ }
104
+ return false;
105
+ },
106
+ // Check the DOM to see if there are inline cursor targets
107
+ // after a composition event ends. If so, manually insert the
108
+ // event data in order to prevent contents ending up inside
109
+ // of the cursor target decorations.
110
+ compositionend: function compositionend(view, incorrectlyTypedEvent) {
111
+ // This is typed by the prosemirror definitions as Event,
112
+ // this type is incorrect, and it is actually an InputEvent
113
+ var event = incorrectlyTypedEvent;
114
+ var state = view.state;
115
+ var _ref2 = inlineCursorTargetStateKey.getState(state),
116
+ cursorTarget = _ref2.cursorTarget;
117
+ if (cursorTarget !== undefined) {
118
+ handleTextInputInsideCursorTargetDecoration({
119
+ event: event,
120
+ cursorTarget: cursorTarget,
121
+ view: view
122
+ });
123
+ return true;
124
+ }
125
+ return false;
126
+ },
127
+ // Check the DOM to see if there are inline cursor targets
128
+ // before any input event. If so, manually insert the
129
+ // event data in order to prevent contents ending up inside
130
+ // of the cursor target decorations.
131
+ beforeinput: function beforeinput(view, incorrectlyTypedEvent) {
132
+ // This is typed by the prosemirror definitions as Event,
133
+ // this type is incorrect, and it is actually an InputEvent
134
+ var event = incorrectlyTypedEvent;
135
+ var state = view.state;
136
+ var _ref3 = inlineCursorTargetStateKey.getState(state),
137
+ cursorTarget = _ref3.cursorTarget;
138
+ if (!event.isComposing && cursorTarget !== undefined) {
139
+ handleTextInputInsideCursorTargetDecoration({
140
+ event: event,
141
+ cursorTarget: cursorTarget,
142
+ view: view
143
+ });
144
+ return true;
145
+ }
146
+ return false;
147
+ }
148
+ }
149
+ }
150
+ });
151
+ };
152
+ exports.default = _default;
153
+ function cursorTargetHasValidDecorations(cursorTarget) {
154
+ if (!cursorTarget ||
155
+ // Decorations can end up as null when the decorations prop is
156
+ // called after the decorations have been removed from the dom.
157
+ // https://github.com/ProseMirror/prosemirror-view/blob/8f0d313a6389b86a335274fba36534ba1cb21f12/src/decoration.js#L30
158
+ cursorTarget.decorations.includes(null)) {
159
+ return false;
160
+ }
161
+ return true;
162
+ }
163
+ function handleTextInputInsideCursorTargetDecoration(_ref4) {
164
+ var event = _ref4.event,
165
+ view = _ref4.view,
166
+ cursorTarget = _ref4.cursorTarget;
167
+ event.stopPropagation();
168
+ event.preventDefault();
169
+ var content = event.data || '';
170
+ var tr = view.state.tr;
171
+
172
+ // ensure any custom handleTextInput handlers are called for the input event
173
+ // ie. type ahead, emoji shortcuts.
174
+ var potentiallyHandleByHandleTextInput = view.someProp('handleTextInput', function (f) {
175
+ return f(view, cursorTarget.positions.from, cursorTarget.positions.to, content);
176
+ });
177
+ if (potentiallyHandleByHandleTextInput) {
178
+ // if a handleTextInput handler has handled the event, we don't want to
179
+ // manually update the document.
180
+ return;
181
+ }
182
+ tr.insertText(content);
183
+ view.dispatch(tr);
184
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.newlinePreserveMarksKey = exports.default = void 0;
7
+ var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
8
+ var _utils = require("@atlaskit/editor-common/utils");
9
+ var _keymap = require("@atlaskit/editor-prosemirror/keymap");
10
+ var _state = require("@atlaskit/editor-prosemirror/state");
11
+ var newlinePreserveMarksKey = new _state.PluginKey('newlinePreserveMarksPlugin');
12
+ exports.newlinePreserveMarksKey = newlinePreserveMarksKey;
13
+ var isSelectionAligned = function isSelectionAligned(state) {
14
+ return !!state.selection.$to.parent.marks.find(function (m) {
15
+ return m.type === state.schema.marks.alignment;
16
+ });
17
+ };
18
+ var splitBlockPreservingMarks = function splitBlockPreservingMarks(state, dispatch) {
19
+ if (dispatch) {
20
+ dispatch(state.tr.split(state.tr.mapping.map(state.selection.$from.pos), 1));
21
+ }
22
+ return true;
23
+ };
24
+ var _default = function _default() {
25
+ return new _safePlugin.SafePlugin({
26
+ key: newlinePreserveMarksKey,
27
+ props: {
28
+ handleKeyDown: (0, _keymap.keydownHandler)({
29
+ Enter: (0, _utils.filterCommand)([_utils.isSelectionEndOfParagraph, isSelectionAligned], splitBlockPreservingMarks)
30
+ })
31
+ }
32
+ });
33
+ };
34
+ exports.default = _default;