@lexical/mark 0.44.1-nightly.20260519.0 → 0.45.1-dev.0

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/package.json CHANGED
@@ -8,35 +8,49 @@
8
8
  "mark"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.44.1-nightly.20260519.0",
12
- "main": "LexicalMark.js",
13
- "types": "index.d.ts",
11
+ "version": "0.45.1-dev.0",
12
+ "main": "./dist/LexicalMark.js",
13
+ "types": "./dist/index.d.ts",
14
14
  "dependencies": {
15
- "@lexical/utils": "0.44.1-nightly.20260519.0",
16
- "lexical": "0.44.1-nightly.20260519.0"
15
+ "@lexical/utils": "0.45.1-dev.0",
16
+ "lexical": "0.45.1-dev.0"
17
17
  },
18
18
  "repository": {
19
19
  "type": "git",
20
20
  "url": "git+https://github.com/facebook/lexical.git",
21
21
  "directory": "packages/lexical-mark"
22
22
  },
23
- "module": "LexicalMark.mjs",
23
+ "module": "./dist/LexicalMark.mjs",
24
24
  "sideEffects": false,
25
25
  "exports": {
26
26
  ".": {
27
+ "source": "./src/index.ts",
27
28
  "import": {
28
- "types": "./index.d.ts",
29
- "development": "./LexicalMark.dev.mjs",
30
- "production": "./LexicalMark.prod.mjs",
31
- "node": "./LexicalMark.node.mjs",
32
- "default": "./LexicalMark.mjs"
29
+ "types": "./dist/index.d.ts",
30
+ "development": "./dist/LexicalMark.dev.mjs",
31
+ "production": "./dist/LexicalMark.prod.mjs",
32
+ "node": "./dist/LexicalMark.node.mjs",
33
+ "default": "./dist/LexicalMark.mjs"
33
34
  },
34
35
  "require": {
35
- "types": "./index.d.ts",
36
- "development": "./LexicalMark.dev.js",
37
- "production": "./LexicalMark.prod.js",
38
- "default": "./LexicalMark.js"
36
+ "types": "./dist/index.d.ts",
37
+ "development": "./dist/LexicalMark.dev.js",
38
+ "production": "./dist/LexicalMark.prod.js",
39
+ "default": "./dist/LexicalMark.js"
39
40
  }
40
41
  }
41
- }
42
+ },
43
+ "files": [
44
+ "dist",
45
+ "src",
46
+ "!src/__tests__",
47
+ "!src/__bench__",
48
+ "!src/__mocks__",
49
+ "!src/**/*.test.ts",
50
+ "!src/**/*.test.tsx",
51
+ "!src/**/*.bench.ts",
52
+ "!src/**/*.bench.tsx",
53
+ "README.md",
54
+ "LICENSE"
55
+ ]
42
56
  }
