@octanejs/lexical 0.1.57 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -10
- package/package.json +33 -24
- package/src/LexicalAutoEmbedPlugin.tsrx +30 -17
- package/src/{autoEmbedShared.ts → LexicalAutoEmbedPluginUtils.ts} +4 -4
- package/src/LexicalCollaborationContext.ts +7 -66
- package/src/LexicalCollaborationContextUtils.ts +76 -0
- package/src/LexicalDecoratorBlockNode.ts +41 -23
- package/src/LexicalDraggableBlockPlugin.tsrx +37 -39
- package/src/LexicalHorizontalRuleNode.tsrx +13 -21
- package/src/LexicalMarkdownShortcutPlugin.tsrx +12 -1
- package/src/LexicalMenuOption.ts +26 -0
- package/src/LexicalNodeContextMenuPlugin.tsrx +20 -14
- package/src/LexicalNodeEventPlugin.tsrx +2 -3
- package/src/LexicalNodeMenuPlugin.tsrx +2 -1
- package/src/LexicalTypeaheadMenuPlugin.tsrx +10 -9
- package/src/LexicalTypeaheadMenuPluginUtils.ts +78 -0
- package/src/index.ts +19 -4
- package/src/shared/LexicalContentEditableElement.tsrx +1 -1
- package/src/shared/LexicalDecorators.tsrx +23 -35
- package/src/shared/LexicalMenu.tsrx +63 -45
- package/src/shared/LexicalReactGeneratedJSON.ts +78 -0
- package/src/shared/menuShared.ts +75 -24
- package/src/shared/useCharacterLimit.ts +48 -23
- package/src/shared/useDynamicPositioning.ts +25 -18
- package/src/shared/useMenuAnchorRef.ts +28 -12
- package/src/typeaheadShared.ts +2 -55
- package/src/useLexicalAriaLiveRegion.ts +25 -0
- package/src/useLexicalFocusManagerRef.ts +40 -0
- package/src/useLexicalFocusTrapRef.ts +65 -0
- package/src/useLexicalIsTextContentEmpty.ts +5 -1
- package/src/useLexicalNodeSelection.ts +11 -0
- package/src/useLexicalRovingTabIndexRef.ts +41 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Copied from @lexical/react/src/shared/LexicalReactGeneratedJSON.ts (0.51.0, MIT).
|
|
2
|
+
// Upstream generates this from DecoratorBlockNode's `json` schema; it is copied
|
|
3
|
+
// unchanged apart from repository formatting, and must be re-copied rather than
|
|
4
|
+
// hand-edited when that schema changes.
|
|
5
|
+
|
|
6
|
+
// Generated upstream by scripts/generate-node-json.mjs from each class's `json`
|
|
7
|
+
// schema.
|
|
8
|
+
|
|
9
|
+
// Keys are emitted in the order the schema-driven walk writes them, so the two
|
|
10
|
+
// produce byte-identical JSON. That order is the schema's, not alphabetical.
|
|
11
|
+
|
|
12
|
+
import type { DecoratorBlockNode } from '../LexicalDecoratorBlockNode';
|
|
13
|
+
import type { GeneratedJSONFactory } from 'lexical';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* DecoratorBlockNode's schema-declared fields, for a clone. Generated from that
|
|
17
|
+
* schema; do not edit by hand.
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export function afterCloneDecoratorBlockNode(
|
|
22
|
+
node: DecoratorBlockNode,
|
|
23
|
+
prevNode: DecoratorBlockNode,
|
|
24
|
+
): void {
|
|
25
|
+
node.__format = prevNode.__format;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** DecoratorBlockNode's generated implementations, for its `$config`. @internal */
|
|
29
|
+
export const GENERATED_DECORATORBLOCK: GeneratedJSONFactory = () => {
|
|
30
|
+
/** Generated from DecoratorBlockNode's serialization schema. Do not edit by hand. */
|
|
31
|
+
function exportDecoratorBlockNode(node: DecoratorBlockNode): {
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
} {
|
|
34
|
+
return {
|
|
35
|
+
format: node.__format,
|
|
36
|
+
type: node.__type,
|
|
37
|
+
version: 1,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Generated from DecoratorBlockNode's serialization schema. Do not edit by hand. */
|
|
42
|
+
function exportCompactDecoratorBlockNode(node: DecoratorBlockNode): {
|
|
43
|
+
[key: string]: unknown;
|
|
44
|
+
} {
|
|
45
|
+
const json: { [key: string]: unknown } = { type: node.__type };
|
|
46
|
+
const format = node.__format;
|
|
47
|
+
if (format !== undefined && format !== '') {
|
|
48
|
+
json.format = format;
|
|
49
|
+
}
|
|
50
|
+
return json;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Generated from DecoratorBlockNode's serialization schema. Do not edit by hand. */
|
|
54
|
+
function updateDecoratorBlockNode(
|
|
55
|
+
node: DecoratorBlockNode,
|
|
56
|
+
json: { readonly [key: string]: unknown },
|
|
57
|
+
): DecoratorBlockNode {
|
|
58
|
+
const format = json.format;
|
|
59
|
+
node.__format =
|
|
60
|
+
format === '' ||
|
|
61
|
+
format === 'left' ||
|
|
62
|
+
format === 'start' ||
|
|
63
|
+
format === 'center' ||
|
|
64
|
+
format === 'right' ||
|
|
65
|
+
format === 'end' ||
|
|
66
|
+
format === 'justify'
|
|
67
|
+
? format
|
|
68
|
+
: '';
|
|
69
|
+
return node;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
exportJSON: exportDecoratorBlockNode,
|
|
74
|
+
exportCompactJSON: exportCompactDecoratorBlockNode,
|
|
75
|
+
updateFromJSON: updateDecoratorBlockNode,
|
|
76
|
+
afterCloneFrom: afterCloneDecoratorBlockNode,
|
|
77
|
+
};
|
|
78
|
+
};
|
package/src/shared/menuShared.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { LexicalEditor, TextNode } from 'lexical';
|
|
2
|
+
import { MenuOption } from '../LexicalMenuOption';
|
|
3
|
+
import { SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND } from '../LexicalTypeaheadMenuPluginUtils';
|
|
2
4
|
import {
|
|
3
5
|
$getSelection,
|
|
4
6
|
$isRangeSelection,
|
|
5
7
|
CAN_USE_DOM,
|
|
6
|
-
|
|
8
|
+
getParentElement,
|
|
7
9
|
isDOMShadowRoot,
|
|
10
|
+
isHTMLElement,
|
|
8
11
|
} from 'lexical';
|
|
9
12
|
|
|
10
13
|
// Non-hook, non-JSX shared pieces of @lexical/react/src/shared/LexicalMenu.tsx
|
|
@@ -37,35 +40,18 @@ export type MenuRenderFn<TOption extends MenuOption> = (
|
|
|
37
40
|
|
|
38
41
|
export type TriggerFn = (text: string, editor: LexicalEditor) => MenuTextMatch | null;
|
|
39
42
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
icon?: unknown;
|
|
44
|
-
title?: unknown;
|
|
45
|
-
|
|
46
|
-
constructor(key: string) {
|
|
47
|
-
this.key = key;
|
|
48
|
-
this.ref = { current: null };
|
|
49
|
-
this.setRefElement = this.setRefElement.bind(this);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
setRefElement(element: HTMLElement | null) {
|
|
53
|
-
this.ref = { current: element };
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export const SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND: LexicalCommand<{
|
|
58
|
-
index: number;
|
|
59
|
-
option: MenuOption;
|
|
60
|
-
}> = createCommand('SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND');
|
|
43
|
+
// Defined in their own entry points (as upstream) so the menu modules and
|
|
44
|
+
// consumers share one class/command identity; re-exported for internal importers.
|
|
45
|
+
export { MenuOption, SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND };
|
|
61
46
|
|
|
62
47
|
export const scrollIntoViewIfNeeded = (target: HTMLElement) => {
|
|
63
48
|
const typeaheadContainerNode = target.closest('#typeahead-menu') as HTMLElement | null;
|
|
64
49
|
if (!typeaheadContainerNode) {
|
|
65
50
|
return;
|
|
66
51
|
}
|
|
52
|
+
const ownerWindow = target.ownerDocument.defaultView ?? window;
|
|
67
53
|
const typeaheadRect = typeaheadContainerNode.getBoundingClientRect();
|
|
68
|
-
if (typeaheadRect.top + typeaheadRect.height >
|
|
54
|
+
if (typeaheadRect.top + typeaheadRect.height > ownerWindow.innerHeight) {
|
|
69
55
|
typeaheadContainerNode.scrollIntoView({ block: 'center' });
|
|
70
56
|
}
|
|
71
57
|
if (typeaheadRect.top < 0) {
|
|
@@ -137,6 +123,70 @@ export function setContainerDivAttributes(containerDiv: HTMLElement, className?:
|
|
|
137
123
|
containerDiv.style.position = 'absolute';
|
|
138
124
|
}
|
|
139
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Whether an element establishes the containing block that an absolutely
|
|
128
|
+
* positioned descendant resolves its offsets against. Being positioned is the
|
|
129
|
+
* usual reason, but a transform, filter, containment or a `will-change` naming
|
|
130
|
+
* one of those does it too, on an otherwise statically positioned element.
|
|
131
|
+
*/
|
|
132
|
+
function establishesContainingBlock(style: CSSStyleDeclaration): boolean {
|
|
133
|
+
if (style.position !== 'static') {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
const willChange = style.willChange;
|
|
137
|
+
return (
|
|
138
|
+
style.transform !== 'none' ||
|
|
139
|
+
style.perspective !== 'none' ||
|
|
140
|
+
style.filter !== 'none' ||
|
|
141
|
+
style.backdropFilter !== 'none' ||
|
|
142
|
+
style.contain.includes('paint') ||
|
|
143
|
+
style.contain.includes('layout') ||
|
|
144
|
+
style.contain.includes('strict') ||
|
|
145
|
+
style.contain.includes('content') ||
|
|
146
|
+
willChange.includes('transform') ||
|
|
147
|
+
willChange.includes('perspective') ||
|
|
148
|
+
willChange.includes('filter') ||
|
|
149
|
+
willChange.includes('contain')
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The anchor is absolutely positioned, so its `top`/`left` are resolved against
|
|
155
|
+
* its containing block. That is the initial containing block (document
|
|
156
|
+
* coordinates, hence the page scroll offsets) only while the anchor's ancestors
|
|
157
|
+
* are all statically positioned. Walks up from the element the anchor is
|
|
158
|
+
* appended to rather than from the anchor's `offsetParent`, because the anchor
|
|
159
|
+
* is usually detached when this runs.
|
|
160
|
+
*
|
|
161
|
+
* @returns The viewport coordinates of the origin that the anchor's `top`/
|
|
162
|
+
* `left` are measured from, or `null` when document coordinates apply.
|
|
163
|
+
*/
|
|
164
|
+
export function getContainingBlockOrigin(
|
|
165
|
+
parent: HTMLElement | ShadowRoot,
|
|
166
|
+
): null | { left: number; top: number } {
|
|
167
|
+
// An anchor inside a shadow tree is laid out against the flat tree, so the
|
|
168
|
+
// walk continues at the host.
|
|
169
|
+
const start = isDOMShadowRoot(parent) ? parent.host : parent;
|
|
170
|
+
for (
|
|
171
|
+
let element: HTMLElement | null = isHTMLElement(start) ? start : null;
|
|
172
|
+
element !== null;
|
|
173
|
+
element = getParentElement(element)
|
|
174
|
+
) {
|
|
175
|
+
const view = element.ownerDocument.defaultView;
|
|
176
|
+
if (view === null) {
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
if (establishesContainingBlock(view.getComputedStyle(element))) {
|
|
180
|
+
const rect = element.getBoundingClientRect();
|
|
181
|
+
return {
|
|
182
|
+
left: rect.left + element.clientLeft - element.scrollLeft,
|
|
183
|
+
top: rect.top + element.clientTop - element.scrollTop,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
|
|
140
190
|
export function resolveMenuParent(editor: LexicalEditor): HTMLElement | ShadowRoot | undefined {
|
|
141
191
|
if (!CAN_USE_DOM) {
|
|
142
192
|
return undefined;
|
|
@@ -147,6 +197,7 @@ export function resolveMenuParent(editor: LexicalEditor): HTMLElement | ShadowRo
|
|
|
147
197
|
if (isDOMShadowRoot(root)) {
|
|
148
198
|
return root as ShadowRoot;
|
|
149
199
|
}
|
|
200
|
+
return rootElement.ownerDocument.body;
|
|
150
201
|
}
|
|
151
202
|
return document.body;
|
|
152
203
|
}
|
|
@@ -58,7 +58,40 @@ export function useCharacterLimit(...args: any[]): void {
|
|
|
58
58
|
useEffect(
|
|
59
59
|
() => {
|
|
60
60
|
let text = editor.read('latest', $rootTextContent);
|
|
61
|
-
let lastComputedTextLength =
|
|
61
|
+
let lastComputedTextLength: null | number = null;
|
|
62
|
+
|
|
63
|
+
// Not `$`-prefixed: this runs outside an update/read context (from the
|
|
64
|
+
// effect body and from listener callbacks) and opens its own
|
|
65
|
+
// editor.update() for the OverflowNode wrapping.
|
|
66
|
+
function updateCharacterLimit(): void {
|
|
67
|
+
const textLength = strlen(text);
|
|
68
|
+
const textLengthAboveThreshold =
|
|
69
|
+
textLength > maxCharacters ||
|
|
70
|
+
(lastComputedTextLength !== null && lastComputedTextLength > maxCharacters);
|
|
71
|
+
const diff = maxCharacters - textLength;
|
|
72
|
+
|
|
73
|
+
remainingCharacters(diff);
|
|
74
|
+
|
|
75
|
+
if (lastComputedTextLength === null || textLengthAboveThreshold) {
|
|
76
|
+
const offset = findOffset(text, maxCharacters, strlen);
|
|
77
|
+
editor.update(
|
|
78
|
+
() => {
|
|
79
|
+
$wrapOverflowedNodes(offset);
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
tag: HISTORY_MERGE_TAG,
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
lastComputedTextLength = textLength;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Derive the count from the content that is already there before
|
|
91
|
+
// subscribing. registerUpdateListener does not fire on registration, so
|
|
92
|
+
// otherwise both the reported count and the OverflowNode wrapping stay at
|
|
93
|
+
// their initial values until the next edit.
|
|
94
|
+
updateCharacterLimit();
|
|
62
95
|
|
|
63
96
|
return mergeRegister(
|
|
64
97
|
editor.registerTextContentListener((currentText: string) => {
|
|
@@ -72,27 +105,7 @@ export function useCharacterLimit(...args: any[]): void {
|
|
|
72
105
|
return;
|
|
73
106
|
}
|
|
74
107
|
|
|
75
|
-
|
|
76
|
-
const textLengthAboveThreshold =
|
|
77
|
-
textLength > maxCharacters ||
|
|
78
|
-
(lastComputedTextLength !== null && lastComputedTextLength > maxCharacters);
|
|
79
|
-
const diff = maxCharacters - textLength;
|
|
80
|
-
|
|
81
|
-
remainingCharacters(diff);
|
|
82
|
-
|
|
83
|
-
if (lastComputedTextLength === null || textLengthAboveThreshold) {
|
|
84
|
-
const offset = findOffset(text, maxCharacters, strlen);
|
|
85
|
-
editor.update(
|
|
86
|
-
() => {
|
|
87
|
-
$wrapOverflowedNodes(offset);
|
|
88
|
-
},
|
|
89
|
-
{
|
|
90
|
-
tag: HISTORY_MERGE_TAG,
|
|
91
|
-
},
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
lastComputedTextLength = textLength;
|
|
108
|
+
updateCharacterLimit();
|
|
96
109
|
}),
|
|
97
110
|
editor.registerCommand(
|
|
98
111
|
DELETE_CHARACTER_COMMAND,
|
|
@@ -172,6 +185,14 @@ export function $wrapOverflowedNodes(offset: number): void {
|
|
|
172
185
|
for (let i = 0; i < dfsNodesLength; i += 1) {
|
|
173
186
|
const { node } = dfsNodes[i];
|
|
174
187
|
|
|
188
|
+
// ElementNode.getTextContent() inserts '\n\n' after each non-inline
|
|
189
|
+
// element child that is not the last child. findOffset counts those
|
|
190
|
+
// separators (via $rootTextContent), so the DFS must count them too.
|
|
191
|
+
const prevSibling = node.getPreviousSibling();
|
|
192
|
+
if ($isElementNode(prevSibling) && !prevSibling.isInline()) {
|
|
193
|
+
accumulatedLength += 2;
|
|
194
|
+
}
|
|
195
|
+
|
|
175
196
|
const isSlotValueLeaf = $isLeafNode(node) && $getSlotHost(node) !== null;
|
|
176
197
|
const needsOverflowParent =
|
|
177
198
|
$isLeafNode(node) && !isSlotValueLeaf && !$findMatchingParent(node, $isOverflowNode);
|
|
@@ -232,6 +253,10 @@ export function $wrapOverflowedNodes(offset: number): void {
|
|
|
232
253
|
}
|
|
233
254
|
|
|
234
255
|
$mergePrevious(overflowNode);
|
|
256
|
+
const nextNode = overflowNode.getNextSibling();
|
|
257
|
+
if ($isOverflowNode(nextNode)) {
|
|
258
|
+
$mergePrevious(nextNode);
|
|
259
|
+
}
|
|
235
260
|
}
|
|
236
261
|
}
|
|
237
262
|
}
|
|
@@ -269,7 +294,7 @@ export function $mergePrevious(overflowNode: OverflowNode): void {
|
|
|
269
294
|
const anchor = selection.anchor;
|
|
270
295
|
const anchorNode = anchor.getNode();
|
|
271
296
|
const focus = selection.focus;
|
|
272
|
-
const focusNode =
|
|
297
|
+
const focusNode = focus.getNode();
|
|
273
298
|
|
|
274
299
|
if (anchorNode.is(previousNode)) {
|
|
275
300
|
anchor.set(overflowNode.getKey(), anchor.offset, 'element');
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { MenuResolution } from './menuShared';
|
|
2
2
|
|
|
3
3
|
import { getScrollParent } from '@lexical/utils';
|
|
4
|
-
import { getDOMShadowRoots } from 'lexical';
|
|
4
|
+
import { getDOMShadowRoots, mergeRegister, registerEventListener } from 'lexical';
|
|
5
5
|
import { useEffect } from 'octane';
|
|
6
6
|
|
|
7
7
|
import { useLexicalComposerContext } from '../LexicalComposerContext';
|
|
@@ -21,8 +21,12 @@ export function useDynamicPositioning(
|
|
|
21
21
|
() => {
|
|
22
22
|
if (targetElement != null && resolution != null) {
|
|
23
23
|
const rootElement = editor.getRootElement();
|
|
24
|
+
// Listen in the editor's own browsing context: an editor inside an
|
|
25
|
+
// iframe scrolls and resizes with that frame, not the host window.
|
|
26
|
+
const ownerDocument = rootElement != null ? rootElement.ownerDocument : document;
|
|
27
|
+
const ownerWindow = ownerDocument.defaultView ?? window;
|
|
24
28
|
const rootScrollParent =
|
|
25
|
-
rootElement != null ? getScrollParent(rootElement, false) :
|
|
29
|
+
rootElement != null ? getScrollParent(rootElement, false) : ownerDocument.body;
|
|
26
30
|
let ticking = false;
|
|
27
31
|
let previousIsInView = isTriggerVisibleInNearestScrollContainer(
|
|
28
32
|
targetElement,
|
|
@@ -30,7 +34,7 @@ export function useDynamicPositioning(
|
|
|
30
34
|
);
|
|
31
35
|
const handleScroll = function () {
|
|
32
36
|
if (!ticking) {
|
|
33
|
-
|
|
37
|
+
ownerWindow.requestAnimationFrame(function () {
|
|
34
38
|
onReposition();
|
|
35
39
|
ticking = false;
|
|
36
40
|
});
|
|
@@ -48,22 +52,25 @@ export function useDynamicPositioning(
|
|
|
48
52
|
}
|
|
49
53
|
};
|
|
50
54
|
const resizeObserver = new ResizeObserver(onReposition);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const enclosingShadowRoots = getDOMShadowRoots(
|
|
55
|
-
for (const root of enclosingShadowRoots) {
|
|
56
|
-
root.addEventListener('scroll', handleScroll, { capture: true, passive: true });
|
|
57
|
-
}
|
|
55
|
+
// Scroll events are non-composed and do not cross shadow boundaries, so
|
|
56
|
+
// also listen on the editor root's enclosing shadow roots (keyed off the
|
|
57
|
+
// root rather than the target, which may be portaled into light DOM).
|
|
58
|
+
const enclosingShadowRoots = getDOMShadowRoots(rootElement ?? targetElement);
|
|
58
59
|
resizeObserver.observe(targetElement);
|
|
59
|
-
return (
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
return mergeRegister(
|
|
61
|
+
registerEventListener(ownerWindow, 'resize', onReposition),
|
|
62
|
+
registerEventListener(ownerDocument, 'scroll', handleScroll, {
|
|
63
|
+
capture: true,
|
|
64
|
+
passive: true,
|
|
65
|
+
}),
|
|
66
|
+
...enclosingShadowRoots.map((root) =>
|
|
67
|
+
registerEventListener(root, 'scroll', handleScroll, {
|
|
68
|
+
capture: true,
|
|
69
|
+
passive: true,
|
|
70
|
+
}),
|
|
71
|
+
),
|
|
72
|
+
() => resizeObserver.unobserve(targetElement),
|
|
73
|
+
);
|
|
67
74
|
}
|
|
68
75
|
},
|
|
69
76
|
[targetElement, editor, onVisibilityChange, onReposition, resolution],
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { MenuRef, MenuResolution } from './menuShared';
|
|
2
2
|
|
|
3
|
-
import { CAN_USE_DOM } from 'lexical';
|
|
3
|
+
import { CAN_USE_DOM, getRootOwnerDocument } from 'lexical';
|
|
4
4
|
import { useCallback, useEffect, useRef } from 'octane';
|
|
5
5
|
|
|
6
6
|
import { useLexicalComposerContext } from '../LexicalComposerContext';
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
getContainingBlockOrigin,
|
|
9
|
+
resolveMenuParent,
|
|
10
|
+
setContainerDivAttributes,
|
|
11
|
+
} from './menuShared';
|
|
8
12
|
import { useDynamicPositioning } from './useDynamicPositioning';
|
|
9
13
|
import { splitSlot, subSlot } from './internal';
|
|
10
14
|
|
|
@@ -22,7 +26,9 @@ export function useMenuAnchorRef(...args: any[]): MenuRef {
|
|
|
22
26
|
|
|
23
27
|
const [editor] = useLexicalComposerContext();
|
|
24
28
|
const resolvedParent = parent ?? resolveMenuParent(editor);
|
|
25
|
-
const initialAnchorElement = CAN_USE_DOM
|
|
29
|
+
const initialAnchorElement = CAN_USE_DOM
|
|
30
|
+
? getRootOwnerDocument(editor.getRootElement()).createElement('div')
|
|
31
|
+
: null;
|
|
26
32
|
const anchorElementRef = useRef<HTMLElement | null>(
|
|
27
33
|
initialAnchorElement,
|
|
28
34
|
subSlot(slot, 'uma:ref'),
|
|
@@ -42,10 +48,21 @@ export function useMenuAnchorRef(...args: any[]): MenuRef {
|
|
|
42
48
|
if (rootElement !== null && resolution !== null) {
|
|
43
49
|
const { left, top, width, height } = resolution.getRect();
|
|
44
50
|
const anchorHeight = anchorElementRef.current.offsetHeight;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
// The anchor is created in the editor's owner document, so its page
|
|
52
|
+
// offsets and viewport come from that document's window (an iframe's,
|
|
53
|
+
// in a multi-window setup), not the host window.
|
|
54
|
+
const ownerWindow = rootElement.ownerDocument.defaultView ?? window;
|
|
55
|
+
// `left`/`top` from getRect() are viewport coordinates; translate them
|
|
56
|
+
// into the coordinate space the anchor is actually positioned in.
|
|
57
|
+
const origin = getContainingBlockOrigin(resolvedParent);
|
|
58
|
+
const toAnchorLeft = (viewportLeft: number) =>
|
|
59
|
+
origin !== null ? viewportLeft - origin.left : viewportLeft + ownerWindow.pageXOffset;
|
|
60
|
+
const toAnchorTop = (viewportTop: number) =>
|
|
61
|
+
origin !== null
|
|
62
|
+
? viewportTop - origin.top
|
|
63
|
+
: viewportTop + (shouldIncludePageYOffset ? ownerWindow.pageYOffset : 0);
|
|
64
|
+
containerDiv.style.top = `${toAnchorTop(top + anchorHeight + 3)}px`;
|
|
65
|
+
containerDiv.style.left = `${toAnchorLeft(left)}px`;
|
|
49
66
|
containerDiv.style.height = `${height}px`;
|
|
50
67
|
containerDiv.style.width = `${width}px`;
|
|
51
68
|
if (menuEle !== null) {
|
|
@@ -55,15 +72,14 @@ export function useMenuAnchorRef(...args: any[]): MenuRef {
|
|
|
55
72
|
const menuWidth = menuRect.width;
|
|
56
73
|
const rootElementRect = rootElement.getBoundingClientRect();
|
|
57
74
|
if (left + menuWidth > rootElementRect.right) {
|
|
58
|
-
containerDiv.style.left = `${rootElementRect.right - menuWidth
|
|
75
|
+
containerDiv.style.left = `${toAnchorLeft(rootElementRect.right - menuWidth)}px`;
|
|
59
76
|
}
|
|
60
77
|
if (
|
|
61
|
-
(top + menuHeight >
|
|
78
|
+
(top + menuHeight > ownerWindow.innerHeight ||
|
|
79
|
+
top + menuHeight > rootElementRect.bottom) &&
|
|
62
80
|
top - rootElementRect.top > menuHeight + height
|
|
63
81
|
) {
|
|
64
|
-
containerDiv.style.top = `${
|
|
65
|
-
top - menuHeight - height + (shouldIncludePageYOffset ? window.pageYOffset : 0)
|
|
66
|
-
}px`;
|
|
82
|
+
containerDiv.style.top = `${toAnchorTop(top - menuHeight - height)}px`;
|
|
67
83
|
}
|
|
68
84
|
}
|
|
69
85
|
if (!containerDiv.isConnected) {
|
package/src/typeaheadShared.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { LexicalEditor, RangeSelection } from 'lexical';
|
|
2
|
-
import type { MenuTextMatch, TriggerFn } from './shared/menuShared';
|
|
3
2
|
|
|
4
3
|
import {
|
|
5
4
|
$getSelection,
|
|
@@ -8,11 +7,9 @@ import {
|
|
|
8
7
|
getDOMSelection,
|
|
9
8
|
getDOMSelectionPoints,
|
|
10
9
|
} from 'lexical';
|
|
11
|
-
import { useCallback } from 'octane';
|
|
12
10
|
|
|
13
|
-
// Non-component
|
|
14
|
-
|
|
15
|
-
export const PUNCTUATION = '\\.,\\+\\*\\?\\$\\@\\|#{}\\(\\)\\^\\-\\[\\]\\\\/!%\'"~=<>_:;';
|
|
11
|
+
// Non-component internals of LexicalTypeaheadMenuPlugin.tsx. The public helpers
|
|
12
|
+
// live in LexicalTypeaheadMenuPluginUtils.ts.
|
|
16
13
|
|
|
17
14
|
export function getTextUpToAnchor(selection: RangeSelection): string | null {
|
|
18
15
|
const anchor = selection.anchor;
|
|
@@ -80,53 +77,3 @@ export function isSelectionOnEntityBoundary(editor: LexicalEditor, offset: numbe
|
|
|
80
77
|
return false;
|
|
81
78
|
});
|
|
82
79
|
}
|
|
83
|
-
|
|
84
|
-
// Two required user args (trigger, options), so the trailing slot is positional.
|
|
85
|
-
export function useBasicTypeaheadTriggerMatch(
|
|
86
|
-
trigger: string,
|
|
87
|
-
options: {
|
|
88
|
-
minLength?: number;
|
|
89
|
-
maxLength?: number;
|
|
90
|
-
punctuation?: string;
|
|
91
|
-
allowWhitespace?: boolean;
|
|
92
|
-
},
|
|
93
|
-
slot?: symbol,
|
|
94
|
-
): TriggerFn {
|
|
95
|
-
const minLength = options.minLength ?? 1;
|
|
96
|
-
const maxLength = options.maxLength ?? 75;
|
|
97
|
-
const punctuation = options.punctuation ?? PUNCTUATION;
|
|
98
|
-
const allowWhitespace = options.allowWhitespace ?? false;
|
|
99
|
-
return useCallback(
|
|
100
|
-
(text: string) => {
|
|
101
|
-
const validCharsSuffix = allowWhitespace ? '' : '\\s';
|
|
102
|
-
const validChars = '[^' + trigger + punctuation + validCharsSuffix + ']';
|
|
103
|
-
const TypeaheadTriggerRegex = new RegExp(
|
|
104
|
-
'(^|\\s|\\()(' +
|
|
105
|
-
'[' +
|
|
106
|
-
trigger +
|
|
107
|
-
']' +
|
|
108
|
-
'((?:' +
|
|
109
|
-
validChars +
|
|
110
|
-
'){0,' +
|
|
111
|
-
maxLength +
|
|
112
|
-
'})' +
|
|
113
|
-
')$',
|
|
114
|
-
);
|
|
115
|
-
const match = TypeaheadTriggerRegex.exec(text);
|
|
116
|
-
if (match !== null) {
|
|
117
|
-
const maybeLeadingWhitespace = match[1];
|
|
118
|
-
const matchingString = match[3];
|
|
119
|
-
if (matchingString.length >= minLength) {
|
|
120
|
-
return {
|
|
121
|
-
leadOffset: match.index + maybeLeadingWhitespace.length,
|
|
122
|
-
matchingString,
|
|
123
|
-
replaceableString: match[2],
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return null;
|
|
128
|
-
},
|
|
129
|
-
[allowWhitespace, trigger, punctuation, maxLength, minLength],
|
|
130
|
-
slot,
|
|
131
|
-
);
|
|
132
|
-
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AriaLiveRegionExtension } from '@lexical/a11y';
|
|
2
|
+
import { getExtensionDependencyFromEditor } from '@lexical/extension';
|
|
3
|
+
import { useCallback } from 'octane';
|
|
4
|
+
|
|
5
|
+
import { useLexicalComposerContext } from './LexicalComposerContext';
|
|
6
|
+
|
|
7
|
+
// Ported from @lexical/react/src/useLexicalAriaLiveRegion.ts. Composes exactly one
|
|
8
|
+
// slot-keyed base hook (useCallback), so the caller's slot is forwarded directly.
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Returns a stable `announce` function backed by the editor's
|
|
12
|
+
* {@link AriaLiveRegionExtension} output. Requires `AriaLiveRegionExtension` in
|
|
13
|
+
* the editor's extension tree.
|
|
14
|
+
*/
|
|
15
|
+
export function useLexicalAriaLiveRegion(slot?: symbol): (message: string) => void {
|
|
16
|
+
const [editor] = useLexicalComposerContext();
|
|
17
|
+
return useCallback(
|
|
18
|
+
(message: string) => {
|
|
19
|
+
const dep = getExtensionDependencyFromEditor(editor, AriaLiveRegionExtension);
|
|
20
|
+
dep.output.announce(message);
|
|
21
|
+
},
|
|
22
|
+
[editor],
|
|
23
|
+
slot,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { FocusManagerExtension, type FocusManagerOptions } from '@lexical/a11y';
|
|
2
|
+
import { getExtensionDependencyFromEditor } from '@lexical/extension';
|
|
3
|
+
import { useCallback, useRef } from 'octane';
|
|
4
|
+
|
|
5
|
+
import { useLexicalComposerContext } from './LexicalComposerContext';
|
|
6
|
+
import { splitSlot, subSlot } from './shared/internal';
|
|
7
|
+
|
|
8
|
+
export type { FocusManagerOptions } from '@lexical/a11y';
|
|
9
|
+
|
|
10
|
+
// Ported from @lexical/react/src/useLexicalFocusManagerRef.ts. `options` is
|
|
11
|
+
// optional, so the trailing slot is found with splitSlot. Public
|
|
12
|
+
// signature is `(options?: FocusManagerOptions)`.
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns a callback ref that registers the attached element as a focus-managed
|
|
16
|
+
* toolbar with {@link FocusManagerExtension}, and releases it when the element
|
|
17
|
+
* detaches. Requires `FocusManagerExtension` in the editor's extension tree.
|
|
18
|
+
*/
|
|
19
|
+
export function useLexicalFocusManagerRef(...args: any[]): (node: HTMLElement | null) => void {
|
|
20
|
+
const [user, slot] = splitSlot(args);
|
|
21
|
+
const options = (user[0] as FocusManagerOptions | undefined) ?? {};
|
|
22
|
+
const [editor] = useLexicalComposerContext();
|
|
23
|
+
const { toolbarItemSelector } = options;
|
|
24
|
+
const disposeRef = useRef<(() => void) | null>(null, subSlot(slot, 'ulfm:dispose'));
|
|
25
|
+
|
|
26
|
+
return useCallback(
|
|
27
|
+
(node: HTMLElement | null) => {
|
|
28
|
+
if (disposeRef.current !== null) {
|
|
29
|
+
disposeRef.current();
|
|
30
|
+
disposeRef.current = null;
|
|
31
|
+
}
|
|
32
|
+
if (node !== null) {
|
|
33
|
+
const dep = getExtensionDependencyFromEditor(editor, FocusManagerExtension);
|
|
34
|
+
disposeRef.current = dep.output.register(node, { toolbarItemSelector });
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
[editor, toolbarItemSelector],
|
|
38
|
+
subSlot(slot, 'ulfm:ref'),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { FocusTrapExtension, type FocusTrapInitialFocus } from '@lexical/a11y';
|
|
2
|
+
import { getExtensionDependencyFromEditor } from '@lexical/extension';
|
|
3
|
+
import { useCallback, useLayoutEffect, useRef } from 'octane';
|
|
4
|
+
|
|
5
|
+
import { useLexicalComposerContext } from './LexicalComposerContext';
|
|
6
|
+
import { splitSlot, subSlot } from './shared/internal';
|
|
7
|
+
|
|
8
|
+
export type { FocusTrapInitialFocus } from '@lexical/a11y';
|
|
9
|
+
|
|
10
|
+
// Ported from @lexical/react/src/useLexicalFocusTrapRef.ts. `initialFocus` and
|
|
11
|
+
// `allowOutside` are optional, so the trailing slot is found with splitSlot. Public
|
|
12
|
+
// signature is `(isActive, initialFocus?, allowOutside?)`.
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns a callback ref that registers the attached element as a focus-trap
|
|
16
|
+
* container with {@link FocusTrapExtension}. The trap activates when `isActive`
|
|
17
|
+
* is `true` and the element is mounted, and is released when the element
|
|
18
|
+
* detaches or `isActive` becomes `false`.
|
|
19
|
+
*
|
|
20
|
+
* `allowOutside` is held in a ref and read at event time, so an inline lambda
|
|
21
|
+
* does not re-create the trap on every render. Requires `FocusTrapExtension` in
|
|
22
|
+
* the editor's extension tree.
|
|
23
|
+
*/
|
|
24
|
+
export function useLexicalFocusTrapRef(...args: any[]): (node: HTMLElement | null) => void {
|
|
25
|
+
const [user, slot] = splitSlot(args);
|
|
26
|
+
const isActive = user[0] as boolean;
|
|
27
|
+
const initialFocus = (user[1] as FocusTrapInitialFocus | undefined) ?? 'firstFocusable';
|
|
28
|
+
const allowOutside = user[2] as ((target: HTMLElement) => boolean) | undefined;
|
|
29
|
+
const [editor] = useLexicalComposerContext();
|
|
30
|
+
const disposeRef = useRef<(() => void) | null>(null, subSlot(slot, 'ulft:dispose'));
|
|
31
|
+
// Keep the latest predicate in a ref so an inline lambda doesn't change the
|
|
32
|
+
// callback-ref identity (which would tear down and rebuild the trap every
|
|
33
|
+
// render); the registered trap reads it at event time. Upstream writes the
|
|
34
|
+
// ref during render; here it is committed in a layout effect so an abandoned
|
|
35
|
+
// render (e.g. a suspended transition) never leaves an uncommitted predicate.
|
|
36
|
+
const allowOutsideRef = useRef(allowOutside, subSlot(slot, 'ulft:allow'));
|
|
37
|
+
useLayoutEffect(
|
|
38
|
+
() => {
|
|
39
|
+
allowOutsideRef.current = allowOutside;
|
|
40
|
+
},
|
|
41
|
+
[allowOutside],
|
|
42
|
+
subSlot(slot, 'ulft:allowSync'),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
return useCallback(
|
|
46
|
+
(node: HTMLElement | null) => {
|
|
47
|
+
if (disposeRef.current !== null) {
|
|
48
|
+
disposeRef.current();
|
|
49
|
+
disposeRef.current = null;
|
|
50
|
+
}
|
|
51
|
+
if (node !== null && isActive) {
|
|
52
|
+
const dep = getExtensionDependencyFromEditor(editor, FocusTrapExtension);
|
|
53
|
+
disposeRef.current = dep.output.register(node, {
|
|
54
|
+
allowOutside: (target) => {
|
|
55
|
+
const fn = allowOutsideRef.current;
|
|
56
|
+
return fn ? fn(target) : false;
|
|
57
|
+
},
|
|
58
|
+
initialFocus,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
[editor, isActive, initialFocus],
|
|
63
|
+
subSlot(slot, 'ulft:ref'),
|
|
64
|
+
);
|
|
65
|
+
}
|