feat: Link feature add search function

This commit is contained in:
xiaodong zuo
2022-09-01 10:44:21 +08:00
parent 73d6e34c5d
commit e18a35989f
9 changed files with 367 additions and 2 deletions
+1
View File
@@ -19,6 +19,7 @@ declare module 'slate' {
}
}
export { default as isUrl } from 'is-url';
export { BlockPreview, StyledBlockPreview } from './block-preview';
export { default as Button } from './button';
export { CollapsibleTitle } from './collapsible-title';
@@ -2,6 +2,7 @@
import {
Descendant,
Editor,
Element as SlateElement,
Location,
Node as SlateNode,
Path,
@@ -1230,6 +1231,57 @@ class SlateUtils {
});
}
public wrapLink(url: string, preSelection?: Location) {
if (!ReactEditor.isFocused(this.editor) && preSelection) {
Transforms.select(this.editor, preSelection);
}
if (this.isLinkActive()) {
this.unwrapLink();
}
const { selection } = this.editor;
const isCollapsed = selection && this.isCollapsed();
const link = {
type: 'link',
url: url,
children: isCollapsed ? [{ text: url }] : [],
id: getRandomString('link'),
};
if (isCollapsed) {
Transforms.insertNodes(this.editor, link);
} else {
Transforms.wrapNodes(this.editor, link, { split: true });
Transforms.collapse(this.editor, { edge: 'end' });
}
requestAnimationFrame(() => {
ReactEditor.focus(this.editor);
});
}
public isLinkActive() {
const [link] = Editor.nodes(this.editor, {
match: n =>
!Editor.isEditor(n) &&
SlateElement.isElement(n) &&
// @ts-expect-error
n.type === 'link',
});
return !!link;
}
public unwrapLink() {
Transforms.unwrapNodes(this.editor, {
match: n => {
return (
!Editor.isEditor(n) &&
SlateElement.isElement(n) &&
// @ts-expect-error
n.type === 'link'
);
},
});
}
/** todo improve if selection is collapsed */
public getCommentsIdsBySelection() {
const commentedTextNodes = Editor.nodes(this.editor, {