Skip to content

Commit

Permalink
first step of autoformatting feature
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaroldi committed May 17, 2022
1 parent 6609e23 commit 03f92b0
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 193 deletions.
15 changes: 3 additions & 12 deletions packages/roosterjs-editor-api/lib/format/toggleBullet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import toggleListType from '../utils/toggleListType';
import { BulletListType, IEditor, ListType, NumberingListType } from 'roosterjs-editor-types';
import { IEditor, ListType } from 'roosterjs-editor-types';

/**
* Toggle bullet at selection
Expand All @@ -9,15 +9,6 @@ import { BulletListType, IEditor, ListType, NumberingListType } from 'roosterjs-
* browser execCommand API
* @param editor The editor instance
*/
export default function toggleBullet(
editor: IEditor,
styleType?: BulletListType | NumberingListType
) {
toggleListType(
editor,
ListType.Unordered,
undefined /** startNumber */,
false /**includeSiblingLists*/,
styleType
);
export default function toggleBullet(editor: IEditor) {
toggleListType(editor, ListType.Unordered);
}
16 changes: 3 additions & 13 deletions packages/roosterjs-editor-api/lib/format/toggleNumbering.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import toggleListType from '../utils/toggleListType';
import { BulletListType, IEditor, ListType, NumberingListType } from 'roosterjs-editor-types';
import { IEditor, ListType } from 'roosterjs-editor-types';

/**
* Toggle numbering at selection
Expand All @@ -10,16 +10,6 @@ import { BulletListType, IEditor, ListType, NumberingListType } from 'roosterjs-
* @param editor The editor instance
* @param startNumber (Optional) Start number of the list
*/
export default function toggleNumbering(
editor: IEditor,
startNumber?: number,
styleType?: NumberingListType | BulletListType
) {
toggleListType(
editor,
ListType.Ordered,
startNumber,
false /** includeSiblingLists */,
styleType
);
export default function toggleNumbering(editor: IEditor, startNumber?: number) {
toggleListType(editor, ListType.Ordered, startNumber);
}
7 changes: 3 additions & 4 deletions packages/roosterjs-editor-api/lib/utils/toggleListType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import blockFormat from '../utils/blockFormat';
import { BulletListType, IEditor, ListType, NumberingListType } from 'roosterjs-editor-types';
import { createVListFromRegion, getBlockElementAtNode } from 'roosterjs-editor-dom';
import { IEditor, ListType } from 'roosterjs-editor-types';
import type { CompatibleListType } from 'roosterjs-editor-types/lib/compatibleTypes';

/**
Expand All @@ -24,8 +24,7 @@ export default function toggleListType(
editor: IEditor,
listType: ListType | CompatibleListType,
startNumber?: number,
includeSiblingLists: boolean = true,
listStyleType?: NumberingListType | BulletListType
includeSiblingLists: boolean = true
) {
blockFormat(editor, (region, start, end, chains) => {
const chain =
Expand All @@ -39,7 +38,7 @@ export default function toggleListType(
: createVListFromRegion(region, includeSiblingLists);

if (vList) {
vList.changeListType(start, end, listType, listStyleType);
vList.changeListType(start, end, listType);
vList.writeBack();
}
});
Expand Down
14 changes: 4 additions & 10 deletions packages/roosterjs-editor-dom/lib/list/VList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {
PositionType,
NodeType,
Alignment,
NumberingListType,
BulletListType,
} from 'roosterjs-editor-types';
import type {
CompatibleAlignment,
Expand Down Expand Up @@ -330,20 +328,16 @@ export default class VList {
changeListType(
start: NodePosition,
end: NodePosition,
targetType: ListType | CompatibleListType,
listStyleType?: NumberingListType | BulletListType
targetType: ListType | CompatibleListType
) {
let needChangeType = false;

this.findListItems(start, end, item => {
needChangeType = needChangeType || item.getListType() != targetType;
});
this.findListItems(start, end, item => {
needChangeType ? item.changeListType(targetType) : item.outdent();
if (listStyleType) {
item.setListItemMarkerStyle(listStyleType as NumberingListType, item);
}
});
this.findListItems(start, end, item =>
needChangeType ? item.changeListType(targetType) : item.outdent()
);
}

/**
Expand Down
7 changes: 1 addition & 6 deletions packages/roosterjs-editor-dom/lib/list/VListItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import isBlockElement from '../utils/isBlockElement';
import moveChildNodes from '../utils/moveChildNodes';
import safeInstanceOf from '../utils/safeInstanceOf';
import setListItemStyle from './setListItemStyle';
import setNumberingListMarkers from './setNumberingListMarkers';
import toArray from '../utils/toArray';
import unwrap from '../utils/unwrap';
import wrap from '../utils/wrap';
import { KnownCreateElementDataIndex, ListType, NumberingListType } from 'roosterjs-editor-types';
import { KnownCreateElementDataIndex, ListType } from 'roosterjs-editor-types';
import type { CompatibleListType } from 'roosterjs-editor-types/lib/compatibleTypes';

const orderListStyles = [null, 'lower-alpha', 'lower-roman'];
Expand Down Expand Up @@ -187,10 +186,6 @@ export default class VListItem {
}
}

setListItemMarkerStyle(listStyleType: NumberingListType, li: VListItem) {
setNumberingListMarkers(listStyleType, li, this.node);
}

/**
* Set whether the item is a dummy item
* @param isDummy Whether the item is a dummy item
Expand Down
25 changes: 0 additions & 25 deletions packages/roosterjs-editor-dom/lib/list/setBulletListMarkers.ts

This file was deleted.

101 changes: 0 additions & 101 deletions packages/roosterjs-editor-dom/lib/list/setNumberingListMarkers.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getListStyle } from '../utils/getListStyle';
import { getListType } from '../utils/getListType';
import {
blockFormat,
experimentCommitListChains,
Expand Down Expand Up @@ -150,10 +148,11 @@ const AutoBullet: BuildInEditFeature<PluginKeyboardEvent> = {
if (!cacheGetListElement(event, editor)) {
let searcher = editor.getContentSearcherOfCursor(event);
let textBeforeCursor = searcher.getSubStringBefore(4);

// Auto list is triggered if:
// 1. Text before cursor exactly matches '*', '-' or '1.'
// 2. There's no non-text inline entities before cursor
return getListType(textBeforeCursor) && !searcher.getNearestNonTextInlineElement();
return isAListPattern(textBeforeCursor) && !searcher.getNearestNonTextInlineElement();
}
return false;
},
Expand All @@ -167,21 +166,21 @@ const AutoBullet: BuildInEditFeature<PluginKeyboardEvent> = {
let textBeforeCursor = searcher.getSubStringBefore(4);
let textRange = searcher.getRangeFromText(textBeforeCursor, true /*exactMatch*/);

const listType = getListType(textBeforeCursor);
const listStyle = getListStyle(textBeforeCursor, listType);

if (!textRange) {
// no op if the range can't be found
} else if (listType === ListType.Unordered) {
} else if (
textBeforeCursor.indexOf('*') == 0 ||
textBeforeCursor.indexOf('-') == 0
) {
prepareAutoBullet(editor, textRange);
toggleBullet(editor, listStyle);
} else if (listType === ListType.Ordered) {
toggleBullet(editor);
} else if (isAListPattern(textBeforeCursor)) {
prepareAutoBullet(editor, textRange);
toggleNumbering(editor, undefined /** startNumber */, listStyle);
toggleNumbering(editor);
} else if ((regions = editor.getSelectedRegions()) && regions.length == 1) {
const num = parseInt(textBeforeCursor);
prepareAutoBullet(editor, textRange);
toggleNumbering(editor, num, listStyle);
toggleNumbering(editor, num);
}
searcher.getRangeFromText(textBeforeCursor, true /*exactMatch*/)?.deleteContents();
},
Expand Down Expand Up @@ -214,10 +213,10 @@ const MaintainListChain: BuildInEditFeature<PluginKeyboardEvent> = {
* 1. 1> 1) 1- (1)
* @returns if a text is considered a list pattern
*/
// function isAListPattern(textBeforeCursor: string) {
// const REGEX: RegExp = /^(\*|-|[0-9]{1,2}\.|[0-9]{1,2}\>|[0-9]{1,2}\)|[0-9]{1,2}\-|\([0-9]{1,2}\))$/;
// return REGEX.test(textBeforeCursor);
// }
function isAListPattern(textBeforeCursor: string) {
const REGEX: RegExp = /^(\*|-|[0-9]{1,2}\.|[0-9]{1,2}\>|[0-9]{1,2}\)|[0-9]{1,2}\-|\([0-9]{1,2}\))$/;
return REGEX.test(textBeforeCursor);
}

