-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2485 from microsoft/u/juliaroldi/auto-link-2
[Part 2] Port Auto link
- Loading branch information
Showing
6 changed files
with
385 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,8 @@ export function createLink(editor: IEditor) { | |
}, | ||
dataset: {}, | ||
}); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/roosterjs-content-model-plugins/lib/autoFormat/link/createLinkAfterSpace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { createText } from 'roosterjs-content-model-dom'; | ||
import { getSelectedSegmentsAndParagraphs } from 'roosterjs-content-model-core'; | ||
import { matchLink } from 'roosterjs-content-model-api'; | ||
import type { IEditor } from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function createLinkAfterSpace(editor: IEditor) { | ||
editor.formatContentModel(model => { | ||
const selectedSegmentsAndParagraphs = getSelectedSegmentsAndParagraphs( | ||
model, | ||
false /* includingFormatHolder */ | ||
); | ||
if (selectedSegmentsAndParagraphs[0] && selectedSegmentsAndParagraphs[0][1]) { | ||
const length = selectedSegmentsAndParagraphs[0][1].segments.length; | ||
const marker = selectedSegmentsAndParagraphs[0][1].segments[length - 1]; | ||
const textSegment = selectedSegmentsAndParagraphs[0][1].segments[length - 2]; | ||
if ( | ||
marker.segmentType == 'SelectionMarker' && | ||
textSegment.segmentType == 'Text' && | ||
!textSegment.link | ||
) { | ||
const link = textSegment.text.split(' ').pop(); | ||
if (link && matchLink(link)) { | ||
textSegment.text = textSegment.text.replace(link, ''); | ||
const linkSegment = createText(link, marker.format, { | ||
format: { | ||
href: link, | ||
underline: true, | ||
}, | ||
dataset: {}, | ||
}); | ||
selectedSegmentsAndParagraphs[0][1].segments.splice(length - 1, 0, linkSegment); | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.