aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/EquationBox.tsx
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2025-06-26 10:53:54 -0400
committerbobzel <zzzman@gmail.com>2025-06-26 10:53:54 -0400
commitbaae27b205356898c5866a0f095e4ec056e02459 (patch)
tree1b62de5579b8de8be81b6d342a9767f0f379bb91 /src/client/views/nodes/EquationBox.tsx
parentccfdf905400cd4b81d8cde0f16bb0e15cd65621b (diff)
parent0093370a04348ef38b91252d02ab850f25d753b2 (diff)
Merge branch 'master' into agent-paper-main
Diffstat (limited to 'src/client/views/nodes/EquationBox.tsx')
-rw-r--r--src/client/views/nodes/EquationBox.tsx44
1 files changed, 31 insertions, 13 deletions
diff --git a/src/client/views/nodes/EquationBox.tsx b/src/client/views/nodes/EquationBox.tsx
index 3cacb6692..2ce24b688 100644
--- a/src/client/views/nodes/EquationBox.tsx
+++ b/src/client/views/nodes/EquationBox.tsx
@@ -6,7 +6,7 @@ import { TraceMobx } from '../../../fields/util';
import { DocUtils } from '../../documents/DocUtils';
import { DocumentType } from '../../documents/DocumentTypes';
import { Docs } from '../../documents/Documents';
-import { undoBatch } from '../../util/UndoManager';
+import { undoBatch, UndoManager } from '../../util/UndoManager';
import { ViewBoxBaseComponent } from '../DocComponent';
import { StyleProp } from '../StyleProp';
import { DocumentView } from './DocumentView';
@@ -14,6 +14,7 @@ import './EquationBox.scss';
import { FieldView, FieldViewProps } from './FieldView';
import EquationEditor from './formattedText/EquationEditor';
import { FormattedTextBox } from './formattedText/FormattedTextBox';
+import { Doc } from '../../../fields/Doc';
@observer
export class EquationBox extends ViewBoxBaseComponent<FieldViewProps>() {
@@ -21,6 +22,7 @@ export class EquationBox extends ViewBoxBaseComponent<FieldViewProps>() {
return FieldView.LayoutString(EquationBox, fieldKey);
}
_ref: React.RefObject<EquationEditor> = React.createRef();
+ _liveTextUndo: UndoManager.Batch | undefined; // captured undo batch when typing a new text note into a collection
constructor(props: FieldViewProps) {
super(props);
@@ -29,12 +31,17 @@ export class EquationBox extends ViewBoxBaseComponent<FieldViewProps>() {
componentDidMount() {
this._props.setContentViewBox?.(this);
- if (DocumentView.SelectOnLoad === this.Document && (!DocumentView.LightboxDoc() || DocumentView.LightboxContains(this.DocumentView?.()))) {
+ if (DocumentView.SelectOnLoad === this.rootDoc && (!DocumentView.LightboxDoc() || DocumentView.LightboxContains(this.DocumentView?.()))) {
+ this._liveTextUndo = FormattedTextBox.LiveTextUndo;
+ FormattedTextBox.LiveTextUndo = undefined;
this._props.select(false);
+ this.dataDoc[Doc.LayoutDataKey(this.Document)] = FormattedTextBox.SelectOnLoadChar ?? '';
+
this._ref.current?.mathField.focus();
- this.dataDoc.text === 'x' && this._ref.current?.mathField.select();
+ this.dataDoc[Doc.LayoutDataKey(this.Document)] === 'x' && this._ref.current?.mathField.select();
DocumentView.SetSelectOnLoad(undefined);
+ FormattedTextBox.SelectOnLoadChar = '';
}
reaction(
() => this._props.isSelected(),
@@ -53,7 +60,7 @@ export class EquationBox extends ViewBoxBaseComponent<FieldViewProps>() {
@action
keyPressed = (e: KeyboardEvent) => {
if (e.key === 'Enter') {
- const nextEq = Docs.Create.EquationDocument(e.shiftKey ? StrCast(this.dataDoc.text) : '', {
+ const nextEq = Docs.Create.EquationDocument(e.shiftKey ? StrCast(this.dataDoc[Doc.LayoutDataKey(this.Document)]) : '', {
title: '# math',
_width: NumCast(this.layoutDoc._width),
_height: NumCast(this.layoutDoc._height),
@@ -70,9 +77,10 @@ export class EquationBox extends ViewBoxBaseComponent<FieldViewProps>() {
e.stopPropagation();
}
if (e.key === 'Tab') {
+ const target = this.Document.isTemplateDoc ? this.rootDoc : this.Document;
const graph = Docs.Create.FunctionPlotDocument([this.Document], {
- x: NumCast(this.layoutDoc.x) + NumCast(this.layoutDoc._width),
- y: NumCast(this.layoutDoc.y),
+ x: NumCast(target.x) + NumCast(this.layoutDoc._width),
+ y: NumCast(target.y),
_width: 400,
_height: 300,
backgroundColor: 'white',
@@ -82,11 +90,11 @@ export class EquationBox extends ViewBoxBaseComponent<FieldViewProps>() {
link && this._props.addDocument?.(link);
e.stopPropagation();
}
- if (e.key === 'Backspace' && !this.dataDoc.text) this._props.removeDocument?.(this.Document);
+ if (e.key === 'Backspace' && !this.dataDoc[Doc.LayoutDataKey(this.Document)]) this._props.removeDocument?.(this.Document);
};
@undoBatch
onChange = (str: string) => {
- this.dataDoc.text = str;
+ this.dataDoc[Doc.LayoutDataKey(this.Document)] = str;
};
updateSize = (mathSpan: HTMLSpanElement) => {
@@ -103,19 +111,22 @@ export class EquationBox extends ViewBoxBaseComponent<FieldViewProps>() {
this.layoutDoc._width = mathWidth * nScale;
this.layoutDoc._height = mathHeight * nScale;
+ if (this.layoutDoc._nativeWidth) {
+ this.layoutDoc._nativeWidth = mathWidth;
+ this.layoutDoc._nativeHeight = mathHeight;
+ }
};
+ setRef = (r: HTMLDivElement) => r && this._ref.current?.element.current && this.updateSize(this._ref.current?.element.current);
render() {
TraceMobx();
const scale = this._props.NativeDimScaling?.() || 1;
return (
<div
- ref={r => r && this._ref.current?.element.current && this.updateSize(this._ref.current?.element.current)}
+ ref={this.setRef}
className="equationBox-cont"
onKeyDown={e => e.stopPropagation()}
onPointerDown={e => !e.ctrlKey && e.stopPropagation()}
- onBlur={() => {
- FormattedTextBox.LiveTextUndo?.end();
- }}
+ onBlur={() => this._liveTextUndo?.end()}
style={{
transform: `scale(${scale})`,
minWidth: `${100 / scale}%`,
@@ -128,7 +139,14 @@ export class EquationBox extends ViewBoxBaseComponent<FieldViewProps>() {
paddingTop: NumCast(this.layoutDoc.yMargin),
paddingBottom: NumCast(this.layoutDoc.yMargin),
}}>
- <EquationEditor ref={this._ref} value={StrCast(this.dataDoc.text, '')} spaceBehavesLikeTab onChange={this.onChange} autoCommands="pi theta sqrt sum prod alpha beta gamma rho" autoOperatorNames="sin cos tan" />
+ <EquationEditor
+ ref={this._ref}
+ value={StrCast(this.dataDoc[Doc.LayoutDataKey(this.Document)], '')}
+ spaceBehavesLikeTab
+ onChange={this.onChange}
+ autoCommands="pi theta sqrt sum prod alpha beta gamma rho"
+ autoOperatorNames="sin cos tan"
+ />
</div>
);
}