Skip to content

Commit

Permalink
Fix possible double entry in Layers. Closes #5820
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed May 24, 2024
1 parent d7f4e85 commit 1085831
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
2 changes: 2 additions & 0 deletions src/dom_components/model/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import Trait from '../../trait_manager/model/Trait';
import { ToolbarButtonProps } from './ToolbarButton';
import { TraitProperties } from '../../trait_manager/types';
import { ActionLabelComponents, ComponentsEvents } from '../types';
import ItemView from '../../navigator/view/ItemView';

export interface IComponent extends ExtractMethods<Component> {}

Expand Down Expand Up @@ -222,6 +223,7 @@ export default class Component extends StyleableModel<ComponentProperties> {
ccid!: string;
views!: ComponentView[];
view?: ComponentView;
viewLayer?: ItemView;
frame?: Frame;
rule?: CssRule;
prevColl?: Components;
Expand Down
8 changes: 8 additions & 0 deletions src/dom_components/model/Components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ Component> {
}

model.__postAdd({ recursive: true });

if (em && !opts.temporary) {
const triggerAdd = (model: Component) => {
em.trigger(ComponentsEvents.add, model, opts);
model.components().forEach(comp => triggerAdd(comp));
};
triggerAdd(model);
}
// this.__onAddEnd();
}

Expand Down
19 changes: 3 additions & 16 deletions src/dom_components/view/ComponentsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Component from '../model/Component';
import ComponentView from './ComponentView';
import FrameView from '../../canvas/view/FrameView';
import Components from '../model/Components';
import { ComponentsEvents } from '../types';

export default class ComponentsView extends View {
opts!: any;
Expand Down Expand Up @@ -47,18 +46,8 @@ export default class ComponentsView extends View {
* @param {Object} opts
* @private
* */
addTo(model: Component, coll: any = {}, opts: { temporary?: boolean } = {}) {
const { em } = this;
const i = this.collection.indexOf(model);
this.addToCollection(model, null, i);

if (em && !opts.temporary) {
const triggerAdd = (model: Component) => {
em.trigger(ComponentsEvents.add, model, opts);
model.components().forEach(comp => triggerAdd(comp));
};
triggerAdd(model);
}
addTo(model: Component) {
this.addToCollection(model, null, this.collection.indexOf(model));
}

/**
Expand All @@ -70,10 +59,8 @@ export default class ComponentsView extends View {
* @return {Object} Object rendered
* @private
* */
addToCollection(model: Component, fragmentEl?: DocumentFragment | null, index?: number) {
// if (!this.compView) this.compView = require('./ComponentView').default;
addToCollection(model: Component, fragment?: DocumentFragment | null, index?: number) {
const { config, opts, em } = this;
const fragment = fragmentEl || null;
const { frameView } = config;
const sameFrameView = frameView?.model && model.getView(frameView.model);
const dt = opts.componentTypes || em?.Components.getTypes();
Expand Down
1 change: 0 additions & 1 deletion src/navigator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ export default class LayerManager extends Module<LayerManagerConfig> {
}

if (selected && scrollLayers) {
// @ts-ignore
const el = selected.viewLayer?.el;
el?.scrollIntoView(scrollLayers);
}
Expand Down
59 changes: 33 additions & 26 deletions src/navigator/view/ItemsView.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { isUndefined } from 'underscore';
import { View } from '../../common';
import Component from '../../dom_components/model/Component';
import EditorModel from '../../editor/model/Editor';
import ItemView from './ItemView';
import Components from '../../dom_components/model/Components';

export default class ItemsView extends View {
items: ItemView[];
opt: any;
config: any;
parentView: ItemView;
/** @ts-ignore */
collection!: Components;

constructor(opt: any = {}) {
super(opt);
Expand Down Expand Up @@ -47,11 +51,9 @@ export default class ItemsView extends View {
}

removeChildren(removed: Component) {
// @ts-ignore
const view = removed.viewLayer;
if (!view) return;
view.remove();
// @ts-ignore
delete removed.viewLayer;
}

Expand All @@ -62,8 +64,7 @@ export default class ItemsView extends View {
* @return Object
* */
addTo(model: Component) {
var i = this.collection.indexOf(model);
this.addToCollection(model, null, i);
this.addToCollection(model, null, this.collection.indexOf(model));
}

/**
Expand All @@ -74,41 +75,47 @@ export default class ItemsView extends View {
*
* @return Object Object created
* */
addToCollection(model: Component, fragmentEl: DocumentFragment | null, index?: number) {
const { parentView, opt, config } = this;
addToCollection(model: Component, fragment: DocumentFragment | null, index?: number) {
const { parentView, opt, config, el } = this;
const { ItemView, opened, module, level, sorter } = opt;
const fragment = fragmentEl || null;
const item = new ItemView({
ItemView,
level,
model,
parentView,
config,
sorter,
opened,
module,
});
const item: ItemView =
model.viewLayer ||
new ItemView({
ItemView,
level,
model,
parentView,
config,
sorter,
opened,
module,
});
const rendered = item.render().el;

if (fragment) {
fragment.appendChild(rendered);
} else {
if (typeof index !== 'undefined') {
var method = 'before';
const parent = el;
const children = parent.childNodes;

if (!isUndefined(index)) {
const lastIndex = children.length == index;

// If the added model is the last of collection
// need to change the logic of append
if (this.$el.children().length == index) {
if (lastIndex) {
index--;
method = 'after';
}

// In case the added is new in the collection index will be -1
if (index < 0) {
this.$el.append(rendered);
if (lastIndex || !children.length) {
parent.appendChild(rendered);
} else {
// @ts-ignore
this.$el.children().eq(index)[method](rendered);
parent.insertBefore(rendered, children[index]);
}
} else this.$el.append(rendered);
} else {
parent.appendChild(rendered);
}
}
this.items.push(item);
return rendered;
Expand Down

0 comments on commit 1085831

Please sign in to comment.