Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply config #456

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e/specs/starter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ test.describe('Starter Templates from UI', () => {

await waitForEditorFocus(app);
await waitForResultUpdate();
await app.waitForTimeout(3_000);
await app.waitForTimeout(6_000);

const titleText = await getResult().innerText('h1');
expect(titleText).toBe('Hello, PHP!');
Expand Down Expand Up @@ -788,7 +788,7 @@ test.describe('Starter Templates from URL', () => {

await waitForEditorFocus(app);
await waitForResultUpdate();
await app.waitForTimeout(3_000);
await app.waitForTimeout(6_000);

const titleText = await getResult().innerText('h1');
expect(titleText).toBe('Hello, PHP!');
Expand Down
62 changes: 41 additions & 21 deletions src/livecodes/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,18 +1166,38 @@ const loadConfig = async (

// load config
await bootstrap(true);

updateUI(config);
await applyConfig(config);

changingContent = false;
};

const updateUI = (config: Config) => {
window.deps.showMode(config.mode);
configureToolsPane(config.tools, config.mode);
if (config.autotest) {
const applyConfig = async (newConfig: Partial<Config>) => {
if (!isEmbed) {
loadSettings(getConfig());
}
if (newConfig.mode) {
window.deps.showMode(newConfig.mode);
}
if (newConfig.tools) {
configureToolsPane(newConfig.tools, newConfig.mode);
}
if (newConfig.zoom) {
zoom(newConfig.zoom);
}
if (newConfig.theme) {
setTheme(newConfig.theme);
}
if (newConfig.autotest) {
UI.getWatchTestsButton()?.classList.remove('disabled');
}
const editorConfig = {
...getEditorConfig(newConfig as Config),
...getFormatterConfig(newConfig as Config),
};
const hasEditorConfig = Object.values(editorConfig).some((value) => value != null);
if (hasEditorConfig) {
await reloadEditors({ ...getConfig(), ...newConfig });
}
};

const setUserConfig = (newConfig: Partial<UserConfig> | null, save = true) => {
Expand Down Expand Up @@ -3719,29 +3739,30 @@ const configureToolsPane = (
tools: Config['tools'] | undefined,
mode: Config['mode'] | undefined,
) => {
if (!toolsPane) return;
if (mode === 'result' && (!tools || tools.status === '' || tools.status === 'none')) {
toolsPane?.hide();
toolsPane.hide();
return;
}
if (tools?.active) {
toolsPane?.setActiveTool(tools.active);
toolsPane.setActiveTool(tools.active);
}
if (!tools) {
toolsPane?.close();
toolsPane.close();
return;
}
if (tools.status === 'none') {
toolsPane?.hide();
toolsPane.hide();
return;
}
if (tools.status === 'full') {
toolsPane?.maximize();
toolsPane.maximize();
}
if (tools.status === 'open') {
toolsPane?.open();
toolsPane.open();
}
if (tools.status === 'closed' || tools.status === '') {
toolsPane?.close();
toolsPane.close();
}
// TODO: handle tools.enabled
};
Expand Down Expand Up @@ -4139,18 +4160,17 @@ const createApi = (): API => {
};

const apiSetConfig = async (newConfig: Partial<Config>): Promise<Config> => {
const newAppConfig = {
const newAppConfig: Config = {
...getConfig(),
...buildConfig(newConfig),
};

// TODO: apply changes in App AppConfig, UserConfig & EditorConfig
if (newAppConfig.mode !== getConfig().mode) {
window.deps.showMode(newAppConfig.mode);
}

setConfig(newAppConfig);
await loadConfig(newAppConfig);
await applyConfig(newConfig);
const content = getContentConfig(newConfig as Config);
const hasContent = Object.values(content).some((value) => value != null);
if (hasContent) {
await loadConfig(newAppConfig);
}
return newAppConfig;
};

Expand Down
3 changes: 2 additions & 1 deletion src/livecodes/languages/ruby-wasm/lang-ruby-wasm-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ livecodes.rubyWasm.run =
await init(hasImports(code));
const { DefaultRubyVM } = (window as any)['ruby-wasm-wasi'];
const { vm } = await DefaultRubyVM(livecodes.rubyWasm.module);
vm.eval(code);
const patch = code.includes('$0') ? '$0 = __FILE__\\n' : '';
vm.eval(patch + code);
parent.postMessage({ type: 'loading', payload: false }, '*');
});

Expand Down
3 changes: 2 additions & 1 deletion src/livecodes/languages/ruby/lang-ruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const ruby: LanguageSpecs = {
'ruby',
config,
);
return (self as any).Opal.compile(code, options);
const patch = code.includes('$0') ? '$0 = __FILE__\\n' : '';
return (self as any).Opal.compile(patch + code, options);
};
},
scripts: ({ compiled, config }) => {
Expand Down
1 change: 1 addition & 0 deletions src/livecodes/templates/starter/php-wasm-starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ body {
script: {
language: 'php-wasm',
content: `
<?php
$title = 'PHP';
vrzno_eval('document.getElementById("title").innerText = "' . $title . '"');
Expand Down