arnog / mathlive

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

#0 as part of insert doesn't become a placeholder #33

Closed NSoiffer closed 6 years ago

NSoiffer commented 6 years ago

Description

Here is the documentation that describes how #0 should work:

.insert(content, options) insert the specified content at the current insertion point. With options it is possible to specify the insertion mode, as well as what will be selected after the insertion. If the content contains a #? a placeholder will be indicated in its stead. The #0 sequence will be replaced by the item currently selected (or a placeholder if nothing is selected)

In practice, doing an insert with #0 does nothing with a few exceptions (fractions, square roots, ???).

Steps to Reproduce

Inline shortcuts are an easy way to see the problem.

  1. open mathlive.io
  2. type alt-[

What is inserted is \left[\right] -- no placeholder. If you type ctrl-2, you get a square root with a radicand placeholder.

Both shortcuts use #0 (see editor-shortcuts.js).

The bug appears to be in Parser.prototype.scanImplicitGroup

arnog commented 6 years ago

There are different concepts of "placeholder". The first one is an argument placeholder, which can be indicated in a formula using "#0", "#1". This indicates that the placeholder should be substituted by a value provided programatically. In many cases, "#0" will refer to whatever is currently selected, so some shortcuts, such as alt-[, are defined to replace the selection with a sequence that contains the selection (represented by #0). The issue here is that if there is no selection, the "#0" placeholder was left empty. The fix is in that case to use a default value for the placeholder when no argument list/selection is available.

The second concept of "placeholder" is a value that has not been specified yet. For example, the expression "\frac{}{}" has two placeholders for the missing numerator and denominator of the fraction.

Although in both cases this can result in a "placeholder" value being displayed in the formula, they are different mechanisms and should not be conflated. Hence, there is a difference between a "#" token (which represent an argument to be substituted later) and a "placeholder" token which represent a missing child.