Files
webstudio/apps/builder/app/builder/shared/code-editor.tsx
T
Oleg Isonen 215034f4d8 feat: Allow maximizing CSS value for complex values (#4566)
## Description

When the value is not a simple keyword or unit, we now show a maximize
button in the css value input and that lets you edit comfortably a long
value.

When dialog gets closed, value gets applied.

## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
0000)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-12-15 21:35:04 +01:00

132 lines
3.3 KiB
TypeScript

import {
forwardRef,
useMemo,
type ComponentProps,
useEffect,
type ReactNode,
} from "react";
import { styleTags, tags } from "@lezer/highlight";
import {
keymap,
tooltips,
highlightSpecialChars,
highlightActiveLine,
} from "@codemirror/view";
import { bracketMatching, indentOnInput } from "@codemirror/language";
import {
autocompletion,
closeBrackets,
closeBracketsKeymap,
completionKeymap,
} from "@codemirror/autocomplete";
import { html } from "@codemirror/lang-html";
import { markdown } from "@codemirror/lang-markdown";
import { css } from "@webstudio-is/design-system";
import {
EditorContent,
EditorDialog,
EditorDialogButton,
EditorDialogControl,
getMinMaxHeightVars,
} from "./code-editor-base";
const wrapperStyle = css({
position: "relative",
// 1 line is 16px
// set min 10 lines and max 20 lines
...getMinMaxHeightVars({ minHeight: "160px", maxHeight: "320px" }),
});
const getHtmlExtensions = () => [
highlightActiveLine(),
highlightSpecialChars(),
indentOnInput(),
html({}),
bracketMatching(),
closeBrackets(),
// render autocomplete in body
// to prevent popover scroll overflow
tooltips({ parent: document.body }),
autocompletion({ icons: false }),
keymap.of([...closeBracketsKeymap, ...completionKeymap]),
];
const getMarkdownExtensions = () => [
highlightActiveLine(),
highlightSpecialChars(),
indentOnInput(),
markdown({
extensions: [
{
props: [
styleTags({
HorizontalRule: tags.separator,
HeaderMark: tags.annotation,
QuoteMark: tags.annotation,
ListMark: tags.annotation,
LinkMark: tags.annotation,
EmphasisMark: tags.annotation,
CodeMark: tags.annotation,
InlineCode: tags.string,
URL: tags.url,
}),
],
},
],
}),
bracketMatching(),
closeBrackets(),
keymap.of(closeBracketsKeymap),
];
export const CodeEditor = forwardRef<
HTMLDivElement,
Omit<ComponentProps<typeof EditorContent>, "extensions"> & {
lang?: "html" | "markdown";
title?: ReactNode;
}
>(({ lang, title, ...editorContentProps }, ref) => {
const extensions = useMemo(() => {
if (lang === "html") {
return getHtmlExtensions();
}
if (lang === "markdown") {
return getMarkdownExtensions();
}
return [];
}, [lang]);
// prevent clicking on autocomplete options propagating to body
// and closing dialogs and popovers
useEffect(() => {
const handlePointerDown = (event: PointerEvent) => {
if (
event.target instanceof HTMLElement &&
event.target.closest(".cm-tooltip-autocomplete")
) {
event.stopPropagation();
}
};
const options = { capture: true };
document.addEventListener("pointerdown", handlePointerDown, options);
return () => {
document.removeEventListener("pointerdown", handlePointerDown, options);
};
}, []);
const content = (
<EditorContent {...editorContentProps} extensions={extensions} />
);
return (
<div className={wrapperStyle()} ref={ref}>
<EditorDialogControl>
{content}
<EditorDialog title={title} content={content}>
<EditorDialogButton />
</EditorDialog>
</EditorDialogControl>
</div>
);
});
CodeEditor.displayName = "CodeEditor";