-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Announce Plugin, add features to current plugin (#2119)
* init * remove * Fix type issues * add tests * Fix build * Move logic from Editor to Plugin * Add type to param * DefaultAnnounceString to KnownAnnounceStrings * merge classes * Add more details in comments * Fix build * const enum * fix * init * Add callback that returns string * init2 * Fix test after merge * Refactor * refactor * Fix * Dispose editor * Move util from dom to plugin pkg & fix * remove unneeded if * remove unneeded test
- Loading branch information
1 parent
badefc5
commit 1ba8d4a
Showing
16 changed files
with
600 additions
and
145 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
50 changes: 50 additions & 0 deletions
50
packages/roosterjs-editor-plugins/lib/pluginUtils/announceData/getAnnounceDataForList.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,50 @@ | ||
import { KnownAnnounceStrings } from 'roosterjs-editor-types'; | ||
import { | ||
convertDecimalsToAlpha, | ||
convertDecimalsToRoman, | ||
safeInstanceOf, | ||
VList, | ||
} from 'roosterjs-editor-dom'; | ||
import type { AnnounceData } from 'roosterjs-editor-types'; | ||
|
||
/** | ||
* @internal | ||
* Get the announce data for the current List | ||
* @returns announce data for list or undefined. | ||
*/ | ||
export default function getAnnounceDataForList( | ||
list: HTMLElement | null, | ||
li: HTMLElement | null | ||
): AnnounceData | undefined { | ||
if (!safeInstanceOf(li, 'HTMLLIElement')) { | ||
return undefined; | ||
} | ||
|
||
if (li && safeInstanceOf(list, 'HTMLOListElement')) { | ||
const vList = new VList(list); | ||
const listItemIndex = vList.getListItemIndex(li); | ||
let stringToAnnounce = listItemIndex == -1 ? '' : listItemIndex.toString(); | ||
switch (list.style.listStyleType) { | ||
case 'lower-alpha': | ||
case 'lower-latin': | ||
case 'upper-alpha': | ||
case 'upper-latin': | ||
stringToAnnounce = convertDecimalsToAlpha(listItemIndex - 1); | ||
break; | ||
case 'lower-roman': | ||
case 'upper-roman': | ||
stringToAnnounce = convertDecimalsToRoman(listItemIndex); | ||
break; | ||
} | ||
|
||
return { | ||
defaultStrings: KnownAnnounceStrings.AnnounceListItemNumbering, | ||
formatStrings: [stringToAnnounce], | ||
}; | ||
} else if (safeInstanceOf(list, 'HTMLUListElement')) { | ||
return { | ||
defaultStrings: KnownAnnounceStrings.AnnounceListItemBullet, | ||
}; | ||
} | ||
return undefined; | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/roosterjs-editor-plugins/lib/plugins/Announce/AnnounceFeature.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,17 @@ | ||
import type { IEditor, AnnounceData } from 'roosterjs-editor-types'; | ||
|
||
/** | ||
* Represents a Announce feature used in Announce Plugin. | ||
* If the Should Handle Callback returns announce data, it will be announced by using a aria-live region. | ||
*/ | ||
export interface AnnounceFeature { | ||
/** | ||
* Whether to handle this feature, if returns Announce Data, will be announced, otherwise will do nothing. | ||
* @returns | ||
*/ | ||
shouldHandle: (editor: IEditor, lastFocusedElement: HTMLElement | null) => AnnounceData | false; | ||
/** | ||
* Keys handled in the current event | ||
*/ | ||
keys: number[]; | ||
} |
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
15 changes: 15 additions & 0 deletions
15
packages/roosterjs-editor-plugins/lib/plugins/Announce/features/AnnounceFeatures.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,15 @@ | ||
import announceNewListItemNumber from './announceNewListItem'; | ||
import announceWarningOnLastCell from './announceWarningOnLastTableCell'; | ||
import type { AnnounceFeature } from '../AnnounceFeature'; | ||
|
||
/** | ||
* Announce feature keys | ||
*/ | ||
export type AnnounceFeatureKey = 'announceNewListItem' | 'announceWarningOnLastTableCell'; | ||
/** | ||
* @internal | ||
*/ | ||
export const AnnounceFeatures: Record<AnnounceFeatureKey, AnnounceFeature> = { | ||
announceNewListItem: announceNewListItemNumber, | ||
announceWarningOnLastTableCell: announceWarningOnLastCell, | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/roosterjs-editor-plugins/lib/plugins/Announce/features/announceNewListItem.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,17 @@ | ||
import getAnnounceDataForList from '../../../pluginUtils/announceData/getAnnounceDataForList'; | ||
import { Keys } from 'roosterjs-editor-types'; | ||
import type { AnnounceFeature } from '../AnnounceFeature'; | ||
|
||
const LIST_SELECTOR = 'OL,UL'; | ||
const LIST_ITEM_SELECTOR = 'LI'; | ||
|
||
const announceNewListItemNumber: AnnounceFeature = { | ||
keys: [Keys.ENTER], | ||
shouldHandle: editor => { | ||
const li = editor.getElementAtCursor(LIST_ITEM_SELECTOR); | ||
const list = editor.getElementAtCursor(LIST_SELECTOR); | ||
return (!!(list && li) && getAnnounceDataForList(list, li)) || false; | ||
}, | ||
}; | ||
|
||
export default announceNewListItemNumber; |
40 changes: 40 additions & 0 deletions
40
.../roosterjs-editor-plugins/lib/plugins/Announce/features/announceWarningOnLastTableCell.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,40 @@ | ||
import { contains, safeInstanceOf } from 'roosterjs-editor-dom'; | ||
import { Keys, KnownAnnounceStrings, SelectionRangeTypes } from 'roosterjs-editor-types'; | ||
import type { AnnounceFeature } from '../AnnounceFeature'; | ||
|
||
const TABLE_CELL_SELECTOR = 'td,th'; | ||
const TABLE_SELECTOR = 'table'; | ||
|
||
const announceWarningOnLastCell: AnnounceFeature = { | ||
shouldHandle: (editor, lastFocusedElement) => { | ||
const selection = editor.getSelectionRangeEx(); | ||
|
||
return ( | ||
selection?.type == SelectionRangeTypes.Normal && | ||
selection.areAllCollapsed && | ||
selection.ranges.length === 1 && | ||
!contains( | ||
lastFocusedElement, | ||
selection.ranges[0].startContainer, | ||
true /*treatSameNodeAsContain*/ | ||
) && | ||
isLastCell() && { | ||
defaultStrings: KnownAnnounceStrings.AnnounceOnFocusLastCell, | ||
} | ||
); | ||
function isLastCell(): boolean { | ||
const table = editor.getElementAtCursor(TABLE_SELECTOR); | ||
|
||
if (safeInstanceOf(table, 'HTMLTableElement')) { | ||
const allCells = table.querySelectorAll(TABLE_CELL_SELECTOR); | ||
const focusedCell = editor.getElementAtCursor(TABLE_CELL_SELECTOR); | ||
|
||
return focusedCell == allCells.item(allCells.length - 1); | ||
} | ||
return false; | ||
} | ||
}, | ||
keys: [Keys.TAB, Keys.UP, Keys.DOWN, Keys.LEFT, Keys.RIGHT], | ||
}; | ||
|
||
export default announceWarningOnLastCell; |
2 changes: 2 additions & 0 deletions
2
packages/roosterjs-editor-plugins/lib/plugins/Announce/index.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 +1,3 @@ | ||
export { AnnounceFeatureKey } from './features/AnnounceFeatures'; | ||
export { AnnounceFeature } from './AnnounceFeature'; | ||
export { default as Announce } from './AnnouncePlugin'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -613,4 +613,3 @@ export default class PickerPlugin<T extends PickerDataProvider = PickerDataProvi | |
); | ||
} | ||
} | ||
|
Oops, something went wrong.