Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
- **Collaborative Editing** - Real-time collaboration via [Yjs](https://github.com/yjs/yjs) integration
- **Serialization** - Import/export from JSON, Markdown, and HTML
- **Rich Content** - Support for tables, lists, code blocks, images, and custom nodes
- **Cross-browser** - Firefox 52+, Chrome 49+, Safari 11+, Edge 79+
- **Cross-browser** - Firefox 115+, Safari 15+, Chrome 86+ (see [Supported Browsers](https://lexical.dev/docs/getting-started/supported-browsers))

- **Type Safe** - Written in TypeScript with comprehensive type definitions

## Quick Start
Expand Down
17 changes: 17 additions & 0 deletions packages/lexical-link/src/LexicalLinkNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,23 @@ export function $toggleLink(
return;
}

if (selection.isCollapsed() && url === null) {
for (const node of selection.getNodes()) {
const parentLink = $findMatchingParent(
node,
(parent): parent is LinkNode =>
!$isAutoLinkNode(parent) && $isLinkNode(parent),
);
if (parentLink !== null) {
parentLink.getChildren().forEach((child) => {
parentLink.insertBefore(child);
});
parentLink.remove();
}
return;
}
}

// Handle RangeSelection
const nodes = selection.extract();

Expand Down
27 changes: 27 additions & 0 deletions packages/lexical-link/src/__tests__/unit/LexicalLinkNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {$createHeadingNode} from '@lexical/rich-text';
import {
$createLineBreakNode,
$createParagraphNode,
$createRangeSelection,
$createTextNode,
$getNodeByKey,
$getRoot,
Expand All @@ -27,6 +28,7 @@ import {
$isRangeSelection,
$isTextNode,
$selectAll,
$setSelection,
ParagraphNode,
RangeSelection,
SerializedParagraphNode,
Expand Down Expand Up @@ -819,6 +821,31 @@ describe('LexicalLinkNode tests', () => {
}
});
});

test('$toggleLink removes link when selection is collapsed', async () => {
const {editor} = testEnv;
await editor.update(() => {
const p = $createParagraphNode();
const textNode = $createTextNode('textboldtext');
p.append(textNode);
$getRoot().append(p);
$selectAll();
$toggleLink('https://lexical.dev/', {title: 'Lexical Website'});

const linkNode = p.getFirstChild() as LinkNode;
textNode.select(4, 8).formatText('bold');
const sel = $createRangeSelection();
const key = linkNode.getChildAtIndex(1)!.getKey();
sel.anchor.set(key, 4, 'text');
sel.focus.set(key, 4, 'text');
$setSelection(sel);
$toggleLink(null);

const children = p.getChildren();
expect(children.length).toBe(3);
children.forEach((child) => expect($isTextNode(child)).toBe(true));
});
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ sidebar_position: 4

> Note: Lexical does not support Internet Explorer or legacy versions of Edge.

- Firefox 52+
- Chrome 49+
- Edge 79+ (when Edge switched to Chromium)
- Safari 11+
- iOS 11+ (Safari)
- iPad OS 13+ (Safari)
- Android Chrome 72+
- Firefox 115+
- Chrome 86+ (and other browsers based on Chrome 86+ such as Android Chrome 86+, Edge 86+ and Opera 72+)
- Safari 15+ (iOS 15+, iPad OS 15+)
Loading