@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.
Files changed (32) hide show
  1. package/README.md +16 -10
  2. package/package.json +33 -24
  3. package/src/LexicalAutoEmbedPlugin.tsrx +30 -17
  4. package/src/{autoEmbedShared.ts → LexicalAutoEmbedPluginUtils.ts} +4 -4
  5. package/src/LexicalCollaborationContext.ts +7 -66
  6. package/src/LexicalCollaborationContextUtils.ts +76 -0
  7. package/src/LexicalDecoratorBlockNode.ts +41 -23
  8. package/src/LexicalDraggableBlockPlugin.tsrx +37 -39
  9. package/src/LexicalHorizontalRuleNode.tsrx +13 -21
  10. package/src/LexicalMarkdownShortcutPlugin.tsrx +12 -1
  11. package/src/LexicalMenuOption.ts +26 -0
  12. package/src/LexicalNodeContextMenuPlugin.tsrx +20 -14
  13. package/src/LexicalNodeEventPlugin.tsrx +2 -3
  14. package/src/LexicalNodeMenuPlugin.tsrx +2 -1
  15. package/src/LexicalTypeaheadMenuPlugin.tsrx +10 -9
  16. package/src/LexicalTypeaheadMenuPluginUtils.ts +78 -0
  17. package/src/index.ts +19 -4
  18. package/src/shared/LexicalContentEditableElement.tsrx +1 -1
  19. package/src/shared/LexicalDecorators.tsrx +23 -35
  20. package/src/shared/LexicalMenu.tsrx +63 -45
  21. package/src/shared/LexicalReactGeneratedJSON.ts +78 -0
  22. package/src/shared/menuShared.ts +75 -24
  23. package/src/shared/useCharacterLimit.ts +48 -23
  24. package/src/shared/useDynamicPositioning.ts +25 -18
  25. package/src/shared/useMenuAnchorRef.ts +28 -12
  26. package/src/typeaheadShared.ts +2 -55
  27. package/src/useLexicalAriaLiveRegion.ts +25 -0
  28. package/src/useLexicalFocusManagerRef.ts +40 -0
  29. package/src/useLexicalFocusTrapRef.ts +65 -0
  30. package/src/useLexicalIsTextContentEmpty.ts +5 -1
  31. package/src/useLexicalNodeSelection.ts +11 -0
  32. package/src/useLexicalRovingTabIndexRef.ts +41 -0
