mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-17 01:56:27 +08:00
feat: Link feature add search function
This commit is contained in:
@@ -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, {
|
||||
|
||||
Reference in New Issue
Block a user