AutoCorrect
Typing corrects as it goes, as Word's default AutoCorrect does. Only typed input is corrected: text inserted through
the API (insertText) and pasted text never are. Each correction is its own undo step after the typing's, so Ctrl+Z
(Cmd+Z) right after one undoes only the correction. With track changes on, a correction is tracked as an ordinary
deletion and insertion.
export async function autoCorrect(editor: HeadlessEditor) {
// Typing corrects as it goes. Switch rules off, replace the list, and never correct some texts.
await editor.commands.setAutoCorrect({
smartQuotes: false,
entries: [
{ from: 'teh', to: 'the' },
{ from: '(c)', to: '©' },
],
never: ['adn'],
});
console.log(editor.getState().autoCorrect.never);
// The correction just made: put back what was typed, or that and never again.
if (editor.getState().commands.revertAutoCorrect.enabled) await editor.commands.revertAutoCorrect();
if (editor.getState().commands.neverAutoCorrect.enabled) await editor.commands.neverAutoCorrect();
// "AutoCorrect rules…" asks for the application's own dialog.
editor.on('dialogRequested', ({ kind }) => {
if (kind === 'autoCorrect') console.log('open the AutoCorrect rules dialog');
});
}
// Switch a rule off and replace the replacement list. Unset values are kept.
await session.Commands.SetAutoCorrectAsync(new AutoCorrectChange(SmartQuotes: false, Entries: [new("adn", "and"), new("teh", "the")]));
var options = session.State.AutoCorrect;
Console.WriteLine($"smart quotes {options.SmartQuotes}, never corrected: {string.Join(", ", options.Never)}");
// The correction at the caret: put back what was typed, or never make it again.
await session.Commands.RevertAutoCorrectAsync();
await session.Commands.NeverAutoCorrectAsync();
// The application draws the popovers; closed on its own, it closes the engine's too.
session.PopoverRequested += (_, e) =>
{
if (e.Popover is not Popover.AutoCorrect ac) return;
var menu = new ContextMenuStrip();
menu.Closed += async (_, c) =>
{
if (c.CloseReason != ToolStripDropDownCloseReason.ItemClicked) await session.Commands.ClosePopoverAsync();
};
menu.Items.Add($"Revert to {ac.From}", null, async (_, _) => await session.Commands.RevertAutoCorrectAsync());
menu.Items.Add($"Never correct {ac.From}", null, async (_, _) => await session.Commands.NeverAutoCorrectAsync());
menu.Show(owner, Point.Round(new PointF(ac.Anchor.Left, ac.Anchor.Bottom)));
};
The rules
Every rule is on by default. state.autoCorrect reports them.
| Option | Corrects |
|---|---|
twoInitialCapitals | THe → The (the initial-caps exceptions such as IDs stay) |
sentenceCapitals | the first letter of a sentence (not after an abbreviation in the first-letter exceptions, such as e.g.) |
cellCapitals | the first letter of a table cell |
dayNames | monday → Monday |
replaceText | the replacement list and the symbols: teh → the, (c) → © |
smartQuotes | straight quotes to curly ones |
ordinals | 1st with the suffix raised |
fractions | 1/2 → ½ |
dashes | hyphens to en and em dashes |
numberedLists | 1. or 1) then a space or Tab at a paragraph's start: a numbered list |
bulletedLists | * or - then a space or Tab at a paragraph's start: a bulleted list |
A correction runs when you finish what it corrects: a space, punctuation, Tab or Enter after it.
A list's start works only in a paragraph that isn't in a list yet. The marker goes, and the paragraph continues the list right before it when that list is the same kind, or starts a new one. One undo puts the marker back as typed.
Options
setAutoCorrect(change) switches the rules given and replaces the lists given. It's a session setting, not a document
change, so it has no undo step and isn't saved in the document.
- the rules above, each
trueorfalse; entries: [{ from, to }], in place of the shipped replacement list;firstLetterExceptionsandinitialCapsExceptions, in place of the shipped exception lists;never, the texts never corrected (what Never correct adds to).state.autoCorrect.neverlists them; an application that keeps them between sessions sets them again at start.
Revert and Never
While the caret is at a correction, the corrected text has a dashed accent underline and the engine draws a small
options button by it. A click on the button, or Alt+Shift+F10 (showAutoCorrectOptions), asks for the AutoCorrect
menu: popoverRequested with kind: 'autoCorrect', from (what was typed) and to (what it became). The menu offers:
| Entry | Does |
|---|---|
| Revert to from | revertAutoCorrect: puts back what was typed. Right after the correction it undoes it (documentChanged with origin undo); later it is one undo step of its own |
| Never correct from | neverAutoCorrect: reverts it as Revert does, adds from to never, and announces "Won't autocorrect from again" |
| AutoCorrect rules… | the application's own dialog: dialogRequested with kind: 'autoCorrect'. The engine supplies no values for it; build it on state.autoCorrect and setAutoCorrect |
The commands are available while there's a correction to act on (state.commands.revertAutoCorrect.enabled). The
mark goes once you type on or move away.
Your application draws the menu (see Appearance); the editor draws none.