ContentBox.js

InnovaStudio

Documentation

ContentBox.js is a web page designer. It uses ContentBuilder.js as its HTML editor with added features for page designing. You can use it to create your own CMS or online site builder.

Contents

  • Getting Started
  • Additional Includes for Viewing Content
  • Usage
  • Examples (HTML, PHP, React, Vue)
  • Folder Structure
  • Predesigned Sections (Templates)
  • Snippets
  • Typography Styles
  • Slider Feature
  • Language File
  • Adding Custom Buttons to the Sidebar
  • Embedding Box Cover (Background) Image
    • Form Method
    • AJAX Post Method
  • Programmatically Load the Content
  • Undo & Redo
  • Destroy
  • ContentBuilder.js Features

2

…………………………………………………………………………………. 3
…………………….. 5
..……………………………….……………………………………………………………………… 7
.……………………………………… 9
..…………………………………………………………………………….. 10
……..……..……………………. 11
…………………..………………………………….………………………………………… 15
………………………………………………………………………… 18
………………………………..………..………………………………………… 21

…………………………………………………………………………………. 22
…..……………….. 23
..……… 25
…………………………..……………………………………… 28
..…..………………………………………………….. 29
.……..……………………. 31
..……..…………………………………………………………………………… 32
………………………………………………………………………………………………… 32
.………..………………………………………… 33

Getting Started

Include the ContentBuilder css files:

<link href="assets/minimalist-blocks/content.css" rel="stylesheet" type="text/css" /> <!-- for snippets -->
<link href="contentbuilder/contentbuilder.css" rel="stylesheet" type="text/css" />

and the main ContentBox css file:

<link href="contentbox/contentbox.css" rel="stylesheet" type="text/css" />

Step 1

CSS

3

Install as Web Library

<link href="assets/minimalist-blocks/content.css" rel="stylesheet" type="text/css" /> <!-- for snippets -->
<link href="contentbuilder/contentbuilder.css" rel="stylesheet" type="text/css" />

Then import into your project:

import ContentBox from '@innovastudio/contentbox';

Step 2

JS

Or Install with NPM

npm install @innovastudio/contentbox

4

<link href="box/box-flex.css" rel="stylesheet" type="text/css" />

Additional Includes for Viewing Content

For viewing content, additional includes are also needed.

On the head section:

Before the end of the body tag:

<script src="box/box-flex.js" type="text/javascript">

5

<link href="box/box.css" rel="stylesheet" type="text/css" />

If you are upgrading from the previous version of ContentBox

Note: this is the same as in the previous version.

On the head section:

Before the end of the body tag:

<script src="path-to/jquery.min.js"> /* for running the slider */
<script src="box/box.js" type="text/javascript">

The JQuery include is needed here because the previous version of ContentBox includes predesigned sliders (using Slick slider) that require JQuery to run. If you are not using the slider in your content, then you do not need JQuery include and you can change the box.css & box.js includes with the box-flex.css & box-flex.js which exclude the JQuery dependency.

6

 <div class="is-wrapper">
 </div>

Usage

const builder = new ContentBox({
    wrapper: '.is-wrapper',
});

7

To get the edited content:

1. Get the HTML content

 let html = builder.html();

2. Get the styles

let mainCss = builder.mainCss(); // Returns the default typography style for the page.
let sectionCss = builder.sectionCss(); // Returns the typography styles for specific sections on the page

Then you can do anything with the content, for example, posting it to the server for saving, etc.

In production, the saved HTML content should be rendered with the styles.

8

Examples (HTML, PHP, React, Vue)

ContentBox.js is written in pure Javascript (ES6) so you can use it in most situations. Sample use in simple HTML, PHP, React and Vue projects are included.

React and Vue project examples are provided in separate downloads in the user area.

9

