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

Mac OS uses shortcut phrases BUG #1008

Open
1 task
Mass001-he opened this issue Sep 20, 2024 · 0 comments
Open
1 task

Mac OS uses shortcut phrases BUG #1008

Mass001-he opened this issue Sep 20, 2024 · 0 comments

Comments

@Mass001-he
Copy link

Mass001-he commented Sep 20, 2024

Bug description

Mac OS uses shortcut phrases on Google, enter the shortcut phrase in the Chinese input method, then double-click the prompt and parse the text error in the onChange of ReactQuill

ReactQuill version

  • v2.0.0

shortcut phrases

`
最新官网链接

bb.com
aa.com
`

Current solution

`
const beforeChangeDeltas = useRef<any[]>();
useEffect(() => {

  function compositionstartFn() {
    const editor = getEditor();
    if (!editor) {
      console.error('Editor is not ready');
      return;
    }
    const currentDeltas = editor.getContents();
    beforeChangeDeltas.current = currentDeltas.ops;
    console.log('feat before change', currentDeltas);
  }
  function compositionendFn(e: { data: any }) {
    const editor = getEditor();
    if (!editor) {
      console.error('feat Editor is not ready');
      return;
    }
    const data = e.data;
    const beforeDeltas = beforeChangeDeltas.current!;
    const index = quillRef.current?.editor?.getSelection(true).index ?? 0;
    const insertIndex = index - data.replaceAll('\n', '').length;
    let isInsert = false;
    const afterData = beforeDeltas.reduce(
      (prev, cur: { insert: any }) => {
        prev.newDeltas.push(cur);
        if (isInsert) return prev;
        if (typeof cur.insert === 'string') {
          const processedInsert = cur.insert.replace(/\r?\n$/, '');
          prev.curIdx += processedInsert.length;
          if (prev.curIdx >= insertIndex) {
            const left = processedInsert.substring(0, insertIndex);
            const right = processedInsert.substring(insertIndex);
            //拼接一个文本
            const newStr = left + data + right;
            cur.insert = newStr;
            isInsert = true;
          }
        } else {
          //emoji index 是1
          prev.curIdx += 1;
          if (prev.curIdx >= insertIndex) {
            prev.newDeltas.push({
              insert: data,
            });
            isInsert = true;
          }
        }
        return prev;
      },
      { curIdx: 0, newDeltas: [] },
    );
    console.log('feat after change', afterData.newDeltas);
    editor.setContents(afterData.newDeltas);
    queueMicrotask(() => {
      editor.setSelection(index, 0);
    });
  }
  if (
    navigator.userAgent.includes('Chrome') &&
    navigator.userAgent.includes('Mac OS')
  ) {
    // 兼容mac chrome 中文输入 中文输入时,会触发compositionstart和compositionend事件
    quillRef.current?.editor?.root?.addEventListener(
      'compositionstart',
      compositionstartFn,
      false,
    );

    quillRef.current?.editor?.root?.addEventListener(
      'compositionend',
      compositionendFn,
      false,
    );
  }

  return () => {
    if (
      navigator.userAgent.includes('Chrome') &&
      navigator.userAgent.includes('Mac OS')
    ) {
      quillRef.current?.editor?.root?.removeEventListener(
        'compositionstart',
        compositionstartFn,
      );
      quillRef.current?.editor?.root?.removeEventListener(
        'compositionend',
        compositionendFn,
      );
    }
  };
}, []);

`

@github-staff github-staff deleted a comment from SAMBILI Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant