/* * This file is part of ORY Editor. * * ORY Editor is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ORY Editor is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with ORY Editor. If not, see . * * @license LGPL-3.0 * @copyright 2016-2018 Aeneas Rekkas * @author Aeneas Rekkas * */ import { Cell, NewIds } from '../../types/editable'; import { Action } from 'redux'; import { generateIds } from '../helpers'; import { editMode } from '../display'; import { focusCell } from './core'; export const CELL_INSERT_ABOVE = 'CELL_INSERT_ABOVE'; export const CELL_INSERT_BELOW = 'CELL_INSERT_BELOW'; export const CELL_INSERT_LEFT_OF = 'CELL_INSERT_LEFT_OF'; export const CELL_INSERT_RIGHT_OF = 'CELL_INSERT_RIGHT_OF'; export const CELL_INSERT_INLINE_LEFT = 'CELL_INSERT_INLINE_LEFT'; export const CELL_INSERT_INLINE_RIGHT = 'CELL_INSERT_INLINE_RIGHT'; export interface InsertAction extends Action { ts: Date; item: Partial; hover: string; level: number; ids: NewIds; } const insert = (type: string) => ( item: Partial, { id: hover, inline, hasInlineNeighbour }: Partial, level: number = 0, ids: NewIds = null ) => { let l = level; switch (type) { case CELL_INSERT_ABOVE: case CELL_INSERT_BELOW: { if ((inline || hasInlineNeighbour) && level < 1) { l = 1; } break; } case CELL_INSERT_LEFT_OF: case CELL_INSERT_RIGHT_OF: { if ((inline || hasInlineNeighbour) && level < 1) { l = 1; } break; } default: } const insertAction = { type, ts: new Date(), item, hover, level: l, // FIXME: item handling is a bit confusing, // we now give some of them a name like "cell" or "item", // but the purpose of the others is unclear ids: ids ? ids : generateIds(), }; return dispatch => { dispatch(insertAction); // FIXME: checking if an item is new or just moved around is a bit awkward const isNew = !item.id; if (isNew) { dispatch(editMode()); setTimeout(() => { dispatch(focusCell(insertAction.ids.item)()); }, 300); } }; }; /** * Insert a cell below of the hovering cell. */ export const insertCellBelow = insert(CELL_INSERT_BELOW); /** * Insert a cell above of the hovering cell. */ export const insertCellAbove = insert(CELL_INSERT_ABOVE); /** * Insert a cell right of the hovering cell. */ export const insertCellRightOf = insert(CELL_INSERT_RIGHT_OF); /** * Insert a cell left of the hovering cell. */ export const insertCellLeftOf = insert(CELL_INSERT_LEFT_OF); /** * Insert a cell inside the hovering cell, on the left. */ export const insertCellLeftInline = insert(CELL_INSERT_INLINE_LEFT); /** * Insert a cell inside the hovering cell, on the right. */ export const insertCellRightInline = insert(CELL_INSERT_INLINE_RIGHT); export const insertActions = { insertCellRightInline, insertCellLeftInline, insertCellLeftOf, insertCellRightOf, insertCellAbove, insertCellBelow, insert, };