@atlaskit/editor-plugin-base 0.3.0 → 0.3.2

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.
@@ -0,0 +1,244 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import { GUTTER_SELECTOR, GUTTER_SIZE_IN_PX, isEmptyDocument } from '@atlaskit/editor-common/utils';
3
+ import { getScrollGutterPluginState, scrollGutterPluginKey } from './plugin-key';
4
+ export const getKeyboardHeight = state => {
5
+ if (state) {
6
+ const scrollGutterPluginState = getScrollGutterPluginState(state);
7
+ return scrollGutterPluginState ? scrollGutterPluginState.keyboardHeight : undefined;
8
+ }
9
+ };
10
+ const MIN_TAP_SIZE_IN_PX = 40;
11
+ function supportsIntersectionObserver() {
12
+ if (typeof window !== 'undefined' && 'IntersectionObserver' in window && 'IntersectionObserverEntry' in window && 'intersectionRatio' in
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ window.IntersectionObserverEntry.prototype) {
15
+ return true;
16
+ }
17
+ return false;
18
+ }
19
+ function listenForGutterVisibilityChanges(scrollElement, gutterIsVisible) {
20
+ if (supportsIntersectionObserver()) {
21
+ const observer = new IntersectionObserver((entries, _) => {
22
+ entries.forEach(entry => {
23
+ gutterIsVisible(entry.intersectionRatio > 0);
24
+ });
25
+ }, {
26
+ root: scrollElement,
27
+ rootMargin: '0px',
28
+ threshold: 0
29
+ });
30
+ return observer;
31
+ }
32
+ return undefined;
33
+ }
34
+
35
+ /**
36
+ * Create a gutter element that can be added or removed from the DOM.
37
+ */
38
+ function createGutter(gutterSize) {
39
+ const gutter = document.createElement('div');
40
+ gutter.style.paddingBottom = `${gutterSize}px`;
41
+ gutter.id = GUTTER_SELECTOR.substr(1);
42
+ let initialized = false;
43
+ let mounted = false;
44
+ let currentParent = null;
45
+ let observer;
46
+ let isVisible = false;
47
+ return {
48
+ addGutter(parent) {
49
+ if (parent) {
50
+ currentParent = parent;
51
+ parent.appendChild(gutter);
52
+ mounted = true;
53
+ if (observer) {
54
+ observer.observe(gutter);
55
+ }
56
+ }
57
+ },
58
+ removeGutter() {
59
+ if (currentParent && mounted) {
60
+ mounted = false;
61
+ currentParent.removeChild(gutter);
62
+ if (observer) {
63
+ observer.unobserve(gutter);
64
+ }
65
+ }
66
+ },
67
+ element() {
68
+ return gutter;
69
+ },
70
+ isMounted() {
71
+ return mounted;
72
+ },
73
+ visible() {
74
+ // If we know whether it's visible we can avoid expensive calculations
75
+ if (observer) {
76
+ return isVisible;
77
+ }
78
+ // Fallback for legacy browsers assumes it's visible (if mounted)
79
+ return mounted;
80
+ },
81
+ observe(scrollElement) {
82
+ if (!initialized) {
83
+ initialized = true;
84
+ observer = listenForGutterVisibilityChanges(scrollElement, visible => isVisible = visible);
85
+ }
86
+ },
87
+ destroy() {
88
+ if (observer) {
89
+ observer.disconnect();
90
+ }
91
+ observer = undefined;
92
+ this.removeGutter();
93
+ initialized = mounted = false;
94
+ }
95
+ };
96
+ }
97
+
98
+ /**
99
+ * Get caret top position given the current selection,
100
+ * use start container position as fallback
101
+ */
102
+ function getCaretTopPosition() {
103
+ const windowSelection = window.getSelection();
104
+ if (windowSelection && windowSelection.rangeCount > 0) {
105
+ const range = windowSelection.getRangeAt(0);
106
+ if (range) {
107
+ const clientRects = range.getClientRects();
108
+ // Return client rects
109
+ if (clientRects && clientRects.length > 0) {
110
+ return clientRects[0].top;
111
+ }
112
+
113
+ // Return container top
114
+ const container = range.startContainer;
115
+ if (container && container.getBoundingClientRect) {
116
+ return container.getBoundingClientRect().top;
117
+ }
118
+ }
119
+ }
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;
140
+ }
141
+ export default ((pluginOptions = {}) => {
142
+ const {
143
+ getScrollElement,
144
+ allowCustomScrollHandler,
145
+ gutterSize = GUTTER_SIZE_IN_PX
146
+ } = pluginOptions;
147
+ if (!getScrollElement) {
148
+ return undefined;
149
+ }
150
+ const gutter = createGutter(gutterSize);
151
+ let scrollElement = null; // | undefined;
152
+
153
+ return new SafePlugin({
154
+ key: scrollGutterPluginKey,
155
+ state: {
156
+ init: () => ({}),
157
+ apply: (tr, pluginState) => {
158
+ if (tr.getMeta(scrollGutterPluginKey)) {
159
+ return tr.getMeta(scrollGutterPluginKey);
160
+ }
161
+ return pluginState;
162
+ }
163
+ },
164
+ props: {
165
+ // Determines the distance (in pixels) between the cursor and the end of the visible viewport at which point,
166
+ // when scrolling the cursor into view, scrolling takes place.
167
+ // Defaults to 0: https://prosemirror.net/docs/ref/#view.EditorProps.scrollThreshold
168
+ scrollThreshold: gutterSize / 2,
169
+ // Determines the extra space (in pixels) that is left above or below the cursor when it is scrolled into view.
170
+ // 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
+ }
184
+ },
185
+ view(view) {
186
+ // Store references to avoid lookups on successive checks.
187
+ scrollElement = getScrollElement(view);
188
+ let editorElement = view.dom;
189
+ let editorParentElement = editorElement.parentElement;
190
+ return {
191
+ destroy() {
192
+ // Remove if it's mounted
193
+ gutter.destroy();
194
+ scrollElement = editorParentElement = editorElement = null;
195
+ },
196
+ /**
197
+ * Toggle the Scroll Gutter Element
198
+ */
199
+ update(view, prevState) {
200
+ var _getKeyboardHeight;
201
+ if (!scrollElement || !editorParentElement) {
202
+ return;
203
+ }
204
+ const state = view.state;
205
+ if (prevState.selection === state.selection) {
206
+ // No need to recheck if the selected node hasn't changed.
207
+ return;
208
+ }
209
+ const gutterMounted = gutter.isMounted();
210
+ const addAndObserveGutter = () => {
211
+ if (!gutterMounted) {
212
+ gutter.observe(scrollElement);
213
+ gutter.addGutter(editorParentElement);
214
+ }
215
+ };
216
+ if (pluginOptions.persistScrollGutter) {
217
+ if (!isEmptyDocument(state.doc)) {
218
+ addAndObserveGutter();
219
+ } else {
220
+ gutter.removeGutter();
221
+ }
222
+ return;
223
+ }
224
+ // Determine whether we need to consider Keyboard Height
225
+ const keyboardHeight = (_getKeyboardHeight = getKeyboardHeight(state)) !== null && _getKeyboardHeight !== void 0 ? _getKeyboardHeight : 0;
226
+ const viewportHeight = scrollElement.offsetHeight - keyboardHeight;
227
+ const contentHeight = editorParentElement.offsetHeight - (gutterMounted ? gutterSize : 0);
228
+
229
+ // Add or remove the gutter based on whether the content is about to exceed the viewport height.
230
+ // We do this to ensure there is sufficient white space below the last content node in order to
231
+ // see any UI control elements which may sit beneath it.
232
+ const gutterThresholdHeight = viewportHeight - gutterSize;
233
+ if (contentHeight >= gutterThresholdHeight) {
234
+ addAndObserveGutter();
235
+ } else {
236
+ if (gutterMounted) {
237
+ gutter.removeGutter();
238
+ }
239
+ }
240
+ }
241
+ };
242
+ }
243
+ });
244
+ });
@@ -5,6 +5,7 @@ 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 { getBooleanFF } from '@atlaskit/platform-feature-flags';
8
9
  import { setKeyboardHeight } from './commands';
