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