@@ -0,0 +1,198 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import type {
10
+ BaseSelection,
11
+ EditorConfig,
12
+ LexicalNode,
13
+ LexicalUpdateJSON,
14
+ NodeKey,
15
+ RangeSelection,
16
+ SerializedElementNode,
17
+ Spread,
18
+ } from 'lexical';
19
+
20
+ import {
21
+ addClassNamesToElement,
22
+ removeClassNamesFromElement,
23
+ } from '@lexical/utils';
24
+ import {$applyNodeReplacement, $isRangeSelection, ElementNode} from 'lexical';
25
+
26
+ export type SerializedMarkNode = Spread<
27
+ {
28
+ ids: Array<string>;
29
+ },
30
+ SerializedElementNode
31
+ >;
32
+
33
+ const NO_IDS: readonly string[] = [];
34
+
35
+ /** @noInheritDoc */
36
+ export class MarkNode extends ElementNode {
37
+ /** @internal */
38
+ __ids: readonly string[];
39
+
40
+ static getType(): string {
41
+ return 'mark';
42
+ }
43
+
44
+ static clone(node: MarkNode): MarkNode {
45
+ return new MarkNode(node.__ids, node.__key);
46
+ }
47
+
48
+ afterCloneFrom(prevNode: this): void {
49
+ super.afterCloneFrom(prevNode);
50
+ this.__ids = prevNode.__ids;
51
+ }
52
+
53
+ static importDOM(): null {
54
+ return null;
55
+ }
56
+
57
+ static importJSON(serializedNode: SerializedMarkNode): MarkNode {
58
+ return $createMarkNode().updateFromJSON(serializedNode);
59
+ }
60
+
61
+ updateFromJSON(serializedNode: LexicalUpdateJSON<SerializedMarkNode>): this {
62
+ return super.updateFromJSON(serializedNode).setIDs(serializedNode.ids);
63
+ }
64
+
65
+ exportJSON(): SerializedMarkNode {
66
+ return {
67
+ ...super.exportJSON(),
68
+ ids: this.getIDs(),
69
+ };
70
+ }
71
+
72
+ constructor(ids: readonly string[] = NO_IDS, key?: NodeKey) {
73
+ super(key);
74
+ this.__ids = ids;
75
+ }
76
+
77
+ createDOM(config: EditorConfig): HTMLElement {
78
+ const element = document.createElement('mark');
79
+ addClassNamesToElement(element, config.theme.mark);
80
+ if (this.__ids.length > 1) {
81
+ addClassNamesToElement(element, config.theme.markOverlap);
82
+ }
83
+ return element;
84
+ }
85
+
86
+ updateDOM(
87
+ prevNode: this,
88
+ element: HTMLElement,
89
+ config: EditorConfig,
90
+ ): boolean {
91
+ const prevIDs = prevNode.__ids;
92
+ const nextIDs = this.__ids;
93
+ const prevIDsCount = prevIDs.length;
94
+ const nextIDsCount = nextIDs.length;
95
+ const overlapTheme = config.theme.markOverlap;
96
+
97
+ if (prevIDsCount !== nextIDsCount) {
98
+ if (prevIDsCount === 1) {
99
+ if (nextIDsCount === 2) {
100
+ addClassNamesToElement(element, overlapTheme);
101
+ }
102
+ } else if (nextIDsCount === 1) {
103
+ removeClassNamesFromElement(element, overlapTheme);
104
+ }
105
+ }
106
+ return false;
107
+ }
108
+
109
+ hasID(id: string): boolean {
110
+ return this.getIDs().includes(id);
111
+ }
112
+
113
+ getIDs(): Array<string> {
114
+ return Array.from(this.getLatest().__ids);
115
+ }
116
+
117
+ setIDs(ids: readonly string[]): this {
118
+ const self = this.getWritable();
119
+ self.__ids = ids;
120
+ return self;
121
+ }
122
+
123
+ addID(id: string): this {
124
+ const self = this.getWritable();
125
+ return self.__ids.includes(id) ? self : self.setIDs([...self.__ids, id]);
126
+ }
127
+
128
+ deleteID(id: string): this {
129
+ const self = this.getWritable();
130
+ const idx = self.__ids.indexOf(id);
131
+ if (idx === -1) {
132
+ return self;
133
+ }
134
+ const ids = Array.from(self.__ids);
135
+ ids.splice(idx, 1);
136
+ return self.setIDs(ids);
137
+ }
138
+
139
+ insertNewAfter(
140
+ selection: RangeSelection,
141
+ restoreSelection = true,
142
+ ): null | ElementNode {
143
+ const markNode = $createMarkNode(this.__ids);
144
+ this.insertAfter(markNode, restoreSelection);
145
+ return markNode;
146
+ }
147
+
148
+ canInsertTextBefore(): false {
149
+ return false;
150
+ }
151
+
152
+ canInsertTextAfter(): false {
153
+ return false;
154
+ }
155
+
156
+ canBeEmpty(): false {
157
+ return false;
158
+ }
159
+
160
+ isInline(): true {
161
+ return true;
162
+ }
163
+
164
+ extractWithChild(
165
+ child: LexicalNode,
166
+ selection: BaseSelection,
167
+ destination: 'clone' | 'html',
168
+ ): boolean {
169
+ if (!$isRangeSelection(selection) || destination === 'html') {
170
+ return false;
171
+ }
172
+ const anchor = selection.anchor;
173
+ const focus = selection.focus;
174
+ const anchorNode = anchor.getNode();
175
+ const focusNode = focus.getNode();
176
+ const isBackward = selection.isBackward();
177
+ const selectionLength = isBackward
178
+ ? anchor.offset - focus.offset
179
+ : focus.offset - anchor.offset;
180
+ return (
181
+ this.isParentOf(anchorNode) &&
182
+ this.isParentOf(focusNode) &&
183
+ this.getTextContent().length === selectionLength
184
+ );
185
+ }
186
+
187
+ excludeFromCopy(destination: 'clone' | 'html'): boolean {
188
+ return destination !== 'clone';
189
+ }
190
+ }
191
+
192
+ export function $createMarkNode(ids: readonly string[] = NO_IDS): MarkNode {
193
+ return $applyNodeReplacement(new MarkNode(ids));
194
+ }
195
+
196
+ export function $isMarkNode(node: LexicalNode | null): node is MarkNode {
197
+ return node instanceof MarkNode;
198
+ }
package/src/index.ts ADDED
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import type {SerializedMarkNode} from './MarkNode';
10
+ import type {ElementNode, LexicalNode, RangeSelection, TextNode} from 'lexical';
11
+
12
+ import {
13
+ $createRangeSelection,
14
+ $isDecoratorNode,
15
+ $isElementNode,
16
+ $isTextNode,
17
+ defineExtension,
18
+ } from 'lexical';
19
+
20
+ import {$createMarkNode, $isMarkNode, MarkNode} from './MarkNode';
21
+
22
+ export function $unwrapMarkNode(node: MarkNode): void {
23
+ const children = node.getChildren();
24
+ let target = null;
25
+ for (let i = 0; i < children.length; i++) {
26
+ const child = children[i];
27
+ if (target === null) {
28
+ node.insertBefore(child);
29
+ } else {
30
+ target.insertAfter(child);
31
+ }
32
+ target = child;
33
+ }
34
+ node.remove();
35
+ }
36
+
37
+ export function $wrapSelectionInMarkNode(
38
+ selection: RangeSelection,
39
+ isBackward: boolean,
40
+ id: string,
41
+ createNode?: (ids: Array<string>) => MarkNode,
42
+ ): void {
43
+ // Force a forwards selection since append is used, ignore the argument.
44
+ // A new selection is used to avoid side-effects of flipping the given
45
+ // selection
46
+ const forwardSelection = $createRangeSelection();
47
+ const [startPoint, endPoint] = selection.isBackward()
48
+ ? [selection.focus, selection.anchor]
49
+ : [selection.anchor, selection.focus];
50
+ forwardSelection.anchor.set(
51
+ startPoint.key,
52
+ startPoint.offset,
53
+ startPoint.type,
54
+ );
55
+ forwardSelection.focus.set(endPoint.key, endPoint.offset, endPoint.type);
56
+
57
+ let currentNodeParent: ElementNode | null | undefined;
58
+ let lastCreatedMarkNode: MarkNode | undefined;
59
+
60
+ // Note that extract will split text nodes at the boundaries
61
+ const nodes = forwardSelection.extract();
62
+ // We only want wrap adjacent text nodes, line break nodes
63
+ // and inline element nodes. For decorator nodes and block
64
+ // element nodes, we step out of their boundary and start
65
+ // again after, if there are more nodes.
66
+ for (const node of nodes) {
67
+ if (
68
+ $isElementNode(lastCreatedMarkNode) &&
69
+ lastCreatedMarkNode.isParentOf(node)
70
+ ) {
71
+ // If the current node is a child of the last created mark node, there is nothing to do here
72
+ continue;
73
+ }
74
+ let targetNode: LexicalNode | null = null;
75
+
76
+ if ($isTextNode(node)) {
77
+ // Case 1: The node is a text node and we can include it
78
+ targetNode = node;
79
+ } else if ($isMarkNode(node)) {
80
+ // Case 2: the node is a mark node and we can ignore it as a target,
81
+ // moving on to its children. Note that when we make a mark inside
82
+ // another mark, it may ultimately be unnested by a call to
83
+ // `registerNestedElementResolver<MarkNode>` somewhere else in the
84
+ // codebase.
85
+ continue;
86
+ } else if (
87
+ ($isElementNode(node) || $isDecoratorNode(node)) &&
88
+ node.isInline()
89
+ ) {
90
+ // Case 3: inline element/decorator nodes can be added in their entirety
91
+ // to the new mark
92
+ targetNode = node;
93
+ }
94
+
95
+ if (targetNode !== null) {
96
+ // Now that we have a target node for wrapping with a mark, we can run
97
+ // through special cases.
98
+ if (targetNode && targetNode.is(currentNodeParent)) {
99
+ // The current node is a child of the target node to be wrapped, there
100
+ // is nothing to do here.
101
+ continue;
102
+ }
103
+ const parentNode = targetNode.getParent();
104
+ if (parentNode == null || !parentNode.is(currentNodeParent)) {
105
+ // If the parent node is not the current node's parent node, we can
106
+ // clear the last created mark node.
107
+ lastCreatedMarkNode = undefined;
108
+ }
109
+
110
+ currentNodeParent = parentNode;
111
+
112
+ if (lastCreatedMarkNode === undefined) {
113
+ // If we don't have a created mark node, we can make one
114
+ const createMarkNode = createNode || $createMarkNode;
115
+ lastCreatedMarkNode = createMarkNode([id]);
116
+ targetNode.insertBefore(lastCreatedMarkNode);
117
+ }
118
+
119
+ // Add the target node to be wrapped in the latest created mark node
120
+ lastCreatedMarkNode.append(targetNode);
121
+ } else {
122
+ // If we don't have a target node to wrap we can clear our state and
123
+ // continue on with the next node
124
+ currentNodeParent = undefined;
125
+ lastCreatedMarkNode = undefined;
126
+ }
127
+ }
128
+ // Make selection collapsed at the end
129
+ if ($isElementNode(lastCreatedMarkNode)) {
130
+ if (isBackward) {
131
+ lastCreatedMarkNode.selectStart();
132
+ } else {
133
+ lastCreatedMarkNode.selectEnd();
134
+ }
135
+ }
136
+ }
137
+
138
+ export function $getMarkIDs(
139
+ node: TextNode,
140
+ offset: number,
141
+ ): null | Array<string> {
142
+ let currentNode: LexicalNode | null = node;
143
+ while (currentNode !== null) {
144
+ if ($isMarkNode(currentNode)) {
145
+ return currentNode.getIDs();
146
+ } else if (
147
+ $isTextNode(currentNode) &&
148
+ offset === currentNode.getTextContentSize()
149
+ ) {
150
+ const nextSibling = currentNode.getNextSibling();
151
+ if ($isMarkNode(nextSibling)) {
152
+ return nextSibling.getIDs();
153
+ }
154
+ }
155
+ currentNode = currentNode.getParent();
156
+ }
157
+ return null;
158
+ }
159
+
160
+ /**
161
+ * Configures {@link MarkNode}
162
+ */
163
+ export const MarkExtension = defineExtension({
164
+ name: '@lexical/mark',
165
+ nodes: () => [MarkNode],
166
+ });
167
+
168
+ export {$createMarkNode, $isMarkNode, MarkNode, SerializedMarkNode};
File without changes
File without changes
File without changes
File without changes
File without changes