3.0.2 • Published 1 year ago

@edtr-io/plugin-text v3.0.2

Weekly downloads
281
License
MIT
Repository
github
Last release
1 year ago

Text plugin (TextEditor)

Table of contents

Usage

An example of how to use the Text plugin can be found in ./__fixtures__/index.ts.

Structure

The ./src/index.tsx file contains and exposes the Text plugin factory function and exposes the public types.

Additionally, there are:

  • components - React components
  • hooks - plugable hooks for configuration
  • plugins - Slate plugin files
  • types - TypeScript types used across the Text plugin (React component prop types are located in their respective component files)
  • utils - utility functions used across the Text plugin

Technical decisions

Configuration

A config argument (of TextEditorConfig type) is passed to createTextPlugin factory, when creating a new Text plugin instance.

This argument is then passed to the useTextConfig hook, where it's merged with the default settings, as well as enriched with i18n and theming.

The config object received from the useTextConfig hook is then used as the source of truth for configuration across that instance of the Text plugin.

Controls (Text plugin plugins)

Currently used Slate version only allows Slate plugins to modify the editor object. To allow for the same functionality of plugins from the earlier version of serlo-editor/edtr-io, a hook approach was used (as recommended by the creator of Slate).

The useControls hook receives the config object and exposes three properties:

  1. createTextEditor - a function that receives a Slate editor instance and wraps it in all the configured Slate plugins
  2. toolbarControls - the configuration for Text plugin's toolbar
  3. handleHotkeys - keyboard shortcut handlers for configured controls

This approach allows to simply pass an array of desired controls (as controls property of the config argument) when creating a Text plugin instance, thus making the controls easily configurable for the user of Text plugin.

Suggestions (serlo-editor/edtr-io plugins)

In order to easily transform a Text plugin into another serlo-editor/edtr-io plugin, the user can simply type / into an empty Text plugin, and they will be presented with a list of suggestions. A hook approach was used to make the suggestions easily configurable.

The useSuggestions hook receives:

  • current text content of the Text plugin
  • the id of the Text plugin
  • focused and editable states of the Text plugin

and exposes:

  1. showSuggestions - a flag controlling if the suggestions box should be shown
  2. suggestionsProps - props for the Suggestions component
  3. hotKeysProps - props for the HotKeys component
  4. handleHotkeys - keyboard shortcut handlers for configured controls

Saving state to Redux store

In order to enable global undo/redo behavior (TODO: and maybe other things?), any content changes are saved to the store, and previous values of Editor's value and selection are saved as refs withing the instance of Text plugin component.

If a portion of the content is selected and then replaced with some text, undo will restore the replaced content and the selection. Slate Editor's value prop is used only as an initial value and changing the bound value will not result in a rerender. Therefore, we have to manually assign the value to editor.children (as recommended by the Slate team).

Simple selection changes are not saved to the store, because we don't want to undo pure selection changes.

LinkControls workaround

The LinkControls component should only be shown if the selection is currently on a link inline. But, since we don't save pure selection changes to the store, LinkControls doesn't rerender on selection changes. To work around this problem, a simple useState hook is used:

  1. hasSelectionChanged is passed to LinkControls, where it's used a dependency in a useEffect hook which takes care of showing LinkControls
  2. setHasSelectionChanged is called whenever selection changes, which increments hasSelectionChanged and makes sure LinkControls visibility will be updated
3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.8.2

2 years ago

2.6.0

2 years ago

2.5.1

2 years ago

2.4.0

2 years ago

2.5.0

2 years ago

2.3.2

2 years ago

2.3.0

3 years ago

2.3.1

3 years ago

2.2.0

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

2.0.0-beta.2

3 years ago

2.0.0-beta.1

3 years ago

2.0.0-beta.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.0-beta.4

4 years ago

1.0.0-beta.3

4 years ago

1.0.0-beta.2

4 years ago

1.0.0-beta.1

4 years ago

1.0.0-beta.0

4 years ago

0.52.2

4 years ago

0.52.1

4 years ago

0.52.0

4 years ago

0.51.2

4 years ago

0.51.3

4 years ago

0.51.1

4 years ago

0.51.0

4 years ago

0.50.1

4 years ago

0.50.0

4 years ago

0.14.0

4 years ago

0.13.7

4 years ago

0.13.6

4 years ago

0.13.4

4 years ago

0.13.5

4 years ago

0.13.5-alpha.1

4 years ago

0.13.5-alpha.0

4 years ago

0.13.5-alpha.2

4 years ago

0.13.3

4 years ago

0.13.2

4 years ago

0.13.1

4 years ago

0.13.0

4 years ago

0.13.0-alpha.0

4 years ago

0.12.0

5 years ago

0.11.3

5 years ago

0.11.2

5 years ago

0.11.1

5 years ago

0.11.0

5 years ago

0.10.3

5 years ago

0.10.2

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.7

5 years ago

0.9.6

5 years ago

0.9.5

5 years ago

0.9.4

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.8.0-alpha.1

5 years ago

0.8.0-alpha.0

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.7.0-alpha.4

5 years ago

0.7.0-alpha.3

5 years ago

0.6.2

5 years ago

0.7.0-alpha.2

5 years ago

0.7.0-alpha.1

5 years ago

0.7.0-alpha.0

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.1

5 years ago