3.5.2 • Published 1 month ago

@oakjs/core v3.5.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

GitHub npm CI codecov

@oakjs/core

A modern, lightweight & modulable page builder

ℹ️ This package is probably not what you're looking for, unless you're building a renderer for your favorite framework or you're creating a shiny addon.

Installation

yarn add @oakjs/core

Usage

import { Builder } from '@oakjs/core';

const builder = new Builder();

Documentation

Options

addons

  • Type: Array<AddonObject>
  • Default: []

Adds a list of addons to the page builder. See addons for more information.

new Builder({ addons: [baseAddon()] });

content

  • Type: Array<ElementObject|Element>
  • Default: []

Default content to add to the builder on init.

new Builder({ content: [{ id: 1, type: 'text', content: 'Foo' }] });

options

  • Type: BuilderOptions
  • Default: {}

Builder options.

new Builder({ options: {} });

debug

  • Type: boolean
  • Default: false

Enable/disable debug mode (more logs).

new Builder({ options: { debug: true } });

historyLimit

  • Type: number
  • Default: 20

Maximum count of history items (for undo/redo).

new Builder({ options: { historyLimit: 100 } });

generateId

  • Type: Function generateId(): string
  • Default: null

Allows to generate custom unique ids for elements (defaults to uuidv4())

new Builder({ options: { generateId: () => Math.random() } });

Events

onChange

  • Arguments: ({ value: Array }: Object)

Example:

import { render } from '@poool/oak';

render(element, {
  events: {
    onChange: ({ value }) => console.log(value),
  },
});

Called everytime the builder's content changes.

onImageUpload

  • Arguments: (event: Event)

Called when an image is uploaded using the image field type. The event argument is the native file input event.

Example:

import { render } from '@poool/oak';

render(element, {
  events: {
    onImageUpload: event => {
      const reader = new FileReader();
      const image = e.target.files[0];

      return { url: reader.readAsDataURL(image), name: image.name };
    },
  },
});

Addons

Oak is by definition an empty shell, composed of addons. Creating addons allows you to add new components, fields, settings or even override some of them, to the core builder.

There are 4 things an addon can create:

  • Components: the building blocks of oak, they define the final structure of the elements that will be added to your content
  • Fields: field definition used by the settings to edit the configuration of your elements
  • Settings: elements settings using field definitions
  • Overrides: allows to override (hence the name) some component settings fields, fields props, ...
  • Texts: allows to create localized text sheets for the builder and everything inside

ℹ️ For things that need to be rendered (e.g components & fields), the renderer will need to extend the addon to add a render method.

If you need to have a look at more complex examples than those in this file, feel free to take a look at all the addons we have already created inside the @oakjs/react or @oakjs/addon-ckeditor5-react packages.

Components

Type definition: packages/core/lib/types.d.ts:159

Example for a new quote component:

import { Builder } from '@oakjs/core';

const builder = new Builder({
  addons: [{
    components: [{
      id: 'quote',
      name: t => t('customTexts.quote.title', 'Quote component'),
      type: 'component',
      render: ({ content, author }) =>
        `<blockquote>${content}<cite>${author}</cite></blockquote>`,
      construct: () => ({
        type: 'quote',
        content: '',
        author: '',
      }),
      settings: {
        title: t => t('customTexts.quote.settings.title',
          'Quote options'),
        fields: [{
          key: 'content',
          type: 'text',
          default: '',
          displayable: true,
        }, {
          key: 'author',
          type: 'text',
          displayable: true,
        }],
      },
    }],
  }],
});

Fields

Type definition: packages/core/lib/types.d.ts:34

Example for a new enhanced-text field:

import { Builder } from '@oakjs/core';

const builder = new Builder({
  addons: [{
    fields: [{
      type: 'enhanced-text',
      render: (baseProps, customProps) => (
        <textarea { ...customProps } { ...baseProps } />
      ),
    }],
  }],
});

Settings

The settings definition can be split into 2 parts :

Fields not referencing any tab will be displayed in the first tab, aka "Settings".

