Skip to content

Commit

Permalink
feat(Config): make Config.tools fields optional
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Apr 16, 2024
1 parent cf0e3bb commit 42342a7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/docs/configuration/configuration-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ Sets [display mode](../features/display-modes.md)

### `tools`

Type: [`{ enabled: Array<'console' | 'compiled' | 'tests'> | 'all'; active: 'console' | 'compiled' | 'tests' | ''; status: 'closed' | 'open' | 'full' | 'none' | ''; }`](../api/interfaces/Config.md#tools)
Type: [`Partial<{ enabled: Array<'console' | 'compiled' | 'tests'> | 'all'; active: 'console' | 'compiled' | 'tests' | ''; status: 'closed' | 'open' | 'full' | 'none' | ''; }>`](../api/interfaces/Config.md#tools)

Default: `{ enabled: 'all', active: '', status: '' }`

Expand Down
10 changes: 7 additions & 3 deletions src/livecodes/config/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,11 @@ export const loadParamConfig = (config: Config, params: UrlQueryParams): Partial
}

if (!status && ['open', 'full', 'closed'].includes(params[tool]!)) {
if (paramsConfig.tools.enabled !== 'all' && !paramsConfig.tools.enabled.includes(tool)) {
if (
paramsConfig.tools.enabled &&
paramsConfig.tools.enabled !== 'all' &&
!paramsConfig.tools.enabled.includes(tool)
) {
paramsConfig.tools.enabled.push(tool);
}
paramsConfig.tools.active = tool;
Expand All @@ -273,9 +277,9 @@ export const loadParamConfig = (config: Config, params: UrlQueryParams): Partial
if (paramsConfig.tools.enabled === 'all') {
paramsConfig.tools.enabled = [...allTools];
}
paramsConfig.tools.enabled = paramsConfig.tools.enabled.filter((t) => t !== tool);
paramsConfig.tools.enabled = paramsConfig.tools.enabled?.filter((t) => t !== tool) || [];
if (paramsConfig.tools.active === tool) {
paramsConfig.tools.active = paramsConfig.tools.enabled[0];
paramsConfig.tools.active = paramsConfig.tools.enabled?.[0] || '';
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/livecodes/config/validate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const validateConfig = (config: Partial<Config>): Partial<Config> => {
...(x && is(x.position, 'object') ? { position: x.position } : {}),
});

const validateToolsProps = (x: Partial<Config['tools']>): Config['tools'] => ({
const validateToolsProps = (x: Config['tools']): Config['tools'] => ({
...defaultConfig.tools,
...(x && Array.isArray(x.enabled)
? { enabled: x.enabled.filter((t) => tools.includes(t)) }
Expand Down
4 changes: 2 additions & 2 deletions src/livecodes/toolspane/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const createToolsPane = (
];

const isEnabled = (tool: ToolList[number]) =>
config.tools.enabled === 'all' || config.tools.enabled.includes(tool.name);
config.tools.enabled === 'all' || config.tools.enabled?.includes(tool.name) === true;

const toolList: ToolList = fullList.filter(isEnabled);

Expand Down Expand Up @@ -344,7 +344,7 @@ export const createToolsPane = (

const load = async () => {
const initialLoad = status === undefined;
activeToolId = getToolId(config.tools.active);
activeToolId = getToolId(config.tools.active || '');
status = config.tools.status || 'closed';

if (initialLoad) {
Expand Down
4 changes: 2 additions & 2 deletions src/sdk/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ export interface AppConfig {
readonly: boolean;
allowLangChange: boolean;
mode: 'full' | 'focus' | 'simple' | 'editor' | 'codeblock' | 'result';
tools: {
tools: Partial<{
enabled: Array<Tool['name']> | 'all';
active: Tool['name'] | '';
status: ToolsPaneStatus;
};
}>;
zoom: 1 | 0.5 | 0.25;
}

Expand Down

0 comments on commit 42342a7

Please sign in to comment.