From be52bbc0cbe4d095186899461bf1a5735f53ab60 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Thu, 31 Oct 2024 10:06:21 -0700 Subject: [PATCH] let editor extensions contribute experiments --- localtypings/pxteditor.d.ts | 12 ++++++++++++ pxteditor/experiments.ts | 29 +++++++++++++++++++---------- webapp/src/app.tsx | 3 ++- webapp/src/cmds.ts | 4 ++++ webapp/src/scriptsearch.tsx | 6 +++--- 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/localtypings/pxteditor.d.ts b/localtypings/pxteditor.d.ts index d174cd25a3a..0364180fa5a 100644 --- a/localtypings/pxteditor.d.ts +++ b/localtypings/pxteditor.d.ts @@ -1120,6 +1120,7 @@ declare namespace pxt.editor { blocklyToolbox: ToolboxDefinition; monacoToolbox: ToolboxDefinition; projectView: IProjectView; + showNotification: (msg) => void; } export interface IToolboxOptions { @@ -1152,6 +1153,17 @@ declare namespace pxt.editor { // Used with @codeStart, @codeStop metadata (MINECRAFT HOC ONLY) onCodeStart?: () => void; onCodeStop?: () => void; + + experiments?: Experiment[]; + } + + export interface Experiment { + id: string; // == field in apptheme also assumes image at /static/experiments/ID.png + name: string; + description: string; + feedbackUrl?: string; // allows user to put feedback + enableOnline?: boolean; // requires internet connection, disable in offline app + onClick?: () => void; // code to run when the experiment is clicked } export interface FieldExtensionOptions { diff --git a/pxteditor/experiments.ts b/pxteditor/experiments.ts index 7c236ad92ee..a9724f2dd87 100644 --- a/pxteditor/experiments.ts +++ b/pxteditor/experiments.ts @@ -1,16 +1,16 @@ -export interface Experiment { - id: string; // == field in apptheme also assumes image at /static/experiments/ID.png - name: string; - description: string; - feedbackUrl?: string; // allows user to put feedback - enableOnline?: boolean; // requires internet connection, disable in offline app -} +import Experiment = pxt.editor.Experiment; function key(experiment: Experiment | string): string { const id = (typeof experiment === "object") ? experiment.id : experiment; return `experiments-${id}` } +let editorExtensionExperiments: Experiment[]; + +export function setEditorExtensionExperiments(experiments: Experiment[]) { + editorExtensionExperiments = experiments; +} + export function syncTheme() { const theme: pxt.Map = >pxt.savedAppTheme(); const r: pxt.Map = {}; @@ -31,7 +31,7 @@ export function syncTheme() { export function all(): Experiment[] { const ids = pxt.appTarget.appTheme.experiments; if (!ids) return []; - return [ + const exps: Experiment[] = [ { id: "print", name: lf("Print Code"), @@ -182,7 +182,11 @@ export function all(): Experiment[] { name: lf("Time Machine"), description: lf("Save and restore past versions of a project") }, - ].filter(experiment => ids.indexOf(experiment.id) > -1 && !(pxt.BrowserUtils.isPxtElectron() && experiment.enableOnline)); + ]; + + return exps.filter(experiment => ids.indexOf(experiment.id) > -1) + .concat(editorExtensionExperiments || []) + .filter(experiment => !(pxt.BrowserUtils.isPxtElectron() && experiment.enableOnline)); } export function clear() { @@ -199,7 +203,12 @@ export function isEnabled(experiment: Experiment | string): boolean { } export function toggle(experiment: Experiment) { - setState(experiment, !isEnabled(experiment)); + if (experiment.onClick) { + experiment.onClick(); + } + else { + setState(experiment, !isEnabled(experiment)); + } } export function state(): string { diff --git a/webapp/src/app.tsx b/webapp/src/app.tsx index fbd47b242eb..cb1d1eebce7 100644 --- a/webapp/src/app.tsx +++ b/webapp/src/app.tsx @@ -5858,7 +5858,8 @@ function initExtensionsAsync(): Promise { const opts: pxt.editor.ExtensionOptions = { blocklyToolbox: blocklyToolbox.getToolboxDefinition(), monacoToolbox: monacoToolbox.getToolboxDefinition(), - projectView: theEditor + projectView: theEditor, + showNotification: (msg) => core.infoNotification(msg) }; return pxt.BrowserUtils.loadScriptAsync("editor.js") .then(() => pxt.editor.initExtensionsAsync(opts)) diff --git a/webapp/src/cmds.ts b/webapp/src/cmds.ts index 08936037bf4..273c714b031 100644 --- a/webapp/src/cmds.ts +++ b/webapp/src/cmds.ts @@ -12,6 +12,7 @@ import * as pxtblockly from "../../pxtblocks"; import ExtensionResult = pxt.editor.ExtensionResult; import NativeHostMessage = pxt.editor.NativeHostMessage; +import { setEditorExtensionExperiments } from "../../pxteditor/experiments"; function log(msg: string) { @@ -397,6 +398,9 @@ function applyExtensionResult() { log(`extension onMarkdownActivityLoad`); pxt.commands.onMarkdownActivityLoad = res.onMarkdownActivityLoad; } + if (res.experiments) { + setEditorExtensionExperiments(res.experiments); + } } export async function initAsync() { diff --git a/webapp/src/scriptsearch.tsx b/webapp/src/scriptsearch.tsx index 13d6c5c1e40..9fa9330ba65 100644 --- a/webapp/src/scriptsearch.tsx +++ b/webapp/src/scriptsearch.tsx @@ -204,7 +204,7 @@ export class ScriptSearch extends data.Component