-
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 branch 'master' into u/jisong/shadoweditselection
- Loading branch information
Showing
18 changed files
with
2,618 additions
and
1,491 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
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
46 changes: 46 additions & 0 deletions
46
packages/roosterjs-content-model-plugins/lib/autoFormat/hyphen/transformHyphen.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,46 @@ | ||
import { splitTextSegment } from '../../pluginUtils/splitTextSegment'; | ||
import type { | ||
ContentModelParagraph, | ||
ContentModelText, | ||
FormatContentModelContext, | ||
} from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function transformHyphen( | ||
previousSegment: ContentModelText, | ||
paragraph: ContentModelParagraph, | ||
context: FormatContentModelContext | ||
): boolean { | ||
const segments = previousSegment.text.split(' '); | ||
const dashes = segments[segments.length - 2]; | ||
if (dashes === '--') { | ||
const textIndex = previousSegment.text.lastIndexOf('--'); | ||
const textSegment = splitTextSegment(previousSegment, paragraph, textIndex, textIndex + 2); | ||
|
||
textSegment.text = textSegment.text.replace('--', '—'); | ||
context.canUndoByBackspace = true; | ||
return true; | ||
} else { | ||
const text = segments.pop(); | ||
const hasDashes = text && text?.indexOf('--') > -1; | ||
if (hasDashes && text.trim() !== '--') { | ||
const textIndex = previousSegment.text.indexOf(text); | ||
const textSegment = splitTextSegment( | ||
previousSegment, | ||
paragraph, | ||
textIndex, | ||
textIndex + text.length - 1 | ||
); | ||
|
||
const textLength = textSegment.text.length; | ||
if (textSegment.text[0] !== '-' && textSegment.text[textLength - 1] !== '-') { | ||
textSegment.text = textSegment.text.replace('--', '—'); | ||
context.canUndoByBackspace = true; | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} |
15 changes: 8 additions & 7 deletions
15
packages/roosterjs-content-model-plugins/lib/autoFormat/link/createLink.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
78 changes: 31 additions & 47 deletions
78
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 |
---|---|---|
@@ -1,57 +1,41 @@ | ||
import { getSelectedSegmentsAndParagraphs } from 'roosterjs-content-model-dom'; | ||
import { matchLink } from 'roosterjs-content-model-api'; | ||
import { splitTextSegment } from '../../pluginUtils/splitTextSegment'; | ||
import type { IEditor, LinkData } from 'roosterjs-content-model-types'; | ||
import type { | ||
ContentModelParagraph, | ||
ContentModelText, | ||
FormatContentModelContext, | ||
LinkData, | ||
} from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function createLinkAfterSpace(editor: IEditor) { | ||
editor.formatContentModel((model, context) => { | ||
const selectedSegmentsAndParagraphs = getSelectedSegmentsAndParagraphs( | ||
model, | ||
false /* includingFormatHolder */ | ||
export function createLinkAfterSpace( | ||
previousSegment: ContentModelText, | ||
paragraph: ContentModelParagraph, | ||
context: FormatContentModelContext | ||
) { | ||
const link = previousSegment.text.split(' ').pop(); | ||
const url = link?.trim(); | ||
let linkData: LinkData | null = null; | ||
if (url && link && (linkData = matchLink(url))) { | ||
const linkSegment = splitTextSegment( | ||
previousSegment, | ||
paragraph, | ||
previousSegment.text.length - link.trimLeft().length, | ||
previousSegment.text.trimRight().length | ||
); | ||
if (selectedSegmentsAndParagraphs.length > 0 && selectedSegmentsAndParagraphs[0][1]) { | ||
const markerIndex = selectedSegmentsAndParagraphs[0][1].segments.findIndex( | ||
segment => segment.segmentType == 'SelectionMarker' | ||
); | ||
const paragraph = selectedSegmentsAndParagraphs[0][1]; | ||
if (markerIndex > 0) { | ||
const textSegment = paragraph.segments[markerIndex - 1]; | ||
const marker = paragraph.segments[markerIndex]; | ||
if ( | ||
marker.segmentType == 'SelectionMarker' && | ||
textSegment && | ||
textSegment.segmentType == 'Text' && | ||
!textSegment.link | ||
) { | ||
const link = textSegment.text.split(' ').pop(); | ||
const url = link?.trim(); | ||
let linkData: LinkData | null = null; | ||
if (url && link && (linkData = matchLink(url))) { | ||
const linkSegment = splitTextSegment( | ||
textSegment, | ||
paragraph, | ||
textSegment.text.length - link.trimLeft().length, | ||
textSegment.text.trimRight().length | ||
); | ||
linkSegment.link = { | ||
format: { | ||
href: linkData.normalizedUrl, | ||
underline: true, | ||
}, | ||
dataset: {}, | ||
}; | ||
linkSegment.link = { | ||
format: { | ||
href: linkData.normalizedUrl, | ||
underline: true, | ||
}, | ||
dataset: {}, | ||
}; | ||
|
||
context.canUndoByBackspace = true; | ||
context.canUndoByBackspace = true; | ||
|
||
return true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
}); | ||
return true; | ||
} | ||
return false; | ||
} |
29 changes: 0 additions & 29 deletions
29
packages/roosterjs-content-model-plugins/lib/autoFormat/link/getLinkSegment.ts
This file was deleted.
Oops, something went wrong.
12 changes: 6 additions & 6 deletions
12
packages/roosterjs-content-model-plugins/lib/autoFormat/link/unlink.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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { getLinkSegment } from './getLinkSegment'; | ||
import { formatTextSegmentBeforeSelectionMarker } from '../../pluginUtils/formatTextSegmentBeforeSelectionMarker'; | ||
|
||
import type { IEditor } from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function unlink(editor: IEditor, rawEvent: KeyboardEvent) { | ||
editor.formatContentModel(model => { | ||
const link = getLinkSegment(model); | ||
if (link?.link) { | ||
link.link = undefined; | ||
formatTextSegmentBeforeSelectionMarker(editor, (_model, linkSegment, _paragraph) => { | ||
if (linkSegment?.link) { | ||
linkSegment.link = undefined; | ||
rawEvent.preventDefault(); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
} |
Oops, something went wrong.