9
10
  import contextIdentifierPlugin, { getContextIdentifier } from './pm-plugins/context-identifier';
10
11
  import disableSpellcheckingPlugin from './pm-plugins/disable-spell-checking';
@@ -13,6 +14,7 @@ import frozenEditor from './pm-plugins/frozen-editor';
13
14
  import inlineCursorTargetPlugin from './pm-plugins/inline-cursor-target';
14
15
  import newlinePreserveMarksPlugin from './pm-plugins/newline-preserve-marks';
15
16
  import scrollGutter, { getKeyboardHeight } from './pm-plugins/scroll-gutter';
17
+ import deprecatedScrollGutter from './pm-plugins/scroll-gutter/plugin-deprecated';
16
18
  var basePlugin = function basePlugin(_ref) {
17
19
  var _api$featureFlags;
18
20
  var options = _ref.config,
@@ -40,10 +42,13 @@ var basePlugin = function basePlugin(_ref) {
40
42
 
41
43
  // In Chrome, when the selection is placed between adjacent nodes which are not contenteditatble
42
44
  // the cursor appears at the right most point of the parent container.
45
+ //
43
46
  // In Firefox, when the selection is placed between adjacent nodes which are not contenteditatble
44
47
  // no cursor is presented to users.
48
+ //
45
49
  // In Safari, when the selection is placed between adjacent nodes which are not contenteditatble
46
50
  // it is not possible to navigate with arrow keys.
51
+ //
47
52
  // This plugin works around the issues by inserting decorations between
48
53
  // inline nodes which are set as contenteditable, and have a zero width space.
49
54
  plugins.push({
@@ -93,7 +98,7 @@ var basePlugin = function basePlugin(_ref) {
93
98
  plugins.push({
94
99
  name: 'scrollGutterPlugin',
95
100
  plugin: function plugin() {
96
- return scrollGutter(options.allowScrollGutter);
101
+ return getBooleanFF('platform.editor.scroll-gutter.reflow-optimisation') ? scrollGutter(options.allowScrollGutter) : deprecatedScrollGutter(options.allowScrollGutter);
97
102
  }
98
103
  });
99
104
  }
@@ -1,3 +1,6 @@
1
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
2
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
3
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
1
4
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
5
  import { GUTTER_SELECTOR, GUTTER_SIZE_IN_PX, isEmptyDocument } from '@atlaskit/editor-common/utils';
3
6
  import { getScrollGutterPluginState, scrollGutterPluginKey } from './plugin-key';
@@ -199,17 +202,78 @@ export default (function () {
199
202
  scrollElement = getScrollElement(view);
200
203
  var editorElement = view.dom;
201
204
  var editorParentElement = editorElement.parentElement;
205
+ var viewportElementHeight = 0;
206
+ var contentElementHeight = 0;
207
+ var addAndObserveGutter = function addAndObserveGutter() {
208
+ if (!gutter.isMounted()) {
209
+ gutter.observe(scrollElement);
210
+ gutter.addGutter(editorParentElement);
211
+ }
212
+ };
213
+ var toggleGutter = function toggleGutter(viewPortSize, contentSize) {
214
+ if (pluginOptions.persistScrollGutter) {
215
+ if (!isEmptyDocument(view.state.doc)) {
216
+ addAndObserveGutter();
217
+ } else {
218
+ gutter.removeGutter();
219
+ }
220
+ return;
221
+ }
222
+
223
+ // Add or remove the gutter based on whether the content is about to exceed the viewport height.
224
+ // We do this to ensure there is sufficient white space below the last content node in order to
225
+ // see any UI control elements which may sit beneath it.
226
+ var gutterThresholdHeight = viewPortSize - gutterSize;
227
+ if (contentSize >= gutterThresholdHeight) {
228
+ addAndObserveGutter();
229
+ } else {
230
+ if (gutter.isMounted()) {
231
+ gutter.removeGutter();
232
+ }
233
+ }
234
+ };
235
+ var observerCallback = function observerCallback(entries) {
236
+ var _iterator = _createForOfIteratorHelper(entries),
237
+ _step;
238
+ try {
239
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
240
+ var entry = _step.value;
241
+ var elementHeight = Math.round(entry.contentRect.height);
242
+ if (entry.target === scrollElement) {
243
+ var _getKeyboardHeight;
244
+ var keyboardHeight = (_getKeyboardHeight = getKeyboardHeight(view === null || view === void 0 ? void 0 : view.state)) !== null && _getKeyboardHeight !== void 0 ? _getKeyboardHeight : 0;
245
+ viewportElementHeight = elementHeight - keyboardHeight;
246
+ } else if (entry.target === editorParentElement) {
247
+ contentElementHeight = elementHeight - (gutter.isMounted() ? gutterSize : 0);
248
+ }
249
+ }
250
+ } catch (err) {
251
+ _iterator.e(err);
252
+ } finally {
253
+ _iterator.f();
254
+ }
255
+ toggleGutter(viewportElementHeight, contentElementHeight);
256
+ };
257
+
258
+ // Subscribe to updates of the height of the viewport and content elements
259
+ // We toggle the value when it is set here or when PM updates to ensure we're using
260
+ // the latest.
261
+ var resizeObserver = new ResizeObserver(observerCallback);
262
+ if (scrollElement && editorParentElement) {
263
+ resizeObserver.observe(scrollElement);
264
+ resizeObserver.observe(editorParentElement);
265
+ }
202
266
  return {
203
267
  destroy: function destroy() {
204
268
  // Remove if it's mounted
205
269
  gutter.destroy();
206
270
  scrollElement = editorParentElement = editorElement = null;
271
+ resizeObserver.disconnect();
207
272
  },
208
273
  /**
209
274
  * Toggle the Scroll Gutter Element
210
275
  */
211
276
  update: function update(view, prevState) {
212
- var _getKeyboardHeight;
213
277
  if (!scrollElement || !editorParentElement) {
214
278
  return;
215
279
  }
@@ -218,37 +282,7 @@ export default (function () {
218
282
  // No need to recheck if the selected node hasn't changed.
219
283
  return;
220
284
  }
221
- var gutterMounted = gutter.isMounted();
222
- var addAndObserveGutter = function addAndObserveGutter() {
223
- if (!gutterMounted) {
224
- gutter.observe(scrollElement);
225
- gutter.addGutter(editorParentElement);
226
- }
227
- };
228
- if (pluginOptions.persistScrollGutter) {
229
- if (!isEmptyDocument(state.doc)) {
230
- addAndObserveGutter();
231
- } else {
232
- gutter.removeGutter();
233
- }
234
- return;
235
- }
236
- // Determine whether we need to consider Keyboard Height
237
- var keyboardHeight = (_getKeyboardHeight = getKeyboardHeight(state)) !== null && _getKeyboardHeight !== void 0 ? _getKeyboardHeight : 0;
238
- var viewportHeight = scrollElement.offsetHeight - keyboardHeight;
239
- var contentHeight = editorParentElement.offsetHeight - (gutterMounted ? gutterSize : 0);
240
-
241
- // Add or remove the gutter based on whether the content is about to exceed the viewport height.
242
- // We do this to ensure there is sufficient white space below the last content node in order to
243
- // see any UI control elements which may sit beneath it.
244
- var gutterThresholdHeight = viewportHeight - gutterSize;
245
- if (contentHeight >= gutterThresholdHeight) {
246
- addAndObserveGutter();
247
- } else {
248
- if (gutterMounted) {
249
- gutter.removeGutter();
250
- }
251
- }
285
+ toggleGutter(viewportElementHeight, contentElementHeight);
252
286
  }
253
287
  };
254
288
  })
@@ -0,0 +1,256 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import { GUTTER_SELECTOR, GUTTER_SIZE_IN_PX, isEmptyDocument } from '@atlaskit/editor-common/utils';
3
+ import { getScrollGutterPluginState, scrollGutterPluginKey } from './plugin-key';
4
+ export var getKeyboardHeight = function getKeyboardHeight(state) {
5
+ if (state) {
6
+ var scrollGutterPluginState = getScrollGutterPluginState(state);
7
+ return scrollGutterPluginState ? scrollGutterPluginState.keyboardHeight : undefined;
8
+ }
9
+ };
10
+ var MIN_TAP_SIZE_IN_PX = 40;
11
+ function supportsIntersectionObserver() {
12
+ if (typeof window !== 'undefined' && 'IntersectionObserver' in window && 'IntersectionObserverEntry' in window && 'intersectionRatio' in
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ window.IntersectionObserverEntry.prototype) {
15
+ return true;
16
+ }
17
+ return false;
18
+ }
19
+ function listenForGutterVisibilityChanges(scrollElement, gutterIsVisible) {
20
+ if (supportsIntersectionObserver()) {
21
+ var observer = new IntersectionObserver(function (entries, _) {
22
+ entries.forEach(function (entry) {
23
+ gutterIsVisible(entry.intersectionRatio > 0);
24
+ });
25
+ }, {
26
+ root: scrollElement,
27
+ rootMargin: '0px',
28
+ threshold: 0
29
+ });
30
+ return observer;
31
+ }
32
+ return undefined;
33
+ }
34
+
35
+ /**
36
+ * Create a gutter element that can be added or removed from the DOM.
37
+ */
38
+ function createGutter(gutterSize) {
39
+ var gutter = document.createElement('div');
40
+ gutter.style.paddingBottom = "".concat(gutterSize, "px");
41
+ gutter.id = GUTTER_SELECTOR.substr(1);
42
+ var initialized = false;
43
+ var mounted = false;
44
+ var currentParent = null;
45
+ var observer;
46
+ var isVisible = false;
47
+ return {
48
+ addGutter: function addGutter(parent) {
49
+ if (parent) {
50
+ currentParent = parent;
51
+ parent.appendChild(gutter);
52
+ mounted = true;
53
+ if (observer) {
54
+ observer.observe(gutter);
55
+ }
56
+ }
57
+ },
58
+ removeGutter: function removeGutter() {
59
+ if (currentParent && mounted) {
60
+ mounted = false;
61
+ currentParent.removeChild(gutter);
62
+ if (observer) {
63
+ observer.unobserve(gutter);
64
+ }
65
+ }
66
+ },
67
+ element: function element() {
68
+ return gutter;
69
+ },
70
+ isMounted: function isMounted() {
71
+ return mounted;
72
+ },
73
+ visible: function visible() {
74
+ // If we know whether it's visible we can avoid expensive calculations
75
+ if (observer) {
76
+ return isVisible;
77
+ }
78
+ // Fallback for legacy browsers assumes it's visible (if mounted)
79
+ return mounted;
80
+ },
81
+ observe: function observe(scrollElement) {
82
+ if (!initialized) {
83
+ initialized = true;
84
+ observer = listenForGutterVisibilityChanges(scrollElement, function (visible) {
85
+ return isVisible = visible;
86
+ });
87
+ }
88
+ },
89
+ destroy: function destroy() {
90
+ if (observer) {
91
+ observer.disconnect();
92
+ }
93
+ observer = undefined;
94
+ this.removeGutter();
95
+ initialized = mounted = false;
96
+ }
97
+ };
98
+ }
99
+
100
+ /**
101
+ * Get caret top position given the current selection,
102
+ * use start container position as fallback
103
+ */
104
+ function getCaretTopPosition() {
105
+ var windowSelection = window.getSelection();
106
+ if (windowSelection && windowSelection.rangeCount > 0) {
107
+ var range = windowSelection.getRangeAt(0);
108
+ if (range) {
109
+ var clientRects = range.getClientRects();
110
+ // Return client rects
111
+ if (clientRects && clientRects.length > 0) {
112
+ return clientRects[0].top;
113
+ }
114
+
115
+ // Return container top
116
+ var container = range.startContainer;
117
+ if (container && container.getBoundingClientRect) {
118
+ return container.getBoundingClientRect().top;
119
+ }
120
+ }
121
+ }
122
+ return;
123
+ }
124
+ function scrollToGutterElement(scrollContainer, gutterElement, gutterSize) {
125
+ var viewportHeight = scrollContainer.offsetHeight;
126
+ var viewportOffsetY = scrollContainer.getBoundingClientRect().top;
127
+ var caretTopPosition = getCaretTopPosition();
128
+ if (!caretTopPosition) {
129
+ return false;
130
+ }
131
+ var caretTopFromContainer = caretTopPosition - viewportOffsetY;
132
+ var gutterThresholdTop = viewportHeight - gutterSize - MIN_TAP_SIZE_IN_PX * 2;
133
+ if (caretTopFromContainer < gutterThresholdTop) {
134
+ return false;
135
+ }
136
+
137
+ // Clamp the scroll position to above the scroll gutter element.
138
+ gutterElement.scrollIntoView(false);
139
+
140
+ // Mark scrolling as handled so that other plugin's don't override our position.
141
+ return true;
142
+ }
143
+ export default (function () {
144
+ var pluginOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
145
+ var getScrollElement = pluginOptions.getScrollElement,
146
+ allowCustomScrollHandler = pluginOptions.allowCustomScrollHandler,
147
+ _pluginOptions$gutter = pluginOptions.gutterSize,
148
+ gutterSize = _pluginOptions$gutter === void 0 ? GUTTER_SIZE_IN_PX : _pluginOptions$gutter;
149
+ if (!getScrollElement) {
150
+ return undefined;
151
+ }
152
+ var gutter = createGutter(gutterSize);
153
+ var scrollElement = null; // | undefined;
154
+
155
+ return new SafePlugin({
156
+ key: scrollGutterPluginKey,
157
+ state: {
158
+ init: function init() {
159
+ return {};
160
+ },
161
+ apply: function apply(tr, pluginState) {
162
+ if (tr.getMeta(scrollGutterPluginKey)) {
163
+ return tr.getMeta(scrollGutterPluginKey);
164
+ }
165
+ return pluginState;
166
+ }
167
+ },
168
+ props: {
169
+ // Determines the distance (in pixels) between the cursor and the end of the visible viewport at which point,
170
+ // when scrolling the cursor into view, scrolling takes place.
171
+ // Defaults to 0: https://prosemirror.net/docs/ref/#view.EditorProps.scrollThreshold
172
+ scrollThreshold: gutterSize / 2,
173
+ // Determines the extra space (in pixels) that is left above or below the cursor when it is scrolled into view.
174
+ // Defaults to 5: https://prosemirror.net/docs/ref/#view.EditorProps.scrollMargin
175
+ scrollMargin: gutterSize,
176
+ // Called when the view, after updating its state, tries to scroll the selection into view
177
+ // https://prosemirror.net/docs/ref/#view.EditorProps.handleScrollToSelection
178
+ handleScrollToSelection: function handleScrollToSelection() {
179
+ if (allowCustomScrollHandler === false) {
180
+ return false;
181
+ }
182
+ if (!gutter.isMounted() || !gutter.visible() || !scrollElement) {
183
+ // Avoid scrolling until applicable
184
+ return false;
185
+ }
186
+ return scrollToGutterElement(scrollElement, gutter.element(), gutterSize);
187
+ }
188
+ },
189
+ view: function (_view) {
190
+ function view(_x) {
191
+ return _view.apply(this, arguments);
192
+ }
193
+ view.toString = function () {
194
+ return _view.toString();
195
+ };
196
+ return view;
197
+ }(function (view) {
198
+ // Store references to avoid lookups on successive checks.
199
+ scrollElement = getScrollElement(view);
200
+ var editorElement = view.dom;
201
+ var editorParentElement = editorElement.parentElement;
202
+ return {
203
+ destroy: function destroy() {
204
+ // Remove if it's mounted
205
+ gutter.destroy();
206
+ scrollElement = editorParentElement = editorElement = null;
207
+ },
208
+ /**
209
+ * Toggle the Scroll Gutter Element
210
+ */
211
+ update: function update(view, prevState) {
212
+ var _getKeyboardHeight;
213
+ if (!scrollElement || !editorParentElement) {
214
+ return;
215
+ }
216
+ var state = view.state;
217
+ if (prevState.selection === state.selection) {
218
+ // No need to recheck if the selected node hasn't changed.
219
+ return;
220
+ }
221
+ var gutterMounted = gutter.isMounted();
222
+ var addAndObserveGutter = function addAndObserveGutter() {
223
+ if (!gutterMounted) {
224
+ gutter.observe(scrollElement);
225
+ gutter.addGutter(editorParentElement);
226
+ }
227
+ };
228
+ if (pluginOptions.persistScrollGutter) {
229
+ if (!isEmptyDocument(state.doc)) {
230
+ addAndObserveGutter();
231
+ } else {
232
+ gutter.removeGutter();
233
+ }
234
+ return;
235
+ }
236
+ // Determine whether we need to consider Keyboard Height
237
+ var keyboardHeight = (_getKeyboardHeight = getKeyboardHeight(state)) !== null && _getKeyboardHeight !== void 0 ? _getKeyboardHeight : 0;
238
+ var viewportHeight = scrollElement.offsetHeight - keyboardHeight;
239
+ var contentHeight = editorParentElement.offsetHeight - (gutterMounted ? gutterSize : 0);
240
+
241
+ // Add or remove the gutter based on whether the content is about to exceed the viewport height.
242
+ // We do this to ensure there is sufficient white space below the last content node in order to
243
+ // see any UI control elements which may sit beneath it.
244
+ var gutterThresholdHeight = viewportHeight - gutterSize;
245
+ if (contentHeight >= gutterThresholdHeight) {
246
+ addAndObserveGutter();
247
+ } else {
248
+ if (gutterMounted) {
249
+ gutter.removeGutter();
250
+ }
251
+ }
252
+ }
253
+ };
254
+ })
255
+ });
256
+ });
@@ -0,0 +1,22 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import type { EditorState } from '@atlaskit/editor-prosemirror/state';
3
+ import type { EditorView } from '@atlaskit/editor-prosemirror/view';
4
+ export declare const getKeyboardHeight: (state?: EditorState) => number | undefined;
5
+ export type ScrollGutterPluginOptions = {
6
+ /** Element the page uses for scrolling */
7
+ getScrollElement?: (view: EditorView) => HTMLElement | null;
8
+ /**
9
+ * Whether to allow custom functionality to scroll to gutter element in
10
+ * plugin's handleScrollToSelection function
11
+ * Default is true
12
+ */
13
+ allowCustomScrollHandler?: boolean;
14
+ /**
15
+ * Persist scroll gutter when the mobile appearance is COMPACT
16
+ * Default is false
17
+ */
18
+ persistScrollGutter?: boolean;
19
+ gutterSize?: number;
20
+ };
21
+ declare const _default: (pluginOptions?: ScrollGutterPluginOptions) => SafePlugin<{}> | undefined;
22
+ export default _default;