Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 110 additions & 49 deletions src/ExpressionVisualizerWebComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ async function _initMath() {
@localized()
export class ExpressionVisualizerWebComponent extends LitElement {
static styles = css`
html {
color:#4E5969;
font-family: SimSun, sans-serif;
}
html {
color: #4e5969;
font-family: SimSun, sans-serif;
}
.expression-visualizer {
min-height: 300px;
}
Expand Down Expand Up @@ -167,22 +167,18 @@ export class ExpressionVisualizerWebComponent extends LitElement {
color: red;
}
.title-name {
display:inline-block;
text-align: right;
display: inline-block;
text-align: left;
font-size: 14px;
margin-right: 10px;
}
.w-125 {
width: 110px;
margin-right: 20px;
width: 160px;
}
.w-70 {
width: 70px;
}
.optbtn{

.optbtn {
height: 20px;
min-width: 20px;
border-radius: 3px;
background-color: #1BBEF7;
background-color: #1bbef7;
color: #fff;
border: none;
cursor: pointer;
Expand All @@ -191,11 +187,21 @@ export class ExpressionVisualizerWebComponent extends LitElement {
height: 22px;
border: none;
cursor: pointer;
color:#4E5969;
color: #4e5969;
}
.varbtn:hover {
border: 1px solid #1bbef7;
}
.delete-btn {
color:#4E5969;
border-color: color:#4E5969;;
color: #1bbef7;
cursor: pointer;
font-size: 14px;
}
.expression-cls {
background-color: #f2f3f5;
height: 32px;
border: none;
line-height: 32px;
}
`;

Expand Down Expand Up @@ -239,8 +245,12 @@ export class ExpressionVisualizerWebComponent extends LitElement {
@property({ type: Array }) variables: {
name: string;
test: string | number | boolean;
op?: string;
isFn?: string;
}[] = [];

@property({ type: String }) operatorMode: 'default' | 'variable' = 'default';

@state()
_curTheme = themeMap.get(this.theme);

Expand Down Expand Up @@ -590,6 +600,71 @@ export class ExpressionVisualizerWebComponent extends LitElement {
};
}

// 添加变量
private _addSymbolNode2(variable: any) {
return () => {
let block: any;

if (variable.isFn) {
block = {
args: [
{
type: 'SymbolNode',
uuid: uuidv4(),
path: 'args[0]',
index: 0,
name: variable.name,
},
{
type: 'ConstantNode',
value: variable.test,
uuid: uuidv4(),
path: 'args[1]',
index: 1,
},
],
fn: {
name: variable.isFn,
},
implicit: false,
isPercentage: false,
type: 'FunctionNode',
uuid: uuidv4(),
};
} else {
block = {
uuid: uuidv4(),
implicit: false,
isPercentage: false,
op: variable.op,
fn: operatorMap.get(variable.op),
args: [
{
type: 'SymbolNode',
uuid: uuidv4(),
path: 'args[0]',
index: 0,
name: variable.name,
},
{
type: 'ConstantNode',
value: variable.test,
uuid: uuidv4(),
path: 'args[1]',
index: 1,
},
],
type: 'OperatorNode',
};
}

this._blocks = [block, ...this._blocks];

// 表达式
this._generateExpression();
};
}

// 修改
// 子组件上报事件
// 拖拽一个表达式到另一个表达式的插槽
Expand Down Expand Up @@ -784,24 +859,15 @@ export class ExpressionVisualizerWebComponent extends LitElement {
<div
class=${this.hiddenexpression ? 'hidden expression' : 'expression'}
>
<span
class=${this.locale === 'en'
? 'title-name w-125'
: 'title-name w-70'}
style="margin-right:15px"
<span class="title-name" style="margin-right:25px"
>${msg('Expression')}:</span
>
<span class="block">${this._expression}</span>
<span class="block expression-cls">${this._expression}</span>
<span class="equal">=</span>
<span class="block">${this._result}</span>
<span class="block expression-cls">${this._result}</span>
</div>
<div class="tools">
<span
class=${this.locale === 'en'
? 'title-name w-125'
: 'title-name w-70'}
>${msg('Toolbar')}:</span
>
<span class="title-name">${msg('Toolbar')}:</span>
<input
id="newconstant-input"
class=${this.hiddenConstant ? 'hidden' : ''}
Expand All @@ -815,7 +881,7 @@ export class ExpressionVisualizerWebComponent extends LitElement {
>
${msg('Add constant')}
</button>
<span class="split-group"></span>
<span class=${this.hiddenConstant ? 'hidden' : 'split-group'}></span>
${map(
this.operators.filter(operator => operatorMap.has(operator.name)),
operator => html`
Expand All @@ -842,38 +908,32 @@ export class ExpressionVisualizerWebComponent extends LitElement {
`
)}
<div style="padding: 8px 0">
<span
class=${this.locale === 'en'
? 'title-name w-125'
: 'title-name w-70'}
>${msg('Attribute List')}:</span
>
<span class="title-name">${msg('Attribute List')}:</span>
${map(
this.variables,
variable => html`
<button
class="varbtn"
.id=${`${variable.name}-btn`}
@click=${this._addSymbolNode(variable.name)}
@click=${this.operatorMode === 'variable'
? this._addSymbolNode2(variable)
: this._addSymbolNode(variable.name)}
>
${variable.name} = ${variable.test}
${variable.name}
${this.operatorMode === 'variable' ? variable.op : '='}
${variable.test}
</button>
`
)}
</div>
<div>
<span
class=${this.locale === 'en'
? 'title-name w-125'
: 'title-name w-70'}
>${msg('Constants List')}:</span
>
<div class=${this.operatorMode === 'variable' ? 'hidden' : ''}>
<span class="title-name">${msg('Constants List')}:</span>
${map(
this.constantList,
constant => html`
<button
class="varbtn"
.id=${`${constant}-constbtn`}
.id=${`constbtn-${constant}`}
@click=${this._addConstantNodeForm2List(constant)}
>
${constant}
Expand All @@ -897,10 +957,11 @@ export class ExpressionVisualizerWebComponent extends LitElement {
<tree-component
@changed=${this._handleChanged}
.block=${block}
.operatorMode=${this.operatorMode}
></tree-component>
<button
class="delete-btn"
style="height: 26px;"
style="height: 26px; border: none; background-color: transparent"
@click=${this._deleteBlock(index)}
>
${msg('Delete')}
Expand Down
60 changes: 51 additions & 9 deletions src/helper/demo-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,31 @@ const _operators = [
{ name: 'not' },
];

const _operators1 = [
{ name: 'and' },
{ name: 'or' },
{ name: 'xor' },
{ name: 'not' },
];

const _funcs = [{ name: 'equalText' }];

const _variables: {
name: string;
test: boolean | number | string;
test: string | number | boolean;
op?: string;
isFn?: string;
}[] = [
{ name: 'variable1', test: 1 },
{ name: 'variable2', test: true },
{ name: 'variable3', test: false },
{ name: 'variable4', test: 'abc' },
{ name: 'variable1', test: 1, op: '>' },
{ name: 'variable2', test: true, op: '==' },
{ name: 'variable3', test: false, op: '!=' },
{ name: 'variable4', test: 'abc', op: '==', isFn: 'equalText' },
];

const _operatorMode = 'default';

const _operatorModeList = ['default', 'variable'];

@customElement('demo-helper')
export class DemoHelper extends LitElement {
static styles = css`
Expand All @@ -54,6 +67,8 @@ export class DemoHelper extends LitElement {
}
`;

@state() private operatorMode: string = _operatorMode;

@state()
private theme: 'light' | 'dark' = 'light';

Expand Down Expand Up @@ -107,7 +122,6 @@ export class DemoHelper extends LitElement {
variableListChange(e: CustomEvent) {
// console.log( e.detail.constantList)
this.constantList = e.detail.constantList;
console.log(this.constantList);
}

onLocaleChanged(e: CustomEvent) {
Expand Down Expand Up @@ -153,6 +167,11 @@ export class DemoHelper extends LitElement {
this.variables = e.detail.filter;
}

modeChanged(e: Event) {
const newMode = (e.target as HTMLSelectElement).value;
this.operatorMode = newMode;
}

render() {
return html`
<expression-visualizer-web-component
Expand All @@ -161,8 +180,11 @@ export class DemoHelper extends LitElement {
.hiddenexpression=${this.hiddenexpression}
.hiddenConstant=${this.hiddenConstant}
.expression=${this.expression}
.operators=${this.operators}
.funcs=${this.funcs}
.operators=${this.operatorMode === 'variable'
? _operators1
: this.operators}
.operatorMode=${this.operatorMode}
.funcs=${this.operatorMode === 'variable' ? [] : this.funcs}
.variables=${this.variables}
.constantList=${this.constantList}
@expression-inited=${handleExpressionInited}
Expand All @@ -175,6 +197,26 @@ export class DemoHelper extends LitElement {
.locale=${this.locale}
@locale-changed=${this.onLocaleChanged}
></locale-picker-helper>

<div class="properties-helper">
<label id="locale-label" for="locale-picker">operatorMode:</label>
<select
id="operator-mode"
aria-label="operatormode"
@change=${this.modeChanged}
>
${_operatorModeList.map(
mode =>
html`<option
value=${mode}
?selected=${mode === this.operatorMode}
>
${mode}
</option>`
)}
</select>
</div>

<div class="properties-helper"></div>
hiddenexpression:
<input
Expand Down Expand Up @@ -203,7 +245,7 @@ export class DemoHelper extends LitElement {
/>
<button @click=${this.onSendExpression}>Send</button>
<div class="properties-helper"></div>
operators:
constantList:
<filter-list-helper
.list=${_operators}
@filter-changed=${this.onOperatorsChanged}
Expand Down
Loading