@atlaskit/editor-plugin-base 2.0.12 → 2.1.1

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.
@@ -1,155 +1,29 @@
1
1
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
- import { GUTTER_SELECTOR, GUTTER_SIZE_IN_PX, isEmptyDocument } from '@atlaskit/editor-common/utils';
3
2
  import { scrollGutterPluginKey } from './plugin-key';
4
- import { getKeyboardHeight } from './util/get-keyboard-height';
5
- const MIN_TAP_SIZE_IN_PX = 40;
6
- function supportsIntersectionObserver() {
7
- if (typeof window !== 'undefined' && 'IntersectionObserver' in window && 'IntersectionObserverEntry' in window && 'intersectionRatio' in
8
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
- window.IntersectionObserverEntry.prototype) {
10
- return true;
11
- }
12
- return false;
13
- }
14
- function listenForGutterVisibilityChanges(scrollElement, gutterIsVisible) {
15
- if (supportsIntersectionObserver()) {
16
- const observer = new IntersectionObserver((entries, _) => {
17
- entries.forEach(entry => {
18
- gutterIsVisible(entry.intersectionRatio > 0);
19
- });
20
- }, {
21
- root: scrollElement,
22
- rootMargin: '0px',
23
- threshold: 0
24
- });
25
- return observer;
26
- }
27
- return undefined;
28
- }
3
+ const GUTTER_SIZE_IN_PX = 120;
4
+ const GUTTER_SELECTOR_NAME = 'editor-scroll-gutter';
29
5
 
30
6
  /**
31
7
  * Create a gutter element that can be added or removed from the DOM.
32
8
  */
