@atlaskit/editor-plugin-block-controls 34.0.3 → 34.0.5

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 (49) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/cjs/blockControlsPlugin.js +2 -10
  3. package/dist/cjs/pm-plugins/interaction-tracking/handle-key-down.js +8 -0
  4. package/dist/cjs/pm-plugins/interaction-tracking/handle-mouse-move.js +121 -2
  5. package/dist/cjs/pm-plugins/interaction-tracking/pm-plugin.js +3 -0
  6. package/dist/cjs/pm-plugins/sparse-gutter-hover.js +17 -57
  7. package/dist/cjs/pm-plugins/sparse-surfaces.js +2 -2
  8. package/dist/cjs/pm-plugins/utils/after-render-scheduler.js +39 -0
  9. package/dist/cjs/ui/block-controls-left-surface.js +19 -83
  10. package/dist/cjs/ui/block-controls-right-surface.js +19 -70
  11. package/dist/cjs/ui/block-controls-surface-targets.js +1 -1
  12. package/dist/es2019/blockControlsPlugin.js +2 -10
  13. package/dist/es2019/pm-plugins/interaction-tracking/handle-key-down.js +8 -0
  14. package/dist/es2019/pm-plugins/interaction-tracking/handle-mouse-move.js +118 -1
  15. package/dist/es2019/pm-plugins/interaction-tracking/pm-plugin.js +4 -1
  16. package/dist/es2019/pm-plugins/sparse-gutter-hover.js +16 -56
  17. package/dist/es2019/pm-plugins/sparse-surfaces.js +2 -2
  18. package/dist/es2019/pm-plugins/utils/after-render-scheduler.js +33 -0
  19. package/dist/es2019/ui/block-controls-left-surface.js +10 -76
  20. package/dist/es2019/ui/block-controls-right-surface.js +10 -63
  21. package/dist/es2019/ui/block-controls-surface-targets.js +1 -1
  22. package/dist/esm/blockControlsPlugin.js +2 -10
  23. package/dist/esm/pm-plugins/interaction-tracking/handle-key-down.js +8 -0
  24. package/dist/esm/pm-plugins/interaction-tracking/handle-mouse-move.js +120 -1
  25. package/dist/esm/pm-plugins/interaction-tracking/pm-plugin.js +4 -1
  26. package/dist/esm/pm-plugins/sparse-gutter-hover.js +16 -56
  27. package/dist/esm/pm-plugins/sparse-surfaces.js +2 -2
  28. package/dist/esm/pm-plugins/utils/after-render-scheduler.js +33 -0
  29. package/dist/esm/ui/block-controls-left-surface.js +19 -83
  30. package/dist/esm/ui/block-controls-right-surface.js +19 -70
  31. package/dist/esm/ui/block-controls-surface-targets.js +1 -1
  32. package/dist/types/pm-plugins/interaction-tracking/handle-mouse-move.d.ts +1 -0
  33. package/dist/types/pm-plugins/utils/after-render-scheduler.d.ts +9 -0
  34. package/dist/types/ui/block-controls-left-surface.d.ts +1 -1
  35. package/dist/types/ui/block-controls-right-surface.d.ts +1 -1
  36. package/dist/types/ui/block-controls-surface-targets.d.ts +1 -1
  37. package/package.json +4 -4
  38. package/dist/cjs/pm-plugins/sparse-surfaces-view.js +0 -149
  39. package/dist/cjs/ui/block-controls-left-surfaces.js +0 -294
  40. package/dist/cjs/ui/block-controls-right-surfaces.js +0 -285
  41. package/dist/es2019/pm-plugins/sparse-surfaces-view.js +0 -136
  42. package/dist/es2019/ui/block-controls-left-surfaces.js +0 -259
  43. package/dist/es2019/ui/block-controls-right-surfaces.js +0 -245
  44. package/dist/esm/pm-plugins/sparse-surfaces-view.js +0 -142
  45. package/dist/esm/ui/block-controls-left-surfaces.js +0 -285
  46. package/dist/esm/ui/block-controls-right-surfaces.js +0 -276
  47. package/dist/types/pm-plugins/sparse-surfaces-view.d.ts +0 -16
  48. package/dist/types/ui/block-controls-left-surfaces.d.ts +0 -15
  49. package/dist/types/ui/block-controls-right-surfaces.d.ts +0 -24
