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 | 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 1x 1x 15x 15x 15x 1x 14x 3x 1x 3x 11x 11x 5x 11x 11x 11x 11x 11x 1x 12x 3x 9x 5x 1x 5x 5x 1x 8x 8x 8x 24x 22x 8x 1x 3x 3x 1x | import { ChangeSource, getObjectKeys } from 'roosterjs-content-model-dom'; import { isModelEmptyFast } from './isModelEmptyFast'; import type { WatermarkFormat } from './WatermarkFormat'; import type { EditorPlugin, IEditor, PluginEvent } from 'roosterjs-content-model-types'; const WATERMARK_CONTENT_KEY = '_WatermarkContent'; const styleMap: Record<keyof WatermarkFormat, string> = { fontFamily: 'font-family', fontSize: 'font-size', textColor: 'color', }; /** * A watermark plugin to manage watermark string for roosterjs */ export class WatermarkPlugin implements EditorPlugin { private editor: IEditor | null = null; private format: WatermarkFormat; private isShowing = false; private darkTextColor: string | null = null; private disposer: (() => void) | null = null; /** * Create an instance of Watermark plugin * @param watermark The watermark string */ constructor(protected watermark: string, format?: WatermarkFormat) { this.format = format || { fontSize: '14px', textColor: '#AAAAAA', }; } /** * Get a friendly name of this plugin */ getName() { return 'Watermark'; } /** * Initialize this plugin. This should only be called from Editor * @param editor Editor instance */ initialize(editor: IEditor) { this.editor = editor; this.disposer = this.editor.attachDomEvent({ compositionstart: { beforeDispatch: this.onCompositionStart, }, }); } /** * Dispose this plugin */ dispose() { this.disposer?.(); this.disposer = null; this.editor = null; } /** * Handle events triggered from editor * @param event PluginEvent object */ onPluginEvent(event: PluginEvent) { const editor = this.editor; Iif (!editor) { return; } if (event.eventType == 'input' && event.rawEvent.inputType == 'insertText') { // When input text, editor must not be empty, so we can do hide watermark now without checking content model this.showHide(editor, false /*isEmpty*/); } else if ( event.eventType == 'contentChanged' && (event.source == ChangeSource.SwitchToDarkMode || event.source == ChangeSource.SwitchToLightMode) && this.isShowing ) { // When the placeholder is shown and user switches the mode, we need to update watermark style if ( event.source == ChangeSource.SwitchToDarkMode && !this.darkTextColor && this.format.textColor ) { // Get the dark color only once when dark mode is enabled for the first time this.darkTextColor = editor .getColorManager() .getDarkColor(this.format.textColor, undefined, 'text'); } this.applyWatermarkStyle(editor); } else Eif ( event.eventType == 'editorReady' || event.eventType == 'contentChanged' || event.eventType == 'input' || event.eventType == 'beforeDispose' || event.eventType == 'compositionEnd' ) { this.update(editor); } } private onCompositionStart = () => { if (this.editor) { this.showHide(this.editor, false /*isEmpty*/); } }; private update(editor: IEditor) { editor.formatContentModel(model => { const isEmpty = isModelEmptyFast(model); this.showHide(editor, isEmpty); return false; }); } private showHide(editor: IEditor, isEmpty: boolean) { if (this.isShowing && !isEmpty) { this.hide(editor); } else if (!this.isShowing && isEmpty) { this.show(editor); } } protected show(editor: IEditor) { this.applyWatermarkStyle(editor); this.isShowing = true; } private applyWatermarkStyle(editor: IEditor) { let rule = `position: absolute; pointer-events: none; margin-inline-start: 1px; content: "${this.watermark}";`; const format = { ...this.format, textColor: editor.isDarkMode() ? this.darkTextColor : this.format.textColor, }; getObjectKeys(styleMap).forEach(x => { if (format[x]) { rule += `${styleMap[x]}: ${format[x]}!important;`; } }); editor.setEditorStyle(WATERMARK_CONTENT_KEY, rule, 'before'); } protected hide(editor: IEditor) { editor.setEditorStyle(WATERMARK_CONTENT_KEY, null); this.isShowing = false; } } |