Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaroldi committed Sep 23, 2024
1 parent f37e9de commit a13c33c
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { transformFraction } from './numbers/transformFraction';
import { transformHyphen } from './hyphen/transformHyphen';
import { transformOrdinals } from './numbers/transformOrdinals';
import { unlink } from './link/unlink';
import type { AutoFormatOptions } from './interface/AutoFormatOptions';
import type {
ContentChangedEvent,
EditorInputEvent,
Expand All @@ -17,56 +18,6 @@ import type {
PluginEvent,
} from 'roosterjs-content-model-types';

/**
* Options to customize the Content Model Auto Format Plugin
*/
export type AutoFormatOptions = {
/**
* When true, after type *, ->, -, --, => , —, > and space key a type of bullet list will be triggered. @default true
*/
autoBullet?: boolean;

/**
* When true, after type 1, A, a, i, I followed by ., ), - or between () and space key a type of numbering list will be triggered. @default true
*/
autoNumbering?: boolean;

/**
* When press backspace before a link, remove the hyperlink
*/
autoUnlink?: boolean;

/**
* When paste or type content with a link, create hyperlink for the link
*/
autoLink?: boolean;

/**
* Transform -- into hyphen, if typed between two words
*/
autoHyphen?: boolean;

/**
* Transform 1/2, 1/4, 3/4 into fraction character
*/
autoFraction?: boolean;

/**
* Transform ordinal numbers into superscript
*/
autoOrdinals?: boolean;

/**
* When paste content or type content with telephone, create hyperlink for the telephone number
*/
autoTel?: boolean;

/**
* When paste or type a content with mailto, create hyperlink for the content
*/
autoMailto?: boolean;
};

/**
* @internal
*/
Expand All @@ -90,11 +41,11 @@ export class AutoFormatPlugin implements EditorPlugin {
* @param options An optional parameter that takes in an object of type AutoFormatOptions, which includes the following properties:
* - autoBullet: A boolean that enables or disables automatic bullet list formatting. Defaults to false.
* - autoNumbering: A boolean that enables or disables automatic numbering formatting. Defaults to false.
* - autoLink: A boolean that enables or disables automatic hyperlink url address creation when pasting or typing content. Defaults to false.
* - autoUnlink: A boolean that enables or disables automatic hyperlink removal when pressing backspace. Defaults to false.
* - autoHyphen: A boolean that enables or disables automatic hyphen transformation. Defaults to false.
* - autoFraction: A boolean that enables or disables automatic fraction transformation. Defaults to false.
* - autoOrdinals: A boolean that enables or disables automatic ordinal number transformation. Defaults to false.
* - autoLink: A boolean that enables or disables automatic hyperlink url address creation when pasting or typing content. Defaults to false.
* - autoUnlink: A boolean that enables or disables automatic hyperlink removal when pressing backspace. Defaults to false.
* - autoTel: A boolean that enables or disables automatic hyperlink telephone numbers transformation. Defaults to false.
* - autoMailto: A boolean that enables or disables automatic hyperlink email address transformation. Defaults to false.
*/
Expand Down Expand Up @@ -197,9 +148,11 @@ export class AutoFormatPlugin implements EditorPlugin {
previousSegment,
paragraph,
context,
!!autoLink,
!!autoTel,
!!autoMailto
{
autoLink,
autoTel,
autoMailto,
}
);
}

Expand Down Expand Up @@ -262,7 +215,11 @@ export class AutoFormatPlugin implements EditorPlugin {
private handleContentChangedEvent(editor: IEditor, event: ContentChangedEvent) {
const { autoLink, autoTel, autoMailto } = this.options;
if (event.source == 'Paste' && (autoLink || autoTel || autoMailto)) {
createLink(editor, !!autoLink, !!autoTel, !!autoMailto);
createLink(editor, {
autoLink,
autoTel,
autoMailto,
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { AutoLinkOptions } from './AutoLinkOptions';

/**
* Options to customize the Content Model Auto Format Plugin
*/
export interface AutoFormatOptions extends AutoLinkOptions {
/**
* When true, after type *, ->, -, --, => , —, > and space key a type of bullet list will be triggered.
*/
autoBullet?: boolean;

/**
* When true, after type 1, A, a, i, I followed by ., ), - or between () and space key a type of numbering list will be triggered.
*/
autoNumbering?: boolean;

/**
* Transform -- into hyphen, if typed between two words
*/
autoHyphen?: boolean;

/**
* Transform 1/2, 1/4, 3/4 into fraction character
*/
autoFraction?: boolean;

/**
* Transform ordinal numbers into superscript
*/
autoOrdinals?: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Options to customize the Auto link options in Auto Format Plugin
*/
export interface AutoLinkOptions {
/**
* When press backspace before a link, remove the hyperlink
*/
autoUnlink?: boolean;

/**
* When paste or type content with a link, create hyperlink for the link
*/
autoLink?: boolean;

/**
* When paste content or type content with telephone, create hyperlink for the telephone number
*/
autoTel?: boolean;

/**
* When paste or type a content with mailto, create hyperlink for the content
*/
autoMailto?: boolean;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { addLink, ChangeSource } from 'roosterjs-content-model-dom';
import { formatTextSegmentBeforeSelectionMarker } from 'roosterjs-content-model-api';
import { getLinkUrl } from './getLinkUrl';
import type { AutoLinkOptions } from '../interface/AutoLinkOptions';
import type { ContentModelLink, IEditor } from 'roosterjs-content-model-types';

/**
* @internal
*/
export function createLink(
editor: IEditor,
autoLink: boolean,
autoTel: boolean,
autoMailto: boolean
) {
export function createLink(editor: IEditor, autoLinkOptions: AutoLinkOptions) {
let anchorNode: Node | null = null;
const links: ContentModelLink[] = [];
formatTextSegmentBeforeSelectionMarker(
Expand All @@ -22,10 +18,7 @@ export function createLink(
return true;
}
let linkUrl: string | undefined = undefined;
if (
!linkSegment.link &&
(linkUrl = getLinkUrl(linkSegment.text, autoLink, autoTel, autoMailto))
) {
if (!linkSegment.link && (linkUrl = getLinkUrl(linkSegment.text, autoLinkOptions))) {
addLink(linkSegment, {
format: {
href: linkUrl,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getLinkUrl } from './getLinkUrl';
import { splitTextSegment } from 'roosterjs-content-model-api';
import type { AutoLinkOptions } from '../interface/AutoLinkOptions';
import type {
ContentModelText,
FormatContentModelContext,
Expand All @@ -13,14 +14,12 @@ export function createLinkAfterSpace(
previousSegment: ContentModelText,
paragraph: ShallowMutableContentModelParagraph,
context: FormatContentModelContext,
autoLink: boolean,
autoTel: boolean,
autoMailto: boolean
autoLinkOptions: AutoLinkOptions
) {
const link = previousSegment.text.split(' ').pop();
const url = link?.trim();
let linkUrl: string | undefined = undefined;
if (url && link && (linkUrl = getLinkUrl(url, autoLink, autoTel, autoMailto))) {
if (url && link && (linkUrl = getLinkUrl(url, autoLinkOptions))) {
const linkSegment = splitTextSegment(
previousSegment,
paragraph,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { matchLink } from 'roosterjs-content-model-api';
import type { AutoLinkOptions } from '../interface/AutoLinkOptions';

const COMMON_REGEX = `[\s]*[a-zA-Z0-9+][\s]*`;
const TELEPHONE_REGEX = `(T|t)el:${COMMON_REGEX}`;
Expand All @@ -7,15 +8,11 @@ const MAILTO_REGEX = `(M|m)ailto:${COMMON_REGEX}`;
/**
* @internal
*/
export function getLinkUrl(
text: string,
shouldLink: boolean,
shouldMatchTel: boolean,
shouldMatchMailto: boolean
): string | undefined {
const linkMatch = shouldLink ? matchLink(text)?.normalizedUrl : undefined;
const telMatch = shouldMatchTel ? matchTel(text) : undefined;
const mailtoMatch = shouldMatchMailto ? matchMailTo(text) : undefined;
export function getLinkUrl(text: string, autoLinkOptions: AutoLinkOptions): string | undefined {
const { autoLink, autoMailto, autoTel } = autoLinkOptions;
const linkMatch = autoLink ? matchLink(text)?.normalizedUrl : undefined;
const telMatch = autoTel ? matchTel(text) : undefined;
const mailtoMatch = autoMailto ? matchMailTo(text) : undefined;

return linkMatch || telMatch || mailtoMatch;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/roosterjs-content-model-plugins/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ export { TableEditFeatureName } from './tableEdit/editors/features/TableEditFeat
export { PastePlugin } from './paste/PastePlugin';
export { DefaultSanitizers } from './paste/DefaultSanitizers';
export { EditPlugin, EditOptions } from './edit/EditPlugin';
export { AutoFormatPlugin, AutoFormatOptions } from './autoFormat/AutoFormatPlugin';
export { AutoFormatPlugin } from './autoFormat/AutoFormatPlugin';
export { AutoFormatOptions } from './autoFormat/interface/AutoFormatOptions';
export { AutoLinkOptions } from './autoFormat/interface/AutoLinkOptions';

export {
ShortcutBold,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as createLink from '../../lib/autoFormat/link/createLink';
import * as formatTextSegmentBeforeSelectionMarker from 'roosterjs-content-model-api/lib/publicApi/utils/formatTextSegmentBeforeSelectionMarker';
import * as unlink from '../../lib/autoFormat/link/unlink';
import { AutoFormatOptions, AutoFormatPlugin } from '../../lib/autoFormat/AutoFormatPlugin';
import { AutoFormatOptions } from '../../lib/autoFormat/interface/AutoFormatOptions';
import { AutoFormatPlugin } from '../../lib/autoFormat/AutoFormatPlugin';
import { ChangeSource } from '../../../roosterjs-content-model-dom/lib/constants/ChangeSource';
import { createLinkAfterSpace } from '../../lib/autoFormat/link/createLinkAfterSpace';
import { keyboardListTrigger } from '../../lib/autoFormat/list/keyboardListTrigger';
Expand Down Expand Up @@ -182,10 +183,9 @@ describe('Content Model Auto Format Plugin Test', () => {
plugin.initialize(editor);

plugin.onPluginEvent(event);
const { autoLink, autoTel, autoMailto } = options;

if (shouldCallTrigger) {
expect(createLinkSpy).toHaveBeenCalledWith(editor, autoLink, autoTel, autoMailto);
expect(createLinkSpy).toHaveBeenCalledWith(editor, options);
} else {
expect(createLinkSpy).not.toHaveBeenCalled();
}
Expand Down Expand Up @@ -309,15 +309,7 @@ describe('Content Model Auto Format Plugin Test', () => {
) => {
const { autoLink, autoMailto, autoTel } = options;

Check failure on line 310 in packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts

View workflow job for this annotation

GitHub Actions / build

All destructured elements are unused.

Check failure on line 310 in packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts

View workflow job for this annotation

GitHub Actions / build

All destructured elements are unused.
const result =
options &&
createLinkAfterSpace(
segment,
paragraph,
context,
!!autoLink,
!!autoTel,
!!autoMailto
);
options && createLinkAfterSpace(segment, paragraph, context, options);

expect(result).toBe(expectResult);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ describe('createLinkAfterSpace', () => {
context: FormatContentModelContext,
expectedResult: boolean
) {
const result = createLinkAfterSpace(previousSegment, paragraph, context, true, true, true);
const result = createLinkAfterSpace(previousSegment, paragraph, context, {
autoLink: true,
autoMailto: true,
autoTel: true,
});
expect(result).toBe(expectedResult);
}

Expand Down Expand Up @@ -85,7 +89,11 @@ describe('formatTextSegmentBeforeSelectionMarker - createLinkAfterSpace', () =>
formatContentModel: formatWithContentModelSpy,
} as any,
(_model, previousSegment, paragraph, _markerFormat, context) => {
return createLinkAfterSpace(previousSegment, paragraph, context, true, true, true);
return createLinkAfterSpace(previousSegment, paragraph, context, {
autoLink: true,
autoMailto: true,
autoTel: true,
});
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ describe('createLink', () => {
focus: () => {},
formatContentModel: formatWithContentModelSpy,
} as any,
true,
true,
true
{ autoLink: true, autoMailto: true, autoTel: true }
);

expect(formatWithContentModelSpy).toHaveBeenCalled();
Expand Down
Loading

0 comments on commit a13c33c

Please sign in to comment.