Skip to content

Commit

Permalink
Merge pull request #2653 from microsoft/u/juliaroldi/auto-link
Browse files Browse the repository at this point in the history
Auto link for link preview
  • Loading branch information
juliaroldi authored May 21, 2024
2 parents 8ec51b3 + e09c51b commit 09a01de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import type { IEditor, LinkData } from 'roosterjs-content-model-types';
* @internal
*/
export function createLink(editor: IEditor) {
let anchorNode: Node | null = null;
formatTextSegmentBeforeSelectionMarker(
editor,
(_model, linkSegment, _paragraph) => {
if (linkSegment.link) {
return true;
}
let linkData: LinkData | null = null;
if (!linkSegment.link && (linkData = matchLink(linkSegment.text))) {
addLink(linkSegment, {
Expand All @@ -20,10 +24,17 @@ export function createLink(editor: IEditor) {
});
return true;
}

return false;
},
{
changeSource: ChangeSource.AutoLink,
onNodeCreated: (_modelElement, node) => {
if (!anchorNode) {
anchorNode = node;
}
},
getChangeData: () => anchorNode,
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,6 @@ describe('createLink', () => {
format: {},
};

runTest(input, input, false);
runTest(input, input, true);
});
});

0 comments on commit 09a01de

Please sign in to comment.