Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions packages/multi-trait-rubric/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,44 @@ export default class MultiTraitRubric extends HTMLElement {
this._model = {};
this._session = null;
this._root = null;
this._mathObserver = null;
this._mathRenderPending = false;
}

// React commits asynchronously, so a queueMicrotask(renderMath) can run before
// the LaTeX spans are in the DOM and leave raw LaTeX on first render — this is
// especially visible when this element is mounted inside complex-rubric, which
// swaps its innerHTML and re-defines the inner element. Observing the DOM and
// typesetting after each commit keeps math in sync regardless of timing.
_scheduleMathRender = () => {
if (this._mathRenderPending) return;
this._mathRenderPending = true;

requestAnimationFrame(() => {
if (this._mathObserver) {
this._mathObserver.disconnect();
}
renderMath(this);
this._mathRenderPending = false;
setTimeout(() => {
if (this._mathObserver) {
this._mathObserver.observe(this, { childList: true, subtree: true });
}
}, 50);
});
};

_initMathObserver() {
if (this._mathObserver) return;
this._mathObserver = new MutationObserver(this._scheduleMathRender);
this._mathObserver.observe(this, { childList: true, subtree: true });
}

_disconnectMathObserver() {
if (this._mathObserver) {
this._mathObserver.disconnect();
this._mathObserver = null;
}
}

set model(s) {
Expand All @@ -28,22 +66,23 @@ export default class MultiTraitRubric extends HTMLElement {
}

connectedCallback() {
this._initMathObserver();
this._render();
}

_render() {
this._initMathObserver();

const el = React.createElement(Main, { model: this._model, session: this._session });

if (!this._root) {
this._root = createRoot(this);
}
this._root.render(el);
queueMicrotask(() => {
renderMath(this);
});
}

disconnectedCallback() {
this._disconnectMathObserver();
if (this._root) {
this._root.unmount();
}
Expand Down
45 changes: 42 additions & 3 deletions packages/rubric/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,44 @@ export default class RubricRender extends HTMLElement {
debug.log('constructor called');
this.onModelChanged = this.onModelChanged.bind(this);
this._root = null;
this._mathObserver = null;
this._mathRenderPending = false;
}

// React commits asynchronously, so a queueMicrotask(renderMath) can run before
// the LaTeX spans are in the DOM and leave raw LaTeX on first render — this is
// especially visible when rubric is mounted inside complex-rubric, which swaps
// its innerHTML and re-defines the inner element. Observing the DOM and
// typesetting after each commit keeps math in sync regardless of timing.
_scheduleMathRender = () => {
if (this._mathRenderPending) return;
this._mathRenderPending = true;

requestAnimationFrame(() => {
if (this._mathObserver) {
this._mathObserver.disconnect();
}
renderMath(this);
this._mathRenderPending = false;
setTimeout(() => {
if (this._mathObserver) {
this._mathObserver.observe(this, { childList: true, subtree: true });
}
}, 50);
});
};

_initMathObserver() {
if (this._mathObserver) return;
this._mathObserver = new MutationObserver(this._scheduleMathRender);
this._mathObserver.observe(this, { childList: true, subtree: true });
}

_disconnectMathObserver() {
if (this._mathObserver) {
this._mathObserver.disconnect();
this._mathObserver = null;
}
}

set model(s) {
Expand All @@ -23,24 +61,25 @@ export default class RubricRender extends HTMLElement {
}

connectedCallback() {
this._initMathObserver();
this._render();
}

_render() {
if (this._model) {
this._initMathObserver();

const el = React.createElement(Rubric, { value: this._model });

if (!this._root) {
this._root = createRoot(this);
}
this._root.render(el);
queueMicrotask(() => {
renderMath(this);
});
}
}

disconnectedCallback() {
this._disconnectMathObserver();
if (this._root) {
this._root.unmount();
}
Expand Down
Loading