@astryxdesign/richtext 0.7.12-canary.f543ec7 → 0.7.12
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/CHANGELOG.md +32 -0
- package/README.md +9 -0
- package/dist/CodePastePlugin.d.ts +2 -0
- package/dist/CodePastePlugin.d.ts.map +1 -0
- package/dist/CodePastePlugin.js +103 -0
- package/dist/CodeTabPlugin.d.ts +2 -0
- package/dist/CodeTabPlugin.d.ts.map +1 -0
- package/dist/CodeTabPlugin.js +256 -0
- package/dist/DisabledLinkGuardPlugin.d.ts +4 -0
- package/dist/DisabledLinkGuardPlugin.d.ts.map +1 -0
- package/dist/DisabledLinkGuardPlugin.js +93 -0
- package/dist/RichTextEditor.d.ts +3 -2
- package/dist/RichTextEditor.d.ts.map +1 -1
- package/dist/RichTextEditor.js +17 -7
- package/dist/RichTextEditorAutoLinkPlugin.d.ts +5 -5
- package/dist/RichTextEditorAutoLinkPlugin.d.ts.map +1 -1
- package/dist/RichTextEditorAutoLinkPlugin.js +12 -6
- package/dist/privateAttributes/layerPortalBoundary.stylex.d.ts +12 -0
- package/dist/privateAttributes/layerPortalBoundary.stylex.d.ts.map +1 -0
- package/dist/privateAttributes/layerPortalBoundary.stylex.js +16 -0
- package/package.json +5 -5
- package/src/CodePastePlugin.tsx +143 -0
- package/src/CodeTabPlugin.tsx +298 -0
- package/src/DisabledLinkGuardPlugin.tsx +93 -0
- package/src/RichTextEditor.doc.mjs +16 -1
- package/src/RichTextEditor.tsx +18 -4
- package/src/RichTextEditorAutoLinkPlugin.doc.mjs +1 -1
- package/src/RichTextEditorAutoLinkPlugin.tsx +18 -6
- package/src/privateAttributes/layerPortalBoundary.stylex.ts +18 -0
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
* @output Exports `RichTextEditorAutoLinkPlugin` and its props type, plus the
|
|
7
7
|
* default `DEFAULT_LINK_MATCHERS`.
|
|
8
8
|
* @position @astryxdesign/richtext. Drop into RichTextEditor's `plugins` slot to
|
|
9
|
-
* auto-linkify URLs and emails as they are typed or pasted
|
|
10
|
-
* `AutoLinkNode` to be registered (it is, by
|
|
9
|
+
* auto-linkify URLs and emails as they are typed or pasted, except inside
|
|
10
|
+
* a code block. Requires `AutoLinkNode` to be registered (it is, by
|
|
11
|
+
* default).
|
|
11
12
|
*
|
|
12
13
|
* SYNC: When modified, update:
|
|
13
14
|
* - /packages/richtext/src/index.ts (exports / barrel)
|
|
@@ -24,9 +25,8 @@
|
|
|
24
25
|
* and new-tab defaults, so a consumer supplies a richer matcher set — an
|
|
25
26
|
* application-specific identifier or ticket syntax, say — through the
|
|
26
27
|
* `matchers` prop without changing the contract. An application needing a
|
|
27
|
-
* hardened transform (expanded separators,
|
|
28
|
-
*
|
|
29
|
-
* `AutoLinkPlugin`.
|
|
28
|
+
* hardened transform (expanded separators, say) substitutes its own plugin in
|
|
29
|
+
* the same slot; astryx stays on the upstream `AutoLinkPlugin`.
|
|
30
30
|
*/
|
|
31
31
|
import { type LinkMatcher } from '@lexical/react/LexicalAutoLinkPlugin';
|
|
32
32
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextEditorAutoLinkPlugin.d.ts","sourceRoot":"","sources":["../src/RichTextEditorAutoLinkPlugin.tsx"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAGL,KAAK,WAAW,EACjB,MAAM,sCAAsC,CAAC;
|
|
1
|
+
{"version":3,"file":"RichTextEditorAutoLinkPlugin.d.ts","sourceRoot":"","sources":["../src/RichTextEditorAutoLinkPlugin.tsx"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAGL,KAAK,WAAW,EACjB,MAAM,sCAAsC,CAAC;AAK9C;;;;GAIG;AACH,eAAO,MAAM,uBAAuB;;;CAG1B,CAAC;AAEX;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,EAAE,WAAW,EAc9C,CAAC;AAMF,MAAM,WAAW,iCAAiC;IAChD;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,WAAW,EAAE,CAAC;IACzB;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,KAAK,IAAI,CAAC;CACjE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,4BAA4B,CAAC,EAC3C,QAAgC,EAChC,QAAQ,GACT,EAAE,iCAAiC,+BAQnC;yBAXe,4BAA4B"}
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
* @output Exports `RichTextEditorAutoLinkPlugin` and its props type, plus the
|
|
11
11
|
* default `DEFAULT_LINK_MATCHERS`.
|
|
12
12
|
* @position @astryxdesign/richtext. Drop into RichTextEditor's `plugins` slot to
|
|
13
|
-
* auto-linkify URLs and emails as they are typed or pasted
|
|
14
|
-
* `AutoLinkNode` to be registered (it is, by
|
|
13
|
+
* auto-linkify URLs and emails as they are typed or pasted, except inside
|
|
14
|
+
* a code block. Requires `AutoLinkNode` to be registered (it is, by
|
|
15
|
+
* default).
|
|
15
16
|
*
|
|
16
17
|
* SYNC: When modified, update:
|
|
17
18
|
* - /packages/richtext/src/index.ts (exports / barrel)
|
|
@@ -28,11 +29,11 @@
|
|
|
28
29
|
* and new-tab defaults, so a consumer supplies a richer matcher set — an
|
|
29
30
|
* application-specific identifier or ticket syntax, say — through the
|
|
30
31
|
* `matchers` prop without changing the contract. An application needing a
|
|
31
|
-
* hardened transform (expanded separators,
|
|
32
|
-
*
|
|
33
|
-
* `AutoLinkPlugin`.
|
|
32
|
+
* hardened transform (expanded separators, say) substitutes its own plugin in
|
|
33
|
+
* the same slot; astryx stays on the upstream `AutoLinkPlugin`.
|
|
34
34
|
*/
|
|
35
35
|
import { AutoLinkPlugin, createLinkMatcherWithRegExp } from '@lexical/react/LexicalAutoLinkPlugin';
|
|
36
|
+
import { $isCodeNode } from '@lexical/code';
|
|
36
37
|
import { URL_MATCHER, EMAIL_MATCHER, sanitizeUrl } from "./linkUtils.js";
|
|
37
38
|
|
|
38
39
|
/**
|
|
@@ -65,6 +66,10 @@ export const DEFAULT_LINK_MATCHERS = [text => {
|
|
|
65
66
|
attributes: NEW_TAB_LINK_ATTRIBUTES
|
|
66
67
|
} : null;
|
|
67
68
|
}];
|
|
69
|
+
|
|
70
|
+
// Text in a code block is never linked. The upstream plugin registers again
|
|
71
|
+
// when this array changes. Keep it a module constant.
|
|
72
|
+
const EXCLUDE_PARENTS = [$isCodeNode];
|
|
68
73
|
/**
|
|
69
74
|
* Automatically converts typed/pasted URLs and email addresses into links.
|
|
70
75
|
* Render it inside the editor's `plugins` slot — it reaches the editor via
|
|
@@ -87,7 +92,8 @@ export function RichTextEditorAutoLinkPlugin({
|
|
|
87
92
|
}) {
|
|
88
93
|
return /*#__PURE__*/_jsx(AutoLinkPlugin, {
|
|
89
94
|
matchers: matchers,
|
|
90
|
-
onChange: onChange
|
|
95
|
+
onChange: onChange,
|
|
96
|
+
excludeParents: EXCLUDE_PARENTS
|
|
91
97
|
});
|
|
92
98
|
}
|
|
93
99
|
RichTextEditorAutoLinkPlugin.displayName = 'RichTextEditorAutoLinkPlugin';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file privateAttributes/layerPortalBoundary.stylex.ts
|
|
3
|
+
* @input The editable root Lexical reconciles
|
|
4
|
+
* @output rendererManagedRootProps, which marks that root
|
|
5
|
+
* @position Owner module of the private layer-portal-boundary attribute in
|
|
6
|
+
* RichText: the only RichText file that writes its name. Core's Layer reads
|
|
7
|
+
* the mark through its own owner module of the same name. No package entry
|
|
8
|
+
* exports it.
|
|
9
|
+
*/
|
|
10
|
+
/** Spread on the editable root, whose DOM Lexical reconciles. */
|
|
11
|
+
export declare const rendererManagedRootProps: Readonly<Record<`data-${string}`, string>>;
|
|
12
|
+
//# sourceMappingURL=layerPortalBoundary.stylex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layerPortalBoundary.stylex.d.ts","sourceRoot":"","sources":["../../src/privateAttributes/layerPortalBoundary.stylex.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AAEH,iEAAiE;AACjE,eAAO,MAAM,wBAAwB,EAAE,QAAQ,CAC7C,MAAM,CAAC,QAAQ,MAAM,EAAE,EAAE,MAAM,CAAC,CAGjC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file privateAttributes/layerPortalBoundary.stylex.ts
|
|
5
|
+
* @input The editable root Lexical reconciles
|
|
6
|
+
* @output rendererManagedRootProps, which marks that root
|
|
7
|
+
* @position Owner module of the private layer-portal-boundary attribute in
|
|
8
|
+
* RichText: the only RichText file that writes its name. Core's Layer reads
|
|
9
|
+
* the mark through its own owner module of the same name. No package entry
|
|
10
|
+
* exports it.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Spread on the editable root, whose DOM Lexical reconciles. */
|
|
14
|
+
export const rendererManagedRootProps = {
|
|
15
|
+
'data-astryx-layer-portal-boundary': 'external'
|
|
16
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/richtext",
|
|
3
3
|
"displayName": "Rich Text",
|
|
4
|
-
"version": "0.7.12
|
|
4
|
+
"version": "0.7.12",
|
|
5
5
|
"description": "Astryx rich text — a Lexical-based rich text editor and viewer, styled with Astryx design tokens.",
|
|
6
6
|
"author": "Meta Open Source",
|
|
7
7
|
"license": "MIT",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
68
|
"@stylexjs/stylex": ">=0.10.0",
|
|
69
|
-
"@astryxdesign/core": "0.7.12
|
|
69
|
+
"@astryxdesign/core": "0.7.12",
|
|
70
70
|
"react": ">=19.0.0",
|
|
71
71
|
"react-dom": ">=19.0.0",
|
|
72
72
|
"lexical": "^0.52.0",
|
|
@@ -125,8 +125,6 @@
|
|
|
125
125
|
}
|
|
126
126
|
},
|
|
127
127
|
"devDependencies": {
|
|
128
|
-
"@astryxdesign/a11y-spec": "0.7.12-canary.f543ec7",
|
|
129
|
-
"@astryxdesign/cli": "0.7.12-canary.f543ec7",
|
|
130
128
|
"@babel/cli": "^7.29.7",
|
|
131
129
|
"@babel/core": "^7.29.7",
|
|
132
130
|
"@babel/preset-react": "^7.29.7",
|
|
@@ -145,7 +143,9 @@
|
|
|
145
143
|
"@lexical/utils": "^0.52.0",
|
|
146
144
|
"@stylexjs/babel-plugin": "^0.19.0",
|
|
147
145
|
"@testing-library/react": "^16.3.2",
|
|
148
|
-
"lexical": "^0.52.0"
|
|
146
|
+
"lexical": "^0.52.0",
|
|
147
|
+
"@astryxdesign/a11y-spec": "0.0.0",
|
|
148
|
+
"@astryxdesign/cli": "0.7.12"
|
|
149
149
|
},
|
|
150
150
|
"scripts": {
|
|
151
151
|
"api:report": "node ../../scripts/api-report/cli.mjs --package .",
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file CodePastePlugin.tsx
|
|
7
|
+
* @input Uses @lexical/react (composer context), @lexical/code (CodeNode), and
|
|
8
|
+
* lexical.
|
|
9
|
+
* @output Exports CodePastePlugin, which makes rich content pasted or dropped
|
|
10
|
+
* into a code block go in as plain lines of code.
|
|
11
|
+
* @position Rendered by RichTextEditor; always on; not exported. A paste
|
|
12
|
+
* whose selection starts in a code block goes in as plain text: the
|
|
13
|
+
* clipboard's plain text, or else one line per pasted block.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {useEffect} from 'react';
|
|
17
|
+
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
|
18
|
+
import {$isCodeNode} from '@lexical/code';
|
|
19
|
+
import {
|
|
20
|
+
$addUpdateTag,
|
|
21
|
+
$findMatchingParent,
|
|
22
|
+
$getSelection,
|
|
23
|
+
$isElementNode,
|
|
24
|
+
$isRangeSelection,
|
|
25
|
+
$isSelectionCapturedInDecoratorInput,
|
|
26
|
+
COMMAND_PRIORITY_LOW,
|
|
27
|
+
isDOMNode,
|
|
28
|
+
mergeRegister,
|
|
29
|
+
PASTE_COMMAND,
|
|
30
|
+
PASTE_TAG,
|
|
31
|
+
SELECTION_INSERT_CLIPBOARD_NODES_COMMAND,
|
|
32
|
+
type BaseSelection,
|
|
33
|
+
type LexicalNode,
|
|
34
|
+
type RangeSelection,
|
|
35
|
+
} from 'lexical';
|
|
36
|
+
|
|
37
|
+
/** Clipboard types that carry more than plain text. */
|
|
38
|
+
const RICH_TYPES = ['text/html', 'application/x-lexical-editor'];
|
|
39
|
+
|
|
40
|
+
/** Whether content inserted at the selection lands in a code block. */
|
|
41
|
+
function $startsInCodeBlock(
|
|
42
|
+
selection: BaseSelection | null,
|
|
43
|
+
): selection is RangeSelection {
|
|
44
|
+
if (!$isRangeSelection(selection)) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
const start = selection.isBackward() ? selection.focus : selection.anchor;
|
|
48
|
+
const node = start.getNode();
|
|
49
|
+
// A link (an autolinked URL) can sit between the text and its code block.
|
|
50
|
+
return $findMatchingParent(node, $isCodeNode) !== null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Clipboard nodes as plain lines: each block's text on a line of its own (a
|
|
55
|
+
* paragraph, a heading, a list item, a table cell), and inline content
|
|
56
|
+
* between blocks on one line.
|
|
57
|
+
*/
|
|
58
|
+
function $plainLinesOf(nodes: ReadonlyArray<LexicalNode>): string {
|
|
59
|
+
const lines: string[] = [];
|
|
60
|
+
let inline: string | null = null;
|
|
61
|
+
const endInline = () => {
|
|
62
|
+
if (inline != null) {
|
|
63
|
+
lines.push(inline);
|
|
64
|
+
inline = null;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const visit = (node: LexicalNode) => {
|
|
68
|
+
if (node.isInline()) {
|
|
69
|
+
inline = (inline ?? '') + node.getTextContent();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
endInline();
|
|
73
|
+
if (
|
|
74
|
+
$isElementNode(node) &&
|
|
75
|
+
node.getChildren().some(child => !child.isInline())
|
|
76
|
+
) {
|
|
77
|
+
node.getChildren().forEach(visit);
|
|
78
|
+
endInline();
|
|
79
|
+
} else {
|
|
80
|
+
lines.push(node.getTextContent());
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
nodes.forEach(visit);
|
|
84
|
+
endInline();
|
|
85
|
+
return lines.join('\n');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function CodePastePlugin(): null {
|
|
89
|
+
const [editor] = useLexicalComposerContext();
|
|
90
|
+
useEffect(
|
|
91
|
+
() =>
|
|
92
|
+
mergeRegister(
|
|
93
|
+
// Ahead of the rich-text paste, which inserts the clipboard's rich
|
|
94
|
+
// content when there is any.
|
|
95
|
+
editor.registerCommand(
|
|
96
|
+
PASTE_COMMAND,
|
|
97
|
+
event => {
|
|
98
|
+
// A paste into an input a decorator node renders is that input's.
|
|
99
|
+
if (
|
|
100
|
+
isDOMNode(event.target) &&
|
|
101
|
+
$isSelectionCapturedInDecoratorInput(event.target)
|
|
102
|
+
) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const data = 'clipboardData' in event ? event.clipboardData : null;
|
|
106
|
+
if (
|
|
107
|
+
!editor.isEditable() ||
|
|
108
|
+
data == null ||
|
|
109
|
+
!RICH_TYPES.some(type => data.types.includes(type))
|
|
110
|
+
) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const text = data.getData('text/plain');
|
|
114
|
+
const selection = $getSelection();
|
|
115
|
+
if (text === '' || !$startsInCodeBlock(selection)) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
event.preventDefault();
|
|
119
|
+
// Its own undo step, as every paste is.
|
|
120
|
+
$addUpdateTag(PASTE_TAG);
|
|
121
|
+
selection.insertRawText(text);
|
|
122
|
+
return true;
|
|
123
|
+
},
|
|
124
|
+
COMMAND_PRIORITY_LOW,
|
|
125
|
+
),
|
|
126
|
+
// Every insertion of clipboard nodes, a paste with no plain text or a
|
|
127
|
+
// drop, comes through here; ahead of the table's own handler.
|
|
128
|
+
editor.registerCommand(
|
|
129
|
+
SELECTION_INSERT_CLIPBOARD_NODES_COMMAND,
|
|
130
|
+
({nodes, selection}) => {
|
|
131
|
+
if (!$startsInCodeBlock(selection)) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
selection.insertRawText($plainLinesOf(nodes));
|
|
135
|
+
return true;
|
|
136
|
+
},
|
|
137
|
+
COMMAND_PRIORITY_LOW,
|
|
138
|
+
),
|
|
139
|
+
),
|
|
140
|
+
[editor],
|
|
141
|
+
);
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file CodeTabPlugin.tsx
|
|
7
|
+
* @input Uses @lexical/react (composer context), @lexical/code (CodeNode), and
|
|
8
|
+
* lexical.
|
|
9
|
+
* @output Exports CodeTabPlugin, which makes Tab and Shift+Tab in a code block
|
|
10
|
+
* indent and outdent every line the selection touches, by the indent the
|
|
11
|
+
* block's own lines use.
|
|
12
|
+
* @position Rendered by RichTextEditor; always on, and not exported from the
|
|
13
|
+
* package. A press indents by the block's own indent, so edited code keeps
|
|
14
|
+
* the indentation its author chose; the selection stays on the same
|
|
15
|
+
* characters, and each press is one undo step. It replaces only what Tab
|
|
16
|
+
* did in a code block before: TabIndentationPlugin put a tab character in
|
|
17
|
+
* place of the selection. Its listener sits at the front of the
|
|
18
|
+
* editor-priority queue, ahead of TabIndentationPlugin and behind every
|
|
19
|
+
* low-priority listener, so Escape then Tab still moves focus out of the
|
|
20
|
+
* editor (TabFocusEscapePlugin, WCAG 2.1.2). A selection that reaches
|
|
21
|
+
* outside the code block keeps TabIndentationPlugin's meaning. Read-only
|
|
22
|
+
* and disabled editors are unchanged.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import {useEffect} from 'react';
|
|
26
|
+
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
|
27
|
+
import {$isCodeNode, type CodeNode} from '@lexical/code';
|
|
28
|
+
import {
|
|
29
|
+
$addUpdateTag,
|
|
30
|
+
$createTextNode,
|
|
31
|
+
$getSelection,
|
|
32
|
+
$isRangeSelection,
|
|
33
|
+
$isTabNode,
|
|
34
|
+
$isTextNode,
|
|
35
|
+
COMMAND_PRIORITY_BEFORE_EDITOR,
|
|
36
|
+
HISTORY_PUSH_TAG,
|
|
37
|
+
KEY_TAB_COMMAND,
|
|
38
|
+
type LexicalNode,
|
|
39
|
+
type PointType,
|
|
40
|
+
type RangeSelection,
|
|
41
|
+
type TextNode,
|
|
42
|
+
} from 'lexical';
|
|
43
|
+
|
|
44
|
+
/** The narrowest space indent a block uses, and the widest. */
|
|
45
|
+
const MIN_SPACE_INDENT = 2;
|
|
46
|
+
const MAX_SPACE_INDENT = 8;
|
|
47
|
+
|
|
48
|
+
function leadingSpaces(line: string): number {
|
|
49
|
+
return /^ */.exec(line)?.[0].length ?? 0;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The indent a code block's lines use: a tab when any line starts with one;
|
|
54
|
+
* otherwise the fewest spaces an indented line starts with, kept between two
|
|
55
|
+
* and eight; otherwise two spaces.
|
|
56
|
+
*/
|
|
57
|
+
function codeIndentUnit(lines: ReadonlyArray<string>): string {
|
|
58
|
+
if (lines.some(line => line.startsWith('\t'))) {
|
|
59
|
+
return '\t';
|
|
60
|
+
}
|
|
61
|
+
const widths = lines.map(leadingSpaces).filter(width => width > 0);
|
|
62
|
+
const width = widths.length === 0 ? MIN_SPACE_INDENT : Math.min(...widths);
|
|
63
|
+
return ' '.repeat(
|
|
64
|
+
Math.min(MAX_SPACE_INDENT, Math.max(MIN_SPACE_INDENT, width)),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A code block's children, the offset in the block's text where each one
|
|
70
|
+
* starts, and that text. A line ends at a line break node or at a newline
|
|
71
|
+
* inside a text node: imported code holds newlines, and Enter adds line
|
|
72
|
+
* break nodes.
|
|
73
|
+
*/
|
|
74
|
+
interface CodeLayout {
|
|
75
|
+
readonly children: ReadonlyArray<LexicalNode>;
|
|
76
|
+
readonly starts: ReadonlyArray<number>;
|
|
77
|
+
readonly text: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function $layoutOf(code: CodeNode): CodeLayout {
|
|
81
|
+
const children = code.getChildren();
|
|
82
|
+
const starts: number[] = [];
|
|
83
|
+
let text = '';
|
|
84
|
+
for (const child of children) {
|
|
85
|
+
starts.push(text.length);
|
|
86
|
+
text += child.getTextContent();
|
|
87
|
+
}
|
|
88
|
+
return {children, starts, text};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A text node that can take a caret and grow: any but a tab node. */
|
|
92
|
+
function $isPlainText(node: LexicalNode): node is TextNode {
|
|
93
|
+
return $isTextNode(node) && !$isTabNode(node);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The code block that holds both ends of the selection, or null. */
|
|
97
|
+
function $selectionCodeBlock(selection: RangeSelection): CodeNode | null {
|
|
98
|
+
const blockOf = (point: PointType) => {
|
|
99
|
+
const node = point.getNode();
|
|
100
|
+
return $isCodeNode(node) ? node : node.getParent();
|
|
101
|
+
};
|
|
102
|
+
const block = blockOf(selection.anchor);
|
|
103
|
+
return $isCodeNode(block) && block.is(blockOf(selection.focus))
|
|
104
|
+
? block
|
|
105
|
+
: null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Where a point of the block's selection falls in the block's text. */
|
|
109
|
+
function $offsetOf(
|
|
110
|
+
code: CodeNode,
|
|
111
|
+
layout: CodeLayout,
|
|
112
|
+
point: PointType,
|
|
113
|
+
): number {
|
|
114
|
+
if (point.key === code.getKey()) {
|
|
115
|
+
return point.offset < layout.children.length
|
|
116
|
+
? layout.starts[point.offset]
|
|
117
|
+
: layout.text.length;
|
|
118
|
+
}
|
|
119
|
+
const index = layout.children.findIndex(
|
|
120
|
+
child => child.getKey() === point.key,
|
|
121
|
+
);
|
|
122
|
+
return index < 0 ? 0 : layout.starts[index] + point.offset;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Moves a selection point to an offset in the block's text: into the text
|
|
127
|
+
* node there, or between children where none holds it (beside a tab or a
|
|
128
|
+
* line break, or on an empty line).
|
|
129
|
+
*/
|
|
130
|
+
function $placePoint(code: CodeNode, point: PointType, offset: number): void {
|
|
131
|
+
const children = code.getChildren();
|
|
132
|
+
let start = 0;
|
|
133
|
+
for (let index = 0; index < children.length; index++) {
|
|
134
|
+
const child = children[index];
|
|
135
|
+
const size = child.getTextContentSize();
|
|
136
|
+
if ($isPlainText(child) && offset >= start && offset <= start + size) {
|
|
137
|
+
point.set(child.getKey(), offset - start, 'text');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (offset <= start) {
|
|
141
|
+
point.set(code.getKey(), index, 'element');
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
start += size;
|
|
145
|
+
}
|
|
146
|
+
point.set(code.getKey(), children.length, 'element');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Inserts text at an offset in the block's text. */
|
|
150
|
+
function $insertAt(code: CodeNode, offset: number, text: string): void {
|
|
151
|
+
const {children, starts} = $layoutOf(code);
|
|
152
|
+
for (let index = 0; index < children.length; index++) {
|
|
153
|
+
const child = children[index];
|
|
154
|
+
const start = starts[index];
|
|
155
|
+
if (
|
|
156
|
+
$isPlainText(child) &&
|
|
157
|
+
offset >= start &&
|
|
158
|
+
offset <= start + child.getTextContentSize()
|
|
159
|
+
) {
|
|
160
|
+
const content = child.getTextContent();
|
|
161
|
+
const at = offset - start;
|
|
162
|
+
child.setTextContent(content.slice(0, at) + text + content.slice(at));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (offset <= start) {
|
|
166
|
+
child.insertBefore($createTextNode(text));
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
code.append($createTextNode(text));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Removes `count` characters at an offset in the block's text. */
|
|
174
|
+
function $removeAt(code: CodeNode, offset: number, count: number): void {
|
|
175
|
+
const {children, starts} = $layoutOf(code);
|
|
176
|
+
let left = count;
|
|
177
|
+
for (let index = 0; index < children.length && left > 0; index++) {
|
|
178
|
+
const child = children[index];
|
|
179
|
+
const size = child.getTextContentSize();
|
|
180
|
+
if (!$isTextNode(child) || starts[index] + size <= offset) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const from = Math.max(0, offset - starts[index]);
|
|
184
|
+
const removed = Math.min(left, size - from);
|
|
185
|
+
const content = child.getTextContent();
|
|
186
|
+
const rest = content.slice(0, from) + content.slice(from + removed);
|
|
187
|
+
if (rest === '') {
|
|
188
|
+
child.remove();
|
|
189
|
+
} else {
|
|
190
|
+
child.setTextContent(rest);
|
|
191
|
+
}
|
|
192
|
+
left -= removed;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Indents, or outdents, every line the selection touches by the block's own
|
|
198
|
+
* indent, and puts the selection back on the characters it held.
|
|
199
|
+
*/
|
|
200
|
+
function $shiftLines(
|
|
201
|
+
code: CodeNode,
|
|
202
|
+
selection: RangeSelection,
|
|
203
|
+
isOutdent: boolean,
|
|
204
|
+
): void {
|
|
205
|
+
const layout = $layoutOf(code);
|
|
206
|
+
const lines = layout.text.split('\n');
|
|
207
|
+
const lineStarts: number[] = [];
|
|
208
|
+
let lineStart = 0;
|
|
209
|
+
for (const line of lines) {
|
|
210
|
+
lineStarts.push(lineStart);
|
|
211
|
+
lineStart += line.length + 1;
|
|
212
|
+
}
|
|
213
|
+
const lineAt = (offset: number) => {
|
|
214
|
+
let line = 0;
|
|
215
|
+
while (line + 1 < lineStarts.length && lineStarts[line + 1] <= offset) {
|
|
216
|
+
line++;
|
|
217
|
+
}
|
|
218
|
+
return line;
|
|
219
|
+
};
|
|
220
|
+
const unit = codeIndentUnit(lines);
|
|
221
|
+
// A tab-indented block outdents a space-indented line by the narrowest
|
|
222
|
+
// space indent.
|
|
223
|
+
const spaceUnit = unit === '\t' ? MIN_SPACE_INDENT : unit.length;
|
|
224
|
+
const anchor = $offsetOf(code, layout, selection.anchor);
|
|
225
|
+
const focus = $offsetOf(code, layout, selection.focus);
|
|
226
|
+
const start = Math.min(anchor, focus);
|
|
227
|
+
const end = Math.max(anchor, focus);
|
|
228
|
+
const first = lineAt(start);
|
|
229
|
+
let last = lineAt(end);
|
|
230
|
+
// A selection that ends where a line begins does not reach into that line.
|
|
231
|
+
if (last > first && lineStarts[last] === end) {
|
|
232
|
+
last--;
|
|
233
|
+
}
|
|
234
|
+
// Each edit as [offset, characters added (negative when removed)], made
|
|
235
|
+
// from the last line up so the offsets of the lines above stay true.
|
|
236
|
+
const edits: Array<readonly [number, number]> = [];
|
|
237
|
+
for (let line = last; line >= first; line--) {
|
|
238
|
+
const at = lineStarts[line];
|
|
239
|
+
if (!isOutdent) {
|
|
240
|
+
$insertAt(code, at, unit);
|
|
241
|
+
edits.push([at, unit.length]);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
const removed = lines[line].startsWith('\t')
|
|
245
|
+
? 1
|
|
246
|
+
: Math.min(leadingSpaces(lines[line]), spaceUnit);
|
|
247
|
+
if (removed > 0) {
|
|
248
|
+
$removeAt(code, at, removed);
|
|
249
|
+
edits.push([at, -removed]);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
// A point keeps its character: text added at or before it pushes it on,
|
|
253
|
+
// and a point inside removed indentation moves to its line's start.
|
|
254
|
+
const shifted = (offset: number) =>
|
|
255
|
+
edits.reduce((moved, [at, delta]) => {
|
|
256
|
+
if (delta > 0) {
|
|
257
|
+
return offset >= at ? moved + delta : moved;
|
|
258
|
+
}
|
|
259
|
+
return offset > at ? moved - Math.min(-delta, offset - at) : moved;
|
|
260
|
+
}, offset);
|
|
261
|
+
$placePoint(code, selection.anchor, shifted(anchor));
|
|
262
|
+
$placePoint(code, selection.focus, shifted(focus));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function CodeTabPlugin(): null {
|
|
266
|
+
const [editor] = useLexicalComposerContext();
|
|
267
|
+
useEffect(
|
|
268
|
+
() =>
|
|
269
|
+
editor.registerCommand(
|
|
270
|
+
KEY_TAB_COMMAND,
|
|
271
|
+
event => {
|
|
272
|
+
const selection = $getSelection();
|
|
273
|
+
if (!editor.isEditable() || !$isRangeSelection(selection)) {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
const code = $selectionCodeBlock(selection);
|
|
277
|
+
if (code == null) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
event.preventDefault();
|
|
281
|
+
// One undo step per press, even when a press adds or removes a
|
|
282
|
+
// single character beside the caret. The push tag starts a step
|
|
283
|
+
// before the press. A dirty block makes history class the press
|
|
284
|
+
// as its own change, so typing after it starts another step.
|
|
285
|
+
$addUpdateTag(HISTORY_PUSH_TAG);
|
|
286
|
+
$shiftLines(code, selection, event.shiftKey);
|
|
287
|
+
code.markDirty();
|
|
288
|
+
return true;
|
|
289
|
+
},
|
|
290
|
+
// After TabFocusEscapePlugin's low-priority listener, which lets an
|
|
291
|
+
// armed Tab move focus, and before TabIndentationPlugin's; see the
|
|
292
|
+
// file header.
|
|
293
|
+
COMMAND_PRIORITY_BEFORE_EDITOR,
|
|
294
|
+
),
|
|
295
|
+
[editor],
|
|
296
|
+
);
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file DisabledLinkGuardPlugin.tsx
|
|
7
|
+
* @input The composer's editor, and whether RichTextEditor is disabled
|
|
8
|
+
* @output DisabledLinkGuardPlugin (internal; not re-exported from index.ts):
|
|
9
|
+
* while the editor is disabled, its links add no tab stop and never navigate
|
|
10
|
+
* @position Rendered by RichTextEditor inside its LexicalExtensionComposer
|
|
11
|
+
*
|
|
12
|
+
* A disabled editor is not editable, so the browser treats each link in it as
|
|
13
|
+
* a live link: a tab stop that navigates on a click or on Enter. A disabled
|
|
14
|
+
* control must not navigate, and it keeps the focus order it has when enabled,
|
|
15
|
+
* where its links are not tab stops.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import {useEffect} from 'react';
|
|
19
|
+
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
|
20
|
+
|
|
21
|
+
// Enter on a focused link and a screen reader's activation arrive as a click.
|
|
22
|
+
// A middle click arrives as auxclick.
|
|
23
|
+
function preventLinkActivation(event: MouseEvent) {
|
|
24
|
+
if (event.target instanceof Element && event.target.closest('a[href]')) {
|
|
25
|
+
event.preventDefault();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Guards the links under one root element. It records the links it changes,
|
|
31
|
+
* so it restores only those.
|
|
32
|
+
*/
|
|
33
|
+
function createLinkGuard(root: HTMLElement) {
|
|
34
|
+
const guarded = new Set<Element>();
|
|
35
|
+
const guard = () => {
|
|
36
|
+
for (const link of root.querySelectorAll('a[href]')) {
|
|
37
|
+
if (!guarded.has(link)) {
|
|
38
|
+
guarded.add(link);
|
|
39
|
+
link.setAttribute('tabindex', '-1');
|
|
40
|
+
// Assistive technology announces each link as disabled. Only the
|
|
41
|
+
// link ignores the pointer, so a wide table still scrolls.
|
|
42
|
+
link.setAttribute('aria-disabled', 'true');
|
|
43
|
+
if (link instanceof HTMLElement) {
|
|
44
|
+
link.style.pointerEvents = 'none';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
guard();
|
|
50
|
+
root.addEventListener('click', preventLinkActivation, true);
|
|
51
|
+
root.addEventListener('auxclick', preventLinkActivation, true);
|
|
52
|
+
return {
|
|
53
|
+
guard,
|
|
54
|
+
release() {
|
|
55
|
+
for (const link of guarded) {
|
|
56
|
+
link.removeAttribute('tabindex');
|
|
57
|
+
link.removeAttribute('aria-disabled');
|
|
58
|
+
if (link instanceof HTMLElement) {
|
|
59
|
+
link.style.removeProperty('pointer-events');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
guarded.clear();
|
|
63
|
+
root.removeEventListener('click', preventLinkActivation, true);
|
|
64
|
+
root.removeEventListener('auxclick', preventLinkActivation, true);
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function DisabledLinkGuardPlugin({isDisabled}: {isDisabled: boolean}) {
|
|
70
|
+
const [editor] = useLexicalComposerContext();
|
|
71
|
+
// Synchronizes the editor's link elements, which Lexical renders and owns,
|
|
72
|
+
// with the disabled state: guarded while disabled, restored when enabled.
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (!isDisabled) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
let linkGuard: ReturnType<typeof createLinkGuard> | null = null;
|
|
78
|
+
const unregisterRoot = editor.registerRootListener(next => {
|
|
79
|
+
linkGuard?.release();
|
|
80
|
+
linkGuard = next == null ? null : createLinkGuard(next);
|
|
81
|
+
});
|
|
82
|
+
// A new or changed document renders new link elements.
|
|
83
|
+
const unregisterUpdate = editor.registerUpdateListener(() => {
|
|
84
|
+
linkGuard?.guard();
|
|
85
|
+
});
|
|
86
|
+
return () => {
|
|
87
|
+
unregisterUpdate();
|
|
88
|
+
unregisterRoot();
|
|
89
|
+
linkGuard?.release();
|
|
90
|
+
};
|
|
91
|
+
}, [editor, isDisabled]);
|
|
92
|
+
return null;
|
|
93
|
+
}
|