Editor placeholder

This commit is contained in:
Gregory Schier
2023-03-01 14:22:10 -08:00
parent 61fe95b300
commit 70f534f1d8
2 changed files with 16 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
import type { Transaction, TransactionSpec } from '@codemirror/state'; import type { Transaction, TransactionSpec } from '@codemirror/state';
import { Compartment, EditorSelection, EditorState, Prec } from '@codemirror/state'; import { Compartment, EditorSelection, EditorState, Prec } from '@codemirror/state';
import { placeholder as placeholderExt } from '@codemirror/view';
import classnames from 'classnames'; import classnames from 'classnames';
import { EditorView } from 'codemirror'; import { EditorView } from 'codemirror';
import type { HTMLAttributes } from 'react'; import type { HTMLAttributes } from 'react';
@@ -10,6 +11,7 @@ import { baseExtensions, getLanguageExtension, multiLineExtensions } from './ext
interface Props extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'> { interface Props extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'> {
contentType: string; contentType: string;
valueKey?: string; valueKey?: string;
placeholder?: string;
useTemplating?: boolean; useTemplating?: boolean;
onChange?: (value: string) => void; onChange?: (value: string) => void;
onSubmit?: () => void; onSubmit?: () => void;
@@ -18,6 +20,7 @@ interface Props extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'> {
export default function Editor({ export default function Editor({
contentType, contentType,
placeholder,
valueKey, valueKey,
useTemplating, useTemplating,
defaultValue, defaultValue,
@@ -30,7 +33,8 @@ export default function Editor({
const [cm, setCm] = useState<{ view: EditorView; langHolder: Compartment } | null>(null); const [cm, setCm] = useState<{ view: EditorView; langHolder: Compartment } | null>(null);
const ref = useRef<HTMLDivElement>(null); const ref = useRef<HTMLDivElement>(null);
const extensions = useMemo( const extensions = useMemo(
() => getExtensions({ onSubmit, singleLine, onChange, contentType, useTemplating }), () =>
getExtensions({ placeholder, onSubmit, singleLine, onChange, contentType, useTemplating }),
[contentType], [contentType],
); );
@@ -91,11 +95,15 @@ export default function Editor({
function getExtensions({ function getExtensions({
singleLine, singleLine,
placeholder,
onChange, onChange,
onSubmit, onSubmit,
contentType, contentType,
useTemplating, useTemplating,
}: Pick<Props, 'singleLine' | 'onChange' | 'onSubmit' | 'contentType' | 'useTemplating'>) { }: Pick<
Props,
'singleLine' | 'onChange' | 'onSubmit' | 'contentType' | 'useTemplating' | 'placeholder'
>) {
const ext = getLanguageExtension({ contentType, useTemplating }); const ext = getLanguageExtension({ contentType, useTemplating });
return [ return [
...(singleLine ...(singleLine
@@ -134,6 +142,7 @@ function getExtensions({
...baseExtensions, ...baseExtensions,
...(!singleLine ? [multiLineExtensions] : []), ...(!singleLine ? [multiLineExtensions] : []),
...(ext ? [ext] : []), ...(ext ? [ext] : []),
...(placeholder ? [placeholderExt(placeholder)] : []),
EditorView.updateListener.of((update) => { EditorView.updateListener.of((update) => {
if (typeof onChange === 'function' && update.docChanged) { if (typeof onChange === 'function' && update.docChanged) {
onChange(update.state.doc.toString()); onChange(update.state.doc.toString());

View File

@@ -1,5 +1,6 @@
import type { InputHTMLAttributes, ReactNode } from 'react'; import type { InputHTMLAttributes, ReactNode } from 'react';
import classnames from 'classnames'; import classnames from 'classnames';
import { placeholders } from './Editor/widgets';
import { HStack, VStack } from './Stacks'; import { HStack, VStack } from './Stacks';
import Editor from './Editor/Editor'; import Editor from './Editor/Editor';
@@ -25,6 +26,7 @@ export function Input({
containerClassName, containerClassName,
labelClassName, labelClassName,
onSubmit, onSubmit,
placeholder,
useTemplating, useTemplating,
size = 'md', size = 'md',
useEditor, useEditor,
@@ -68,6 +70,7 @@ export function Input({
defaultValue={defaultValue} defaultValue={defaultValue}
onChange={onChange} onChange={onChange}
onSubmit={onSubmit} onSubmit={onSubmit}
placeholder={placeholder}
className={classnames( className={classnames(
className, className,
'bg-transparent min-w-0 pl-3 pr-2 h-full w-full focus:outline-none', 'bg-transparent min-w-0 pl-3 pr-2 h-full w-full focus:outline-none',
@@ -79,6 +82,8 @@ export function Input({
<input <input
id={id} id={id}
onChange={(e) => onChange?.(e.target.value)} onChange={(e) => onChange?.(e.target.value)}
placeholder={placeholder}
defaultValue={defaultValue}
className={classnames( className={classnames(
className, className,
'bg-transparent min-w-0 pl-3 pr-2 h-full w-full focus:outline-none', 'bg-transparent min-w-0 pl-3 pr-2 h-full w-full focus:outline-none',