Skip to content

Improve suggestion handling for chinese input #1292

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

Closed
wants to merge 2 commits into from
Closed
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
35 changes: 33 additions & 2 deletions packages/core/src/extensions/SuggestionMenu/SuggestionPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class SuggestionMenuView<

this.pluginState = stopped ? prev : next;

if (stopped || !this.editor.isEditable) {
if (stopped || !this.editor.isEditable || this.pluginState?.composing) {
Copy link

@ClytzeL ClytzeL Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like the suggestion menu to always open whenever the user is typing between the compositionStart and compositionEnd events.But here colsed the menu.Maybe like the code follows?

...
let _isComposing = false; 
export class SuggestionMenuProseMirrorPlugin<
...
apply(transaction, prev, _oldState, newState): SuggestionPluginState {
          if (_isComposing) {
            return prev;
          }
...
props: {
        handleDOMEvents:{
          compositionstart:(view)=>{
            console.log('run compositionstart');
            _isComposing = true;
            view.dispatch(view.state.tr.setMeta("isComposing", true));
            return false;
          },
          compositionend:(view)=>{
            console.log('run compositionend');
            _isComposing = false;
            view.dispatch(view.state.tr.setMeta("isComposing", false));
            return false;
          }
        },
        handleTextInput(view, _from, _to, text) {
...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ClytzeL : I have updated the code. Can you please test again ?

this.state!.show = false;
this.emitUpdate(this.pluginState!.triggerCharacter);

Expand Down Expand Up @@ -148,10 +148,12 @@ type SuggestionPluginState =
query: string;
decorationId: string;
ignoreQueryLength?: boolean;
composing: boolean;
}
| undefined;

const suggestionMenuPluginKey = new PluginKey("SuggestionMenuPlugin");
let isComposing = false;

/**
* A ProseMirror plugin for suggestions, designed to make '/'-commands possible as well as mentions.
Expand Down Expand Up @@ -192,11 +194,25 @@ export class SuggestionMenuProseMirrorPlugin<
state: {
// Initialize the plugin's internal state.
init(): SuggestionPluginState {
return undefined;
return {
triggerCharacter: "",
deleteTriggerCharacter: false,
queryStartPos: 0,
query: "",
decorationId: "",
ignoreQueryLength: false,
composing: false
};
},

// Apply changes to the plugin state from an editor transaction.
apply(transaction, prev, _oldState, newState): SuggestionPluginState {

if (isComposing) {
// Ensure the menu stays open by keeping the previous state.
return prev;
}

// TODO: More clearly define which transactions should be ignored.
if (transaction.getMeta("orderedListIndexing") !== undefined) {
return prev;
Expand Down Expand Up @@ -232,6 +248,7 @@ export class SuggestionMenuProseMirrorPlugin<
decorationId: `id_${Math.floor(Math.random() * 0xffffffff)}`,
ignoreQueryLength:
suggestionPluginTransactionMeta?.ignoreQueryLength,
composing: false,
};
}

Expand Down Expand Up @@ -266,11 +283,25 @@ export class SuggestionMenuProseMirrorPlugin<
newState.selection.from
);

next.composing = true;

return next;
},
},

props: {
handleDOMEvents: {
compositionstart: (view) => {
isComposing = true;
view.dispatch(view.state.tr.setMeta("isComposing", true));
return false;
},
compositionend: (view) => {
isComposing = false;
view.dispatch(view.state.tr.setMeta("isComposing", false));
return false;
},
},
handleTextInput(view, _from, _to, text) {
const suggestionPluginState: SuggestionPluginState = (
this as Plugin
Expand Down