-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59350f0
commit 27572b3
Showing
11 changed files
with
229 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Embed to `<iframe>` | ||
|
||
You can embed this project into your own website. But you should notice that, because of the use of `SharedArrayBuffer`, you need to set your host server's COOP/COEP headers, and use following `<iframe>` attribute: | ||
|
||
```html | ||
<iframe src="https://clangd.guyutongxue.site/?embed" allow="cross-origin-isolated" width="600" height="400"> | ||
``` | ||
|
||
It's recommended to pass `embed` url params, so you can control some behavior like setting editor value or trigger a compile request. You can do so by `contentWindow.postMessage`. | ||
|
||
## Message format | ||
|
||
Should be a JavaScript object, with following properties: | ||
|
||
```ts | ||
interface ClangdInBrowserMessage { | ||
cdib: "1.0.0"; | ||
id?: any; // if presented, you will receive a reply | ||
type: "setCode" | "setInput" | "runCode"; | ||
value?: string; // should provided when setCode / setInput | ||
} | ||
``` | ||
|
||
`setCode` message will set the editor value with given `value`. `setInput` will set the input textarea to your given `value` (for next time's compile). `runCode` will send a compile request just like pressing "Run code" button in the runner panel. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# About URL Params | ||
|
||
All options are case-insensitive and `true` `false` `1` `0` `on` `off` `yes` `no` `y` `n` are all valid boolean values. | ||
|
||
### `code` (`string`) | ||
|
||
Set the initial code when showing editor. Defaults to a Hello World program. | ||
|
||
### `theme` (`"light"|"dark"`) | ||
|
||
Whether use light theme or dark theme. Defaults to user's last time choice or system preference. | ||
|
||
### `lsp` (`boolean`) | ||
|
||
Whether enable Clangd and Language Client. Defaults to `true`. | ||
|
||
### `run` (`boolean|"showOnly"`) | ||
|
||
Whether to automatically run the code when editor is ready. If `"showOnly"` is provided, the runner panel is shown but won't run the code. Defaults to `false`. | ||
|
||
### `embed` (`boolean`) | ||
|
||
Whether enable embed `<iframe>` message handlers [see here](./embed.md). When passing `embed=true`, [iframe-resizer](https://github.com/davidjbradshaw/iframe-resizer) is also enabled so you can resize to your host container size freely. Defaults to `false`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { setEditorValue } from "./config"; | ||
import { setInput, compileAndRun } from "./ui"; | ||
|
||
console.log("!"); | ||
globalThis.addEventListener("message", async (e) => { | ||
const data = e.data; | ||
console.log(data); | ||
if (typeof data !== "object" || data === null) { | ||
return; | ||
} | ||
if (!("cdib" in data)) { | ||
return; | ||
} | ||
if (!["1.0.0"].includes(data.cdib)) { | ||
console.error("Unsupported message version."); | ||
return; | ||
} | ||
const hasId = "id" in data; | ||
const id = data.id; | ||
switch (data.type) { | ||
case void 0: { | ||
console.error("Message type not specified."); | ||
return; | ||
} | ||
case "setCode": { | ||
if (typeof data.value !== "string") { | ||
console.error("Invalid value type."); | ||
return; | ||
} | ||
setEditorValue(data.value); | ||
if (hasId) { | ||
globalThis.postMessage({ cdib: "1.0.0", id, type: "reply:setCode" }); | ||
} | ||
break; | ||
} | ||
case "setInput": { | ||
if (typeof data.value !== "string") { | ||
console.error("Invalid value type."); | ||
return; | ||
} | ||
setInput(data.value); | ||
if (hasId) { | ||
globalThis.postMessage({ cdib: "1.0.0", id, type: "reply:setInput" }); | ||
} | ||
break; | ||
} | ||
case "runCode": { | ||
await compileAndRun(); | ||
if (hasId) { | ||
globalThis.postMessage({ cdib: "1.0.0", id, type: "reply:runCode" }); | ||
} | ||
break; | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,68 @@ | ||
import "./style.css"; | ||
import "./ui"; | ||
import { compileAndRun, setClangdStatus, toggleBuildPanel } from "./ui"; | ||
import { ExtendedSearchParams } from "./search_params"; | ||
|
||
if (!globalThis.crossOriginIsolated) { | ||
document.body.innerHTML = | ||
"This page requires cross-origin isolation to work properly. Page will reload in 3s."; | ||
setTimeout(() => window.location.reload(), 3000); | ||
"This page requires cross-origin isolation to work properly. You may forget to set server's COOP/COEP headers. If you are using this page as an <iframe>, you should also pass <code>allow=\"cross-origin\"</code> to the <code>iframe</code> element."; | ||
throw new Error("Cross-origin isolation is not enabled"); | ||
} | ||
|
||
const params = new ExtendedSearchParams(window.location.search); | ||
|
||
let isDark = false; | ||
const themeParam = params.get("theme"); | ||
const userTheme = localStorage.getItem("color-theme"); | ||
const systemDark = window.matchMedia("(prefers-color-scheme: dark)").matches; | ||
if (userTheme === "dark" || (userTheme !== "light" && systemDark)) { | ||
if ( | ||
themeParam === "dark" || | ||
(themeParam !== "light" && | ||
(userTheme === "dark" || (userTheme !== "light" && systemDark))) | ||
) { | ||
document.body.classList.toggle("dark", true); | ||
isDark = true; | ||
} | ||
// @ts-ignore | ||
import("iframe-resizer/js/iframeResizer.contentWindow"); | ||
|
||
const code = `#include <iostream> | ||
if (params.isTrue("embed")) { | ||
// @ts-ignore | ||
import("iframe-resizer/js/iframeResizer.contentWindow"); | ||
import("./embed"); | ||
} | ||
|
||
const code = | ||
params.get("code") ?? | ||
`#include <iostream> | ||
#include <format> | ||
int main() { | ||
std::cout << std::format("Hello, {}!\\n", "world"); | ||
} | ||
`; | ||
|
||
const serverPromise = import("./server").then(({ createServer }) => createServer()); | ||
const runCodeNow = params.isTrue("run"); | ||
const showBuildPanel = | ||
runCodeNow || params.get("run")?.toLowerCase() === "showonly"; | ||
|
||
if (showBuildPanel) { | ||
toggleBuildPanel(); | ||
} | ||
|
||
const disableLsp = params.isExplicitFalse("lsp"); | ||
let serverPromise: Promise<Worker>; | ||
if (disableLsp) { | ||
setClangdStatus("disabled"); | ||
serverPromise = new Promise<never>(() => {}); | ||
} else { | ||
serverPromise = import("./server").then(({ createServer }) => { | ||
return createServer(); | ||
}); | ||
} | ||
import("./client").then(async ({ createEditor, createClient }) => { | ||
await createEditor(document.getElementById("editor")!, code); | ||
await createClient(await serverPromise); | ||
}) | ||
|
||
if (!disableLsp) { | ||
await createClient(await serverPromise); | ||
} | ||
if (runCodeNow) { | ||
compileAndRun(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const trueValues = ["", "true", "1", "yes", "y", "on"]; | ||
const falseValues = ["false", "0", "no", "n", "off"]; | ||
|
||
export class ExtendedSearchParams extends URLSearchParams { | ||
isTrue(key: string): boolean { | ||
return this.has(key) && trueValues.includes(this.get(key)!.toLowerCase()); | ||
} | ||
isExplicitFalse(key: string): boolean { | ||
return falseValues.includes(this.get(key)?.toLowerCase() ?? ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters