Skip to content

Commit

Permalink
fix addSubBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
rinick committed Feb 15, 2023
1 parent b28bdaf commit a1b436a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
23 changes: 23 additions & 0 deletions src/core/functions/data/CreateObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,26 @@ Functions.add(CreateObjectFunction, {
configs: ([{name: '#extend', type: 'object'}] as (string | PropDesc)[]).concat(defaultConfigs),
category: 'data',
});

export class CreateObjectFunctionOptional extends BaseFunction {
configChanged(config: BlockConfig, val: any): boolean {
switch (config._name) {
case '#extend':
case '#optional':
return true;
default:
return false;
}
}
run() {
let baseObj = this._data.getValue('#extend');
let output = baseObj ? {...baseObj} : {};
for (let field of this._data.getOptionalProps()) {
let value = this._data.getValue(field);
if (value !== undefined) {
output[field] = value;
}
}
this._data.output(output);
}
}
5 changes: 2 additions & 3 deletions src/editor/popup/PropertyDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface Props {
paths: string[];
name: string;
baseName?: string;
onAddSubBlock: () => void;
onAddSubBlock: (id: string, desc?: FunctionDesc, data?: any) => void;
}

const PendingUpdate = <div />;
Expand Down Expand Up @@ -219,9 +219,8 @@ export class PropertyDropdown extends React.PureComponent<Props, State> {

onAddSubBlock = (id: string, desc?: FunctionDesc, data?: any) => {
let {onAddSubBlock} = this.props;
PropertyDropdown.addSubBlock(this.props, id, desc, data);
this.setState({visible: false});
onAddSubBlock?.();
onAddSubBlock?.(id, desc, data);
};

getMenu() {
Expand Down
6 changes: 3 additions & 3 deletions src/editor/property/PropertyEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ export class PropertyEditor extends MultiSelectComponent<PropertyEditorProps, St
// check drag from type
let blockData = DragState.getData('blockData', conn.getBaseConn());
if (blockData && blockData['#is']) {
PropertyDropdown.addSubBlock(this.props, blockData['#is'], null, getSubBlockFuncData(blockData));
this.onAddSubBlock();
this.onAddSubBlock(blockData['#is'], null, getSubBlockFuncData(blockData));
return;
}
}
Expand Down Expand Up @@ -342,7 +341,8 @@ export class PropertyEditor extends MultiSelectComponent<PropertyEditorProps, St
}
};

onAddSubBlock = () => {
onAddSubBlock = (id: string, desc?: FunctionDesc, data?: any) => {
PropertyDropdown.addSubBlock(this.props, id, desc, data);
this.safeSetState({showSubBlock: true});
};

Expand Down
27 changes: 2 additions & 25 deletions src/html/functions/CreateStyle.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
import {BaseFunction, defaultConfigs, Functions, PropDesc, PropGroupDesc} from '../../../src/core';
import StyleDef from './StyleDef';
import {BlockConfig} from '../../core/block/BlockProperty';

export class CreateStyleFunction extends BaseFunction {
configChanged(config: BlockConfig, val: any): boolean {
switch (config._name) {
case '#optional':
case '#extend':
return true;
default:
return false;
}
}
run() {
let baseObj = this._data.getValue('#extend');
let output = baseObj ? {...baseObj} : {};
for (let field of this._data.getOptionalProps()) {
let value = this._data.getValue(field);
if (value !== undefined) {
output[field] = value;
}
}
this._data.output(output);
}
}
import {CreateObjectFunctionOptional} from '../../core/functions/data/CreateObject';

Functions.add(
CreateStyleFunction,
CreateObjectFunctionOptional,
{
name: 'create-style',
icon: 'fab:css3',
Expand Down
2 changes: 1 addition & 1 deletion src/react/BaseElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {htmlAttributes, htmlEventHandlers, optionalHtmlProperties} from './HtmlA
import {BlockConfig} from '../../src/core/block/BlockProperty';

export class HtmlElementFunction extends BlockFunction {
// _comp never changes, this prevent re-render of any parent component, TicloComp should handle all the changes internally
// _comp never changes, this prevents re-render of any parent component, TicloComp should handle all the changes internally
readonly _comp: React.ReactNode;

readonly _eventHandlers = new Map<string, (e: any) => void>();
Expand Down

0 comments on commit a1b436a

Please sign in to comment.