@atlaskit/editor-plugin-block-controls 18.2.1 → 18.2.4

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 (34) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/cjs/ui/block-controls-left-surface.compiled.css +1 -0
  3. package/dist/cjs/ui/block-controls-left-surface.js +4 -4
  4. package/dist/cjs/ui/block-controls-left-surfaces.js +61 -15
  5. package/dist/cjs/ui/block-controls-right-surface.compiled.css +1 -0
  6. package/dist/cjs/ui/block-controls-right-surface.js +4 -4
  7. package/dist/cjs/ui/block-controls-right-surfaces.js +68 -18
  8. package/dist/cjs/ui/global-styles.js +27 -1
  9. package/dist/cjs/ui/utils/get-node-content-element.js +4 -1
  10. package/dist/cjs/ui/utils/get-surface-placement.js +65 -1
  11. package/dist/es2019/ui/block-controls-left-surface.compiled.css +1 -0
  12. package/dist/es2019/ui/block-controls-left-surface.js +4 -4
  13. package/dist/es2019/ui/block-controls-left-surfaces.js +51 -11
  14. package/dist/es2019/ui/block-controls-right-surface.compiled.css +1 -0
  15. package/dist/es2019/ui/block-controls-right-surface.js +4 -4
  16. package/dist/es2019/ui/block-controls-right-surfaces.js +58 -14
  17. package/dist/es2019/ui/global-styles.js +31 -2
  18. package/dist/es2019/ui/utils/get-node-content-element.js +1 -0
  19. package/dist/es2019/ui/utils/get-surface-placement.js +67 -0
  20. package/dist/esm/ui/block-controls-left-surface.compiled.css +1 -0
  21. package/dist/esm/ui/block-controls-left-surface.js +4 -4
  22. package/dist/esm/ui/block-controls-left-surfaces.js +63 -17
  23. package/dist/esm/ui/block-controls-right-surface.compiled.css +1 -0
  24. package/dist/esm/ui/block-controls-right-surface.js +4 -4
  25. package/dist/esm/ui/block-controls-right-surfaces.js +70 -20
  26. package/dist/esm/ui/global-styles.js +28 -2
  27. package/dist/esm/ui/utils/get-node-content-element.js +3 -0
  28. package/dist/esm/ui/utils/get-surface-placement.js +64 -0
  29. package/dist/types/ui/block-controls-left-surface.d.ts +2 -2
  30. package/dist/types/ui/block-controls-right-surface.d.ts +2 -2
  31. package/dist/types/ui/block-controls-right-surfaces.d.ts +7 -3
  32. package/dist/types/ui/utils/get-node-content-element.d.ts +1 -0
  33. package/dist/types/ui/utils/get-surface-placement.d.ts +30 -0
  34. package/package.json +4 -4
@@ -6,17 +6,19 @@ import { getDocument } from '@atlaskit/browser-apis';
6
6
  import { BLOCK_CONTROL_UI_CONTEXT } from '@atlaskit/editor-common/block-controls/block-control-ui-context';
7
7
  import { BLOCK_CONTROLS_LEFT_SURFACE } from '@atlaskit/editor-common/block-controls/surface-keys';
8
8
  import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks';
9
+ import { isCSSAnchorSupported } from '@atlaskit/editor-common/styles';
9
10
  import { createSurfaceContext } from '@atlaskit/editor-ui-control-model/create-surface-context';
10
11
  import { willSurfaceRender } from '@atlaskit/editor-ui-control-model/surface-renderer';
11
12
  import { getNodeTypeWithLevel } from '../pm-plugins/decorations-common';
12
13
  import { BlockControlsLeftSurface } from './block-controls-left-surface';
13
14
  import { createBlockControlsSurfaceContext, createBlockControlsSurfaceContextForPosition } from './block-controls-surface-context';
14
15
  import { getBlockControlsSurfaceTargets } from './block-controls-surface-targets';
15
- import { getNodeContentElement } from './utils/get-node-content-element';
16
- import { getAbsoluteSurfacePlacement, getSurfacePlacement } from './utils/get-surface-placement';
16
+ import { getNodeContentElement, hasInnerContentContainer } from './utils/get-node-content-element';
17
+ import { getAbsoluteSurfacePlacement, getAnchoredSurfacePlacement, getSurfacePlacement, toMeasuredSurfaceWrapperPlacement } from './utils/get-surface-placement';
17
18
  import { hasSurfaceControls } from './utils/has-surface-controls';
18
19
  const EMPTY_SURFACE_POSITIONS = [];
19
20
  const EMPTY_SURFACE_COMPONENTS = [];
