Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hint text in stan editor #150

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 7 additions & 14 deletions gui/src/app/FileEditor/StanFileEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,16 @@ const StanFileEditor: FunctionComponent<Props> = ({

const isCompiling = compileStatus === "compiling";

const messagePaneNeeded = syntaxWindowVisible || !editedFileContent;

const window = messagePaneNeeded ? (
editedFileContent ? (
<StanCompileResultWindow
stancErrors={stancErrors}
onClose={() => setSyntaxWindowVisible(false)}
/>
) : (
<div className="StanEditorDefaultText">
Begin editing or select an example from the left panel
</div>
)
const window = syntaxWindowVisible ? (
<StanCompileResultWindow
stancErrors={stancErrors}
onClose={() => setSyntaxWindowVisible(false)}
/>
) : (
<></>
);

const initialSizes = messagePaneNeeded ? [60, 40] : [100, 0];
const initialSizes = syntaxWindowVisible ? [60, 40] : [100, 0];

return (
<Splitter direction={SplitDirection.Vertical} initialSizes={initialSizes}>
Expand All @@ -242,6 +234,7 @@ const StanFileEditor: FunctionComponent<Props> = ({
readOnly={!isCompiling ? readOnly : true}
toolbarItems={toolbarItems}
codeMarkers={stancErrorsToCodeMarkers(stancErrors)}
hintTextOnEmpty="Begin editing or select an example from the left panel"
/>
{window}
</Splitter>
Expand Down
35 changes: 35 additions & 0 deletions gui/src/app/FileEditor/TextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Props = {
toolbarItems?: ToolbarItem[];
label: string;
codeMarkers?: CodeMarker[];
hintTextOnEmpty?: string;
};

export type ToolbarItem =
Expand Down Expand Up @@ -54,6 +55,7 @@ const TextEditor: FunctionComponent<Props> = ({
language,
label,
codeMarkers,
hintTextOnEmpty,
}) => {
const handleChange = useCallback(
(value: string | undefined) => {
Expand Down Expand Up @@ -100,6 +102,19 @@ const TextEditor: FunctionComponent<Props> = ({
);
}, [codeMarkers, monacoInstance, editorInstance]);

useEffect(() => {
if (!editorInstance) return;
if (!hintTextOnEmpty) return;
if (text || editedText) {
return;
}
const contentWidget = createHintTextContentWidget(hintTextOnEmpty);
editorInstance.addContentWidget(contentWidget);
return () => {
editorInstance.removeContentWidget(contentWidget);
};
}, [text, editorInstance, editedText, hintTextOnEmpty]);

/////////////////////////////////////////////////

// Can't do this in the usual way with monaco editor:
Expand Down Expand Up @@ -227,4 +242,24 @@ const NotSelectable: FunctionComponent<PropsWithChildren> = ({ children }) => {
return <div className="NotSelectable">{children}</div>;
};

const createHintTextContentWidget = (hintText: string) => {
return {
getDomNode: () => {
const node = document.createElement("div");
node.style.width = "max-content";
node.style.pointerEvents = "none";
node.textContent = hintText;
node.style.fontStyle = "italic";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on pulling these .styles into a class? In particular, it would also be nice to make the text slightly lower contrast than it is currently, as vscode does

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created a css class EditorHintText for the italics and color. I feel like pointerEvents still belongs inline though.

return node;
},
getId: () => "hintText",
getPosition: () => {
return {
position: { lineNumber: 1, column: 1 },
preference: [editor.ContentWidgetPositionPreference.EXACT],
};
},
};
};

export default TextEditor;