All files / roosterjs-content-model-plugins/lib/shortcut shortcuts.ts

97.83% Statements 45/46
100% Branches 2/2
93.75% Functions 15/16
97.83% Lines 45/46

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 2481x 1x 1x                     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x           2x               1x           2x               1x           2x               1x           2x               1x           2x               1x           1x                 1x           1x                   1x                             1x           2x               1x           2x               1x           2x               1x           2x               1x           2x               1x             1x                   1x             1x        
import { redo, undo } from 'roosterjs-content-model-core';
import { setShortcutIndentationCommand } from './utils/setShortcutIndentationCommand';
import {
    changeFontSize,
    clearFormat,
    toggleBold,
    toggleBullet,
    toggleItalic,
    toggleNumbering,
    toggleUnderline,
} from 'roosterjs-content-model-api';
import type { ShortcutCommand } from './ShortcutCommand';
 
const enum Keys {
    BACKSPACE = 8,
    SPACE = 32,
    B = 66,
    I = 73,
    U = 85,
    Y = 89,
    Z = 90,
    COMMA = 188,
    PERIOD = 190,
    FORWARD_SLASH = 191,
    ArrowRight = 39,
    ArrowLeft = 37,
}
 
/**
 * Shortcut command for Bold
 * Windows: Ctrl + B
 * MacOS: Meta + B
 */
export const ShortcutBold: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: false,
        which: Keys.B,
    },
    onClick: editor => toggleBold(editor),
};
 
/**
 * Shortcut command for Italic
 * Windows: Ctrl + I
 * MacOS: Meta + I
 */
export const ShortcutItalic: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: false,
        which: Keys.I,
    },
    onClick: editor => toggleItalic(editor),
};
 
/**
 * Shortcut command for Underline
 * Windows: Ctrl + U
 * MacOS: Meta + U
 */
export const ShortcutUnderline: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: false,
        which: Keys.U,
    },
    onClick: editor => toggleUnderline(editor),
};
 
/**
 * Shortcut command for Clear Format
 * Windows: Ctrl + Space
 * MacOS: Meta + Space, this shortcut is the same as the default global spotlight shortcut, so it is invalid if the user keeps spotlight‘s.
 */
export const ShortcutClearFormat: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: false,
        which: Keys.SPACE,
    },
    onClick: editor => clearFormat(editor),
};
 
/**
 * Shortcut command for Undo 1
 * Windows: Ctrl + Z
 * MacOS: Meta + Z
 */
export const ShortcutUndo: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: false,
        which: Keys.Z,
    },
    onClick: editor => undo(editor),
};
 
/**
 * Shortcut command for Undo 2
 * Windows: Alt + Backspace
 * MacOS: N/A
 */
export const ShortcutUndo2: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'alt',
        shiftKey: false,
        which: Keys.BACKSPACE,
    },
    onClick: editor => undo(editor),
    environment: 'nonMac',
};
 
/**
 * Shortcut command for Redo 1
 * Windows: Ctrl + Y
 * MacOS: N/A
 */
export const ShortcutRedo: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: false,
        which: Keys.Y,
    },
    onClick: editor => redo(editor),
    environment: 'nonMac',
};
 
/**
 * @deprecated
 * Shortcut command for Redo 2
 * Windows: N/A
 * MacOS: Meta + Shift + Z
 */
export const ShortcutRedoMacOS: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: true,
        which: Keys.Z,
    },
    onClick: editor => redo(editor),
    environment: 'mac',
};
 
/**
 * Shortcut command for Redo 3
 * Windows: Ctrl + Shift + Z
 * MacOS: Meta + Shift + Z
 */
export const ShortcutRedoAlt: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: true,
        which: Keys.Z,
    },
    onClick: editor => redo(editor),
};
 
/**
 * Shortcut command for Bullet List
 * Windows: Ctrl + . (Period)
 * MacOS: Meta + . (Period)
 */
export const ShortcutBullet: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: false,
        which: Keys.PERIOD,
    },
    onClick: editor => toggleBullet(editor),
};
 
/**
 * Shortcut command for Numbering List
 * Windows: Ctrl + / (Forward slash)
 * MacOS: Meta + / (Forward slash)
 */
export const ShortcutNumbering: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: false,
        which: Keys.FORWARD_SLASH,
    },
    onClick: editor => toggleNumbering(editor),
};
 
/**
 * Shortcut command for Increase Font
 * Windows: Ctrl + Shift + . (Period)
 * MacOS: Meta + Shift + . (Period)
 */
export const ShortcutIncreaseFont: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: true,
        which: Keys.PERIOD,
    },
    onClick: editor => changeFontSize(editor, 'increase'),
};
 
/**
 * Shortcut command for Decrease Font
 * Windows: Ctrl + Shift + , (Comma)
 * MacOS: Meta + Shift + , (Comma)
 */
export const ShortcutDecreaseFont: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'ctrl',
        shiftKey: true,
        which: Keys.COMMA,
    },
    onClick: editor => changeFontSize(editor, 'decrease'),
};
 
/**
 * Shortcut command for Intent list
 * Windows: Alt + Shift + Arrow Right
 * MacOS: N/A
 */
export const ShortcutIndentList: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'alt',
        shiftKey: true,
        which: Keys.ArrowRight,
    },
    onClick: editor => {
        setShortcutIndentationCommand(editor, 'indent');
    },
    environment: 'nonMac',
};
 
/**
 * Shortcut command for Outdent list
 * Windows: Alt + Shift + Arrow Left
 * MacOS: N/A
 */
export const ShortcutOutdentList: ShortcutCommand = {
    shortcutKey: {
        modifierKey: 'alt',
        shiftKey: true,
        which: Keys.ArrowLeft,
    },
    onClick: editor => {
        setShortcutIndentationCommand(editor, 'outdent');
    },
    environment: 'nonMac',
};