-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Part 2] Port Auto link #2485
[Part 2] Port Auto link #2485
Changes from 5 commits
fd79ae3
b7fc824
ad391f1
ac1837d
698dbe4
570bbfe
4987166
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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, autoLink: boolean) { | ||
if (!autoLink) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
return; | ||
} | ||
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; | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ import type { IEditor } from 'roosterjs-content-model-types'; | |
/** | ||
* @internal | ||
*/ | ||
export function unlink(editor: IEditor, rawEvent: KeyboardEvent) { | ||
export function unlink(editor: IEditor, rawEvent: KeyboardEvent, unlink: boolean) { | ||
if (!unlink) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why need this parameter? If unlink is false, we should not call this function at all |
||
editor.formatContentModel(model => { | ||
const link = getLinkSegment(model); | ||
if (link?.link) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,26 +13,28 @@ export function keyboardListTrigger( | |
shouldSearchForBullet: boolean = true, | ||
shouldSearchForNumbering: boolean = true | ||
) { | ||
editor.formatContentModel((model, _context) => { | ||
const listStyleType = getListTypeStyle( | ||
model, | ||
shouldSearchForBullet, | ||
shouldSearchForNumbering | ||
); | ||
if (listStyleType) { | ||
const segmentsAndParagraphs = getSelectedSegmentsAndParagraphs(model, false); | ||
if (segmentsAndParagraphs[0] && segmentsAndParagraphs[0][1]) { | ||
segmentsAndParagraphs[0][1].segments.splice(0, 1); | ||
} | ||
const { listType, styleType, index } = listStyleType; | ||
triggerList(editor, model, listType, styleType, index); | ||
rawEvent.preventDefault(); | ||
normalizeContentModel(model); | ||
if (shouldSearchForBullet || shouldSearchForNumbering) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, check outside to reduce unnecessary parameters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already use these parameter on line 20 and 21, then we cannot remove them There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I return this check to outside the function anyway? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. In that case we can keep it |
||
editor.formatContentModel((model, _context) => { | ||
const listStyleType = getListTypeStyle( | ||
model, | ||
shouldSearchForBullet, | ||
shouldSearchForNumbering | ||
); | ||
if (listStyleType) { | ||
const segmentsAndParagraphs = getSelectedSegmentsAndParagraphs(model, false); | ||
if (segmentsAndParagraphs[0] && segmentsAndParagraphs[0][1]) { | ||
segmentsAndParagraphs[0][1].segments.splice(0, 1); | ||
} | ||
const { listType, styleType, index } = listStyleType; | ||
triggerList(editor, model, listType, styleType, index); | ||
rawEvent.preventDefault(); | ||
normalizeContentModel(model); | ||
|
||
return true; | ||
} | ||
return false; | ||
}); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
} | ||
|
||
const triggerList = ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here