package/README.md CHANGED
@@ -31,11 +31,13 @@ function MyPlugin() {
31
31
  A convenience root barrel (`@octanejs/lexical`) re-exporting everything is also
32
32
  provided, but the per-subpath paths above are the drop-in form.
33
33
 
34
- Pinned to **Lexical 0.46.0**.
34
+ Pinned to **Lexical 0.51.0**.
35
35
 
36
36
  ## Status
37
37
 
38
- Near-complete `@lexical/react` parity — **35 of 39 modules ported** (Lexical 0.46.0).
38
+ `@lexical/react` parity for the classic composer API — **49 of 60 entry points ported**
39
+ (Lexical 0.51.0). The 11 remaining entry points are the React extension subsystem,
40
+ collaboration, and the tree view, each listed below.
39
41
  Parity is verified two ways: **differential** tests (the same `.tsrx` fixture run on
40
42
  octane *and* the real `@lexical/react`, asserting byte-identical DOM) plus ports of
41
43
  Lexical's own unit tests onto octane's harness.
@@ -47,20 +49,24 @@ Landed: the composer + context (`LexicalComposer`, `LexicalComposerContext`,
47
49
  link + clickable-link, hashtag, tab-indentation, markdown shortcuts, horizontal-rule,
48
50
  table, table-of-contents, auto-focus / -link / -embed, clear-editor, character-limit,
49
51
  draggable-block, node-event, on-change, selection-always-on-display, and the
50
- typeahead / node-menu / node-context-menu family — plus the `useLexical*` hooks and
51
- `LexicalCollaborationContext`.
52
+ typeahead / node-menu / node-context-menu family (with `LexicalMenuOption` and the
53
+ `*Utils` entry points) — plus the `useLexical*` hooks, including the accessibility
54
+ hooks backed by `@lexical/a11y` extensions, and `LexicalCollaborationContext`.
52
55
 
53
- The 4 not-yet-ported modules are each deferred for a specific reason, not merely
56
+ The not-yet-ported entry points are each deferred for a specific reason, not merely
54
57
  undone:
55
58
 
56
59
  - `LexicalCollaborationPlugin` — real-time Yjs collaboration. A genuine binding-layer
57
60
  port (it wraps the framework-agnostic `@lexical/yjs`), deferred until there's a
58
61
  two-peer Yjs harness to verify sync: the differential DOM-parity suite the other
59
62
  modules rely on can't meaningfully exercise live collaboration.
60
- - `LexicalExtensionComposer` + `LexicalExtensionEditorComposer` — the newer
61
- extension-builder composer API. Thin wrappers over a separate React-only subsystem
62
- (`@lexical/react/ReactExtension` + `ReactProviderExtension`); the classic
63
- `LexicalComposer` path here is fully supported.
63
+ - `LexicalExtensionComposer` + `LexicalExtensionEditorComposer`, and the rest of the
64
+ React extension subsystem (`ReactExtension`, `ReactProviderExtension`,
65
+ `ReactPluginHostExtension`, `ExtensionComponent`, `useExtensionComponent`,
66
+ `useExtensionSignalValue`, `TreeViewExtension`) — the newer extension-builder
67
+ composer API. The classic `LexicalComposer` path here is fully supported. The
68
+ accessibility hooks need an editor built from the matching `@lexical/a11y`
69
+ extension; provide one through `LexicalComposerContext` yourself.
64
70
  - `LexicalTreeView` — the debug tree viewer. A thin wrapper over
65
71
  `@lexical/devtools-core`'s `TreeView`, which is itself a React component (React is
66
72
  its peer dependency) — porting it means porting that separate devtools UI.
@@ -70,7 +76,7 @@ undone:
70
76
  The agnostic `@lexical/*` packages contain no React code, so they're consumed
71
77
  directly. Only `@lexical/react`'s components/hooks are reimplemented — translating
72
78
  React hooks to octane's (1:1 in nearly all cases), `forwardRef` to octane's
73
- ref-as-prop, `@floating-ui/react` to `@floating-ui/dom`, and the class-based
79
+ ref-as-prop, `@floating-ui/react` to `@octanejs/floating-ui`, and the class-based
74
80
  `LexicalErrorBoundary` to octane's `<ErrorBoundary>`.
75
81
 
76
82
  Lexical node classes require one module-family identity. The package manifest marks
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octanejs/lexical",
3
- "version": "0.1.57",
3
+ "version": "0.2.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "engines": {
@@ -83,42 +83,51 @@
83
83
  "./LexicalTableOfContentsPlugin": "./src/LexicalTableOfContentsPlugin.tsrx",
84
84
  "./LexicalAutoEmbedPlugin": "./src/LexicalAutoEmbedPlugin.tsrx",
85
85
  "./LexicalDraggableBlockPlugin": "./src/LexicalDraggableBlockPlugin.tsrx",
86
- "./LexicalNodeContextMenuPlugin": "./src/LexicalNodeContextMenuPlugin.tsrx"
86
+ "./LexicalNodeContextMenuPlugin": "./src/LexicalNodeContextMenuPlugin.tsrx",
87
+ "./LexicalMenuOption": "./src/LexicalMenuOption.ts",
88
+ "./LexicalTypeaheadMenuPluginUtils": "./src/LexicalTypeaheadMenuPluginUtils.ts",
89
+ "./LexicalAutoEmbedPluginUtils": "./src/LexicalAutoEmbedPluginUtils.ts",
90
+ "./LexicalCollaborationContextUtils": "./src/LexicalCollaborationContextUtils.ts",
91
+ "./useLexicalAriaLiveRegion": "./src/useLexicalAriaLiveRegion.ts",
92
+ "./useLexicalFocusManagerRef": "./src/useLexicalFocusManagerRef.ts",
93
+ "./useLexicalFocusTrapRef": "./src/useLexicalFocusTrapRef.ts",
94
+ "./useLexicalRovingTabIndexRef": "./src/useLexicalRovingTabIndexRef.ts"
87
95
  },
