|
| 1 | +/// <reference path="../../built/pxtlib.d.ts" /> |
| 2 | + |
| 3 | +import * as Blockly from "blockly"; |
| 4 | +import { FieldCustom, FieldCustomOptions, setBlockDataForField, getBlockDataForField } from "./field_utils"; |
| 5 | + |
| 6 | +interface FieldScopedValueSelectorOptions extends FieldCustomOptions { |
| 7 | + defl?: string; |
| 8 | + type?: string; |
| 9 | + types?: string; // comma separated list |
| 10 | +} |
| 11 | + |
| 12 | +export class FieldScopedValueSelector extends Blockly.FieldLabel implements FieldCustom { |
| 13 | + public isFieldCustom_ = true; |
| 14 | + defl: string | undefined; |
| 15 | + types: string[] = []; |
| 16 | + scopedValue: string | undefined; |
| 17 | + dragging = false; |
| 18 | + |
| 19 | + constructor(value: string, params: FieldScopedValueSelectorOptions) { |
| 20 | + super(value); |
| 21 | + this.defl = params.defl; |
| 22 | + if (params.types) this.types = params.types.split(","); |
| 23 | + else if (params.type) this.types = [params.type]; |
| 24 | + this.types = this.types.map(t => t.trim().replace(/['"]+/g, "")); |
| 25 | + } |
| 26 | + |
| 27 | + init(): void { |
| 28 | + super.init(); |
| 29 | + if (this.sourceBlock_) { |
| 30 | + this.scopedValue = getBlockDataForField(this.sourceBlock_, "scopedValue"); |
| 31 | + this.sourceBlock_.workspace.addChangeListener(this.onWorkspaceChange); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + dispose(): void { |
| 36 | + if (this.sourceBlock_) { |
| 37 | + this.sourceBlock_.workspace.removeChangeListener(this.onWorkspaceChange); |
| 38 | + } |
| 39 | + super.dispose(); |
| 40 | + } |
| 41 | + |
| 42 | + getValue(): string | null { |
| 43 | + // compiler emits into typescript |
| 44 | + if (this.sourceBlock_?.isInFlyout) { |
| 45 | + return lf("(dynamic)"); |
| 46 | + } else if (this.dragging) { |
| 47 | + return lf("what will it be?"); |
| 48 | + } |
| 49 | + if (this.sourceBlock_ && !this.scopedValue) { |
| 50 | + this.scopedValue = getBlockDataForField(this.sourceBlock_, "scopedValue"); |
| 51 | + } |
| 52 | + return this.scopedValue || this.defl || lf("unknown"); |
| 53 | + } |
| 54 | + |
| 55 | + setValue(newValue: any, fireChangeEvent?: boolean): void { |
| 56 | + this.scopedValue = newValue || this.defl || lf("unknown"); |
| 57 | + if (this.sourceBlock_) |
| 58 | + setBlockDataForField(this.sourceBlock_, "scopedValue", this.scopedValue || ""); |
| 59 | + super.setValue(this.scopedValue, fireChangeEvent); |
| 60 | + this.forceRerender(); |
| 61 | + } |
| 62 | + |
| 63 | + onDragEvent = (ev: Blockly.Events.BlockDrag) => { |
| 64 | + // Make sure our block is in the event |
| 65 | + const block = ev.blocks.find(b => b.id === this.sourceBlock_.id); |
| 66 | + if (!block) return; |
| 67 | + |
| 68 | + this.dragging = ev.isStart; |
| 69 | + if (ev.isStart) { |
| 70 | + this.forceRerender(); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // gather all scopes where we might find a compatible value |
| 75 | + const scopes: Blockly.Block[] = []; |
| 76 | + { |
| 77 | + let parent = this.sourceBlock_.getParent()?.getParent(); |
| 78 | + while (parent) { |
| 79 | + scopes.push(parent); |
| 80 | + parent = parent.getParent(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const getCodeCard = (block: Blockly.Block): pxt.CodeCard => { |
| 85 | + return (block as any).codeCard; |
| 86 | + } |
| 87 | + |
| 88 | + const apiInfos = pxt.getBundledApiInfo(); |
| 89 | + const getSymbolInfo = (block: Blockly.Block): ts.pxtc.SymbolInfo | undefined => { |
| 90 | + const card = getCodeCard(block); |
| 91 | + if (!card || !card.name) return null; |
| 92 | + // check each entry in apiInfo |
| 93 | + for (const info of Object.values(apiInfos)) { |
| 94 | + if (info.apis.byQName[card.name]) { |
| 95 | + return info.apis.byQName[card.name]; |
| 96 | + } |
| 97 | + } |
| 98 | + return undefined; |
| 99 | + } |
| 100 | + |
| 101 | + // find the value in the scopes |
| 102 | + this.scopedValue = null; |
| 103 | + for (const scope of scopes) { |
| 104 | + if (scope.type === "variables_set") { |
| 105 | + const inputList = scope.inputList; |
| 106 | + const input = inputList.find(i => i.name === "VALUE"); |
| 107 | + if (!input) continue; |
| 108 | + const fieldRow = input.fieldRow; |
| 109 | + if (!fieldRow) continue; |
| 110 | + const field = fieldRow.find(f => f.name === "VAR") as Blockly.FieldVariable; |
| 111 | + if (!field) continue; |
| 112 | + const variable = field.getVariable(); |
| 113 | + if (!variable) continue; |
| 114 | + //if (this.types.includes(variable.type)) { |
| 115 | + { |
| 116 | + return this.setValue(variable.name); |
| 117 | + } |
| 118 | + continue; |
| 119 | + } |
| 120 | + const symbol = getSymbolInfo(scope); |
| 121 | + if (!symbol) continue; |
| 122 | + for (const parameter of symbol.parameters) { |
| 123 | + if (parameter.handlerParameters) { |
| 124 | + for (const handlerParameter of parameter.handlerParameters) { |
| 125 | + if (this.types.includes(handlerParameter.type)) { |
| 126 | + return this.setValue(handlerParameter.name); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + this.setValue(this.defl); |
| 133 | + } |
| 134 | + |
| 135 | + onWorkspaceChange = (ev: Blockly.Events.Abstract) => { |
| 136 | + if (!this.sourceBlock_ || !this.sourceBlock_.workspace || this.sourceBlock_.disposed) return; |
| 137 | + if (ev.type === Blockly.Events.BLOCK_DRAG) return this.onDragEvent(ev as Blockly.Events.BlockDrag); |
| 138 | + } |
| 139 | +} |
0 commit comments