@ekz/lexical-mark 0.40.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.
@@ -0,0 +1,273 @@
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
+ 'use strict';
10
+
11
+ var lexical = require('@ekz/lexical');
12
+ var lexicalUtils = require('@ekz/lexical-utils');
13
+
14
+ /**
15
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ *
20
+ */
21
+
22
+ const NO_IDS = [];
23
+
24
+ /** @noInheritDoc */
25
+ class MarkNode extends lexical.ElementNode {
26
+ /** @internal */
27
+ __ids;
28
+ static getType() {
29
+ return 'mark';
30
+ }
31
+ static clone(node) {
32
+ return new MarkNode(node.__ids, node.__key);
33
+ }
34
+ static importDOM() {
35
+ return null;
36
+ }
37
+ static importJSON(serializedNode) {
38
+ return $createMarkNode().updateFromJSON(serializedNode);
39
+ }
40
+ updateFromJSON(serializedNode) {
41
+ return super.updateFromJSON(serializedNode).setIDs(serializedNode.ids);
42
+ }
43
+ exportJSON() {
44
+ return {
45
+ ...super.exportJSON(),
46
+ ids: this.getIDs()
47
+ };
48
+ }
49
+ constructor(ids = NO_IDS, key) {
50
+ super(key);
51
+ this.__ids = ids;
52
+ }
53
+ createDOM(config) {
54
+ const element = document.createElement('mark');
55
+ lexicalUtils.addClassNamesToElement(element, config.theme.mark);
56
+ if (this.__ids.length > 1) {
57
+ lexicalUtils.addClassNamesToElement(element, config.theme.markOverlap);
58
+ }
59
+ return element;
60
+ }
61
+ updateDOM(prevNode, element, config) {
62
+ const prevIDs = prevNode.__ids;
63
+ const nextIDs = this.__ids;
64
+ const prevIDsCount = prevIDs.length;
65
+ const nextIDsCount = nextIDs.length;
66
+ const overlapTheme = config.theme.markOverlap;
67
+ if (prevIDsCount !== nextIDsCount) {
68
+ if (prevIDsCount === 1) {
69
+ if (nextIDsCount === 2) {
70
+ lexicalUtils.addClassNamesToElement(element, overlapTheme);
71
+ }
72
+ } else if (nextIDsCount === 1) {
73
+ lexicalUtils.removeClassNamesFromElement(element, overlapTheme);
74
+ }
75
+ }
76
+ return false;
77
+ }
78
+ hasID(id) {
79
+ return this.getIDs().includes(id);
80
+ }
81
+ getIDs() {
82
+ return Array.from(this.getLatest().__ids);
83
+ }
84
+ setIDs(ids) {
85
+ const self = this.getWritable();
86
+ self.__ids = ids;
87
+ return self;
88
+ }
89
+ addID(id) {
90
+ const self = this.getWritable();
91
+ return self.__ids.includes(id) ? self : self.setIDs([...self.__ids, id]);
92
+ }
93
+ deleteID(id) {
94
+ const self = this.getWritable();
95
+ const idx = self.__ids.indexOf(id);
96
+ if (idx === -1) {
97
+ return self;
98
+ }
99
+ const ids = Array.from(self.__ids);
100
+ ids.splice(idx, 1);
101
+ return self.setIDs(ids);
102
+ }
103
+ insertNewAfter(selection, restoreSelection = true) {
104
+ const markNode = $createMarkNode(this.__ids);
105
+ this.insertAfter(markNode, restoreSelection);
106
+ return markNode;
107
+ }
108
+ canInsertTextBefore() {
109
+ return false;
110
+ }
111
+ canInsertTextAfter() {
112
+ return false;
113
+ }
114
+ canBeEmpty() {
115
+ return false;
116
+ }
117
+ isInline() {
118
+ return true;
119
+ }
120
+ extractWithChild(child, selection, destination) {
121
+ if (!lexical.$isRangeSelection(selection) || destination === 'html') {
122
+ return false;
123
+ }
124
+ const anchor = selection.anchor;
125
+ const focus = selection.focus;
126
+ const anchorNode = anchor.getNode();
127
+ const focusNode = focus.getNode();
128
+ const isBackward = selection.isBackward();
129
+ const selectionLength = isBackward ? anchor.offset - focus.offset : focus.offset - anchor.offset;
130
+ return this.isParentOf(anchorNode) && this.isParentOf(focusNode) && this.getTextContent().length === selectionLength;
131
+ }
132
+ excludeFromCopy(destination) {
133
+ return destination !== 'clone';
134
+ }
135
+ }
136
+ function $createMarkNode(ids = NO_IDS) {
137
+ return lexical.$applyNodeReplacement(new MarkNode(ids));
138
+ }
139
+ function $isMarkNode(node) {
140
+ return node instanceof MarkNode;
141
+ }
142
+
143
+ /**
144
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
145
+ *
146
+ * This source code is licensed under the MIT license found in the
147
+ * LICENSE file in the root directory of this source tree.
148
+ *
149
+ */
150
+
151
+ function $unwrapMarkNode(node) {
152
+ const children = node.getChildren();
153
+ let target = null;
154
+ for (let i = 0; i < children.length; i++) {
155
+ const child = children[i];
156
+ if (target === null) {
157
+ node.insertBefore(child);
158
+ } else {
159
+ target.insertAfter(child);
160
+ }
161
+ target = child;
162
+ }
163
+ node.remove();
164
+ }
165
+ function $wrapSelectionInMarkNode(selection, isBackward, id, createNode) {
166
+ // Force a forwards selection since append is used, ignore the argument.
167
+ // A new selection is used to avoid side-effects of flipping the given
168
+ // selection
169
+ const forwardSelection = lexical.$createRangeSelection();
170
+ const [startPoint, endPoint] = selection.isBackward() ? [selection.focus, selection.anchor] : [selection.anchor, selection.focus];
171
+ forwardSelection.anchor.set(startPoint.key, startPoint.offset, startPoint.type);
172
+ forwardSelection.focus.set(endPoint.key, endPoint.offset, endPoint.type);
173
+ let currentNodeParent;
174
+ let lastCreatedMarkNode;
175
+
176
+ // Note that extract will split text nodes at the boundaries
177
+ const nodes = forwardSelection.extract();
178
+ // We only want wrap adjacent text nodes, line break nodes
179
+ // and inline element nodes. For decorator nodes and block
180
+ // element nodes, we step out of their boundary and start
181
+ // again after, if there are more nodes.
182
+ for (const node of nodes) {
183
+ if (lexical.$isElementNode(lastCreatedMarkNode) && lastCreatedMarkNode.isParentOf(node)) {
184
+ // If the current node is a child of the last created mark node, there is nothing to do here
185
+ continue;
186
+ }
187
+ let targetNode = null;
188
+ if (lexical.$isTextNode(node)) {
189
+ // Case 1: The node is a text node and we can include it
190
+ targetNode = node;
191
+ } else if ($isMarkNode(node)) {
192
+ // Case 2: the node is a mark node and we can ignore it as a target,
193
+ // moving on to its children. Note that when we make a mark inside
194
+ // another mark, it may ultimately be unnested by a call to
195
+ // `registerNestedElementResolver<MarkNode>` somewhere else in the
196
+ // codebase.
197
+ continue;
198
+ } else if ((lexical.$isElementNode(node) || lexical.$isDecoratorNode(node)) && node.isInline()) {
199
+ // Case 3: inline element/decorator nodes can be added in their entirety
200
+ // to the new mark
201
+ targetNode = node;
202
+ }
203
+ if (targetNode !== null) {
204
+ // Now that we have a target node for wrapping with a mark, we can run
205
+ // through special cases.
206
+ if (targetNode && targetNode.is(currentNodeParent)) {
207
+ // The current node is a child of the target node to be wrapped, there
208
+ // is nothing to do here.
209
+ continue;
210
+ }
211
+ const parentNode = targetNode.getParent();
212
+ if (parentNode == null || !parentNode.is(currentNodeParent)) {
213
+ // If the parent node is not the current node's parent node, we can
214
+ // clear the last created mark node.
215
+ lastCreatedMarkNode = undefined;
216
+ }
217
+ currentNodeParent = parentNode;
218
+ if (lastCreatedMarkNode === undefined) {
219
+ // If we don't have a created mark node, we can make one
220
+ const createMarkNode = createNode || $createMarkNode;
221
+ lastCreatedMarkNode = createMarkNode([id]);
222
+ targetNode.insertBefore(lastCreatedMarkNode);
223
+ }
224
+
225
+ // Add the target node to be wrapped in the latest created mark node
226
+ lastCreatedMarkNode.append(targetNode);
227
+ } else {
228
+ // If we don't have a target node to wrap we can clear our state and
229
+ // continue on with the next node
230
+ currentNodeParent = undefined;
231
+ lastCreatedMarkNode = undefined;
232
+ }
233
+ }
234
+ // Make selection collapsed at the end
235
+ if (lexical.$isElementNode(lastCreatedMarkNode)) {
236
+ if (isBackward) {
237
+ lastCreatedMarkNode.selectStart();
238
+ } else {
239
+ lastCreatedMarkNode.selectEnd();
240
+ }
241
+ }
242
+ }
243
+ function $getMarkIDs(node, offset) {
244
+ let currentNode = node;
245
+ while (currentNode !== null) {
246
+ if ($isMarkNode(currentNode)) {
247
+ return currentNode.getIDs();
248
+ } else if (lexical.$isTextNode(currentNode) && offset === currentNode.getTextContentSize()) {
249
+ const nextSibling = currentNode.getNextSibling();
250
+ if ($isMarkNode(nextSibling)) {
251
+ return nextSibling.getIDs();
252
+ }
253
+ }
254
+ currentNode = currentNode.getParent();
255
+ }
256
+ return null;
257
+ }
258
+
259
+ /**
260
+ * Configures {@link MarkNode}
261
+ */
262
+ const MarkExtension = lexical.defineExtension({
263
+ name: '@ekz/lexical-mark',
264
+ nodes: () => [MarkNode]
265
+ });
266
+
267
+ exports.$createMarkNode = $createMarkNode;
268
+ exports.$getMarkIDs = $getMarkIDs;
269
+ exports.$isMarkNode = $isMarkNode;
270
+ exports.$unwrapMarkNode = $unwrapMarkNode;
271
+ exports.$wrapSelectionInMarkNode = $wrapSelectionInMarkNode;
272
+ exports.MarkExtension = MarkExtension;
273
+ exports.MarkNode = MarkNode;
@@ -0,0 +1,265 @@
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 { ElementNode, $isRangeSelection, $applyNodeReplacement, defineExtension, $createRangeSelection, $isElementNode, $isTextNode, $isDecoratorNode } from '@ekz/lexical';
10
+ import { addClassNamesToElement, removeClassNamesFromElement } from '@ekz/lexical-utils';
11
+
12
+ /**
13
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
14
+ *
15
+ * This source code is licensed under the MIT license found in the
16
+ * LICENSE file in the root directory of this source tree.
17
+ *
18
+ */
19
+
20
+ const NO_IDS = [];
21
+
22
+ /** @noInheritDoc */
23
+ class MarkNode extends ElementNode {
24
+ /** @internal */
25
+ __ids;
26
+ static getType() {
27
+ return 'mark';
28
+ }
29
+ static clone(node) {
30
+ return new MarkNode(node.__ids, node.__key);
31
+ }
32
+ static importDOM() {
33
+ return null;
34
+ }
35
+ static importJSON(serializedNode) {
36
+ return $createMarkNode().updateFromJSON(serializedNode);
37
+ }
38
+ updateFromJSON(serializedNode) {
39
+ return super.updateFromJSON(serializedNode).setIDs(serializedNode.ids);
40
+ }
41
+ exportJSON() {
42
+ return {
43
+ ...super.exportJSON(),
44
+ ids: this.getIDs()
45
+ };
46
+ }
47
+ constructor(ids = NO_IDS, key) {
48
+ super(key);
49
+ this.__ids = ids;
50
+ }
51
+ createDOM(config) {
52
+ const element = document.createElement('mark');
53
+ addClassNamesToElement(element, config.theme.mark);
54
+ if (this.__ids.length > 1) {
55
+ addClassNamesToElement(element, config.theme.markOverlap);
56
+ }
57
+ return element;
58
+ }
59
+ updateDOM(prevNode, element, config) {
60
+ const prevIDs = prevNode.__ids;
61
+ const nextIDs = this.__ids;
62
+ const prevIDsCount = prevIDs.length;
63
+ const nextIDsCount = nextIDs.length;
64
+ const overlapTheme = config.theme.markOverlap;
65
+ if (prevIDsCount !== nextIDsCount) {
66
+ if (prevIDsCount === 1) {
67
+ if (nextIDsCount === 2) {
68
+ addClassNamesToElement(element, overlapTheme);
69
+ }
70
+ } else if (nextIDsCount === 1) {
71
+ removeClassNamesFromElement(element, overlapTheme);
72
+ }
73
+ }
74
+ return false;
75
+ }
76
+ hasID(id) {
77
+ return this.getIDs().includes(id);
78
+ }
79
+ getIDs() {
80
+ return Array.from(this.getLatest().__ids);
81
+ }
82
+ setIDs(ids) {
83
+ const self = this.getWritable();
84
+ self.__ids = ids;
85
+ return self;
86
+ }
87
+ addID(id) {
88
+ const self = this.getWritable();
89
+ return self.__ids.includes(id) ? self : self.setIDs([...self.__ids, id]);
90
+ }
91
+ deleteID(id) {
92
+ const self = this.getWritable();
93
+ const idx = self.__ids.indexOf(id);
94
+ if (idx === -1) {
95
+ return self;
96
+ }
97
+ const ids = Array.from(self.__ids);
98
+ ids.splice(idx, 1);
99
+ return self.setIDs(ids);
100
+ }
101
+ insertNewAfter(selection, restoreSelection = true) {
102
+ const markNode = $createMarkNode(this.__ids);
103
+ this.insertAfter(markNode, restoreSelection);
104
+ return markNode;
105
+ }
106
+ canInsertTextBefore() {
107
+ return false;
108
+ }
109
+ canInsertTextAfter() {
110
+ return false;
111
+ }
112
+ canBeEmpty() {
113
+ return false;
114
+ }
115
+ isInline() {
116
+ return true;
117
+ }
118
+ extractWithChild(child, selection, destination) {
119
+ if (!$isRangeSelection(selection) || destination === 'html') {
120
+ return false;
121
+ }
122
+ const anchor = selection.anchor;
123
+ const focus = selection.focus;
124
+ const anchorNode = anchor.getNode();
125
+ const focusNode = focus.getNode();
126
+ const isBackward = selection.isBackward();
127
+ const selectionLength = isBackward ? anchor.offset - focus.offset : focus.offset - anchor.offset;
128
+ return this.isParentOf(anchorNode) && this.isParentOf(focusNode) && this.getTextContent().length === selectionLength;
129
+ }
130
+ excludeFromCopy(destination) {
131
+ return destination !== 'clone';
132
+ }
133
+ }
134
+ function $createMarkNode(ids = NO_IDS) {
135
+ return $applyNodeReplacement(new MarkNode(ids));
136
+ }
137
+ function $isMarkNode(node) {
138
+ return node instanceof MarkNode;
139
+ }
140
+
141
+ /**
142
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
143
+ *
144
+ * This source code is licensed under the MIT license found in the
145
+ * LICENSE file in the root directory of this source tree.
146
+ *
147
+ */
148
+
149
+ function $unwrapMarkNode(node) {
150
+ const children = node.getChildren();
151
+ let target = null;
152
+ for (let i = 0; i < children.length; i++) {
153
+ const child = children[i];
154
+ if (target === null) {
155
+ node.insertBefore(child);
156
+ } else {
157
+ target.insertAfter(child);
158
+ }
159
+ target = child;
160
+ }
161
+ node.remove();
162
+ }
163
+ function $wrapSelectionInMarkNode(selection, isBackward, id, createNode) {
164
+ // Force a forwards selection since append is used, ignore the argument.
165
+ // A new selection is used to avoid side-effects of flipping the given
166
+ // selection
167
+ const forwardSelection = $createRangeSelection();
168
+ const [startPoint, endPoint] = selection.isBackward() ? [selection.focus, selection.anchor] : [selection.anchor, selection.focus];
169
+ forwardSelection.anchor.set(startPoint.key, startPoint.offset, startPoint.type);
170
+ forwardSelection.focus.set(endPoint.key, endPoint.offset, endPoint.type);
171
+ let currentNodeParent;
172
+ let lastCreatedMarkNode;
173
+
174
+ // Note that extract will split text nodes at the boundaries
175
+ const nodes = forwardSelection.extract();
176
+ // We only want wrap adjacent text nodes, line break nodes
177
+ // and inline element nodes. For decorator nodes and block
178
+ // element nodes, we step out of their boundary and start
179
+ // again after, if there are more nodes.
180
+ for (const node of nodes) {
181
+ if ($isElementNode(lastCreatedMarkNode) && lastCreatedMarkNode.isParentOf(node)) {
182
+ // If the current node is a child of the last created mark node, there is nothing to do here
183
+ continue;
184
+ }
185
+ let targetNode = null;
186
+ if ($isTextNode(node)) {
187
+ // Case 1: The node is a text node and we can include it
188
+ targetNode = node;
189
+ } else if ($isMarkNode(node)) {
190
+ // Case 2: the node is a mark node and we can ignore it as a target,
191
+ // moving on to its children. Note that when we make a mark inside
192
+ // another mark, it may ultimately be unnested by a call to
193
+ // `registerNestedElementResolver<MarkNode>` somewhere else in the
194
+ // codebase.
195
+ continue;
196
+ } else if (($isElementNode(node) || $isDecoratorNode(node)) && node.isInline()) {
197
+ // Case 3: inline element/decorator nodes can be added in their entirety
198
+ // to the new mark
199
+ targetNode = node;
200
+ }
201
+ if (targetNode !== null) {
202
+ // Now that we have a target node for wrapping with a mark, we can run
203
+ // through special cases.
204
+ if (targetNode && targetNode.is(currentNodeParent)) {
205
+ // The current node is a child of the target node to be wrapped, there
206
+ // is nothing to do here.
207
+ continue;
208
+ }
209
+ const parentNode = targetNode.getParent();
210
+ if (parentNode == null || !parentNode.is(currentNodeParent)) {
211
+ // If the parent node is not the current node's parent node, we can
212
+ // clear the last created mark node.
213
+ lastCreatedMarkNode = undefined;
214
+ }
215
+ currentNodeParent = parentNode;
216
+ if (lastCreatedMarkNode === undefined) {
217
+ // If we don't have a created mark node, we can make one
218
+ const createMarkNode = createNode || $createMarkNode;
219
+ lastCreatedMarkNode = createMarkNode([id]);
220
+ targetNode.insertBefore(lastCreatedMarkNode);
221
+ }
222
+
223
+ // Add the target node to be wrapped in the latest created mark node
224
+ lastCreatedMarkNode.append(targetNode);
225
+ } else {
226
+ // If we don't have a target node to wrap we can clear our state and
227
+ // continue on with the next node
228
+ currentNodeParent = undefined;
229
+ lastCreatedMarkNode = undefined;
230
+ }
231
+ }
232
+ // Make selection collapsed at the end
233
+ if ($isElementNode(lastCreatedMarkNode)) {
234
+ if (isBackward) {
235
+ lastCreatedMarkNode.selectStart();
236
+ } else {
237
+ lastCreatedMarkNode.selectEnd();
238
+ }
239
+ }
240
+ }
241
+ function $getMarkIDs(node, offset) {
242
+ let currentNode = node;
243
+ while (currentNode !== null) {
244
+ if ($isMarkNode(currentNode)) {
245
+ return currentNode.getIDs();
246
+ } else if ($isTextNode(currentNode) && offset === currentNode.getTextContentSize()) {
247
+ const nextSibling = currentNode.getNextSibling();
248
+ if ($isMarkNode(nextSibling)) {
249
+ return nextSibling.getIDs();
250
+ }
251
+ }
252
+ currentNode = currentNode.getParent();
253
+ }
254
+ return null;
255
+ }
256
+
257
+ /**
258
+ * Configures {@link MarkNode}
259
+ */
260
+ const MarkExtension = defineExtension({
261
+ name: '@ekz/lexical-mark',
262
+ nodes: () => [MarkNode]
263
+ });
264
+
265
+ export { $createMarkNode, $getMarkIDs, $isMarkNode, $unwrapMarkNode, $wrapSelectionInMarkNode, MarkExtension, MarkNode };
@@ -0,0 +1,11 @@
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
+ 'use strict'
10
+ const EkzLexicalMark = process.env.NODE_ENV !== 'production' ? require('./EkzLexicalMark.dev.js') : require('./EkzLexicalMark.prod.js');
11
+ module.exports = EkzLexicalMark;
@@ -0,0 +1,18 @@
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 * as modDev from './EkzLexicalMark.dev.mjs';
10
+ import * as modProd from './EkzLexicalMark.prod.mjs';
11
+ const mod = process.env.NODE_ENV !== 'production' ? modDev : modProd;
12
+ export const $createMarkNode = mod.$createMarkNode;
13
+ export const $getMarkIDs = mod.$getMarkIDs;
14
+ export const $isMarkNode = mod.$isMarkNode;
15
+ export const $unwrapMarkNode = mod.$unwrapMarkNode;
16
+ export const $wrapSelectionInMarkNode = mod.$wrapSelectionInMarkNode;
17
+ export const MarkExtension = mod.MarkExtension;
18
+ export const MarkNode = mod.MarkNode;
@@ -0,0 +1,16 @@
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
+ const mod = await (process.env.NODE_ENV !== 'production' ? import('./EkzLexicalMark.dev.mjs') : import('./EkzLexicalMark.prod.mjs'));
10
+ export const $createMarkNode = mod.$createMarkNode;
11
+ export const $getMarkIDs = mod.$getMarkIDs;
12
+ export const $isMarkNode = mod.$isMarkNode;
13
+ export const $unwrapMarkNode = mod.$unwrapMarkNode;
14
+ export const $wrapSelectionInMarkNode = mod.$wrapSelectionInMarkNode;
15
+ export const MarkExtension = mod.MarkExtension;
16
+ export const MarkNode = mod.MarkNode;
@@ -0,0 +1,9 @@
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
+ "use strict";var e=require("@ekz/lexical"),t=require("@ekz/lexical-utils");const r=[];class s extends e.ElementNode{__ids;static getType(){return"mark"}static clone(e){return new s(e.__ids,e.__key)}static importDOM(){return null}static importJSON(e){return n().updateFromJSON(e)}updateFromJSON(e){return super.updateFromJSON(e).setIDs(e.ids)}exportJSON(){return{...super.exportJSON(),ids:this.getIDs()}}constructor(e=r,t){super(t),this.__ids=e}createDOM(e){const r=document.createElement("mark");return t.addClassNamesToElement(r,e.theme.mark),this.__ids.length>1&&t.addClassNamesToElement(r,e.theme.markOverlap),r}updateDOM(e,r,s){const n=e.__ids,i=this.__ids,o=n.length,a=i.length,l=s.theme.markOverlap;return o!==a&&(1===o?2===a&&t.addClassNamesToElement(r,l):1===a&&t.removeClassNamesFromElement(r,l)),!1}hasID(e){return this.getIDs().includes(e)}getIDs(){return Array.from(this.getLatest().__ids)}setIDs(e){const t=this.getWritable();return t.__ids=e,t}addID(e){const t=this.getWritable();return t.__ids.includes(e)?t:t.setIDs([...t.__ids,e])}deleteID(e){const t=this.getWritable(),r=t.__ids.indexOf(e);if(-1===r)return t;const s=Array.from(t.__ids);return s.splice(r,1),t.setIDs(s)}insertNewAfter(e,t=!0){const r=n(this.__ids);return this.insertAfter(r,t),r}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}extractWithChild(t,r,s){if(!e.$isRangeSelection(r)||"html"===s)return!1;const n=r.anchor,i=r.focus,o=n.getNode(),a=i.getNode(),l=r.isBackward()?n.offset-i.offset:i.offset-n.offset;return this.isParentOf(o)&&this.isParentOf(a)&&this.getTextContent().length===l}excludeFromCopy(e){return"clone"!==e}}function n(t=r){return e.$applyNodeReplacement(new s(t))}function i(e){return e instanceof s}const o=e.defineExtension({name:"@ekz/lexical-mark",nodes:()=>[s]});exports.$createMarkNode=n,exports.$getMarkIDs=function(t,r){let s=t;for(;null!==s;){if(i(s))return s.getIDs();if(e.$isTextNode(s)&&r===s.getTextContentSize()){const e=s.getNextSibling();if(i(e))return e.getIDs()}s=s.getParent()}return null},exports.$isMarkNode=i,exports.$unwrapMarkNode=function(e){const t=e.getChildren();let r=null;for(let s=0;s<t.length;s++){const n=t[s];null===r?e.insertBefore(n):r.insertAfter(n),r=n}e.remove()},exports.$wrapSelectionInMarkNode=function(t,r,s,o){const a=e.$createRangeSelection(),[l,c]=t.isBackward()?[t.focus,t.anchor]:[t.anchor,t.focus];let u,d;a.anchor.set(l.key,l.offset,l.type),a.focus.set(c.key,c.offset,c.type);const f=a.extract();for(const t of f){if(e.$isElementNode(d)&&d.isParentOf(t))continue;let r=null;if(e.$isTextNode(t))r=t;else{if(i(t))continue;(e.$isElementNode(t)||e.$isDecoratorNode(t))&&t.isInline()&&(r=t)}if(null!==r){if(r&&r.is(u))continue;const e=r.getParent();if(null!=e&&e.is(u)||(d=void 0),u=e,void 0===d){d=(o||n)([s]),r.insertBefore(d)}d.append(r)}else u=void 0,d=void 0}e.$isElementNode(d)&&(r?d.selectStart():d.selectEnd())},exports.MarkExtension=o,exports.MarkNode=s;
@@ -0,0 +1,9 @@
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{ElementNode as t,$isRangeSelection as e,$applyNodeReplacement as r,defineExtension as n,$createRangeSelection as s,$isElementNode as i,$isTextNode as o,$isDecoratorNode as c}from"@ekz/lexical";import{addClassNamesToElement as u,removeClassNamesFromElement as l}from"@ekz/lexical-utils";const a=[];class f extends t{__ids;static getType(){return"mark"}static clone(t){return new f(t.__ids,t.__key)}static importDOM(){return null}static importJSON(t){return d().updateFromJSON(t)}updateFromJSON(t){return super.updateFromJSON(t).setIDs(t.ids)}exportJSON(){return{...super.exportJSON(),ids:this.getIDs()}}constructor(t=a,e){super(e),this.__ids=t}createDOM(t){const e=document.createElement("mark");return u(e,t.theme.mark),this.__ids.length>1&&u(e,t.theme.markOverlap),e}updateDOM(t,e,r){const n=t.__ids,s=this.__ids,i=n.length,o=s.length,c=r.theme.markOverlap;return i!==o&&(1===i?2===o&&u(e,c):1===o&&l(e,c)),!1}hasID(t){return this.getIDs().includes(t)}getIDs(){return Array.from(this.getLatest().__ids)}setIDs(t){const e=this.getWritable();return e.__ids=t,e}addID(t){const e=this.getWritable();return e.__ids.includes(t)?e:e.setIDs([...e.__ids,t])}deleteID(t){const e=this.getWritable(),r=e.__ids.indexOf(t);if(-1===r)return e;const n=Array.from(e.__ids);return n.splice(r,1),e.setIDs(n)}insertNewAfter(t,e=!0){const r=d(this.__ids);return this.insertAfter(r,e),r}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}extractWithChild(t,r,n){if(!e(r)||"html"===n)return!1;const s=r.anchor,i=r.focus,o=s.getNode(),c=i.getNode(),u=r.isBackward()?s.offset-i.offset:i.offset-s.offset;return this.isParentOf(o)&&this.isParentOf(c)&&this.getTextContent().length===u}excludeFromCopy(t){return"clone"!==t}}function d(t=a){return r(new f(t))}function h(t){return t instanceof f}function _(t){const e=t.getChildren();let r=null;for(let n=0;n<e.length;n++){const s=e[n];null===r?t.insertBefore(s):r.insertAfter(s),r=s}t.remove()}function m(t,e,r,n){const u=s(),[l,a]=t.isBackward()?[t.focus,t.anchor]:[t.anchor,t.focus];let f,_;u.anchor.set(l.key,l.offset,l.type),u.focus.set(a.key,a.offset,a.type);const m=u.extract();for(const t of m){if(i(_)&&_.isParentOf(t))continue;let e=null;if(o(t))e=t;else{if(h(t))continue;(i(t)||c(t))&&t.isInline()&&(e=t)}if(null!==e){if(e&&e.is(f))continue;const t=e.getParent();if(null!=t&&t.is(f)||(_=void 0),f=t,void 0===_){_=(n||d)([r]),e.insertBefore(_)}_.append(e)}else f=void 0,_=void 0}i(_)&&(e?_.selectStart():_.selectEnd())}function g(t,e){let r=t;for(;null!==r;){if(h(r))return r.getIDs();if(o(r)&&e===r.getTextContentSize()){const t=r.getNextSibling();if(h(t))return t.getIDs()}r=r.getParent()}return null}const p=n({name:"@ekz/lexical-mark",nodes:()=>[f]});export{d as $createMarkNode,g as $getMarkIDs,h as $isMarkNode,_ as $unwrapMarkNode,m as $wrapSelectionInMarkNode,p as MarkExtension,f as MarkNode};
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,60 @@
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
+ * @flow strict
8
+ */
9
+
10
+ import type {
11
+ NodeKey,
12
+ RangeSelection,
13
+ TextNode,
14
+ SerializedElementNode,
15
+ LexicalExtension,
16
+ ExtensionConfigBase,
17
+ } from '@ekz/lexical';
18
+ import {ElementNode, LexicalNode} from '@ekz/lexical';
19
+
20
+ export type SerializedMarkNode = SerializedElementNode & {
21
+ ids: Array<string>,
22
+ };
23
+
24
+ declare export class MarkNode extends ElementNode {
25
+ __ids: Array<string>;
26
+
27
+ // $FlowFixMe[incompatible-variance]
28
+ static clone(node: this): MarkNode;
29
+ constructor(ids: Array<string>, key?: NodeKey): void;
30
+
31
+ hasID(id: string): boolean;
32
+ getIDs(): Array<string>;
33
+ addID(id: string): void;
34
+ deleteID(id: string): void;
35
+ canInsertTextBefore(): false;
36
+ canInsertTextAfter(): false;
37
+ isInline(): true;
38
+ }
39
+
40
+ declare export function $isMarkNode(
41
+ node: ?LexicalNode,
42
+ ): node is MarkNode;
43
+
44
+ declare export function $createMarkNode(ids: Array<string>): MarkNode;
45
+
46
+ declare export function $getMarkIDs(
47
+ node: TextNode,
48
+ offset: number,
49
+ ): null | Array<string>;
50
+
51
+ declare export function $wrapSelectionInMarkNode(
52
+ selection: RangeSelection,
53
+ isBackward: boolean,
54
+ id: string,
55
+ createNode?: (ids: Array<string>) => MarkNode,
56
+ ): void;
57
+
58
+ declare export function $unwrapMarkNode(node: MarkNode): void;
59
+
60
+ declare export var MarkExtension: LexicalExtension<ExtensionConfigBase, "@ekz/lexical-mark", void, void>;
package/MarkNode.d.ts ADDED
@@ -0,0 +1,40 @@
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
+ import type { BaseSelection, EditorConfig, LexicalNode, LexicalUpdateJSON, NodeKey, RangeSelection, SerializedElementNode, Spread } from '@ekz/lexical';
9
+ import { ElementNode } from '@ekz/lexical';
10
+ export type SerializedMarkNode = Spread<{
11
+ ids: Array<string>;
12
+ }, SerializedElementNode>;
13
+ /** @noInheritDoc */
14
+ export declare class MarkNode extends ElementNode {
15
+ /** @internal */
16
+ __ids: readonly string[];
17
+ static getType(): string;
18
+ static clone(node: MarkNode): MarkNode;
19
+ static importDOM(): null;
20
+ static importJSON(serializedNode: SerializedMarkNode): MarkNode;
21
+ updateFromJSON(serializedNode: LexicalUpdateJSON<SerializedMarkNode>): this;
22
+ exportJSON(): SerializedMarkNode;
23
+ constructor(ids?: readonly string[], key?: NodeKey);
24
+ createDOM(config: EditorConfig): HTMLElement;
25
+ updateDOM(prevNode: this, element: HTMLElement, config: EditorConfig): boolean;
26
+ hasID(id: string): boolean;
27
+ getIDs(): Array<string>;
28
+ setIDs(ids: readonly string[]): this;
29
+ addID(id: string): this;
30
+ deleteID(id: string): this;
31
+ insertNewAfter(selection: RangeSelection, restoreSelection?: boolean): null | ElementNode;
32
+ canInsertTextBefore(): false;
33
+ canInsertTextAfter(): false;
34
+ canBeEmpty(): false;
35
+ isInline(): true;
36
+ extractWithChild(child: LexicalNode, selection: BaseSelection, destination: 'clone' | 'html'): boolean;
37
+ excludeFromCopy(destination: 'clone' | 'html'): boolean;
38
+ }
39
+ export declare function $createMarkNode(ids?: readonly string[]): MarkNode;
40
+ export declare function $isMarkNode(node: LexicalNode | null): node is MarkNode;
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # `@lexical/mark`
2
+
3
+ [![See API Documentation](https://lexical.dev/img/see-api-documentation.svg)](https://lexical.dev/docs/api/modules/lexical_mark)
4
+
5
+ This package contains helpers and nodes for wrapping content in marks for Lexical.
package/index.d.ts ADDED
@@ -0,0 +1,18 @@
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
+ import type { SerializedMarkNode } from './MarkNode';
9
+ import type { RangeSelection, TextNode } from '@ekz/lexical';
10
+ import { $createMarkNode, $isMarkNode, MarkNode } from './MarkNode';
11
+ export declare function $unwrapMarkNode(node: MarkNode): void;
12
+ export declare function $wrapSelectionInMarkNode(selection: RangeSelection, isBackward: boolean, id: string, createNode?: (ids: Array<string>) => MarkNode): void;
13
+ export declare function $getMarkIDs(node: TextNode, offset: number): null | Array<string>;
14
+ /**
15
+ * Configures {@link MarkNode}
16
+ */
17
+ export declare const MarkExtension: import("@ekz/lexical").LexicalExtension<import("@ekz/lexical").ExtensionConfigBase, "@ekz/lexical-mark", unknown, unknown>;
18
+ export { $createMarkNode, $isMarkNode, MarkNode, SerializedMarkNode };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@ekz/lexical-mark",
3
+ "description": "This package contains helpers and nodes for wrapping content in marks for Lexical.",
4
+ "keywords": [
5
+ "lexical",
6
+ "editor",
7
+ "rich-text",
8
+ "mark"
9
+ ],
10
+ "license": "MIT",
11
+ "version": "0.40.0",
12
+ "main": "LexicalMark.js",
13
+ "types": "index.d.ts",
14
+ "dependencies": {
15
+ "@ekz/lexical-utils": "0.40.0",
16
+ "@ekz/lexical": "0.40.0"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/facebook/lexical.git",
21
+ "directory": "packages/lexical-mark"
22
+ },
23
+ "module": "LexicalMark.mjs",
24
+ "sideEffects": false,
25
+ "exports": {
26
+ ".": {
27
+ "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"
33
+ },
34
+ "require": {
35
+ "types": "./index.d.ts",
36
+ "development": "./LexicalMark.dev.js",
37
+ "production": "./LexicalMark.prod.js",
38
+ "default": "./LexicalMark.js"
39
+ }
40
+ }
41
+ }
42
+ }