Giter VIP home page Giter VIP logo

Comments (7)

arnog avatar arnog commented on June 25, 2024 1

@rosinghal yes, because there is no keybinding for "[". The keybinding that got filtered out was for "alt+leftbracket". If you want "leftbracket" to do something else, you can add a keybinding for it.

from mathlive.

arnog avatar arnog commented on June 25, 2024

The issue is most likely occurring because the mathfield is not properly mounted by the time you attempt top customize it (by the time you set the event listener). React lifecycle can be a bit tricky.

To ensure a mathfield is customized at the right time, I recommend two options:

1/ Use the onMount event handler. The TypeScript bindings for the math-field element currently do not have this event handler type appropriately, so you may need to do some convincing to have TypeScript let you use it.

2/ Use a useEffect hook, but wait on the ref to be ready.

  useEffect(() => {
    const mathfield = ref.current;
    // ...
  }, [ref.current]);

Also, note you do not need to copy the set of keybindings from source. You can simply filter the ones you don't want at runtime:

    mathfield.keybindings = mathfield.keybindings.filter((x) => !x.key.includes("[BracketLeft]"));

from mathlive.

rosinghal avatar rosinghal commented on June 25, 2024

Thanks for the suggestion. However, it does not work either. Here is my code

import { createElement, DetailedHTMLProps, HTMLAttributes, ReactElement, useEffect, useMemo, useRef } from 'react';

import Mathlive, { MathfieldElement } from 'mathlive';

type MathfieldProps = DetailedHTMLProps<HTMLAttributes<MathfieldElement>, MathfieldElement>;

export const Mathfield = ({ children, className, ...props }: MathfieldProps): ReactElement<MathfieldProps> => {
  const ref = useRef<MathfieldElement>(null);
  const mfe = useMemo(() => new MathfieldElement(), []);

  useEffect(() => {
    const mathfield = ref.current;

    if (mathfield) {
      mathfield.keybindings = mathfield.keybindings.filter(x => !x.key.includes('[BracketLeft]'));
    }
  }, [ref, mfe]);

  return createElement('math-field', { ref, class: className, 'data-testid': 'math-field', ...props }, children);
};

from mathlive.

rosinghal avatar rosinghal commented on June 25, 2024

Please check https://codesandbox.io/p/sandbox/exciting-aryabhata-th68p9

from mathlive.

arnog avatar arnog commented on June 25, 2024

I think it should work if you use the code I included. Try https://codesandbox.io/p/sandbox/exciting-aryabhata-forked-tll5zh?file=%2Fsrc%2FMathField.jsx

Note that the dependency in the useEffect is on ref.current. Also, you don't need the reference to mfe.

from mathlive.

rosinghal avatar rosinghal commented on June 25, 2024

@arnog even in the code you sent, if I type [ab] I get the value as \left\lbrack ab\right\rbrack even though we have filtered out keybindings with [BracketLeft].

You will see the output after typing in the math field and then click outside.

from mathlive.

rosinghal avatar rosinghal commented on June 25, 2024

Understood, I have updated the keybindings to

mathfield.keybindings = [
  ...mathfield.keybindings.filter(
    (x) => !x.key.includes("[BracketLeft]")
  ),
  {
    key: "[BracketLeft]",
    ifLayout: ["apple.en-intl", "windows.en-intl", "linux.en"],
    ifMode: "math",
    command: ["insert", "[#0]"],
  },
];

This is what I was looking for. Thanks a lot. Closing the issue.

from mathlive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.