2019-11-30 21:42:39 +01:00
|
|
|
import * as _ from './utils';
|
2018-11-23 17:11:50 +01:00
|
|
|
import $ from './dom';
|
2019-11-30 21:42:39 +01:00
|
|
|
import Block, {BlockToolAPI} from './block';
|
2018-11-23 17:11:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Blocks
|
|
|
|
* @classdesc Class to work with Block instances array
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* @property {HTMLElement} workingArea — editor`s working node
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export default class Blocks {
|
|
|
|
/**
|
|
|
|
* Get length of Block instances array
|
|
|
|
*
|
|
|
|
* @returns {Number}
|
|
|
|
*/
|
|
|
|
public get length(): number {
|
|
|
|
return this.blocks.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Block instances array
|
|
|
|
*
|
|
|
|
* @returns {Block[]}
|
|
|
|
*/
|
|
|
|
public get array(): Block[] {
|
|
|
|
return this.blocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get blocks html elements array
|
|
|
|
*
|
|
|
|
* @returns {HTMLElement[]}
|
|
|
|
*/
|
|
|
|
public get nodes(): HTMLElement[] {
|
|
|
|
return _.array(this.workingArea.children);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy trap to implement array-like setter
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* blocks[0] = new Block(...)
|
|
|
|
*
|
|
|
|
* @param {Blocks} instance — Blocks instance
|
2019-11-30 21:42:39 +01:00
|
|
|
* @param {Number|String} property — block index or any Blocks class property to set
|
|
|
|
* @param {Block} value — value to set
|
2018-11-23 17:11:50 +01:00
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
2019-11-30 21:42:39 +01:00
|
|
|
public static set(instance: Blocks, property: number | string, value: Block | any) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If property name is not a number (method or other property, access it via reflect
|
|
|
|
*/
|
|
|
|
if (isNaN(Number(property))) {
|
|
|
|
Reflect.set(instance, property, value);
|
|
|
|
return true;
|
2018-11-23 17:11:50 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 21:42:39 +01:00
|
|
|
/**
|
|
|
|
* If property is number, call insert method to emulate array behaviour
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* blocks[0] = new Block();
|
|
|
|
*/
|
|
|
|
instance.insert(+property, value);
|
2018-11-23 17:11:50 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy trap to implement array-like getter
|
|
|
|
*
|
|
|
|
* @param {Blocks} instance — Blocks instance
|
2019-11-30 21:42:39 +01:00
|
|
|
* @param {Number|String} property — Blocks class property
|
2018-11-23 17:11:50 +01:00
|
|
|
* @returns {Block|*}
|
|
|
|
*/
|
2019-11-30 21:42:39 +01:00
|
|
|
public static get(instance: Blocks, property: any | number) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If property is not a number, get it via Reflect object
|
|
|
|
*/
|
|
|
|
if (isNaN(Number(property))) {
|
|
|
|
return Reflect.get(instance, property);
|
2018-11-23 17:11:50 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 21:42:39 +01:00
|
|
|
/**
|
|
|
|
* If property is a number (Block index) return Block by passed index
|
|
|
|
*/
|
|
|
|
return instance.get(+property);
|
2018-11-23 17:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of Block instances in order of addition
|
|
|
|
*/
|
|
|
|
public blocks: Block[];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Editor`s area where to add Block`s HTML
|
|
|
|
*/
|
|
|
|
public workingArea: HTMLElement;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} workingArea — editor`s working node
|
|
|
|
*/
|
|
|
|
constructor(workingArea: HTMLElement) {
|
|
|
|
this.blocks = [];
|
|
|
|
this.workingArea = workingArea;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push new Block to the blocks array and append it to working area
|
|
|
|
*
|
|
|
|
* @param {Block} block
|
|
|
|
*/
|
|
|
|
public push(block: Block): void {
|
|
|
|
this.blocks.push(block);
|
2019-11-30 21:42:39 +01:00
|
|
|
this.insertToDOM(block);
|
2018-11-23 17:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Swaps blocks with indexes first and second
|
|
|
|
* @param {Number} first - first block index
|
|
|
|
* @param {Number} second - second block index
|
|
|
|
*/
|
|
|
|
public swap(first: number, second: number): void {
|
|
|
|
const secondBlock = this.blocks[second];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change in DOM
|
|
|
|
*/
|
|
|
|
$.swap(this.blocks[first].holder, secondBlock.holder);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change in array
|
|
|
|
*/
|
|
|
|
this.blocks[second] = this.blocks[first];
|
|
|
|
this.blocks[first] = secondBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert new Block at passed index
|
|
|
|
*
|
|
|
|
* @param {Number} index — index to insert Block
|
|
|
|
* @param {Block} block — Block to insert
|
|
|
|
* @param {Boolean} replace — it true, replace block on given index
|
|
|
|
*/
|
|
|
|
public insert(index: number, block: Block, replace: boolean = false): void {
|
|
|
|
if (!this.length) {
|
|
|
|
this.push(block);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index > this.length) {
|
|
|
|
index = this.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (replace) {
|
|
|
|
this.blocks[index].holder.remove();
|
2019-11-30 21:42:39 +01:00
|
|
|
this.blocks[index].call(BlockToolAPI.REMOVED);
|
2018-11-23 17:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const deleteCount = replace ? 1 : 0;
|
|
|
|
|
|
|
|
this.blocks.splice(index, deleteCount, block);
|
|
|
|
|
|
|
|
if (index > 0) {
|
|
|
|
const previousBlock = this.blocks[index - 1];
|
|
|
|
|
2019-11-30 21:42:39 +01:00
|
|
|
this.insertToDOM(block, 'afterend', previousBlock);
|
2018-11-23 17:11:50 +01:00
|
|
|
} else {
|
|
|
|
const nextBlock = this.blocks[index + 1];
|
|
|
|
|
|
|
|
if (nextBlock) {
|
2019-11-30 21:42:39 +01:00
|
|
|
this.insertToDOM(block, 'beforebegin', nextBlock);
|
2018-11-23 17:11:50 +01:00
|
|
|
} else {
|
2019-11-30 21:42:39 +01:00
|
|
|
this.insertToDOM(block);
|
2018-11-23 17:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove block
|
|
|
|
* @param {Number|null} index
|
|
|
|
*/
|
|
|
|
public remove(index: number): void {
|
|
|
|
if (isNaN(index)) {
|
|
|
|
index = this.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.blocks[index].holder.remove();
|
2019-11-30 21:42:39 +01:00
|
|
|
|
|
|
|
this.blocks[index].call(BlockToolAPI.REMOVED);
|
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
this.blocks.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all blocks
|
|
|
|
*/
|
|
|
|
public removeAll(): void {
|
|
|
|
this.workingArea.innerHTML = '';
|
2019-11-30 21:42:39 +01:00
|
|
|
|
|
|
|
this.blocks.forEach((block) => block.call(BlockToolAPI.REMOVED));
|
|
|
|
|
2018-11-23 17:11:50 +01:00
|
|
|
this.blocks.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert Block after passed target
|
|
|
|
*
|
|
|
|
* @todo decide if this method is necessary
|
|
|
|
*
|
|
|
|
* @param {Block} targetBlock — target after wich Block should be inserted
|
|
|
|
* @param {Block} newBlock — Block to insert
|
|
|
|
*/
|
|
|
|
public insertAfter(targetBlock: Block, newBlock: Block): void {
|
|
|
|
const index = this.blocks.indexOf(targetBlock);
|
|
|
|
|
|
|
|
this.insert(index + 1, newBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Block by index
|
|
|
|
*
|
|
|
|
* @param {Number} index — Block index
|
|
|
|
* @returns {Block}
|
|
|
|
*/
|
|
|
|
public get(index: number): Block {
|
|
|
|
return this.blocks[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return index of passed Block
|
|
|
|
*
|
|
|
|
* @param {Block} block
|
|
|
|
* @returns {Number}
|
|
|
|
*/
|
|
|
|
public indexOf(block: Block): number {
|
|
|
|
return this.blocks.indexOf(block);
|
|
|
|
}
|
2019-11-30 21:42:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert new Block into DOM
|
|
|
|
*
|
|
|
|
* @param {Block} block - Block to insert
|
|
|
|
* @param {InsertPosition} position — insert position (if set, will use insertAdjacentElement)
|
|
|
|
* @param {Block} target — Block related to position
|
|
|
|
*/
|
|
|
|
private insertToDOM(block: Block, position?: InsertPosition, target?: Block): void {
|
|
|
|
if (position) {
|
|
|
|
target.holder.insertAdjacentElement(position, block.holder);
|
|
|
|
} else {
|
|
|
|
this.workingArea.appendChild(block.holder);
|
|
|
|
}
|
|
|
|
|
|
|
|
block.call(BlockToolAPI.RENDERED);
|
|
|
|
}
|
2018-11-23 17:11:50 +01:00
|
|
|
}
|