Skip to content

Commit

Permalink
Fix #2903 Handle watermark for IME (#2913)
Browse files Browse the repository at this point in the history
* Fix #2903

* improve

* fix test
  • Loading branch information
JiuqingSong authored Jan 7, 2025
1 parent 0bc2578 commit 93b0cb2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class WatermarkPlugin implements EditorPlugin {
private format: WatermarkFormat;
private isShowing = false;
private darkTextColor: string | null = null;
private disposer: (() => void) | null = null;

/**
* Create an instance of Watermark plugin
Expand All @@ -43,12 +44,20 @@ export class WatermarkPlugin implements EditorPlugin {
*/
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;
}

Expand All @@ -63,10 +72,7 @@ export class WatermarkPlugin implements EditorPlugin {
return;
}

if (
(event.eventType == 'input' && event.rawEvent.inputType == 'insertText') ||
event.eventType == 'compositionEnd'
) {
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 (
Expand All @@ -92,16 +98,27 @@ export class WatermarkPlugin implements EditorPlugin {
event.eventType == 'editorReady' ||
event.eventType == 'contentChanged' ||
event.eventType == 'input' ||
event.eventType == 'beforeDispose'
event.eventType == 'beforeDispose' ||
event.eventType == 'compositionEnd'
) {
editor.formatContentModel(model => {
const isEmpty = isModelEmptyFast(model);

this.showHide(editor, isEmpty);
this.update(editor);
}
}

return false;
});
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ describe('WatermarkPlugin', () => {
let formatContentModelSpy: jasmine.Spy;
let isModelEmptyFastSpy: jasmine.Spy;
let setEditorStyleSpy: jasmine.Spy;
let attachDomEventSpy: jasmine.Spy;

const mockedModel = 'Model' as any;

beforeEach(() => {
isModelEmptyFastSpy = spyOn(isModelEmptyFast, 'isModelEmptyFast');
setEditorStyleSpy = jasmine.createSpy('setEditorStyle');
attachDomEventSpy = jasmine.createSpy('attachDomEvent');

formatContentModelSpy = jasmine
.createSpy('formatContentModel')
Expand All @@ -26,6 +28,7 @@ describe('WatermarkPlugin', () => {
formatContentModel: formatContentModelSpy,
setEditorStyle: setEditorStyleSpy,
isDarkMode: () => false,
attachDomEvent: attachDomEventSpy,
} as any;
});

Expand All @@ -36,6 +39,12 @@ describe('WatermarkPlugin', () => {

plugin.initialize(editor);

expect(attachDomEventSpy).toHaveBeenCalledWith({
compositionstart: {
beforeDispatch: jasmine.anything(),
},
});

plugin.onPluginEvent({
eventType: 'editorReady',
addedBlockElements: [],
Expand Down Expand Up @@ -169,6 +178,8 @@ describe('WatermarkPlugin dark mode', () => {
let setEditorStyleSpy: jasmine.Spy;
let getDarkColorSpy: jasmine.Spy;
let isDarkModeSpy: jasmine.Spy;
let attachDomEventSpy: jasmine.Spy;

const DEFAULT_DARK_COLOR_SUFFIX_COLOR = 'DarkColorMock-';

const mockedModel = 'Model' as any;
Expand All @@ -178,6 +189,7 @@ describe('WatermarkPlugin dark mode', () => {
setEditorStyleSpy = jasmine.createSpy('setEditorStyle');
getDarkColorSpy = jasmine.createSpy('getDarkColor');
isDarkModeSpy = jasmine.createSpy('isDarkMode');
attachDomEventSpy = jasmine.createSpy('attachDomEvent');

formatContentModelSpy = jasmine
.createSpy('formatContentModel')
Expand All @@ -191,6 +203,7 @@ describe('WatermarkPlugin dark mode', () => {
formatContentModel: formatContentModelSpy,
setEditorStyle: setEditorStyleSpy,
isDarkMode: isDarkModeSpy,
attachDomEvent: attachDomEventSpy,
getColorManager: () => ({
getDarkColor: getDarkColorSpy.and.callFake((color: string) => {
return `${DEFAULT_DARK_COLOR_SUFFIX_COLOR}${color}`;
Expand Down

0 comments on commit 93b0cb2

Please sign in to comment.