@@ -1,136 +0,0 @@
1
- import { BLOCK_CONTROLS_LEFT_SURFACE, BLOCK_CONTROLS_RIGHT_SURFACE } from '@atlaskit/editor-common/block-controls/surface-keys';
2
- import { getNodeIdProvider } from '@atlaskit/editor-common/node-anchor-provider';
3
- import { resolveSurface } from '@atlaskit/editor-ui-control-model/surface-renderer';
4
- import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
5
- import { createSparseSurfacesViewWithLifecycle } from '../surface-lifecycle/sparse-surfaces-view-with-lifecycle';
6
- import { createSurfaceAnchorDecorations } from './decorations-surface-anchor';
7
- import { key as blockControlsKey } from './main';
8
- import { bindSparseGutterHover } from './sparse-gutter-hover';
9
- import { reconcileSparseSurfaceCandidates } from './utils/sparse-surface-candidates';
10
- import { observeSurfaceViewport } from './utils/surface-viewport';
11
- const haveAnchorPositionsChanged = (positions, anchors) => positions.length !== anchors.size || positions.some(position => !anchors.has(position));
12
- const createLegacySparseSurfacesView = ({
13
- api,
14
- pluginKey,
15
- view
16
- }) => {
17
- var _api$uiControlRegistr3, _api$editorDisabled, _api$editorViewMode, _api$limitedMode2, _api$showDiff, _api$userIntent;
18
- const win = view.dom.ownerDocument.defaultView;
19
- let frame;
20
- let visiblePositions = [];
21
- let registryChanged = true;
22
- const reconcile = () => {
23
- var _api$limitedMode, _api$limitedMode$shar;
24
- frame = undefined;
25
- const previous = pluginKey.getState(view.state);
26
- if (!previous || api !== null && api !== void 0 && (_api$limitedMode = api.limitedMode) !== null && _api$limitedMode !== void 0 && (_api$limitedMode$shar = _api$limitedMode.sharedState.currentState()) !== null && _api$limitedMode$shar !== void 0 && _api$limitedMode$shar.enabled) {
27
- return;
28
- }
29
- const controls = blockControlsKey.getState(view.state);
30
- const activeNode = controls === null || controls === void 0 ? void 0 : controls.activeNode;
31
- const activeNodesByPosition = new Map();
32
- const menu = controls !== null && controls !== void 0 && controls.isMenuOpen ? controls.menuTriggerByNode : undefined;
33
- if (menu && controls !== null && controls !== void 0 && controls.menuTriggerBy && view.state.doc.nodeAt(menu.pos)) {
34
- var _menu$rootPos;
35
- const menuNode = {
36
- ...menu,
37
- anchorName: controls.menuTriggerBy
38
- };
39
- activeNodesByPosition.set(menu.pos, menuNode);
40
- activeNodesByPosition.set((_menu$rootPos = menu.rootPos) !== null && _menu$rootPos !== void 0 ? _menu$rootPos : menu.pos, menuNode);
41
- }
42
- const resolvedSurfaces = [BLOCK_CONTROLS_LEFT_SURFACE, BLOCK_CONTROLS_RIGHT_SURFACE].map(surface => {
43
- var _api$uiControlRegistr, _api$uiControlRegistr2;
44
- return resolveSurface((_api$uiControlRegistr = api === null || api === void 0 ? 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);
45
- });
46
- const candidates = reconcileSparseSurfaceCandidates({
47
- activeNode,
48
- activeNodesByPosition,
49
- newState: view.state,
50
- state: previous.candidates,
51
- visiblePositions,
52
- protectedPositions: [...activeNodesByPosition.keys()],
53
- resolvedSurfaces,
54
- documentChanged: previous.documentChanged,
55
- invalidation: {
56
- ...previous.invalidation,
57
- all: registryChanged
58
- }
59
- });
60
- const visibilityChanged = registryChanged;
61
- registryChanged = false;
62
- const anchorPositions = candidates.positions;
63
- if (!visibilityChanged && !haveAnchorPositionsChanged(anchorPositions, previous.anchors) && candidates === previous.candidates && !previous.documentChanged && !previous.invalidation) {
64
- return;
65
- }
66
- const nodeIdProvider = getNodeIdProvider(view);
67
- const {
68
- anchors,
69
- decorations
70
- } = createSurfaceAnchorDecorations(view.state, anchorPositions, (node, position, previousAnchorName) => {
71
- if (previousAnchorName) {
72
- nodeIdProvider.setIdForNode(node, previousAnchorName);
73
- return previousAnchorName;
74
- }
75
- return nodeIdProvider.getOrGenerateId(node, position);
76
- }, previous.anchors);
77
- view.dispatch(view.state.tr.setMeta(pluginKey, {
78
- reconciliation: {
79
- activeNodesByPosition,
80
- anchors,
81
- candidates,
82
- decorations
83
- },
84
- type: 'commitReconciliation'
85
- }));
86
- };
87
- const schedule = () => {
88
- if (frame === undefined && win) {
89
- frame = win.requestAnimationFrame(reconcile);
90
- }
91
- };
92
- const observer = observeSurfaceViewport(view, positions => {
93
- visiblePositions = positions;
94
- schedule();
95
- });
96
- const invalidate = () => {
97
- registryChanged = true;
98
- schedule();
99
- };
100
- const unsubscribe = api === null || api === void 0 ? void 0 : (_api$uiControlRegistr3 = api.uiControlRegistry) === null || _api$uiControlRegistr3 === void 0 ? void 0 : _api$uiControlRegistr3.actions.subscribe(invalidate);
101
- const unbindGutterHover = bindSparseGutterHover({
102
- api,
103
- view
104
- });
105
- const unsubscribers = [api === null || api === void 0 ? void 0 : (_api$editorDisabled = api.editorDisabled) === null || _api$editorDisabled === void 0 ? void 0 : _api$editorDisabled.sharedState.onChange(invalidate), api === null || api === void 0 ? void 0 : (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 ? void 0 : _api$editorViewMode.sharedState.onChange(invalidate), api === null || api === void 0 ? void 0 : (_api$limitedMode2 = api.limitedMode) === null || _api$limitedMode2 === void 0 ? void 0 : _api$limitedMode2.sharedState.onChange(invalidate), api === null || api === void 0 ? void 0 : (_api$showDiff = api.showDiff) === null || _api$showDiff === void 0 ? void 0 : _api$showDiff.sharedState.onChange(invalidate), api === null || api === void 0 ? void 0 : (_api$userIntent = api.userIntent) === null || _api$userIntent === void 0 ? void 0 : _api$userIntent.sharedState.onChange(invalidate)];
106
- return {
107
- update(currentView, previousState) {
108
- observer.update();
109
- const sparse = pluginKey.getState(currentView.state);
110
- const controls = blockControlsKey.getState(currentView.state);
111
- const previousControls = blockControlsKey.getState(previousState);
112
- 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)) {
113
- invalidate();
114
- }
115
- 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)) {
116
- schedule();
117
- }
118
- },
119
- destroy() {
120
- if (frame !== undefined) {
121
- win === null || win === void 0 ? void 0 : win.cancelAnimationFrame(frame);
122
- }
123
- observer.destroy();
124
- unbindGutterHover === null || unbindGutterHover === void 0 ? void 0 : unbindGutterHover();
125
- unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe();
126
- unsubscribers.forEach(unsubscribe => unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe());
127
- }
128
- };
129
- };
130
- export const createSparseSurfacesView = options => {
131
- if (isExperimentEnabled('platform_editor_block_controls_cls_fix')) {
132
- return createSparseSurfacesViewWithLifecycle(options);
133
- } else {
134
- return createLegacySparseSurfacesView(options);
135
- }
136
- };
@@ -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
- };