Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2903 Handle watermark for IME #2913

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading