Skip to content

Commit

Permalink
feat(editor): support codeium AI code assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Aug 15, 2023
1 parent cdd3959 commit 2949ccd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
4 changes: 4 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ const prepareDir = async () => {
path.resolve(__dirname + '/../src/index.html'),
path.resolve(outDir + '/index.html'),
);
await fs.promises.copyFile(
path.resolve(__dirname + '/../src/livecodes/html/app-base.html'),
path.resolve(outDir + '/livecodes/app.html'),
);
};

try {
Expand Down
18 changes: 18 additions & 0 deletions src/livecodes/html/app-base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="codeium:type" content="monaco" />
<title>LiveCodes</title>
<script>
window.addEventListener('message', function (event) {
if (event.data.content) {
document.write(event.data.content);
document.close();
}
});
</script>
</head>
<body></body>
</html>
1 change: 1 addition & 0 deletions src/livecodes/html/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="codeium:type" content="monaco" />
<title>LiveCodes</title>
<style>
body {
Expand Down
28 changes: 15 additions & 13 deletions src/livecodes/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ export const livecodes = (container: string, config: Partial<Config> = {}): Prom
const iframe = document.createElement('iframe');
iframe.name = 'app';
iframe.style.display = 'none';

containerElement.appendChild(iframe);
iframe.contentWindow?.document.open();
iframe.contentWindow?.document.write(
appHTML
iframe.src = baseUrl + '{{hash:app.html}}';
let contentLoaded = false;
iframe.onload = () => {
if (contentLoaded) return;
const appContent = appHTML
.replace(/{{baseUrl}}/g, baseUrl)
.replace(/{{script}}/g, scriptFile)
.replace(/{{appCDN}}/g, appCDN)
Expand All @@ -83,18 +83,20 @@ export const livecodes = (container: string, config: Partial<Config> = {}): Prom
supportsImportMaps
? ''
: `
<script type="module">
import * as mod from '${baseUrl}{{hash:codemirror.js}}';
window['${baseUrl}{{hash:codemirror.js}}'] = mod;
</script>
`,
<script type="module">
import * as mod from '${baseUrl}{{hash:codemirror.js}}';
window['${baseUrl}{{hash:codemirror.js}}'] = mod;
</script>
`,
)
.replace(
/{{codemirrorCoreUrl}}/g,
`${baseUrl}vendor/codemirror/${process.env.codemirrorVersion}/codemirror-core.js`,
),
);
iframe.contentWindow?.document.close();
);
iframe.contentWindow?.postMessage({ content: appContent }, location.origin);
contentLoaded = true;
};
containerElement.appendChild(iframe);

if (isEmbed) {
window.addEventListener(customEvents.appLoaded, () => {
Expand Down

0 comments on commit 2949ccd

Please sign in to comment.