0.0.2 • Published 8 months ago

@huongda-group/lexical-file-manager v0.0.2

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

Power by Hương Đá Group 🇻🇳

File manager for Lexical (React) editor

This package provides a set of components and hooks for Lexical that allow for manage files (like Wordpress media) in React applications. Pull requests are welcome.


See full example. Demo

Hương Đá Group Lexical file manager


Features

  • List files.
  • Upload file.
  • Delete file.
  • Loading status.
  • View file text.
  • Multiple language.
  • View file audio.
  • View file image
  • View file video
  • Folder manager

Getting started

Install lexical and @lexical/react:

npm install --save lexical @lexical/react

Install @huongda-group/lexical-file-manager:

npm install --save @huongda-group/lexical-file-manager

Add Node & Plugin to config:

import { FileManagerPlugin, FileManagerNode } from '@huongda-group/lexical-file-manager';

const editorConfig = {
  namespace: 'React.js Demo',
  nodes: [FileManagerNode], // Add node
  onError(error: Error) {
    throw error;
  },
  theme: ExampleTheme
};
<LexicalComposer initialConfig={editorConfig}>
  <div className="editor-container">
    <ToolbarPlugin />
    <div className="editor-inner">
      <RichTextPlugin
        contentEditable={
          <ContentEditable
            className="editor-input"
            aria-placeholder={placeholder}
          />
        }
        placeholder={
          <div className="editor-placeholder">{placeholder}</div>
        }
        ErrorBoundary={LexicalErrorBoundary}
      />
      <HistoryPlugin />
      <AutoFocusPlugin />
      <TreeViewPlugin />
      <FileManagerPlugin /> {/*Add plugin here*/}
    </div>
  </div>
</LexicalComposer>

Add Button to Toolbar:

import { FileManager } from '@huongda-group/lexical-file-manager';
<FileManager
  title="Multiple Files Manager"
  editor={editor}
  files={[
    {
      id: '1',
      name: 'Image 1',
      url: 'https://placehold.co/600x400/orange/white',
      size: 1024,
      mimetype: 'image/jpeg',
    },
    {
      id: '2',
      name: 'Audio 1',
      url: 'https://www.kozco.com/tech/LRMonoPhase4.mp3',
      size: 1024,
      mimetype: 'audio/mpeg',
    },
    {
      id: '3',
      name: 'Video 1',
      url: 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
      size: 1024,
      mimetype: 'video/mp4',
    },
    {
      id: '4',
      name: 'File 1',
      url: 'https://example-files.online-convert.com/document/txt/example.txt',
      size: 1024,
      mimetype: 'text/plain'
    },
  ].map((item, index) => ({ ...item, index }))}
  aria-label="File Manager Multiple"
>
  <button className="toolbar-item" aria-label="File Manager">
    Upload
  </button>
</FileManager>

Props

NameRequiredTypeDefault valueDescription
editortrueLexicalEditor-Editor from useLexicalComposerContext()
filestrueFileItem[]List of files to add
onUploadtrue{multiple: boolean; onUpload: (f: FileItem/FileItem[]) => void}-callback when upload
onClosefalse() => void - optional-callback when close modal
onInsertfalse(f: FileItem/FileItem[]) => void - optional-callback when insert file/files
onDeletefalse(f: FileItem) => void - optional-callback when delete file
multiplefalseboolean - optionalfalseconfig upload simple or multiple files
titlefalsestring - optional-title of modal

FileItem interface

NameRequiredTypeDefault valueDescription
idtruestring-Identify each file
nametruestring-Name
urltruestring-Url of file
sizetruenumber-Size of file in bytes
mimetypetrueMimetype-Mimetype of file
indextruenumber-Order of file
keyfalseNodeKey-NodeKey of lexical

Mimetype type

Mimetype includes file types such as:

  • Image
    • image/jpeg
    • image/png
    • image/gif
    • image/bmp
    • image/webp
    • image/tiff
    • image/svg+xml
  • Audio
    • audio/mpeg
    • audio/wav
    • audio/ogg
    • audio/mp4
    • audio/aac
    • audio/flac
    • audio/amr
    • audio/x-ms-wma
  • Video
    • video/mp4
    • video/webm
    • video/x-msvideo
    • video/quicktime
    • video/x-ms-wav
    • video/x-flv
    • video/x-matroska
    • video/mpeg
  • Other file
    • string ...

License

This project is licensed under the MIT License - see the LICENSE