@lexical/hashtag 0.1.15
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/LICENSE +21 -0
- package/LexicalHashtag.d.ts +23 -0
- package/LexicalHashtag.dev.js +96 -0
- package/LexicalHashtag.js +9 -0
- package/LexicalHashtag.prod.js +9 -0
- package/README.md +5 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Dominic Gannaway
|
|
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,23 @@
|
|
|
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 {EditorConfig, LexicalNode, NodeKey} from 'lexical';
|
|
10
|
+
import {TextNode} from 'lexical';
|
|
11
|
+
|
|
12
|
+
export declare class HashtagNode extends TextNode {
|
|
13
|
+
getType(): string;
|
|
14
|
+
clone(node: HashtagNode): HashtagNode;
|
|
15
|
+
constructor(text: string, key?: NodeKey);
|
|
16
|
+
createDOM<EditorContext>(config: EditorConfig<EditorContext>): HTMLElement;
|
|
17
|
+
setTextContent(text: string): TextNode;
|
|
18
|
+
canInsertTextBefore(): boolean;
|
|
19
|
+
canInsertTextAfter(): boolean;
|
|
20
|
+
}
|
|
21
|
+
export function $toggleHashtag(node: TextNode): TextNode;
|
|
22
|
+
export function $createHashtagNode(text?: string): TextNode;
|
|
23
|
+
export function $isHashtagNode(node: LexicalNode | null | undefined): boolean;
|
|
@@ -0,0 +1,96 @@
|
|
|
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
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
var utils = require('@lexical/utils');
|
|
10
|
+
var lexical = require('lexical');
|
|
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
|
+
class HashtagNode extends lexical.TextNode {
|
|
21
|
+
static getType() {
|
|
22
|
+
return 'hashtag';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static clone(node) {
|
|
26
|
+
return new HashtagNode(node.__text, node.__key);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
constructor(text, key) {
|
|
30
|
+
super(text, key);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
createDOM(config) {
|
|
34
|
+
const element = super.createDOM(config);
|
|
35
|
+
utils.addClassNamesToElement(element, config.theme.hashtag);
|
|
36
|
+
return element;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
setTextContent(text) {
|
|
40
|
+
let targetNode = super.setTextContent(text); // Handle hashtags
|
|
41
|
+
|
|
42
|
+
if (targetNode.getParent() !== null && !targetNode.isComposing()) {
|
|
43
|
+
const indexOfHash = text.indexOf('#');
|
|
44
|
+
|
|
45
|
+
if (indexOfHash === -1 || targetNode.getTextContent() === '#') {
|
|
46
|
+
targetNode = $toggleHashtag(targetNode);
|
|
47
|
+
} else if (indexOfHash > 0) {
|
|
48
|
+
[targetNode] = targetNode.splitText(indexOfHash);
|
|
49
|
+
targetNode = $toggleHashtag(targetNode);
|
|
50
|
+
} // Check for invalid characters
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if (lexical.$isTextNode(targetNode) && targetNode.isAttached()) {
|
|
54
|
+
const targetTextContent = targetNode.getTextContent().slice(1);
|
|
55
|
+
const indexOfInvalidChar = targetTextContent.search(/[\s.,\\\/#!$%\^&\*;:{}=\-`~()@]/);
|
|
56
|
+
|
|
57
|
+
if (indexOfInvalidChar === 0) {
|
|
58
|
+
targetNode = $toggleHashtag(targetNode);
|
|
59
|
+
} else if (indexOfInvalidChar > 0) {
|
|
60
|
+
[, targetNode] = targetNode.splitText(indexOfInvalidChar + 1);
|
|
61
|
+
targetNode = $toggleHashtag(targetNode);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return targetNode;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
canInsertTextBefore() {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
canInsertTextAfter() {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
function $toggleHashtag(node) {
|
|
81
|
+
const text = node.getTextContent();
|
|
82
|
+
const replacement = !$isHashtagNode(node) ? $createHashtagNode(text) : lexical.$createTextNode(text);
|
|
83
|
+
node.replace(replacement);
|
|
84
|
+
return replacement;
|
|
85
|
+
}
|
|
86
|
+
function $createHashtagNode(text = '') {
|
|
87
|
+
return new HashtagNode(text);
|
|
88
|
+
}
|
|
89
|
+
function $isHashtagNode(node) {
|
|
90
|
+
return node instanceof HashtagNode;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
exports.$createHashtagNode = $createHashtagNode;
|
|
94
|
+
exports.$isHashtagNode = $isHashtagNode;
|
|
95
|
+
exports.$toggleHashtag = $toggleHashtag;
|
|
96
|
+
exports.HashtagNode = HashtagNode;
|
|
@@ -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
|
+
'use strict'
|
|
8
|
+
const LexicalHashtag = process.env.NODE_ENV === 'development' ? require('./LexicalHashtag.dev.js') : require('./LexicalHashtag.prod.js')
|
|
9
|
+
module.exports = LexicalHashtag;
|
|
@@ -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
|
+
var c=require("@lexical/utils"),d=require("lexical");
|
|
8
|
+
class e extends d.TextNode{static getType(){return"hashtag"}static clone(b){return new e(b.__text,b.__key)}constructor(b,a){super(b,a)}createDOM(b){const a=super.createDOM(b);c.addClassNamesToElement(a,b.theme.hashtag);return a}setTextContent(b){let a=super.setTextContent(b);return null===a.getParent()||a.isComposing()?this:(b=b.indexOf("#"),-1===b||"#"===a.getTextContent()?a=f(a):0<b&&([a]=a.splitText(b),a=f(a)),d.$isTextNode(a)&&a.isAttached()&&(b=a.getTextContent().slice(1).search(/[\s.,\\\/#!$%\^&\*;:{}=\-`~()@]/),
|
|
9
|
+
0===b?a=f(a):0<b&&([,a]=a.splitText(b+1),a=f(a))),a)}canInsertTextBefore(){return!1}canInsertTextAfter(){return!0}}function f(b){var a=b.getTextContent();a=g(b)?d.$createTextNode(a):h(a);b.replace(a);return a}function h(b=""){return new e(b)}function g(b){return b instanceof e}exports.$createHashtagNode=h;exports.$isHashtagNode=g;exports.$toggleHashtag=f;exports.HashtagNode=e;
|
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lexical/hashtag",
|
|
3
|
+
"author": {
|
|
4
|
+
"name": "Dominic Gannaway",
|
|
5
|
+
"email": "dg@domgan.com"
|
|
6
|
+
},
|
|
7
|
+
"description": "This package contains the functionality for Lexical hashtags.",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"lexical",
|
|
10
|
+
"editor",
|
|
11
|
+
"rich-text",
|
|
12
|
+
"table",
|
|
13
|
+
"hashtag"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"version": "0.1.15",
|
|
17
|
+
"main": "LexicalHashtag.js",
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"lexical": "0.1.15"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@lexical/utils": "0.1.15"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/facebook/lexical",
|
|
27
|
+
"directory": "packages/lexical-hashtag"
|
|
28
|
+
}
|
|
29
|
+
}
|