33
- function createGutter(gutterSize) {
9
+ function createGutter(gutterSize, parent) {
34
10
  const gutter = document.createElement('div');
35
11
  gutter.style.paddingBottom = `${gutterSize}px`;
36
- gutter.id = GUTTER_SELECTOR.substr(1);
37
- let initialized = false;
38
- let mounted = false;
39
- let currentParent = null;
40
- let observer;
41
- let isVisible = false;
42
- return {
43
- addGutter(parent) {
44
- if (parent) {
45
- var _parent$deref;
46
- currentParent = parent;
47
- (_parent$deref = parent.deref()) === null || _parent$deref === void 0 ? void 0 : _parent$deref.appendChild(gutter);
48
- mounted = true;
49
- if (observer) {
50
- observer.observe(gutter);
51
- }
52
- }
53
- },
54
- removeGutter() {
55
- var _currentParent;
56
- const parentRef = (_currentParent = currentParent) === null || _currentParent === void 0 ? void 0 : _currentParent.deref();
57
- if (parentRef && mounted) {
58
- mounted = false;
59
- parentRef.removeChild(gutter);
60
- if (observer) {
61
- observer.unobserve(gutter);
62
- }
63
- }
64
- },
65
- element() {
66
- return gutter;
67
- },
68
- isMounted() {
69
- return mounted;
70
- },
71
- visible() {
72
- // If we know whether it's visible we can avoid expensive calculations
73
- if (observer) {
74
- return isVisible;
75
- }
76
- // Fallback for legacy browsers assumes it's visible (if mounted)
77
- return mounted;
78
- },
79
- observe(scrollElement) {
80
- if (!initialized) {
81
- initialized = true;
82
- observer = listenForGutterVisibilityChanges(scrollElement, visible => isVisible = visible);
83
- }
84
- },
85
- destroy() {
86
- if (observer) {
87
- observer.disconnect();
88
- }
89
- observer = undefined;
90
- this.removeGutter();
91
- initialized = mounted = false;
92
- }
93
- };
94
- }
95
-
96
- /**
97
- * Get caret top position given the current selection,
98
- * use start container position as fallback
99
- */
100
- function getCaretTopPosition() {
101
- const windowSelection = window.getSelection();
102
- if (windowSelection && windowSelection.rangeCount > 0) {
103
- const range = windowSelection.getRangeAt(0);
104
- if (range) {
105
- const clientRects = range.getClientRects();
106
- // Return client rects
107
- if (clientRects && clientRects.length > 0) {
108
- return clientRects[0].top;
109
- }
110
-
111
- // Return container top
112
- // Ignored via go/ees005
113
- // eslint-disable-next-line @atlaskit/editor/no-as-casting
114
- const container = range.startContainer;
115
- if (container && container.getBoundingClientRect) {
116
- return container.getBoundingClientRect().top;
117
- }
118
- }
12
+ gutter.setAttribute('data-vc', 'scroll-gutter');
13
+ gutter.id = GUTTER_SELECTOR_NAME;
14
+ if (parent) {
15
+ parent.appendChild(gutter);
119
16
  }
120
- return;
121
- }
122
- function scrollToGutterElement(scrollContainer, gutterElement, gutterSize) {
123
- const viewportHeight = scrollContainer.offsetHeight;
124
- const viewportOffsetY = scrollContainer.getBoundingClientRect().top;
125
- const caretTopPosition = getCaretTopPosition();
126
- if (!caretTopPosition) {
127
- return false;
128
- }
129
- const caretTopFromContainer = caretTopPosition - viewportOffsetY;
130
- const gutterThresholdTop = viewportHeight - gutterSize - MIN_TAP_SIZE_IN_PX * 2;
131
- if (caretTopFromContainer < gutterThresholdTop) {
132
- return false;
133
- }
134
-
135
- // Clamp the scroll position to above the scroll gutter element.
136
- gutterElement.scrollIntoView(false);
137
-
138
- // Mark scrolling as handled so that other plugin's don't override our position.
139
- return true;
17
+ return () => parent === null || parent === void 0 ? void 0 : parent.removeChild(gutter);
140
18
  }
141
19
  export default ((pluginOptions = {}) => {
142
20
  const {
143
21
  getScrollElement,
144
- allowCustomScrollHandler,
145
22
  gutterSize = GUTTER_SIZE_IN_PX
146
23
  } = pluginOptions;
147
24
  if (!getScrollElement) {
148
25
  return undefined;
149
26
  }
150
- const gutter = createGutter(gutterSize);
151
- let scrollElement = null; // | undefined;
152
-
153
27
  return new SafePlugin({
154
28
  key: scrollGutterPluginKey,
155
29
  state: {
@@ -165,104 +39,25 @@ export default ((pluginOptions = {}) => {
165
39
  // Determines the distance (in pixels) between the cursor and the end of the visible viewport at which point,
166
40
  // when scrolling the cursor into view, scrolling takes place.
167
41
  // Defaults to 0: https://prosemirror.net/docs/ref/#view.EditorProps.scrollThreshold
168
- scrollThreshold: gutterSize / 2,
42
+ scrollThreshold: gutterSize,
169
43
  // Determines the extra space (in pixels) that is left above or below the cursor when it is scrolled into view.
170
44
  // Defaults to 5: https://prosemirror.net/docs/ref/#view.EditorProps.scrollMargin
171
- scrollMargin: gutterSize,
172
- // Called when the view, after updating its state, tries to scroll the selection into view
173
- // https://prosemirror.net/docs/ref/#view.EditorProps.handleScrollToSelection
174
- handleScrollToSelection: () => {
175
- if (allowCustomScrollHandler === false) {
176
- return false;
177
- }
178
- if (!gutter.isMounted() || !gutter.visible() || !scrollElement) {
179
- // Avoid scrolling until applicable
180
- return false;
181
- }
182
- return scrollToGutterElement(scrollElement, gutter.element(), gutterSize);
183
- }
45
+ scrollMargin: gutterSize
184
46
  },
185
47
  view(view) {
48
+ var _editorElement;
186
49
  // Store references to avoid lookups on successive checks.
187
- scrollElement = getScrollElement(view);
188
- // Ignored via go/ees005
189
- // eslint-disable-next-line @atlaskit/editor/no-as-casting
190
- let editorElement = view.dom;
191
- let editorParentElement = editorElement.parentElement;
192
- let viewportElementHeight = 0;
193
- let contentElementHeight = 0;
194
- const addAndObserveGutter = () => {
195
- if (!gutter.isMounted()) {
196
- // Ignored via go/ees005
197
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
198
- gutter.observe(scrollElement);
199
- gutter.addGutter(editorParentElement ? new WeakRef(editorParentElement) : null);
200
- }
201
- };
202
- const toggleGutter = (contentSize, threshold) => {
203
- if (pluginOptions.persistScrollGutter) {
204
- if (!isEmptyDocument(view.state.doc)) {
205
- addAndObserveGutter();
206
- } else {
207
- gutter.removeGutter();
208
- }
209
- return;
210
- }
211
-
212
- // Add or remove the gutter based on whether the content is about to exceed the viewport height.
213
- // We do this to ensure there is sufficient white space below the last content node in order to
214
- // see any UI control elements which may sit beneath it.
215
- if (contentSize >= threshold) {
216
- addAndObserveGutter();
217
- } else {
218
- if (gutter.isMounted()) {
219
- gutter.removeGutter();
220
- }
221
- }
222
- };
223
- const observerCallback = entries => {
224
- for (const entry of entries) {
225
- const elementHeight = Math.round(entry.contentRect.height);
226
- if (entry.target === scrollElement) {
227
- var _getKeyboardHeight;
228
- const keyboardHeight = (_getKeyboardHeight = getKeyboardHeight(view === null || view === void 0 ? void 0 : view.state)) !== null && _getKeyboardHeight !== void 0 ? _getKeyboardHeight : 0;
229
- viewportElementHeight = elementHeight - keyboardHeight;
230
- } else if (entry.target === editorParentElement) {
231
- contentElementHeight = elementHeight - (gutter.isMounted() ? gutterSize : 0);
232
- }
233
- }
234
- toggleGutter(contentElementHeight, viewportElementHeight);
235
- };
236
-
237
- // Subscribe to updates of the height of the viewport and content elements
238
- // We toggle the value when it is set here or when PM updates to ensure we're using
239
- // the latest.
240
- const resizeObserver = new ResizeObserver(observerCallback);
241
- if (scrollElement && editorParentElement) {
242
- resizeObserver.observe(scrollElement);
243
- resizeObserver.observe(editorParentElement);
50
+ const scrollElement = getScrollElement(view);
51
+ let editorElement = view.dom instanceof HTMLElement ? view.dom : null;
52
+ let editorParentElement = (_editorElement = editorElement) === null || _editorElement === void 0 ? void 0 : _editorElement.parentElement;
53
+ let cleanup = () => {};
54
+ if (editorParentElement && scrollElement) {
55
+ cleanup = createGutter(gutterSize, editorParentElement);
244
56
  }
245
57
  return {
246
58
  destroy() {
247
- // Remove if it's mounted
248
- gutter.destroy();
249
- scrollElement = editorParentElement = editorElement = null;
250
- resizeObserver.disconnect();
251
- },
252
- /**
253
- * Toggle the Scroll Gutter Element
254
- */
255
- update(view, prevState) {
256
- if (!scrollElement || !editorParentElement) {
257
- return;
258
- }
259
- const state = view.state;
260
- if (prevState.selection === state.selection) {
261
- // No need to recheck if the selected node hasn't changed.
262
- return;
263
- }
264
- const gutterThresholdHeight = viewportElementHeight - gutterSize;
265
- toggleGutter(contentElementHeight, gutterThresholdHeight);
59
+ cleanup();
60
+ editorParentElement = editorElement = null;
266
61
  }
267
62
  };
268
63
  }
@@ -5,7 +5,6 @@ import { doc, paragraph, text } from '@atlaskit/adf-schema';
5
5
  import { keymap } from '@atlaskit/editor-common/keymaps';
6
6
  import { baseKeymap } from '@atlaskit/editor-prosemirror/commands';
7
7
  import { history } from '@atlaskit/editor-prosemirror/history';
8
- import { fg } from '@atlaskit/platform-feature-flags';
9
8
  import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
10
9
  import { setKeyboardHeight } from './editor-commands/set-keyboard-height';
11
10
  import disableSpellcheckingPlugin from './pm-plugins/disable-spell-checking';
@@ -15,14 +14,64 @@ import inlineCursorTargetPlugin from './pm-plugins/inline-cursor-target';
15
14
  import { createLazyNodeViewDecorationPlugin } from './pm-plugins/lazy-node-view-decoration';
16
15
  import newlinePreserveMarksPlugin from './pm-plugins/newline-preserve-marks';
17
16
  import scrollGutter from './pm-plugins/scroll-gutter/plugin';
18
- import scrollGutterNext from './pm-plugins/scroll-gutter/plugin-next';
19
17
  import { getKeyboardHeight } from './pm-plugins/scroll-gutter/util/get-keyboard-height';
20
18
  import { inputTracking } from './pm-plugins/utils/inputTrackingConfig';
19
+ export function resolveCallbacks(from, to, tr, callbacks) {
20
+ var doc = tr.doc;
21
+ doc.nodesBetween(from, to, function (node, pos) {
22
+ callbacks.forEach(function (cb) {
23
+ return cb({
24
+ tr: tr,
25
+ node: node,
26
+ pos: pos,
27
+ from: from,
28
+ to: to
29
+ });
30
+ });
31
+ });
32
+ }
33
+ var SMART_TO_ASCII = {
34
+ '…': '...',
35
+ '→': '->',
36
+ '←': '<-',
37
+ '–': '--',
38
+ '“': '"',
39
+ '”': '"',
40
+ '‘': "'",
41
+ '’': "'"
42
+ };
43
+
44
+ // eslint-disable-next-line require-unicode-regexp
45
+ var FIND_SMART_CHAR = new RegExp("[".concat(Object.keys(SMART_TO_ASCII).join(''), "]"), 'g');
21
46
  var basePlugin = function basePlugin(_ref) {
22
- var _api$featureFlags;
47
+ var _api$featureFlags, _api$base;
23
48
  var options = _ref.config,
24
49
  api = _ref.api;
25
50
  var featureFlags = (api === null || api === void 0 || (_api$featureFlags = api.featureFlags) === null || _api$featureFlags === void 0 ? void 0 : _api$featureFlags.sharedState.currentState()) || {};
51
+ var callbacks = [];
52
+ api === null || api === void 0 || (_api$base = api.base) === null || _api$base === void 0 || _api$base.actions.registerMarks(function (_ref2) {
53
+ var tr = _ref2.tr,
54
+ node = _ref2.node,
55
+ pos = _ref2.pos,
56
+ from = _ref2.from,
57
+ to = _ref2.to;
58
+ var doc = tr.doc;
59
+ var schema = doc.type.schema;
60
+ var textNodeType = schema.nodes.text;
61
+ if (node.type === textNodeType && node.text) {
62
+ // Find a valid start and end position because the text may be partially selected.
63
+ var startPositionInSelection = Math.max(pos, from);
64
+ var endPositionInSelection = Math.min(pos + node.nodeSize, to);
65
+ var textForReplacing = doc.textBetween(startPositionInSelection, endPositionInSelection);
66
+ var newText = textForReplacing.replace(FIND_SMART_CHAR, function (match) {
67
+ var _SMART_TO_ASCII$match;
68
+ return (_SMART_TO_ASCII$match = SMART_TO_ASCII[match]) !== null && _SMART_TO_ASCII$match !== void 0 ? _SMART_TO_ASCII$match : match;
69
+ });
70
+ var currentStartPos = tr.mapping.map(startPositionInSelection);
71
+ var currentEndPos = tr.mapping.map(endPositionInSelection);
72
+ tr.replaceWith(currentStartPos, currentEndPos, schema.text(newText, node.marks));
73
+ }
74
+ });
26
75
  return {
27
76
  name: 'base',
28
77
  getSharedState: function getSharedState(editorState) {
@@ -31,13 +80,19 @@ var basePlugin = function basePlugin(_ref) {
31
80
  };
32
81
  },
33
82
  actions: {
34
- setKeyboardHeight: setKeyboardHeight
83
+ setKeyboardHeight: setKeyboardHeight,
84
+ resolveMarks: function resolveMarks(from, to, tr) {
85
+ return resolveCallbacks(from, to, tr, callbacks);
86
+ },
87
+ registerMarks: function registerMarks(callback) {
88
+ callbacks.push(callback);
89
+ }
35
90
  },
36
91
  pmPlugins: function pmPlugins() {
37
92
  var plugins = [{
38
93
  name: 'filterStepsPlugin',
39
- plugin: function plugin(_ref2) {
40
- var dispatchAnalyticsEvent = _ref2.dispatchAnalyticsEvent;
94
+ plugin: function plugin(_ref3) {
95
+ var dispatchAnalyticsEvent = _ref3.dispatchAnalyticsEvent;
41
96
  return filterStepsPlugin(dispatchAnalyticsEvent);
42
97
  }
43
98
  }];
@@ -74,8 +129,8 @@ var basePlugin = function basePlugin(_ref) {
74
129
  plugin: newlinePreserveMarksPlugin
75
130
  }, {
76
131
  name: 'frozenEditor',
77
- plugin: function plugin(_ref3) {
78
- var dispatchAnalyticsEvent = _ref3.dispatchAnalyticsEvent;
132
+ plugin: function plugin(_ref4) {
133
+ var dispatchAnalyticsEvent = _ref4.dispatchAnalyticsEvent;
79
134
  return frozenEditor(api === null || api === void 0 ? void 0 : api.contextIdentifier)(dispatchAnalyticsEvent, inputTracking, undefined);
80
135
  }
81
136
  }, {
@@ -99,21 +154,12 @@ var basePlugin = function basePlugin(_ref) {
99
154
  }
100
155
  });
101
156
  if (options && options.allowScrollGutter) {
102
- if (fg('platform_editor_improved_scroll_gutter')) {
103
- plugins.push({
104
- name: 'scrollGutterPlugin',
105
- plugin: function plugin() {
106
- return scrollGutterNext(options.allowScrollGutter);
107
- }
108
- });
109
- } else {
110
- plugins.push({
111
- name: 'scrollGutterPlugin',
112
- plugin: function plugin() {
113
- return scrollGutter(options.allowScrollGutter);
114
- }
115
- });
116
- }
157
+ plugins.push({
158
+ name: 'scrollGutterPlugin',
159
+ plugin: function plugin() {
160
+ return scrollGutter(options.allowScrollGutter);
161
+ }
162
+ });
117
163
  }
118
164
  plugins.push({
119
165
  name: 'disableSpellcheckingPlugin',
@@ -19,10 +19,7 @@ var DEFAULT_TRACK_SEVERITY_ENABLED = false;
19
19
  export var DEFAULT_TRACK_SEVERITY_THRESHOLD_NORMAL = 100;
20
20
  export var DEFAULT_TRACK_SEVERITY_THRESHOLD_DEGRADED = 500;
21
21
  var dispatchLongTaskEvent = function dispatchLongTaskEvent(contextIdentifierPlugin) {
22
- return function (dispatchAnalyticsEvent, view, time, getNodeCount, interactionType, severity
23
- // Ignored via go/ees005
24
- // eslint-disable-next-line @typescript-eslint/max-params
25
- ) {
22
+ return function (dispatchAnalyticsEvent, view, time, getNodeCount, interactionType, severity) {
26
23
  var _contextIdentifierPlu;
27
24
  var state = view.state;
28
25
  var nodesCount = getNodeCount(state);