arnog / mathlive

A web component for easy math input
https://cortexjs.io/mathlive
MIT License
1.56k stars 277 forks source link

fonts- 404 #2450

Closed chenjiayu119 closed 2 months ago

chenjiayu119 commented 2 months ago

Description

react project fonts is 404

Steps to Reproduce

i try,

customElements.whenDefined('math-field').then(() => {
MathfieldElement.fontsDirectory = null;
const mathField = new MathfieldElement();
mfRef.current.appendChild(mathField);
});

but fail image

Actual Behavior

fonts is 404

Expected Behavior

I hope to prevent him from appearing 404. It would be best if the font file could be successfully loaded, and if not, disable it

Environment

0.100.0

Operating System Windows10

Browser Chrome

arnog commented 2 months ago

An attempt to load the fonts from the indicated location (https://172.16.64.95:9002/docs/static/fonts) is made before your code gets executed. That location does not have the needed fonts. In order to prevent that error message, make sure the fonts are available at that location.

By setting the fontsDirectory to null you are preventing the fonts from being loaded subsequently.

If you want to change the fonts directory, you should do so before the custom element is defined. Try something like this:

      MathfieldElement.fontsDirectory = "https://my-host/my-fonts-directory;
      customElements.whenDefined('math-field').then(() => {
        const mathField = new MathfieldElement();
        mfRef.current.appendChild(mathField);
      });
chenjiayu119 commented 2 months ago

I have tried following your method, but it still failed

  useEffect(() => {
      MathfieldElement.fontsDirectory = '';// 禁止加载字体(不起作用)
      // 设置静态属性
      customElements.whenDefined('math-field').then(() => {
        const mathField = new MathfieldElement();
        mfRef.current.appendChild(mathField);
        mathliveConfig();
        // 打开弹窗自动聚焦(位置在最后)
        setTimeout(() => {
          mfRef.current.executeCommand('moveToGroupEnd')
        }, 100);
        // 绑定点击事件,点击父级后进行聚焦
        mfRef.current.parentElement.addEventListener('click', () => {
          mfRef.current.focus();
        });
        // 自动聚焦
        onFocus()
      });
  }, []);

image