0.0.163 • Published 2 years ago

otter-wp v0.0.163

Weekly downloads
305
License
MIT
Repository
github
Last release
2 years ago

Otter, a block-based content editor

A content editor, with declaratively defined content blocks. Easily imports as a react component. Generates data in a simple JSON format.

Contents

Otter element

<Otter.Editor data={data} load_state={Otter.State.Loaded} delegate={my_delegate} blocks={blockset} />,

Renders an Otter editor.

  • blocks : the set of editor blocks available in this editor (see Block API)
  • data : initialize the editor with some content
  • load_state : one of the loading states (Loading and Error are useful when loading content data asynchronously):
    • Otter.State.Loading
    • Otter.State.Loaded
    • Otter.State.Error
  • delegate : Otter calls the on_update() method of your delegate object when data is saved:
const my_delagate = {
  on_update(data) {
    // Kick-off a request to update the post data
  },
};

Block API

Otter.Blockset([ <block>, <block>, ... ]) -> blockset

  • Define a blockset (a set of content blocks) for use in the editor.
  • Returns the blockset object.
  • Arguments:
    • [ <block>, <block>, ... ]: an array of content block definitions
const text_blocks = Otter.Blockset([{
  type: 'MyTextBlock',
  description: 'Text block',
  fields: [ <field>, <field>, ... ],
}]);

Methods

get(block_type) -> block

  • Defined on a blockset created with Otter.blockset().
  • Returns the requested block object from the blockset array.

Field API

A block contains one or more fields (see above).

  • <field> :
{
  name: 'content',
  description: 'Content',
  type: Otter.Fields.Textarea,
  display_if: [{                  // To display this field only if a sibling has a value
    sibling: <sibling_name>,
    equal_to: <value>,            // mutually exclusive
    not_equal_to: <value>,        //
  }],
}

(Note that display_if can only operate on siblings of type Bool or Radio.)

Field types:

Otter.Fields.TextInput

  • A simple text input.

Otter.Fields.TextArea

  • A textarea for multiline, unstyled text.

Otter.Fields.TextEditor

  • Multiline rich text editor.

Otter.Fields.Bool

  • A yes/no toggle.
  • Options:
    • text__yes: "Yes": label for the on state option
    • text__no: "No": label for the off state option

Otter.Fields.Radios

  • A set of radio buttons
  • Options:
    • options: { value: "Label", ... } : the set of radios

Otter.Fields.WPImage

  • For wordpress integration: a wordpress media item. TBD.

Otter.Fields.SubBlock

  • Embed another block into this block. Otter can compose blocks together recursively, allowing for complex content types, and aiding re-use of block definitions.
  • Options:
    • description : if supplied, used to title the wrapped subblock in the editor
    • subblock_type: MyTextBlock : a block object (previously created with Otter.Blockset()) defining which subblock to embed.
Otter.Blockset([
  {
    name: 'MyHeaderAndContentBlock',
    fields: [
      {
        name: 'header',
        description: 'Header',
        type: Otter.Fields.TextInput,
      },
      {
        name: 'content',
        description: 'Content',
        type: Otter.Fields.SubBlock,
        subblock_type: text_blocks.get('MyTextBlock'),
      },
    ],
  }
]);

Otter.Fields.SubBlockArray

  • Create a picker where the user can manage an array of subblocks, picking from types you predefine.
  • Options:
    • description : if suppied, used to title the wrapped subblock array in the editor
    • subblock_types: [ <block1>, <block2>, ... ] : an array of block objects (previously created with Otter.Blockset()) defining what types of subblock the user can add to this subblock array.
Otter.Blockset([
  {
    name: 'MyMultiContentBlock',
    fields: [
      {
        name: 'content',
        type: Otter.Fields.SubBlockArray,
        subblock_types: [
          my_blocks.get('MyContentBlock__1'),
          my_blocks.get('MyContentBlock__2'),
        ],
      },
    ],
  }
]);

Sample setup with create-react-app

npx create-react-app my-app
cd my-app
npm i -P otter-editor

To start:

  • replace src/App.js with the demo App.js file
  • run npm run start
  • visit localhost:3000

Basic full example

import React from 'react';
import ReactDOM from 'react-dom';
import Otter from 'otter-editor';
import 'otter-editor/dist/otter.css';


// Define content blocks
// -------------------------------

const blockset = Otter.Blockset([
  {
    type: 'HeaderBlock',
    description: 'Heading',
    fields: [
      { name: 'title', description: 'Title', type: Otter.Fields.TextInput },
      { name: 'author', description: 'Author', type: Otter.Fields.TextInput },
    ],
  },
  {
    type: 'TextBlock',
    description: 'Text content',
    fields: [
      { name: 'content', description: 'Content', type: Otter.Fields.TextArea },
    ],
  },
]);


