@atlaskit/editor-plugin-block-controls 34.0.4 → 34.0.6

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.
Files changed (35) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/blockControlsPlugin.js +2 -10
  3. package/dist/cjs/pm-plugins/sparse-gutter-hover.js +15 -22
  4. package/dist/cjs/pm-plugins/sparse-surfaces.js +2 -2
  5. package/dist/cjs/ui/block-controls-left-surface.js +19 -83
  6. package/dist/cjs/ui/block-controls-right-surface.js +19 -70
  7. package/dist/cjs/ui/block-controls-surface-targets.js +1 -1
  8. package/dist/es2019/blockControlsPlugin.js +2 -10
  9. package/dist/es2019/pm-plugins/sparse-gutter-hover.js +15 -22
  10. package/dist/es2019/pm-plugins/sparse-surfaces.js +2 -2
  11. package/dist/es2019/ui/block-controls-left-surface.js +10 -76
  12. package/dist/es2019/ui/block-controls-right-surface.js +10 -63
  13. package/dist/es2019/ui/block-controls-surface-targets.js +1 -1
  14. package/dist/esm/blockControlsPlugin.js +2 -10
  15. package/dist/esm/pm-plugins/sparse-gutter-hover.js +15 -22
  16. package/dist/esm/pm-plugins/sparse-surfaces.js +2 -2
  17. package/dist/esm/ui/block-controls-left-surface.js +19 -83
  18. package/dist/esm/ui/block-controls-right-surface.js +19 -70
  19. package/dist/esm/ui/block-controls-surface-targets.js +1 -1
  20. package/dist/types/ui/block-controls-left-surface.d.ts +1 -1
  21. package/dist/types/ui/block-controls-right-surface.d.ts +1 -1
  22. package/dist/types/ui/block-controls-surface-targets.d.ts +1 -1
  23. package/package.json +7 -7
  24. package/dist/cjs/pm-plugins/sparse-surfaces-view.js +0 -149
  25. package/dist/cjs/ui/block-controls-left-surfaces.js +0 -294
  26. package/dist/cjs/ui/block-controls-right-surfaces.js +0 -285
  27. package/dist/es2019/pm-plugins/sparse-surfaces-view.js +0 -136
  28. package/dist/es2019/ui/block-controls-left-surfaces.js +0 -259
  29. package/dist/es2019/ui/block-controls-right-surfaces.js +0 -245
  30. package/dist/esm/pm-plugins/sparse-surfaces-view.js +0 -142
  31. package/dist/esm/ui/block-controls-left-surfaces.js +0 -285
  32. package/dist/esm/ui/block-controls-right-surfaces.js +0 -276
  33. package/dist/types/pm-plugins/sparse-surfaces-view.d.ts +0 -16
  34. package/dist/types/ui/block-controls-left-surfaces.d.ts +0 -15
  35. package/dist/types/ui/block-controls-right-surfaces.d.ts +0 -24
