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 | 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 1x 1x 12x 1x 1x 38x 38x 32x 32x 5x 5x 5x 1x 1x 1x 32x 32x 32x 32x 12x 10x 1x 1x 9x 12x 4x 2x 1x 1x 1x 4x 4x 2x 1x 1x 1x 4x 4x 2x 1x 1x 1x 4x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 5x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x | import { setFormat } from './utils/setFormat'; import type { ContentChangedEvent, ContentModelCodeFormat, EditorInputEvent, EditorPlugin, IEditor, KeyDownEvent, PluginEvent, } from 'roosterjs-content-model-types'; /** * * Options for Markdown plugin * - strikethrough: If true text between ~ will receive strikethrough format. * - bold: If true text between * will receive bold format. * - italic: If true text between _ will receive italic format. * - codeFormat: If provided, text between ` will receive code format. If equal to {}, it will set the default code format. */ export interface MarkdownOptions { strikethrough?: boolean; bold?: boolean; italic?: boolean; codeFormat?: ContentModelCodeFormat; } /** * @internal */ const DefaultOptions: Partial<MarkdownOptions> = { strikethrough: false, bold: false, italic: false, }; /** * Markdown plugin handles markdown formatting, such as transforming * characters into bold text. */ export class MarkdownPlugin implements EditorPlugin { private editor: IEditor | null = null; private shouldBold = false; private shouldItalic = false; private shouldStrikethrough = false; private shouldCode = false; private lastKeyTyped: string | null = null; /** * @param options An optional parameter that takes in an object of type MarkdownOptions, which includes the following properties: * - strikethrough: If true text between ~ will receive strikethrough format. Defaults to false. * - bold: If true text between * will receive bold format. Defaults to false. * - italic: If true text between _ will receive italic format. Defaults to false. * - codeFormat: If provided, text between ` will receive code format. Defaults to undefined. */ constructor(private Ioptions: MarkdownOptions = DefaultOptions) {} /** * Get name of this plugin */ getName() { return 'Markdown'; } /** * 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.disableAllFeatures(); this.lastKeyTyped = 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) { Eif (this.editor) { switch (event.eventType) { case 'input': this.handleEditorInputEvent(this.editor, event); break; case 'keyDown': this.handleBackspaceEvent(event); this.handleKeyDownEvent(event); break; case 'contentChanged': this.handleContentChangedEvent(event); break; } } } private handleEditorInputEvent(editor: IEditor, event: EditorInputEvent) { const rawEvent = event.rawEvent; const selection = editor.getDOMSelection(); Eif ( selection && selection.type == 'range' && selection.range.collapsed && rawEvent.inputType == 'insertText' ) { switch (rawEvent.data) { case '*': if (this.options.bold) { if (this.shouldBold) { setFormat(editor, '*', { fontWeight: 'bold' }); this.shouldBold = false; } else { this.shouldBold = true; } } break; case '~': if (this.options.strikethrough) { if (this.shouldStrikethrough) { setFormat(editor, '~', { strikethrough: true }); this.shouldStrikethrough = false; } else { this.shouldStrikethrough = true; } } break; case '_': if (this.options.italic) { if (this.shouldItalic) { setFormat(editor, '_', { italic: true }); this.shouldItalic = false; } else { this.shouldItalic = true; } } break; case '`': if (this.options.codeFormat) { if (this.shouldCode) { setFormat(editor, '`', {} /* format */, this.options.codeFormat); this.shouldCode = false; } else { this.shouldCode = true; } } break; } } } private handleKeyDownEvent(event: KeyDownEvent) { const rawEvent = event.rawEvent; Eif (!event.handledByEditFeature && !rawEvent.defaultPrevented) { switch (rawEvent.key) { case 'Enter': this.disableAllFeatures(); this.lastKeyTyped = null; break; case ' ': Eif (this.lastKeyTyped === '*' && this.shouldBold) { this.shouldBold = false; } else if (this.lastKeyTyped === '~' && this.shouldStrikethrough) { this.shouldStrikethrough = false; } else if (this.lastKeyTyped === '_' && this.shouldItalic) { this.shouldItalic = false; } else if (this.lastKeyTyped === '`' && this.shouldCode) { this.shouldCode = false; } this.lastKeyTyped = null; break; default: this.lastKeyTyped = rawEvent.key; break; } } } private handleBackspaceEvent(event: KeyDownEvent) { if (!event.handledByEditFeature && event.rawEvent.key === 'Backspace') { Eif (this.lastKeyTyped === '*' && this.shouldBold) { this.shouldBold = false; } else if (this.lastKeyTyped === '~' && this.shouldStrikethrough) { this.shouldStrikethrough = false; } else if (this.lastKeyTyped === '_' && this.shouldItalic) { this.shouldItalic = false; } else if (this.lastKeyTyped === '`' && this.shouldCode) { this.shouldCode = false; } this.lastKeyTyped = null; } } private handleContentChangedEvent(event: ContentChangedEvent) { Eif (event.source == 'Format') { this.disableAllFeatures(); } } private disableAllFeatures() { this.shouldBold = false; this.shouldItalic = false; this.shouldStrikethrough = false; this.shouldCode = false; } } |