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

Small improvements to non-CodeMirror editor #1566

Merged
merged 1 commit into from
Nov 16, 2024
Merged
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
10 changes: 8 additions & 2 deletions src/components/container/dialog-card/dialog-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as React from 'react';
import './dialog-editor.css';

export const DialogEditor: React.FC = props => (
<div className="dialog-editor">{props.children}</div>
export type DialogEditorProps = React.ComponentPropsWithoutRef<'div'>;

export const DialogEditor = React.forwardRef<HTMLDivElement, DialogEditorProps>(
(props, ref) => (
<div className="dialog-editor" ref={ref}>
{props.children}
</div>
)
);
11 changes: 11 additions & 0 deletions src/dialogs/passage-edit/passage-edit-contents.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,15 @@

.passage-edit-contents .tags {
padding: var(--grid-size);
}

/* Needed when CodeMirror is disabled. */

.passage-edit-contents .dialog-editor textarea {
border-radius: 0;
}

.passage-edit-contents .dialog-editor textarea:focus {
box-shadow: inset 0 0 0px 2px var(--blue);
outline: none;
}
18 changes: 17 additions & 1 deletion src/dialogs/passage-edit/passage-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const PassageText: React.FC<PassageTextProps> = props => {
const autocompletePassageNames = useCodeMirrorPassageHints(story);
const mode =
useFormatCodeMirrorMode(storyFormat.name, storyFormat.version) ?? 'text';
const codeAreaContainerRef = React.useRef<HTMLDivElement>(null);
const {t} = useTranslation();

// These are refs so that changing them doesn't trigger a rerender, and more
Expand Down Expand Up @@ -123,6 +124,21 @@ export const PassageText: React.FC<PassageTextProps> = props => {
[onEditorChange]
);

// Emulate the above behavior re: focus if we aren't using CodeMirror.

React.useEffect(() => {
if (!prefs.useCodeMirror && codeAreaContainerRef.current) {
const area = codeAreaContainerRef.current.querySelector('textarea');

if (!area) {
return;
}

area.focus();
area.setSelectionRange(area.value.length, area.value.length);
}
}, []);

const options = React.useMemo(
() => ({
...codeMirrorOptionsFromPrefs(prefs),
Expand All @@ -147,7 +163,7 @@ export const PassageText: React.FC<PassageTextProps> = props => {
);

return (
<DialogEditor>
<DialogEditor ref={codeAreaContainerRef}>
<CodeArea
editorDidMount={handleMount}
fontFamily={prefs.passageEditorFontFamily}
Expand Down
Loading