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: insert a variable at the caret position from the list (#3469)
Bogdan Chadkin4f285a1dd2a9
diff --git a/apps/builder/app/builder/features/settings-panel/variable-popover.tsx b/apps/builder/app/builder/features/settings-panel/variable-popover.tsxindex ea368c6b77..25852a9e23 100644--- a/apps/builder/app/builder/features/settings-panel/variable-popover.tsx+++ b/apps/builder/app/builder/features/settings-panel/variable-popover.tsx@@ -87,6 +87,7 @@ const NameField = ({ defaultValue }: { defaultValue: string }) => {inputRef={ref}name="name"id={nameId}+ autoComplete="off"color={error ? "error" : undefined}defaultValue={defaultValue}onChange={(event) => {diff --git a/apps/builder/app/builder/shared/binding-popover.tsx b/apps/builder/app/builder/shared/binding-popover.tsxindex c7567613e7..70e5581745 100644--- a/apps/builder/app/builder/shared/binding-popover.tsx+++ b/apps/builder/app/builder/shared/binding-popover.tsx@@ -40,7 +40,11 @@ import {getExpressionIdentifiers,lintExpression,} from "@webstudio-is/sdk";-import { ExpressionEditor, formatValuePreview } from "./expression-editor";+import {+ ExpressionEditor,+ formatValuePreview,+ type EditorApi,+} from "./expression-editor";import { useSideOffset } from "./floating-panel";import { $dataSourceVariables } from "~/shared/nano-states";@@ -76,6 +80,7 @@ const BindingPanel = ({onChange: () => void;onSave: (value: string, invalid: boolean) => void;}) => {+ const editorApiRef = useRef<undefined | EditorApi>();const [expression, setExpression] = useState(value);const usedIdentifiers = useMemo(() => getExpressionIdentifiers(value),@@ -85,16 +90,20 @@ const BindingPanel = ({const [touched, setTouched] = useState(false);const scopeEntries = Object.entries(scope);- const updateExpression = (newExpression: string) => {- setExpression(newExpression);- onChange();+ const validate = (expression: string) => {const diagnostics = lintExpression({- expression: newExpression,+ expression,availableVariables: new Set(aliases.keys()),});setErrors(diagnostics.map((diagnostic) => diagnostic.message));};+ const updateExpression = (newExpression: string) => {+ setExpression(newExpression);+ onChange();+ validate(newExpression);+ };+return (<ScrollAreacss={{@@ -139,8 +148,13 @@ const BindingPanel = ({active={usedIdentifiers.has(identifier)}// convert variable to expressiononClick={() => {- updateExpression(identifier);- onSave(identifier, false);+ editorApiRef.current?.replaceSelection(identifier);+ }}+ // expression editor blur is fired after pointer down even+ // preventing it allows to not trigger validation+ // and flickering error tooltip+ onPointerDown={(event) => {+ event.preventDefault();}}/>);@@ -176,10 +190,11 @@ const BindingPanel = ({><div><ExpressionEditor+ editorApiRef={editorApiRef}scope={scope}aliases={aliases}color={- errors.length > 0 || valueError !== undefined+ (touched && errors.length > 0) || valueError !== undefined? "error": undefined}diff --git a/apps/builder/app/builder/shared/code-editor-base.tsx b/apps/builder/app/builder/shared/code-editor-base.tsxindex e1bb8e2aef..53cfc77815 100644--- a/apps/builder/app/builder/shared/code-editor-base.tsx+++ b/apps/builder/app/builder/shared/code-editor-base.tsx@@ -5,6 +5,8 @@ import {useState,forwardRef,type ComponentProps,+ type RefObject,+ useImperativeHandle,} from "react";import {Annotation,@@ -139,7 +141,12 @@ const highlightStyle = HighlightStyle.define([},]);+export type EditorApi = {+ replaceSelection: (string: string) => void;+};+type EditorContentProps = {+ editorApiRef?: RefObject<undefined | EditorApi>;extensions?: Extension[];readOnly?: boolean;autoFocus?: boolean;@@ -150,6 +157,7 @@ type EditorContentProps = {};export const EditorContent = ({+ editorApiRef,extensions = [],readOnly = false,autoFocus = false,@@ -248,6 +256,17 @@ export const EditorContent = ({});}, [value]);+ useImperativeHandle(editorApiRef, () => ({+ replaceSelection: (string) => {+ const view = viewRef.current;+ if (view === undefined) {+ return;+ }+ view.dispatch(view.state.replaceSelection(string));+ view.focus();+ },+ }));+return (<divclassName={editorContentStyle()}diff --git a/apps/builder/app/builder/shared/expression-editor.tsx b/apps/builder/app/builder/shared/expression-editor.tsxindex 6909300385..a0aab6c903 100644--- a/apps/builder/app/builder/shared/expression-editor.tsx+++ b/apps/builder/app/builder/shared/expression-editor.tsx@@ -1,4 +1,10 @@-import { useEffect, useMemo, useRef, type ReactNode } from "react";+import {+ useEffect,+ useMemo,+ useRef,+ type ReactNode,+ type RefObject,+} from "react";import { matchSorter } from "match-sorter";import type { SyntaxNode } from "@lezer/common";import { Facet } from "@codemirror/state";@@ -36,8 +42,11 @@ import {CodeEditorBase,EditorContent,EditorDialog,+ type EditorApi,} from "./code-editor-base";+export type { EditorApi };+export const formatValue = (value: unknown) => {if (Array.isArray(value)) {// format arrays as multiline@@ -343,6 +352,7 @@ const wrapperStyle = css({});export const ExpressionEditor = ({+ editorApiRef,scope = emptyScope,aliases = emptyAliases,color,@@ -352,6 +362,7 @@ export const ExpressionEditor = ({onChange,onBlur,}: {+ editorApiRef?: RefObject<undefined | EditorApi>;/*** object with variables and their data to autocomplete*/@@ -417,6 +428,7 @@ export const ExpressionEditor = ({return (<div className={wrapperStyle.toString()}><CodeEditorBase+ editorApiRef={editorApiRef}extensions={extensions}invalid={color === "error"}readOnly={readOnly}diff --git a/apps/builder/package.json b/apps/builder/package.jsonindex 5b0e64c5dd..af642542f6 100644--- a/apps/builder/package.json+++ b/apps/builder/package.json@@ -19,8 +19,8 @@"middleware:dev": "DOTENV_CONFIG_PATH=.env.preview tsx watch -r dotenv/config ./middleware.js"},"dependencies": {- "@codemirror/autocomplete": "^6.16.0",- "@codemirror/commands": "^6.3.3",+ "@codemirror/autocomplete": "^6.16.2",+ "@codemirror/commands": "^6.5.0","@codemirror/lang-html": "^6.4.9","@codemirror/lang-javascript": "^6.2.2","@codemirror/language": "^6.10.1",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex fafd0220c9..21e9dd9854 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -103,11 +103,11 @@ importers:apps/builder:dependencies:'@codemirror/autocomplete':- specifier: ^6.16.0- version: 6.16.0(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])+ specifier: ^6.16.2+ version: 6.16.2(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])'@codemirror/commands':- specifier: ^6.3.3- version: 6.3.3+ specifier: ^6.5.0+ version: 6.5.0'@codemirror/lang-html':specifier: ^6.4.9version: 6.4.9@@ -2898,16 +2898,16 @@ packages:'@cloudflare/[email protected]':resolution: {integrity: sha512-sEVOhyOgXUwfLkgHqbLZa/sfkSYrh7/zLmI6EZNibPaVPvAnAcItbNNl3SAlLyLKuwf8m4wAIAgu9meKWCvXjg==}- '@codemirror/[email protected]':- resolution: {integrity: sha512-P/LeCTtZHRTCU4xQsa89vSKWecYv1ZqwzOd5topheGRf+qtacFgBeIMQi3eL8Kt/BUNvxUWkx+5qP2jlGoARrg==}+ '@codemirror/[email protected]':+ resolution: {integrity: sha512-MjfDrHy0gHKlPWsvSsikhO1+BOh+eBHNgfH1OXs1+DAf30IonQldgMM3kxLDTG9ktE7kDLaA1j/l7KMPA4KNfw==}peerDependencies:'@codemirror/language': ^6.0.0'@codemirror/state': ^6.0.0'@codemirror/view': ^6.0.0'@lezer/common': ^1.0.0- '@codemirror/[email protected]':- resolution: {integrity: sha512-dO4hcF0fGT9tu1Pj1D2PvGvxjeGkbC6RGcZw6Qs74TH+Ed1gw98jmUgd2axWvIZEqTeTuFrg1lEB1KV6cK9h1A==}+ '@codemirror/[email protected]':+ resolution: {integrity: sha512-rK+sj4fCAN/QfcY9BEzYMgp4wwL/q5aj/VfNSoH1RWPF9XS/dUwBkvlL3hpWgEjOqlpdN1uLC9UkjJ4tmyjJYg==}'@codemirror/[email protected]':resolution: {integrity: sha512-/UNWDNV5Viwi/1lpr/dIXJNWiwDxpw13I4pTUAsNxZdg6E0mI2kTQb0P2iHczg1Tu+H4EBgJR+hYhKiHKko7qg==}@@ -10626,14 +10626,14 @@ snapshots:'@cloudflare/[email protected]': {}- '@codemirror/[email protected](@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])':+ '@codemirror/[email protected](@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])':dependencies:'@codemirror/language': 6.10.1'@codemirror/state': 6.4.1'@codemirror/view': 6.26.3'@lezer/common': 1.2.1- '@codemirror/[email protected]':+ '@codemirror/[email protected]':dependencies:'@codemirror/language': 6.10.1'@codemirror/state': 6.4.1@@ -10642,7 +10642,7 @@ snapshots:'@codemirror/[email protected](@codemirror/[email protected])':dependencies:- '@codemirror/autocomplete': 6.16.0(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])+ '@codemirror/autocomplete': 6.16.2(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])'@codemirror/language': 6.10.1'@codemirror/state': 6.4.1'@lezer/common': 1.2.1@@ -10652,7 +10652,7 @@ snapshots:'@codemirror/[email protected]':dependencies:- '@codemirror/autocomplete': 6.16.0(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])+ '@codemirror/autocomplete': 6.16.2(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])'@codemirror/lang-css': 6.2.1(@codemirror/[email protected])'@codemirror/lang-javascript': 6.2.2'@codemirror/language': 6.10.1@@ -10664,7 +10664,7 @@ snapshots:'@codemirror/[email protected]':dependencies:- '@codemirror/autocomplete': 6.16.0(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])+ '@codemirror/autocomplete': 6.16.2(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@lezer/[email protected])'@codemirror/language': 6.10.1'@codemirror/lint': 6.4.2'@codemirror/state': 6.4.1