// Handle updates
// -------------------------------

const delegate = {
  on_update(data) {
    console.log('new data:', data),
  },
};


// Fetch some data & render
// -------------------------------

function render() {
  const data = [
    { __type: 'HeaderBlock', title: 'My article', author: 'Jane Smith' },
  ];

  ReactDOM.render(
    <Otter.Editor data={data} load_state='loaded' delegate={delegate} blocks={blockset} />,
    document.querySelector('.otter-container')
  );
}


render();

CSS

The app needs to import the Otter CSS:

import 'otter-editor/dist/otter.css';

This currently includes bulma so at present weighs ~150kB. TBD: move to something more lightweight.

Demo project

/demo contains a sample Otter editor, including some simple predefined blocks. Run npm run dev to start webpack-dev-server, then visit http://localhost:3000.

Or using parcel (npm i -g parcel-bundler), instead run parcel demo/index.html.

Development

Use the demo project and the dev task.

To publish: npm publish.

License

Otter is dual-licensed to enable Wordpress integration. The license is:

  • GPLv2 for the purpose of embedding within Wordpress themes
  • MIT for all other purposes

See LICENSE.md for details.

0.0.163

2 years ago

0.0.162

3 years ago

0.0.159

3 years ago

0.0.158

3 years ago

0.0.151

3 years ago

0.0.150

3 years ago

0.0.157

3 years ago

0.0.156

3 years ago

0.0.154

3 years ago

0.0.161

3 years ago

0.0.160

3 years ago

0.0.148

3 years ago

0.0.147

3 years ago

0.0.142

3 years ago

0.0.141

3 years ago

0.0.146

3 years ago

0.0.145

3 years ago

0.0.128

3 years ago

0.0.127

3 years ago

0.0.126

3 years ago

0.0.129

3 years ago

0.0.139

3 years ago

0.0.138

3 years ago

0.0.137

3 years ago

0.0.136

3 years ago

0.0.130

3 years ago

0.0.135

3 years ago

0.0.134

3 years ago

0.0.132

3 years ago

0.0.140

3 years ago

0.0.125

3 years ago

0.0.120

3 years ago

0.0.124

3 years ago

0.0.123

3 years ago

0.0.122

3 years ago

0.0.121

3 years ago

0.0.119

3 years ago

0.0.117

3 years ago

0.0.116

3 years ago

0.0.115

3 years ago

0.0.118

3 years ago

0.0.113

3 years ago

0.0.112

3 years ago

0.0.111

3 years ago

0.0.110

3 years ago

0.0.106

3 years ago

0.0.109

3 years ago

0.0.108

3 years ago

0.0.107

3 years ago

0.0.105

3 years ago

0.0.104

3 years ago

0.0.103

3 years ago

0.0.102

3 years ago

0.0.101

3 years ago

0.0.96

3 years ago

0.0.97

3 years ago

0.0.99

3 years ago

0.0.91

3 years ago

0.0.92

3 years ago

0.0.100

3 years ago

0.0.93

3 years ago

0.0.90

3 years ago

0.0.89

3 years ago

0.0.87

3 years ago

0.0.88

3 years ago

0.0.85

3 years ago

0.0.86

3 years ago

0.0.84

3 years ago

0.0.81

3 years ago

0.0.82

3 years ago

0.0.83

3 years ago

0.0.80

3 years ago

0.0.76

3 years ago

0.0.77

3 years ago

0.0.78

3 years ago

0.0.79

3 years ago

0.0.75

3 years ago

0.0.74

3 years ago

0.0.73

3 years ago

0.0.72

3 years ago

0.0.71

3 years ago

0.0.70

3 years ago

0.0.69

3 years ago

0.0.68

3 years ago

0.0.67

3 years ago

0.0.66

3 years ago

0.0.65

3 years ago

0.0.63

3 years ago

0.0.64

3 years ago

0.0.62

3 years ago

0.0.61

3 years ago

0.0.60

3 years ago

0.0.59

4 years ago

0.0.58

4 years ago

0.0.53

4 years ago

0.0.54

4 years ago

0.0.55

4 years ago

0.0.56

4 years ago

0.0.57

4 years ago

0.0.40

4 years ago

0.0.39

4 years ago

0.0.37

4 years ago

0.0.36

4 years ago

0.0.35

4 years ago

0.0.34

4 years ago

0.0.33

4 years ago

0.0.31

4 years ago

0.0.32

4 years ago

0.0.30

4 years ago

0.0.29

4 years ago

0.0.28

4 years ago

0.0.26

4 years ago

0.0.27

4 years ago

0.0.25

4 years ago

0.0.23

4 years ago

0.0.24

4 years ago

0.0.22

4 years ago

0.0.20

4 years ago

0.0.21

4 years ago

0.0.19

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.16

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago