Skip to content

Commit

Permalink
Use snippet names as fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
thsparks committed Sep 24, 2024
1 parent 944acd7 commit c131bfb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 63 deletions.
48 changes: 27 additions & 21 deletions webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4145,37 +4145,43 @@ export class ProjectView

getBlockTextParts(blockId: string): pxt.editor.BlockTextParts | undefined {
// Get toolbox block definition.
let toolboxBlockMatch: BlockDefinition = undefined;
for (const advanced of [true, false]) {
const toolboxBlockMatches: BlockDefinition[] = [];
for (const advanced of [false, true]) {
for (const category of this.blocksEditor.getToolboxCategories(advanced)) {
toolboxBlockMatch = category.blocks.find(b => b.attributes.blockId === blockId);
if (toolboxBlockMatch) break;
for (const match of category.blocks?.filter(b => b.attributes.blockId === blockId) ?? []) {
toolboxBlockMatches.push(match);
}
}
if (toolboxBlockMatch) break;
}

if (!toolboxBlockMatch) {
console.log("DEBUG: No Block Match for blockId: " + blockId);
if (toolboxBlockMatches.length === 0) {
return undefined;
}

let readableName = toolboxHelpers.getBlockTextParts(
toolboxBlockMatch,
toolboxBlockMatch.parameters ? toolboxBlockMatch.parameters.slice() : null,
false);
const blockSnippets: string[] = [];
for (const match of toolboxBlockMatches) {
let readableName = toolboxHelpers.getBlockTextParts(
match,
match.parameters ? match.parameters.slice() : null,
false);

if (!readableName) {
const blocksBlockMatch = pxt.blocks.getBlockDefinition(blockId);
if (blocksBlockMatch) {
readableName = toolboxHelpers.getBlockTextPartsFromBlocksBlockDefinition(blocksBlockMatch);
}
if (readableName) {
// TODO thsparks - what to do if there are multiple matches?
return readableName;
}

if (!readableName) {
blockSnippets.push(toolboxHelpers.getSnippetName(match, false) || match.name);
}
}

if (!readableName) {
readableName.parts.push({
kind: "label",
content: toolboxHelpers.getSnippetName(toolboxBlockMatch, false) || toolboxBlockMatch.name,
});
const readableName: pxt.editor.BlockTextParts = blockSnippets.length === 0 ? undefined : {
parts: [
{
kind: "label",
content: blockSnippets.join(" | ")
}
],
}

return readableName;
Expand Down
42 changes: 0 additions & 42 deletions webapp/src/toolboxHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,6 @@ export function getSnippetName(block: toolbox.BlockDefinition, isPython: boolean
return (isPython ? (block.pySnippetName || block.pyName) : undefined) || block.snippetName || block.name;
}

// TODO thsparks : find a better name for this function
export function getBlockTextPartsFromBlocksBlockDefinition(block: pxt.blocks.BlockDefinition): pxt.editor.BlockTextParts | undefined {
const parts: pxt.editor.BlockTextPart[] = [];
if (block?.block && block.block["message0"]) {
// These message values use %1, %2, etc. for parameters.
// Extract these into generic "value" parameters.
const message = block.block["message0"];
const regex = /%(\d+)/g;
let lastIndex = 0;
let match;

while ((match = regex.exec(message)) !== null) {
// Add the text before the parameter as a label (if it's not empty)
if (match.index > lastIndex) {
const content = message.substring(lastIndex, match.index).trim();
if (content) {
parts.push({ kind: "label", content });
}
}

// Add the parameter
parts.push({
kind: "param",
content: "value"
});
lastIndex = regex.lastIndex;
}

// Add any remaining text after the last parameter
if (lastIndex < message.length) {
parts.push({
kind: "label",
content: message.substring(lastIndex).trim()
});
}
} else {
parts.push({ kind: "label", content: block.name });
}

return { parts };
}

// Breaks a block down into segments that can be displayed in a readable format.
export function getBlockTextParts(block: toolbox.BlockDefinition, params: pxtc.ParameterDesc[], isPython: boolean): pxt.editor.BlockTextParts | undefined {
let description: pxt.editor.BlockTextPart[] = [];
Expand Down

0 comments on commit c131bfb

Please sign in to comment.