@@ -1,259 +0,0 @@
1
- /* block-controls-left-surfaces.tsx generated by @compiled/babel-plugin v3.0.4 */
2
- import { ax, ix } from "@compiled/react/runtime";
3
- import React, { useLayoutEffect, useMemo, useRef, useState } from 'react';
4
- import { bind } from 'bind-event-listener';
5
- import { getDocument } from '@atlaskit/browser-apis';
6
- import { BLOCK_CONTROL_UI_CONTEXT } from '@atlaskit/editor-common/block-controls/block-control-ui-context';
7
- import { BLOCK_CONTROLS_LEFT_SURFACE } from '@atlaskit/editor-common/block-controls/surface-keys';
8
- import { isCSSAnchorSupported } from '@atlaskit/editor-common/styles/native-anchor';
9
- import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/useSharedPluginStateWithSelector';
10
- import { createSurfaceContext } from '@atlaskit/editor-ui-control-model/create-surface-context';
11
- import { getNodeTypeWithLevel } from '../pm-plugins/decorations-common';
12
- import { BlockControlsLeftSurface } from './block-controls-left-surface';
13
- import { getBlockControlsLeftSurfaceComponents } from './block-controls-surface-components';
14
- import { createBlockControlsSurfaceContextForPosition } from './block-controls-surface-context';
15
- import { getBlockControlsSurfaceTargets } from './block-controls-surface-targets';
16
- import { getNodeContentElement } from './utils/get-node-content-element';
17
- import { getAbsoluteSurfacePlacement, getAnchoredLeftGutterSurfacePlacement, getAnchoredSurfacePlacement, getLeftGutterSurfacePlacement, getSurfacePlacement, toMeasuredSurfaceWrapperPlacement } from './utils/get-surface-placement';
18
- const EMPTY_SURFACE_POSITIONS = [];
19
- const EMPTY_SURFACE_ANCHORS = new Map();
20
- const EMPTY_SURFACE_COMPONENTS = [];
21
- const EMPTY_MEASURED_SURFACES = [];
22
- export const BlockControlsLeftSurfaces = ({
23
- api,
24
- editorView
25
- }) => {
26
- var _api$uiControlRegistr, _api$uiControlRegistr2;
27
- const {
28
- activeNode,
29
- surfaceAnchors,
30
- surfaceActiveNodes,
31
- surfaceNodePositions
32
- } = useSharedPluginStateWithSelector(api, ['blockControls'], states => {
33
- var _states$blockControls, _states$blockControls2, _states$blockControls3, _states$blockControls4, _states$blockControls5, _states$blockControls6;
34
- return {
35
- activeNode: (_states$blockControls = states.blockControlsState) === null || _states$blockControls === void 0 ? void 0 : _states$blockControls.activeNode,
36
- surfaceActiveNodes: (_states$blockControls2 = states.blockControlsState) === null || _states$blockControls2 === void 0 ? void 0 : _states$blockControls2.surfaceActiveNodes,
37
- surfaceAnchors: (_states$blockControls3 = (_states$blockControls4 = states.blockControlsState) === null || _states$blockControls4 === void 0 ? void 0 : _states$blockControls4.surfaceAnchors) !== null && _states$blockControls3 !== void 0 ? _states$blockControls3 : EMPTY_SURFACE_ANCHORS,
38
- surfaceNodePositions: (_states$blockControls5 = (_states$blockControls6 = states.blockControlsState) === null || _states$blockControls6 === void 0 ? void 0 : _states$blockControls6.surfaceNodePositions) !== null && _states$blockControls5 !== void 0 ? _states$blockControls5 : EMPTY_SURFACE_POSITIONS
39
- };
40
- });
41
- const components = (_api$uiControlRegistr = (_api$uiControlRegistr2 = api.uiControlRegistry) === null || _api$uiControlRegistr2 === void 0 ? void 0 : _api$uiControlRegistr2.actions.getComponents(BLOCK_CONTROLS_LEFT_SURFACE)) !== null && _api$uiControlRegistr !== void 0 ? _api$uiControlRegistr : EMPTY_SURFACE_COMPONENTS;
42
- const targets = useMemo(() => {
43
- const candidates = new Set(surfaceNodePositions);
44
- return getBlockControlsSurfaceTargets(activeNode, surfaceNodePositions).filter(({
45
- position
46
- }) => candidates.has(position));
47
- }, [activeNode, surfaceNodePositions]);
48
- const surfaces = useMemo(() => targets.flatMap(target => {
49
- var _surfaceActiveNodes$g;
50
- const context = createBlockControlsSurfaceContextForPosition(editorView.state, target.position, (_surfaceActiveNodes$g = surfaceActiveNodes === null || surfaceActiveNodes === void 0 ? void 0 : surfaceActiveNodes.get(target.position)) !== null && _surfaceActiveNodes$g !== void 0 ? _surfaceActiveNodes$g : activeNode);
51
- if (!context) {
52
- return [];
53
- }
54
- const surfaceContext = createSurfaceContext(BLOCK_CONTROL_UI_CONTEXT, context);
55
- const surfaceComponents = getBlockControlsLeftSurfaceComponents(components, surfaceContext);
56
- if (!surfaceComponents) {
57
- return [];
58
- }
59
- // The cache supplies the same anchor names used by ProseMirror decorations.
60
- const anchorName = isCSSAnchorSupported() ? surfaceAnchors.get(target.position) : undefined;
61
- // Top-level controls can respond to the available left gutter without the surface
62
- // host knowing which controls are registered or how they adapt.
63
- const isTopLevel = context.targetNode.parentType === 'doc';
64
- return [{
65
- ...target,
66
- anchorName,
67
- blockControlsContext: context,
68
- isTopLevel,
69
- surfaceComponents,
70
- surfaceContext
71
- }];
72
- }), [activeNode, components, editorView, targets, surfaceAnchors, surfaceActiveNodes]);
73
- /**
74
- * Placements that need no measuring, so they are derived rather than stored: every inset is an
75
- * `anchor()` the browser re-resolves on every layout change.
76
- */
77
- const anchoredPlacements = useMemo(() => new Map(surfaces.flatMap(({
78
- anchorName,
79
- blockControlsContext,
80
- isTopLevel,
81
- position
82
- }) => {
83
- if (!anchorName) {
84
- return [];
85
- }
86
- const options = {
87
- anchorName,
88
- layout: blockControlsContext.targetNode.node.attrs.layout,
89
- nodeType: blockControlsContext.targetNode.type.name,
90
- nodeTypeWithLevel: getNodeTypeWithLevel(blockControlsContext.targetNode.node),
91
- parentNodeType: blockControlsContext.targetNode.parentType === 'doc' ? undefined : blockControlsContext.targetNode.parentType
92
- };
93
- const placement = isTopLevel ? getAnchoredLeftGutterSurfacePlacement(options) : getAnchoredSurfacePlacement({
94
- ...options,
95
- side: 'left'
96
- });
97
- return [[position, placement]];
98
- })), [surfaces]);
99
- // Only the surfaces that still need measuring drive the effect below, so a document of anchored
100
- // blocks binds no scroll or resize listeners at all.
101
- //
102
- // The shared empty array matters: `surfaces` gets a new identity on every hover, since
103
- // `activeNode` is one of its dependencies. Returning a fresh `[]` would give the effect a
104
- // changed dependency each time and re-run it for nothing, which is the cost the anchored path
105
- // exists to avoid.
106
- const measuredSurfaces = useMemo(() => {
107
- const measured = surfaces.filter(({
108
- anchorName
109
- }) => !anchorName);
110
- return measured.length === 0 ? EMPTY_MEASURED_SURFACES : measured;
111
- }, [surfaces]);
112
- const [placements, setPlacements] = useState(new Map());
113
- const animationFrameRef = useRef(undefined);
114
- useLayoutEffect(() => {
115
- var _getDocument;
116
- if (measuredSurfaces.length === 0) {
117
- // Clear only when there is something to clear. Handing React a fresh Map every time would
118
- // fail its identity check and commit a render to replace one empty map with another.
119
- setPlacements(previous => previous.size > 0 ? new Map() : previous);
120
- return;
121
- }
122
- const measurementTargets = measuredSurfaces.flatMap(({
123
- blockControlsContext,
124
- isTopLevel,
125
- position
126
- }) => {
127
- const targetNodeElement = editorView.nodeDOM(blockControlsContext.targetNode.pos);
128
- const targetElementRoot = targetNodeElement instanceof HTMLElement ? targetNodeElement : targetNodeElement === null || targetNodeElement === void 0 ? void 0 : targetNodeElement.parentElement;
129
- if (!targetElementRoot) {
130
- return [];
131
- }
132
- return [{
133
- blockControlsContext,
134
- isTopLevel,
135
- position,
136
- targetElement: getNodeContentElement(targetElementRoot, blockControlsContext.targetNode.type.name),
137
- targetElementRoot
138
- }];
139
- });
140
- const offsetParent = editorView.dom.offsetParent;
141
- const isDocumentOffsetParent = !offsetParent || offsetParent === ((_getDocument = getDocument()) === null || _getDocument === void 0 ? void 0 : _getDocument.body);
142
- const scrollingOffsetParent = offsetParent instanceof HTMLElement && !isDocumentOffsetParent ? offsetParent : undefined;
143
- const updatePositions = () => {
144
- var _scrollingOffsetParen, _scrollingOffsetParen2;
145
- animationFrameRef.current = undefined;
146
- const offsetParentRect = offsetParent === null || offsetParent === void 0 ? void 0 : offsetParent.getBoundingClientRect();
147
- const offsetParentScrollTop = (_scrollingOffsetParen = scrollingOffsetParent === null || scrollingOffsetParent === void 0 ? void 0 : scrollingOffsetParent.scrollTop) !== null && _scrollingOffsetParen !== void 0 ? _scrollingOffsetParen : window.scrollY;
148
- const offsetParentScrollLeft = (_scrollingOffsetParen2 = scrollingOffsetParent === null || scrollingOffsetParent === void 0 ? void 0 : scrollingOffsetParent.scrollLeft) !== null && _scrollingOffsetParen2 !== void 0 ? _scrollingOffsetParen2 : window.scrollX;
149
- const nextPlacements = new Map();
150
- for (const {
151
- blockControlsContext,
152
- isTopLevel,
153
- position,
154
- targetElement,
155
- targetElementRoot
156
- } of measurementTargets) {
157
- var _targetElementRoot$ge, _offsetParentRect$lef;
158
- if (!targetElementRoot.isConnected || !targetElement.isConnected) {
159
- continue;
160
- }
161
- const options = {
162
- layout: (_targetElementRoot$ge = targetElementRoot.getAttribute('layout')) !== null && _targetElementRoot$ge !== void 0 ? _targetElementRoot$ge : '',
163
- nodeRect: targetElement.getBoundingClientRect(),
164
- nodeType: blockControlsContext.targetNode.type.name,
165
- nodeTypeWithLevel: getNodeTypeWithLevel(blockControlsContext.targetNode.node),
166
- parentNodeType: blockControlsContext.targetNode.parentType === 'doc' ? undefined : blockControlsContext.targetNode.parentType
167
- };
168
- const placement = isTopLevel ? getLeftGutterSurfacePlacement({
169
- ...options,
170
- leftGutterBoundary: (_offsetParentRect$lef = offsetParentRect === null || offsetParentRect === void 0 ? void 0 : offsetParentRect.left) !== null && _offsetParentRect$lef !== void 0 ? _offsetParentRect$lef : 0
171
- }) : getSurfacePlacement({
172
- ...options,
173
- side: 'left'
174
- });
175
- nextPlacements.set(position, toMeasuredSurfaceWrapperPlacement(getAbsoluteSurfacePlacement({
176
- offsetParentRect,
177
- offsetParentScrollLeft,
178
- offsetParentScrollTop,
179
- placement
180
- })));
181
- }
182
- setPlacements(nextPlacements);
183
- };
184
- const schedulePositionUpdate = () => {
185
- if (animationFrameRef.current === undefined) {
186
- animationFrameRef.current = requestAnimationFrame(updatePositions);
187
- }
188
- };
189
- updatePositions();
190
- const resizeObserver = new ResizeObserver(schedulePositionUpdate);
191
- const observedElements = new Set([editorView.dom]);
192
- for (const {
193
- targetElement
194
- } of measurementTargets) {
195
- observedElements.add(targetElement);
196
- }
197
- if (offsetParent instanceof HTMLElement) {
198
- observedElements.add(offsetParent);
199
- }
200
- observedElements.forEach(element => resizeObserver.observe(element));
201
- const unbindWindowResize = bind(window, {
202
- type: 'resize',
203
- listener: schedulePositionUpdate
204
- });
205
- const unbindWindowScroll = bind(window, {
206
- type: 'scroll',
207
- listener: schedulePositionUpdate,
208
- options: {
209
- capture: true,
210
- passive: true
211
- }
212
- });
213
- const visualViewport = window.visualViewport;
214
- const unbindVisualViewportResize = visualViewport ? bind(visualViewport, {
215
- type: 'resize',
216
- listener: schedulePositionUpdate
217
- }) : undefined;
218
- const unbindVisualViewportScroll = visualViewport ? bind(visualViewport, {
219
- type: 'scroll',
220
- listener: schedulePositionUpdate
221
- }) : undefined;
222
- const unbindTransitionEnd = bind(editorView.dom, {
223
- type: 'transitionend',
224
- listener: schedulePositionUpdate,
225
- options: {
226
- capture: true
227
- }
228
- });
229
- return () => {
230
- if (animationFrameRef.current !== undefined) {
231
- cancelAnimationFrame(animationFrameRef.current);
232
- animationFrameRef.current = undefined;
233
- }
234
- resizeObserver.disconnect();
235
- unbindWindowResize();
236
- unbindWindowScroll();
237
- unbindVisualViewportResize === null || unbindVisualViewportResize === void 0 ? void 0 : unbindVisualViewportResize();
238
- unbindVisualViewportScroll === null || unbindVisualViewportScroll === void 0 ? void 0 : unbindVisualViewportScroll();
239
- unbindTransitionEnd();
240
- };
241
- }, [editorView, measuredSurfaces]);
242
- return /*#__PURE__*/React.createElement(React.Fragment, null, surfaces.map(({
243
- position,
244
- source,
245
- surfaceComponents,
246
- surfaceContext
247
- }) => {
248
- var _activeNode$handleOpt, _anchoredPlacements$g;
249
- return /*#__PURE__*/React.createElement(BlockControlsLeftSurface, {
250
- api: api,
251
- components: surfaceComponents,
252
- forceVisibleOnMouseOut: Boolean(activeNode === null || activeNode === void 0 ? void 0 : (_activeNode$handleOpt = activeNode.handleOptions) === null || _activeNode$handleOpt === void 0 ? void 0 : _activeNode$handleOpt.isFocused),
253
- key: position,
254
- placement: (_anchoredPlacements$g = anchoredPlacements.get(position)) !== null && _anchoredPlacements$g !== void 0 ? _anchoredPlacements$g : placements.get(position),
255
- source: source,
256
- surfaceContext: surfaceContext
257
- });
258
- }));
259
- };
@@ -1,245 +0,0 @@
1
- /* block-controls-right-surfaces.tsx generated by @compiled/babel-plugin v3.0.4 */
2
- import { ax, ix } from "@compiled/react/runtime";
3
- import React, { useLayoutEffect, useMemo, useRef, useState } from 'react';
4
- import { bind } from 'bind-event-listener';
5
- import { getDocument } from '@atlaskit/browser-apis';
6
- import { BLOCK_CONTROL_UI_CONTEXT } from '@atlaskit/editor-common/block-controls/block-control-ui-context';
7
- import { BLOCK_CONTROLS_RIGHT_SURFACE } from '@atlaskit/editor-common/block-controls/surface-keys';
8
- import { isCSSAnchorSupported } from '@atlaskit/editor-common/styles/native-anchor';
9
- import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/useSharedPluginStateWithSelector';
10
- import { createSurfaceContext } from '@atlaskit/editor-ui-control-model/create-surface-context';
11
- import { getNodeTypeWithLevel } from '../pm-plugins/decorations-common';
12
- import { BlockControlsRightSurface } from './block-controls-right-surface';
13
- import { getBlockControlsRightSurfaceComponents } from './block-controls-surface-components';
14
- import { createBlockControlsSurfaceContextForPosition } from './block-controls-surface-context';
15
- import { getBlockControlsSurfaceTargets } from './block-controls-surface-targets';
16
- import { getNodeContentElement } from './utils/get-node-content-element';
17
- import { getAbsoluteSurfacePlacement, getAnchoredSurfacePlacement, getSurfacePlacement, toMeasuredSurfaceWrapperPlacement } from './utils/get-surface-placement';
18
- const EMPTY_SURFACE_POSITIONS = [];
19
- const EMPTY_SURFACE_ANCHORS = new Map();
20
- const EMPTY_SURFACE_COMPONENTS = [];
21
- const EMPTY_MEASURED_SURFACES = [];
22
- /**
23
- * Registry-backed right surface tree. Mirrors the left surface's single-tree approach, but keeps its
24
- * own components lookup, targets, placements and measurement lifecycle so the right surface never
25
- * depends on the left surface's render pass.
26
- *
27
- * Targets are placed one of two ways. Where the node's anchor name can be relied on, the surface is
28
- * positioned with `anchor()` and the browser keeps it attached with nothing re-rendering. Everything
29
- * else is measured and kept honest by the observers below. See `hasInnerContentContainer`.
30
- */
31
- export const BlockControlsRightSurfaces = ({
32
- api,
33
- editorView
34
- }) => {
35
- var _api$uiControlRegistr, _api$uiControlRegistr2;
36
- const {
37
- activeNode,
38
- surfaceAnchors,
39
- surfaceActiveNodes,
40
- surfaceNodePositions
41
- } = useSharedPluginStateWithSelector(api, ['blockControls'], states => {
42
- var _states$blockControls, _states$blockControls2, _states$blockControls3, _states$blockControls4, _states$blockControls5, _states$blockControls6;
43
- return {
44
- activeNode: (_states$blockControls = states.blockControlsState) === null || _states$blockControls === void 0 ? void 0 : _states$blockControls.activeNode,
45
- surfaceActiveNodes: (_states$blockControls2 = states.blockControlsState) === null || _states$blockControls2 === void 0 ? void 0 : _states$blockControls2.surfaceActiveNodes,
46
- surfaceAnchors: (_states$blockControls3 = (_states$blockControls4 = states.blockControlsState) === null || _states$blockControls4 === void 0 ? void 0 : _states$blockControls4.surfaceAnchors) !== null && _states$blockControls3 !== void 0 ? _states$blockControls3 : EMPTY_SURFACE_ANCHORS,
47
- surfaceNodePositions: (_states$blockControls5 = (_states$blockControls6 = states.blockControlsState) === null || _states$blockControls6 === void 0 ? void 0 : _states$blockControls6.surfaceNodePositions) !== null && _states$blockControls5 !== void 0 ? _states$blockControls5 : EMPTY_SURFACE_POSITIONS
48
- };
49
- });
50
- const components = (_api$uiControlRegistr = (_api$uiControlRegistr2 = api.uiControlRegistry) === null || _api$uiControlRegistr2 === void 0 ? void 0 : _api$uiControlRegistr2.actions.getComponents(BLOCK_CONTROLS_RIGHT_SURFACE)) !== null && _api$uiControlRegistr !== void 0 ? _api$uiControlRegistr : EMPTY_SURFACE_COMPONENTS;
51
- const targets = useMemo(() => {
52
- const candidates = new Set(surfaceNodePositions);
53
- return getBlockControlsSurfaceTargets(activeNode, surfaceNodePositions).filter(({
54
- position
55
- }) => candidates.has(position));
56
- }, [activeNode, surfaceNodePositions]);
57
- const surfaces = useMemo(() => targets.flatMap(target => {
58
- var _surfaceActiveNodes$g;
59
- const context = createBlockControlsSurfaceContextForPosition(editorView.state, target.position, (_surfaceActiveNodes$g = surfaceActiveNodes === null || surfaceActiveNodes === void 0 ? void 0 : surfaceActiveNodes.get(target.position)) !== null && _surfaceActiveNodes$g !== void 0 ? _surfaceActiveNodes$g : activeNode);
60
- if (!context) {
61
- return [];
62
- }
63
- const surfaceContext = createSurfaceContext(BLOCK_CONTROL_UI_CONTEXT, context);
64
- const surfaceComponents = getBlockControlsRightSurfaceComponents(components, surfaceContext);
65
- if (!surfaceComponents) {
66
- return [];
67
- }
68
- // The cache supplies the same anchor names used by ProseMirror decorations.
69
- const anchorName = isCSSAnchorSupported() ? surfaceAnchors.get(target.position) : undefined;
70
- return [{
71
- ...target,
72
- anchorName,
73
- blockControlsContext: context,
74
- surfaceComponents,
75
- surfaceContext
76
- }];
77
- }), [activeNode, components, editorView, targets, surfaceAnchors, surfaceActiveNodes]);
78
- /**
79
- * Placements that need no measuring, so they are derived rather than stored: every inset is an
80
- * `anchor()` the browser re-resolves on every layout change.
81
- */
82
- const anchoredPlacements = useMemo(() => new Map(surfaces.flatMap(({
83
- anchorName,
84
- blockControlsContext,
85
- position
86
- }) => anchorName ? [[position, getAnchoredSurfacePlacement({
87
- anchorName,
88
- nodeType: blockControlsContext.targetNode.type.name,
89
- nodeTypeWithLevel: getNodeTypeWithLevel(blockControlsContext.targetNode.node),
90
- layout: blockControlsContext.targetNode.node.attrs.layout,
91
- parentNodeType: blockControlsContext.targetNode.parentType === 'doc' ? undefined : blockControlsContext.targetNode.parentType,
92
- side: 'right'
93
- })]] : [])), [surfaces]);
94
- // Only the surfaces that still need measuring drive the effect below, so a document of anchored
95
- // blocks binds no scroll or resize listeners at all.
96
- //
97
- // The shared empty array matters: `surfaces` gets a new identity on every hover, since
98
- // `activeNode` is one of its dependencies. Returning a fresh `[]` would give the effect a
99
- // changed dependency each time and re-run it for nothing, which is the cost the anchored path
100
- // exists to avoid.
101
- const measuredSurfaces = useMemo(() => {
102
- const measured = surfaces.filter(({
103
- anchorName
104
- }) => !anchorName);
105
- return measured.length === 0 ? EMPTY_MEASURED_SURFACES : measured;
106
- }, [surfaces]);
107
- const [placements, setPlacements] = useState(new Map());
108
- const animationFrameRef = useRef(undefined);
109
- useLayoutEffect(() => {
110
- var _getDocument;
111
- if (measuredSurfaces.length === 0) {
112
- // Clear only when there is something to clear. Handing React a fresh Map every time would
113
- // fail its identity check and commit a render to replace one empty map with another.
114
- setPlacements(previous => previous.size > 0 ? new Map() : previous);
115
- return;
116
- }
117
- const measurementTargets = measuredSurfaces.flatMap(({
118
- blockControlsContext,
119
- position
120
- }) => {
121
- const targetNodeElement = editorView.nodeDOM(blockControlsContext.targetNode.pos);
122
- const targetElementRoot = targetNodeElement instanceof HTMLElement ? targetNodeElement : targetNodeElement === null || targetNodeElement === void 0 ? void 0 : targetNodeElement.parentElement;
123
- if (!targetElementRoot) {
124
- return [];
125
- }
126
- return [{
127
- blockControlsContext,
128
- position,
129
- targetElement: getNodeContentElement(targetElementRoot, blockControlsContext.targetNode.type.name),
130
- targetElementRoot
131
- }];
132
- });
133
- const offsetParent = editorView.dom.offsetParent;
134
- const isDocumentOffsetParent = !offsetParent || offsetParent === ((_getDocument = getDocument()) === null || _getDocument === void 0 ? void 0 : _getDocument.body);
135
- const scrollingOffsetParent = offsetParent instanceof HTMLElement && !isDocumentOffsetParent ? offsetParent : undefined;
136
- const updatePositions = () => {
137
- var _scrollingOffsetParen, _scrollingOffsetParen2;
138
- animationFrameRef.current = undefined;
139
- const offsetParentRect = offsetParent === null || offsetParent === void 0 ? void 0 : offsetParent.getBoundingClientRect();
140
- const offsetParentScrollTop = (_scrollingOffsetParen = scrollingOffsetParent === null || scrollingOffsetParent === void 0 ? void 0 : scrollingOffsetParent.scrollTop) !== null && _scrollingOffsetParen !== void 0 ? _scrollingOffsetParen : window.scrollY;
141
- const offsetParentScrollLeft = (_scrollingOffsetParen2 = scrollingOffsetParent === null || scrollingOffsetParent === void 0 ? void 0 : scrollingOffsetParent.scrollLeft) !== null && _scrollingOffsetParen2 !== void 0 ? _scrollingOffsetParen2 : window.scrollX;
142
- const nextPlacements = new Map();
143
- for (const {
144
- blockControlsContext,
145
- position,
146
- targetElement,
147
- targetElementRoot
148
- } of measurementTargets) {
149
- var _targetElementRoot$ge;
150
- if (!targetElementRoot.isConnected || !targetElement.isConnected) {
151
- continue;
152
- }
153
- const placement = getSurfacePlacement({
154
- layout: (_targetElementRoot$ge = targetElementRoot.getAttribute('layout')) !== null && _targetElementRoot$ge !== void 0 ? _targetElementRoot$ge : '',
155
- nodeRect: targetElement.getBoundingClientRect(),
156
- nodeType: blockControlsContext.targetNode.type.name,
157
- nodeTypeWithLevel: getNodeTypeWithLevel(blockControlsContext.targetNode.node),
158
- parentNodeType: blockControlsContext.targetNode.parentType === 'doc' ? undefined : blockControlsContext.targetNode.parentType,
159
- side: 'right'
160
- });
161
- nextPlacements.set(position, toMeasuredSurfaceWrapperPlacement(getAbsoluteSurfacePlacement({
162
- offsetParentRect,
163
- offsetParentScrollLeft,
164
- offsetParentScrollTop,
165
- placement
166
- })));
167
- }
168
- setPlacements(nextPlacements);
169
- };
170
- const schedulePositionUpdate = () => {
171
- if (animationFrameRef.current === undefined) {
172
- animationFrameRef.current = requestAnimationFrame(updatePositions);
173
- }
174
- };
175
- updatePositions();
176
- const resizeObserver = new ResizeObserver(schedulePositionUpdate);
177
- const observedElements = new Set([editorView.dom]);
178
- for (const {
179
- targetElement
180
- } of measurementTargets) {
181
- observedElements.add(targetElement);
182
- }
183
- if (offsetParent instanceof HTMLElement) {
184
- observedElements.add(offsetParent);
185
- }
186
- observedElements.forEach(element => resizeObserver.observe(element));
187
- const unbindWindowResize = bind(window, {
188
- type: 'resize',
189
- listener: schedulePositionUpdate
190
- });
191
- const unbindWindowScroll = bind(window, {
192
- type: 'scroll',
193
- listener: schedulePositionUpdate,
194
- options: {
195
- capture: true,
196
- passive: true
197
- }
198
- });
199
- const visualViewport = window.visualViewport;
200
- const unbindVisualViewportResize = visualViewport ? bind(visualViewport, {
201
- type: 'resize',
202
- listener: schedulePositionUpdate
203
- }) : undefined;
204
- const unbindVisualViewportScroll = visualViewport ? bind(visualViewport, {
205
- type: 'scroll',
206
- listener: schedulePositionUpdate
207
- }) : undefined;
208
- const unbindTransitionEnd = bind(editorView.dom, {
209
- type: 'transitionend',
210
- listener: schedulePositionUpdate,
211
- options: {
212
- capture: true
213
- }
214
- });
215
- return () => {
216
- if (animationFrameRef.current !== undefined) {
217
- cancelAnimationFrame(animationFrameRef.current);
218
- animationFrameRef.current = undefined;
219
- }
220
- resizeObserver.disconnect();
221
- unbindWindowResize();
222
- unbindWindowScroll();
223
- unbindVisualViewportResize === null || unbindVisualViewportResize === void 0 ? void 0 : unbindVisualViewportResize();
224
- unbindVisualViewportScroll === null || unbindVisualViewportScroll === void 0 ? void 0 : unbindVisualViewportScroll();
225
- unbindTransitionEnd();
226
- };
227
- }, [editorView, measuredSurfaces]);
228
- return /*#__PURE__*/React.createElement(React.Fragment, null, surfaces.map(({
229
- position,
230
- source,
231
- surfaceComponents,
232
- surfaceContext
233
- }) => {
234
- var _activeNode$handleOpt, _anchoredPlacements$g;
235
- return /*#__PURE__*/React.createElement(BlockControlsRightSurface, {
236
- api: api,
237
- components: surfaceComponents,
238
- forceVisibleOnMouseOut: Boolean(activeNode === null || activeNode === void 0 ? void 0 : (_activeNode$handleOpt = activeNode.handleOptions) === null || _activeNode$handleOpt === void 0 ? void 0 : _activeNode$handleOpt.isFocused),
239
- key: position,
240
- placement: (_anchoredPlacements$g = anchoredPlacements.get(position)) !== null && _anchoredPlacements$g !== void 0 ? _anchoredPlacements$g : placements.get(position),
241
- source: source,
242
- surfaceContext: surfaceContext
243
- });
244
- }));
245
- };
@@ -1,142 +0,0 @@
1
- import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
- 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; }
4
- 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) { _defineProperty(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; }
5
- import { BLOCK_CONTROLS_LEFT_SURFACE, BLOCK_CONTROLS_RIGHT_SURFACE } from '@atlaskit/editor-common/block-controls/surface-keys';
6
- import { getNodeIdProvider } from '@atlaskit/editor-common/node-anchor-provider';
7
- import { resolveSurface } from '@atlaskit/editor-ui-control-model/surface-renderer';
8
- import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
9
- import { createSparseSurfacesViewWithLifecycle } from '../surface-lifecycle/sparse-surfaces-view-with-lifecycle';
10
- import { createSurfaceAnchorDecorations } from './decorations-surface-anchor';
11
- import { key as blockControlsKey } from './main';
12
- import { bindSparseGutterHover } from './sparse-gutter-hover';
13
- import { reconcileSparseSurfaceCandidates } from './utils/sparse-surface-candidates';
14
- import { observeSurfaceViewport } from './utils/surface-viewport';
15
- var haveAnchorPositionsChanged = function haveAnchorPositionsChanged(positions, anchors) {
16
- return positions.length !== anchors.size || positions.some(function (position) {
17
- return !anchors.has(position);
18
- });
19
- };
20
- var createLegacySparseSurfacesView = function createLegacySparseSurfacesView(_ref) {
21
- var _api$uiControlRegistr3, _api$editorDisabled, _api$editorViewMode, _api$limitedMode2, _api$showDiff, _api$userIntent;
22
- var api = _ref.api,
23
- pluginKey = _ref.pluginKey,
24
- view = _ref.view;
25
- var win = view.dom.ownerDocument.defaultView;
26
- var frame;
27
- var visiblePositions = [];
28
- var registryChanged = true;
29
- var reconcile = function reconcile() {
30
- var _api$limitedMode;
31
- frame = undefined;
32
- var previous = pluginKey.getState(view.state);
33
- if (!previous || api !== null && api !== void 0 && (_api$limitedMode = api.limitedMode) !== null && _api$limitedMode !== void 0 && (_api$limitedMode = _api$limitedMode.sharedState.currentState()) !== null && _api$limitedMode !== void 0 && _api$limitedMode.enabled) {
34
- return;
35
- }
36
- var controls = blockControlsKey.getState(view.state);
37
- var activeNode = controls === null || controls === void 0 ? void 0 : controls.activeNode;
38
- var activeNodesByPosition = new Map();
39
- var menu = controls !== null && controls !== void 0 && controls.isMenuOpen ? controls.menuTriggerByNode : undefined;
40
- if (menu && controls !== null && controls !== void 0 && controls.menuTriggerBy && view.state.doc.nodeAt(menu.pos)) {
41
- var _menu$rootPos;
42
- var menuNode = _objectSpread(_objectSpread({}, menu), {}, {
43
- anchorName: controls.menuTriggerBy
44
- });
45
- activeNodesByPosition.set(menu.pos, menuNode);
46
- activeNodesByPosition.set((_menu$rootPos = menu.rootPos) !== null && _menu$rootPos !== void 0 ? _menu$rootPos : menu.pos, menuNode);
47
- }
48
- var resolvedSurfaces = [BLOCK_CONTROLS_LEFT_SURFACE, BLOCK_CONTROLS_RIGHT_SURFACE].map(function (surface) {
49
- var _api$uiControlRegistr, _api$uiControlRegistr2;
50
- return resolveSurface((_api$uiControlRegistr = api === null || api === void 0 || (_api$uiControlRegistr2 = api.uiControlRegistry) === null || _api$uiControlRegistr2 === void 0 ? void 0 : _api$uiControlRegistr2.actions.getComponents(surface)) !== null && _api$uiControlRegistr !== void 0 ? _api$uiControlRegistr : [], surface);
51
- });
52
- var candidates = reconcileSparseSurfaceCandidates({
53
- activeNode: activeNode,
54
- activeNodesByPosition: activeNodesByPosition,
55
- newState: view.state,
56
- state: previous.candidates,
57
- visiblePositions: visiblePositions,
58
- protectedPositions: _toConsumableArray(activeNodesByPosition.keys()),
59
- resolvedSurfaces: resolvedSurfaces,
60
- documentChanged: previous.documentChanged,
61
- invalidation: _objectSpread(_objectSpread({}, previous.invalidation), {}, {
62
- all: registryChanged
63
- })
64
- });
65
- var visibilityChanged = registryChanged;
66
- registryChanged = false;
67
- var anchorPositions = candidates.positions;
68
- if (!visibilityChanged && !haveAnchorPositionsChanged(anchorPositions, previous.anchors) && candidates === previous.candidates && !previous.documentChanged && !previous.invalidation) {
69
- return;
70
- }
71
- var nodeIdProvider = getNodeIdProvider(view);
72
- var _createSurfaceAnchorD = createSurfaceAnchorDecorations(view.state, anchorPositions, function (node, position, previousAnchorName) {
73
- if (previousAnchorName) {
74
- nodeIdProvider.setIdForNode(node, previousAnchorName);
75
- return previousAnchorName;
76
- }
77
- return nodeIdProvider.getOrGenerateId(node, position);
78
- }, previous.anchors),
79
- anchors = _createSurfaceAnchorD.anchors,
80
- decorations = _createSurfaceAnchorD.decorations;
81
- view.dispatch(view.state.tr.setMeta(pluginKey, {
82
- reconciliation: {
83
- activeNodesByPosition: activeNodesByPosition,
84
- anchors: anchors,
85
- candidates: candidates,
86
- decorations: decorations
87
- },
88
- type: 'commitReconciliation'
89
- }));
90
- };
91
- var schedule = function schedule() {
92
- if (frame === undefined && win) {
93
- frame = win.requestAnimationFrame(reconcile);
94
- }
95
- };
96
- var observer = observeSurfaceViewport(view, function (positions) {
97
- visiblePositions = positions;
98
- schedule();
99
- });
100
- var invalidate = function invalidate() {
101
- registryChanged = true;
102
- schedule();
103
- };
104
- var unsubscribe = api === null || api === void 0 || (_api$uiControlRegistr3 = api.uiControlRegistry) === null || _api$uiControlRegistr3 === void 0 ? void 0 : _api$uiControlRegistr3.actions.subscribe(invalidate);
105
- var unbindGutterHover = bindSparseGutterHover({
106
- api: api,
107
- view: view
108
- });
109
- var unsubscribers = [api === null || api === void 0 || (_api$editorDisabled = api.editorDisabled) === null || _api$editorDisabled === void 0 ? void 0 : _api$editorDisabled.sharedState.onChange(invalidate), api === null || api === void 0 || (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 ? void 0 : _api$editorViewMode.sharedState.onChange(invalidate), api === null || api === void 0 || (_api$limitedMode2 = api.limitedMode) === null || _api$limitedMode2 === void 0 ? void 0 : _api$limitedMode2.sharedState.onChange(invalidate), api === null || api === void 0 || (_api$showDiff = api.showDiff) === null || _api$showDiff === void 0 ? void 0 : _api$showDiff.sharedState.onChange(invalidate), api === null || api === void 0 || (_api$userIntent = api.userIntent) === null || _api$userIntent === void 0 ? void 0 : _api$userIntent.sharedState.onChange(invalidate)];
110
- return {
111
- update: function update(currentView, previousState) {
112
- observer.update();
113
- var sparse = pluginKey.getState(currentView.state);
114
- var controls = blockControlsKey.getState(currentView.state);
115
- var previousControls = blockControlsKey.getState(previousState);
116
- if ((controls === null || controls === void 0 ? void 0 : controls.isDragging) !== (previousControls === null || previousControls === void 0 ? void 0 : previousControls.isDragging) || (controls === null || controls === void 0 ? void 0 : controls.isResizerResizing) !== (previousControls === null || previousControls === void 0 ? void 0 : previousControls.isResizerResizing)) {
117
- invalidate();
118
- }
119
- if (sparse !== null && sparse !== void 0 && sparse.documentChanged || sparse !== null && sparse !== void 0 && sparse.invalidation || (controls === null || controls === void 0 ? void 0 : controls.activeNode) !== (previousControls === null || previousControls === void 0 ? void 0 : previousControls.activeNode) || (controls === null || controls === void 0 ? void 0 : controls.isMenuOpen) !== (previousControls === null || previousControls === void 0 ? void 0 : previousControls.isMenuOpen) || (controls === null || controls === void 0 ? void 0 : controls.menuTriggerByNode) !== (previousControls === null || previousControls === void 0 ? void 0 : previousControls.menuTriggerByNode)) {
120
- schedule();
121
- }
122
- },
123
- destroy: function destroy() {
124
- if (frame !== undefined) {
125
- win === null || win === void 0 || win.cancelAnimationFrame(frame);
126
- }
127
- observer.destroy();
128
- unbindGutterHover === null || unbindGutterHover === void 0 || unbindGutterHover();
129
- unsubscribe === null || unsubscribe === void 0 || unsubscribe();
130
- unsubscribers.forEach(function (unsubscribe) {
131
- return unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe();
132
- });
133
- }
134
- };
135
- };
136
- export var createSparseSurfacesView = function createSparseSurfacesView(options) {
137
- if (isExperimentEnabled('platform_editor_block_controls_cls_fix')) {
138
- return createSparseSurfacesViewWithLifecycle(options);
139
- } else {
140
- return createLegacySparseSurfacesView(options);
141
- }
142
- };