@toptal/picasso-rich-text-editor 4.1.0 → 4.2.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
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { $isLinkNode, TOGGLE_LINK_COMMAND } from '@lexical/link';
|
|
1
|
+
import { $isLinkNode, TOGGLE_LINK_COMMAND, $createLinkNode, } from '@lexical/link';
|
|
2
2
|
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
3
3
|
import { Link16 } from '@toptal/picasso';
|
|
4
|
-
import { $getSelection, $isRangeSelection } from 'lexical';
|
|
4
|
+
import { $createTextNode, $getSelection, $isRangeSelection } from 'lexical';
|
|
5
5
|
import React, { useCallback, useState } from 'react';
|
|
6
6
|
import { getSelectedNode } from '../../LexicalEditor/utils/getSelectedNode';
|
|
7
7
|
import { useRTEPluginContext, useRTEUpdate } from '../api';
|
|
@@ -23,13 +23,40 @@ const LinkPluginButton = ({ 'data-testid': testId }) => {
|
|
|
23
23
|
if (active) {
|
|
24
24
|
return editor.dispatchCommand(TOGGLE_LINK_COMMAND, null);
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
|
|
26
|
+
editor.update(() => {
|
|
27
|
+
const selection = $getSelection();
|
|
28
|
+
if ($isRangeSelection(selection)) {
|
|
29
|
+
const isEmptySelection = selection.anchor.is(selection.focus);
|
|
30
|
+
const url = window.prompt('URL');
|
|
31
|
+
if (url != null) {
|
|
32
|
+
if (!validateUrl(url)) {
|
|
33
|
+
return window.alert('Not a valid URL');
|
|
34
|
+
}
|
|
35
|
+
const sanitizedUrl = sanitizeUrl(url);
|
|
36
|
+
// When nothing is selected, we create a new Link node without dispatching
|
|
37
|
+
// any commands to the original Lexical Link plugin
|
|
38
|
+
if (isEmptySelection) {
|
|
39
|
+
// The only way to reliably insert a link is to first create a dummy text node
|
|
40
|
+
selection.insertNodes([$createTextNode(sanitizedUrl)]);
|
|
41
|
+
// Once created, node becomes selected
|
|
42
|
+
const node = getSelectedNode(selection);
|
|
43
|
+
const text = node.getTextContent();
|
|
44
|
+
// Then we create a target Link node and replace the dummy text node with it
|
|
45
|
+
const linkNode = $createLinkNode(sanitizedUrl, {
|
|
46
|
+
rel: 'noreferrer',
|
|
47
|
+
});
|
|
48
|
+
linkNode.append($createTextNode(text));
|
|
49
|
+
node.replace(linkNode);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
// If we have a selection of any kind, pass the creation of the Link node to the plugin
|
|
53
|
+
editor.dispatchCommand(TOGGLE_LINK_COMMAND, {
|
|
54
|
+
url: sanitizedUrl,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
30
58
|
}
|
|
31
|
-
|
|
32
|
-
}
|
|
59
|
+
});
|
|
33
60
|
}, [editor, active]);
|
|
34
61
|
return (React.createElement(RichTextEditorButton, { icon: React.createElement(Link16, null), onClick: onLinkClick, active: active, disabled: disabled || !focused, "data-testid": testId }));
|
|
35
62
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LinkPluginButton.js","sourceRoot":"","sources":["../../../src/plugins/LinkPlugin/LinkPluginButton.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"LinkPluginButton.js","sourceRoot":"","sources":["../../../src/plugins/LinkPlugin/LinkPluginButton.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,mBAAmB,EACnB,eAAe,GAChB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,yBAAyB,EAAE,MAAM,uCAAuC,CAAA;AACjF,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AACxC,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAC3E,OAAO,KAAK,EAAE,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,OAAO,C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