Help Center
Search…
Setka Post Editor initialization (JavaScript)

Terms and Definitions

Style Manager: web interface for creating and editing a branded style of a publication (module grid, text and color styles, icons, etc.). You can find it at editor.setka.io.
Post: HTML code of the post wrapped in <div class=“stk-post”>...</div>.
Post style: a set of CSS rules for text styles, fonts, colors, dividers, etc., which determines a branded style of a publication. Post style is edited in Style Manager.
Post grid system: a set of CSS rules for building grid modules with columns and indents and for adaptation to mobile devices. The post grid system is edited in the Style Manager.
All styles and grid systems within one company are uploaded from Style Manager to CDN in the form of a CSS file with styles and a JSON file with meta information to be used in the editor.
The editor requires 5 files to operate properly. These files need to be loaded in the post-editing and/or view pages.
File
Description
Post-editing page
Post view page
editor.min.js
Main editor file
yes
no
editor.min.css
Editor UI styles
yes
no
company_name.min.css
Post styles defined in the Style Manager (fonts, colors, grids, etc.)
yes
yes
company_name.json
JSON with metadata required for work of the editor
yes
no
public.js
JS code required for work of interactive capabilities of the editor (galleries, animation)
no
yes

Editor integration steps

4 initial steps of the integration should be passed to use Setka Editor fully:
1. Load all of the required files to a post-editing page and add the editor initialization code.
2. Load all of the required files to a post view page.
3. Configure saving the post HTML code into a database and load it back into the editor.
4. Use the editor API to upload and delete images from your server.

1. Initializing the editor

Here is the editor initialization sample code:
1
2
// 1. Set paths to editor files:
3
// (on the Setka Editor CDN or on your own server):
4
5
// Main editor file
6
const EDITOR_JS_URL =
7
'https://ceditor.setka.io/path/to/editor.v.1.2.3.min.js';
8
9
// Editor interface styles
10
const EDITOR_CSS_URL =
11
'https://ceditor.setka.io/path/to/editor.v.1.2.3.min.css';
12
13
// Company styles
14
const COMPANY_CSS_URL =
15
'https://ceditor.setka.io/path/to/company_name.v.1.2.3.min.css';
16
17
// Company metadata
18
const COMPANY_JSON_URL =
19
'https://ceditor.setka.io/path/to/company_name.v.1.2.3.json';
20
21
// 2. Set the container on the page,
22
// where the editor is being initialized:
23
const CONTAINER = '.setka-editor-container';
24
25
// 3. Set the company's public token (public_token)
26
// for queries to editor.setka.io API
27
// (it is located on the “CMS Integration” tab of the Style Manager)
28
const PUBLIC_TOKEN = 'my_public_token_123456';
29
30
// 4. Load editor files to the page:
31
const editorCSS = document.createElement('link');
32
editorCSS.rel = 'stylesheet';
33
editorCSS.href = EDITOR_CSS_URL;
34
document.head.appendChild(editorCSS);
35
36
const companyCSS = document.createElement('link');
37
companyCSS.rel = 'stylesheet';
38
companyCSS.href = COMPANY_CSS_URL;
39
document.head.appendChild(companyCSS);
40
41
const editorJS = document.createElement('script');
42
editorJS.async = true;
43
editorJS.src = EDITOR_JS_URL;
44
editorJS.onload = initSetkaEditor;
45
document.head.appendChild(editorJS);
46
47
async function initSetkaEditor() {
48
// 5. Load the JSON metadata of the company:
49
const res = await fetch(COMPANY_JSON_URL);
50
const json = await res.json();
51
52
// 6. Set the editor initialization parameters:
53
const config = {
54
...json.config,
55
container: CONTAINER,
56
public_token: PUBLIC_TOKEN,
57
};
58
59
const { assets } = json;
60
61
// 7. Launch the editor
62
SetkaEditor.start(config, assets);
63
}
Copied!
Only one instance of Setka Editor can be launched on a page at the moment.
To stop and remove the working editor instance from the page, please use this method:
1
SetkaEditor.stop()
Copied!

2. Add the company styles and scripts to the post view page

To view posts correctly, load these files to external pages of the site:
company_name.v.1.2.3.min.css — CSS styles of the post.
public.v.1.2.3.js — JS code for launching interactive elements (galleries, animations, footnotes. etc.)
Do not link the public.js file to the post editing page.

Getting post's JSON

1
SetkaEditor.getJSON()
Copied!
This method is available only with a JSONExport key.

Getting assets (images, layouts, symbols, themes)

1
SetkaEditor.getAssets()
Copied!

Getting a current post's style

1
SetkaEditor.getCurrentTheme()
Copied!

Getting a current post's grid

1
SetkaEditor.getCurrentLayout()
Copied!

3. Saving the HTML code to the database and loading it back to the editor

Setting a post's content

1
SetkaEditor.setHTML(HTML)
Copied!
where HTML stands for a required string.

How to get the content from the editor for saving it to the database

There is a specific method to get the post HTML code from the editor:
1
SetkaEditor.getHTML();
Copied!
Do not get the editor's content by queries to DOM elements on a page — they contain some extra elements and attributes, required only for working in the editor. They should not get into the saved post's HTML code. Always use the SetkaEditor.getHTML() method.

How to open the previously saved post in the editor

To open the existing post in the editor, please call the SetkaEditor.replaceHTML() method after the editor initialization:
1
async function initSetkaEditor() {
2
// ...
3
4
// Load the post content from the database
5
const postContent = await loadPostContentFromDB();
6
7
// ...
8
9
// Launch the editor
10
SetkaEditor.start(config, assets);
11
12
// Put the post content into the editor
13
SetkaEditor.replaceHTML(postContent);
14
}
Copied!
Do not put the post's HTML code on its' editing page. If <script> and <iframe> tags are present in the code, they will be executed by the browser. Also, animation styles may apply to elements inside the editor.
Setka Editor works with its' own HTML code structure. The post should be wrapped into the <div class=”stk-post”>...</div>container and contain elements with specific classes and attributes. Unfamiliar elements may come uneditable or cause the editor launch error. There is no sense in trying to open posts created in other editors, in Setka Editor.

4. Working with images

Setka Editor interface supports the following operations with images: loading, deleting and editing the alt attribute.
To make Setka Editor able to work with images from your server, please do the following:
1. Build the image operations API on your server or use one of the existing APIs. The implementation fully depends on your preferences.
2. Write JavaScript functions for API interactions (there are some examples below).
3. Specify these functions on editor initialization:
1
async function initSetkaEditor() {
2
// ...
3
config.onFileUpload = uploadFile;
4
config.onFileRemove = removeFile;
5
config.onFileUpdate = updateFile;
6
// ...
7
SetkaEditor.start(config, assets);
8
}
Copied!
The editor supports the srcset attribute for images. Thus, you can generate images of various sizes and handle them in the sizes key on editor start and on loading new images. As a result, images that are fit to a user's browser width will be loaded automatically.

Loading images

