1.6.0 • Published 8 months ago

@mittirorg/react-richtext v1.6.0

Weekly downloads
-
License
-
Repository
github
Last release
8 months ago

@mittirorg/react-richtext

A rich text editor component for React.

Installation

You can install the package via npm:

npm install @mittirorg/react-richtext

or via yarn:

yarn add @mittirorg/react-richtext

Usage

Here’s a basic example of how to use the RichTextEditor component:

import React, { useState, useRef } from 'react';
import RichTextEditor, {
  IEditorProps,
  IFetchSuggestions,
  IRenderSuggestions,
  IRenderHint,
  IHandleLinks,
  IHandleEntitiesCb,
  IEntityInfo,
} from '@mittirorg/react-richtext';

const InputBox = () => {
  // Initialize editor state
  const [editorState, setEditorState] = useState(null);
  const [isTriggerInserted, setTriggerInserted] = useState(false);
  const editorContainerRef = useRef(null);

  const fetchSuggestions: IFetchSuggestions = async (searchText, triggerKey) => {
    // Fetch suggestions based on searchText and triggerKey
    // Replace with actual suggestion fetching logic
    return {
      suggestions: [{ viewText: 'Suggestion 1' }, { viewText: 'Suggestion 2' }],
      showHint: true,
    };
  };

  const renderSuggestions: IRenderSuggestions = (
    position,
    suggestions,
    currentTriggerKey,
    onClose,
    handleAddEntity,
    hideSuggestions,
  ) => {
    // Render suggestions based on position, suggestions, currentTriggerKey, etc.
    // Replace with actual rendering logic
    return (
      <div
        style={{
          position: 'absolute',
          top: position.bottom,
          left: position.left,
        }}
      >
        {suggestions.map((suggestion, index) => (
          <div key={index} onClick={() => handleAddEntity(suggestion)}>
            {suggestion.viewText}
          </div>
        ))}
      </div>
    );
  };

  const renderHint: IRenderHint = (position, currentTriggerKey, onClose, hideSuggestions) => {
    // Render hint based on position, currentTriggerKey, etc.
    // Replace with actual rendering logic
    return (
      <div style={{ position: 'absolute', top: position.bottom, left: position.left }}>
        <div>Hint for {currentTriggerKey}</div>
      </div>
    );
  };

  const handleLinks: IHandleLinks = (type, data) => {
    // Handle links based on type and data
    // Replace with actual link handling logic
    console.log('Link handling:', type, data);
  };

  const handleEntitiesCb: IHandleEntitiesCb = (entityData, triggerKey) => {
    // Handle entities based on entityData and triggerKey
    // Replace with actual entity handling logic
    console.log('Entity handling:', entityData, triggerKey);
  };

  const editorProps: IEditorProps = {
    isTriggerInserted,
    possibleTriggerKeys: ['@', '#'],
    placeholder: 'Write something...',
    editorContainerRef,
    initialState: editorState,
    setEditorState,
    fetchSuggestions,
    renderSuggestions,
    renderHint,
    handleLinks,
    handleEntitiesCb,
    resetIsTriggerInserted: () => setTriggerInserted(false),
    onFocusCb: () => console.log('Editor focused'),
  };

  return (
    <div>
      <RichTextEditor {...editorProps} />
    </div>
  );
};

export default InputBox;

Props

NameTypeDescription
isTriggerInserted?booleanIndicates if a trigger is inserted.
possibleTriggerKeys?ArrayArray of possible trigger keys.
placeholderstringPlaceholder text for the editor.
editorContainerRefReact.MutableRefObjectRef to the editor container.
childrenReact.ReactNodeChild components.
fontStyleClass?stringCSS class for editor font style.
initialState?IEditorStateInitial state of the editor.
externalTriggerKey?stringExternal trigger key.
setEditorState(editorState: EditorState) => voidFunction to set editor state.
fetchSuggestions?IFetchSuggestionsFunction to fetch suggestions.
renderSuggestions?IRenderSuggestionsFunction to render suggestions.
renderHint?IRenderHintFunction to render hints.
handleLinks?IHandleLinksFunction to handle links.
handleEntitiesCb?IHandleEntitiesCbFunction to handle entities.
resetIsTriggerInserted?() => voidFunction to reset trigger insertion.
onFocusCb?() => voidCallback on editor focus.

Prop Definition

NameTypeDescription
IFetchSuggestions(searchText: string, triggerKey: string) => Promise<{ suggestions: Array, showHint: boolean }>Function to fetch suggestions based on searchText and triggerKey.
IRenderSuggestions(position: { bottom: number; top: number; left: number; }, suggestions: Array, currentTriggerKey: string, onClose: () => void, handleAddEntity: (entityInfoData: IEntityInfo) => void, hideSuggestions: () => void) => JSX.ElementFunction to render suggestions with specified position, suggestions, current trigger key, and callbacks for actions.
IRenderHint(position: { bottom: number; top: number; left: number; }, currentTriggerKey: string, onClose: () => void, hideSuggestions: () => void) => JSX.ElementFunction to render a hint with specified position, current trigger key, and callbacks for actions.
IHandleLinks(type: 'add''delete', data: { meta: IEntityInfo, offsetKey: string }) => voidFunction to handle links based on the type of action ('add' or 'delete') and the provided data.
IHandleEntitiesCb(entityData: IEntityInfo, triggerKey: string) => voidFunction to handle entity data and trigger key.

Contributing

We welcome contributions! To contribute to this project, follow these steps:

  1. Fork the repository.
  2. Create a new branch: git checkout -b feature/your-feature-name.
  3. Make your changes and commit them: git commit -m 'Add some feature'.
  4. Push to the branch: git push origin feature/your-feature-name.
  5. Submit a pull request.
1.6.0

8 months ago

1.5.7

8 months ago

1.5.6

8 months ago

1.5.5

8 months ago

1.5.4

8 months ago

1.5.3

8 months ago

1.5.2

8 months ago

1.5.1

8 months ago

1.5.0

8 months ago

1.4.9

8 months ago