Skip to content

Commit

Permalink
fix: correctly propagate style
Browse files Browse the repository at this point in the history
  • Loading branch information
arnog committed Oct 12, 2024
1 parent 4d00696 commit 3daf194
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
22 changes: 19 additions & 3 deletions src/core/atom-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,25 @@ export class Atom<T extends (Argument | null)[] = (Argument | null)[]> {
applyStyle(style: Style, options?: { unstyledOnly: boolean }): void {
this.isDirty = true;

if (options?.unstyledOnly && Object.keys(this.style).length === 0) return;

this.style = { ...this.style, ...style };
if (options?.unstyledOnly) {
if (style.color && !this.style.color) this.style.color = style.color;
if (style.backgroundColor && !this.style.backgroundColor)
this.style.backgroundColor = style.backgroundColor;
if (style.fontFamily && !this.style.fontFamily)
this.style.fontFamily = style.fontFamily;
if (style.fontShape && !this.style.fontShape)
this.style.fontShape = style.fontShape;
if (style.fontSeries && !this.style.fontSeries)
this.style.fontSeries = style.fontSeries;
if (style.fontSize && !this.style.fontSize)
this.style.fontSize = style.fontSize;
if (style.variant && !this.style.variant)
this.style.variant = style.variant;
if (style.variantStyle && !this.style.variantStyle)
this.style.variantStyle = style.variantStyle;
} else {
this.style = { ...this.style, ...style };
}

if (this.style.fontFamily === 'none') delete this.style.fontFamily;

Expand Down
6 changes: 6 additions & 0 deletions src/editor-mathfield/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from './mode-editor-latex';
import { ModeEditor } from './mode-editor';
import { ParseMode } from '../public/core-types';
import { getSelectionStyle } from './keyboard-input';

export function removeSuggestion(mathfield: _Mathfield): void {
const group = getLatexGroupBody(mathfield.model).filter(
Expand Down Expand Up @@ -158,11 +159,16 @@ export function complete(
mathfield.switchMode(options?.mode ?? 'math');

if (completion === 'reject') return true;
const style = {
...getSelectionStyle(mathfield.model),
...mathfield.defaultStyle,
};

ModeEditor.insert(mathfield.model, latex, {
selectionMode: options?.selectItem ?? false ? 'item' : 'placeholder',
format: 'latex',
mode: 'math',
style,
});

mathfield.snapshot();
Expand Down
2 changes: 1 addition & 1 deletion src/editor-mathfield/keyboard-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ function insertMathModeChar(mathfield: _Mathfield, c: string): void {
mathfield.snapshot(`insert-${model.at(model.position).type}`);
}

function getSelectionStyle(model: _Model): Readonly<Style> {
export function getSelectionStyle(model: _Model): Readonly<Style> {
// When the selection is collapsed, we inherit the style from the
// preceding atom
if (model.selectionIsCollapsed) return model.at(model.position)?.style ?? {};
Expand Down
1 change: 0 additions & 1 deletion src/editor-mathfield/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ export function render(
mathfield: _Mathfield,
renderOptions?: { forHighlighting?: boolean; interactive?: boolean }
): void {
console.log('rendering');
if (!isValidMathfield(mathfield)) return;
renderOptions ??= {};

Expand Down
13 changes: 1 addition & 12 deletions src/editor-model/styling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,7 @@ export function applyStyleToUnstyledAtoms(
// Apply styling options to each atom
atom.forEach((x) => applyStyleToUnstyledAtoms(x, style));
} else if (typeof atom === 'object') {
if (
!atom.style.color &&
!atom.style.backgroundColor &&
!atom.style.fontFamily &&
!atom.style.fontShape &&
!atom.style.fontSeries &&
!atom.style.fontSize &&
!atom.style.variant &&
!atom.style.variantStyle
) {
atom.applyStyle(style, { unstyledOnly: true });
}
atom.applyStyle(style, { unstyledOnly: true });
}
}

Expand Down

0 comments on commit 3daf194

Please sign in to comment.