Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
feat: Use html editor for custom code in the project settings (#3145)
Oleg Isonena7a2bb777d42
diff --git a/apps/builder/app/builder/features/project-settings/section-general.tsx b/apps/builder/app/builder/features/project-settings/section-general.tsxindex df19219016..641ea4ff1d 100644--- a/apps/builder/app/builder/features/project-settings/section-general.tsx+++ b/apps/builder/app/builder/features/project-settings/section-general.tsx@@ -5,7 +5,6 @@ import {Label,theme,Text,- TextArea,Separator,Button,CheckboxAndLabel,@@ -20,6 +19,7 @@ import type { ProjectMeta, CompilerSettings } from "@webstudio-is/sdk";import { useState } from "react";import { serverSyncStore } from "~/shared/sync";import { sectionSpacing } from "./utils";+import { HtmlEditor } from "~/builder/shared/html-editor";const imgStyle = css({width: 72,@@ -40,7 +40,7 @@ export const SectionGeneral = () => {const [meta, setMeta] = useState(() => $pages.get()?.meta ?? defaultMetaSettings);- const ids = useIds(["siteName", "code"]);+ const ids = useIds(["siteName"]);const assets = useStore($assets);const asset = assets.get(meta.faviconAssetId ?? "");const favIconUrl = asset ? `${asset.name}` : undefined;@@ -107,19 +107,12 @@ export const SectionGeneral = () => {<Separator /><Grid gap={2} css={sectionSpacing}>- <Label htmlFor={ids.code}>Custom Code</Label>+ <Label>Custom Code</Label><Text color="subtle">Custom code and scripts will be added at the end of the <head>tag to every page across the published project.</Text>- <TextArea- id={ids.code}- rows={5}- autoGrow- maxRows={10}- value={meta.code ?? ""}- onChange={handleSave("code")}- />+ <HtmlEditor value={meta.code ?? ""} onChange={handleSave("code")} /></Grid><Separator />diff --git a/apps/builder/app/builder/shared/code-editor.tsx b/apps/builder/app/builder/shared/code-editor.tsxindex 15d0249590..4b094b5717 100644--- a/apps/builder/app/builder/shared/code-editor.tsx+++ b/apps/builder/app/builder/shared/code-editor.tsx@@ -25,6 +25,20 @@ import { CrossIcon, MaximizeIcon, MinimizeIcon } from "@webstudio-is/icons";const ExternalChange = Annotation.define<boolean>();+const minHeightVar = "--ws-code-editor-min-height";+const maxHeightVar = "--ws-code-editor-max-height";++export const getMinMaxHeightVars = ({+ minHeight,+ maxHeight,+}: {+ minHeight: string;+ maxHeight: string;+}) => ({+ [minHeightVar]: minHeight,+ [maxHeightVar]: maxHeight,+});+const editorContentStyle = css({...textVariants.mono,// fit editor into parent if stretched@@ -66,8 +80,8 @@ const editorContentStyle = css({width: "100%",// avoid modifying height in .cm-content// because it breaks scroll events and makes scrolling laggy- minHeight: "var(--ws-code-editor-min-height, auto)",- maxHeight: "var(--ws-code-editor-max-height, none)",+ minHeight: `var(${minHeightVar}, auto)`,+ maxHeight: `var(${maxHeightVar}, none)`,},});diff --git a/apps/builder/app/builder/shared/html-editor.tsx b/apps/builder/app/builder/shared/html-editor.tsxindex 6ce565b4a3..9e438946f2 100644--- a/apps/builder/app/builder/shared/html-editor.tsx+++ b/apps/builder/app/builder/shared/html-editor.tsx@@ -1,4 +1,4 @@-import { forwardRef, useMemo, type ReactNode } from "react";+import { forwardRef, useMemo, type ComponentProps } from "react";import {keymap,drawSelection,@@ -22,7 +22,7 @@ import {} from "@codemirror/autocomplete";import { html } from "@codemirror/lang-html";import { theme, textVariants, css } from "@webstudio-is/design-system";-import { CodeEditor } from "./code-editor";+import { CodeEditor, getMinMaxHeightVars } from "./code-editor";const autocompletionStyle = css({"&.cm-tooltip.cm-tooltip-autocomplete": {@@ -70,68 +70,47 @@ const wrapperStyle = css({position: "relative",// 1 line is 16px// set min 10 lines and max 20 lines- "--ws-code-editor-min-height": "160px",- "--ws-code-editor-max-height": "320px",+ ...getMinMaxHeightVars({ minHeight: "160px", maxHeight: "320px" }),});export const HtmlEditor = forwardRef<HTMLDivElement,- {- readOnly?: boolean;- invalid?: boolean;- value: string;- title?: ReactNode;- onChange: (newValue: string) => void;- onBlur?: (event: FocusEvent) => void;- }->(- (- { readOnly = false, invalid = false, value, title, onChange, onBlur },- ref- ) => {- const extensions = useMemo(- () => [- highlightActiveLine(),- highlightSpecialChars(),- history(),- drawSelection(),- dropCursor(),- indentOnInput(),- syntaxHighlighting(defaultHighlightStyle, { fallback: true }),- html({}),- bracketMatching(),- closeBrackets(),- // render autocomplete in body- // to prevent popover scroll overflow- tooltips({ parent: document.body }),- autocompletion({- icons: false,- tooltipClass: () => autocompletionStyle.toString(),- }),- keymap.of([- ...closeBracketsKeymap,- ...defaultKeymap,- ...historyKeymap,- ...completionKeymap,- ]),- ],- []- );+ Omit<ComponentProps<typeof CodeEditor>, "extensions">+>((props, ref) => {+ const extensions = useMemo(+ () => [+ highlightActiveLine(),+ highlightSpecialChars(),+ history(),+ drawSelection(),+ dropCursor(),+ indentOnInput(),+ syntaxHighlighting(defaultHighlightStyle, { fallback: true }),+ html({}),+ bracketMatching(),+ closeBrackets(),+ // render autocomplete in body+ // to prevent popover scroll overflow+ tooltips({ parent: document.body }),+ autocompletion({+ icons: false,+ tooltipClass: () => autocompletionStyle.toString(),+ }),+ keymap.of([+ ...closeBracketsKeymap,+ ...defaultKeymap,+ ...historyKeymap,+ ...completionKeymap,+ ]),+ ],+ []+ );- return (- <div className={wrapperStyle()} ref={ref}>- <CodeEditor- title={title}- extensions={extensions}- readOnly={readOnly}- invalid={invalid}- value={value}- onChange={onChange}- onBlur={onBlur}- />- </div>- );- }-);+ return (+ <div className={wrapperStyle()} ref={ref}>+ <CodeEditor {...props} extensions={extensions} />+ </div>+ );+});HtmlEditor.displayName = "HtmlEditor";