Skip to content
Open
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
48 changes: 48 additions & 0 deletions js/panes/PropertyItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@

import React, { useEffect, useRef, useState } from 'react';

import { typesetMathJax } from './utils/mathjaxHelpers';

const LATEX_PATTERN = /\$[^$]+\$|\\\([^)]+\\\)/;

function EditablePropertyText(props) {
const { value, validateHandler, submitHandler, blurStopPropagation } = props;

// state varibles
// --------------
const textInput = useRef();
const previewRef = useRef();
const [actualValue, setActualValue] = useState(value);
const [isEdited, setIsEdited] = useState(false);

const hasLatex =
typeof actualValue === 'string' && LATEX_PATTERN.test(actualValue);

// private events
// --------------

Expand All @@ -42,6 +50,17 @@ function EditablePropertyText(props) {
if (blurStopPropagation) event.stopPropagation();
};

const handlePreviewClick = () => {
setIsEdited(true);
};

const handlePreviewKeyDown = (event) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
setIsEdited(true);
}
};

// Enter invokes blur and thus submits the change
const handleKeyPress = (event) => {
if (event.key === 'Enter') textInput.current.blur();
Expand All @@ -55,9 +74,38 @@ function EditablePropertyText(props) {
if (!isEdited) setActualValue(value);
}, [value]);

useEffect(() => {
if (isEdited) textInput.current?.focus();
}, [isEdited]);

useEffect(() => {
if (isEdited || !hasLatex) return;
let cancelled = false;
typesetMathJax(previewRef.current, () => cancelled);
return () => {
cancelled = true;
};
}, [isEdited, hasLatex, actualValue]);

// rendering
// ---------

if (!isEdited && hasLatex) {
return (
<span
ref={previewRef}
role="textbox"
tabIndex={0}
style={{ cursor: 'text' }}
onClick={handlePreviewClick}
onFocus={handlePreviewClick}
onKeyDown={handlePreviewKeyDown}
>
{actualValue}
</span>
);
}

return (
<input
type="text"
Expand Down
Loading