| import type { LayoutLocator } from "./types"; |
|
|
| |
| export const findItemInLayout = (item: string, layout: string[][][]): LayoutLocator | null => { |
| for (const [page, element] of layout.entries()) { |
| for (const [column, element_] of element.entries()) { |
| for (const [section, element__] of element_.entries()) { |
| if (element__ === item) { |
| return { page, column, section }; |
| } |
| } |
| } |
| } |
|
|
| return null; |
| }; |
|
|
| |
| export const removeItemInLayout = (item: string, layout: string[][][]): LayoutLocator | null => { |
| const locator = findItemInLayout(item, layout); |
|
|
| if (locator) { |
| layout[locator.page][locator.column].splice(locator.section, 1); |
| } |
|
|
| return locator; |
| }; |
|
|
| |
| export const moveItemInLayout = ( |
| current: LayoutLocator, |
| target: LayoutLocator, |
| layout: string[][][], |
| ): string[][][] => { |
| try { |
| |
| const newLayout = JSON.parse(JSON.stringify(layout)); |
|
|
| |
| const item = newLayout[current.page][current.column][current.section]; |
|
|
| |
| newLayout[current.page][current.column].splice(current.section, 1); |
|
|
| |
| newLayout[target.page][target.column].splice(target.section, 0, item); |
|
|
| return newLayout; |
| } catch { |
| return layout; |
| } |
| }; |
|
|