@atlaskit/editor-common 88.6.2 → 88.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/cjs/lazy-node-view/index.js +89 -11
- package/dist/cjs/lazy-node-view/node-view.js +27 -7
- package/dist/cjs/monitoring/error.js +1 -1
- package/dist/cjs/ui/DropList/index.js +1 -1
- package/dist/es2019/lazy-node-view/index.js +81 -11
- package/dist/es2019/lazy-node-view/node-view.js +25 -6
- package/dist/es2019/monitoring/error.js +1 -1
- package/dist/es2019/ui/DropList/index.js +1 -1
- package/dist/esm/lazy-node-view/index.js +85 -11
- package/dist/esm/lazy-node-view/node-view.js +27 -7
- package/dist/esm/monitoring/error.js +1 -1
- package/dist/esm/ui/DropList/index.js +1 -1
- package/dist/types/lazy-node-view/index.d.ts +18 -1
- package/dist/types/lazy-node-view/node-view.d.ts +4 -5
- package/dist/types-ts4.5/lazy-node-view/index.d.ts +18 -1
- package/dist/types-ts4.5/lazy-node-view/node-view.d.ts +4 -5
- package/package.json +1 -1
- package/dist/cjs/lazy-node-view/replace-node-views.js +0 -173
- package/dist/es2019/lazy-node-view/replace-node-views.js +0 -166
- package/dist/esm/lazy-node-view/replace-node-views.js +0 -166
- package/dist/types/lazy-node-view/replace-node-views.d.ts +0 -34
- package/dist/types-ts4.5/lazy-node-view/replace-node-views.d.ts +0 -34
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
2
|
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
3
4
|
import memoize from 'lodash/memoize';
|
|
4
5
|
import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
|
|
5
6
|
var getEditorLineWidth = memoize(function (view) {
|
|
@@ -13,18 +14,34 @@ var getEditorLineWidth = memoize(function (view) {
|
|
|
13
14
|
* A NodeView that serves as a placeholder until the actual NodeView is loaded.
|
|
14
15
|
*/
|
|
15
16
|
export var LazyNodeView = /*#__PURE__*/function () {
|
|
16
|
-
function LazyNodeView(
|
|
17
|
-
var
|
|
17
|
+
function LazyNodeView(_node, view, _getPos, nodeViewLoader) {
|
|
18
|
+
var _this = this,
|
|
19
|
+
_node$type;
|
|
18
20
|
_classCallCheck(this, LazyNodeView);
|
|
19
|
-
this
|
|
20
|
-
|
|
21
|
+
_defineProperty(this, "update", function (node) {
|
|
22
|
+
var prevNode = _this.node;
|
|
23
|
+
_this.node = node;
|
|
24
|
+
|
|
25
|
+
// Forcing NodeView to be re-created
|
|
26
|
+
// so that ProseMirror can replace LazyNodeView with the real one.
|
|
27
|
+
if (_this.isNodeViewLoaded) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Copying some of the default NodeView update behaviour
|
|
32
|
+
// https://github.com/ProseMirror/prosemirror-view/blob/cfa73eb969777f63bcb39972594fd4a9110f5a93/src/viewdesc.ts#L803
|
|
33
|
+
return !_this.node.sameMarkup(prevNode);
|
|
34
|
+
});
|
|
35
|
+
this.node = _node;
|
|
36
|
+
this.isNodeViewLoaded = false;
|
|
37
|
+
if (typeof ((_node$type = _node.type) === null || _node$type === void 0 || (_node$type = _node$type.spec) === null || _node$type === void 0 ? void 0 : _node$type.toDOM) !== 'function') {
|
|
21
38
|
this.dom = document.createElement('div');
|
|
22
39
|
return;
|
|
23
40
|
}
|
|
24
41
|
var toDOMConfiguration = {
|
|
25
42
|
editorLineWidth: getEditorLineWidth(view)
|
|
26
43
|
};
|
|
27
|
-
var fallback = DOMSerializer.renderSpec(document,
|
|
44
|
+
var fallback = DOMSerializer.renderSpec(document, _node.type.spec.toDOM(_node,
|
|
28
45
|
// We are injecting a second parameter to be used by the toDOM lazy node view implementations
|
|
29
46
|
// @ts-expect-error
|
|
30
47
|
toDOMConfiguration));
|
|
@@ -33,16 +50,19 @@ export var LazyNodeView = /*#__PURE__*/function () {
|
|
|
33
50
|
if (this.dom instanceof HTMLElement) {
|
|
34
51
|
// This attribute is mostly used for debugging purposed
|
|
35
52
|
// It will help us to found out when the node was replaced
|
|
36
|
-
this.dom.setAttribute('data-lazy-node-view',
|
|
53
|
+
this.dom.setAttribute('data-lazy-node-view', _node.type.name);
|
|
37
54
|
// This is used on Libra tests
|
|
38
55
|
// We are using this to make sure all lazy noded were replaced
|
|
39
56
|
// before the test started
|
|
40
57
|
this.dom.setAttribute('data-lazy-node-view-fallback', 'true');
|
|
41
58
|
}
|
|
59
|
+
nodeViewLoader.then(function () {
|
|
60
|
+
_this.isNodeViewLoaded = true;
|
|
61
|
+
});
|
|
42
62
|
}
|
|
43
63
|
_createClass(LazyNodeView, [{
|
|
44
64
|
key: "ignoreMutation",
|
|
45
|
-
value: function ignoreMutation(
|
|
65
|
+
value: function ignoreMutation() {
|
|
46
66
|
if (this.node.type.isTextblock) {
|
|
47
67
|
return false;
|
|
48
68
|
}
|
|
@@ -7,7 +7,7 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
|
|
|
7
7
|
import { isFedRamp } from './environment';
|
|
8
8
|
var SENTRY_DSN = 'https://0b10c8e02fb44d8796c047b102c9bee8@o55978.ingest.sentry.io/4505129224110080';
|
|
9
9
|
var packageName = 'editor-common'; // Sentry doesn't accept '/' in its releases https://docs.sentry.io/platforms/javascript/configuration/releases/
|
|
10
|
-
var packageVersion = "88.
|
|
10
|
+
var packageVersion = "88.7.0";
|
|
11
11
|
var sanitiseSentryEvents = function sanitiseSentryEvents(data, _hint) {
|
|
12
12
|
// Remove URL as it has UGC
|
|
13
13
|
// TODO: Sanitise the URL instead of just removing it
|
|
@@ -22,7 +22,7 @@ import withAnalyticsEvents from '@atlaskit/analytics-next/withAnalyticsEvents';
|
|
|
22
22
|
import { N0, N50A, N60A, N900 } from '@atlaskit/theme/colors';
|
|
23
23
|
import Layer from '../Layer';
|
|
24
24
|
var packageName = "@atlaskit/editor-common";
|
|
25
|
-
var packageVersion = "88.
|
|
25
|
+
var packageVersion = "88.7.0";
|
|
26
26
|
var halfFocusRing = 1;
|
|
27
27
|
var dropOffset = '0, 8';
|
|
28
28
|
var DropList = /*#__PURE__*/function (_Component) {
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
|
|
2
|
+
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
|
|
2
3
|
import type { Decoration, EditorView, NodeView } from '@atlaskit/editor-prosemirror/view';
|
|
3
4
|
import type { DispatchAnalyticsEvent } from '../analytics';
|
|
4
5
|
import type { LazyNodeViewToDOMConfiguration, NodeViewConstructor } from './types';
|
|
5
6
|
export { convertToInlineCss } from './css-helper';
|
|
6
7
|
export type { NodeViewConstructor, LazyNodeViewToDOMConfiguration };
|
|
8
|
+
/**
|
|
9
|
+
* 📢 Public Plugin Key
|
|
10
|
+
*
|
|
11
|
+
* Communication channel between LazyNodeView loader and LazyNodeViewDecorationPlugin.
|
|
12
|
+
*/
|
|
13
|
+
export declare const lazyNodeViewDecorationPluginKey: PluginKey<any>;
|
|
7
14
|
/**
|
|
8
15
|
* 📢 Public Type
|
|
9
16
|
*
|
|
@@ -21,6 +28,16 @@ export type LazyLoadingProps<NodeViewOptions> = {
|
|
|
21
28
|
getNodeViewOptions: () => NodeViewOptions;
|
|
22
29
|
dispatchAnalyticsEvent?: DispatchAnalyticsEvent;
|
|
23
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* 🧱 Internal: Editor FE Platform
|
|
33
|
+
*
|
|
34
|
+
* Used in tests to prevent lazy node view being replaced by a real node view.
|
|
35
|
+
*
|
|
36
|
+
* This needs to be replaced with proper implementation once LazyNodeView is converted to a plugin.
|
|
37
|
+
*
|
|
38
|
+
* @deprecated DO NOT USE THIS OUSIDE TESTS.
|
|
39
|
+
*/
|
|
40
|
+
export declare function testOnlyIgnoreLazyNodeView(view: EditorView): void;
|
|
24
41
|
/**
|
|
25
42
|
* 📢 Public: Any EditorPlugin can use this function
|
|
26
43
|
*
|
|
@@ -61,4 +78,4 @@ export type LazyLoadingProps<NodeViewOptions> = {
|
|
|
61
78
|
*
|
|
62
79
|
* // Then, use `lazyTableView` in ProseMirror editor setup to enhance 'table' nodes with lazy loading
|
|
63
80
|
*/
|
|
64
|
-
export declare const withLazyLoading: <Options>({ nodeName, loader, getNodeViewOptions,
|
|
81
|
+
export declare const withLazyLoading: <Options>({ nodeName, loader, getNodeViewOptions, }: LazyLoadingProps<Options>) => NodeViewConstructor;
|
|
@@ -9,9 +9,8 @@ export declare class LazyNodeView implements NodeView {
|
|
|
9
9
|
dom: Node;
|
|
10
10
|
contentDOM?: HTMLElement;
|
|
11
11
|
private node;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}): boolean;
|
|
12
|
+
private isNodeViewLoaded;
|
|
13
|
+
constructor(node: PMNode, view: EditorView, _getPos: () => number | undefined, nodeViewLoader: Promise<unknown>);
|
|
14
|
+
update: (node: PMNode) => boolean;
|
|
15
|
+
ignoreMutation(): boolean;
|
|
17
16
|
}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
|
|
2
|
+
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
|
|
2
3
|
import type { Decoration, EditorView, NodeView } from '@atlaskit/editor-prosemirror/view';
|
|
3
4
|
import type { DispatchAnalyticsEvent } from '../analytics';
|
|
4
5
|
import type { LazyNodeViewToDOMConfiguration, NodeViewConstructor } from './types';
|
|
5
6
|
export { convertToInlineCss } from './css-helper';
|
|
6
7
|
export type { NodeViewConstructor, LazyNodeViewToDOMConfiguration };
|
|
8
|
+
/**
|
|
9
|
+
* 📢 Public Plugin Key
|
|
10
|
+
*
|
|
11
|
+
* Communication channel between LazyNodeView loader and LazyNodeViewDecorationPlugin.
|
|
12
|
+
*/
|
|
13
|
+
export declare const lazyNodeViewDecorationPluginKey: PluginKey<any>;
|
|
7
14
|
/**
|
|
8
15
|
* 📢 Public Type
|
|
9
16
|
*
|
|
@@ -21,6 +28,16 @@ export type LazyLoadingProps<NodeViewOptions> = {
|
|
|
21
28
|
getNodeViewOptions: () => NodeViewOptions;
|
|
22
29
|
dispatchAnalyticsEvent?: DispatchAnalyticsEvent;
|
|
23
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* 🧱 Internal: Editor FE Platform
|
|
33
|
+
*
|
|
34
|
+
* Used in tests to prevent lazy node view being replaced by a real node view.
|
|
35
|
+
*
|
|
36
|
+
* This needs to be replaced with proper implementation once LazyNodeView is converted to a plugin.
|
|
37
|
+
*
|
|
38
|
+
* @deprecated DO NOT USE THIS OUSIDE TESTS.
|
|
39
|
+
*/
|
|
40
|
+
export declare function testOnlyIgnoreLazyNodeView(view: EditorView): void;
|
|
24
41
|
/**
|
|
25
42
|
* 📢 Public: Any EditorPlugin can use this function
|
|
26
43
|
*
|
|
@@ -61,4 +78,4 @@ export type LazyLoadingProps<NodeViewOptions> = {
|
|
|
61
78
|
*
|
|
62
79
|
* // Then, use `lazyTableView` in ProseMirror editor setup to enhance 'table' nodes with lazy loading
|
|
63
80
|
*/
|
|
64
|
-
export declare const withLazyLoading: <Options>({ nodeName, loader, getNodeViewOptions,
|
|
81
|
+
export declare const withLazyLoading: <Options>({ nodeName, loader, getNodeViewOptions, }: LazyLoadingProps<Options>) => NodeViewConstructor;
|
|
@@ -9,9 +9,8 @@ export declare class LazyNodeView implements NodeView {
|
|
|
9
9
|
dom: Node;
|
|
10
10
|
contentDOM?: HTMLElement;
|
|
11
11
|
private node;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}): boolean;
|
|
12
|
+
private isNodeViewLoaded;
|
|
13
|
+
constructor(node: PMNode, view: EditorView, _getPos: () => number | undefined, nodeViewLoader: Promise<unknown>);
|
|
14
|
+
update: (node: PMNode) => boolean;
|
|
15
|
+
ignoreMutation(): boolean;
|
|
17
16
|
}
|
package/package.json
CHANGED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.queueReplaceNodeViews = exports.debouncedReplaceNodeviews = void 0;
|
|
8
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
9
|
-
var _debounce = _interopRequireDefault(require("lodash/debounce"));
|
|
10
|
-
var _memoize = _interopRequireDefault(require("lodash/memoize"));
|
|
11
|
-
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; }
|
|
12
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
13
|
-
/**
|
|
14
|
-
* 🧱 Internal: Editor FE Platform
|
|
15
|
-
*/
|
|
16
|
-
var isFirefox = /gecko\/\d/i.test(navigator.userAgent);
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 🧱 Internal: Editor FE Platform
|
|
20
|
-
*
|
|
21
|
-
* Replaces the node views in a ProseMirror editor with lazy-loaded node views.
|
|
22
|
-
*
|
|
23
|
-
* This function is used to update the `nodeViews` property of the `EditorView` after lazy-loaded
|
|
24
|
-
* node views have been loaded.
|
|
25
|
-
*
|
|
26
|
-
* The function checks if there are any loaded node views in the cache associated with the given
|
|
27
|
-
* `EditorView`. If there are, it replaces the current `nodeViews` in the `EditorView` with the
|
|
28
|
-
* loaded node views. The replacement is scheduled using `requestIdleCallback` or
|
|
29
|
-
* `requestAnimationFrame` to avoid blocking the main thread, especially in Firefox where
|
|
30
|
-
* `requestIdleCallback` may not be supported.
|
|
31
|
-
*
|
|
32
|
-
* @param {WeakMap<EditorView, Record<string, NodeViewConstructor>>} cache - A WeakMap that stores
|
|
33
|
-
* the loaded node views for each `EditorView`. The key is the `EditorView`, and the value
|
|
34
|
-
* is a record of node type names to their corresponding `NodeViewConstructor`.
|
|
35
|
-
* @param {EditorView} view - The ProseMirror `EditorView` instance whose `nodeViews` property
|
|
36
|
-
* needs to be updated.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* const cache = new WeakMap();
|
|
40
|
-
* const view = new EditorView(...);
|
|
41
|
-
*
|
|
42
|
-
* // Assume some node views have been loaded and stored in the cache
|
|
43
|
-
* cache.set(view, {
|
|
44
|
-
* 'table': TableViewConstructor,
|
|
45
|
-
* 'tableCell': TableCellViewConstructor,
|
|
46
|
-
* });
|
|
47
|
-
*
|
|
48
|
-
* replaceNodeViews(cache, view);
|
|
49
|
-
*/
|
|
50
|
-
var replaceNodeViews = function replaceNodeViews(cache, view) {
|
|
51
|
-
var loadedNodeviews = cache.get(view);
|
|
52
|
-
if (!loadedNodeviews || Object.keys(loadedNodeviews).length === 0) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
var nodeViewsToReplace = _objectSpread({}, loadedNodeviews);
|
|
56
|
-
|
|
57
|
-
// Make sure the cache is cleaned
|
|
58
|
-
// From here, we will access the loaded node views by lexical scope
|
|
59
|
-
cache.set(view, {});
|
|
60
|
-
var callback = function callback() {
|
|
61
|
-
var currentNodeViews = view.props.nodeViews;
|
|
62
|
-
var nextNodeViews = _objectSpread(_objectSpread({}, currentNodeViews), nodeViewsToReplace);
|
|
63
|
-
view.setProps({
|
|
64
|
-
nodeViews: nextNodeViews
|
|
65
|
-
});
|
|
66
|
-
};
|
|
67
|
-
// eslint-disable-next-line compat/compat
|
|
68
|
-
var idle = window.requestIdleCallback;
|
|
69
|
-
|
|
70
|
-
/*
|
|
71
|
-
* For reasons that goes beyond my knowledge
|
|
72
|
-
* some Firefox versions aren't calling the requestIdleCallback.
|
|
73
|
-
*
|
|
74
|
-
* So, we need this check to make sure we are using the requestAnimationFrame for Firefox
|
|
75
|
-
*/
|
|
76
|
-
if (isFirefox || typeof idle !== 'function') {
|
|
77
|
-
window.requestAnimationFrame(callback);
|
|
78
|
-
} else {
|
|
79
|
-
idle(callback, {
|
|
80
|
-
timeout: 2000
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 🧱 Internal: Editor FE Platform
|
|
87
|
-
*/
|
|
88
|
-
var loadedPerEditorView = new WeakMap();
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* 🧱 Internal: Editor FE Platform
|
|
92
|
-
* Based on https://github.com/lodash/lodash/issues/2403#issuecomment-1706130395
|
|
93
|
-
*
|
|
94
|
-
* Creates a debounced function that delays invoking the provided function until after a specified
|
|
95
|
-
* wait time has elapsed since the last time the debounced function was invoked. Additionally, the
|
|
96
|
-
* debounced function is memoized so that the same function instance is used for each unique set
|
|
97
|
-
* of arguments based on the resolver.
|
|
98
|
-
*
|
|
99
|
-
* This is particularly useful in scenarios where you want to debounce function calls while ensuring
|
|
100
|
-
* that each unique input combination receives its own debounced function instance. It's a combination
|
|
101
|
-
* of lodash's `debounce` and `memoize`.
|
|
102
|
-
*
|
|
103
|
-
* @template T
|
|
104
|
-
* @param {T} func - The function to debounce.
|
|
105
|
-
* @param {number} [wait=0] - The number of milliseconds to delay.
|
|
106
|
-
* @param {Object} [options] - The options object to pass to `debounce`.
|
|
107
|
-
* @param {Function} [resolver] - The function to resolve the cache key for memoization.
|
|
108
|
-
* @returns {Function} A new debounced and memoized function.
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* const debouncedFunction = memoizeDebounce(myFunction, 300, { leading: true }, myResolver);
|
|
112
|
-
* debouncedFunction(arg1, arg2);
|
|
113
|
-
*/
|
|
114
|
-
function memoizeDebounce(func) {
|
|
115
|
-
var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
116
|
-
var options = arguments.length > 2 ? arguments[2] : undefined;
|
|
117
|
-
var resolver = arguments.length > 3 ? arguments[3] : undefined;
|
|
118
|
-
var mem = (0, _memoize.default)(function () {
|
|
119
|
-
return (0, _debounce.default)(func, wait, options);
|
|
120
|
-
}, resolver);
|
|
121
|
-
return function () {
|
|
122
|
-
return mem.apply(void 0, arguments).apply(void 0, arguments);
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* 🧱 Internal: Editor FE Platform
|
|
128
|
-
*
|
|
129
|
-
* Debounced and memoized version of `replaceNodeViews`.
|
|
130
|
-
*
|
|
131
|
-
* This function is designed to update the `nodeViews` property of an `EditorView` after a
|
|
132
|
-
* period of inactivity (debounce), ensuring that multiple rapid updates do not occur in quick
|
|
133
|
-
* succession. It uses lodash's `debounce` to handle the debouncing.
|
|
134
|
-
*
|
|
135
|
-
* Memoization is crucial here to ensure that each `EditorView` instance has its own opportunity
|
|
136
|
-
* to update the node views. Without memoization, if you have multiple `EditorView` instances on
|
|
137
|
-
* the same page, only one instance would potentially call `view.setProps`, which could lead to
|
|
138
|
-
* incorrect or missing updates in other `EditorView` instances. By memoizing the debounced function,
|
|
139
|
-
* we ensure that each `EditorView` maintains its own debounced update logic.
|
|
140
|
-
*
|
|
141
|
-
* @param {CacheLoadedReactNodeViews} cache - A WeakMap that stores the loaded node views for each
|
|
142
|
-
* `EditorView`. The key is the `EditorView`, and the value is a record of node type names
|
|
143
|
-
* to their corresponding `NodeViewConstructor`.
|
|
144
|
-
* @param {EditorView} view - The ProseMirror `EditorView` instance whose `nodeViews` property
|
|
145
|
-
* needs to be updated.
|
|
146
|
-
*
|
|
147
|
-
* This function is typically not called directly. Instead, it is invoked through `queueReplaceNodeViews`,
|
|
148
|
-
* which handles adding node views to the cache and triggering this debounced update.
|
|
149
|
-
*/
|
|
150
|
-
var debouncedReplaceNodeviews = exports.debouncedReplaceNodeviews = memoizeDebounce(replaceNodeViews, 0,
|
|
151
|
-
/**
|
|
152
|
-
* Use the default debounce options:
|
|
153
|
-
* {leading: false, trailing: true}
|
|
154
|
-
*/
|
|
155
|
-
undefined, function (cache, view) {
|
|
156
|
-
/**
|
|
157
|
-
* EditorView is a singleton.
|
|
158
|
-
* There is only one instance per Editor.
|
|
159
|
-
*/
|
|
160
|
-
return view;
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* 🧱 Internal: Editor FE Platform
|
|
165
|
-
*/
|
|
166
|
-
var queueReplaceNodeViews = exports.queueReplaceNodeViews = function queueReplaceNodeViews(view, _ref) {
|
|
167
|
-
var nodeName = _ref.nodeName,
|
|
168
|
-
nodeViewFunc = _ref.nodeViewFunc;
|
|
169
|
-
var nodeViews = loadedPerEditorView.get(view) || {};
|
|
170
|
-
nodeViews[nodeName] = nodeViewFunc;
|
|
171
|
-
loadedPerEditorView.set(view, nodeViews);
|
|
172
|
-
debouncedReplaceNodeviews(loadedPerEditorView, view);
|
|
173
|
-
};
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import debounce from 'lodash/debounce';
|
|
2
|
-
import memoize from 'lodash/memoize';
|
|
3
|
-
/**
|
|
4
|
-
* 🧱 Internal: Editor FE Platform
|
|
5
|
-
*/
|
|
6
|
-
const isFirefox = /gecko\/\d/i.test(navigator.userAgent);
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 🧱 Internal: Editor FE Platform
|
|
10
|
-
*
|
|
11
|
-
* Replaces the node views in a ProseMirror editor with lazy-loaded node views.
|
|
12
|
-
*
|
|
13
|
-
* This function is used to update the `nodeViews` property of the `EditorView` after lazy-loaded
|
|
14
|
-
* node views have been loaded.
|
|
15
|
-
*
|
|
16
|
-
* The function checks if there are any loaded node views in the cache associated with the given
|
|
17
|
-
* `EditorView`. If there are, it replaces the current `nodeViews` in the `EditorView` with the
|
|
18
|
-
* loaded node views. The replacement is scheduled using `requestIdleCallback` or
|
|
19
|
-
* `requestAnimationFrame` to avoid blocking the main thread, especially in Firefox where
|
|
20
|
-
* `requestIdleCallback` may not be supported.
|
|
21
|
-
*
|
|
22
|
-
* @param {WeakMap<EditorView, Record<string, NodeViewConstructor>>} cache - A WeakMap that stores
|
|
23
|
-
* the loaded node views for each `EditorView`. The key is the `EditorView`, and the value
|
|
24
|
-
* is a record of node type names to their corresponding `NodeViewConstructor`.
|
|
25
|
-
* @param {EditorView} view - The ProseMirror `EditorView` instance whose `nodeViews` property
|
|
26
|
-
* needs to be updated.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* const cache = new WeakMap();
|
|
30
|
-
* const view = new EditorView(...);
|
|
31
|
-
*
|
|
32
|
-
* // Assume some node views have been loaded and stored in the cache
|
|
33
|
-
* cache.set(view, {
|
|
34
|
-
* 'table': TableViewConstructor,
|
|
35
|
-
* 'tableCell': TableCellViewConstructor,
|
|
36
|
-
* });
|
|
37
|
-
*
|
|
38
|
-
* replaceNodeViews(cache, view);
|
|
39
|
-
*/
|
|
40
|
-
const replaceNodeViews = (cache, view) => {
|
|
41
|
-
const loadedNodeviews = cache.get(view);
|
|
42
|
-
if (!loadedNodeviews || Object.keys(loadedNodeviews).length === 0) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const nodeViewsToReplace = {
|
|
46
|
-
...loadedNodeviews
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
// Make sure the cache is cleaned
|
|
50
|
-
// From here, we will access the loaded node views by lexical scope
|
|
51
|
-
cache.set(view, {});
|
|
52
|
-
const callback = () => {
|
|
53
|
-
const currentNodeViews = view.props.nodeViews;
|
|
54
|
-
const nextNodeViews = {
|
|
55
|
-
...currentNodeViews,
|
|
56
|
-
...nodeViewsToReplace
|
|
57
|
-
};
|
|
58
|
-
view.setProps({
|
|
59
|
-
nodeViews: nextNodeViews
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
|
-
// eslint-disable-next-line compat/compat
|
|
63
|
-
const idle = window.requestIdleCallback;
|
|
64
|
-
|
|
65
|
-
/*
|
|
66
|
-
* For reasons that goes beyond my knowledge
|
|
67
|
-
* some Firefox versions aren't calling the requestIdleCallback.
|
|
68
|
-
*
|
|
69
|
-
* So, we need this check to make sure we are using the requestAnimationFrame for Firefox
|
|
70
|
-
*/
|
|
71
|
-
if (isFirefox || typeof idle !== 'function') {
|
|
72
|
-
window.requestAnimationFrame(callback);
|
|
73
|
-
} else {
|
|
74
|
-
idle(callback, {
|
|
75
|
-
timeout: 2000
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 🧱 Internal: Editor FE Platform
|
|
82
|
-
*/
|
|
83
|
-
const loadedPerEditorView = new WeakMap();
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 🧱 Internal: Editor FE Platform
|
|
87
|
-
* Based on https://github.com/lodash/lodash/issues/2403#issuecomment-1706130395
|
|
88
|
-
*
|
|
89
|
-
* Creates a debounced function that delays invoking the provided function until after a specified
|
|
90
|
-
* wait time has elapsed since the last time the debounced function was invoked. Additionally, the
|
|
91
|
-
* debounced function is memoized so that the same function instance is used for each unique set
|
|
92
|
-
* of arguments based on the resolver.
|
|
93
|
-
*
|
|
94
|
-
* This is particularly useful in scenarios where you want to debounce function calls while ensuring
|
|
95
|
-
* that each unique input combination receives its own debounced function instance. It's a combination
|
|
96
|
-
* of lodash's `debounce` and `memoize`.
|
|
97
|
-
*
|
|
98
|
-
* @template T
|
|
99
|
-
* @param {T} func - The function to debounce.
|
|
100
|
-
* @param {number} [wait=0] - The number of milliseconds to delay.
|
|
101
|
-
* @param {Object} [options] - The options object to pass to `debounce`.
|
|
102
|
-
* @param {Function} [resolver] - The function to resolve the cache key for memoization.
|
|
103
|
-
* @returns {Function} A new debounced and memoized function.
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* const debouncedFunction = memoizeDebounce(myFunction, 300, { leading: true }, myResolver);
|
|
107
|
-
* debouncedFunction(arg1, arg2);
|
|
108
|
-
*/
|
|
109
|
-
function memoizeDebounce(func, wait = 0, options, resolver) {
|
|
110
|
-
const mem = memoize(function () {
|
|
111
|
-
return debounce(func, wait, options);
|
|
112
|
-
}, resolver);
|
|
113
|
-
return function (...args) {
|
|
114
|
-
return mem(...args)(...args);
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* 🧱 Internal: Editor FE Platform
|
|
120
|
-
*
|
|
121
|
-
* Debounced and memoized version of `replaceNodeViews`.
|
|
122
|
-
*
|
|
123
|
-
* This function is designed to update the `nodeViews` property of an `EditorView` after a
|
|
124
|
-
* period of inactivity (debounce), ensuring that multiple rapid updates do not occur in quick
|
|
125
|
-
* succession. It uses lodash's `debounce` to handle the debouncing.
|
|
126
|
-
*
|
|
127
|
-
* Memoization is crucial here to ensure that each `EditorView` instance has its own opportunity
|
|
128
|
-
* to update the node views. Without memoization, if you have multiple `EditorView` instances on
|
|
129
|
-
* the same page, only one instance would potentially call `view.setProps`, which could lead to
|
|
130
|
-
* incorrect or missing updates in other `EditorView` instances. By memoizing the debounced function,
|
|
131
|
-
* we ensure that each `EditorView` maintains its own debounced update logic.
|
|
132
|
-
*
|
|
133
|
-
* @param {CacheLoadedReactNodeViews} cache - A WeakMap that stores the loaded node views for each
|
|
134
|
-
* `EditorView`. The key is the `EditorView`, and the value is a record of node type names
|
|
135
|
-
* to their corresponding `NodeViewConstructor`.
|
|
136
|
-
* @param {EditorView} view - The ProseMirror `EditorView` instance whose `nodeViews` property
|
|
137
|
-
* needs to be updated.
|
|
138
|
-
*
|
|
139
|
-
* This function is typically not called directly. Instead, it is invoked through `queueReplaceNodeViews`,
|
|
140
|
-
* which handles adding node views to the cache and triggering this debounced update.
|
|
141
|
-
*/
|
|
142
|
-
export const debouncedReplaceNodeviews = memoizeDebounce(replaceNodeViews, 0,
|
|
143
|
-
/**
|
|
144
|
-
* Use the default debounce options:
|
|
145
|
-
* {leading: false, trailing: true}
|
|
146
|
-
*/
|
|
147
|
-
undefined, (cache, view) => {
|
|
148
|
-
/**
|
|
149
|
-
* EditorView is a singleton.
|
|
150
|
-
* There is only one instance per Editor.
|
|
151
|
-
*/
|
|
152
|
-
return view;
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* 🧱 Internal: Editor FE Platform
|
|
157
|
-
*/
|
|
158
|
-
export const queueReplaceNodeViews = (view, {
|
|
159
|
-
nodeName,
|
|
160
|
-
nodeViewFunc
|
|
161
|
-
}) => {
|
|
162
|
-
const nodeViews = loadedPerEditorView.get(view) || {};
|
|
163
|
-
nodeViews[nodeName] = nodeViewFunc;
|
|
164
|
-
loadedPerEditorView.set(view, nodeViews);
|
|
165
|
-
debouncedReplaceNodeviews(loadedPerEditorView, view);
|
|
166
|
-
};
|