Skip to content

Commit

Permalink
Merge pull request #1566 from klembot/native-code-area-improvements
Browse files Browse the repository at this point in the history
Small improvements to non-CodeMirror editor
  • Loading branch information
klembot authored Nov 16, 2024
2 parents 9622dfc + 111bd18 commit 0c1208d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
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

0 comments on commit 0c1208d

Please sign in to comment.