Skip to content

Commit 7515e29

Browse files
authored
store block param localization in translation cache (#10593)
1 parent b057fec commit 7515e29

File tree

4 files changed

+95
-6
lines changed

4 files changed

+95
-6
lines changed

pxtblocks/loader.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import { FieldDropdown } from "./fields/field_dropdown";
2626
import { setDraggableShadowBlocks, setDuplicateOnDrag, setDuplicateOnDragStrategy } from "./plugins/duplicateOnDrag";
2727
import { initCopyPaste } from "./copyPaste";
2828
import { FieldVariable } from "./plugins/newVariableField/fieldVariable";
29+
import { ArgumentReporterBlock, FieldArgumentReporter, setArgumentReporterLocalizeFunction } from "./plugins/functions";
30+
import { getArgumentReporterParent } from "./plugins/functions/utils";
31+
import { isFunctionDefinition } from "./compiler/util";
2932

3033
export const DRAGGABLE_PARAM_INPUT_PREFIX = "HANDLER_DRAG_PARAM_";
3134

@@ -104,7 +107,11 @@ export function blockSymbol(type: string): pxtc.SymbolInfo {
104107
export function injectBlocks(blockInfo: pxtc.BlocksInfo): pxtc.SymbolInfo[] {
105108
cachedBlockInfo = blockInfo;
106109

107-
setDraggableShadowBlocks(blockInfo.blocks.filter(fn => fn.attributes.duplicateShadowOnDrag).map(fn => fn.attributes.blockId));
110+
setDraggableShadowBlocks(blockInfo.blocks.filter(fn => fn.attributes.duplicateShadowOnDrag).map(fn => fn.attributes.blockId));
111+
112+
setArgumentReporterLocalizeFunction((arg, block) => {
113+
return localizeArgumentReporter(blockInfo, arg, block);
114+
});
108115

109116
// inject Blockly with all block definitions
110117
return blockInfo.blocks
@@ -849,3 +856,37 @@ export function setVarFieldValue(block: Blockly.Block, fieldName: string, newNam
849856
varField.setValue(model.getId());
850857
}
851858
}
859+
860+
861+
function localizeArgumentReporter(blocksInfo: pxtc.BlocksInfo, field: FieldArgumentReporter, block: ArgumentReporterBlock): string | undefined {
862+
let result: string = undefined;
863+
864+
const mutationName = block.getLocalizationName();
865+
if (mutationName) {
866+
const localized = pxt.U.rlf(mutationName);
867+
if (localized !== mutationName) {
868+
result = localized;
869+
}
870+
else {
871+
result = pxtc.getBlockTranslationsCacheKey(mutationName);
872+
}
873+
}
874+
875+
const parent = getArgumentReporterParent(block, block);
876+
877+
if (!parent || isFunctionDefinition(parent)) return result;
878+
879+
const fn = blocksInfo.blocksById[parent.type];
880+
881+
if (!fn) return result;
882+
883+
const comp = pxt.blocks.compileInfo(fn);
884+
885+
const handlerArg = comp.handlerArgs?.find(arg => arg.name === field.getValue());
886+
887+
if (handlerArg) {
888+
return pxtc.getBlockTranslationsCacheKey(handlerArg.localizationKey);
889+
}
890+
891+
return result;
892+
}

pxtblocks/plugins/functions/fields/fieldArgumentReporter.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import * as Blockly from "blockly";
22
import { isFunctionArgumentReporter } from "../utils";
33
import { ArgumentReporterBlock } from "../blocks/argumentReporterBlocks";
44

5+
let localizeFunction: (arg: FieldArgumentReporter, block: ArgumentReporterBlock) => string;
6+
57
export class FieldArgumentReporter extends Blockly.FieldLabelSerializable {
68
protected override getDisplayText_(): string {
79
const source = this.getSourceBlock();
8-
if (source && isFunctionArgumentReporter(source)) {
9-
const localizeKey = (source as ArgumentReporterBlock).getLocalizationName();
10+
if (source && isFunctionArgumentReporter(source) && localizeFunction) {
11+
const localized = localizeFunction(this, source as ArgumentReporterBlock);
1012

11-
const localized = localizeKey && pxt.U.rlf(localizeKey);
12-
if (localized && localized !== localizeKey) {
13+
if (localized) {
1314
return localized;
1415
}
1516
}
@@ -18,4 +19,8 @@ export class FieldArgumentReporter extends Blockly.FieldLabelSerializable {
1819
}
1920
}
2021

22+
export function setArgumentReporterLocalizeFunction(func: (arg: FieldArgumentReporter, block: ArgumentReporterBlock) => string) {
23+
localizeFunction = func;
24+
}
25+
2126
Blockly.registry.register(Blockly.registry.Type.FIELD, "field_argument_reporter", FieldArgumentReporter);

pxtblocks/plugins/functions/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export function doArgumentReporterDragChecks(a: Blockly.RenderedConnection, b: B
278278
return !!(getArgumentReporterParent(draggedBlock, destinationBlock));
279279
}
280280

281-
function getArgumentReporterParent(reporter: Blockly.Block, location: Blockly.Block): Blockly.Block | undefined {
281+
export function getArgumentReporterParent(reporter: Blockly.Block, location: Blockly.Block): Blockly.Block | undefined {
282282
pxt.U.assert(isFunctionArgumentReporter(reporter));
283283

284284
const varName = reporter.getFieldValue("VALUE");

pxtlib/service.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace ts.pxtc {
3030

3131
export const NATIVE_TYPE_THUMB = "thumb";
3232
export const NATIVE_TYPE_VM = "vm";
33+
export const BLOCK_TRANSLATION_CACHE_KEY = "_blocks";
3334

3435
export interface BlocksInfo {
3536
apis: ApisInfo;
@@ -724,6 +725,20 @@ namespace ts.pxtc {
724725
const nsDoc = loc['{id:category}' + Util.capitalize(fn.qName)];
725726
let locBlock = loc[`${fn.qName}|block`] || fn.attributes.locs?.[attrBlockLocsKey];
726727

728+
if (fn.attributes.block) {
729+
const comp = pxt.blocks.compileInfo(fn);
730+
if (comp.handlerArgs) {
731+
for (const arg of comp.handlerArgs) {
732+
if (loc[arg.localizationKey]) {
733+
setBlockTranslationCacheKey(arg.localizationKey, loc[arg.localizationKey]);
734+
}
735+
else {
736+
clearBlockTranslationCacheKey(arg.localizationKey);
737+
}
738+
}
739+
}
740+
}
741+
727742
if (!locBlock && altLocSrcFn) {
728743
const otherTranslation = loc[`${altLocSrcFn.qName}|block`] || altLocSrcFn.attributes.locs?.[attrBlockLocsKey];
729744
const isSameBlockDef = fn.attributes.block === (altLocSrcFn.attributes._untranslatedBlock || altLocSrcFn.attributes.block);
@@ -795,6 +810,34 @@ namespace ts.pxtc {
795810
return apis;
796811
}
797812

813+
function setBlockTranslationCacheKey(key: string, value: string) {
814+
const cache = pxt.Util.translationsCache();
815+
816+
if (!cache[BLOCK_TRANSLATION_CACHE_KEY]) {
817+
cache[BLOCK_TRANSLATION_CACHE_KEY] = {};
818+
}
819+
820+
cache[BLOCK_TRANSLATION_CACHE_KEY][key] = value;
821+
}
822+
823+
function clearBlockTranslationCacheKey(key: string) {
824+
const cache = pxt.Util.translationsCache();
825+
826+
if (cache[BLOCK_TRANSLATION_CACHE_KEY]) {
827+
delete cache[BLOCK_TRANSLATION_CACHE_KEY][key];
828+
}
829+
}
830+
831+
export function getBlockTranslationsCacheKey(key: string): string | undefined {
832+
const cache = pxt.Util.translationsCache();
833+
834+
if (cache[BLOCK_TRANSLATION_CACHE_KEY]) {
835+
return cache[BLOCK_TRANSLATION_CACHE_KEY][key];
836+
}
837+
838+
return undefined;
839+
}
840+
798841
function hasEquivalentParameters(a: pxt.blocks.BlockCompileInfo, b: pxt.blocks.BlockCompileInfo) {
799842
if (a.parameters.length != b.parameters.length) {
800843
pxt.debug(`Localized block has extra or missing parameters`);

0 commit comments

Comments
 (0)