Skip to content

Commit

Permalink
Word "constructor" should not trigger auto list (#2690)
Browse files Browse the repository at this point in the history
* Fix "constructor" triggers auto list issue

* add test
  • Loading branch information
JiuqingSong authored Jun 11, 2024
1 parent a0f32c6 commit 39f2d34
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function getListTypeStyle(
listMarkerSegment.segmentType == 'Text'
) {
const listMarker = listMarkerSegment.text.trim();
const bulletType = bulletListType[listMarker];
const bulletType = bulletListType.get(listMarker);

if (bulletType && shouldSearchForBullet) {
return { listType: 'UL', styleType: bulletType };
Expand Down Expand Up @@ -117,16 +117,16 @@ const getPreviousListStyle = (list?: ContentModelListItem) => {
}
};

const bulletListType: Record<string, number> = {
'*': BulletListType.Disc,
'-': BulletListType.Dash,
'--': BulletListType.Square,
'->': BulletListType.LongArrow,
'-->': BulletListType.DoubleLongArrow,
'=>': BulletListType.UnfilledArrow,
'>': BulletListType.ShortArrow,
'—': BulletListType.Hyphen,
};
const bulletListType: Map<string, number> = new Map<string, number>([
['*', BulletListType.Disc],
['-', BulletListType.Dash],
['--', BulletListType.Square],
['->', BulletListType.LongArrow],
['-->', BulletListType.DoubleLongArrow],
['=>', BulletListType.UnfilledArrow],
['>', BulletListType.ShortArrow],
['—', BulletListType.Hyphen],
]);

const isNewList = (listMarker: string) => {
const marker = listMarker.replace(/[^\w\s]/g, '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import type {
ShallowMutableContentModelParagraph,
} from 'roosterjs-content-model-types';

const FRACTIONS: Record<string, string> = {
'1/2': '½',
'1/4': '¼',
'3/4': '¾',
};
const FRACTIONS: Map<string, string> = new Map<string, string>([
['1/2', '½'],
['1/4', '¼'],
['3/4', '¾'],
]);

/**
* @internal
Expand All @@ -20,11 +20,13 @@ export function transformFraction(
context: FormatContentModelContext
): boolean {
const fraction = previousSegment.text.split(' ').pop()?.trim();
if (fraction && FRACTIONS[fraction]) {
const text = fraction ? FRACTIONS.get(fraction) : undefined;

if (fraction && text) {
const textLength = previousSegment.text.length - 1;
const textIndex = textLength - fraction.length;
const textSegment = splitTextSegment(previousSegment, paragraph, textIndex, textLength);
textSegment.text = FRACTIONS[fraction];
textSegment.text = text;

context.canUndoByBackspace = true;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1005,4 +1005,31 @@ describe('getListTypeStyle', () => {
};
runTest(model, undefined);
});

it('"Constructor" should not trigger list', () => {
const model: ContentModelDocument = {
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'constructor',
format: {},
},
{
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
],
format: {},
},
],
format: {},
};

runTest(model, undefined);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,18 @@ describe('transformFraction', () => {
};
runTest(segment, paragraph, { canUndoByBackspace: true } as any, false);
});

it('"constructor"', () => {
const segment: ContentModelText = {
segmentType: 'Text',
text: 'constructor',
format: {},
};
const paragraph: ContentModelParagraph = {
blockType: 'Paragraph',
segments: [segment],
format: {},
};
runTest(segment, paragraph, { canUndoByBackspace: true } as any, false);
});
});

0 comments on commit 39f2d34

Please sign in to comment.