# Pages
You can customize the initial state of the module from the editor initialization
const editor = grapesjs.init({
....
pageManager: {
pages: [
{
id: 'page-id',
styles: `.my-class { color: red }`, // or a JSON of styles
component: '<div class="my-class">My element</div>', // or a JSON of components
}
]
},
})
Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance
const pageManager = editor.Pages;
# Available Events
page:addAdded new page. The page is passed as an argument to the callback.
editor.on('page:add', (page) => { ... });
page:removePage removed. The page is passed as an argument to the callback.
editor.on('page:remove', (page) => { ... });
page:selectNew page selected. The newly selected page and the previous one, are passed as arguments to the callback.
editor.on('page:select', (page, previousPage) => { ... });
page:updatePage updated. The updated page and the object containing changes are passed as arguments to the callback.
editor.on('page:update', (page, changes) => { ... });
page:frame:addFrame added to a page. The frame and options are passed as arguments to the callback.
editor.on('page:frame:add', (frame, options) => { ... });
page:frame:removeFrame removed from a page. The frame and options are passed as arguments to the callback.
editor.on('page:frame:remove', (frame, options) => { ... });
page:frame:updateFrame updated. The frame, changed properties, and options are passed as arguments to the callback.
editor.on('page:frame:update', (frame, changes, options) => { ... });
# page Catch-all event for all the events mentioned above. An object containing all the available data about the triggered event is passed as an argument to the callback.
Frame events use their page:frame:\* names and include the frame and, for updates, the changed properties.
editor.on('page', ({ event, page, frame, changes, options }) => { ... });
# getAll
Get all pages
# Examples
const arrayOfPages = pageManager.getAll();
Returns Array (opens new window)<Page>
# add
Add new page
# Parameters
propsObject (opens new window) Page propertiesoptsObject (opens new window)? Options (optional, default{})
# Examples
const newPage = pageManager.add({
id: 'new-page-id', // without an explicit ID, a random one will be created
styles: `.my-class { color: red }`, // or a JSON of styles
component: '<div class="my-class">My element</div>', // or a JSON of components
});
Returns Page
# remove
Remove page
# Parameters
page(String (opens new window) | Page) Page or page idoptsany (optional, default{})
# Examples
const removedPage = pageManager.remove('page-id');
// or by passing the page
const somePage = pageManager.get('page-id');
pageManager.remove(somePage);
Returns Page Removed Page
# move
Move a page to a specific index in the pages collection. If the index is out of bounds, the page will not be moved.
# Parameters
page(string (opens new window) | Page) Page or page id to move.optsObject (opens new window)? Move options (optional, default{})opts.atnumber (opens new window)? The target index where the page should be moved.
# Examples
// Move a page to index 2
const movedPage = pageManager.move('page-id', { at: 2 });
if (movedPage) {
console.log('Page moved successfully:', movedPage);
} else {
console.log('Page could not be moved.');
}
Returns (Page | undefined (opens new window)) The moved page, or undefined if the page does not exist or the index is out of bounds.
# get
Get page by id
# Parameters
idString (opens new window) Page id
# Examples
const somePage = pageManager.get('page-id');
Returns Page
# getMain
Get main page (the first one available)
# Examples
const mainPage = pageManager.getMain();
Returns Page
# getAllWrappers
Get wrapper components (aka body) from all pages and frames.
# Examples
const wrappers = pageManager.getAllWrappers();
// Get all `image` components from the project
const allImages = wrappers.map(wrp => wrp.findType('image')).flat();
Returns Array (opens new window)<Component>
# select
Change the selected page. This will switch the page rendered in canvas
# Parameters
page(String (opens new window) | Page) Page or page idoptsSetOptions (optional, default{})
# Examples
pageManager.select('page-id');
// or by passing the page
const somePage = pageManager.get('page-id');
pageManager.select(somePage);
Returns this
# getSelected
Get the selected page
# Examples
const selectedPage = pageManager.getSelected();
Returns Page
← Panels Page →