88
96
  "dependencies": {
89
- "@lexical/devtools-core": "0.46.0",
90
- "@lexical/dragon": "0.46.0",
91
- "@lexical/extension": "0.46.0",
92
- "@lexical/hashtag": "0.46.0",
93
- "@lexical/history": "0.46.0",
94
- "@lexical/internal": "0.46.0",
95
- "@lexical/link": "0.46.0",
96
- "@lexical/list": "0.46.0",
97
- "@lexical/mark": "0.46.0",
98
- "@lexical/markdown": "0.46.0",
99
- "@lexical/overflow": "0.46.0",
100
- "@lexical/plain-text": "0.46.0",
101
- "@lexical/rich-text": "0.46.0",
102
- "@lexical/table": "0.46.0",
103
- "@lexical/text": "0.46.0",
104
- "@lexical/utils": "0.46.0",
105
- "@lexical/yjs": "0.46.0",
106
- "lexical": "0.46.0",
97
+ "@lexical/a11y": "0.51.0",
98
+ "@lexical/devtools-core": "0.51.0",
99
+ "@lexical/dragon": "0.51.0",
100
+ "@lexical/extension": "0.51.0",
101
+ "@lexical/hashtag": "0.51.0",
102
+ "@lexical/history": "0.51.0",
103
+ "@lexical/internal": "0.51.0",
104
+ "@lexical/link": "0.51.0",
105
+ "@lexical/list": "0.51.0",
106
+ "@lexical/mark": "0.51.0",
107
+ "@lexical/markdown": "0.51.0",
108
+ "@lexical/overflow": "0.51.0",
109
+ "@lexical/plain-text": "0.51.0",
110
+ "@lexical/rich-text": "0.51.0",
111
+ "@lexical/table": "0.51.0",
112
+ "@lexical/text": "0.51.0",
113
+ "@lexical/utils": "0.51.0",
114
+ "@lexical/yjs": "0.51.0",
115
+ "lexical": "0.51.0",
107
116
  "yjs": "^13.6.31",
108
- "@octanejs/floating-ui": "0.1.57"
117
+ "@octanejs/floating-ui": "0.1.58"
109
118
  },
110
119
  "peerDependencies": {
111
- "octane": "^0.6.0"
120
+ "octane": "^0.7.0"
112
121
  },
113
122
  "devDependencies": {
114
- "@lexical/react": "0.46.0",
123
+ "@lexical/react": "0.51.0",
115
124
  "@tsrx/react": "^0.3.7",
116
125
  "esbuild": "^0.28.1",
117
126
  "react": "19.2.7",
118
127
  "react-dom": "19.2.7",
119
128
  "typescript": "^5.9.3",
120
129
  "vitest": "^4.1.10",
121
- "octane": "0.6.0"
130
+ "octane": "0.7.0"
122
131
  },