Folder Structure

  • public/
    • assets/
    • box/
    • contentbox/
    • contentbuilder/
    • uploads/
    • example.html (HTML example)
    • example.php (PHP example)
    • example-bootstrap.html (using Bootstrap framework)
    • example-foundation.html (using Foundation framework)
    • example-jquery.html (HTML example from the previous version using JQuery)
  • src/
    • contentbox/ (Only provided in Source Code package)
    • scss/ (Only provided in Source Code package)
  • docs/
  • README.md (Documentation)
  • readme.txt (Readme file)
  • readme-sourcecode.txt (Readme file for Source Code package)

10

Predesigned Sections (Templates)

To start building a page, you can click the (+) button on the top left sidebar. This will open a selection of predesigned sections that you can add into your page.

11

There are 2 predesigned section categories:

  • Simple Start (to start from a basic/clean design)
  • Quick Start (to start with examples)

12

The predesigned section files are located in the folder:

assets/designs/

It contains:

  • basic.js (a JSON file containing a list of simple designs)
  • examples.js (a JSON file containing a list of ready made designs)
  • images (contains assets for the designs)
  • preview (contains design thumbnails)

You can configure the file location by setting the designUrl1, designUrl2 and designPath parameters:

const builder = new ContentBox({
    wrapper: '.is-wrapper',
    designUrl1: 'assets/designs/basic.js',
    designUrl2: 'assets/designs/examples.js',
    designPath: 'assets/designs/',
});

13

In case of a different assets location, path adjustment may be needed. Here you can use the designPathReplace parameter.

Example:

const builder = new ContentBox({
    wrapper: '.is-wrapper',
    designUrl1: 'https://path-to/assets/designs/basic.js',
    designUrl2: 'https://path-to/assets/designs/examples.js',
    designPath: 'https://path-to/assets/designs/',
    designPathReplace: ['assets/designs/', 'https://path-to/assets/designs/'], // replace the default path to the new location
});

In this example, the default location is changed to https://path-to/assets/designs/

With this, you can place all the assets in a separate server or different host (e.g. from a CDN).

14

Snippets

Snippets are predesigned blocks that you can add or drag & drop into your content.
Snippet selection can be opened from the left sidebar.

15

Snippet files are located in the folder:

assets/minimalist-blocks/

It contains:

  • content.js (snippets JSON file)
  • content.css (snippets css file)
  • images (contains assets for the snippets)
  • preview (contains snippet thumbnails)

You can configure the snippets location by setting the snippetUrl and snippetPath parameters:

const builder = new ContentBox({
    wrapper: '.is-wrapper',
    snippetUrl: 'assets/minimalist-blocks/content.js', // Snippet file
    snippetPath: 'assets/minimalist-blocks/',  // Location of snippets' assets
});

16

In case of a different location, path adjustment may be needed. Here you can use the snippetPathReplace parameter.

Example:

const builder = new ContentBox({
    wrapper: '.is-wrapper',
    snippetUrl: 'https://path-to/assets/minimalist-blocks/content.js', // Snippet file
    snippetPath: 'https://path-to/assets/minimalist-blocks/',  // Location of snippets' assets
    snippetPathReplace: ['assets/', 'https://path-to/assets/'], // replace the default path to the new location
});

In this example, the default location is changed to https://path-to/assets/minimalist-blocks/

With this, you can place all the snippet assets in a separate server or different host (e.g. from a CDN).

17

Typography Styles

A selection of typography styles is provided for you to choose to format your page. The selection can be opened from the left sidebar.

18

The style can be used to format the entire page or just a specific section of your page.

To apply the typography style to a specific box area, open the Box Settings dialog, select the Text tab and click Change Style.

This will re-open the typography selection and your selection will be applied to the chosen box area only.

19

Typography style files are located in the folder:

assets/styles/

It contains all the css needed and its preview images. You can change its location using the contentStylePath parameter.

const builder = new ContentBox({
    wrapper: '.is-wrapper',
    contentStylePath: 'assets/styles/',
});

20

