Browse Source

Cleanup BlocksView

pull/5642/head
Artur Arseniev 3 years ago
parent
commit
250f638425
  1. 5
      src/block_manager/model/Block.ts
  2. 26
      src/block_manager/view/BlocksView.ts

5
src/block_manager/model/Block.ts

@ -3,6 +3,7 @@ import { isFunction } from 'underscore';
import Editor from '../../editor';
import Category, { CategoryProperties } from '../../abstract/ModuleCategory';
import { ComponentDefinition } from '../../dom_components/model/types';
import Blocks from './Blocks';
/** @private */
export interface BlockProperties {
@ -98,6 +99,10 @@ export default class Block extends Model<BlockProperties> {
return cat instanceof Category ? cat : undefined;
}
get parent() {
return this.collection as unknown as Blocks;
}
/**
* Get block id
* @returns {String}

26
src/block_manager/view/BlocksView.ts

@ -126,38 +126,24 @@ export default class BlocksView extends View {
* */
add(model: Block, fragment?: DocumentFragment) {
const { config, renderedCategories } = this;
const view = new BlockView(
{
model,
attributes: model.get('attributes'),
},
config
);
const attributes = model.get('attributes');
const view = new BlockView({ model, attributes }, config);
const rendered = view.render().el;
let category = model.get('category');
const category = model.parent.initCategory(model);
// Check for categories
if (category && this.categories && !config.ignoreCategories) {
if (isString(category)) {
category = { id: category, label: category };
} else if (isObject(category) && !category.id) {
category.id = category.label;
}
const catModel = this.categories.add(category);
const catId = catModel.get('id')!;
const catId = category.getId();
const categories = this.getCategoriesEl();
let catView = renderedCategories.get(catId);
// @ts-ignore
model.set('category', catModel, { silent: true });
if (!catView && categories) {
catView = new CategoryView({ model: catModel }, config, 'block').render();
catView = new CategoryView({ model: category }, config, 'block').render();
renderedCategories.set(catId, catView);
categories.appendChild(catView.el);
}
catView && catView.append(rendered);
catView?.append(rendered);
return;
}

Loading…
Cancel
Save