Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,18 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { readRepoFile } from "~/utils/testing/repoPaths";
|
||||
|
||||
describe("Lazy Monaco editor boundary", () => {
|
||||
it("keeps Monaco behind a lazy client import and renders a fallback", () => {
|
||||
const boundarySource = readRepoFile("apps/web/app/components/editors/MonacoEditor.tsx");
|
||||
|
||||
expect(boundarySource).toContain('lazy(() => import("./MonacoEditor.client"))');
|
||||
expect(boundarySource).toContain("<Suspense fallback=");
|
||||
expect(boundarySource).not.toContain("@monaco-editor/react");
|
||||
});
|
||||
|
||||
it("keeps the Monaco package import isolated to the client editor", () => {
|
||||
const clientSource = readRepoFile("apps/web/app/components/editors/MonacoEditor.client.tsx");
|
||||
|
||||
expect(clientSource).toContain('from "@monaco-editor/react"');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,148 @@
|
||||
import { Editor, useMonaco } from "@monaco-editor/react";
|
||||
import clsx from "clsx";
|
||||
import { Fragment, useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
export type MonacoAutoCompletion = {
|
||||
label: string;
|
||||
kind: any;
|
||||
documentation: string;
|
||||
insertText: string;
|
||||
insertTextRules?: any;
|
||||
};
|
||||
|
||||
export interface MonacoEditorClientProps {
|
||||
name?: string;
|
||||
value: string;
|
||||
onChange?: (value: string) => void;
|
||||
theme?: "vs-dark" | "light";
|
||||
hideLineNumbers?: boolean;
|
||||
language?: "javascript" | "typescript" | "html" | "css" | "json" | "markdown" | "yaml" | "sql";
|
||||
fontSize?: number;
|
||||
className?: string;
|
||||
autocompletions?: MonacoAutoCompletion[];
|
||||
tabSize?: number;
|
||||
readOnly?: boolean;
|
||||
options?: Record<string, unknown>;
|
||||
defaultValue?: string;
|
||||
defaultLanguage?: string;
|
||||
onMount?: (editor: any, monaco: any) => void;
|
||||
onControlEnter?: () => void;
|
||||
}
|
||||
|
||||
let registerCompletion: any;
|
||||
|
||||
export default function MonacoEditorClient({
|
||||
name,
|
||||
value,
|
||||
onChange,
|
||||
theme,
|
||||
hideLineNumbers,
|
||||
language,
|
||||
fontSize,
|
||||
className,
|
||||
autocompletions,
|
||||
tabSize = 4,
|
||||
readOnly,
|
||||
options,
|
||||
defaultValue,
|
||||
defaultLanguage,
|
||||
onMount,
|
||||
onControlEnter,
|
||||
}: MonacoEditorClientProps) {
|
||||
const monaco = useMonaco();
|
||||
const { t } = useTranslation();
|
||||
|
||||
useEffect(() => {
|
||||
function createDependencyProposals(autocompletions: MonacoAutoCompletion[], range: any) {
|
||||
return autocompletions.map((f) => {
|
||||
return {
|
||||
...f,
|
||||
range: range,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
if (monaco) {
|
||||
if (onControlEnter) {
|
||||
const executeAction: any = {
|
||||
id: "onControlEnter",
|
||||
label: t("components.editor.actions.execute"),
|
||||
contextMenuOrder: 2,
|
||||
contextMenuGroupId: "1_modification",
|
||||
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter],
|
||||
run: onControlEnter,
|
||||
};
|
||||
monaco.editor.addEditorAction(executeAction);
|
||||
}
|
||||
|
||||
if (registerCompletion) {
|
||||
registerCompletion.dispose();
|
||||
}
|
||||
registerCompletion = monaco.languages.registerCompletionItemProvider("markdown", {
|
||||
provideCompletionItems: function (model: any, position: any) {
|
||||
// find out if we are completing a property in the 'dependencies' object.
|
||||
// var textUntilPosition = model.getValueInRange({
|
||||
// startLineNumber: 1,
|
||||
// startColumn: 1,
|
||||
// endLineNumber: position.lineNumber,
|
||||
// endColumn: position.column,
|
||||
// });
|
||||
// var match = textUntilPosition.match(/"dependencies"\s*:\s*\{\s*("[^"]*"\s*:\s*"[^"]*"\s*,\s*)*([^"]*)?$/);
|
||||
// if (!match) {
|
||||
// return { suggestions: [] };
|
||||
// }
|
||||
var word = model.getWordUntilPosition(position);
|
||||
var range = {
|
||||
startLineNumber: position.lineNumber,
|
||||
endLineNumber: position.lineNumber,
|
||||
startColumn: word.startColumn,
|
||||
endColumn: word.endColumn,
|
||||
};
|
||||
return {
|
||||
suggestions: createDependencyProposals(autocompletions ?? [], range),
|
||||
};
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (registerCompletion) {
|
||||
registerCompletion.dispose();
|
||||
}
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [autocompletions, monaco]);
|
||||
|
||||
return (
|
||||
<Fragment>
|
||||
{name && <textarea name={name} value={value} hidden readOnly />}
|
||||
{typeof window !== "undefined" && (
|
||||
<Editor
|
||||
loading={<div className={clsx(className)}></div>}
|
||||
theme={theme}
|
||||
className={clsx(
|
||||
className,
|
||||
"focus:border-border focus:ring-ring border-border block w-full min-w-0 flex-1 rounded-md sm:text-sm",
|
||||
hideLineNumbers && "-ml-10"
|
||||
)}
|
||||
defaultLanguage={defaultLanguage}
|
||||
language={language}
|
||||
options={{
|
||||
fontSize,
|
||||
renderValidationDecorations: "off",
|
||||
wordWrap: "on",
|
||||
unusualLineTerminators: "off",
|
||||
tabSize,
|
||||
readOnly,
|
||||
...options,
|
||||
}}
|
||||
value={value}
|
||||
defaultValue={defaultValue}
|
||||
onMount={onMount}
|
||||
onChange={(e) => (onChange ? onChange(e ?? "") : null)}
|
||||
/>
|
||||
)}
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { lazy, Suspense, useEffect, useState } from "react";
|
||||
|
||||
import type { MonacoAutoCompletion, MonacoEditorClientProps } from "./MonacoEditor.client";
|
||||
|
||||
const MonacoEditorClient = lazy(() => import("./MonacoEditor.client"));
|
||||
|
||||
export type { MonacoAutoCompletion };
|
||||
|
||||
export default function MonacoEditor(props: MonacoEditorClientProps) {
|
||||
// The editor is strictly client-only: ./MonacoEditor.client pulls in the
|
||||
// browser-only monaco runtime, so importing it during SSR throws and the
|
||||
// streaming render sets HTTP 500 on every page that mounts an editor (the page
|
||||
// still hydrates fine on the client, which is why this stayed invisible until a
|
||||
// readiness test asserted the response status). Render the placeholder on the
|
||||
// server AND the first client render — keeping markup identical so hydration
|
||||
// matches — then swap in the real editor after mount, so the client bundle is
|
||||
// the only place that ever imports it.
|
||||
const [mounted, setMounted] = useState(false);
|
||||
useEffect(() => setMounted(true), []);
|
||||
|
||||
if (!mounted) {
|
||||
return <div className={props.className} />;
|
||||
}
|
||||
|
||||
return (
|
||||
<Suspense fallback={<div className={props.className} />}>
|
||||
<MonacoEditorClient {...props} />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user