123
132
  "scripts": {
124
133
  "test": "vitest run"
@@ -1,25 +1,31 @@
1
1
  // Ported from @lexical/react/src/LexicalAutoEmbedPlugin.tsx. Watches pasted
2
2
  // AutoLink/Link nodes that match an embed config (YouTube, tweet, …) and shows a
3
3
  // node menu offering to replace the link with an embed node. Built on the ported
4
- // LexicalNodeMenuPlugin. Typed surface + AutoEmbedOption live in autoEmbedShared.ts.
4
+ // LexicalNodeMenuPlugin. Typed surface + AutoEmbedOption live in
5
+ // LexicalAutoEmbedPluginUtils.ts.
5
6
  import { $isLinkNode, AutoLinkNode, LinkNode } from '@lexical/link';
6
- import type { AutoEmbedOption, EmbedConfig } from './autoEmbedShared';
7
- import { INSERT_EMBED_COMMAND } from './autoEmbedShared';
7
+ import type { AutoEmbedOption, EmbedConfig } from './LexicalAutoEmbedPluginUtils';
8
+ import { INSERT_EMBED_COMMAND } from './LexicalAutoEmbedPluginUtils';
8
9
  import { useLexicalComposerContext } from './LexicalComposerContext';
9
10
  import { LexicalNodeMenuPlugin } from './LexicalNodeMenuPlugin.tsrx';
10
11
  import type { MenuRenderFn } from './shared/menuShared';
11
- import type { CommandListenerPriority, LexicalNode, NodeKey, NodeMutation } from 'lexical';
12
+ import { objectKlassEquals } from '@lexical/utils';
13
+ import type { CommandListenerPriority, LexicalNode, MutationListener, NodeKey } from 'lexical';
12
14
  import {
13
15
  $getNodeByKey,
14
16
  $getSelection,
17
+ $onUpdate,
18
+ COMMAND_PRIORITY_BEFORE_EDITOR,
15
19
  COMMAND_PRIORITY_EDITOR,
16
20
  COMMAND_PRIORITY_LOW,
17
21
  mergeRegister,
22
+ PASTE_COMMAND,
18
23
  PASTE_TAG,
19
24
  } from 'lexical';
20
25
  import { useCallback, useEffect, useMemo, useState } from 'octane';
21
26
 
22
- export { URL_MATCHER, INSERT_EMBED_COMMAND, AutoEmbedOption } from './autoEmbedShared';
27
+ export { URL_MATCHER, INSERT_EMBED_COMMAND, AutoEmbedOption } from './LexicalAutoEmbedPluginUtils';
28
+ export type { EmbedConfig, EmbedMatchResult } from './LexicalAutoEmbedPluginUtils';
23
29
 
24
30
  type LexicalAutoEmbedPluginProps<TEmbedConfig extends EmbedConfig> = {
25
31
  embedConfigs: TEmbedConfig[];
@@ -69,18 +75,10 @@ export function LexicalAutoEmbedPlugin<TEmbedConfig extends EmbedConfig>(
69
75
  }, [editor, embedConfigs]);
70
76
 
71
77
  useEffect(() => {
72
- const listener = (
73
- nodeMutations: Map<NodeKey, NodeMutation>,
74
- {
75
- updateTags,
76
- dirtyLeaves,
77
- }: {
78
- updateTags: Set<string>;
79
- dirtyLeaves: Set<string>;
80
- },
81
- ) => {
78
+ let isSingleTokenPaste = false;
79
+ const listener: MutationListener = (nodeMutations, { updateTags }) => {
82
80
  for (const [key, mutation] of nodeMutations) {
83
- if (mutation === 'created' && updateTags.has(PASTE_TAG) && dirtyLeaves.size <= 3) {
81
+ if (mutation === 'created' && updateTags.has(PASTE_TAG) && isSingleTokenPaste) {
84
82
  checkIfLinkNodeIsEmbeddable(key);
85
83
  } else if (key === nodeKey) {
86
84
  reset();
@@ -88,8 +86,23 @@ export function LexicalAutoEmbedPlugin<TEmbedConfig extends EmbedConfig>(
88
86
  }
89
87
  };
90
88
  return mergeRegister(
89
+ editor.registerCommand(
90
+ PASTE_COMMAND,
91
+ (event) => {
92
+ isSingleTokenPaste = objectKlassEquals(event, ClipboardEvent) &&
93
+ event.clipboardData !== null &&
94
+ /^\S+$/.test(event.clipboardData.getData('text/plain'));
95
+ if (isSingleTokenPaste) {
96
+ $onUpdate(() => {
97
+ isSingleTokenPaste = false;
98
+ });
99
+ }
100
+ return false;
101
+ },
102
+ COMMAND_PRIORITY_BEFORE_EDITOR,
103
+ ),
91
104
  ...[LinkNode, AutoLinkNode].map(
92
- (Klass) => editor.registerMutationListener(Klass, (...args) => listener(...args), {
105
+ (Klass) => editor.registerMutationListener(Klass, listener, {
93
106
  skipInitialization: true,
94
107
  }),
95
108
  ),
@@ -1,10 +1,10 @@
1
- // Typed / agnostic surface of @lexical/react/src/LexicalAutoEmbedPlugin.tsx — the
2
- // types, the URL matcher, the INSERT_EMBED_COMMAND, and the AutoEmbedOption class.
3
- // The component lives in LexicalAutoEmbedPlugin.tsrx.
1
+ // Ported from @lexical/react/src/LexicalAutoEmbedPluginUtils.ts — the types, the
2
+ // URL matcher, the INSERT_EMBED_COMMAND, and the AutoEmbedOption class. The
3
+ // component lives in LexicalAutoEmbedPlugin.tsrx.
4
4
  import type { LexicalCommand, LexicalEditor, LexicalNode } from 'lexical';
5
5
 
6
6
  import { createCommand } from 'lexical';
7
- import { MenuOption } from './shared/menuShared';
7
+ import { MenuOption } from './LexicalMenuOption';
8
8
 
9
9
  export type EmbedMatchResult<TEmbedMatchResult = unknown> = {
10
10
  url: string;
@@ -1,67 +1,8 @@
1
- // Ported from @lexical/react/src/LexicalCollaborationContext.tsx — the context +
2
- // reader hook (both rely only on useContext, which octane keys by identity, so no
3
- // hook slot is needed and this stays plain `.ts`). The `LexicalCollaboration`
1
+ // Ported from @lexical/react/src/LexicalCollaborationContext.tsx. The context and
2
+ // reader hook live in LexicalCollaborationContextUtils.ts; the `LexicalCollaboration`
4
3
  // provider component lands with P5 (collaboration).
5
- import type { Doc } from 'yjs';
6
-
7
- import { createContext, useContext } from 'octane';
8
-
9
- export type CollaborationContextType = {
10
- color: string;
11
- isCollabActive: boolean;
12
- name: string;
13
- yjsDocMap: Map<string, Doc>;
14
- };
15
-
16
- const entries: [string, string][] = [
17
- ['Cat', 'rgb(125, 50, 0)'],
18
- ['Dog', 'rgb(100, 0, 0)'],
19
- ['Rabbit', 'rgb(150, 0, 0)'],
20
- ['Frog', 'rgb(200, 0, 0)'],
21
- ['Fox', 'rgb(200, 75, 0)'],
22
- ['Hedgehog', 'rgb(0, 75, 0)'],
23
- ['Pigeon', 'rgb(0, 125, 0)'],
24
- ['Squirrel', 'rgb(75, 100, 0)'],
25
- ['Bear', 'rgb(125, 100, 0)'],
26
- ['Tiger', 'rgb(0, 0, 150)'],
27
- ['Leopard', 'rgb(0, 0, 200)'],
28
- ['Zebra', 'rgb(0, 0, 250)'],
29
- ['Wolf', 'rgb(0, 100, 150)'],
30
- ['Owl', 'rgb(0, 100, 100)'],
31
- ['Gull', 'rgb(100, 0, 100)'],
32
- ['Squid', 'rgb(150, 0, 150)'],
33
- ];
34
-
35
- const randomEntry = entries[Math.floor(Math.random() * entries.length)];
36
-
37
- export const CollaborationContext = createContext<CollaborationContextType | null>(null);
38
-
39
- function newContext(): CollaborationContextType {
40
- return {
41
- color: randomEntry[1],
42
- isCollabActive: false,
43
- name: randomEntry[0],
44
- yjsDocMap: new Map(),
45
- };
46
- }
47
-
48
- const UNSAFE_GLOBAL_CONTEXT = newContext();
49
-
50
- export function useCollaborationContext(
51
- username?: string,
52
- color?: string,
53
- ): CollaborationContextType {
54
- let collabContext = useContext(CollaborationContext);
55
-
56
- collabContext = collabContext ?? UNSAFE_GLOBAL_CONTEXT;
57
-
58
- if (username != null) {
59
- collabContext.name = username;
60
- }
61
-
62
- if (color != null) {
63
- collabContext.color = color;
64
- }
65
-
66
- return collabContext;
67
- }
4
+ export {
5
+ CollaborationContext,
6
+ type CollaborationContextType,
7
+ useCollaborationContext,
8
+ } from './LexicalCollaborationContextUtils';
@@ -0,0 +1,76 @@
1
+ // Ported from @lexical/react/src/LexicalCollaborationContextUtils.ts — the context +
2
+ // reader hook (both rely only on useContext, which octane keys by identity, so no
3
+ // hook slot is needed and this stays plain `.ts`).
4
+ import type { Doc } from 'yjs';
5
+
6
+ import { createContext, useContext } from 'octane';
7
+
8
+ export type CollaborationContextType = {
9
+ color: string;
10
+ isCollabActive: boolean;
11
+ name: string;
12
+ yjsDocMap: Map<string, Doc>;
13
+ };
14
+
15
+ const entries: [string, string][] = [
16
+ ['Cat', 'rgb(125, 50, 0)'],
17
+ ['Dog', 'rgb(100, 0, 0)'],
18
+ ['Rabbit', 'rgb(150, 0, 0)'],
19
+ ['Frog', 'rgb(200, 0, 0)'],
20
+ ['Fox', 'rgb(200, 75, 0)'],
21
+ ['Hedgehog', 'rgb(0, 75, 0)'],
22
+ ['Pigeon', 'rgb(0, 125, 0)'],
23
+ ['Squirrel', 'rgb(75, 100, 0)'],
24
+ ['Bear', 'rgb(125, 100, 0)'],
25
+ ['Tiger', 'rgb(0, 0, 150)'],
26
+ ['Leopard', 'rgb(0, 0, 200)'],
27
+ ['Zebra', 'rgb(0, 0, 250)'],
28
+ ['Wolf', 'rgb(0, 100, 150)'],
29
+ ['Owl', 'rgb(0, 100, 100)'],
30
+ ['Gull', 'rgb(100, 0, 100)'],
31
+ ['Squid', 'rgb(150, 0, 150)'],
32
+ ];
33
+
34
+ // Picked on first use rather than at module scope, so importing this module has
35
+ // no side effect. It is still picked once and shared by every context.
36
+ let randomEntry: undefined | [string, string];
37
+ function getRandomEntry(): [string, string] {
38
+ if (randomEntry === undefined) {
39
+ randomEntry = entries[Math.floor(Math.random() * entries.length)];
40
+ }
41
+ return randomEntry;
42
+ }
43
+
44
+ export const CollaborationContext = createContext<CollaborationContextType | null>(null);
45
+
46
+ /** @internal */
47
+ export function newContext(): CollaborationContextType {
48
+ const [name, color] = getRandomEntry();
49
+ return {
50
+ color,
51
+ isCollabActive: false,
52
+ name,
53
+ yjsDocMap: new Map(),
54
+ };
55
+ }
56
+
57
+ const UNSAFE_GLOBAL_CONTEXT = newContext();
58
+
59
+ export function useCollaborationContext(
60
+ username?: string,
61
+ color?: string,
62
+ ): CollaborationContextType {
63
+ let collabContext = useContext(CollaborationContext);
64
+
65
+ collabContext = collabContext ?? UNSAFE_GLOBAL_CONTEXT;
66
+
67
+ if (username != null) {
68
+ collabContext.name = username;
69
+ }
70
+
71
+ if (color != null) {
72
+ collabContext.color = color;
73
+ }
74
+
75
+ return collabContext;
76
+ }
@@ -2,16 +2,22 @@
2
2
  // apart from the decorate() return type — React typed it `JSX.Element`; octane's
3
3
  // decorate() returns a renderable, so the type parameter is left open (subclasses
4
4
  // pin it). Pair with BlockWithAlignableContents for selection + alignment.
5
- import type {
6
- ElementFormatType,
7
- LexicalNode,
8
- LexicalUpdateJSON,
9
- NodeKey,
10
- SerializedLexicalNode,
11
- Spread,
5
+ import {
6
+ $getDocument,
7
+ DecoratorNode,
8
+ type ElementFormatType,
9
+ enumValue,
10
+ type LexicalNode,
11
+ type LexicalParseJSON,
12
+ type NodeKey,
13
+ nodeSchema,
14
+ type SerializedLexicalNode,
15
+ type SerializedPartial,
16
+ type Spread,
17
+ withField,
12
18
  } from 'lexical';
13
19
 
14
- import { DecoratorNode } from 'lexical';
20
+ import { GENERATED_DECORATORBLOCK } from './shared/LexicalReactGeneratedJSON';
15
21
 
16
22
  export type SerializedDecoratorBlockNode = Spread<
17
23
  {
@@ -20,6 +26,23 @@ export type SerializedDecoratorBlockNode = Spread<
20
26
  SerializedLexicalNode
21
27
  >;
22
28
 
29
+ // Single source of truth for parsing the node-specific properties of a
30
+ // SerializedDecoratorBlockNode. DecoratorBlockNode is an abstract base (it has
31
+ // no concrete node type) so it publishes its schema on `$config` under the
32
+ // well-known `Symbol.for('DecoratorBlockNode')` key; concrete subclasses
33
+ // compose it with their own.
34
+ const decoratorBlockNodeSchema = nodeSchema<DecoratorBlockNode>()({
35
+ format: withField(enumValue(['', 'left', 'start', 'center', 'right', 'end', 'justify']), {
36
+ field: '__format',
37
+ }),
38
+ });
39
+
40
+ export interface DecoratorBlockNode {
41
+ exportJSON(compact?: false): SerializedDecoratorBlockNode;
42
+ exportJSON(compact: boolean): SerializedPartial<SerializedDecoratorBlockNode>;
43
+ updateFromJSON(serializedNode: LexicalParseJSON<SerializedDecoratorBlockNode>): this;
44
+ }
45
+
23
46
  export class DecoratorBlockNode extends DecoratorNode<unknown> {
24
47
  __format: ElementFormatType;
25
48
 
@@ -28,20 +51,15 @@ export class DecoratorBlockNode extends DecoratorNode<unknown> {
28
51
  this.__format = format || '';
29
52
  }
30
53
 
31
- afterCloneFrom(prevNode: this): void {
32
- super.afterCloneFrom(prevNode);
33
- this.__format = prevNode.__format;
34
- }
35
-
36
- exportJSON(): SerializedDecoratorBlockNode {
37
- return {
38
- ...super.exportJSON(),
39
- format: this.__format || '',
40
- };
41
- }
42
-
43
- updateFromJSON(serializedNode: LexicalUpdateJSON<SerializedDecoratorBlockNode>): this {
44
- return super.updateFromJSON(serializedNode).setFormat(serializedNode.format || '');
54
+ $config() {
55
+ return this.config(Symbol.for('DecoratorBlockNode'), {
56
+ // Named explicitly: this class carries the only declaration of `format`
57
+ // that its concrete subclasses inherit, and composeSchema honors an
58
+ // explicit `extends` where a severed static prototype chain would stop.
59
+ extends: DecoratorNode,
60
+ generated: GENERATED_DECORATORBLOCK,
61
+ json: decoratorBlockNodeSchema,
62
+ });
45
63
  }
46
64
 
47
65
  canIndent(): false {
@@ -49,7 +67,7 @@ export class DecoratorBlockNode extends DecoratorNode<unknown> {
49
67
  }
50
68
 
51
69
  createDOM(): HTMLElement {
52
- return document.createElement('div');
70
+ return $getDocument().createElement('div');
53
71
  }
54
72
 
55
73
  updateDOM(): false {
@@ -3,6 +3,7 @@
3
3
  // The DOM math helpers are framework-agnostic and copied as-is; the portal body is a
4
4
  // component (DraggableContainer) so its <div> events wire through the template path.
5
5
  import { useLexicalComposerContext } from './LexicalComposerContext';
6
+ import { useLexicalEditable } from './useLexicalEditable';
6
7
  import { eventFiles } from '@lexical/rich-text';
7
8
  import { calculateZoomLevel } from '@lexical/utils';
8
9
  import {
@@ -24,6 +25,8 @@ import {
24
25
  IS_FIREFOX,
25
26
  isHTMLElement,
26
27
  mergeRegister,
28
+ registerEventListener,
29
+ registerEventListeners,
27
30
  } from 'lexical';
28
31
  import type { LexicalEditor } from 'lexical';
29
32
  import { createPortal, useCallback, useEffect, useRef, useState } from 'octane';
@@ -57,14 +60,26 @@ function getTopLevelNodeKeys(editor: LexicalEditor): string[] {
57
60
  return editor.read('latest', () => $getRoot().getChildrenKeys());
58
61
  }
59
62
 
63
+ function restoreEditorFocus(editor: LexicalEditor, rootElement: HTMLElement): void {
64
+ rootElement.focus({ preventScroll: true });
65
+ editor.update(() => {
66
+ const selection = $getSelection();
67
+ if (selection !== null && !selection.dirty) {
68
+ selection.dirty = true;
69
+ }
70
+ });
71
+ }
72
+
60
73
  function getCollapsedMargins(elem: HTMLElement): {
61
74
  marginTop: number;
62
75
  marginBottom: number;
63
76
  } {
77
+ const view = elem.ownerDocument.defaultView;
64
78
  const getMargin = (element: Element | null, margin: 'marginTop' | 'marginBottom'): number =>
65
- element ? parseFloat(window.getComputedStyle(element)[margin]) : 0;
79
+ element && view ? parseFloat(view.getComputedStyle(element)[margin]) : 0;
66
80
 
67
- const { marginTop, marginBottom } = window.getComputedStyle(elem);
81
+ const { marginTop = '0', marginBottom = '0' } =
82
+ view ? view.getComputedStyle(elem) : {};
68
83
  const prevElemSiblingMarginBottom = getMargin(elem.previousElementSibling, 'marginBottom');
69
84
  const nextElemSiblingMarginTop = getMargin(elem.nextElementSibling, 'marginTop');
70
85
  const collapsedTopMargin = Math.max(parseFloat(marginTop), prevElemSiblingMarginBottom);
@@ -168,11 +183,14 @@ function setMenuPosition(
168
183
  }
169
184
 
170
185
  const targetRect = targetElem.getBoundingClientRect();
171
- const targetStyle = window.getComputedStyle(targetElem);
186
+ const targetView = targetElem.ownerDocument.defaultView;
187
+ const targetStyle =
188
+ targetView ? targetView.getComputedStyle(targetElem) : null;
172
189
  const floatingElemRect = floatingElem.getBoundingClientRect();
173
190
  const anchorElementRect = anchorElem.getBoundingClientRect();
174
191
 
175
- let targetCalculateHeight = parseInt(targetStyle.lineHeight, 10);
192
+ let targetCalculateHeight =
193
+ targetStyle ? parseInt(targetStyle.lineHeight, 10) : NaN;
176
194
  if (isNaN(targetCalculateHeight)) {
177
195
  targetCalculateHeight = targetRect.bottom - targetRect.top;
178
196
  }
@@ -294,16 +312,11 @@ function useDraggableBlockMenu(
294
312
  }
295
313
 
296
314
  if (scrollerElem != null) {
297
- scrollerElem.addEventListener('mousemove', onMouseMove);
298
- scrollerElem.addEventListener('mouseleave', onMouseLeave);
315
+ return registerEventListeners(scrollerElem, {
316
+ mouseleave: onMouseLeave,
317
+ mousemove: onMouseMove,
318
+ });
299
319
  }
300
-
301
- return () => {
302
- if (scrollerElem != null) {
303
- scrollerElem.removeEventListener('mousemove', onMouseMove);
304
- scrollerElem.removeEventListener('mouseleave', onMouseLeave);
305
- }
306
- };
307
320
  }, [scrollerElem, anchorElem, editor, isOnMenu, setDraggableBlockElem]);
308
321
 
309
322
  useEffect(() => {
@@ -421,21 +434,14 @@ function useDraggableBlockMenu(
421
434
  const relatedTarget = event.relatedTarget;
422
435
  if (isHTMLElement(relatedTarget) && isOnMenu(relatedTarget)) {
423
436
  if (rootElement) {
424
- rootElement.focus({ preventScroll: true });
425
- editor.update(() => {
426
- const selection = $getSelection();
427
- if (selection !== null && !selection.dirty) {
428
- selection.dirty = true;
429
- }
430
- });
437
+ restoreEditorFocus(editor, rootElement);
431
438
  }
432
439
  event.stopImmediatePropagation();
433
440
  }
434
441
  }
435
442
 
436
443
  if (rootElement) {
437
- rootElement.addEventListener('blur', onBlur, true);
438
- return () => rootElement.removeEventListener('blur', onBlur, true);
444
+ return registerEventListener(rootElement, 'blur', onBlur, true);
439
445
  }
440
446
  }),
441
447
  editor.registerCommand(
@@ -444,13 +450,7 @@ function useDraggableBlockMenu(
444
450
  const rootElement = editor.getRootElement();
445
451
  const activeElement = getActiveElementDeep(getRootOwnerDocument(rootElement));
446
452
  if (rootElement && isHTMLElement(activeElement) && isOnMenu(activeElement)) {
447
- rootElement.focus({ preventScroll: true });
448
- editor.update(() => {
449
- const selection = $getSelection();
450
- if (selection !== null && !selection.dirty) {
451
- selection.dirty = true;
452
- }
453
- });
453
+ restoreEditorFocus(editor, rootElement);
454
454
  return true;
455
455
  }
456
456
  return false;
@@ -480,13 +480,7 @@ function useDraggableBlockMenu(
480
480
  if (IS_FIREFOX) {
481
481
  const rootElement = editor.getRootElement();
482
482
  if (rootElement !== null && getActiveElement(rootElement) !== rootElement) {
483
- rootElement.focus({ preventScroll: true });
484
- editor.update(() => {
485
- const selection = $getSelection();
486
- if (selection !== null && !selection.dirty) {
487
- selection.dirty = true;
488
- }
489
- });
483
+ restoreEditorFocus(editor, rootElement);
490
484
  }
491
485
  }
492
486
  }
@@ -495,8 +489,9 @@ function useDraggableBlockMenu(
495
489
  isDraggingBlockRef.current = false;
496
490
  hideTargetLine(targetLineRef.current);
497
491
 
498
- if (IS_FIREFOX) {
499
- editor.focus();
492
+ const rootElement = editor.getRootElement();
493
+ if (rootElement !== null && getActiveElement(rootElement) !== rootElement) {
494
+ restoreEditorFocus(editor, rootElement);
500
495
  }
501
496
  }
502
497
 
@@ -522,12 +517,15 @@ type Props = {
522
517
  export function DraggableBlockPlugin_EXPERIMENTAL(props: Props) {
523
518
  const anchorElem = props.anchorElem ?? document.body;
524
519
  const [editor] = useLexicalComposerContext();
520
+ // Subscribed rather than read off the editor, so that setEditable() actually
521
+ // re-renders the handle.
522
+ const isEditable = useLexicalEditable();
525
523
  return useDraggableBlockMenu(
526
524
  editor,
527
525
  anchorElem,
528
526
  props.menuRef,
529
527
  props.targetLineRef,
530
- editor._editable,
528
+ isEditable,
531
529
  props.menuComponent,
532
530
  props.targetLineComponent,
533
531
  props.isOnMenu,