fix: rare crash with link rte element
This commit is contained in:
@@ -1,41 +1,37 @@
|
||||
import { Editor, Transforms, Range, Element } from 'slate';
|
||||
|
||||
export const isLinkActive = (editor: Editor): boolean => {
|
||||
const [link] = Editor.nodes(editor, { match: (n) => Element.isElement(n) && n.type === 'link' });
|
||||
return !!link;
|
||||
};
|
||||
import isElementActive from '../isActive';
|
||||
|
||||
export const unwrapLink = (editor: Editor): void => {
|
||||
Transforms.unwrapNodes(editor, { match: (n) => Element.isElement(n) && n.type === 'link' });
|
||||
};
|
||||
|
||||
export const wrapLink = (editor, url?: string, newTab?: boolean): void => {
|
||||
if (isLinkActive(editor)) {
|
||||
unwrapLink(editor);
|
||||
}
|
||||
|
||||
export const wrapLink = (editor: Editor, url?: string, newTab?: boolean): void => {
|
||||
const { selection, blurSelection } = editor;
|
||||
|
||||
if (blurSelection) {
|
||||
Transforms.select(editor, blurSelection);
|
||||
}
|
||||
|
||||
const selectionToUse = selection || blurSelection;
|
||||
|
||||
const isCollapsed = selectionToUse && Range.isCollapsed(selectionToUse);
|
||||
|
||||
const link = {
|
||||
type: 'link',
|
||||
url,
|
||||
newTab,
|
||||
children: isCollapsed ? [{ text: url }] : [],
|
||||
};
|
||||
|
||||
if (isCollapsed) {
|
||||
Transforms.insertNodes(editor, link);
|
||||
if (isElementActive(editor, 'link')) {
|
||||
unwrapLink(editor);
|
||||
} else {
|
||||
Transforms.wrapNodes(editor, link, { split: true });
|
||||
Transforms.collapse(editor, { edge: 'end' });
|
||||
const selectionToUse = selection || blurSelection;
|
||||
|
||||
const isCollapsed = selectionToUse && Range.isCollapsed(selectionToUse);
|
||||
|
||||
const link = {
|
||||
type: 'link',
|
||||
url,
|
||||
newTab,
|
||||
children: isCollapsed ? [{ text: url }] : [],
|
||||
};
|
||||
|
||||
if (isCollapsed) {
|
||||
Transforms.insertNodes(editor, link);
|
||||
} else {
|
||||
Transforms.wrapNodes(editor, link, { split: true });
|
||||
Transforms.collapse(editor, { edge: 'end' });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import Submit from '../../../../../Submit';
|
||||
import X from '../../../../../../icons/X';
|
||||
import Fields from './Fields';
|
||||
import { requests } from '../../../../../../../api';
|
||||
import { injectVoidElement } from '../../../injectVoid';
|
||||
import { injectVoidElement } from '../../injectVoid';
|
||||
|
||||
import './index.scss';
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ import MinimalTemplate from '../../../../../../templates/Minimal';
|
||||
import Button from '../../../../../../elements/Button';
|
||||
import { SanitizedCollectionConfig } from '../../../../../../../../collections/config/types';
|
||||
import PerPage from '../../../../../../elements/PerPage';
|
||||
import { injectVoidElement } from '../../../injectVoid';
|
||||
import { injectVoidElement } from '../../injectVoid';
|
||||
|
||||
import './index.scss';
|
||||
import '../addSwapModals.scss';
|
||||
|
||||
Reference in New Issue
Block a user