<link href="assets/scripts/glide/css/glide.core.css" rel="stylesheet" type="text/css" />
<link href="assets/scripts/glide/css/glide.theme.css" rel="stylesheet" type="text/css" />
<script src="assets/scripts/glide/glide.js" type="text/javascript">

Slider Feature

The new version includes predesigned slider templates (using Glide slider) that require some includes:

To enable the slider:

const builder = new ContentBox({
    /*...*/
    slider: 'glide' // default: 'slick' (old version slider)
});

Values:

  • ” (default) => not using slider
  • ‘glide’
  • ‘slick’ => previous version slider (requires JQuery)

21

Language File

With the Language file, you can translate the ContentBox.js interface into another language.

The language file is located in:

contentbuilder/lang/en.js

To enable the language file, you need to add the file before including ContentBox.js:

<script src="contentbox/lang/en.js" type="text/javascript">

Here is the language file content as seen on en.js:

var _txt = new Array();
_txt['Bold'] = 'Bold';
_txt['Italic'] = 'Italic';

You can create your own language file by copying/modifying this file.

22

Adding Custom Buttons to the Sidebar

To add custom buttons to the sidebar, use the addButton method.

Here is an example of adding the Undo & Redo button. For the undo and redo operation, we call the undo() and redo() methods.

builder.addButton({ 
    'pos': 2, // button position
    'title': 'Undo', // title
    'html': '<svg class="is-icon-flex" style="width:14px;height:14px;">', // icon
    'onClick': ()=>{
        builder.undo();
    }
});

builder.addButton({ 
    'pos': 3, // button position
    'title': 'Redo', // title
    'html': '<svg class="is-icon-flex" style="width:14px;height:14px;">', // icon
    'onClick': ()=>{
        builder.redo();
    }
});

23

The addButton method has 4 parameters:

  • pos (position of the button)
  • title
  • html (to specify the icon for the button)
  • onClick
builder.addButton({ 
    'pos': 5, 
    'title': 'Preview', 
    'html': '<svg class="is-icon-flex" style="width:16px;height:16px;">', 
    'onClick': ()=>{

        var html = builder.html();
        localStorage.setItem('preview-html', html); 
        var mainCss = builder.mainCss(); 
        localStorage.setItem('preview-maincss', mainCss); 
        var sectionCss = builder.sectionCss();
        localStorage.setItem('preview-sectioncss', sectionCss);

        window.open('/preview.html', '_blank').focus();
    }
});

Here is another example for adding a Preview button. If clicked, the button will open a separate page (preview.html) that we use to preview our edited page as in production.

Here we get the content and its styles using the html(), mainCss(), and sectionCss() methods and save them into the browser’s local storage. The content will then be used in the preview.html for viewing.

24

Embedding Box Cover (Background) Image

In the Box Settings, you can specify a box’s cover or background image.

25

However, to quickly upload an image without opening the Box Settings, you can click the box image button. This allows you to browse local images and select an image to upload as a box image cover (background).

26

27

There are 2 methods for uploading cover image:

  1. Form Method
  2. AJAX Post Method

Form Method

Here we use the coverImageHandler parameter to specify the upload handler for saving images on the server.

const builder = new ContentBox({
    wrapper: '.is-wrapper',
    coverImageHandler: 'savecover.php',
});

As examples, you can use the provided handler:

  • savecover.php (for PHP)
  • savecover.ashx (for ASP.NET)

28

AJAX Post Method

Here we use the onUploadCoverImage parameter to specify a custom upload function.

const builder = new ContentBox({
    wrapper: '.is-wrapper',
    onUploadCoverImage: (e) => {
        uploadFile(e, (response)=>{
            const uploadedImageUrl = response.url; // get saved image url
            builder.boxImage(uploadedImageUrl); // change cover image
        });
    },
});

function uploadFile(e, callback) {

    const selectedFile = e.target.files[0];

    const formData = new FormData();
    formData.append('file', selectedFile);
    fetch('/upload', {
        method: 'POST',
        body: formData,
    })
    .then(response=>response.json())
    .then(response=>{
        console.log(response)
        if(callback) callback(response);
    });
}

