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