0.2.60 • Published 5 years ago

scholar-draft v0.2.60

Weekly downloads
5
License
LGPL-3.0
Repository
github
Last release
5 years ago

WIP

Initially inspired by https://github.com/AlastairTaft/draft-js-editor

scholar-draft aims at providing customizable and easy-to-use components for building academy-oriented editing apps with react-js and draft-js libraries.

Goals of scholar-draft

This module provides two react editor components that pursue the following main goals :

  • easily connect draft's editor entities to upstream applicationnal logic in a "one-way binding" manner : in scholar-draft, entities state and components are entirely handled upstream separately from the editor state. An entity's data is called an asset in the module's vocabulary
  • provide callbacks for adding, removing, & editing upstream assets from within the editor's interface
  • allow to insert, move and edit footnotes within a draft-js editor, while supporting assets as well
  • provide two ways to add an asset to the draft : drag-and-drop, inline selection thanks to an input component

To do so, the module provides two components :

  • BasicEditor : editor without footnotes support
  • Editor : editor with footnotes support

These components are as "pure" as possible, that is they do not contain their content's state but rather receive it in their props and trigger callbacks when subjected to user's change. For that reason the module also provides a set of utils functions for manipulating editor's state upstream of the editor (Create/Update/Delete operations on contents' notes, Create/Update/Delete operations on contents' assets, ...). Take a look at the stories folder for implementation examples.

Features

  • upstream-connected entities support
  • footnotes management & edition support
  • assets drag-and-drop support
  • block and inline asset wrappers allowing developpers to provide their own assets components (examples : image preview component, video preview component, reference preview component, ...). These components can be provided as plain previews, or can host user interactions and trigger callbacks to change upstream assets.
  • asset choice wrapper allowing developpers to provide their own asset selection component (example : an input field enabling to search in the list of relevant assets, ...)
  • markdown shortcuts handling
  • customizable medium-like contextual toolbar
  • customizable style, through css classes selectors
  • keyboard shortcuts (default: @ for inserting an asset, cmd+^ for inserting a footnote)

Editor API

Editor.propTypes = {
    /**
     * EDITOR STATE
     */
    // draft-js immutable object representing the main editor state
    mainEditorState: PropTypes.object,
    // map of immutable objects representing the notes editor state
    notes: PropTypes.object,
    // array of ids for representing the order of notes
    notesOrder: PropTypes.array,
    assetRequestPosition: PropTypes.object,
    assetRequestContentId: PropTypes.string,
    assetChoiceProps: PropTypes.object,
    focusedEditorId: PropTypes.string,
    // custom context object that will be passed to downstream entity decoration components
    customContext: PropTypes.object,
    // list of objects possibly embeddable inside the editor
    assets: PropTypes.object,

    /**
     * CALLBACKS
     */
    onEditorChange: PropTypes.func,
    onNoteAdd: PropTypes.func,
    onAssetChange: PropTypes.func,
    onAssetRequest: PropTypes.func,
    onAssetRequestCancel: PropTypes.func,
    onAssetChoice: PropTypes.func,
    onAssetClick: PropTypes.func,
    onAssetMouseOver: PropTypes.func,
    onAssetMouseOut: PropTypes.func,
    onAssetBlur: PropTypes.func,
    handlePastedText: PropTypes.func,
    onNotePointerMouseOver: PropTypes.func,
    onNotePointerMouseOut: PropTypes.func,
    onNotePointerMouseClick: PropTypes.func,
    onNoteDelete: PropTypes.func,
    onDrop: PropTypes.func,
    onDragOver: PropTypes.func,
    onClick: PropTypes.func,
    onBlur: PropTypes.func,

    /**
     * CUSTOMIZATION
     */
    // (translated) messages to provide to the editor for displaying
    messages: PropTypes.shape( {
      addNote: PropTypes.string,
      summonAsset: PropTypes.string,
      cancel: PropTypes.string,
    } ),
    // custom class for the editor
    editorClass: PropTypes.string,
    // editor placeholder text
    editorPlaceholder: PropTypes.string,
    inlineAssetComponents: PropTypes.object,
    blockAssetComponents: PropTypes.object,
    AssetChoiceComponent: PropTypes.func,
    NotePointerComponent: PropTypes.func,
    AssetButtonComponent: PropTypes.func,
    NoteButtonComponent: PropTypes.func,
    NoteLayout: PropTypes.func,
    inlineEntities: PropTypes.array,
    iconMap: PropTypes.object,
    inlineButtons: PropTypes.array,
    NoteContainerComponent: PropTypes.func,
    ElementLayoutComponent: PropTypes.func,
    
    // rendering mode to provide to entity decoration components
    renderingMode: PropTypes.string,

    keyBindingFn: PropTypes.func,
    
    // custom inline styles
    editorStyles: PropTypes.object,
  }

Utils API

insertAssetInEditor(editorState, asset, selection) returns new editorState

insertNoteInEditor(editorState, noteId, selection) returns new editorState

deleteAssetFromEditor(editorState, id, callback) returns new editorState

deleteNoteFromEditor(editorState, id, callback) returns new editorState

updateNotesFromEditor(editorState, notes) returns updated notes map

updateAssetsFromEditor(editorState, assets) returns new assets map

getUnusedAssets(editorState, assets) returns asset map

getUsedAssets(editorState, assets) returns asset map

insertFragment(editorState, fragment)

0.2.60

5 years ago

0.2.59

5 years ago

0.2.58

5 years ago

0.2.57

5 years ago

0.2.56

5 years ago

0.2.55

6 years ago

0.2.54

6 years ago

0.2.53

6 years ago

0.2.52

6 years ago

0.2.51

6 years ago

0.2.50

6 years ago

0.2.49

6 years ago

0.2.48

6 years ago

0.2.47

6 years ago

0.2.46

6 years ago

0.2.45

6 years ago

0.2.44

6 years ago

0.2.43

6 years ago

0.2.42

6 years ago

0.2.41

6 years ago

0.2.40

6 years ago

0.2.39

6 years ago

0.2.38

6 years ago

0.2.37

6 years ago

0.2.36

6 years ago

0.2.35

6 years ago

0.2.34

6 years ago

0.2.33

6 years ago

0.2.32

6 years ago

0.2.31

6 years ago

0.2.30

6 years ago

0.2.29

6 years ago

0.2.28

6 years ago

0.2.27

6 years ago

0.2.26

6 years ago

0.2.25

6 years ago

0.2.24

6 years ago

0.2.23

7 years ago

0.2.22

7 years ago

0.2.21

7 years ago

0.2.20

7 years ago

0.2.19

7 years ago

0.2.18

7 years ago

0.2.17

7 years ago

0.2.16

7 years ago

0.2.15

7 years ago

0.2.14

7 years ago

0.2.13

7 years ago

0.2.12

7 years ago

0.2.11

7 years ago

0.2.10

7 years ago

0.2.9

7 years ago

0.2.8

7 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago