Skip to content

Commit

Permalink
feat(SDK): apply config (app & editor configs) from SDK setConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Oct 17, 2023
1 parent bc2a62d commit 309ffb7
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/livecodes/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1167,17 +1167,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 @@ -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

0 comments on commit 309ffb7

Please sign in to comment.