Skip to content

Commit

Permalink
remove old blocks from pxtrunner
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Jan 25, 2024
1 parent a553994 commit 1d0ebaa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
9 changes: 4 additions & 5 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ const pxtembed = () => gulp.src([
"built/pxtlib.js",
"built/pxtcompiler.js",
"built/pxtpy.js",
"built/pxtblockly.js",
"built/pxteditor.js",
"built/pxtsim.js",
"built/web/runnerembed.js"
Expand Down Expand Up @@ -126,11 +125,11 @@ function initWatch() {
gulp.parallel(pxtcompiler, pxtsim, backendutils),
gulp.parallel(pxtpy, gulp.series(copyBlockly, pxtblocks, pxtblockly)),
pxteditor,
gulp.parallel(pxtrunner, cli, pxtcommon),
gulp.parallel(pxtrunner, cli, pxtcommon, newBlocks),
gulp.parallel(updatestrings, browserifyEmbed),
gulp.parallel(pxtjs, pxtdts, pxtapp, pxtworker, pxtembed),
targetjs,
gulp.parallel(reactCommon, newBlocks),
reactCommon,
webapp,
browserifyWebapp,
browserifyAssetEditor,
Expand Down Expand Up @@ -846,11 +845,11 @@ const buildAll = gulp.series(
gulp.parallel(pxtcompiler, pxtsim, backendutils),
gulp.parallel(pxtpy, gulp.series(copyBlockly, pxtblocks, pxtblockly)),
pxteditor,
gulp.parallel(pxtrunner, cli, pxtcommon),
gulp.parallel(pxtrunner, cli, pxtcommon, newBlocks),
browserifyEmbed,
gulp.parallel(pxtjs, pxtdts, pxtapp, pxtworker, pxtembed),
targetjs,
gulp.parallel(reactCommon, newBlocks),
reactCommon,
gulp.parallel(buildcss, buildSVGIcons),
maybeBuildWebapps(),
webapp,
Expand Down
35 changes: 20 additions & 15 deletions pxtrunner/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { DecompileResult, compileBlocksAsync, decompileSnippetAsync, renderProjectAsync } from "./runner";

import * as Blockly from "blockly";

import { BlockLayout, BlocksRenderOptions, cleanRenderingWorkspace, decompiledDiffAsync, diffXml, render } from "../newblocks";
import { CodeCardRenderOptions, renderCodeCard } from "../newblocks/codecardRenderer";

const JS_ICON = "icon xicon js";
const PY_ICON = "icon xicon python";
const BLOCKS_ICON = "icon xicon blocks";
Expand Down Expand Up @@ -363,15 +368,15 @@ function fillWithWidget(
let renderQueue: {
el: JQuery;
source: string;
options: pxt.blocks.BlocksRenderOptions;
options: BlocksRenderOptions;
render: (container: JQuery, r: DecompileResult) => void;
}[] = [];
function consumeRenderQueueAsync(): Promise<void> {
const existingFilters: pxt.Map<boolean> = {};
return consumeNext()
.then(() => {
Blockly.Workspace.getAll().forEach(el => el.dispose());
pxt.blocks.cleanRenderingWorkspace();
cleanRenderingWorkspace();
});

function consumeNext(): Promise<void> {
Expand Down Expand Up @@ -407,14 +412,14 @@ function consumeRenderQueueAsync(): Promise<void> {

function renderNextSnippetAsync(cls: string,
render: (container: JQuery, r: DecompileResult) => void,
options?: pxt.blocks.BlocksRenderOptions): Promise<void> {
options?: BlocksRenderOptions): Promise<void> {
if (!cls) return Promise.resolve();

let $el = $("." + cls).first();
if (!$el[0]) return Promise.resolve();

if (!options.emPixels) options.emPixels = 18;
if (!options.layout) options.layout = pxt.blocks.BlockLayout.Align;
if (!options.layout) options.layout = BlockLayout.Align;
options.splitSvg = true;

renderQueue.push({ el: $el, source: $el.text(), options, render });
Expand Down Expand Up @@ -481,7 +486,7 @@ function renderSignaturesAsync(options: ClientRenderOptions): Promise<void> {
let block = Blockly.Blocks[symbolInfo.attributes.blockId];
let xml = block?.codeCard?.blocksXml || undefined;

const blocksHtml = xml ? pxt.blocks.render(xml) : r.compileBlocks?.success ? r.blocksSvg : undefined;
const blocksHtml = xml ? render(xml) : r.compileBlocks?.success ? r.blocksSvg : undefined;
const s = blocksHtml ? $(blocksHtml as HTMLElement) : undefined
let jsSig = ts.pxtc.service.displayStringForSymbol(symbolInfo, /** python **/ false, r.apiInfo)
.split("\n")[1] + ";";
Expand Down Expand Up @@ -536,7 +541,7 @@ function renderBlocksXmlAsync(opts: ClientRenderOptions): Promise<void> {
const cls = opts.blocksXmlClass;
function renderNextXmlAsync(cls: string,
render: (container: JQuery, r: DecompileResult) => void,
options?: pxt.blocks.BlocksRenderOptions): Promise<void> {
options?: BlocksRenderOptions): Promise<void> {
let $el = $("." + cls).first();
if (!$el[0]) return Promise.resolve();

Expand Down Expand Up @@ -568,7 +573,7 @@ function renderDiffBlocksXmlAsync(opts: ClientRenderOptions): Promise<void> {
const cls = opts.diffBlocksXmlClass;
function renderNextXmlAsync(cls: string,
render: (container: JQuery, r: DecompileResult) => void,
options?: pxt.blocks.BlocksRenderOptions): Promise<void> {
options?: BlocksRenderOptions): Promise<void> {
let $el = $("." + cls).first();
if (!$el[0]) return Promise.resolve();

Expand All @@ -583,7 +588,7 @@ function renderDiffBlocksXmlAsync(opts: ClientRenderOptions): Promise<void> {
.then(r => {
$el.removeClass(cls);
try {
const diff = pxt.blocks.diffXml(oldXml, newXml);
const diff = diffXml(oldXml, newXml);
if (!diff)
$el.text("no changes");
else {
Expand Down Expand Up @@ -654,7 +659,7 @@ function renderDiffBlocksAsync(opts: ClientRenderOptions): Promise<void> {
}))
.then(resps => {
try {
const diffBlocks = pxt.blocks.decompiledDiffAsync(
const diffBlocks = decompiledDiffAsync(
oldSrc, resps[0].compileBlocks, newSrc, resps[1].compileBlocks, {
hideDeletedTopBlocks: true,
hideDeletedBlocks: true
Expand Down Expand Up @@ -756,7 +761,7 @@ function renderNamespaces(options: ClientRenderOptions): Promise<void> {
});
}

function renderInlineBlocksAsync(options: pxt.blocks.BlocksRenderOptions): Promise<void> {
function renderInlineBlocksAsync(options: BlocksRenderOptions): Promise<void> {
options = pxt.Util.clone(options);
options.emPixels = 18;
options.snippetMode = true;
Expand Down Expand Up @@ -883,7 +888,7 @@ function addCardItem(ul: JQuery, card: pxt.CodeCard) {
const mP = /^\/(v\d+)/.exec(window.location.pathname);
const inEditor = /#doc/i.test(window.location.href);
if (card.url && !mC && mP && !inEditor) card.url = `/${mP[1]}/${card.url}`;
ul.append(pxt.docs.codeCard.render(card, { hideHeader: true, shortName: true }));
ul.append(renderCodeCard(card, { hideHeader: true, shortName: true }));
}

function addSymbolCardItem(ul: JQuery, symbol: pxtc.SymbolInfo, cardStyle?: string) {
Expand Down Expand Up @@ -1042,11 +1047,11 @@ function renderLinksAsync(options: ClientRenderOptions, cls: string, replacePare
}, { package: options.package, aspectRatio: options.blocksAspectRatio, assets: options.assetJSON })
}

function fillCodeCardAsync(c: JQuery, cards: pxt.CodeCard[], options: pxt.docs.codeCard.CodeCardRenderOptions): Promise<void> {
function fillCodeCardAsync(c: JQuery, cards: pxt.CodeCard[], options: CodeCardRenderOptions): Promise<void> {
if (!cards || cards.length == 0) return Promise.resolve();

if (cards.length == 0) {
let cc = pxt.docs.codeCard.render(cards[0], options)
let cc = renderCodeCard(cards[0], options)
c.replaceWith(cc);
} else {
let cd = document.createElement("div")
Expand All @@ -1058,7 +1063,7 @@ function fillCodeCardAsync(c: JQuery, cards: pxt.CodeCard[], options: pxt.docs.c
const mP = /^\/(v\d+)/.exec(window.location.pathname);
const inEditor = /#doc/i.test(window.location.href);
if (card.url && !mC && mP && !inEditor) card.url = `/${mP[1]}${card.url}`;
const cardEl = pxt.docs.codeCard.render(card, options);
const cardEl = renderCodeCard(card, options);
cd.appendChild(cardEl)
// automitcally display package icon for approved packages
if (card.cardType == "package") {
Expand All @@ -1074,7 +1079,7 @@ function fillCodeCardAsync(c: JQuery, cards: pxt.CodeCard[], options: pxt.docs.c
// update card info
card.imageUrl = pxt.github.mkRepoIconUrl(repoId);
// inject
cd.insertBefore(pxt.docs.codeCard.render(card, options), cardEl);
cd.insertBefore(renderCodeCard(card, options), cardEl);
cardEl.remove();
break;
}
Expand Down
18 changes: 9 additions & 9 deletions pxtrunner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/// <reference path="../built/pxtblocks.d.ts" />
/// <reference path="../built/pxtsim.d.ts" />

import { BlocksRenderOptions, blocklyToSvgAsync, initializeAndInject, render } from "../newblocks";
import { defaultClientRenderOptions, renderAsync } from "./renderer";

export interface SimulateOptions {
Expand Down Expand Up @@ -660,21 +661,20 @@ export function startRenderServer() {
const msg = jobQueue.shift();
if (!msg) return; // no more work

const options = (msg.options || {}) as pxt.blocks.BlocksRenderOptions;
const options = (msg.options || {}) as BlocksRenderOptions;
options.splitSvg = false; // don't split when requesting rendered images
pxt.tickEvent("renderer.job")
const isXml = /^\s*<xml/.test(msg.code);

const doWork = async () => {
await pxt.BrowserUtils.loadBlocklyAsync();
const result = isXml
? await compileBlocksAsync(msg.code, options)
: await decompileSnippetAsync(msg.code, msg.options);
const blocksSvg = result.blocksSvg as SVGSVGElement;
const width = blocksSvg.viewBox.baseVal.width;
const height = blocksSvg.viewBox.baseVal.height;
const res = blocksSvg
? await pxt.blocks.layout.blocklyToSvgAsync(blocksSvg, 0, 0, width, height)
? await blocklyToSvgAsync(blocksSvg, 0, 0, width, height)
: undefined;
// try to render to png
let png: string;
Expand Down Expand Up @@ -1155,7 +1155,7 @@ export interface DecompileResult {
let programCache: ts.Program;
let apiCache: pxt.Map<pxtc.ApisInfo>;

export function decompileSnippetAsync(code: string, options?: pxt.blocks.BlocksRenderOptions): Promise<DecompileResult> {
export function decompileSnippetAsync(code: string, options?: BlocksRenderOptions): Promise<DecompileResult> {
const { assets, forceCompilation, snippetMode, generateSourceMap } = options || {};

// code may be undefined or empty!!!
Expand Down Expand Up @@ -1200,7 +1200,7 @@ export function decompileSnippetAsync(code: string, options?: pxt.blocks.BlocksR
return ts.pxtc.localizeApisAsync(apis, mainPkg)
.then(() => {
let blocksInfo = pxtc.getBlocksInfo(apis);
pxt.blocks.initializeAndInject(blocksInfo);
initializeAndInject(blocksInfo);
const tilemapJres = assets?.[pxt.TILEMAP_JRES];
const assetsJres = assets?.[pxt.IMAGES_JRES];
if (tilemapJres || assetsJres) {
Expand Down Expand Up @@ -1230,7 +1230,7 @@ export function decompileSnippetAsync(code: string, options?: pxt.blocks.BlocksR
};
pxt.debug(bresp.outfiles[pxt.MAIN_BLOCKS])

const blocksSvg = pxt.blocks.render(bresp.outfiles[pxt.MAIN_BLOCKS], options);
const blocksSvg = render(bresp.outfiles[pxt.MAIN_BLOCKS], options);

if (tilemapJres || assetsJres) {
tilemapProject = null;
Expand Down Expand Up @@ -1259,7 +1259,7 @@ function getApiInfo(program: ts.Program, opts: pxtc.CompileOptions) {
return apiCache[key];
}

export function compileBlocksAsync(code: string, options?: pxt.blocks.BlocksRenderOptions): Promise<DecompileResult> {
export function compileBlocksAsync(code: string, options?: BlocksRenderOptions): Promise<DecompileResult> {
const { assets } = options || {};

const packageid = options && options.packageId ? "pub:" + options.packageId :
Expand All @@ -1282,7 +1282,7 @@ export function compileBlocksAsync(code: string, options?: pxt.blocks.BlocksRend
return ts.pxtc.localizeApisAsync(apis, mainPkg)
.then(() => {
const blocksInfo = pxtc.getBlocksInfo(apis);
pxt.blocks.initializeAndInject(blocksInfo);
initializeAndInject(blocksInfo);

const tilemapJres = assets?.[pxt.TILEMAP_JRES];
const assetsJres = assets?.[pxt.IMAGES_JRES];
Expand All @@ -1294,7 +1294,7 @@ export function compileBlocksAsync(code: string, options?: pxt.blocks.BlocksRend
if (assetsJres)
tilemapProject.loadAssetsJRes(JSON.parse(assetsJres))
}
const blockSvg = pxt.blocks.render(code, options);
const blockSvg = render(code, options);

if (tilemapJres || assetsJres) {
tilemapProject = null;
Expand Down

0 comments on commit 1d0ebaa

Please sign in to comment.