The example above uses the boxImage(url) method to apply the image url as a background cover.

29

In the example, the image is posted to an endpoint: /upload
If you’re using Node.js, you can implement the endpoint to save the image using:

const express = require('express');
const fs = require('fs');
const app = express();
const path = require('path');
const cors = require('cors');
const serveStatic = require('serve-static');
const formidable = require('formidable-serverless');
const sharp = require('sharp');

//Specify url path
var $path =  __dirname + '/public/uploads/'; // Physical path
var $urlpath = 'uploads/'; // URL path

app.use(cors());
app.use(express.urlencoded({
    extended: true
}));
app.use(express.json({ limit: '50mb' }));
app.use(serveStatic(path.join(__dirname, '/public')));

app.post('/upload', (req, res) => {

    const form = new formidable.IncomingForm();
    form.parse(req, async (err, fields, files) => {

        if (err) return res.status(500).json({ 
            ok:true, 
            status: 500, 
            error: 'Something went wrong.' 
        });

        let extension = path.extname(files.file.name).toLowerCase();
        if(extension  !== '.jpeg' && extension !== '.jpg' 
            && extension !== '.png' && extension !== '.gif' 
            && extension  !== '.webp' && extension  !== '.webm' 
            && extension  !== '.mp4') {

30

            res.status(500).json({ 
                ok:true, 
                status: 500, 
                error: 'File type not allowed.'
            });
            return;
        }
        
        const file = fs.readFileSync(files.file.path);

        let imageFile = file;
        if(extension  === '.jpeg' || extension === '.jpg') {
            imageFile = await sharp(files.file.path).resize(1600, 1600, {
                fit: sharp.fit.inside,
                withoutEnlargement: true, 
            })
            .jpeg({ quality: 80, progressive: true, force: false })
            .toBuffer();
        }

        fs.writeFile($path + files.file.name, imageFile, async (err)=>{
            if (err) {
                res.status(500).json({ 
                    ok:true, 
                    status: 500, 
                    error: 'Something went wrong.' 
                });
                return 
            } 
            res.status(200).json({ 
                ok:true, 
                status: 200, 
                url:  $urlpath + files.file.name
            });
        });
    });
});

Programmatically Load the Content

Content consists of HTML and its styles (e.g. typography styles/css includes). As explained previously, you get the edited content using the following methods:

1. To get the HTML

let html = builder.html();

2. To get the styles

let mainCss = builder.mainCss(); // Returns the default typography style for the page.
let sectionCss = builder.sectionCss(); // Returns the typography styles for specific sections on the page

You can save the HTML and its styles above into a database. And when you need to load the content back for editing, use the loadHtml() and loadStyles methods.

builder.loadHtml(html);
builder.loadStyles(mainCss, sectionCss);

31

Undo & Redo

To undo:

builder.undo();

To redo:

builder.redo();

32

Destroy

To destroy the ContentBox:

builder.destroy();

ContentBuilder.js Features

ContentBox.js uses ContentBuilder.js as its HTML editor. So most of the ContentBuilder.js options/parameters can be accessed through the ContentBox.js object.

For example, to specify a custom file browser (or Asset Manager):

const builder = new ContentBox({
    wrapper: '.is-wrapper',
    imageSelect: 'assets.html',
    fileSelect: 'assets.html',
    videoSelect: 'assets.html',
});

33

const builder = new ContentBox({
    wrapper: '.is-wrapper',

    onMediaUpload: (e)=>{
        uploadFile(e, (response)=>{
            ...
        });
    },
    onVideoUpload: (e)=>{
        uploadFile(e, (response)=>{
            ...
        });
    },  
});

And to specify custom upload functions:

34

More configuration options can be found in the ContentBuilder.js documentation: https://demo.innovastudio.com/docs/ContentBuilder.pdf

35

Home » ContentBox.js Documentation

© 2022 InnovaStudio