Example for a new ariaLabel setting only available on the quote component:

import { Builder } from '@oakjs/core';

const builder = new Builder({
  addons: [{
    settings: [{
      id: 'ariaLabel',
      key: 'settings.ariaLabel',
      type: 'text',
      default: '',
      displayable: true,
      condition: element => element.type === 'quote',
    }],
  }],
});

Overrides

There are currently two type of overrides:

For example, if you want to override the content field for the title, text & button components and make it a richtext field instead of a basic textarea:

import { Builder } from '@oakjs/core';

const builder = new Builder({
  addons: [{
    overrides: [{
      type: 'component',
      targets: ['title', 'text', 'button'],
      fields: [{
        key: 'content',
        type: 'richtext',
        placeholder: 'Write something...',
      }],
    }],
  }],
});

For example, if you want to override the render method of the textarea field to add a text underneath:

import { Builder } from '@oakjs/core';

const builder = new Builder({
  addons: [{
    overrides: [{
      type: 'field',
      targets: ['textarea'],
      render: (fieldProps, customProps) => (
        <div>
          <textarea { ...customProps } { ...fieldProps } />
          <p>Some text underneath</p>
        </div>
      ),
    }],
  }],
});

For example, if you want to override the placeholder property of the settings.className field for the title component:

import { Builder } from '@oakjs/core';

const builder = new Builder({
  addons: [{
    overrides: [{
      type: 'setting',
      targets: ['title'],
      key: 'settings.className',
      placeholder: 'Add a class name...',
    }],
  }],
});

Texts

Type definition: packages/core/lib/types.d.ts:222

Every text inside the builder can be overriden using a new text sheet passed inside an addon. For example, if you need to add a french text sheet:

import { Builder } from '@oakjs/core';

const builder = new Builder({
  addons: [{
    texts: [{
      id: 'fr',
      texts: {
        core: {
          settings: {
            cancel: 'Annuler',
            save: 'Sauvegarder',
          },
        },
      }
    }]
  }],
  activeTextSheet: 'fr',
});

To use these translations, every label, title or name property inside components, fields, overrides & settings can either be strings (not translated), or functions, for which the first argument is a function called the translate function. This function is passed to each of these property for you to be able to provide the text key & the default value in your current language.

For example, if you need to translate the name of your custom quote component:

{
  id: 'quote',
  type: 'component',
  name: t => t('custom.myComponent.myField.label', 'My component'),
}

Contributing

npm.io

Please check the CONTRIBUTING.md doc for contribution guidelines.

License

This software is licensed under MIT.

3.5.2

1 month ago

3.5.1

3 months ago

3.5.0

3 months ago

3.4.0

6 months ago

3.2.2

9 months ago

3.3.0

6 months ago

3.2.1

9 months ago

3.2.0

10 months ago

3.0.10

10 months ago

3.1.0

10 months ago

3.0.9

11 months ago

3.0.4

12 months ago

3.0.1

12 months ago

3.0.0

12 months ago

3.0.0-alpha.39

1 year ago

3.0.0-alpha.33

1 year ago

3.0.0-alpha.32

1 year ago

3.0.0-alpha.29

1 year ago

3.0.0-alpha.26

1 year ago

3.0.0-alpha.25

1 year ago

3.0.0-alpha.24

1 year ago

3.0.0-alpha.21

1 year ago

3.0.0-alpha.18

1 year ago

3.0.0-alpha.17

1 year ago

3.0.0-alpha.15

1 year ago

3.0.0-alpha.14

1 year ago

3.0.0-alpha.13

1 year ago

3.0.0-alpha.12

1 year ago

3.0.0-alpha.11

1 year ago

3.0.0-alpha.7

1 year ago

3.0.0-alpha.6

1 year ago

3.0.0-alpha.4

1 year ago

3.0.0-alpha.3

1 year ago

3.0.0-alpha.2

1 year ago

3.0.0-alpha.1

1 year ago

3.0.0-alpha.0

1 year ago