@lexical/mark 0.13.1 → 0.14.2
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/LexicalMark.dev.esm.js +268 -0
- package/LexicalMark.dev.js +3 -1
- package/LexicalMark.esm.js +15 -0
- package/LexicalMark.js +1 -1
- package/LexicalMark.prod.esm.js +7 -0
- package/README.md +2 -0
- package/package.json +6 -4
|
@@ -0,0 +1,268 @@
|
|
|
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
|
+
import { ElementNode, $applyNodeReplacement, $isRangeSelection, $isElementNode, $isTextNode } from 'lexical';
|
|
8
|
+
import { addClassNamesToElement, removeClassNamesFromElement } from '@lexical/utils';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
12
|
+
*
|
|
13
|
+
* This source code is licensed under the MIT license found in the
|
|
14
|
+
* LICENSE file in the root directory of this source tree.
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
/** @noInheritDoc */
|
|
18
|
+
class MarkNode extends ElementNode {
|
|
19
|
+
/** @internal */
|
|
20
|
+
|
|
21
|
+
static getType() {
|
|
22
|
+
return 'mark';
|
|
23
|
+
}
|
|
24
|
+
static clone(node) {
|
|
25
|
+
return new MarkNode(Array.from(node.__ids), node.__key);
|
|
26
|
+
}
|
|
27
|
+
static importDOM() {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
static importJSON(serializedNode) {
|
|
31
|
+
const node = $createMarkNode(serializedNode.ids);
|
|
32
|
+
node.setFormat(serializedNode.format);
|
|
33
|
+
node.setIndent(serializedNode.indent);
|
|
34
|
+
node.setDirection(serializedNode.direction);
|
|
35
|
+
return node;
|
|
36
|
+
}
|
|
37
|
+
exportJSON() {
|
|
38
|
+
return {
|
|
39
|
+
...super.exportJSON(),
|
|
40
|
+
ids: this.getIDs(),
|
|
41
|
+
type: 'mark',
|
|
42
|
+
version: 1
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
constructor(ids, key) {
|
|
46
|
+
super(key);
|
|
47
|
+
this.__ids = ids || [];
|
|
48
|
+
}
|
|
49
|
+
createDOM(config) {
|
|
50
|
+
const element = document.createElement('mark');
|
|
51
|
+
addClassNamesToElement(element, config.theme.mark);
|
|
52
|
+
if (this.__ids.length > 1) {
|
|
53
|
+
addClassNamesToElement(element, config.theme.markOverlap);
|
|
54
|
+
}
|
|
55
|
+
return element;
|
|
56
|
+
}
|
|
57
|
+
updateDOM(prevNode, element, config) {
|
|
58
|
+
const prevIDs = prevNode.__ids;
|
|
59
|
+
const nextIDs = this.__ids;
|
|
60
|
+
const prevIDsCount = prevIDs.length;
|
|
61
|
+
const nextIDsCount = nextIDs.length;
|
|
62
|
+
const overlapTheme = config.theme.markOverlap;
|
|
63
|
+
if (prevIDsCount !== nextIDsCount) {
|
|
64
|
+
if (prevIDsCount === 1) {
|
|
65
|
+
if (nextIDsCount === 2) {
|
|
66
|
+
addClassNamesToElement(element, overlapTheme);
|
|
67
|
+
}
|
|
68
|
+
} else if (nextIDsCount === 1) {
|
|
69
|
+
removeClassNamesFromElement(element, overlapTheme);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
hasID(id) {
|
|
75
|
+
const ids = this.getIDs();
|
|
76
|
+
for (let i = 0; i < ids.length; i++) {
|
|
77
|
+
if (id === ids[i]) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
getIDs() {
|
|
84
|
+
const self = this.getLatest();
|
|
85
|
+
return $isMarkNode(self) ? self.__ids : [];
|
|
86
|
+
}
|
|
87
|
+
addID(id) {
|
|
88
|
+
const self = this.getWritable();
|
|
89
|
+
if ($isMarkNode(self)) {
|
|
90
|
+
const ids = self.__ids;
|
|
91
|
+
self.__ids = ids;
|
|
92
|
+
for (let i = 0; i < ids.length; i++) {
|
|
93
|
+
// If we already have it, don't add again
|
|
94
|
+
if (id === ids[i]) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
ids.push(id);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
deleteID(id) {
|
|
102
|
+
const self = this.getWritable();
|
|
103
|
+
if ($isMarkNode(self)) {
|
|
104
|
+
const ids = self.__ids;
|
|
105
|
+
self.__ids = ids;
|
|
106
|
+
for (let i = 0; i < ids.length; i++) {
|
|
107
|
+
if (id === ids[i]) {
|
|
108
|
+
ids.splice(i, 1);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
insertNewAfter(selection, restoreSelection = true) {
|
|
115
|
+
const markNode = $createMarkNode(this.__ids);
|
|
116
|
+
this.insertAfter(markNode, restoreSelection);
|
|
117
|
+
return markNode;
|
|
118
|
+
}
|
|
119
|
+
canInsertTextBefore() {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
canInsertTextAfter() {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
canBeEmpty() {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
isInline() {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
extractWithChild(child, selection, destination) {
|
|
132
|
+
if (!$isRangeSelection(selection) || destination === 'html') {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
const anchor = selection.anchor;
|
|
136
|
+
const focus = selection.focus;
|
|
137
|
+
const anchorNode = anchor.getNode();
|
|
138
|
+
const focusNode = focus.getNode();
|
|
139
|
+
const isBackward = selection.isBackward();
|
|
140
|
+
const selectionLength = isBackward ? anchor.offset - focus.offset : focus.offset - anchor.offset;
|
|
141
|
+
return this.isParentOf(anchorNode) && this.isParentOf(focusNode) && this.getTextContent().length === selectionLength;
|
|
142
|
+
}
|
|
143
|
+
excludeFromCopy(destination) {
|
|
144
|
+
return destination !== 'clone';
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function $createMarkNode(ids) {
|
|
148
|
+
return $applyNodeReplacement(new MarkNode(ids));
|
|
149
|
+
}
|
|
150
|
+
function $isMarkNode(node) {
|
|
151
|
+
return node instanceof MarkNode;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** @module @lexical/mark */
|
|
155
|
+
function $unwrapMarkNode(node) {
|
|
156
|
+
const children = node.getChildren();
|
|
157
|
+
let target = null;
|
|
158
|
+
for (let i = 0; i < children.length; i++) {
|
|
159
|
+
const child = children[i];
|
|
160
|
+
if (target === null) {
|
|
161
|
+
node.insertBefore(child);
|
|
162
|
+
} else {
|
|
163
|
+
target.insertAfter(child);
|
|
164
|
+
}
|
|
165
|
+
target = child;
|
|
166
|
+
}
|
|
167
|
+
node.remove();
|
|
168
|
+
}
|
|
169
|
+
function $wrapSelectionInMarkNode(selection, isBackward, id, createNode) {
|
|
170
|
+
const nodes = selection.getNodes();
|
|
171
|
+
const anchorOffset = selection.anchor.offset;
|
|
172
|
+
const focusOffset = selection.focus.offset;
|
|
173
|
+
const nodesLength = nodes.length;
|
|
174
|
+
const startOffset = isBackward ? focusOffset : anchorOffset;
|
|
175
|
+
const endOffset = isBackward ? anchorOffset : focusOffset;
|
|
176
|
+
let currentNodeParent;
|
|
177
|
+
let lastCreatedMarkNode;
|
|
178
|
+
|
|
179
|
+
// We only want wrap adjacent text nodes, line break nodes
|
|
180
|
+
// and inline element nodes. For decorator nodes and block
|
|
181
|
+
// element nodes, we step out of their boundary and start
|
|
182
|
+
// again after, if there are more nodes.
|
|
183
|
+
for (let i = 0; i < nodesLength; i++) {
|
|
184
|
+
const node = nodes[i];
|
|
185
|
+
if ($isElementNode(lastCreatedMarkNode) && lastCreatedMarkNode.isParentOf(node)) {
|
|
186
|
+
// If the current node is a child of the last created mark node, there is nothing to do here
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const isFirstNode = i === 0;
|
|
190
|
+
const isLastNode = i === nodesLength - 1;
|
|
191
|
+
let targetNode = null;
|
|
192
|
+
if ($isTextNode(node)) {
|
|
193
|
+
// Case 1: The node is a text node and we can split it
|
|
194
|
+
const textContentSize = node.getTextContentSize();
|
|
195
|
+
const startTextOffset = isFirstNode ? startOffset : 0;
|
|
196
|
+
const endTextOffset = isLastNode ? endOffset : textContentSize;
|
|
197
|
+
if (startTextOffset === 0 && endTextOffset === 0) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const splitNodes = node.splitText(startTextOffset, endTextOffset);
|
|
201
|
+
targetNode = splitNodes.length > 1 && (splitNodes.length === 3 || isFirstNode && !isLastNode || endTextOffset === textContentSize) ? splitNodes[1] : splitNodes[0];
|
|
202
|
+
} else if ($isMarkNode(node)) {
|
|
203
|
+
// Case 2: the node is a mark node and we can ignore it as a target,
|
|
204
|
+
// moving on to its children. Note that when we make a mark inside
|
|
205
|
+
// another mark, it may utlimately be unnested by a call to
|
|
206
|
+
// `registerNestedElementResolver<MarkNode>` somewhere else in the
|
|
207
|
+
// codebase.
|
|
208
|
+
|
|
209
|
+
continue;
|
|
210
|
+
} else if ($isElementNode(node) && node.isInline()) {
|
|
211
|
+
// Case 3: inline element nodes can be added in their entirety to the new
|
|
212
|
+
// mark
|
|
213
|
+
targetNode = node;
|
|
214
|
+
}
|
|
215
|
+
if (targetNode !== null) {
|
|
216
|
+
// Now that we have a target node for wrapping with a mark, we can run
|
|
217
|
+
// through special cases.
|
|
218
|
+
if (targetNode && targetNode.is(currentNodeParent)) {
|
|
219
|
+
// The current node is a child of the target node to be wrapped, there
|
|
220
|
+
// is nothing to do here.
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const parentNode = targetNode.getParent();
|
|
224
|
+
if (parentNode == null || !parentNode.is(currentNodeParent)) {
|
|
225
|
+
// If the parent node is not the current node's parent node, we can
|
|
226
|
+
// clear the last created mark node.
|
|
227
|
+
lastCreatedMarkNode = undefined;
|
|
228
|
+
}
|
|
229
|
+
currentNodeParent = parentNode;
|
|
230
|
+
if (lastCreatedMarkNode === undefined) {
|
|
231
|
+
// If we don't have a created mark node, we can make one
|
|
232
|
+
const createMarkNode = createNode || $createMarkNode;
|
|
233
|
+
lastCreatedMarkNode = createMarkNode([id]);
|
|
234
|
+
targetNode.insertBefore(lastCreatedMarkNode);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Add the target node to be wrapped in the latest created mark node
|
|
238
|
+
lastCreatedMarkNode.append(targetNode);
|
|
239
|
+
} else {
|
|
240
|
+
// If we don't have a target node to wrap we can clear our state and
|
|
241
|
+
// continue on with the next node
|
|
242
|
+
currentNodeParent = undefined;
|
|
243
|
+
lastCreatedMarkNode = undefined;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// Make selection collapsed at the end
|
|
247
|
+
if ($isElementNode(lastCreatedMarkNode)) {
|
|
248
|
+
// eslint-disable-next-line no-unused-expressions
|
|
249
|
+
isBackward ? lastCreatedMarkNode.selectStart() : lastCreatedMarkNode.selectEnd();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
function $getMarkIDs(node, offset) {
|
|
253
|
+
let currentNode = node;
|
|
254
|
+
while (currentNode !== null) {
|
|
255
|
+
if ($isMarkNode(currentNode)) {
|
|
256
|
+
return currentNode.getIDs();
|
|
257
|
+
} else if ($isTextNode(currentNode) && offset === currentNode.getTextContentSize()) {
|
|
258
|
+
const nextSibling = currentNode.getNextSibling();
|
|
259
|
+
if ($isMarkNode(nextSibling)) {
|
|
260
|
+
return nextSibling.getIDs();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
currentNode = currentNode.getParent();
|
|
264
|
+
}
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export { $createMarkNode, $getMarkIDs, $isMarkNode, $unwrapMarkNode, $wrapSelectionInMarkNode, MarkNode };
|
package/LexicalMark.dev.js
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
import * as modDev from './LexicalMark.dev.esm.js';
|
|
8
|
+
import * as modProd from './LexicalMark.prod.esm.js';
|
|
9
|
+
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
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 MarkNode = mod.MarkNode;
|
package/LexicalMark.js
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
'use strict'
|
|
8
|
-
const LexicalMark = process.env.NODE_ENV === 'development' ? require('./LexicalMark.dev.js') : require('./LexicalMark.prod.js')
|
|
8
|
+
const LexicalMark = process.env.NODE_ENV === 'development' ? require('./LexicalMark.dev.js') : require('./LexicalMark.prod.js');
|
|
9
9
|
module.exports = LexicalMark;
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
import{ElementNode as t,$applyNodeReplacement as e,$isRangeSelection as n,$isElementNode as r,$isTextNode as i}from"lexical";import{addClassNamesToElement as s,removeClassNamesFromElement as o}from"@lexical/utils";class l extends t{static getType(){return"mark"}static clone(t){return new l(Array.from(t.__ids),t.__key)}static importDOM(){return null}static importJSON(t){const e=c(t.ids);return e.setFormat(t.format),e.setIndent(t.indent),e.setDirection(t.direction),e}exportJSON(){return{...super.exportJSON(),ids:this.getIDs(),type:"mark",version:1}}constructor(t,e){super(e),this.__ids=t||[]}createDOM(t){const e=document.createElement("mark");return s(e,t.theme.mark),this.__ids.length>1&&s(e,t.theme.markOverlap),e}updateDOM(t,e,n){const r=t.__ids,i=this.__ids,l=r.length,c=i.length,f=n.theme.markOverlap;return l!==c&&(1===l?2===c&&s(e,f):1===c&&o(e,f)),!1}hasID(t){const e=this.getIDs();for(let n=0;n<e.length;n++)if(t===e[n])return!0;return!1}getIDs(){const t=this.getLatest();return f(t)?t.__ids:[]}addID(t){const e=this.getWritable();if(f(e)){const n=e.__ids;e.__ids=n;for(let e=0;e<n.length;e++)if(t===n[e])return;n.push(t)}}deleteID(t){const e=this.getWritable();if(f(e)){const n=e.__ids;e.__ids=n;for(let e=0;e<n.length;e++)if(t===n[e])return void n.splice(e,1)}}insertNewAfter(t,e=!0){const n=c(this.__ids);return this.insertAfter(n,e),n}canInsertTextBefore(){return!1}canInsertTextAfter(){return!1}canBeEmpty(){return!1}isInline(){return!0}extractWithChild(t,e,r){if(!n(e)||"html"===r)return!1;const i=e.anchor,s=e.focus,o=i.getNode(),l=s.getNode(),c=e.isBackward()?i.offset-s.offset:s.offset-i.offset;return this.isParentOf(o)&&this.isParentOf(l)&&this.getTextContent().length===c}excludeFromCopy(t){return"clone"!==t}}function c(t){return e(new l(t))}function f(t){return t instanceof l}function u(t){const e=t.getChildren();let n=null;for(let r=0;r<e.length;r++){const i=e[r];null===n?t.insertBefore(i):n.insertAfter(i),n=i}t.remove()}function a(t,e,n,s){const o=t.getNodes(),l=t.anchor.offset,u=t.focus.offset,a=o.length,d=e?u:l,h=e?l:u;let g,m;for(let t=0;t<a;t++){const e=o[t];if(r(m)&&m.isParentOf(e))continue;const l=0===t,u=t===a-1;let _=null;if(i(e)){const t=e.getTextContentSize(),n=l?d:0,r=u?h:t;if(0===n&&0===r)continue;const i=e.splitText(n,r);_=i.length>1&&(3===i.length||l&&!u||r===t)?i[1]:i[0]}else{if(f(e))continue;r(e)&&e.isInline()&&(_=e)}if(null!==_){if(_&&_.is(g))continue;const t=_.getParent();if(null!=t&&t.is(g)||(m=void 0),g=t,void 0===m){m=(s||c)([n]),_.insertBefore(m)}m.append(_)}else g=void 0,m=void 0}r(m)&&(e?m.selectStart():m.selectEnd())}function d(t,e){let n=t;for(;null!==n;){if(f(n))return n.getIDs();if(i(n)&&e===n.getTextContentSize()){const t=n.getNextSibling();if(f(t))return t.getIDs()}n=n.getParent()}return null}export{c as $createMarkNode,d as $getMarkIDs,f as $isMarkNode,u as $unwrapMarkNode,a as $wrapSelectionInMarkNode,l as MarkNode};
|
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -8,17 +8,19 @@
|
|
|
8
8
|
"mark"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.
|
|
11
|
+
"version": "0.14.2",
|
|
12
12
|
"main": "LexicalMark.js",
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"lexical": "0.
|
|
14
|
+
"lexical": "0.14.2"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/utils": "0.
|
|
17
|
+
"@lexical/utils": "0.14.2"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
21
|
"url": "https://github.com/facebook/lexical",
|
|
22
22
|
"directory": "packages/lexical-mark"
|
|
23
|
-
}
|
|
23
|
+
},
|
|
24
|
+
"module": "LexicalMark.esm.js",
|
|
25
|
+
"sideEffects": false
|
|
24
26
|
}
|