-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add announce plugin Add a AnnounceHandler, that will be in charge of announcing the messages by using a aria-live element, this handler will require a string map with the localized strings to announce messages from built-in RoosterJS features. Add an additional callback property to ContentEditEventData, getAnnounceData, used in the Announce Plugin Add first announcing logic when indenting/outdenting list
- Loading branch information
1 parent
43a947b
commit a0d3e94
Showing
22 changed files
with
465 additions
and
25 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
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
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
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 @@ | ||
export * from './plugins/Announce/index'; |
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
133 changes: 133 additions & 0 deletions
133
packages/roosterjs-editor-plugins/lib/plugins/Announce/AnnouncePlugin.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,133 @@ | ||
import { createElement } from 'roosterjs-editor-dom'; | ||
import { PluginEventType } from 'roosterjs-editor-types'; | ||
import type { CompatibleKnownAnnounceStrings } from 'roosterjs-editor-types/lib/compatibleTypes'; | ||
import type { | ||
EditorPlugin, | ||
IEditor, | ||
PluginEvent, | ||
AnnounceData, | ||
KnownAnnounceStrings, | ||
} from 'roosterjs-editor-types'; | ||
|
||
const ARIA_LIVE_STYLE = | ||
'clip: rect(0px, 0px, 0px, 0px); clip-path: inset(100%); height: 1px; overflow: hidden; position: absolute; white-space: nowrap; width: 1px;'; | ||
const ARIA_LIVE_ASSERTIVE = 'assertive'; | ||
const DIV_TAG = 'div'; | ||
const createAriaLiveElement = (document: Document): HTMLDivElement => { | ||
const element = createElement( | ||
{ | ||
tag: DIV_TAG, | ||
style: ARIA_LIVE_STYLE, | ||
attributes: { | ||
'aria-live': ARIA_LIVE_ASSERTIVE, | ||
}, | ||
}, | ||
document | ||
) as HTMLDivElement; | ||
|
||
document.body.appendChild(element); | ||
|
||
return element; | ||
}; | ||
|
||
/** | ||
* Announce messages to screen reader by using aria live element. | ||
*/ | ||
export default class Announce implements EditorPlugin { | ||
private ariaLiveElement: HTMLDivElement | undefined; | ||
private editor: IEditor | undefined; | ||
|
||
constructor( | ||
private stringsMapOrGetter?: | ||
| Map<CompatibleKnownAnnounceStrings | KnownAnnounceStrings, string> | ||
| ((key: CompatibleKnownAnnounceStrings | KnownAnnounceStrings) => string) | ||
| undefined | ||
) {} | ||
|
||
/** | ||
* Get a friendly name of this plugin | ||
*/ | ||
getName() { | ||
return 'Announce'; | ||
} | ||
|
||
/** | ||
* Initialize this plugin | ||
* @param editor The editor instance | ||
*/ | ||
initialize(editor: IEditor) { | ||
this.editor = editor; | ||
} | ||
|
||
/** | ||
* Dispose this plugin | ||
*/ | ||
dispose() { | ||
this.ariaLiveElement?.parentElement?.removeChild(this.ariaLiveElement); | ||
this.ariaLiveElement = undefined; | ||
} | ||
|
||
/** | ||
* Handle events triggered from editor | ||
* @param event PluginEvent object | ||
*/ | ||
onPluginEvent(event: PluginEvent) { | ||
if ( | ||
this.editor && | ||
event.eventType == PluginEventType.ContentChanged && | ||
event.additionalData?.getAnnounceData | ||
) { | ||
const data = event.additionalData.getAnnounceData(); | ||
if (data) { | ||
this.announce(data, this.editor); | ||
} | ||
} | ||
} | ||
|
||
protected announce(announceData: AnnounceData, editor: IEditor) { | ||
const { text, defaultStrings, formatStrings = [] } = announceData; | ||
let textToAnnounce = formatString(this.getString(defaultStrings) || text, formatStrings); | ||
if (textToAnnounce) { | ||
if (!this.ariaLiveElement || textToAnnounce == this.ariaLiveElement?.textContent) { | ||
this.ariaLiveElement?.parentElement?.removeChild(this.ariaLiveElement); | ||
this.ariaLiveElement = createAriaLiveElement(editor.getDocument()); | ||
} | ||
if (this.ariaLiveElement) { | ||
this.ariaLiveElement.textContent = textToAnnounce; | ||
} | ||
} | ||
} | ||
|
||
private getString(key: CompatibleKnownAnnounceStrings | KnownAnnounceStrings | undefined) { | ||
if (this.stringsMapOrGetter == undefined || key == undefined) { | ||
return undefined; | ||
} | ||
|
||
if (typeof this.stringsMapOrGetter === 'function') { | ||
return this.stringsMapOrGetter(key); | ||
} else { | ||
return this.stringsMapOrGetter.get(key); | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
* Public only for unit testing. | ||
* @returns | ||
*/ | ||
public getAriaLiveElement() { | ||
return this.ariaLiveElement; | ||
} | ||
} | ||
|
||
function formatString(text: string | undefined, formatStrings: string[]) { | ||
if (text == undefined) { | ||
return text; | ||
} | ||
|
||
formatStrings.forEach((value, index) => { | ||
text = text?.replace(`{${index}}`, value); | ||
}); | ||
|
||
return text; | ||
} |
1 change: 1 addition & 0 deletions
1
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as AnnouncePlugin } 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
Oops, something went wrong.