Browse Source

Up symbols method names

symbols-api
Artur Arseniev 2 years ago
parent
commit
a5146eb2cf
  1. 11
      src/dom_components/index.ts
  2. 14
      src/dom_components/model/Component.ts
  3. 28
      src/dom_components/model/SymbolUtils.ts
  4. 11
      src/dom_components/types.ts

11
src/dom_components/index.ts

@ -102,7 +102,8 @@ import ComponentView, { IComponentView } from './view/ComponentView';
import ComponentWrapperView from './view/ComponentWrapperView';
import ComponentsView from './view/ComponentsView';
import ComponentHead, { type as typeHead } from './model/ComponentHead';
import { getSymbol, getSymbols, getSymbolsToUpdate, isSymbolMain } from './model/SymbolUtils';
import { getSymbolMain, getSymbolInstances, getSymbolsToUpdate, isSymbolMain } from './model/SymbolUtils';
import { SymbolInfo } from './types';
export type ComponentEvent =
| 'component:create'
@ -704,7 +705,7 @@ export default class ComponentManager extends ItemManagerModule<DomComponentsCon
}
/**
* Get the array of all available symbols.
* Get the array of available symbols.
* @returns {Array<[Component]>}
*/
getSymbols() {
@ -716,11 +717,11 @@ export default class ComponentManager extends ItemManagerModule<DomComponentsCon
* @param {[Component]} cmp Component symbol from which to get the info.
* @returns
*/
getSymbolInfo(cmp: Component, opts: { withChanges?: string } = {}) {
getSymbolInfo(cmp: Component, opts: { withChanges?: string } = {}): SymbolInfo {
const isMain = isSymbolMain(cmp);
const mainRef = getSymbol(cmp);
const mainRef = getSymbolMain(cmp);
const isInstance = !!mainRef;
const instances = (isMain ? getSymbols(cmp) : getSymbols(mainRef)) || [];
const instances = (isMain ? getSymbolInstances(cmp) : getSymbolInstances(mainRef)) || [];
const main = mainRef || (isMain ? cmp : undefined);
const relatives = getSymbolsToUpdate(cmp, { changed: opts.withChanges });

14
src/dom_components/model/Component.ts

@ -41,8 +41,8 @@ import { TraitProperties } from '../../trait_manager/types';
import { ActionLabelComponents, ComponentsEvents } from '../types';
import ItemView from '../../navigator/view/ItemView';
import {
getSymbol,
getSymbols,
getSymbolMain,
getSymbolInstances,
initSymbol,
isSymbol,
isSymbolMain,
@ -713,8 +713,8 @@ export default class Component extends StyleableModel<ComponentProperties> {
if (
// Symbols should always have an id
getSymbol(this) ||
getSymbols(this) ||
getSymbolMain(this) ||
getSymbolInstances(this) ||
// Components with script should always have an id
this.get('script-export') ||
this.get('script')
@ -1258,15 +1258,15 @@ export default class Component extends StyleableModel<ComponentProperties> {
// Symbols
// If I clone an inner symbol, I have to reset it
cloned.set(keySymbols, 0);
const symbol = getSymbol(this);
const symbols = getSymbols(this);
const symbol = getSymbolMain(this);
const symbols = getSymbolInstances(this);
if (!opt.symbol && (symbol || symbols)) {
cloned.set(keySymbol, 0);
cloned.set(keySymbols, 0);
} else if (symbol) {
// Contains already a reference to a symbol
symbol.set(keySymbols, [...getSymbols(symbol)!, cloned]);
symbol.set(keySymbols, [...getSymbolInstances(symbol)!, cloned]);
initSymbol(cloned);
} else if (opt.symbol) {
// Request to create a symbol

28
src/dom_components/model/SymbolUtils.ts

@ -17,9 +17,9 @@ export const isSymbolTop = (symbol: Component) => {
export const isSymbolNested = (symbol: Component) => {
if (!isSymbol(symbol)) return false;
const symbTopSelf = getSymbolTop(isSymbolMain(symbol) ? symbol : getSymbol(symbol)!);
const symbTopSelf = getSymbolTop(isSymbolMain(symbol) ? symbol : getSymbolMain(symbol)!);
const symbTop = getSymbolTop(symbol);
const symbTopMain = isSymbolMain(symbTop) ? symbTop : getSymbol(symbTop);
const symbTopMain = isSymbolMain(symbTop) ? symbTop : getSymbolMain(symbTop);
return symbTopMain !== symbTopSelf;
};
@ -29,7 +29,7 @@ export const initSymbol = (symbol: Component) => {
symbol.__symbReady = true;
};
export const getSymbol = (symbol: Component): Component | undefined => {
export const getSymbolMain = (symbol: Component): Component | undefined => {
let result = symbol.get(keySymbol);
if (result && isString(result)) {
@ -45,7 +45,7 @@ export const getSymbol = (symbol: Component): Component | undefined => {
return result || undefined;
};
export const getSymbols = (symbol?: Component): Component[] | undefined => {
export const getSymbolInstances = (symbol?: Component): Component[] | undefined => {
let symbs = symbol?.get(keySymbols);
if (symbs && isArray(symbs)) {
@ -81,9 +81,9 @@ export const getSymbolsToUpdate = (symb: Component, opts: SymbolToUpOptions = {}
return result;
}
const symbols = getSymbols(symb) || [];
const symbol = getSymbol(symb);
const all = symbol ? [symbol, ...(getSymbols(symbol) || [])] : symbols;
const symbols = getSymbolInstances(symb) || [];
const symbol = getSymbolMain(symb);
const all = symbol ? [symbol, ...(getSymbolInstances(symbol) || [])] : symbols;
result = all
.filter(s => s !== symb)
// Avoid updating those with override
@ -106,8 +106,8 @@ export const getSymbolTop = (symbol: Component, opts?: any) => {
};
export const logSymbol = (symb: Component, type: string, toUp: Component[], opts: any = {}) => {
const symbol = getSymbol(symb);
const symbols = getSymbols(symb);
const symbol = getSymbolMain(symb);
const symbols = getSymbolInstances(symb);
if (!symbol && !symbols) {
return;
@ -183,14 +183,14 @@ export const updateSymbolComps = (symbol: Component, m: Component, c: Components
// Add
} else if (o.add) {
let addedInstances: Component[] = [];
const isMainSymb = !!getSymbols(symbol);
const isMainSymb = !!getSymbolInstances(symbol);
const toUp = getSymbolsToUpdate(symbol, {
...toUpOpts,
changed: 'components:add',
});
if (toUp.length) {
const addSymb = getSymbol(m);
addedInstances = (addSymb ? getSymbols(addSymb) : getSymbols(m)) || [];
const addSymb = getSymbolMain(m);
addedInstances = (addSymb ? getSymbolInstances(addSymb) : getSymbolInstances(m)) || [];
addedInstances = [...addedInstances];
addedInstances.push(addSymb ? addSymb : m);
}
@ -214,12 +214,12 @@ export const updateSymbolComps = (symbol: Component, m: Component, c: Components
// Remove
} else {
// Remove instance reference from the symbol
const symb = getSymbol(m);
const symb = getSymbolMain(m);
symb &&
!o.temporary &&
symb.set(
keySymbols,
getSymbols(symb)!.filter(i => i !== m)
getSymbolInstances(symb)!.filter(i => i !== m)
);
// Propagate remove only if the component is an inner symbol

11
src/dom_components/types.ts

@ -1,9 +1,20 @@
import Component from './model/Component';
export enum ActionLabelComponents {
remove = 'component:remove',
add = 'component:add',
move = 'component:move',
}
export interface SymbolInfo {
isSymbol: boolean;
isMain: boolean;
isInstance: boolean;
main?: Component;
instances: Component[];
relatives: Component[];
}
export enum ComponentsEvents {
/**
* @event `component:add` New component added.

Loading…
Cancel
Save