Skip to content

Commit

Permalink
let editor extensions contribute experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Oct 31, 2024
1 parent e3e9ec7 commit be52bbc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
12 changes: 12 additions & 0 deletions localtypings/pxteditor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,7 @@ declare namespace pxt.editor {
blocklyToolbox: ToolboxDefinition;
monacoToolbox: ToolboxDefinition;
projectView: IProjectView;
showNotification: (msg) => void;
}

export interface IToolboxOptions {
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 19 additions & 10 deletions pxteditor/experiments.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> = <pxt.Map<boolean>><any>pxt.savedAppTheme();
const r: pxt.Map<string | number> = {};
Expand All @@ -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"),
Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5858,7 +5858,8 @@ function initExtensionsAsync(): Promise<void> {
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))
Expand Down
4 changes: 4 additions & 0 deletions webapp/src/cmds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -397,6 +398,9 @@ function applyExtensionResult() {
log(`extension onMarkdownActivityLoad`);
pxt.commands.onMarkdownActivityLoad = res.onMarkdownActivityLoad;
}
if (res.experiments) {
setEditorExtensionExperiments(res.experiments);
}
}

export async function initAsync() {
Expand Down
6 changes: 3 additions & 3 deletions webapp/src/scriptsearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class ScriptSearch extends data.Component<ISettingsProps, ScriptSearchSta

}

fetchExperiments(): experiments.Experiment[] {
fetchExperiments(): pxt.editor.Experiment[] {
if (this.state.mode != ScriptSearchMode.Experiments) return [];
return experiments.all();
}
Expand Down Expand Up @@ -309,7 +309,7 @@ export class ScriptSearch extends data.Component<ISettingsProps, ScriptSearchSta
}
}

toggleExperiment(experiment: experiments.Experiment) {
toggleExperiment(experiment: pxt.editor.Experiment) {
experiments.toggle(experiment);
pxt.tickEvent(`experiments.toggle`, { "experiment": experiment.id, "enabled": experiments.isEnabled(experiment) ? 1 : 0 }, { interactiveConsent: true })
this.forceUpdate();
Expand Down Expand Up @@ -505,7 +505,7 @@ export class ScriptSearch extends data.Component<ISettingsProps, ScriptSearchSta
description={experiment.description}
key={'exp' + experiment.id}
role="button"
label={experiments.isEnabled(experiment) ? lf("Enabled") : lf("Disabled")}
label={experiment.onClick ? undefined : (experiments.isEnabled(experiment) ? lf("Enabled") : lf("Disabled"))}
labelClass={experiments.isEnabled(experiment) ? "green right ribbon" : "grey right ribbon"}
onCardClick={this.toggleExperiment}
feedbackUrl={experiment.feedbackUrl}
Expand Down

0 comments on commit be52bbc

Please sign in to comment.