All files / roosterjs-content-model-plugins/lib/announce AnnouncePlugin.ts

100% Statements 22/22
95.45% Branches 21/22
100% Functions 5/5
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 741x                     1x 11x 11x         1x 1x                 1x 11x               1x 12x 12x                 1x 12x 1x     11x 10x 8x       8x 7x                 10x     1x  
import { getIsSelectingOrUnselecting, retrieveStringFromParsedTable } from './tableSelectionUtils';
import type {
    IEditor,
    PluginEvent,
    EditorPlugin,
    DOMSelection,
} from 'roosterjs-content-model-types';
 
/**
 * AnnouncePlugin helps editor announce table selection changes for accessibility
 */
export class AnnouncePlugin implements EditorPlugin {
    private editor: IEditor | null = null;
    private previousSelection: DOMSelection | null = null;
 
    /**
     * Get name of this plugin
     */
    getName() {
        return 'Announce';
    }
 
    /**
     * The first method that editor will call to a plugin when editor is initializing.
     * It will pass in the editor instance, plugin should take this chance to save the
     * editor reference so that it can call to any editor method or format API later.
     * @param editor The editor object
     */
    initialize(editor: IEditor) {
        this.editor = editor;
    }
 
    /**
     * The last method that editor will call to a plugin before it is disposed.
     * Plugin can take this chance to clear the reference to editor. After this method is
     * called, plugin should not call to any editor method since it will result in error.
     */
    dispose() {
        this.editor = null;
        this.previousSelection = null;
    }
 
    /**
     * Core method for a plugin. Once an event happens in editor, editor will call this
     * method of each plugin to handle the event as long as the event is not handled
     * exclusively by another plugin.
     * @param event The event to handle:
     */
    onPluginEvent(event: PluginEvent) {
        if (!this.editor) {
            return;
        }
 
        if (event.eventType == 'selectionChanged') {
            if (event.newSelection?.type == 'table') {
                const action = getIsSelectingOrUnselecting(
                    this.previousSelection?.type == 'table' ? this.previousSelection : null,
                    event.newSelection
                );
                if (action && event.newSelection.tableSelectionInfo) {
                    this.editor.announce({
                        defaultStrings: action === 'unselecting' ? 'unselected' : 'selected',
                        formatStrings: [
                            retrieveStringFromParsedTable(event.newSelection.tableSelectionInfo),
                        ],
                    });
                }
            }
 
            this.previousSelection = event.newSelection;
        }
    }
}