@keboola/design 9.0.0 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/editors.cjs CHANGED
@@ -23,12 +23,11 @@ var r = require('@codemirror/legacy-modes/mode/r');
23
23
  var shell = require('@codemirror/legacy-modes/mode/shell');
24
24
  var toml = require('@codemirror/legacy-modes/mode/toml');
25
25
  var codemirrorThemeGithub = require('@uiw/codemirror-theme-github');
26
- var codemirrorLangSfsql = require('@keboola/codemirror-lang-sfsql');
26
+ var codemirrorLangSql = require('@keboola/codemirror-lang-sql');
27
27
  var colors = require('@keboola/tailwind-config/colors');
28
28
  var simpleMode = require('@codemirror/legacy-modes/mode/simple-mode');
29
29
  var state = require('@codemirror/state');
30
30
  var commands = require('@codemirror/commands');
31
- var codemirrorLangSql = require('@keboola/codemirror-lang-sql');
32
31
 
33
32
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
34
33
 
@@ -1037,6 +1036,7 @@ var LANGUAGE_MODES = [
1037
1036
  "text/x-mysql",
1038
1037
  "text/x-pgsql",
1039
1038
  "text/x-mssql",
1039
+ // Snowflake, under a historical name — these strings are a published prop contract.
1040
1040
  "text/x-sfsql",
1041
1041
  "text/x-plsql",
1042
1042
  "text/x-rsrc",
@@ -1098,8 +1098,6 @@ var ALWAYS_OFF = {
1098
1098
  var buildBasicSetup = (readOnly) => ({
1099
1099
  ...ALWAYS_OFF,
1100
1100
  ...readOnly ? {
1101
- history: false,
1102
- historyKeymap: false,
1103
1101
  closeBrackets: false,
1104
1102
  closeBracketsKeymap: false,
1105
1103
  lintKeymap: false,
@@ -1121,20 +1119,6 @@ var buildCustomTheme = () => codemirrorThemeGithub.githubLightInit({
1121
1119
  selection: resolveToken("primary", "31 143 255")
1122
1120
  }
1123
1121
  });
1124
- var sfsqlWithLanguageData = {
1125
- ...codemirrorLangSfsql.sfsql,
1126
- languageData: {
1127
- ...codemirrorLangSfsql.sfsql.languageData,
1128
- // `toggleComment` is a no-op without these, so Mod-/ silently does nothing.
1129
- commentTokens: { line: "--", block: { open: "/*", close: "*/" } },
1130
- autocomplete: autocomplete.completeFromList(
1131
- Object.keys(codemirrorLangSfsql.modeSpec.keywords).map((label) => ({
1132
- label: label.toUpperCase(),
1133
- type: "keyword"
1134
- }))
1135
- )
1136
- }
1137
- };
1138
1122
  var getLanguageMode = (mode) => {
1139
1123
  switch (mode) {
1140
1124
  case void 0:
@@ -1151,7 +1135,7 @@ var getLanguageMode = (mode) => {
1151
1135
  case "text/x-mssql":
1152
1136
  return langSql.sql({ upperCaseKeywords: true, dialect: langSql.MSSQL });
1153
1137
  case "text/x-sfsql":
1154
- return language.StreamLanguage.define(sfsqlWithLanguageData);
1138
+ return langSql.sql({ upperCaseKeywords: true, dialect: codemirrorLangSql.getSqlDialect("snowflake") });
1155
1139
  case "text/x-plsql":
1156
1140
  return langSql.sql({ upperCaseKeywords: true, dialect: langSql.PLSQL });
1157
1141
  case "text/x-rsrc":
@@ -1413,7 +1397,7 @@ var completeVariables = (variables = []) => {
1413
1397
  return { from: match.to - name.length, options };
1414
1398
  };
1415
1399
  };
1416
- var emptyArray = [];
1400
+ var useVariableCompletion = (variables = []) => useStableCallback(completeVariables(variables));
1417
1401
  var emptyObject = {};
1418
1402
  var CodeEditor = ({
1419
1403
  value,
@@ -1425,7 +1409,8 @@ var CodeEditor = ({
1425
1409
  searchQuery,
1426
1410
  searchMatchIndex,
1427
1411
  readOnly = false,
1428
- autocompletions = emptyArray,
1412
+ inputLocked = false,
1413
+ autocompletions,
1429
1414
  completionSource,
1430
1415
  variables,
1431
1416
  extraKeys = emptyObject,
@@ -1461,21 +1446,23 @@ var CodeEditor = ({
1461
1446
  [boundKeysSignature]
1462
1447
  );
1463
1448
  const tooltipLayer = useTooltipsExtension();
1464
- const variableCompletion = react.useMemo(() => completeVariables(variables), [variables]);
1449
+ const variableCompletion = useVariableCompletion(variables);
1465
1450
  const customTheme = useThemedExtension("editor:syntax", buildCustomTheme);
1466
1451
  const baseTheme = useThemedExtension(
1467
1452
  "code-editor:chrome",
1468
1453
  () => view.EditorView.theme(buildBaseThemeStyles())
1469
1454
  );
1470
1455
  const commonExtensions = react.useMemo(() => {
1456
+ const callerCompletions = completionSource ?? (completeAny ? autocomplete.completeAnyWord : autocompletions?.length ? autocompletions : null);
1457
+ const completionEntries = [
1458
+ ...callerCompletions ? [{ autocomplete: callerCompletions }] : [],
1459
+ { autocomplete: variableCompletion }
1460
+ ];
1471
1461
  return [
1472
1462
  getLanguageMode(mode),
1473
1463
  shouldLintJson(mode, readOnly) ? lint.linter(langJson.jsonParseLinter()) : [],
1474
1464
  withLineWrapping ? view.EditorView.lineWrapping : [],
1475
- CodeMirror.EditorState.languageData.of(() => [
1476
- { autocomplete: completionSource ?? (completeAny ? autocomplete.completeAnyWord : autocompletions) }
1477
- ]),
1478
- CodeMirror.EditorState.languageData.of(() => [{ autocomplete: variableCompletion }]),
1465
+ CodeMirror.EditorState.languageData.of(() => completionEntries),
1479
1466
  view.keymap.of(
1480
1467
  boundKeys.map((key) => ({
1481
1468
  key,
@@ -1516,7 +1503,10 @@ var CodeEditor = ({
1516
1503
  ...commonExtensions,
1517
1504
  view.placeholder(placeholder),
1518
1505
  view.EditorView.editable.of(!readOnly),
1519
- CodeMirror.EditorState.readOnly.of(readOnly),
1506
+ // The lock is this facet and nothing else. `contenteditable` is what the
1507
+ // read-only wash keys off, and `basicSetup` is what holds the undo history,
1508
+ // so leaving both alone is what makes the lock invisible.
1509
+ CodeMirror.EditorState.readOnly.of(readOnly || inputLocked),
1520
1510
  previousValue ? merge.unifiedMergeView({
1521
1511
  original: previousValue,
1522
1512
  mergeControls: false,
@@ -1524,7 +1514,7 @@ var CodeEditor = ({
1524
1514
  collapseUnchanged: withCollapseUnchanged ? {} : void 0
1525
1515
  }) : []
1526
1516
  ];
1527
- }, [commonExtensions, placeholder, readOnly, previousValue, withCollapseUnchanged]);
1517
+ }, [commonExtensions, placeholder, readOnly, inputLocked, previousValue, withCollapseUnchanged]);
1528
1518
  const config = react.useMemo(() => {
1529
1519
  return {
1530
1520
  ...buildBasicSetup(readOnly),
@@ -1750,8 +1740,6 @@ var activeQueryHighlight = (splitQueries) => {
1750
1740
  })
1751
1741
  ];
1752
1742
  };
1753
-
1754
- // src/components/CodeEditor/highlighting.ts
1755
1743
  var HighlightMarker = class extends view.GutterMarker {
1756
1744
  toDOM() {
1757
1745
  const marker = document.createElement("div");
@@ -1867,14 +1855,13 @@ var highlightedLinesTheme = view.EditorView.baseTheme({
1867
1855
  backgroundColor: `${colors__default.default.blue[100]} !important`
1868
1856
  }
1869
1857
  });
1870
- function highlightingExtension(queryParser) {
1858
+ function highlightingExtension() {
1871
1859
  return [
1872
1860
  highlightedLinesField,
1873
1861
  highlightGutter,
1874
1862
  errorLineDecorations,
1875
1863
  processingLineDecorations,
1876
- highlightedLinesTheme,
1877
- ...queryParser ? activeQueryHighlight(queryParser) : []
1864
+ highlightedLinesTheme
1878
1865
  ];
1879
1866
  }
1880
1867
  function updateHighlighting(view, lines) {
@@ -1895,6 +1882,7 @@ var CUSTOM_KEYMAP = [
1895
1882
  }
1896
1883
  }
1897
1884
  ];
1885
+ var OWN_KEYMAP_EXTENSION = CodeMirror.keymap.of([...commands.defaultKeymap, ...CUSTOM_KEYMAP]);
1898
1886
  var buildTheme = () => ({
1899
1887
  ...buildBaseThemeStyles(),
1900
1888
  "&.cm-editor": {
@@ -1932,7 +1920,6 @@ var SqlEditor = ({
1932
1920
  searchQuery = "",
1933
1921
  searchMatchIndex,
1934
1922
  readOnly = false,
1935
- extraKeys,
1936
1923
  queryParser,
1937
1924
  autoFocus = false,
1938
1925
  lineNumbers = true,
@@ -1945,6 +1932,18 @@ var SqlEditor = ({
1945
1932
  dialect
1946
1933
  }) => {
1947
1934
  const editorRef = react.useRef(null);
1935
+ const handleChange = useStableCallback((next) => onChange?.(next));
1936
+ const parseQueries = useStableCallback(async (sql2) => await queryParser?.(sql2) ?? []);
1937
+ const handleBlur = useStableCallback((view) => onBlurEditor?.(view));
1938
+ const handleFocus = useStableCallback((view) => onFocusEditor?.(view));
1939
+ const handleUpdate = useStableCallback((update) => {
1940
+ if (update.selectionSet) {
1941
+ const { main } = update.state.selection;
1942
+ onSelectionChange?.(update.state.sliceDoc(main.from, main.to));
1943
+ }
1944
+ });
1945
+ const variableCompletion = useVariableCompletion(variables);
1946
+ const withQueryParser = queryParser !== void 0;
1948
1947
  const tooltipLayer = useTooltipsExtension();
1949
1948
  const customTheme = useThemedExtension("editor:syntax", buildCustomTheme);
1950
1949
  const themeExtension = useThemedExtension(
@@ -1952,34 +1951,36 @@ var SqlEditor = ({
1952
1951
  () => CodeMirror.EditorView.theme(buildTheme())
1953
1952
  );
1954
1953
  const extensions = react.useMemo(() => {
1955
- const baseKeymap = [...commands.defaultKeymap, ...CUSTOM_KEYMAP];
1956
1954
  return [
1957
1955
  CodeMirror.EditorView.lineWrapping,
1958
- CodeMirror.keymap.of(extraKeys ? [...extraKeys, ...baseKeymap] : baseKeymap),
1956
+ OWN_KEYMAP_EXTENSION,
1959
1957
  ...codemirrorLangSql.installExtensions({
1960
1958
  schema,
1961
1959
  defaultDatabase,
1962
1960
  defaultSchema,
1963
- variableCompletion: completeVariables(variables),
1961
+ variableCompletion,
1964
1962
  dialect
1965
1963
  }),
1966
1964
  searchExtension(),
1967
- highlightingExtension(queryParser),
1965
+ highlightingExtension(),
1966
+ withQueryParser ? activeQueryHighlight(parseQueries) : [],
1968
1967
  tooltipLayer,
1969
1968
  themeExtension,
1970
- onBlurEditor ? CodeMirror.EditorView.domEventHandlers({ blur: (_, view) => onBlurEditor(view) }) : [],
1971
- onFocusEditor ? CodeMirror.EditorView.domEventHandlers({ focus: (_, view) => onFocusEditor(view) }) : []
1969
+ CodeMirror.EditorView.domEventHandlers({
1970
+ blur: (_, view) => handleBlur(view),
1971
+ focus: (_, view) => handleFocus(view)
1972
+ })
1972
1973
  ];
1973
1974
  }, [
1974
- extraKeys,
1975
- queryParser,
1975
+ withQueryParser,
1976
+ parseQueries,
1976
1977
  schema,
1977
1978
  defaultDatabase,
1978
1979
  defaultSchema,
1979
- variables,
1980
+ variableCompletion,
1980
1981
  dialect,
1981
- onBlurEditor,
1982
- onFocusEditor,
1982
+ handleBlur,
1983
+ handleFocus,
1983
1984
  themeExtension,
1984
1985
  tooltipLayer
1985
1986
  ]);
@@ -1996,34 +1997,17 @@ var SqlEditor = ({
1996
1997
  updateSearch(editorRef.current.view, searchQuery, searchMatchIndex);
1997
1998
  }
1998
1999
  }, [searchQuery, searchMatchIndex]);
1999
- react.useEffect(() => {
2000
- if (!onDestroyEditor) return;
2001
- return () => {
2002
- onDestroyEditor();
2003
- };
2004
- }, [onDestroyEditor]);
2005
- const handleOnCreate = react.useCallback(
2006
- (view, state) => {
2007
- onCreateEditor?.(view, state);
2008
- if (highlightLines) {
2009
- updateHighlighting(view, highlightLines);
2010
- }
2011
- if (searchQuery) {
2012
- updateSearch(view, searchQuery, searchMatchIndex);
2013
- }
2014
- },
2015
- [onCreateEditor, highlightLines, searchQuery, searchMatchIndex]
2016
- );
2017
- const handleOnUpdate = react.useCallback(
2018
- (update) => {
2019
- if (update.selectionSet) {
2020
- const { main } = update.state.selection;
2021
- const text = update.state.sliceDoc(main.from, main.to);
2022
- onSelectionChange?.(text);
2023
- }
2024
- },
2025
- [onSelectionChange]
2026
- );
2000
+ const handleDestroy = useStableCallback(() => onDestroyEditor?.());
2001
+ react.useEffect(() => handleDestroy, [handleDestroy]);
2002
+ const handleCreate = useStableCallback((view, state) => {
2003
+ onCreateEditor?.(view, state);
2004
+ if (highlightLines) {
2005
+ updateHighlighting(view, highlightLines);
2006
+ }
2007
+ if (searchQuery) {
2008
+ updateSearch(view, searchQuery, searchMatchIndex);
2009
+ }
2010
+ });
2027
2011
  return /* @__PURE__ */ jsxRuntime.jsx(
2028
2012
  CodeMirror__default.default,
2029
2013
  {
@@ -2031,9 +2015,9 @@ var SqlEditor = ({
2031
2015
  theme: customTheme,
2032
2016
  extensions,
2033
2017
  value,
2034
- onChange,
2035
- onUpdate: handleOnUpdate,
2036
- onCreateEditor: handleOnCreate,
2018
+ onChange: handleChange,
2019
+ onUpdate: handleUpdate,
2020
+ onCreateEditor: handleCreate,
2037
2021
  autoFocus,
2038
2022
  placeholder,
2039
2023
  className,