21
+ const EMPTY_MEASURED_SURFACES = [];
20
22
  export const BlockControlsLeftSurfaces = ({
21
23
  api,
22
24
  editorView
@@ -35,6 +37,7 @@ export const BlockControlsLeftSurfaces = ({
35
37
  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;
36
38
  const targets = useMemo(() => getBlockControlsSurfaceTargets(activeNode, surfaceNodePositions), [activeNode, surfaceNodePositions]);
37
39
  const surfaces = useMemo(() => targets.flatMap(target => {
40
+ var _api$core;
38
41
  const context = target.source === 'stored' ? createBlockControlsSurfaceContextForPosition(editorView.state, target.position, activeNode) : createBlockControlsSurfaceContext(editorView, activeNode, target.source);
39
42
  if (!context) {
40
43
  return [];
@@ -43,21 +46,58 @@ export const BlockControlsLeftSurfaces = ({
43
46
  if (!hasSurfaceControls(components) || !willSurfaceRender(components, BLOCK_CONTROLS_LEFT_SURFACE, surfaceContext)) {
44
47
  return [];
45
48
  }
49
+ // Resolved here rather than in the measurement effect so an anchored surface is placed
50
+ // in the same render that creates it — it never needs a second pass to find its node.
51
+ //
52
+ // At the moment, we only expect top-level nodes to have persistent controls.
53
+ // See surfaceAnchorStyles in global-styles
54
+ const anchorName = isCSSAnchorSupported() && context.targetNode.parentType === 'doc' && !hasInnerContentContainer(context.targetNode.type.name) ? (_api$core = api.core) === null || _api$core === void 0 ? void 0 : _api$core.actions.getAnchorIdForNode(context.targetNode.node, context.targetNode.pos) : undefined;
46
55
  return [{
47
56
  ...target,
57
+ anchorName,
48
58
  blockControlsContext: context,
49
59
  surfaceContext
50
60
  }];
51
- }), [activeNode, components, editorView, targets]);
61
+ }), [activeNode, api, components, editorView, targets]);
62
+ /**
63
+ * Placements that need no measuring, so they are derived rather than stored: every inset is an
64
+ * `anchor()` the browser re-resolves on every layout change.
65
+ */
66
+ const anchoredPlacements = useMemo(() => new Map(surfaces.flatMap(({
67
+ anchorName,
68
+ blockControlsContext,
69
+ position
70
+ }) => anchorName ? [[position, getAnchoredSurfacePlacement({
71
+ anchorName,
72
+ nodeType: blockControlsContext.targetNode.type.name,
73
+ nodeTypeWithLevel: getNodeTypeWithLevel(blockControlsContext.targetNode.node),
74
+ parentNodeType: blockControlsContext.targetNode.parentType === 'doc' ? undefined : blockControlsContext.targetNode.parentType,
75
+ side: 'left'
76
+ })]] : [])), [surfaces]);
77
+ // Only the surfaces that still need measuring drive the effect below, so a document of anchored
78
+ // blocks binds no scroll or resize listeners at all.
79
+ //
80
+ // The shared empty array matters: `surfaces` gets a new identity on every hover, since
81
+ // `activeNode` is one of its dependencies. Returning a fresh `[]` would give the effect a
82
+ // changed dependency each time and re-run it for nothing, which is the cost the anchored path
83
+ // exists to avoid.
84
+ const measuredSurfaces = useMemo(() => {
85
+ const measured = surfaces.filter(({
86
+ anchorName
87
+ }) => !anchorName);
88
+ return measured.length === 0 ? EMPTY_MEASURED_SURFACES : measured;
89
+ }, [surfaces]);
52
90
  const [placements, setPlacements] = useState(new Map());
53
91
  const animationFrameRef = useRef(undefined);
54
92
  useLayoutEffect(() => {
55
93
  var _getDocument;
56
- if (surfaces.length === 0) {
57
- setPlacements(new Map());
94
+ if (measuredSurfaces.length === 0) {
95
+ // Clear only when there is something to clear. Handing React a fresh Map every time would
96
+ // fail its identity check and commit a render to replace one empty map with another.
97
+ setPlacements(previous => previous.size > 0 ? new Map() : previous);
58
98
  return;
59
99
  }
60
- const measurementTargets = surfaces.flatMap(({
100
+ const measurementTargets = measuredSurfaces.flatMap(({
61
101
  blockControlsContext,
62
102
  position
63
103
  }) => {
@@ -101,12 +141,12 @@ export const BlockControlsLeftSurfaces = ({
101
141
  parentNodeType: blockControlsContext.targetNode.parentType === 'doc' ? undefined : blockControlsContext.targetNode.parentType,
102
142
  side: 'left'
103
143
  });
104
- nextPlacements.set(position, getAbsoluteSurfacePlacement({
144
+ nextPlacements.set(position, toMeasuredSurfaceWrapperPlacement(getAbsoluteSurfacePlacement({
105
145
  offsetParentRect,
106
146
  offsetParentScrollLeft,
107
147
  offsetParentScrollTop,
108
148
  placement
109
- }));
149
+ })));
110
150
  }
111
151
  setPlacements(nextPlacements);
112
152
  };
@@ -167,19 +207,19 @@ export const BlockControlsLeftSurfaces = ({
167
207
  unbindVisualViewportScroll === null || unbindVisualViewportScroll === void 0 ? void 0 : unbindVisualViewportScroll();
168
208
  unbindTransitionEnd();
169
209
  };
170
- }, [editorView, surfaces]);
210
+ }, [editorView, measuredSurfaces]);
171
211
  return /*#__PURE__*/React.createElement(React.Fragment, null, surfaces.map(({
172
212
  position,
173
213
  source,
174
214
  surfaceContext
175
215
  }) => {
176
- var _activeNode$handleOpt;
216
+ var _activeNode$handleOpt, _anchoredPlacements$g;
177
217
  return /*#__PURE__*/React.createElement(BlockControlsLeftSurface, {
178
218
  api: api,
179
219
  components: components,
180
220
  forceVisibleOnMouseOut: Boolean(activeNode === null || activeNode === void 0 ? void 0 : (_activeNode$handleOpt = activeNode.handleOptions) === null || _activeNode$handleOpt === void 0 ? void 0 : _activeNode$handleOpt.isFocused),
181
221
  key: position,
182
- placement: placements.get(position),
222
+ placement: (_anchoredPlacements$g = anchoredPlacements.get(position)) !== null && _anchoredPlacements$g !== void 0 ? _anchoredPlacements$g : placements.get(position),
183
223
  source: source,
184
224
  surfaceContext: surfaceContext
185
225
  });
@@ -1,4 +1,5 @@
1
1
  ._zulpv77o{gap:var(--ds-space-025,2px)}._154iu2gc{top:var(--ds-space-100,8px)}
2
+ ._17c61i8t{position-visibility:anchors-valid}
2
3
  ._1e0c1txw{display:flex}
3
4
  ._1pbyb4wl{z-index:100}
4
5
  ._4cvr1h6o{align-items:center}
@@ -32,15 +32,15 @@ export const BlockControlsRightSurface = ({
32
32
  } = partitionComponentsByPersistence(components, BLOCK_CONTROLS_RIGHT_SURFACE, surfaceContext);
33
33
  const willRenderPersistent = willSurfaceRender(persistentComponents, BLOCK_CONTROLS_RIGHT_SURFACE, surfaceContext);
34
34
  const willRenderHoverOnly = willSurfaceRender(hoverOnlyComponents, BLOCK_CONTROLS_RIGHT_SURFACE, surfaceContext);
35
- const isSticky = placement.height !== undefined;
35
+ const isSticky = placement.isSticky;
36
36
  return /*#__PURE__*/React.createElement("div", {
37
37
  "data-editor-block-controls-surface": true,
38
38
  "data-editor-block-controls-side": "right",
39
39
  "data-testid": source === 'stored' ? 'block-controls-right-surface-stored' : source === 'active' ? 'block-controls-right-surface-active' : 'block-controls-right-surface'
40
- // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Placement is resolved from the target node's runtime DOM geometry.
40
+ // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Placement is resolved per target node, from its runtime DOM geometry or from its CSS anchor.
41
41
  ,
42
- style: placement,
43
- className: ax(["_kqswstnw _1pbyb4wl"])
42
+ style: placement.style,
43
+ className: ax(["_kqswstnw _17c61i8t _1pbyb4wl"])
44
44
  }, /*#__PURE__*/React.createElement("div", {
45
45
  className: ax(["_zulpv77o _4cvr1h6o _1e0c1txw", isSticky && "_kqsw1if8 _154iu2gc"])
46
46
  }, willRenderPersistent && /*#__PURE__*/React.createElement(VisibilityContainer, {
@@ -6,21 +6,27 @@ import { getDocument } from '@atlaskit/browser-apis';
6
6
  import { BLOCK_CONTROL_UI_CONTEXT } from '@atlaskit/editor-common/block-controls/block-control-ui-context';
7
7
  import { BLOCK_CONTROLS_RIGHT_SURFACE } from '@atlaskit/editor-common/block-controls/surface-keys';
8
8
  import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks';
9
+ import { isCSSAnchorSupported } from '@atlaskit/editor-common/styles';
9
10
  import { createSurfaceContext } from '@atlaskit/editor-ui-control-model/create-surface-context';
10
11
  import { willSurfaceRender } from '@atlaskit/editor-ui-control-model/surface-renderer';
11
12
  import { getNodeTypeWithLevel } from '../pm-plugins/decorations-common';
12
13
  import { BlockControlsRightSurface } from './block-controls-right-surface';
13
14
  import { createBlockControlsSurfaceContext, createBlockControlsSurfaceContextForPosition } from './block-controls-surface-context';
14
15
  import { getBlockControlsSurfaceTargets } from './block-controls-surface-targets';
15
- import { getNodeContentElement } from './utils/get-node-content-element';
16
- import { getAbsoluteSurfacePlacement, getSurfacePlacement } from './utils/get-surface-placement';
16
+ import { getNodeContentElement, hasInnerContentContainer } from './utils/get-node-content-element';
17
+ import { getAbsoluteSurfacePlacement, getAnchoredSurfacePlacement, getSurfacePlacement, toMeasuredSurfaceWrapperPlacement } from './utils/get-surface-placement';
17
18
  import { hasSurfaceControls } from './utils/has-surface-controls';
18
19
  const EMPTY_SURFACE_POSITIONS = [];
19
20
  const EMPTY_SURFACE_COMPONENTS = [];
21
+ const EMPTY_MEASURED_SURFACES = [];
20
22
  /**
21
- * Registry-backed right surface tree. Mirrors the left surface's single-tree, measured-placement
22
- * approach, but keeps its own components lookup, targets, placements and measurement lifecycle so
23
- * the right surface never depends on the left surface's render pass.
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`.
24
30
  */
25
31
  export const BlockControlsRightSurfaces = ({
26
32
  api,
@@ -40,6 +46,7 @@ export const BlockControlsRightSurfaces = ({
40
46
  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;
41
47
  const targets = useMemo(() => getBlockControlsSurfaceTargets(activeNode, surfaceNodePositions), [activeNode, surfaceNodePositions]);
42
48
  const surfaces = useMemo(() => targets.flatMap(target => {
49
+ var _api$core;
43
50
  const context = target.source === 'stored' ? createBlockControlsSurfaceContextForPosition(editorView.state, target.position, activeNode) : createBlockControlsSurfaceContext(editorView, activeNode, target.source);
44
51
  if (!context) {
45
52
  return [];
@@ -48,21 +55,58 @@ export const BlockControlsRightSurfaces = ({
48
55
  if (!hasSurfaceControls(components) || !willSurfaceRender(components, BLOCK_CONTROLS_RIGHT_SURFACE, surfaceContext)) {
49
56
  return [];
50
57
  }
58
+ // Resolved here rather than in the measurement effect so an anchored surface is placed
59
+ // in the same render that creates it — it never needs a second pass to find its node.
60
+ //
61
+ // At the moment, we only expect top-level nodes to have persistent controls.
62
+ // See surfaceAnchorStyles in global-styles
63
+ const anchorName = isCSSAnchorSupported() && context.targetNode.parentType === 'doc' && !hasInnerContentContainer(context.targetNode.type.name) ? (_api$core = api.core) === null || _api$core === void 0 ? void 0 : _api$core.actions.getAnchorIdForNode(context.targetNode.node, context.targetNode.pos) : undefined;
51
64
  return [{
52
65
  ...target,
66
+ anchorName,
53
67
  blockControlsContext: context,
54
68
  surfaceContext
55
69
  }];
56
- }), [activeNode, components, editorView, targets]);
70
+ }), [activeNode, api, components, editorView, targets]);
71
+ /**
72
+ * Placements that need no measuring, so they are derived rather than stored: every inset is an
73
+ * `anchor()` the browser re-resolves on every layout change.
74
+ */
75
+ const anchoredPlacements = useMemo(() => new Map(surfaces.flatMap(({
76
+ anchorName,
77
+ blockControlsContext,
78
+ position
79
+ }) => anchorName ? [[position, getAnchoredSurfacePlacement({
80
+ anchorName,
81
+ nodeType: blockControlsContext.targetNode.type.name,
82
+ nodeTypeWithLevel: getNodeTypeWithLevel(blockControlsContext.targetNode.node),
83
+ parentNodeType: blockControlsContext.targetNode.parentType === 'doc' ? undefined : blockControlsContext.targetNode.parentType,
84
+ side: 'right'
85
+ })]] : [])), [surfaces]);
86
+ // Only the surfaces that still need measuring drive the effect below, so a document of anchored
87
+ // blocks binds no scroll or resize listeners at all.
88
+ //
89
+ // The shared empty array matters: `surfaces` gets a new identity on every hover, since
90
+ // `activeNode` is one of its dependencies. Returning a fresh `[]` would give the effect a
91
+ // changed dependency each time and re-run it for nothing, which is the cost the anchored path
92
+ // exists to avoid.
93
+ const measuredSurfaces = useMemo(() => {
94
+ const measured = surfaces.filter(({
95
+ anchorName
96
+ }) => !anchorName);
97
+ return measured.length === 0 ? EMPTY_MEASURED_SURFACES : measured;
98
+ }, [surfaces]);
57
99
  const [placements, setPlacements] = useState(new Map());
58
100
  const animationFrameRef = useRef(undefined);
59
101
  useLayoutEffect(() => {
60
102
  var _getDocument;
61
- if (surfaces.length === 0) {
62
- setPlacements(new Map());
103
+ if (measuredSurfaces.length === 0) {
104
+ // Clear only when there is something to clear. Handing React a fresh Map every time would
105
+ // fail its identity check and commit a render to replace one empty map with another.
106
+ setPlacements(previous => previous.size > 0 ? new Map() : previous);
63
107
  return;
64
108
  }
65
- const measurementTargets = surfaces.flatMap(({
109
+ const measurementTargets = measuredSurfaces.flatMap(({
66
110
  blockControlsContext,
67
111
  position
68
112
  }) => {
@@ -106,12 +150,12 @@ export const BlockControlsRightSurfaces = ({
106
150
  parentNodeType: blockControlsContext.targetNode.parentType === 'doc' ? undefined : blockControlsContext.targetNode.parentType,
107
151
  side: 'right'
108
152
  });
109
- nextPlacements.set(position, getAbsoluteSurfacePlacement({
153
+ nextPlacements.set(position, toMeasuredSurfaceWrapperPlacement(getAbsoluteSurfacePlacement({
110
154
  offsetParentRect,
111
155
  offsetParentScrollLeft,
112
156
  offsetParentScrollTop,
113
157
  placement
114
- }));
158
+ })));
115
159
  }
116
160
  setPlacements(nextPlacements);
117
161
  };
@@ -172,19 +216,19 @@ export const BlockControlsRightSurfaces = ({
172
216
  unbindVisualViewportScroll === null || unbindVisualViewportScroll === void 0 ? void 0 : unbindVisualViewportScroll();
173
217
  unbindTransitionEnd();
174
218
  };
175
- }, [editorView, surfaces]);
219
+ }, [editorView, measuredSurfaces]);
176
220
  return /*#__PURE__*/React.createElement(React.Fragment, null, surfaces.map(({
177
221
  position,
178
222
  source,
179
223
  surfaceContext
180
224
  }) => {
181
- var _activeNode$handleOpt;
225
+ var _activeNode$handleOpt, _anchoredPlacements$g;
182
226
  return /*#__PURE__*/React.createElement(BlockControlsRightSurface, {
183
227
  api: api,
184
228
  components: components,
185
229
  forceVisibleOnMouseOut: Boolean(activeNode === null || activeNode === void 0 ? void 0 : (_activeNode$handleOpt = activeNode.handleOptions) === null || _activeNode$handleOpt === void 0 ? void 0 : _activeNode$handleOpt.isFocused),
186
230
  key: position,
187
- placement: placements.get(position),
231
+ placement: (_anchoredPlacements$g = anchoredPlacements.get(position)) !== null && _anchoredPlacements$g !== void 0 ? _anchoredPlacements$g : placements.get(position),
188
232
  source: source,
189
233
  surfaceContext: surfaceContext
190
234
  });
@@ -9,9 +9,10 @@ import { ANCHOR_VARIABLE_NAME, DRAG_HANDLE_WIDTH, isCSSAnchorSupported } from '@
9
9
  import { areToolbarFlagsEnabled } from '@atlaskit/editor-common/toolbar-flag-check';
10
10
  import { ZERO_WIDTH_SPACE } from '@atlaskit/editor-common/whitespace';
11
11
  import { akEditorBreakoutPadding, akEditorCalculatedWideLayoutWidth, akEditorCalculatedWideLayoutWidthSmallViewport, akEditorFullPageNarrowBreakout, akEditorGutterPaddingDynamic, akEditorGutterPaddingReduced } from '@atlaskit/editor-shared-styles';
12
+ import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
12
13
  import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
13
14
  import { editorExperiment } from '@atlaskit/tmp-editor-statsig/editor-experiment';
14
- import { ACTIVE_DRAG_HANDLE_FALLBACK_ANCHOR_NAME, ACTIVE_QUICK_INSERT_FALLBACK_ANCHOR_NAME, DRAG_HANDLE_MAX_WIDTH_PLUS_GAP } from './consts';
15
+ import { ACTIVE_DRAG_HANDLE_ATTR, ACTIVE_DRAG_HANDLE_FALLBACK_ANCHOR_NAME, ACTIVE_QUICK_INSERT_ATTR, ACTIVE_QUICK_INSERT_FALLBACK_ANCHOR_NAME, DRAG_HANDLE_MAX_WIDTH_PLUS_GAP } from './consts';
15
16
  import { NODE_ANCHOR_ATTR_NAME } from './utils/dom-attr-name';
16
17
 
17
18
  /**
@@ -462,6 +463,21 @@ const staticControlsAnchorStyles = css({
462
463
  }
463
464
  });
464
465
 
466
+ /**
467
+ * Names the CSS anchor on top-level nodes, so the block controls surfaces can anchor to the blocks
468
+ * they render against. Only top-level at the moment as we don't have use cases yet for persistent controls that are nested.
469
+ */
470
+ const surfaceAnchorStyles = css({
471
+ // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors
472
+ '.ProseMirror': {
473
+ // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors, @atlaskit/ui-styling-standard/no-unsafe-selectors, @atlaskit/ui-styling-standard/no-unsafe-values, @atlaskit/ui-styling-standard/no-imported-style-values
474
+ [`[${NODE_ANCHOR_ATTR_NAME}]:not([${NODE_ANCHOR_ATTR_NAME}] [${NODE_ANCHOR_ATTR_NAME}]):not([${ACTIVE_DRAG_HANDLE_ATTR}], [${ACTIVE_QUICK_INSERT_ATTR}])`]: {
475
+ // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values, @atlaskit/ui-styling-standard/no-imported-style-values
476
+ anchorName: `var(${ANCHOR_VARIABLE_NAME}, attr(data-node-anchor type(<custom-ident>)))`
477
+ }
478
+ }
479
+ });
480
+
465
481
  // Styles applied to nodes with anchors when dragging
466
482
  const dragAnchorStyles = css({
467
483
  // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors
@@ -503,6 +519,16 @@ export const GlobalStylesWrapper = ({
503
519
  });
504
520
  const isDragging = expValEquals('platform_editor_block_controls_perf_optimization', 'isEnabled', true) ? isDraggingFromState : false;
505
521
  const shouldRenderAnchors = isCSSAnchorSupported() && expValEquals('platform_editor_native_anchor_with_dnd', 'isEnabled', true);
522
+
523
+ // The surfaces anchor to blocks they render against rather than only to the hovered one, so they
524
+ // need a name on every node. Added alongside whichever conditional rule applies rather than
525
+ // replacing it: this one matches only non-active nodes, so the two sets are disjoint.
526
+ //
527
+ // The migration check is what scopes this. Unlike the surfaces, this component renders for every
528
+ // editor, so without it the rule would name anchors on documents that have no surface to anchor.
529
+ const shouldRenderSurfaceAnchors =
530
+ // eslint-disable-next-line @atlaskit/platform/no-preconditioning -- browser capability, not a flag: an anchor name is meaningless where `anchor()` cannot resolve
531
+ Boolean(isCSSAnchorSupported()) && isExperimentEnabled('platform_editor_block_control_migration');
506
532
  const toolbarFlagsEnabled = areToolbarFlagsEnabled(Boolean(api === null || api === void 0 ? void 0 : api.toolbar));
507
533
  return jsx(Global
508
534
  // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
@@ -515,6 +541,9 @@ export const GlobalStylesWrapper = ({
515
541
  // dragAnchorStyles sets anchor-name on ALL nodes during drag (needed for drop target positioning).
516
542
  // staticControlsAnchorStyles handles anchor-name via node decorations for non-drag state.
517
543
  // shouldRenderAnchors guards both: only apply anchor styles on browsers that support CSS anchor positioning.
518
- shouldRenderAnchors ? isDragging ? dragAnchorStyles : staticControlsAnchorStyles : false : shouldRenderAnchors && (isDragging ? dragAnchorStyles : dragHandlerAnchorStyles)]
544
+ shouldRenderAnchors ? isDragging ? dragAnchorStyles : staticControlsAnchorStyles : false : shouldRenderAnchors && (isDragging ? dragAnchorStyles : dragHandlerAnchorStyles),
545
+ // Last so that, at equal specificity, the surfaces' own anchor names are the ones that
546
+ // stand. It cannot overlap the active-node rules above, which this selector excludes.
547
+ shouldRenderSurfaceAnchors && surfaceAnchorStyles]
519
548
  });
520
549
  };
@@ -8,6 +8,7 @@ const INNER_CONTAINER_SELECTORS = {
8
8
  multiBodiedExtension: '.extension-container[data-layout]',
9
9
  table: `.${resizerItemClassName}`
10
10
  };
11
+ export const hasInnerContentContainer = nodeType => INNER_CONTAINER_SELECTORS[nodeType] !== undefined;
11
12
  export const getNodeContentElement = (nodeElement, nodeType) => {
12
13
  var _nodeElement$querySel;
13
14
  const selector = INNER_CONTAINER_SELECTORS[nodeType];
@@ -54,4 +54,71 @@ export const getSurfacePlacement = ({
54
54
  transform: `translateX(calc(-100% - ${gap}px))`,
55
55
  height
56
56
  };
57
+ };
58
+
59
+ /**
60
+ * The insets to apply to a surface wrapper, plus whether that wrapper spans its target node.
61
+ *
62
+ * Two kinds of placement produce this. A measured one resolves to pixel numbers from a
63
+ * `getBoundingClientRect` snapshot, so it is only as fresh as the last render and needs observers
64
+ * to keep it honest. An anchored one resolves to `anchor()` functions that the browser re-evaluates
65
+ * on every layout change, so it stays attached to its node with nothing re-rendering.
66
+ *
67
+ * `isSticky` is carried alongside the style rather than read back out of it because the two variants
68
+ * express the same stretch differently — a measured `height` versus an anchored `bottom`.
69
+ */
70
+
71
+ /**
72
+ * Adapts a measured placement to the shape the surface components consume. Sticky-eligible node
73
+ * types are the ones `getSurfacePlacement` gave a `height` to.
74
+ */
75
+ export const toMeasuredSurfaceWrapperPlacement = placement => ({
76
+ isSticky: placement.height !== undefined,
77
+ style: {
78
+ height: placement.height,
79
+ left: placement.left,
80
+ top: placement.top,
81
+ transform: placement.transform
82
+ }
83
+ });
84
+ export const getAnchoredSurfacePlacement = ({
85
+ anchorName,
86
+ nodeType,
87
+ nodeTypeWithLevel,
88
+ parentNodeType,
89
+ side
90
+ }) => {
91
+ if (nodeType === 'layoutColumn') {
92
+ // The column's horizontal midpoint, matching the measured `nodeRect.left + width / 2` and the
93
+ // legacy drag handle's own layout-column formula. Side-independent.
94
+ return {
95
+ isSticky: false,
96
+ style: {
97
+ left: `calc((anchor(${anchorName} left) + anchor(${anchorName} right)) / 2)`,
98
+ positionAnchor: anchorName,
99
+ top: `anchor(${anchorName} top)`,
100
+ transform: 'translate(-50%, -50%)'
101
+ }
102
+ };
103
+ }
104
+ const gap = dragHandleGap(nodeType, parentNodeType);
105
+ const isSticky = shouldBeSticky(nodeType);
106
+ return {
107
+ isSticky,
108
+ style: {
109
+ // A sticky wrapper is stretched with `bottom` instead of a measured `height`. Pairing it
110
+ // with the adjusted `top` keeps the box ending exactly where the node does, however far
111
+ // the top adjustment pushed the start down and however much the node later grows.
112
+ ...(isSticky ? {
113
+ bottom: `anchor(${anchorName} bottom)`
114
+ } : {}),
115
+ left: side === 'right' ? `calc(anchor(${anchorName} right) + ${gap}px)` : `anchor(${anchorName} left)`,
116
+ positionAnchor: anchorName,
117
+ top: `calc(anchor(${anchorName} top) + ${topPositionAdjustment(nodeTypeWithLevel)}px)`,
118
+ // The left side grows away from the node without knowing its own width, so it pulls itself
119
+ // back past its own box and the gap. The right side grows into the margin, so the gap is
120
+ // already folded into `left` above.
121
+ transform: side === 'right' ? '' : `translateX(calc(-100% - ${gap}px))`
122
+ }
123
+ };
57
124
  };
@@ -1,4 +1,5 @@
1
1
  ._zulpv77o{gap:var(--ds-space-025,2px)}._154iu2gc{top:var(--ds-space-100,8px)}
2
+ ._17c61i8t{position-visibility:anchors-valid}
2
3
  ._1e0c1txw{display:flex}
3
4
  ._1pbyb4wl{z-index:100}
4
5
  ._4cvr1h6o{align-items:center}
@@ -33,15 +33,15 @@ export var BlockControlsLeftSurface = function BlockControlsLeftSurface(_ref) {
33
33
  persistentComponents = _partitionComponentsB.persistentComponents;
34
34
  var willRenderPersistent = willSurfaceRender(persistentComponents, BLOCK_CONTROLS_LEFT_SURFACE, surfaceContext);
35
35
  var willRenderHoverOnly = willSurfaceRender(hoverOnlyComponents, BLOCK_CONTROLS_LEFT_SURFACE, surfaceContext);
36
- var isSticky = placement.height !== undefined;
36
+ var isSticky = placement.isSticky;
37
37
  return /*#__PURE__*/React.createElement("div", {
38
38
  "data-editor-block-controls-surface": true,
39
39
  "data-editor-block-controls-side": "left",
40
40
  "data-testid": source === 'stored' ? 'block-controls-left-surface-stored' : source === 'active' ? 'block-controls-left-surface-active' : 'block-controls-left-surface'
41
- // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Placement is resolved from the target node's runtime DOM geometry.
41
+ // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Placement is resolved per target node, from its runtime DOM geometry or from its CSS anchor.
42
42
  ,
43
- style: placement,
44
- className: ax(["_kqswstnw _1pbyb4wl"])
43
+ style: placement.style,
44
+ className: ax(["_kqswstnw _17c61i8t _1pbyb4wl"])
45
45
  }, /*#__PURE__*/React.createElement("div", {
46
46
  className: ax(["_zulpv77o _4cvr1h6o _1e0c1txw", isSticky && "_kqsw1if8 _154iu2gc"])
47
47
  }, willRenderHoverOnly && /*#__PURE__*/React.createElement(VisibilityContainer, {
@@ -13,17 +13,19 @@ import { getDocument } from '@atlaskit/browser-apis';
13
13
  import { BLOCK_CONTROL_UI_CONTEXT } from '@atlaskit/editor-common/block-controls/block-control-ui-context';
14
14
  import { BLOCK_CONTROLS_LEFT_SURFACE } from '@atlaskit/editor-common/block-controls/surface-keys';
15
15
  import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks';
16
+ import { isCSSAnchorSupported } from '@atlaskit/editor-common/styles';
16
17
  import { createSurfaceContext } from '@atlaskit/editor-ui-control-model/create-surface-context';
17
18
  import { willSurfaceRender } from '@atlaskit/editor-ui-control-model/surface-renderer';
18
19
  import { getNodeTypeWithLevel } from '../pm-plugins/decorations-common';
19
20
  import { BlockControlsLeftSurface } from './block-controls-left-surface';
20
21
  import { createBlockControlsSurfaceContext, createBlockControlsSurfaceContextForPosition } from './block-controls-surface-context';
21
22
  import { getBlockControlsSurfaceTargets } from './block-controls-surface-targets';
22
- import { getNodeContentElement } from './utils/get-node-content-element';
23
- import { getAbsoluteSurfacePlacement, getSurfacePlacement } from './utils/get-surface-placement';
23
+ import { getNodeContentElement, hasInnerContentContainer } from './utils/get-node-content-element';
24
+ import { getAbsoluteSurfacePlacement, getAnchoredSurfacePlacement, getSurfacePlacement, toMeasuredSurfaceWrapperPlacement } from './utils/get-surface-placement';
24
25
  import { hasSurfaceControls } from './utils/has-surface-controls';
25
26
  var EMPTY_SURFACE_POSITIONS = [];
26
27
  var EMPTY_SURFACE_COMPONENTS = [];
28
+ var EMPTY_MEASURED_SURFACES = [];
27
29
  export var BlockControlsLeftSurfaces = function BlockControlsLeftSurfaces(_ref) {
28
30
  var _api$uiControlRegistr, _api$uiControlRegistr2;
29
31
  var api = _ref.api,
@@ -43,6 +45,7 @@ export var BlockControlsLeftSurfaces = function BlockControlsLeftSurfaces(_ref)
43
45
  }, [activeNode, surfaceNodePositions]);
44
46
  var surfaces = useMemo(function () {
45
47
  return targets.flatMap(function (target) {
48
+ var _api$core;
46
49
  var context = target.source === 'stored' ? createBlockControlsSurfaceContextForPosition(editorView.state, target.position, activeNode) : createBlockControlsSurfaceContext(editorView, activeNode, target.source);
47
50
  if (!context) {
48
51
  return [];
@@ -51,12 +54,51 @@ export var BlockControlsLeftSurfaces = function BlockControlsLeftSurfaces(_ref)
51
54
  if (!hasSurfaceControls(components) || !willSurfaceRender(components, BLOCK_CONTROLS_LEFT_SURFACE, surfaceContext)) {
52
55
  return [];
53
56
  }
57
+ // Resolved here rather than in the measurement effect so an anchored surface is placed
58
+ // in the same render that creates it — it never needs a second pass to find its node.
59
+ //
60
+ // At the moment, we only expect top-level nodes to have persistent controls.
61
+ // See surfaceAnchorStyles in global-styles
62
+ var anchorName = isCSSAnchorSupported() && context.targetNode.parentType === 'doc' && !hasInnerContentContainer(context.targetNode.type.name) ? (_api$core = api.core) === null || _api$core === void 0 ? void 0 : _api$core.actions.getAnchorIdForNode(context.targetNode.node, context.targetNode.pos) : undefined;
54
63
  return [_objectSpread(_objectSpread({}, target), {}, {
64
+ anchorName: anchorName,
55
65
  blockControlsContext: context,
56
66
  surfaceContext: surfaceContext
57
67
  })];
58
68
  });
59
- }, [activeNode, components, editorView, targets]);
69
+ }, [activeNode, api, components, editorView, targets]);
70
+ /**
71
+ * Placements that need no measuring, so they are derived rather than stored: every inset is an
72
+ * `anchor()` the browser re-resolves on every layout change.
73
+ */
74
+ var anchoredPlacements = useMemo(function () {
75
+ return new Map(surfaces.flatMap(function (_ref2) {
76
+ var anchorName = _ref2.anchorName,
77
+ blockControlsContext = _ref2.blockControlsContext,
78
+ position = _ref2.position;
79
+ return anchorName ? [[position, getAnchoredSurfacePlacement({
80
+ anchorName: anchorName,
81
+ nodeType: blockControlsContext.targetNode.type.name,
82
+ nodeTypeWithLevel: getNodeTypeWithLevel(blockControlsContext.targetNode.node),
83
+ parentNodeType: blockControlsContext.targetNode.parentType === 'doc' ? undefined : blockControlsContext.targetNode.parentType,
84
+ side: 'left'
85
+ })]] : [];
86
+ }));
87
+ }, [surfaces]);
88
+ // Only the surfaces that still need measuring drive the effect below, so a document of anchored
89
+ // blocks binds no scroll or resize listeners at all.
90
+ //
91
+ // The shared empty array matters: `surfaces` gets a new identity on every hover, since
92
+ // `activeNode` is one of its dependencies. Returning a fresh `[]` would give the effect a
93
+ // changed dependency each time and re-run it for nothing, which is the cost the anchored path
94
+ // exists to avoid.
95
+ var measuredSurfaces = useMemo(function () {
96
+ var measured = surfaces.filter(function (_ref3) {
97
+ var anchorName = _ref3.anchorName;
98
+ return !anchorName;
99
+ });
100
+ return measured.length === 0 ? EMPTY_MEASURED_SURFACES : measured;
101
+ }, [surfaces]);
60
102
  var _useState = useState(new Map()),
61
103
  _useState2 = _slicedToArray(_useState, 2),
62
104
  placements = _useState2[0],
@@ -64,13 +106,17 @@ export var BlockControlsLeftSurfaces = function BlockControlsLeftSurfaces(_ref)
64
106
  var animationFrameRef = useRef(undefined);
65
107
  useLayoutEffect(function () {
66
108
  var _getDocument;
67
- if (surfaces.length === 0) {
68
- setPlacements(new Map());
109
+ if (measuredSurfaces.length === 0) {
110
+ // Clear only when there is something to clear. Handing React a fresh Map every time would
111
+ // fail its identity check and commit a render to replace one empty map with another.
112
+ setPlacements(function (previous) {
113
+ return previous.size > 0 ? new Map() : previous;
114
+ });
69
115
  return;
70
116
  }
71
- var measurementTargets = surfaces.flatMap(function (_ref2) {
72
- var blockControlsContext = _ref2.blockControlsContext,
73
- position = _ref2.position;
117
+ var measurementTargets = measuredSurfaces.flatMap(function (_ref4) {
118
+ var blockControlsContext = _ref4.blockControlsContext,
119
+ position = _ref4.position;
74
120
  var targetNodeElement = editorView.nodeDOM(blockControlsContext.targetNode.pos);
75
121
  var targetElementRoot = targetNodeElement instanceof HTMLElement ? targetNodeElement : targetNodeElement === null || targetNodeElement === void 0 ? void 0 : targetNodeElement.parentElement;
76
122
  if (!targetElementRoot) {
@@ -114,12 +160,12 @@ export var BlockControlsLeftSurfaces = function BlockControlsLeftSurfaces(_ref)
114
160
  parentNodeType: blockControlsContext.targetNode.parentType === 'doc' ? undefined : blockControlsContext.targetNode.parentType,
115
161
  side: 'left'
116
162
  });
117
- nextPlacements.set(position, getAbsoluteSurfacePlacement({
163
+ nextPlacements.set(position, toMeasuredSurfaceWrapperPlacement(getAbsoluteSurfacePlacement({
118
164
  offsetParentRect: offsetParentRect,
119
165
  offsetParentScrollLeft: offsetParentScrollLeft,
120
166
  offsetParentScrollTop: offsetParentScrollTop,
121
167
  placement: placement
122
- }));
168
+ })));
123
169
  }
124
170
  } catch (err) {
125
171
  _iterator.e(err);
@@ -194,18 +240,18 @@ export var BlockControlsLeftSurfaces = function BlockControlsLeftSurfaces(_ref)
194
240
  unbindVisualViewportScroll === null || unbindVisualViewportScroll === void 0 || unbindVisualViewportScroll();
195
241
  unbindTransitionEnd();
196
242
  };
197
- }, [editorView, surfaces]);
198
- return /*#__PURE__*/React.createElement(React.Fragment, null, surfaces.map(function (_ref3) {
199
- var _activeNode$handleOpt;
200
- var position = _ref3.position,
201
- source = _ref3.source,
202
- surfaceContext = _ref3.surfaceContext;
243
+ }, [editorView, measuredSurfaces]);
244
+ return /*#__PURE__*/React.createElement(React.Fragment, null, surfaces.map(function (_ref5) {
245
+ var _activeNode$handleOpt, _anchoredPlacements$g;
246
+ var position = _ref5.position,
247
+ source = _ref5.source,
248
+ surfaceContext = _ref5.surfaceContext;
203
249
  return /*#__PURE__*/React.createElement(BlockControlsLeftSurface, {
204
250
  api: api,
205
251
  components: components,
206
252
  forceVisibleOnMouseOut: Boolean(activeNode === null || activeNode === void 0 || (_activeNode$handleOpt = activeNode.handleOptions) === null || _activeNode$handleOpt === void 0 ? void 0 : _activeNode$handleOpt.isFocused),
207
253
  key: position,
208
- placement: placements.get(position),
254
+ placement: (_anchoredPlacements$g = anchoredPlacements.get(position)) !== null && _anchoredPlacements$g !== void 0 ? _anchoredPlacements$g : placements.get(position),
209
255
  source: source,
210
256
  surfaceContext: surfaceContext
211
257
  });
@@ -1,4 +1,5 @@
1
1
  ._zulpv77o{gap:var(--ds-space-025,2px)}._154iu2gc{top:var(--ds-space-100,8px)}
2
+ ._17c61i8t{position-visibility:anchors-valid}
2
3
  ._1e0c1txw{display:flex}
3
4
  ._1pbyb4wl{z-index:100}
4
5
  ._4cvr1h6o{align-items:center}