Socket
Socket
Sign inDemoInstall

@innovastudio/contentbox

Package Overview
Dependencies
60
Maintainers
1
Versions
405
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @innovastudio/contentbox

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.


Version published
Weekly downloads
140
decreased by-23.08%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

ContentBox.js

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.

Screenshot

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" />
Install as Web Library

Include the js file:

<script src="contentbox/contentbox.min.js" type="text/javascript"></script>
Or Install with NPM
npm install @innovastudio/contentbox

Then import into your project:

import ContentBox from '@innovastudio/contentbox';

Additional Includes for Viewing Content

For viewing content, additional includes are also needed.

On the head section:

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

Before the end of the body tag:

<script src="box/box-flex.js" type="text/javascript"></script>
If you are upgrading from the previous version of ContentBox

(this is the same as in the previous version)

On the head section:

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

Before the end of the body tag:

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

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.

Usage

 <div class="is-wrapper">
 </div>
const builder = new ContentBox({
    wrapper: '.is-wrapper',
});

To get the edited content:

  1. Get the HTML content
let html = builder.html();
  1. 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 its styles.

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.

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)
  • ...

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.

Simple Start

Quick Start

There are 2 predesigned section categories:

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

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/',
});

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).

Snippets

Snippets are predesigned blocks that you can add or drag & drop into your content.

Snippet selection can be opened from the left sidebar.

Snippets

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
});

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).

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.

Typography Styles

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'.

Apply to a specific section

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

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/',
});

Slider Feature

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

<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"></script>

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)

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"></script>

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.

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;"><use xlink:href="#ion-ios-undo"></use></svg>', // icon
    'onClick': ()=>{
        builder.undo();
    }
});

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

The addButton method has 4 parameters:

  • pos (position of the button)
  • title
  • html (to specify the icon for the button)
  • onClick

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.

builder.addButton({ 
    'pos': 5, 
    'title': 'Preview', 
    'html': '<svg class="is-icon-flex" style="width:16px;height:16px;"><use xlink:href="#ion-eye"></use></svg>', 
    '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 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.

Embedding Box Cover (Background) Image

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

Box Cover

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).

Box Cover

Box Cover

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)
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.

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') {
            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();
  1. 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);

Undo & Redo

You can programmatically perform an undo or redo action.

To undo:

builder.undo();

To redo:

builder.redo();

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',
});

And to specify custom upload functions:

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

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

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

Credits

Libraries

FAQs

Last updated on 21 May 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc