The Visual Editor API is designed for experienced CloudCannon developers. If you need help with custom integrations or your CloudCannon setup in general, please contact our friendly support team.
CloudCannon's Visual Editor API CloudCannon's Visual Editor API is a public JavaScript API for building custom integrations with the Visual Editor. It lets you run your own code inside the Visual Editor to read and write content, listen for changes, and upload files using code running inside your website files. CloudCannon's visual editing interface provides a user-friendly way of updating your website files on an interactive preview of your webpage. You can navigate around your website preview using links/buttons, as you would on the live version, and edit your content inline on the page, or with the data panel or sidebar. What you see in the Visual Editor is what your website visitors will see on your live webpages.Visual Editor API
*.createTextEditableRegion(), which makes any supported HTML element directly editable in the Visual EditorVisual Editor
This article assumes you know how to initialize the Visual Editor API. For creating a floating Data Panel A Data Panel is a panel of Inputs for editing a focused scope of structured data, such as image details, link attributes, or Site configuration. Data Panels appear in several places across CloudCannon — in the Visual Editor, in the Content Editor when you edit a snippet, image, or link, and in Configuration Mode, such as the Edit Advanced panel. Developers can also build custom Data Panels with full Input support using the Visual Editor API.Data Panel
Custom inline editing#
Custom inline editing lets Team Members A person who belongs to the same Organization as you. Team members are other CloudCannon users who have been invited to your Organization to collaborate on Sites, edit content, or manage settings. Each team member has their own CloudCannon account with individual permissions assigned through Permission Groups.Team Member
Regions created with *.createTextEditableRegion() use the same underlying mechanism as CloudCannon's data-editable attribute, but give you programmatic control. For example, you can register elements dynamically, pass a custom callback, or configure the toolbar options at runtime.
For more information about Text Editable Regions, please read our documentation on Text Editable Regions.
Create a text editable region
Calling *.createTextEditableRegion() allows you to make a supported HTML element inline-editable. This function takes three arguments: your HTML element, a callback which fires whenever the content changes and receives the updated content as a string, and an optional configuration object.
const file = api.currentFile();
const element = document.querySelector('#hero-heading');
const region = await api.createTextEditableRegion(
element,
(content) => {
file.data.set({ slug: 'heading', value: content });
}
);Access the file whose content you want to update. Here, that's the file open in the Visual Editor.
Select the HTML element you want to make inline-editable.
Call api.createTextEditableRegion() to register the element as an editable region. It returns a region object you can use to update the content programmatically.
Pass a callback as the second argument. It fires whenever the content changes and receives the updated content as a string. The heading maps to a front matter field, so this example uses file.data.set() to write the value to the heading field.
The region only makes the element editable in the preview. It doesn't write your changes anywhere on its own: not to a field, and not back to the element. That's up to you in the callback. Use *.content.set() for the file body, or *.data.set() for a structured data field. Like any edit, these update the Visual Editor data model and mark the file as having unsaved changes. The Team Member still saves the file as usual.
Configure a text editable region
Passing an options object as the third argument to *.createTextEditableRegion() customizes how CloudCannon treats the region.
elementType tells CloudCannon which family of HTML tags you are editing (text, block, span, image, or link). That choice affects toolbar behavior and validation. If you omit it, CloudCannon guesses from the tag name (for example, h2 as text, div as block).
editableType accepts the same values as elementType (text, block, span, image, or link), or 'content'. As a rule of thumb, set editableType to the same value as elementType, unless you are editing body content, in which case set it to 'content'.
extension tells CloudCannon which format to serialize the edited content to before passing it to your callback, based on a file extension such as '.html' or '.md'. When the content changes, CloudCannon generates a string in that format. Match it to the format of the underlying file the content belongs to, so the serialized string matches that file. You can leave it out for plain content where the format doesn't matter.
You can also pass inputConfig to control which rich text toolbar options are available.
The following shows common HTML elements and their typical elementType. This is not a strict mapping. For example, a p element could be configured as span if you only want to allow inline content:
text:p,h1–h6,blockquote,li,dd,dtblock:div,section,article,aside,main,footer,header,navspan:span,small,strong,em,i,b,sub,sup,td,th,citeimage:imglink:a
const region = await api.createTextEditableRegion(
element,
(content) => { /* handle update */ },
{
elementType: 'text',
editableType: 'text',
extension: '.html'
}
);Pass the same element and callback as before.
Pass an options object as the third argument to customize the editable region. All options are optional.
elementType sets the element type, which controls the editing behavior. If omitted, CloudCannon infers it from the element's tag name.
editableType controls the editing mode. Set it to the same value as elementType, or 'content' when editing body content.
extension sets the file extension used when saving the content.
The following lists all options accepted by *.createTextEditableRegion():
The family of HTML element being edited: text, block, span,
image, or link. Inferred from the tag name if omitted (for
example, h2 as text, div as block).
Available on: API Object createTextEditableRegion.
The editing mode. Use the same value as elementType, or content
when editing body content.
Available on: API Object createTextEditableRegion.
The file extension used when saving the edited fragment (for example,
.html or .md). Match it to the format of the underlying file.
Available on: API Object createTextEditableRegion.
Controls which rich text toolbar options are available.
Available on: API Object createTextEditableRegion.
Update an inline editable region
Calling *.setContent() on the region object returned by *.createTextEditableRegion() allows you to update the content of the editable region programmatically.
region.setContent('<p>New content</p>'); Pass a string to region.setContent() to replace the content of the editable region.