Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/MathLive plugin: restyle to match the app's appearance

Adorian Doran012d97610d67
diff --git a/apps/client/src/stylesheets/theme-next/forms.css b/apps/client/src/stylesheets/theme-next/forms.css
index 906716b98d..da76993c07 100644
--- a/apps/client/src/stylesheets/theme-next/forms.css
+++ b/apps/client/src/stylesheets/theme-next/forms.css
@@ -50,6 +50,7 @@ input[type="datetime-local"],
textarea.form-control,
textarea,
:root textarea.ck.ck-textarea,
+:root .ck.ck-math-input > .ck-mathlive-container,
.tn-input-field {
outline: 3px solid transparent;
outline-offset: 6px;
@@ -72,6 +73,7 @@ input[type="datetime-local"]:hover,
textarea.form-control:hover,
textarea:hover,
:root textarea.ck.ck-textarea:hover,
+:root .ck.ck-math-input > .ck-mathlive-container:hover,
.tn-input-field:hover {
background: var(--input-hover-background);
color: var(--input-hover-color);
@@ -90,6 +92,7 @@ input[type="datetime-local"]:focus,
textarea.form-control:focus,
textarea:focus,
:root textarea.ck.ck-textarea:focus,
+:root .ck.ck-math-input > .ck-mathlive-container:focus-within,
.tn-input-field:focus,
.tn-input-field:focus-within {
box-shadow: unset;
diff --git a/apps/client/src/stylesheets/theme-next/notes/text.css b/apps/client/src/stylesheets/theme-next/notes/text.css
index 9da19e0006..1d7424de99 100644
--- a/apps/client/src/stylesheets/theme-next/notes/text.css
+++ b/apps/client/src/stylesheets/theme-next/notes/text.css
@@ -8,7 +8,8 @@
.ck.ck-toolbar,
.ck.ck-block-toolbar-button,
-.ck.ck-color-selector {
+.ck.ck-color-selector,
+.ck-math-form .ck-math-button-row {
--ck-color-toolbar-background: transparent;
--ck-color-button-default-background: transparent;
@@ -87,7 +88,8 @@
/* Focus visible */
.ck.ck-toolbar,
.ck.ck-block-toolbar-button,
-.ck.ck-dropdown__panel {
+.ck.ck-dropdown__panel,
+.ck-math-form .ck-math-button-row {
--ck-focus-ring: 1px solid transparent;
--ck-button-focus-border-color: transparent;
--ck-color-focus-border: var(--input-focus-outline-color);
@@ -96,7 +98,8 @@
}
.ck.ck-toolbar .ck-button:focus-visible,
-.ck.ck-dropdown__panel .ck-button:focus-visible {
+.ck.ck-dropdown__panel .ck-button:focus-visible,
+.ck-math-form .ck-math-button-row .ck-button:focus-visible {
--ck-focus-ring: 1px solid var(--input-focus-outline-color);
--ck-button-focus-border-color: var(--input-focus-outline-color);
}
@@ -634,10 +637,103 @@ div.ck-template-form .ck-search__info span:nth-child(2) {
/* Math popup */
-.ck-math-form .ck-labeled-field-view {
- --ck-input-label-height: 0;
+/* Save and cancel buttons, drawn as the color picker's */
+:root .ck-math-form .ck-math-button-row {
+ --ck-button-border-radius: var(--ck-border-radius);
+}
+
+/* Room inside the scrolling view for the fields' focus ring animation */
+:root .ck.ck-math-input > :is(.ck-mathlive-container, .ck-latex-textarea) {
+ margin: 4px;
+}
+
+:root .ck.ck-math-input > .ck-mathlive-container {
+ padding-inline: 12px;
+}
+
+:root .ck.ck-math-input > .ck-latex-textarea {
+ padding-inline: 16px;
+}
+
+/* MathLive's keyboard and menu toggles, in the colors of icon buttons (buttons.css) */
+:root .ck-math-form math-field::part(virtual-keyboard-toggle),
+:root .ck-math-form math-field::part(menu-toggle) {
+ color: var(--icon-button-color);
+}
+
+:root .ck-math-form math-field::part(virtual-keyboard-toggle):hover,
+:root .ck-math-form math-field::part(menu-toggle):hover {
+ background: var(--icon-button-hover-background);
+ color: var(--icon-button-hover-color);
+}
+
+/* MathLive's menu, drawn as a CKEditor dropdown menu */
+:root .ck-math-form math-field {
+ --ui-font-family: var(--ck-font-face);
+ --ui-font-size: 0.9rem;
+ --ui-menu-bg: var(--menu-background-color);
+ --ui-menu-text: var(--ck-color-text);
+ --ui-menu-bg-active: var(--hover-item-background-color);
+ --ui-menu-text-active: var(--ck-color-text);
+ --ui-menu-bg-active-muted: var(--hover-item-background-color);
+ --ui-menu-text-active-muted: var(--ck-color-text);
+ --ui-menu-divider: var(--menu-item-delimiter-color);
+ --ui-menu-shadow: 0px 10px 20px rgba(0, 0, 0, var(--dropdown-shadow-opacity));
+
+ /* Rows placed as CKEditor's list rows: 27px tall, 4px apart, 11px from the menu's edge */
+ --ui-line-height: 21px;
+ --ck-math-menu-item-margin: 4px 11px;
+ --ck-math-menu-item-padding: 1px 15px;
+ --ck-math-menu-item-border-radius: 6px;
+
+ /* Headings drawn as the app menus' (menus.css) */
+ --ck-math-menu-heading-margin: 0 0 8px;
+ --ck-math-menu-heading-padding: 2px 26px 1px;
+ --ck-math-menu-heading-border: 1px solid var(--menu-item-delimiter-color);
+ --ck-math-menu-heading-opacity: 1;
+ --ck-math-menu-heading-font-size: 0.8em;
+ --ck-math-menu-heading-font-weight: 500;
+ --ck-math-menu-heading-letter-spacing: 1pt;
+ --ck-math-menu-heading-text-transform: uppercase;
+ --ck-math-menu-heading-color: var(--menu-item-group-header-color);
+
+ /* Checkmarks drawn as the app menus' trailing checks (menus.css), as far from the row's end as
+ its text is from the start */
+ --ck-math-menu-checkmark-order: 1;
+ --ck-math-menu-checkmark-margin-inline: 12px 2px;
+ --ck-math-menu-checkmark-color: var(--menu-item-icon-color);
+ --ck-math-menu-checkmark-indent: 0;
+
+ /* Color cells drawn as the tiles of CKEditor's color grids */
+ --ck-math-menu-swatch-size: var(--ck-color-grid-tile-size);
+ --ck-math-menu-swatch-gap: 8px;
+ --ck-math-menu-swatch-background: transparent;
+ --ck-math-menu-swatch-border-radius: 4px;
+ --ck-math-menu-swatch-shadow: inset 0 0 0 1px var(--main-border-color);
+}
+
+:root .ck-math-form math-field::part(ui-menu-container) {
+ /* An outline, as a border would take the room of the color grid's fourth column */
+ outline: 1px solid var(--ck-editor-popup-border-color);
+ outline-offset: -1px;
+ border-radius: var(--dropdown-border-radius);
+ padding-block: 7px;
+ backdrop-filter: var(--dropdown-backdrop-filter);
+}
- margin-inline-end: 8px;
+:root .ck-math-form math-field::part(menu-divider) {
+ margin: calc(.5em + 2px) 0;
+ padding: 0;
+ width: 100%;
+ opacity: 1;
+}
+
+body.background-effects .ck-math-form math-field {
+ --ui-menu-bg: var(--menu-background-color-no-backdrop);
+}
+
+body.background-effects .ck-math-form math-field::part(ui-menu-container) {
+ backdrop-filter: none;
}
/* Special character and emoji tiles, drawn as the cells of the note icon list (shell.css) */
diff --git a/packages/ckeditor5/src/plugins/math/README.md b/packages/ckeditor5/src/plugins/math/README.md
index 25c6756c10..3972d2f280 100644
--- a/packages/ckeditor5/src/plugins/math/README.md
+++ b/packages/ckeditor5/src/plugins/math/README.md
@@ -23,6 +23,44 @@ and opt-in — it turns `$$` or `\[` at the start of a block into an equation pr
| `utils.ts` | Delimiter helpers, `renderEquation`, balloon positioning |
| `typings_external.ts` | Types for KaTeX and MathJax, which are read off globals |
+## Styling
+
+`theme/math_form.css` draws the dialog from CKEditor's tokens, and a theme draws over it (the Next
+theme does so in `theme-next/forms.css` and `theme-next/notes/text.css`). MathLive renders its
+field and its menu in the math field's shadow root, which a theme reaches only through `::part()`
+and MathLive's `--ui-*` variables. As `::part(menu-item)` also matches the headings and the cells
+of the matrix and color grids, and no part reaches a color swatch's color or a checkmark,
+`MathInputView` adopts a stylesheet into that root that reads the look of the menu rows, headings,
+checkmarks and swatches from variables of its own:
+
+| Variable | Default (MathLive's own) |
+|---|---|
+| `--ck-math-menu-item-margin` | `0 6px` |
+| `--ck-math-menu-item-padding` | `1px 7px` |
+| `--ck-math-menu-item-border-radius` | `4px` |
+| `--ck-math-menu-heading-margin` | `0.5em 6px 0` |
+| `--ck-math-menu-heading-padding` | `1px 7px` |
+| `--ck-math-menu-heading-border` | `none` (a bottom border) |
+| `--ck-math-menu-heading-opacity` | `0.5` on the row, `0.4` on its label |
+| `--ck-math-menu-heading-font-size` | inherited |
+| `--ck-math-menu-heading-font-weight` | `bold` |
+| `--ck-math-menu-heading-letter-spacing` | inherited |
+| `--ck-math-menu-heading-text-transform` | `none` |
+| `--ck-math-menu-heading-color` | inherited |
+| `--ck-math-menu-checkmark-order` | `0` (before the label; `1` puts it after) |
+| `--ck-math-menu-checkmark-margin-inline` | `-4px -11px` |
+| `--ck-math-menu-checkmark-color` | inherited |
+| `--ck-math-menu-checkmark-indent` | `12px` (the labels' room for a leading checkmark) |
+| `--ck-math-menu-swatch-size` | `2rem` |
+| `--ck-math-menu-swatch-gap` | `18px` (the grid keeps its four columns) |
+| `--ck-math-menu-swatch-background` | `var(--neutral-200)`, `var(--neutral-100)` when active |
+| `--ck-math-menu-swatch-border-radius` | `50%`, `2px` when active |
+| `--ck-math-menu-swatch-shadow` | `none` |
+
+The same stylesheet fixes the tooltips of the keyboard and menu toggles above them with CSS anchor
+positioning, flipping them below when there is no room, as the scrolling field would clip them.
+Where `position-area` is unsupported, they keep MathLive's placement.
+
## Storage formats
Which one is produced depends on `config.math.outputType` (Trilium uses `span`):
diff --git a/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts b/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts
index e08032b4f8..3124068fd0 100644
--- a/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts
+++ b/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts
@@ -3,7 +3,7 @@ import './math.js';
import { type DISPLAYABLE_LOCALE_IDS, LOCALES } from '@triliumnext/commons';
import { Locale } from 'ckeditor5';
-import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeAll, beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest';
import getCkLocale from '../../i18n.js';
import MathInputView, { LatexTextAreaView, MathFieldFocusableView } from './math_input_view.js';
@@ -32,18 +32,45 @@ describe( 'MathInputView', () => {
return view.latexTextAreaView.element;
}
+ /** Margin, padding and corner radius of a menu row. */
+ function spacing( item: Element ): string[] {
+ const style = getComputedStyle( item );
+ return [ style.margin, style.padding, style.borderRadius ];
+ }
+
afterEach( () => {
view?.element?.remove();
view?.destroy();
vi.restoreAllMocks();
} );
+ // First in the file, so MathLive is not yet loaded in this file's frame.
+ it( 'styles the menu of a field given its value before MathLive loads', async () => {
+ expect( customElements.get( 'math-field' ) ).toBeUndefined();
+ view = new MathInputView( new Locale() );
+ view.render();
+ document.body.appendChild( view.element as HTMLElement );
+ view.value = 'x^2';
+
+ await vi.waitFor( () => expect( view.mathfield?.shadowRoot ).toBeTruthy(), { timeout: 10000 } );
+ const mathfield = view.mathfield as MathField;
+ const menu = document.createElement( 'menu' );
+ menu.className = 'ui-menu-container';
+ menu.innerHTML = '<li part="menu-item"></li>';
+ ( mathfield.shadowRoot as ShadowRoot ).append( menu );
+ mathfield.style.setProperty( '--ck-math-menu-item-padding', '1px 15px' );
+
+ expect( getComputedStyle( menu.children[ 0 ] ).padding ).toBe( '1px 15px' );
+ expect( mathfield.value ).toBe( 'x^2' );
+ } );
+
it( 'renders a LaTeX textarea, a label and a MathLive container', async () => {
await renderAndWait();
expect( view.element?.querySelector( '.ck-mathlive-container' ) ).not.toBeNull();
expect( view.element?.querySelector( '.ck-latex-label' ) ).not.toBeNull();
expect( textarea().tagName ).toBe( 'TEXTAREA' );
+ expect( textarea().classList.contains( 'ck-input' ) ).toBe( true );
expect( textarea().spellcheck ).toBe( false );
} );
@@ -54,6 +81,182 @@ describe( 'MathInputView', () => {
expect( mathfield.getAttribute( 'tabindex' ) ).toBe( '0' );
} );
+ it( 'lets a theme space the menu rows, but not the grid cells or dividers', async () => {
+ const mathfield = await renderAndWait();
+ const root = mathfield.shadowRoot as ShadowRoot;
+ const menu = document.createElement( 'menu' );
+ menu.className = 'ui-menu-container';
+ menu.innerHTML = '<li part="menu-item"></li><li part="menu-divider" role="divider"></li>';
+ const grid = document.createElement( 'menu' );
+ grid.className = 'ui-menu-container insert-matrix-submenu';
+ grid.innerHTML = '<li part="menu-item"></li>';
+ root.append( menu, grid );
+ const [ row, divider ] = Array.from( menu.children );
+ const cell = grid.children[ 0 ];
+
+ expect( spacing( row ) ).toEqual( [ '0px 6px', '1px 7px', '4px' ] );
+
+ mathfield.style.setProperty( '--ck-math-menu-item-margin', '4px 10px' );
+ mathfield.style.setProperty( '--ck-math-menu-item-padding', '1px 15px' );
+ mathfield.style.setProperty( '--ck-math-menu-item-border-radius', '6px' );
+
+ expect( spacing( row ) ).toEqual( [ '4px 10px', '1px 15px', '6px' ] );
+ expect( spacing( cell ) ).toEqual( [ '0px', '0px', '0px' ] );
+ expect( spacing( divider ) ).toEqual( [ '0px 15px 5px', '5px 0px 0px', '0px' ] );
+ } );
+
+ it( 'lets a theme draw the color swatches', async () => {
+ const mathfield = await renderAndWait();
+ const swatch = '<li part="menu-item" class="menu-swatch">'
+ + '<span class="label"><span></span></span></li>';
+ const grid = document.createElement( 'menu' );
+ grid.className = 'ui-menu-container swatches-submenu';
+ grid.innerHTML = swatch + swatch;
+ grid.children[ 1 ].classList.add( 'active' );
+ ( mathfield.shadowRoot as ShadowRoot ).append( grid );
+ const swatches = Array.from( grid.children );
+ const colors = swatches.map( item => item.querySelector( 'span > span' ) as Element );
+ const radii = () => colors.map( color => getComputedStyle( color ).borderRadius );
+
+ expect( radii() ).toEqual( [ '50%', '2px' ] );
+ expect( getComputedStyle( colors[ 0 ] ).minWidth ).toBe( '32px' );
+ expect( [ getComputedStyle( colors[ 0 ] ).margin, getComputedStyle( grid ).maxWidth ] )
+ .toEqual( [ '6px', '216px' ] );
+
+ mathfield.style.setProperty( '--ck-math-menu-swatch-size', '24px' );
+ mathfield.style.setProperty( '--ck-math-menu-swatch-gap', '8px' );
+ mathfield.style.setProperty( '--ck-math-menu-swatch-background', 'transparent' );
+ mathfield.style.setProperty( '--ck-math-menu-swatch-border-radius', '4px' );
+ mathfield.style.setProperty( '--ck-math-menu-swatch-shadow', 'inset 0 0 0 1px red' );
+
+ expect( radii() ).toEqual( [ '4px', '4px' ] );
+ expect( swatches.map( item => getComputedStyle( item ).backgroundColor ) )
+ .toEqual( [ 'rgba(0, 0, 0, 0)', 'rgba(0, 0, 0, 0)' ] );
+ expect( getComputedStyle( colors[ 0 ] ).boxShadow )
+ .toBe( 'rgb(255, 0, 0) 0px 0px 0px 1px inset' );
+ const { minWidth, minHeight, margin } = getComputedStyle( colors[ 0 ] );
+ expect( [ minWidth, minHeight, margin ] ).toEqual( [ '24px', '24px', '1px' ] );
+ expect( getComputedStyle( grid ).maxWidth ).toBe( '144px' );
+ } );
+
+ it( 'lets a theme draw the menu headings', async () => {
+ const mathfield = await renderAndWait();
+ const menu = document.createElement( 'menu' );
+ menu.className = 'ui-menu-container';
+ menu.innerHTML = '<li part="menu-item" aria-disabled="true">'
+ + '<span class="label heading">Calculus</span></li>';
+ ( mathfield.shadowRoot as ShadowRoot ).append( menu );
+ const heading = menu.children[ 0 ];
+ const label = heading.children[ 0 ];
+ const look = () => {
+ const row = getComputedStyle( heading );
+ const text = getComputedStyle( label );
+ return [ row.margin, row.padding, row.borderBottom, row.opacity, text.fontSize, text.fontWeight,
+ text.letterSpacing, text.textTransform, text.color, text.opacity ];
+ };
+ const { fontSize, letterSpacing, color } = getComputedStyle( heading );
+
+ expect( look() ).toEqual( [ `${ parseFloat( fontSize ) / 2 }px 6px 0px`, '1px 7px',
+ `0px none ${ color }`, '0.5', fontSize, '700', letterSpacing, 'none', color, '0.4' ] );
+
+ const theme: Record<string, string> = {
+ margin: '0px 0px 8px', padding: '2px 26px 1px', border: '1px solid rgb(0, 128, 0)',
+ opacity: '1', 'font-size': '10px', 'font-weight': '500', 'letter-spacing': '1px',
+ 'text-transform': 'uppercase', color: 'rgb(255, 0, 0)'
+ };
+ for ( const [ name, value ] of Object.entries( theme ) ) {
+ mathfield.style.setProperty( `--ck-math-menu-heading-${ name }`, value );
+ }
+
+ expect( look() ).toEqual( [ '0px 0px 8px', '2px 26px 1px', '1px solid rgb(0, 128, 0)', '1',
+ '10px', '500', '1px', 'uppercase', 'rgb(255, 0, 0)', '1' ] );
+ } );
+
+ it( 'lets a theme place and color the checkmarks of menu rows, but not of swatches', async () => {
+ const mathfield = await renderAndWait();
+ const menu = document.createElement( 'menu' );
+ menu.className = 'ui-menu-container';
+ menu.innerHTML = '<li part="menu-item" aria-checked="true"><span class="ui-checkmark"></span>'
+ + '<span class="label indent">Math</span></li>'
+ + '<li part="menu-item" aria-checked="mixed"><span class="ui-mixedmark"></span>'
+ + '<span class="label indent">Bold</span></li>';
+ const grid = document.createElement( 'menu' );
+ grid.className = 'ui-menu-container swatches-submenu';
+ grid.innerHTML = '<li part="menu-item" class="menu-swatch" aria-checked="true">'
+ + '<span class="ui-checkmark"></span><span class="label"><span></span></span></li>';
+ ( mathfield.shadowRoot as ShadowRoot ).append( menu, grid );
+ const rows = Array.from( menu.children );
+ const marks = rows.map( row => row.children[ 0 ] );
+ const look = ( mark: Element ) => {
+ const style = getComputedStyle( mark );
+ return [ style.order, style.marginInlineStart, style.marginInlineEnd, style.color ];
+ };
+ const indents = () => rows.map( row => getComputedStyle( row.children[ 1 ] ).marginInlineStart );
+ const swatchMark = grid.querySelector( '.ui-checkmark' ) as Element;
+ const { color } = getComputedStyle( rows[ 0 ] );
+
+ const mathLive = [ '0', '-4px', '-11px', color ];
+ expect( marks.map( look ) ).toEqual( [ mathLive, mathLive ] );
+ expect( indents() ).toEqual( [ '12px', '12px' ] );
+
+ mathfield.style.setProperty( '--ck-math-menu-checkmark-order', '1' );
+ mathfield.style.setProperty( '--ck-math-menu-checkmark-margin-inline', '12px 2px' );
+ mathfield.style.setProperty( '--ck-math-menu-checkmark-color', 'rgb(255, 0, 0)' );
+ mathfield.style.setProperty( '--ck-math-menu-checkmark-indent', '0px' );
+
+ const red = [ '1', '12px', '2px', 'rgb(255, 0, 0)' ];
+ expect( marks.map( look ) ).toEqual( [ red, red ] );
+ expect( indents() ).toEqual( [ '0px', '0px' ] );
+ for ( const [ index, mark ] of marks.entries() ) {
+ const labelEnd = rows[ index ].children[ 1 ].getBoundingClientRect().right;
+ expect( mark.getBoundingClientRect().left ).toBeGreaterThan( labelEnd );
+ }
+ expect( look( swatchMark ) ).toEqual( [ '0', '0px', '0px', 'rgb(255, 255, 255)' ] );
+ } );
+
+ it( 'anchors the tooltips of the toggles to them, out of the scrolling field', async () => {
+ onTestFinished( () => document.body.style.removeProperty( 'padding-top' ) );
+ document.body.style.paddingTop = '100px';
+ const mathfield = await renderAndWait();
+ const toggle = mathfield.shadowRoot?.querySelector( '[part=menu-toggle]' );
+ expect( toggle ).toBeTruthy();
+ const tooltip = getComputedStyle( toggle as Element, '::after' );
+
+ expect( [ tooltip.position, tooltip.positionArea ] ).toEqual( [ 'fixed', 'top' ] );
+ expect( tooltip.positionAnchor ).toBe( getComputedStyle( toggle as Element ).anchorName );
+ expect( tooltip.positionAnchor ).toMatch( /^--/ );
+
+ document.body.style.paddingTop = '0px';
+ await new Promise( resolve => requestAnimationFrame( resolve ) );
+ expect( tooltip.positionArea ).toBe( 'bottom' );
+ } );
+
+ it( 'resizes the math field within the limits of the LaTeX textarea', async () => {
+ const mathfield = await renderAndWait();
+ const form = document.createElement( 'div' );
+ form.className = 'ck ck-math-form';
+ document.body.append( form );
+ form.append( view.element as HTMLElement );
+ const container = view.element?.querySelector( '.ck-mathlive-container' ) as HTMLElement;
+ form.style.setProperty( '--ck-input-width', '18em' );
+ const limits = ( element: Element ) => {
+ const { resize, maxWidth, minHeight, maxHeight } = getComputedStyle( element );
+ return [ resize, maxWidth, minHeight, maxHeight ];
+ };
+
+ expect( limits( container ) ).toEqual( limits( textarea() ) );
+ expect( limits( container ) ).toEqual( [ 'both', '100%', '60px', '120px' ] );
+ // CKEditor's `.ck-input` sets the textarea's min-width to `--ck-input-width` at its font size.
+ expect( parseFloat( getComputedStyle( container ).minWidth ) )
+ .toBeCloseTo( 18 * parseFloat( getComputedStyle( textarea() ).fontSize ) );
+
+ container.style.height = '200px';
+ const { paddingTop, paddingBottom } = getComputedStyle( container );
+ expect( mathfield.getBoundingClientRect().height )
+ .toBeCloseTo( container.clientHeight - parseFloat( paddingTop ) - parseFloat( paddingBottom ) );
+ form.remove();
+ } );
+
it( 'localizes MathLive in the editor UI language rather than the browser one', async () => {
const mathFieldClass = () => customElements.get( 'math-field' ) as unknown as { locale: string };
diff --git a/packages/ckeditor5/src/plugins/math/math_input_view.ts b/packages/ckeditor5/src/plugins/math/math_input_view.ts
index 4863d4b794..33ed25af25 100644
--- a/packages/ckeditor5/src/plugins/math/math_input_view.ts
+++ b/packages/ckeditor5/src/plugins/math/math_input_view.ts
@@ -38,7 +38,9 @@ export class LatexTextAreaView extends View implements FocusableView {
constructor( locale: Locale ) {
super( locale );
this.setTemplate( { tag: 'textarea', attributes: {
- class: [ 'ck', 'ck-textarea', 'ck-latex-textarea' ], spellcheck: 'false', tabindex: 0
+ class: [ 'ck', 'ck-input', 'ck-textarea', 'ck-latex-textarea' ],
+ spellcheck: 'false',
+ tabindex: 0
} } );
}
public focus(): void {
@@ -81,7 +83,7 @@ export default class MathInputView extends View {
}
children.push(
{ tag: 'label', attributes: { class: [ 'ck-latex-label' ] }, children: [ locale.t( 'LaTeX' ) ] },
- { tag: 'div', attributes: { class: [ 'ck-latex-wrapper' ] }, children: [ this.latexTextAreaView ] }
+ this.latexTextAreaView
);
this.setTemplate( {
tag: 'div', attributes: { class: [ 'ck', 'ck-math-input' ] },
@@ -209,7 +211,13 @@ export default class MathInputView extends View {
this._setMathfieldValue( this.value ?? '' );
return;
}
+ // Made before MathLive defines it, the element has no shadow root to adopt `SHADOW_STYLES`;
+ // `_loadMathLive()` creates it with the current value once MathLive has loaded.
+ if ( !customElements.get( 'math-field' ) ) {
+ return;
+ }
const mf = document.createElement( 'math-field' ) as MathFieldElement;
+ adoptShadowStyles( mf );
mf.mathVirtualKeyboardPolicy = 'auto';
mf.setAttribute( 'tabindex', '0' );
mf.value = this.value ?? '';
@@ -302,3 +310,92 @@ export default class MathInputView extends View {
function toMathLiveLocale( uiLanguage: string ): string {
return uiLanguage === 'zh' ? 'zh-cn' : uiLanguage;
}
+
+// The scrolling `.ck-mathlive-container` clips the toggles' tooltips, so these rules fix them to
+// their toggles. `::part()` can't tell MathLive's menu rows from its headings and grid cells, or
+// reach a swatch's color or a checkmark, so the menu rules read variables that fall back to
+// MathLive's values.
+const SHADOW_STYLES = `@supports (position-area: top) {
+ [part='virtual-keyboard-toggle'] {
+ anchor-name: --ck-math-keyboard-toggle;
+ }
+ [part='menu-toggle'] {
+ anchor-name: --ck-math-menu-toggle;
+ }
+ [part='virtual-keyboard-toggle']::after {
+ position-anchor: --ck-math-keyboard-toggle;
+ }
+ [part='menu-toggle']::after {
+ position-anchor: --ck-math-menu-toggle;
+ }
+ :is([part='virtual-keyboard-toggle'], [part='menu-toggle'])::after {
+ position: fixed;
+ inset: auto;
+ margin-bottom: 4px;
+ position-area: top;
+ position-try-fallbacks: flip-block;
+ }
+}
+:where(.ui-menu-container > li:not([role='divider'])) {
+ margin: var(--ck-math-menu-item-margin, 0 6px);
+ padding: var(--ck-math-menu-item-padding, 1px 7px);
+ border-radius: var(--ck-math-menu-item-border-radius, 4px);
+}
+:where(.ui-menu-container > li:has(> .heading)) {
+ margin: var(--ck-math-menu-heading-margin, 0.5em 6px 0);
+ padding: var(--ck-math-menu-heading-padding, 1px 7px);
+ border-bottom: var(--ck-math-menu-heading-border, none);
+ opacity: var(--ck-math-menu-heading-opacity, 0.5);
+}
+:where(.ui-menu-container > li > .heading) {
+ font-size: var(--ck-math-menu-heading-font-size, inherit);
+ font-weight: var(--ck-math-menu-heading-font-weight, bold);
+ letter-spacing: var(--ck-math-menu-heading-letter-spacing, inherit);
+ text-transform: var(--ck-math-menu-heading-text-transform, none);
+ color: var(--ck-math-menu-heading-color, inherit);
+ opacity: var(--ck-math-menu-heading-opacity, 0.4);
+}
+.swatches-submenu {
+ --_swatch-size: var(--ck-math-menu-swatch-size, 2rem);
+ max-width: calc(
+ var(--_columns) * (var(--_swatch-size) + var(--ck-math-menu-swatch-gap, 18px)) + 16px
+ );
+}
+.menu-swatch {
+ background: var(--ck-math-menu-swatch-background, var(--neutral-200));
+}
+.menu-swatch.active {
+ background: var(--ck-math-menu-swatch-background, var(--neutral-100));
+}
+.menu-swatch > .label > span {
+ /* Besides two of these margins, a gap holds two 2px swatch margins and two 1px label borders. */
+ margin: calc((var(--ck-math-menu-swatch-gap, 18px) - 6px) / 2);
+ box-shadow: var(--ck-math-menu-swatch-shadow, none);
+ border-radius: var(--ck-math-menu-swatch-border-radius, 50%);
+}
+.menu-swatch.active > .label > span {
+ border-radius: var(--ck-math-menu-swatch-border-radius, 2px);
+}
+.ui-menu-container > li:not(.menu-swatch) > :is(.ui-checkmark, .ui-mixedmark) {
+ order: var(--ck-math-menu-checkmark-order, 0);
+ margin-inline: var(--ck-math-menu-checkmark-margin-inline, -4px -11px);
+ color: var(--ck-math-menu-checkmark-color, inherit);
+}
+:where(.ui-menu-container > li > .label.indent) {
+ margin-inline-start: var(--ck-math-menu-checkmark-indent, 12px);
+}`;
+
+let shadowStyleSheet: CSSStyleSheet | undefined;
+
+function adoptShadowStyles( mathfield: HTMLElement ): void {
+ const root = mathfield.shadowRoot;
+ /* v8 ignore next 3 -- MathLive attaches an open shadow root in the math-field constructor */
+ if ( !root ) {
+ return;
+ }
+ if ( !shadowStyleSheet ) {
+ shadowStyleSheet = new CSSStyleSheet();
+ shadowStyleSheet.replaceSync( SHADOW_STYLES );
+ }
+ root.adoptedStyleSheets = [ ...root.adoptedStyleSheets, shadowStyleSheet ];
+}
diff --git a/packages/ckeditor5/src/theme/math_form.css b/packages/ckeditor5/src/theme/math_form.css
index ebbe71a96b..3b4674a0cf 100644
--- a/packages/ckeditor5/src/theme/math_form.css
+++ b/packages/ckeditor5/src/theme/math_form.css
@@ -25,13 +25,14 @@
box-sizing: border-box;
min-width: 400px;
max-width: 60vw;
- max-height: 350px;
+ /* Fits both fields at their 120px max-height */
+ max-height: 480px;
overflow: visible;
user-select: text;
}
/* Scrollable content - vertical scroll, horizontal visible for tooltips */
-.ck-math-view {
+.ck.ck-math-form .ck-math-view {
overflow-y: auto;
overflow-x: visible;
display: flex;
@@ -39,7 +40,9 @@
flex: 1 1 auto;
gap: var(--ck-spacing-standard, var(--ck-spacing-base));
min-height: 0;
- width: 100%;
+ /* Room for the fields' 3px focus rings, which the scrolling view clips at its padding edge */
+ margin: -3px;
+ padding: 3px;
}
/* === MATH INPUT === */
@@ -50,24 +53,32 @@
width: fit-content;
min-width: 100%;
max-width: 100%;
- flex: 1 1 auto;
- min-height: 0;
+ flex: 1 0 auto;
overflow: visible !important;
}
/* === MATHLIVE EDITOR === */
.ck.ck-math-input .ck-mathlive-container {
+ display: flex;
+ flex-direction: column;
position: relative;
- width: 100%;
- min-height: 50px;
padding: var(--ck-spacing-small, var(--ck-spacing-sm));
border: 1px solid var(--ck-color-input-border);
border-radius: var(--ck-border-radius);
- background: var(--ck-color-input-background) !important;
+ /* The LaTeX textarea's min-width: `--ck-input-width` (18em) at its 0.95em font size */
+ min-width: calc(0.95 * var(--ck-input-width));
+ background: var(--ck-color-input-background);
+ color: var(--ck-color-input-text);
transition: border-color 120ms ease;
- overflow: auto;
clip-path: none;
}
+.ck.ck-math-input :is(.ck-mathlive-container, .ck-latex-textarea) {
+ max-width: 100%;
+ min-height: 60px;
+ max-height: 120px;
+ resize: both;
+ overflow: auto;
+}
.ck.ck-math-input .ck-mathlive-container:focus-within {
border-color: var(--ck-color-focus-border);
}
@@ -88,10 +99,11 @@
/* Math field element */
.ck.ck-math-form math-field {
display: block !important;
+ flex-grow: 1;
width: 100%;
font-size: 1.5em;
background: transparent !important;
- color: var(--ck-color-input-text);
+ color: inherit;
border: none !important;
padding: 0;
outline: none !important;
@@ -101,22 +113,6 @@
}
/* === LATEX TEXTAREA === */
-.ck.ck-math-input .ck-latex-wrapper {
- display: flex;
- flex-direction: column;
- width: fit-content;
- min-width: 100%;
- max-width: 100%;
- padding: var(--ck-spacing-small, var(--ck-spacing-sm));
- border: 1px solid var(--ck-color-input-border);
- border-radius: var(--ck-border-radius);
- background: var(--ck-color-input-background) !important;
- transition: border-color 120ms ease;
- box-sizing: border-box;
-}
-.ck.ck-math-input .ck-latex-wrapper:focus-within {
- border-color: var(--ck-color-focus-border);
-}
.ck.ck-math-input .ck-latex-label {
font-size: 12px;
font-weight: 600;
@@ -126,20 +122,8 @@
flex-shrink: 0;
}
.ck.ck-math-input .ck-latex-textarea {
- width: fit-content;
- min-width: 100%;
- max-width: 100%;
- min-height: 60px;
- max-height: calc(80vh - 300px);
- resize: both;
- overflow: auto;
font-family: 'Courier New', monospace;
font-size: 0.95em;
- background: transparent !important;
- color: var(--ck-color-input-text);
- border: none !important;
- padding: 0;
- outline: none !important;
box-sizing: border-box;
}
@@ -189,22 +173,4 @@
justify-content: flex-end;
margin-top: var(--ck-spacing-standard, var(--ck-spacing-base));
}
-.ck-button-save, .ck-button-cancel {
- padding-block: var(--ck-spacing-small, var(--ck-spacing-sm));
- padding-inline: var(--ck-spacing-standard, var(--ck-spacing-base));
- border: 1px solid var(--ck-color-input-border);
- border-radius: var(--ck-border-radius);
- cursor: pointer;
- font-weight: 500;
-}
-.ck-button-save {
- background: var(--ck-color-focus-border);
- color: white;
-}
-.ck-button-cancel {
- background: var(--ck-color-input-background);
- color: var(--ck-color-text);
-}
-.ck-button-save:hover { opacity: 0.9; }
-.ck-button-cancel:hover { background: var(--ck-color-base-background); }