function getListChains(editor: IEditor) {
return VListChain.createListChains(editor.getSelectedRegions());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BulletListType, ListType, NumberingListType } from 'roosterjs-editor-types/lib';
import { BulletListType, ListType, NumberingListType } from 'roosterjs-editor-types';

const numberingListTypes: Record<string, number> = {
'1.': NumberingListType.Decimal,
Expand Down Expand Up @@ -38,6 +38,12 @@ const identifyBulletListType = (textBeforeCursor: string): BulletListType => {
return bulletListType[bullet] || null;
};

/**
* @internal
* @param textBeforeCursor The trigger character
* @param listType The type of the list (ordered or unordered)
* @returns the style of the list
*/
export function getListStyle(
textBeforeCursor: string,
listType: ListType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ListType } from 'roosterjs-editor-types/lib';
import { ListType } from 'roosterjs-editor-types';

function isABulletList(textBeforeCursor: string) {
const hasTriggers = ['*', '-', '>'].indexOf(textBeforeCursor[0]) > -1;
Expand All @@ -11,6 +11,11 @@ function isANumberingList(textBeforeCursor: string) {
return REGEX.test(textBeforeCursor.replace(/\s/g, ''));
}

/**
* @internal
* @param textBeforeCursor The trigger character
* @returns If the list is ordered or unordered
*/
export function getListType(textBeforeCursor: string): ListType {
if (isABulletList(textBeforeCursor)) {
return ListType.Unordered;
Expand Down
4 changes: 2 additions & 2 deletions packages/roosterjs-editor-types/lib/enum/BulletListType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Enum used to control the different types of bullet list
/**
* Enum used to control the different types of bullet list
*/

export const enum BulletListType {
Expand Down
5 changes: 2 additions & 3 deletions packages/roosterjs-editor-types/lib/enum/NumberingListType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
* Enum used to control the different types of numbering list
/**
* Enum used to control the different types of numbering list
*/

export const enum NumberingListType {
/**
* Numbering triggered by 1.
Expand Down

0 comments on commit 03f92b0

Please sign in to comment.