Browse Source

Merge branch 'dev' of https://github.com/artf/grapesjs into update-canvas-frame

update-canvas-frame
Artur Arseniev 12 months ago
parent
commit
7338884b87
  1. 2
      packages/core/src/common/index.ts
  2. 31
      packages/core/src/pages/index.ts
  3. 2
      packages/core/src/pages/model/Pages.ts

2
packages/core/src/common/index.ts

@ -24,7 +24,7 @@ export type UndoOptions = { fromUndo?: boolean };
export type WithHTMLParserOptions = { parserOptions?: HTMLParserOptions };
export type RemoveOptions = Backbone.Silenceable & UndoOptions & { dangerously?: boolean };
export type RemoveOptions = Backbone.Silenceable & UndoOptions & { dangerously?: boolean; temporary?: boolean };
export type EventHandler = Backbone.EventHandler;

31
packages/core/src/pages/index.ts

@ -159,6 +159,37 @@ export default class PageManager extends ItemManagerModule<PageManagerConfig, Pa
return !opts.abort && rm();
}
/**
* Move a page to a specific index in the pages collection.
* If the index is out of bounds, an error will be logged and the page will not be moved.
*
* @param {string} pageId - The ID of the page to move.
* @param {number} index - The target index where the page should be moved.
* @returns {Page | undefined} - The moved page, or `undefined` if the page does not exist or the index is out of bounds.
* @example
* // Move a page to index 2
* const movedPage = pageManager.move('page-id', 2);
* if (movedPage) {
* console.log('Page moved successfully:', movedPage);
* } else {
* console.log('Page could not be moved.');
* }
*/
move(pageId: string, index: number): Page | undefined {
const page = this.get(pageId);
if (!page) return;
if (index < 0 || index >= this.pages.length) {
this.em.logError('Index out of bounds');
}
this.remove(page, { temporary: true });
this.pages.add(page, { at: index });
return page;
}
/**
* Get page by id
* @param {String} id Page id

2
packages/core/src/pages/model/Pages.ts

@ -20,7 +20,7 @@ export default class Pages extends Collection<Page> {
onRemove(removed?: Page, _p?: this, opts: RemoveOptions = {}) {
// Avoid removing frames if triggered from undo #6142
if (opts.fromUndo) return;
if (opts.fromUndo || opts.temporary) return;
removed?.onRemove();
}
}

Loading…
Cancel
Save