Skip to content

Commit

Permalink
blockTextParts -> blockAsText
Browse files Browse the repository at this point in the history
  • Loading branch information
thsparks committed Sep 25, 2024
1 parent fbeefda commit cae7c4d
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 46 deletions.
14 changes: 7 additions & 7 deletions localtypings/pxteditor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ declare namespace pxt.editor {
| "convertcloudprojectstolocal"
| "setlanguagerestriction"
| "gettoolboxcategories"
| "getblocktextparts"
| "getblockastext"

| "toggletrace" // EditorMessageToggleTraceRequest
| "togglehighcontrast"
Expand Down Expand Up @@ -458,13 +458,13 @@ declare namespace pxt.editor {
categories: pxt.editor.ToolboxCategoryDefinition[];
}

export interface EditorMessageGetBlockReadableNameRequest extends EditorMessageRequest {
action: "getblocktextparts";
export interface EditorMessageGetBlockAsTextRequest extends EditorMessageRequest {
action: "getblockastext";
blockId: string;
}

export interface EditorMessageGetBlockReadableNameResponse {
readableName: pxt.editor.BlockTextParts | undefined;
export interface EditorMessageGetBlockAsTextResponse {
blockAsText: pxt.editor.BlockAsText | undefined;
}

export interface EditorMessageServiceWorkerRegisteredRequest extends EditorMessageRequest {
Expand Down Expand Up @@ -1007,7 +1007,7 @@ declare namespace pxt.editor {
// getBlocks(): Blockly.Block[];
getBlocks(): any[];
getToolboxCategories(advanced?: boolean): pxt.editor.EditorMessageGetToolboxCategoriesResponse;
getBlockTextParts(blockId: string): pxt.editor.BlockTextParts | undefined;
getBlockAsText(blockId: string): pxt.editor.BlockAsText | undefined;

toggleHighContrast(): void;
setHighContrast(on: boolean): void;
Expand Down Expand Up @@ -1275,7 +1275,7 @@ declare namespace pxt.editor {
blockId?: string;
}

export interface BlockTextParts {
export interface BlockAsText {
parts: BlockTextPart[];
}

Expand Down
8 changes: 4 additions & 4 deletions pxteditor/editorcontroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ case "renderxml": {
resp = projectView.getToolboxCategories(msg.advanced);
});
}
case "getblocktextparts": {
const msg = data as pxt.editor.EditorMessageGetBlockReadableNameRequest;
case "getblockastext": {
const msg = data as pxt.editor.EditorMessageGetBlockAsTextRequest;
return Promise.resolve()
.then(() => {
const readableName = projectView.getBlockTextParts(msg.blockId);
resp = { readableName } as pxt.editor.EditorMessageGetBlockReadableNameResponse;
const readableName = projectView.getBlockAsText(msg.blockId);
resp = { blockAsText: readableName } as pxt.editor.EditorMessageGetBlockAsTextResponse;
});
}
case "renderpython": {
Expand Down
8 changes: 4 additions & 4 deletions pxtservices/editorDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,16 +389,16 @@ export class EditorDriver extends IframeDriver {
return (resp.resp as pxt.editor.EditorMessageGetToolboxCategoriesResponse).categories;
}

async getBlockTextParts(blockId: string): Promise<pxt.editor.BlockTextParts | undefined> {
async getBlockAsText(blockId: string): Promise<pxt.editor.BlockAsText | undefined> {
const resp = await this.sendRequest(
{
type: "pxteditor",
action: "getblocktextparts",
action: "getblockastext",
blockId
} as pxt.editor.EditorMessageGetBlockReadableNameRequest
} as pxt.editor.EditorMessageGetBlockAsTextRequest
) as pxt.editor.EditorMessageResponse;

return (resp.resp as pxt.editor.EditorMessageGetBlockReadableNameResponse)?.readableName;
return (resp.resp as pxt.editor.EditorMessageGetBlockAsTextResponse)?.blockAsText;
}

async runValidatorPlan(validatorPlan: pxt.blocks.ValidatorPlan, planLib: pxt.blocks.ValidatorPlan[]) {
Expand Down
16 changes: 8 additions & 8 deletions teachertool/src/components/CriteriaInstanceDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Strings, Ticks } from "../constants";
import { showModal } from "../transforms/showModal";
import { BlockPickerOptions } from "../types/modalOptions";
import { validateParameterValue } from "../utils/validateParameterValue";
import { loadBlockTextParts } from "../transforms/loadReadableBlockName";
import { loadBlockAsText } from "../transforms/loadReadableBlockName";

interface InlineInputSegmentProps {
initialValue: string;
Expand Down Expand Up @@ -86,32 +86,32 @@ interface ReadableBlockNameProps {
}
const ReadableBlockName: React.FC<ReadableBlockNameProps> = ({ blockId }) => {
const { state: teacherTool } = useContext(AppStateContext);
const [blockTextParts, setBlockTextParts] = useState<pxt.editor.BlockTextParts | undefined>(undefined);
const [blockAsText, setBlockAsText] = useState<pxt.editor.BlockAsText | undefined>(undefined);

useEffect(() => {
async function updateReadableName(blockId: string | undefined) {
let blockReadableName: pxt.editor.BlockTextParts | undefined;
let blockReadableName: pxt.editor.BlockAsText | undefined;
if (blockId) {
blockReadableName = blockId ? await loadBlockTextParts(blockId) : undefined;
blockReadableName = blockId ? await loadBlockAsText(blockId) : undefined;
}

if (blockReadableName) {
setBlockTextParts(blockReadableName);
setBlockAsText(blockReadableName);
} else if (!teacherTool.toolboxCategories) {
// If teacherTool.toolboxCategories has not loaded yet, we may get the readable component later once it loads.
// Show a spinner (handled below).
setBlockTextParts(undefined);
setBlockAsText(undefined);
} else {
// TeacherTool.toolboxCategories has loaded and we still don't have a readable component.
// We won't be able to get it, so fallback to the id.
setBlockTextParts({ parts: [ { kind: "label", content: blockId } ] });
setBlockAsText({ parts: [ { kind: "label", content: blockId } ] });
}
}

updateReadableName(blockId);
}, [blockId, teacherTool.toolboxCategories]);

const readableComponent = blockTextParts?.parts.map((part, i) => {
const readableComponent = blockAsText?.parts.map((part, i) => {
let content = "";
if (part.kind === "param") {
// Mask default values like "hello!" with generic "value"
Expand Down
4 changes: 2 additions & 2 deletions teachertool/src/services/makecodeEditorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export async function getToolboxCategoriesAsync(
return response;
}

export async function getBlockTextParts(blockId: string): Promise<pxt.editor.BlockTextParts | undefined> {
const response = driver ? await driver.getBlockTextParts(blockId) : undefined;
export async function getBlockAsText(blockId: string): Promise<pxt.editor.BlockAsText | undefined> {
const response = driver ? await driver.getBlockAsText(blockId) : undefined;
return response;
}

Expand Down
14 changes: 7 additions & 7 deletions teachertool/src/services/storageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const RUN_ON_LOAD_KEY = [KEY_PREFIX, "runOnLoad"].join("/");
const LAST_ACTIVE_CHECKLIST_KEY = [KEY_PREFIX, "lastActiveChecklist"].join("/");
const SPLIT_POSITION_KEY = [KEY_PREFIX, "splitPosition"].join("/");
const EXPANDED_CATALOG_TAGS_KEY = [KEY_PREFIX, "expandedCatalogTags"].join("/");
const BLOCK_TEXT_PARTS_PREFIX = [KEY_PREFIX, "blockTextParts"].join("/");
const BLOCK_AS_TEXT_PREFIX = [KEY_PREFIX, "blockAsText"].join("/");

function getValue(key: string, defaultValue?: string): string | undefined {
return localStorage.getItem(key) || defaultValue;
Expand Down Expand Up @@ -113,8 +113,8 @@ async function deleteChecklistFromIndexedDbAsync(name: string) {
await db.deleteChecklist(name);
}

function getBlockTextPartsKey(blockId: string): string {
return [BLOCK_TEXT_PARTS_PREFIX, blockId].join("/");
function getBlockAsTextKey(blockId: string): string {
return [BLOCK_AS_TEXT_PREFIX, blockId].join("/");
}

// ----------------------------------
Expand Down Expand Up @@ -241,8 +241,8 @@ export function removeExpandedCatalogTag(tag: string) {
}
}

export function getCachedBlockTextParts(blockId: string): pxt.editor.BlockTextParts | undefined {
const key = getBlockTextPartsKey(blockId);
export function getCachedBlockAsText(blockId: string): pxt.editor.BlockAsText | undefined {
const key = getBlockAsTextKey(blockId);
try {
const cachedReadableBlockName = getValue(key);
return cachedReadableBlockName ? JSON.parse(cachedReadableBlockName) : undefined;
Expand All @@ -252,8 +252,8 @@ export function getCachedBlockTextParts(blockId: string): pxt.editor.BlockTextPa
}
}

export function cacheBlockTextParts(blockId: string, readableBlockName: pxt.editor.BlockTextParts) {
const key = getBlockTextPartsKey(blockId);
export function cacheBlockAsText(blockId: string, readableBlockName: pxt.editor.BlockAsText) {
const key = getBlockAsTextKey(blockId);
try {
setValue(key, JSON.stringify(readableBlockName));
} catch (e) {
Expand Down
12 changes: 6 additions & 6 deletions teachertool/src/transforms/loadReadableBlockName.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { getBlockTextParts } from "../services/makecodeEditorService";
import { cacheBlockTextParts, getCachedBlockTextParts } from "../services/storageService";
import { getBlockAsText } from "../services/makecodeEditorService";
import { cacheBlockAsText, getCachedBlockAsText } from "../services/storageService";

export async function loadBlockTextParts(blockId: string): Promise<pxt.editor.BlockTextParts | undefined> {
export async function loadBlockAsText(blockId: string): Promise<pxt.editor.BlockAsText | undefined> {
// Check for cached version.
let readableBlockName = getCachedBlockTextParts(blockId);
let readableBlockName = getCachedBlockAsText(blockId);
if (readableBlockName) {
return Promise.resolve(readableBlockName);
}

// Call into editor service & cache result
readableBlockName = await getBlockTextParts(blockId);
readableBlockName = await getBlockAsText(blockId);
if (readableBlockName) {
cacheBlockTextParts(blockId, readableBlockName);
cacheBlockAsText(blockId, readableBlockName);
}

return readableBlockName;
Expand Down
6 changes: 3 additions & 3 deletions webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4156,14 +4156,14 @@ export class ProjectView
return matches;
}

getBlockTextParts(blockId: string): pxt.editor.BlockTextParts | undefined {
getBlockAsText(blockId: string): pxt.editor.BlockAsText | undefined {
const blocksWithId = this.getBlocksWithId(blockId);
let readableName: pxt.editor.BlockTextParts = undefined;
let readableName: pxt.editor.BlockAsText = undefined;
if (blocksWithId.length === 0) {
return undefined;
} else if (blocksWithId.length === 1) {
const block = blocksWithId[0];
readableName = toolboxHelpers.getBlockTextParts(block, block.parameters ? block.parameters.slice() : null, false);
readableName = toolboxHelpers.getBlockAsText(block, block.parameters ? block.parameters.slice() : null, false);
}

if (!readableName) {
Expand Down
8 changes: 4 additions & 4 deletions webapp/src/monacoFlyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as data from "./data";
import * as auth from "./auth";
import * as pxtblockly from "../../pxtblocks";
import { HELP_IMAGE_URI } from "../../pxteditor";
import { getBlockTextParts } from "./toolboxHelpers";
import { getBlockAsText } from "./toolboxHelpers";

import ISettingsProps = pxt.editor.ISettingsProps;

Expand Down Expand Up @@ -257,9 +257,9 @@ export class MonacoFlyout extends data.Component<MonacoFlyoutProps, MonacoFlyout
let name = block.qName || block.name;
const isPython = this.props.fileType == pxt.editor.FileType.Python;

const blockTextParts = getBlockTextParts(block, params, isPython);
if (blockTextParts?.parts?.length) {
blockTextParts.parts?.forEach((part, i) => {
const blockAsText = getBlockAsText(block, params, isPython);
if (blockAsText?.parts?.length) {
blockAsText.parts?.forEach((part, i) => {
switch (part.kind) {
case "label":
description.push(<span key={name + i}>{part.content}</span>);
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/toolboxHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as toolbox from "./toolbox";

// 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 {
export function getBlockAsText(block: toolbox.BlockDefinition, params: pxtc.ParameterDesc[], isPython: boolean): pxt.editor.BlockAsText | undefined {
let description: pxt.editor.BlockTextPart[] = [];
let compileInfo = pxt.blocks.compileInfo(block as pxtc.SymbolInfo);
let parts = block.attributes._def && block.attributes._def.parts;
Expand Down

0 comments on commit cae7c4d

Please sign in to comment.