fix: rare crash with link rte element

This commit is contained in:
James
2022-03-07 10:23:50 -05:00
parent 2e9a4c7d71
commit f5535f613a
5 changed files with 23 additions and 27 deletions

View File

@@ -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' });
}
}
};

View File

@@ -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';

View File

@@ -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';