1
// Set the file loading callback function
2
// It receives the file object as an argument
3
// and should return a promise:
4
function uploadFile(file) {
5
return new Promise(async (resolve, reject) => {
6
try {
7
// Upload the file on the server/cloud/etc.
8
// any convenient way: fetch, axis, ...
9
const res = await uploadFileToServer(file);
10
if (res.status !== 200) {
11
reject({ message: res.statusText });
12
}
13
14
const json = await res.json();
15
16
// Resolve the promise with an object of required properties:
17
resolve({
18
// id of an image from the server
19
id: 'abc-def-123-456',
20
21
// image file name
22
name: 'image.jpg'
23
24
// full path to an image
25
url: 'https://example.com/image.jpg',
26
27
// full path to a downsized image (optional)
28
thumbUrl: 'https://example.com/image_thumb.jpg',
29
30
// alt-text of an image (optional)
31
alt: 'alternative text',
32
33
// various image sizes for the "srcset" attribute (optional)
34
sizes: [
35
{ width: 100, height: 100, url: 'image-small.jpg' },
36
{ width: 400, height: 400, url: 'image-medium.jpg' },
37
{ width: 1000, height: 1000, url: 'image-large.jpg' },
38
]
39
});
40
}
41
catch (ex) {
42
reject({ message: ex });
43
}
44
}
45
}
Copied!

Deleting images

1
// Set the file deletion callback function
2
// It receives the file id as an argument
3
// and should return a promise
4
function removeFile(id) {
5
return new Promise(async (resolve, reject) => {
6
try {
7
// Delete the file from the server
8
const res = await removeFileFromServer(id);
9
10
// Resolve the promise, if the file was deleted successfully
11
if (res.status === 200) {
12
resolve();
13
}
14
15
// Reject the promise if something went wrong
16
else {
17
reject({ message: res.statusText });
18
}
19
}
20
catch (ex) {
21
reject({ message: ex });
22
}
23
}
24
}
Copied!

Editing images

1
// Set the file editing callback function
2
// It receives the file id as an argument
3
// and a props object with updated file preferences,
4
// it should return a promise
5
function updateFile(id, props) {
6
return new Promise(async (resolve, reject) => {
7
try {
8
// Update the file on the server
9
const res = await updateFileOnServer(id, props);
10
11
// Resolve the promise on successful update
12
if (res.status === 200) {
13
resolve();
14
}
15
16
// Reject the promise if something went wrong
17
else {
18
reject({ message: res.statusText });
19
}
20
}
21
catch (ex) {
22
reject({ message: ex });
23
}
24
}
25
}
Copied!

Launching the editor with a set of images

To start the editor with the specific set of images in the right sidebar (e.g., uploaded with an earlier saved post), this set should be added into the assets object on start.
1
2
async function initSetkaEditor() {
3
// ...
4
5
// Get images from the database
6
7
// and form the array in the compatible format:
8
const postImages = [
9
{
10
// id of an image saved on the server
11
id: 'abc-def-123-456',
12
13
// image file name
14
name: 'image.jpg'
15
16
// full path to an image
17
url: 'https://example.com/image.jpg',
18
19
// full path to a downsized image (optional)
20
thumbUrl: 'https://example.com/image_thumb.jpg',
21
22
// alt-text of an image (optional)
23
alt: 'alternative text',
24
25
// various image sizes for the "srcset" attribute (optional)
26
sizes: [
27
{ width: 100, height: 100, url: 'image-small.jpg' },
28
{ width: 400, height: 400, url: 'image-medium.jpg' },
29
{ width: 1000, height: 1000, url: 'image-large.jpg' },
30
]
31
}
32
];
33
34
// Add images to editor resources
35
assets.images = postImages;
36
37
// Launch the editor
38
SetkaEditor.start(config, assets);
39
}
Copied!

Getting images from the panel

1
SetkaEditor.getImages()
Copied!

Integration with image storages

In Setka Editor, you can not only download images manually from your PC or via the Unsplash library but can also set integration with the image storage from your CMS. Such integration allows you to use images already downloaded to your storage in new Setka Editor posts. Once you accomplish this integration, a new option called Media Library (you can set any custom name for it) will be added to the right panel.
Screenshot of Setka Editor Interface: Side Toolbar with Images Button highlighted and "Upload" button and "Media Library" option lighlighted
Once you click on it, you will see a pop-up window with the pictures from your storage and a search field. You can choose a set of pictures that appears in the pop-up, e.g., choose the most frequently used images or a set of images for any search query (should be set on the side of your CMS), or even not show any images at all by default.
Once an image has been chosen, it is being uploaded to the editor's right panel, and you can work with it just like you do with any other image in Setka Editor, downloaded in any default way.
Here is an example of how this feature works with our WordPress storage:
Setka Editor Interface: Side Toolbar with "Images" button and "Upload" button and showing how to search Media Library in WordPress

Integration description

To run the integration with your media library, you need to send an object `customMediaLibrary` to the editor's configuration. This object should have 4 keys:
    1.
    1
    isSearchable: true | false
    Copied!
    responsible for a search field in the interface;
    2.
    1
    title: { en: String, ru: String }
    Copied!
    responsible for the integration's name in the interface;
    3.
    1
    onLoadImages: async function() {}
    Copied!
    this function is called during images download to the editor;
    4.
    1
    onFetchImages: async function() {}
    Copied!
    downloads images, inserted in a post.
An example of a full integration:
1
const getGiphy = id => loadXHR(`${GIPHY_BASE_URL}/${id}?api_key=${GIPHY_API_KEY}`);
2
customMediaLibrary: {
3
isSearchable: true,
4
title: { en: 'Giphy', ru: 'Гифи' },
5
onLoadImages: async ({ imagesLoaded, searchValue }) => {
6
const response = await searchGiphy(searchValue, 10, imagesLoaded);
7
const images = response.data.map(item => ({
8
id: item.id,
9
filename: item.title || 'Untitled',
10
url: item.images.original.url
11
}));
12
return images;
13
},
14
onFetchImages: async imagesIds => {
15
const responses = await Promise.all(imagesIds.map(id => getGiphy(id)));
16
const images = responses
17
.filter(Boolean)
18
.map(item => item.data)
19
.map(item => ({ id: item.id, filename: item.title, url: item.images.original.url }));
20
return images;
21
}
22
}
Copied!

Additional initialization parameters

Setting post's style

1
SetkaEditor.setTheme(id)
Copied!
where id stands for a required number.

Setting post's grid

1
SetkaEditor.setLayout(id)
Copied!
where id stands for a required number.

Setting indents for the Setka Editor header

If you already have an element that sticks on top of the page (e.g., top navigation bar), you can set an indent for the Setka Editor header to prevent overlay of one panel to another.
Indent size should be equal to the height of your top or bottom sticky panel, respectively:
1
// upper indent for the editor's top panel
2
config.headerTopOffset = 55;
3
4
// lower indent for the editor's bottom panel
5
config.footerBottomOffset = 20;
Copied!

Tracking changes in the editor

To subscribe to post content changes in the editor, use the onPostContentChange callback function:
1
config.onPostContentChange = () => {
2
console.log('Post content has changed');
3
}
Copied!

Getting editor's status

The method returns true/false depending on whether the editor is launched or not.
1
SetkaEditor.isRunning()
Copied!

Getting editor's state

1
SetkaEditor.getState()
Copied!

Getting editor's configuration

1
SetkaEditor.getConfig()
Copied!

Launching the editor inside a scrollable container

If you are launching the editor inside the scrollable DOM container, you will need to pass its' CSS selector in the scrollingElement parameter for the correct sticking of the top and bottom bars to the boundaries of this container:
1
config.$scrollingElement = '.scrolling-element';
Copied!
Default value — window.document.

Adding your own styles and scripts into the editor preview

To add your own styles or scripts to the editor preview mode (opens on pressing the TAB button), use previewCSS and previewJS keys, containing arrays with paths to required files of styles and scripts, respectively:
1
config.previewCSS = ['https://example.com/styles.css'];
2
config.previewJS = ['https://example.com/script.js'];
Copied!

Working with the editor functions if requests to external CDNs are not possible

Even if you save style and editor files on your platform when working with the Style Manager API, some internal functions of the editor (Enhance Symbols, Unsplash and WordPress Media Library integrations, image editor) are always loaded from the Setka Editor CDN. If requesting files from external resources is prohibited on your platform, save those files on your server and connect them to the editor's initialization.
Please note that the files should be hosted without a redirect. Editor internal functionality files can be received by contacting our support team on [email protected] or via chat.
To get these files on editor initialization, use the featureRootURL key. You will need to pass the path to the folder with the files on your server via this key:
1
config.featureRootURL = 'https://www.example.com/assets/';
Copied!
The editor will be requesting such files afterward:
1
https://www.example.com/assets/typograph/0.1.0/i_typograph.js
2
https://www.example.com/assets/unsplash/0.1.0/i_unsplash.js
Copied!

More about custom integration:

Last modified 7mo ago