From 96365de456ecdd35145a6bbaad420ce770df4f1a Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sun, 10 Sep 2023 11:32:30 +0400 Subject: [PATCH] Update block events --- src/block_manager/index.ts | 56 +++-------------------- src/block_manager/types.ts | 66 ++++++++++++++++++++++++++++ src/block_manager/view/BlocksView.ts | 1 - 3 files changed, 71 insertions(+), 52 deletions(-) create mode 100644 src/block_manager/types.ts diff --git a/src/block_manager/index.ts b/src/block_manager/index.ts index 0496874c6..f43cff21d 100644 --- a/src/block_manager/index.ts +++ b/src/block_manager/index.ts @@ -19,25 +19,7 @@ * blockManager.add(...); * ``` * - * ## Available Events - * * `block:add` - Block added. The [Block] is passed as an argument to the callback. - * * `block:remove` - Block removed. The [Block] is passed as an argument to the callback. - * * `block:update` - Block updated. The [Block] and the object containing changes are passed as arguments to the callback. - * * `block:drag:start` - Started dragging block, the [Block] is passed as an argument. - * * `block:drag` - Dragging block, the [Block] is passed as an argument. - * * `block:drag:stop` - Dragging of the block is stopped. The dropped [Component] (if dropped successfully) and the [Block] are passed as arguments. - * * `block` - Catch-all event for all the events mentioned above. An object containing all the available data about the triggered event is passed as an argument to the callback. - * - * ## Methods - * * [add](#add) - * * [get](#get) - * * [getAll](#getall) - * * [getAllVisible](#getallvisible) - * * [remove](#remove) - * * [getConfig](#getconfig) - * * [getCategories](#getcategories) - * * [getContainer](#getcontainer) - * * [render](#render) + * {REPLACE_EVENTS} * * [Block]: block.html * [Component]: component.html @@ -54,38 +36,10 @@ import Block, { BlockProperties } from './model/Block'; import Blocks from './model/Blocks'; import Categories from './model/Categories'; import Category from './model/Category'; +import { BlocksEvents } from './types'; import BlocksView from './view/BlocksView'; -export type BlockEvent = - | 'block:add' - | 'block:remove' - | 'block:drag:start' - | 'block:drag' - | 'block:drag:stop' - | 'block:custom'; - -export const evAll = 'block'; -export const evPfx = `${evAll}:`; -export const evAdd = `${evPfx}add`; -export const evUpdate = `${evPfx}update`; -export const evRemove = `${evPfx}remove`; -export const evRemoveBefore = `${evRemove}:before`; -export const evDrag = `${evPfx}drag`; -export const evDragStart = `${evDrag}:start`; -export const evDragStop = `${evDrag}:stop`; -export const evCustom = `${evPfx}custom`; - -const blockEvents = { - all: evAll, - update: evUpdate, - add: evAdd, - remove: evRemove, - removeBefore: evRemoveBefore, - drag: evDrag, - dragStart: evDragStart, - dragEnd: evDragStop, - custom: evCustom, -}; +export type BlockEvent = `${BlocksEvents}`; export default class BlockManager extends ItemManagerModule { blocks: Blocks; @@ -94,7 +48,7 @@ export default class BlockManager extends ItemManagerModule; - events!: typeof blockEvents; + events = BlocksEvents; Block = Block; @@ -107,7 +61,7 @@ export default class BlockManager extends ItemManagerModule { ... }); + */ + add = 'block:add', + + /** + * @event `block:remove` Block removed from the collection. The [Block] is passed as an argument to the callback. + * @example + * editor.on('block:remove', (block) => { ... }); + */ + remove = 'block:remove', + + /** + * @event `block:remove:before` Event triggered before Block remove. + * @example + * editor.on('block:remove:before', (block, remove, opts) => { ... }); + */ + removeBefore = 'block:remove:before', + + /** + * @event `block:update` Block updated. The [Block] and the object containing changes are passed as arguments to the callback. + * @example + * editor.on('block:update', (block, updatedProps) => { ... }); + */ + update = 'block:update', + + /** + * @event `block:drag:start` Started dragging block. The [Block] is passed as an argument. + * @example + * editor.on('block:drag:start', (block) => { ... }); + */ + dragStart = 'block:drag:start', + + /** + * @event `block:drag` The block is dragging. The [Block] is passed as an argument. + * @example + * editor.on('block:drag', (block) => { ... }); + */ + drag = 'block:drag', + + /** + * @event `block:drag:stop` Dragging of the block is stopped. The dropped [Component] (if dropped successfully) and the [Block] are passed as arguments. + * @example + * editor.on('block:drag:stop', (component, block) => { ... }); + */ + dragEnd = 'block:drag:stop', + + /** + * @event `block:custom` Event to use in case of [custom Block Manager UI](https://grapesjs.com/docs/modules/Blocks.html#customization). + * @example + * editor.on('block:custom', ({ container, blocks, ... }) => { ... }); + */ + custom = 'block:custom', + + /** + * @event `block` Catch-all event for all the events mentioned above. An object containing all the available data about the triggered event is passed as an argument to the callback. + * @example + * editor.on('block', ({ event, model, ... }) => { ... }); + */ + all = 'block', +} +/**{END_EVENTS}*/ diff --git a/src/block_manager/view/BlocksView.ts b/src/block_manager/view/BlocksView.ts index 8b72ee589..d70bd85c6 100644 --- a/src/block_manager/view/BlocksView.ts +++ b/src/block_manager/view/BlocksView.ts @@ -102,7 +102,6 @@ export default class BlocksView extends View { onMove(ev: Event) { this.__getModule().__drag(ev); - this.em.trigger('block:drag:move', ev); // old event } onDrop(component?: Component) {