{"version":3,"file":"queryFieldConfig.cjs","sources":["../../../../src/components/QueryFieldConfig/queryFieldConfig.ts"],"sourcesContent":["import { Prec } from '@codemirror/state';\nimport { EditorView, keymap, placeholder as placeholderExtension } from '@codemirror/view';\n\nimport { type GrafanaTheme2 } from '@grafana/data';\n\nimport { getFocusStyles } from '../../themes/mixins';\nimport { type CodeMirrorBasicSetup, type CodeMirrorEditorTheme, type CodeMirrorExtension } from '../CodeMirror/types';\n\nexport interface QueryFieldConfigOptions {\n  /**\n   * Called when the user presses Shift+Enter or Ctrl+Enter. Plain Enter keeps\n   * its default behavior and inserts a newline. If the consumer debounces\n   * `onChange`, flush the pending change in this callback before running so the\n   * query that executes matches what was typed.\n   */\n  onRunQuery?: () => void;\n  /**\n   * Called when the editor loses focus.\n   */\n  onBlur?: () => void;\n  /**\n   * Placeholder shown while the field is empty.\n   */\n  placeholder?: string;\n}\n\nexport interface QueryFieldConfig {\n  theme: CodeMirrorEditorTheme;\n  basicSetup: CodeMirrorBasicSetup;\n  extensions: CodeMirrorExtension[];\n}\n\n// A query field reads as a standard Grafana input, not a code editor: no\n// gutters, no active-line highlight, no bracket closing/matching, no\n// auto-indent, and no bundled autocompletion. Editing niceties (undo history,\n// the default keymap — including Enter for a newline) stay on.\n//\n// `drawSelection` is off so the browser's native caret and selection are used,\n// matching the plain-contenteditable behavior of the Slate field this replaces.\nconst QUERY_FIELD_BASIC_SETUP = {\n  lineNumbers: false,\n  foldGutter: false,\n  highlightActiveLine: false,\n  highlightActiveLineGutter: false,\n  closeBrackets: false,\n  bracketMatching: false,\n  indentOnInput: false,\n  autocompletion: false,\n  drawSelection: false,\n} as const;\n\nfunction createQueryFieldTheme(theme: GrafanaTheme2): CodeMirrorEditorTheme {\n  return EditorView.theme(\n    {\n      // The editor root carries the standard input chrome (background, border,\n      // radius) so the field lines up with sibling form controls.\n      '&': {\n        width: '100%',\n        minHeight: theme.spacing(4),\n        fontSize: theme.typography.body.fontSize,\n        fontFamily: theme.typography.fontFamilyMonospace,\n        color: theme.colors.text.primary,\n        backgroundColor: theme.components.input.background,\n        border: `1px solid ${theme.components.input.borderColor}`,\n        borderRadius: theme.shape.radius.default,\n      },\n      '&.cm-focused': getFocusStyles(theme),\n      '.cm-scroller': {\n        fontFamily: theme.typography.fontFamilyMonospace,\n        lineHeight: '18px',\n      },\n      '.cm-content': {\n        padding: theme.spacing(0.75, 1),\n        // `drawSelection` is off (see basicSetup), so this colors the native caret.\n        caretColor: theme.colors.text.primary,\n      },\n      '.cm-line': {\n        padding: 0,\n      },\n      '.cm-placeholder': {\n        color: theme.colors.text.disabled,\n      },\n    },\n    { dark: theme.isDark }\n  );\n}\n\n/**\n * Configuration for rendering a `CodeMirrorEditor` as a query field: styled as\n * a standard Grafana input, runs the query on Shift+Enter or Ctrl+Enter, and\n * wraps long queries instead of scrolling horizontally. Replaces the deprecated\n * Slate-based `QueryField`.\n *\n * Spread the result onto the editor and memoize it — a new config each render\n * reconfigures the editor:\n *\n * ```tsx\n * const config = useMemo(() => getQueryFieldConfig(theme, { onRunQuery }), [theme, onRunQuery]);\n * return <CodeMirrorEditor value={query} onChange={onChange} height=\"auto\" {...config} />;\n * ```\n */\nexport function getQueryFieldConfig(theme: GrafanaTheme2, options: QueryFieldConfigOptions = {}): QueryFieldConfig {\n  const { onRunQuery, onBlur, placeholder } = options;\n\n  const extensions: CodeMirrorExtension[] = [EditorView.lineWrapping];\n\n  if (onRunQuery) {\n    const run = () => {\n      onRunQuery();\n      return true;\n    };\n    // Highest precedence so these win over the default keymap, which binds\n    // Shift-Enter (insert newline) and Mod-Enter (insert blank line).\n    extensions.push(\n      Prec.highest(\n        keymap.of([\n          { key: 'Shift-Enter', run },\n          { key: 'Ctrl-Enter', run },\n        ])\n      )\n    );\n  }\n\n  if (onBlur) {\n    extensions.push(\n      EditorView.domEventHandlers({\n        blur: () => {\n          onBlur();\n          return false;\n        },\n      })\n    );\n  }\n\n  if (placeholder) {\n    extensions.push(placeholderExtension(placeholder));\n  }\n\n  return {\n    theme: createQueryFieldTheme(theme),\n    basicSetup: QUERY_FIELD_BASIC_SETUP,\n    extensions,\n  };\n}\n"],"names":["EditorView","getFocusStyles","Prec","keymap","placeholderExtension"],"mappings":";;;;;;;;;AAuCA,MAAM,uBAAA,GAA0B;AAAA,EAC9B,WAAA,EAAa,KAAA;AAAA,EACb,UAAA,EAAY,KAAA;AAAA,EACZ,mBAAA,EAAqB,KAAA;AAAA,EACrB,yBAAA,EAA2B,KAAA;AAAA,EAC3B,aAAA,EAAe,KAAA;AAAA,EACf,eAAA,EAAiB,KAAA;AAAA,EACjB,aAAA,EAAe,KAAA;AAAA,EACf,cAAA,EAAgB,KAAA;AAAA,EAChB,aAAA,EAAe;AACjB,CAAA;AAEA,SAAS,sBAAsB,KAAA,EAA6C;AAC1E,EAAA,OAAOA,eAAA,CAAW,KAAA;AAAA,IAChB;AAAA;AAAA;AAAA,MAGE,GAAA,EAAK;AAAA,QACH,KAAA,EAAO,MAAA;AAAA,QACP,SAAA,EAAW,KAAA,CAAM,OAAA,CAAQ,CAAC,CAAA;AAAA,QAC1B,QAAA,EAAU,KAAA,CAAM,UAAA,CAAW,IAAA,CAAK,QAAA;AAAA,QAChC,UAAA,EAAY,MAAM,UAAA,CAAW,mBAAA;AAAA,QAC7B,KAAA,EAAO,KAAA,CAAM,MAAA,CAAO,IAAA,CAAK,OAAA;AAAA,QACzB,eAAA,EAAiB,KAAA,CAAM,UAAA,CAAW,KAAA,CAAM,UAAA;AAAA,QACxC,MAAA,EAAQ,CAAA,UAAA,EAAa,KAAA,CAAM,UAAA,CAAW,MAAM,WAAW,CAAA,CAAA;AAAA,QACvD,YAAA,EAAc,KAAA,CAAM,KAAA,CAAM,MAAA,CAAO;AAAA,OACnC;AAAA,MACA,cAAA,EAAgBC,sBAAe,KAAK,CAAA;AAAA,MACpC,cAAA,EAAgB;AAAA,QACd,UAAA,EAAY,MAAM,UAAA,CAAW,mBAAA;AAAA,QAC7B,UAAA,EAAY;AAAA,OACd;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