|
|
@ -161,33 +161,37 @@ export default class PageManager extends ItemManagerModule<PageManagerConfig, Pa |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Move a page to a specific index in the pages collection. |
|
|
* 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. |
|
|
* If the index is out of bounds, the page will not be moved. |
|
|
* |
|
|
* |
|
|
* @param {string} pageId - The ID of the page to move. |
|
|
* @param {string|[Page]} page Page or page id to move. |
|
|
* @param {number} index - The target index where the page should be moved. |
|
|
* @param {Object} [opts] Move options |
|
|
* @returns {Page | undefined} - The moved page, or `undefined` if the page does not exist or the index is out of bounds. |
|
|
* @param {number} [opts.at] 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 |
|
|
* @example |
|
|
* // Move a page to index 2
|
|
|
* // Move a page to index 2
|
|
|
* const movedPage = pageManager.move('page-id', 2); |
|
|
* const movedPage = pageManager.move('page-id', { at: 2 }); |
|
|
* if (movedPage) { |
|
|
* if (movedPage) { |
|
|
* console.log('Page moved successfully:', movedPage); |
|
|
* console.log('Page moved successfully:', movedPage); |
|
|
* } else { |
|
|
* } else { |
|
|
* console.log('Page could not be moved.'); |
|
|
* console.log('Page could not be moved.'); |
|
|
* } |
|
|
* } |
|
|
*/ |
|
|
*/ |
|
|
move(pageId: string, index: number): Page | undefined { |
|
|
move(page: string | Page, opts: AddOptions = {}): Page | undefined { |
|
|
const page = this.get(pageId); |
|
|
const { pages } = this; |
|
|
|
|
|
const pg = isString(page) ? this.get(page) : page; |
|
|
|
|
|
const { at = 0, ...resOpts } = opts; |
|
|
|
|
|
|
|
|
if (!page) return; |
|
|
if (!pg) return; |
|
|
|
|
|
|
|
|
if (index < 0 || index >= this.pages.length) { |
|
|
const currIndex = pages.indexOf(pg); |
|
|
this.em.logError('Index out of bounds'); |
|
|
const sameIndex = currIndex === at; |
|
|
} |
|
|
|
|
|
|
|
|
if (at < 0 || at >= pages.length || sameIndex) return; |
|
|
|
|
|
|
|
|
this.remove(page, { temporary: true }); |
|
|
this.remove(pg, { ...resOpts, temporary: true }); |
|
|
this.pages.add(page, { at: index }); |
|
|
pages.add(pg, { ...resOpts, at }); |
|
|
|
|
|
|
|
|
return page; |
|
|
return pg; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|