Move colors to CSS and fix problems with magnification of enclosures #1391
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR moves the handling of the explorer colors into CSS. Initially, this was to allow the info icon to use the same color, but it also make it possible to simplify the highlighters, which can now use CSS to control the colors, rather than setting them on the nodes themselves. That means you don't have to store and restore the colors, but can just use CSS to determine the colors. There are also several other issues that cropped up while working on this, with the biggest one being that the magnifier didn't handle the extra nodes that we now enclose in the explorer. That is taken care of here.
The main changes:
stylesattribute.rectin SVG output.The Details
The changes in
complexity/collapse.tsare to mark themtextnode used for collapsed sub-expressions withdata-mjx-collapsedfor easier reference in the highlighter and regions code.The
explorer.tschange makes the info icon have the correct background color.In
ExplorerPool.ts, we add a new parameter to distinguish the primary and secondary highlighters, since they need to know which CSS color variables to use. This does make things slightly less general than before, but it is still possible to have multiple additional highlighters if you wanted.The
Highlighter.tsfile has significant changes. Thetypethat I added earlier to theNamedColoredhas been removed (we now pass the type directly to the color functions). Since we are using CSS variables for the colors,getColorString()is no longer needed. The default colors no longer need the types, and the default background now has the correct alpha value.A new
ATTRobject is used to hold the various attribute names needed for the highlighting, to make it easier and less likely to have typos.Since the colors are now in CSS, the
foreground()andbackground()methods are not needed, and nether are the_foregroundand_backgroundproperties.The
getMactionNodes()method is added to the interface, since it is public.The
Highlightinterface is no longer needed, since we don't have to store the old colors any longer (the CSS overrides them instead).Hightlighthas been replaced byHTMLElementthroughout as a result.We don't need the
counter, since the attributes are now based on the primary/secondary value (saved as the new propertypriority). [The diff gets a little out of sync here, as it gets confused about which properties are old one with slight changes and which are new ones. Sorry about that]The
currentHighlightsis now just a list of lists of DOM nodes, since that is all that is needed anymore.The
constructor()now gets a priority (1 = primary, 2 = secondary, and you could add more if needed) and constructs theATTRproperty from that.The
highlight()method now simplifies since we don't need to createinfoobjects. The loop inhighlightAll()is simplified to usefor (const ... of ...)rather than a loop counter.A new
setColorCSS()method calls theLiveRegion.setColor()method to adjust the CSS for the given color type and priority (since the stylesheet holding the CSS for the colors is part of the LiveRegion class). ThesetColor()method now just calls thesetColorCSS()for the foreground and background colors.The
getMactionNodes()andisMactionNode()methods are made abstract since they are overridden in both output highlighters (the original code for these was never used). ThemactionNamegetter is removed since it is not used int he abstract class, and the individual output classes can just use the name they need explicitly.The removal of the
ATTR.ENCLOSEDattribute is moves tounsetHightlighted()as it is logically part of that function.The SVG and CHTML highlighters no longer need to set up
mactionName, and instead just use the explicit name (no need to waste space storing a pointer to it for every equation on the page).The SVG
highlightNode()method is now simplified since we don't have to worry about saving foreground and background colors. All we need to do is decide if we need to make a colored backgroundrector not. Similarly,unhighlightNode()is simpler, since we don't have to replace colors. We just have to decide whether to remove a temporaryrector not.For CHTML,
highlightnode()doesn't need to do anything (the CSS already handles it), and unhighlighting just checks if a temporary enclosure needs to be removed.The remaining changes in this file are to handle the new
priorityvalue.The changes in
KeyExplorer.tsare mostly to allow the magnifier to work without speech or Braille rendering. In the past, that would cause theaddSpeech()method to crash, so we check whether speech or Braille is enabled and skip that function if not. We move the setting of thetabIndexand theUpdate()call that would have been performed there to thesetCurrent()function, whereaddSpeech()is called, so that they are sure to be performed. Similarly, we check for speech and Braille in theUpdate()function.The
Region.Show()functions no longer need the highlighter (as we will see below), as the colors are handled through CSS. This is also true inMouseExplorer.ts.The
Region.tsfile also has significant changes. First, thegetHighlighterisn't needed (it was a redundant call that is removed below). Also, the highlighter no longer needs to be passed to theShow()method, since the colors are set by CSS, so the region colors don't have to be updated explicitly.A new
styleSheet()getter is added to get the stylesheet using its ID, for convenience.The highlight methods are removed as no longer needed (it is automatic with the CSS).
The style selectors are now created using
`...`strings.A
priorityobject is used to make priority names (primary and secondary) to their priority numbers.The
fgandbgnumbers for the two priorities are added to the CSS variable list, along with their alpha values.The LiveRegion styles now include a rule for setting the background color of the region's display automatically, and new rules for adding the foreground and background colors to the selected nodes during expression exploration. The primary rules are a bit complicated so as not to set the foreground color for collapsed sub-expressions, and not to set the background for magnified items (
data-mjx-clone-container) since the magnification region already has the background colorThe
setAlpha()function is now a more generalsetColor()method that sets the color and alpha values for a given type and priority.The HoverRegion gets CSS to set its background color automatically, as well, and we override the display margins so there isn't excessive space above and below the magnified sub-expression in displayed equations.
Again, the
hightlight()method is no longer needed, since the colors are added by CSS.The main changes to the
HoverRegionare in thecloneNode()method, which now has to handle the extra enclosed nodes, when needed. The method has been refactored to move the SVG and CHTML-specific code into separate functions. Some extra work is done move past the selection box (mjx-bboxnode in CHTML), and to handle the extra enclosed items, if any. The clonedmjx-containeris marked usingdata-mjx-clone-containerfor use in the CSS selectors.For CHTML output, the extra enclosed nodes are just cloned into the container (with the assumption that they follow it in the expression).
For SVG, we not only have to clone the nodes, but need to figure out their positions within the enclosure. A new
xy()method does that, and we use the values from that to set the element'stransformattribute to move it into the correct place. When there are extra nodes, we set the bounding box information from the enclosure node, not the selected one. Finally, we set up the SVG attributes to get the size and viewport correct.The
xy()method transforms the node's coordinate system's origin to the SVG's coordinate system, so we can tell how it was placed, even if it is not in the samegelement as the currently selected node.The change in
WebWorker.tsis so that the SRE attributes are applied to an maction's content, not therectused for its hit-box for clicking.The change in
HTMLMathItem.tsis to prevent an error that sometimes occurs in the lab when the expression has been removed before it is typeset. It checks that the parent item is available before trying to replace it with the newly typeset DOM nodes.The maction CSS for giving the collapsed nodes the blue color is simplified (now that we mark the collapsed node by
data-mjx-collapsedattributes).Finally, the
Menu.tscode is modified to have changes to the colors update the color CSS so that any flame highlighting will update immediately. (This probably means that we don't need to set the colors every time a new node is selected, as is currently being done. But theLiveRegion.setColor()checks that the CSS has changed before updating it, so there won't be CSS updates most of the time anyway.)Also, the explorer doesn't need to be loaded when collapsing is enabled, so that has been removed.
Finally, the
MathJax.startupvalue doesn't have to be cached in thererender()function, since it is only used once.