Skip to content

Commit 45b5277

Browse files
authored
Merge pull request #743 from live-codes/fixes
Fixes
2 parents 12e76e8 + cd33134 commit 45b5277

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

src/livecodes/UI/modal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createAccordion } from './accordion';
55

66
export const createModal = (deps: {
77
translate: (container: HTMLElement) => void;
8+
isEmbed: boolean;
89
onClose: () => void;
910
}): Modal => {
1011
const overlay = document.querySelector('#overlay') as HTMLElement;
@@ -21,7 +22,7 @@ export const createModal = (deps: {
2122
isAsync = false,
2223
onClose = () => undefined,
2324
scrollToSelector = '',
24-
autoFocus = true,
25+
autoFocus = !deps.isEmbed,
2526
}: ModalOptions = {},
2627
) => {
2728
modal.className = size;

src/livecodes/core.ts

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ const loadModuleTypes = async (
364364
if (typeof editors?.script?.addTypes !== 'function') return;
365365
const scriptLanguage = config.script.language;
366366
if (['typescript', 'javascript'].includes(mapLanguage(scriptLanguage)) || force) {
367+
if (compiler.isFake) {
368+
// we need the real compiler for types
369+
await reloadCompiler({ ...config, mode: 'full' });
370+
}
367371
const configTypes = {
368372
...getLanguageCompiler(config.markup.language)?.types,
369373
...getLanguageCompiler(config.script.language)?.types,
@@ -449,7 +453,9 @@ const createCopyButtons = () => {
449453
};
450454

451455
const createEditors = async (config: Config) => {
456+
let isReload = false;
452457
if (editors) {
458+
isReload = true;
453459
Object.values(editors).forEach((editor: CodeEditor) => editor.destroy());
454460
resetEditorModeStatus();
455461
}
@@ -539,6 +545,10 @@ const createEditors = async (config: Config) => {
539545
if (config.mode === 'codeblock') {
540546
createCopyButtons();
541547
}
548+
549+
if (isReload) {
550+
loadModuleTypes(editors, config, /* loadAll = */ true);
551+
}
542552
};
543553

544554
const reloadEditors = async (config: Config) => {
@@ -1057,6 +1067,17 @@ const getResultPage = async ({
10571067
return result;
10581068
};
10591069

1070+
const reloadCompiler = async (config: Config, force = false) => {
1071+
if (!compiler.isFake && !force) return;
1072+
compiler = (window as any).compiler = await getCompiler({
1073+
config,
1074+
baseUrl,
1075+
eventsManager,
1076+
});
1077+
setCache();
1078+
await getResultPage({});
1079+
};
1080+
10601081
const setLoading = (status: boolean) => {
10611082
const loading = UI.getToolspaneLoader();
10621083
if (!loading) return;
@@ -4982,7 +5003,15 @@ const changeAppLanguage = async (appLanguage: AppLanguage) => {
49825003

49835004
const basicHandlers = () => {
49845005
notifications = createNotifications();
4985-
modal = createModal({ translate: translateElement, onClose: () => getActiveEditor().focus() });
5006+
modal = createModal({
5007+
translate: translateElement,
5008+
isEmbed,
5009+
onClose: () => {
5010+
if (!isEmbed) {
5011+
getActiveEditor().focus();
5012+
}
5013+
},
5014+
});
49865015
split = createSplitPanes();
49875016
typeLoader = createTypeLoader(baseUrl);
49885017

@@ -5394,8 +5423,11 @@ const initializePlayground = async (
53945423
loadUserConfig(/* updateUI = */ false);
53955424
setConfig(buildConfig({ ...getConfig(), ...appConfig }));
53965425
configureModes({ config: getConfig(), isEmbed, isLite });
5397-
compiler = await getCompiler({ config: getConfig(), baseUrl, eventsManager });
5398-
(window as any).compiler = compiler;
5426+
compiler = (window as any).compiler = await getCompiler({
5427+
config: getConfig(),
5428+
baseUrl,
5429+
eventsManager,
5430+
});
53995431
formatter = getFormatter(getConfig(), baseUrl, isEmbed);
54005432
customEditors = createCustomEditors({ baseUrl, eventsManager });
54015433
await loadI18n(getConfig().appLanguage);
@@ -5448,35 +5480,37 @@ const createApi = (): API => {
54485480
const newAppConfig = buildConfig({ ...currentConfig, ...newConfig });
54495481
const hasNewAppLanguage =
54505482
newConfig.appLanguage && newConfig.appLanguage !== i18n?.getLanguage();
5451-
const reloadCompiler =
5452-
(currentConfig.mode === 'editor' || currentConfig.mode === 'codeblock') &&
5453-
newConfig.mode !== 'editor' &&
5454-
newConfig.mode !== 'codeblock' &&
5455-
compiler.isFake;
5456-
const reloadCodeEditors =
5457-
newConfig.mode != null &&
5458-
newConfig.mode !== currentConfig.mode &&
5459-
(['codeblock', 'result'].includes(currentConfig.mode) ||
5460-
['codeblock', 'result'].includes(newConfig.mode!));
5483+
const shouldRun =
5484+
newConfig.mode != null && newConfig.mode !== 'editor' && newConfig.mode !== 'codeblock';
5485+
const shouldReloadCompiler = shouldRun && compiler.isFake;
5486+
const shouldReloadCodeEditors = (() => {
5487+
if (newConfig.editor != null && !(newConfig.editor in editors.markup)) return true;
5488+
if (newConfig.mode != null) {
5489+
if (newConfig.mode !== 'result' && editors.markup.isFake) return true;
5490+
if (newConfig.mode !== 'codeblock' && editors.markup.codejar) return true;
5491+
}
5492+
return false;
5493+
})();
54615494

54625495
setConfig(newAppConfig);
54635496

54645497
if (hasNewAppLanguage) {
54655498
changeAppLanguage(newConfig.appLanguage!);
54665499
return newAppConfig;
54675500
}
5468-
if (reloadCompiler) {
5469-
compiler = await getCompiler({ config: getConfig(), baseUrl, eventsManager });
5470-
await run();
5501+
if (shouldReloadCompiler) {
5502+
await reloadCompiler(newAppConfig);
54715503
}
5472-
if (reloadCodeEditors) {
5504+
if (shouldReloadCodeEditors) {
54735505
await createEditors(newAppConfig);
54745506
}
54755507
await applyConfig(newConfig);
54765508
const content = getContentConfig(newConfig as Config);
54775509
const hasContent = Object.values(content).some((value) => value != null);
54785510
if (hasContent) {
54795511
await loadConfig(newAppConfig);
5512+
} else if (shouldRun) {
5513+
await run();
54805514
}
54815515
return newAppConfig;
54825516
};

src/livecodes/types/type-loader.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ export const createTypeLoader = (baseUrl: string) => {
9494
if (!code?.trim()) {
9595
return [];
9696
}
97-
const types: EditorLibrary[] = await (window as any).compiler.typescriptFeatures({
98-
feature: 'ata',
99-
payload: code,
100-
});
97+
const types: EditorLibrary[] =
98+
(await (window as any).compiler?.typescriptFeatures?.({
99+
feature: 'ata',
100+
payload: code,
101+
})) ?? [];
101102
libs.push(...types);
102103
if (typeof callback === 'function') {
103104
types.forEach((type) => {

0 commit comments

Comments
 (0)