@bpmn-io/feel-editor 1.3.0 → 1.5.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/index.es.js CHANGED
@@ -3,7 +3,7 @@ import { defaultKeymap } from '@codemirror/commands';
3
3
  import { syntaxTree, LanguageSupport, syntaxHighlighting, HighlightStyle, bracketMatching, indentOnInput } from '@codemirror/language';
4
4
  import { linter as linter$1, setDiagnosticsEffect } from '@codemirror/lint';
5
5
  import { Facet, Compartment, EditorState } from '@codemirror/state';
6
- import { EditorView, tooltips, keymap } from '@codemirror/view';
6
+ import { EditorView, tooltips, keymap, placeholder } from '@codemirror/view';
7
7
  import { snippets, keywordCompletions, feelLanguage } from 'lang-feel';
8
8
  import { domify } from 'min-dom';
9
9
  import { cmFeelLinter } from '@bpmn-io/feel-lint';
@@ -679,12 +679,7 @@ var variables = context => {
679
679
 
680
680
  const variables = context.state.facet(variablesFacet)[0];
681
681
 
682
- const options = variables.map(v => ({
683
- label: v.name,
684
- type: 'variable',
685
- info: v.info,
686
- detail: v.detail
687
- }));
682
+ const options = variables.map(v => createVariableSuggestion(v));
688
683
 
689
684
  // In most cases, use what is typed before the cursor
690
685
  let nodeBefore = syntaxTree(context.state).resolve(context.pos, -1);
@@ -717,6 +712,55 @@ var variables = context => {
717
712
  return result;
718
713
  };
719
714
 
715
+ /**
716
+ * @param {import('..').Variable} variable
717
+ * @returns {import('@codemirror/autocomplete').Completion}
718
+ */
719
+ function createVariableSuggestion(variable) {
720
+ if (variable.type === 'function') {
721
+ return createFunctionVariable(variable);
722
+ }
723
+
724
+ return {
725
+ label: variable.name,
726
+ type: 'variable',
727
+ info: variable.info,
728
+ detail: variable.detail
729
+ };
730
+ }
731
+
732
+ /**
733
+ * @param {import('..').Variable} variable
734
+ * @returns {import('@codemirror/autocomplete').Completion}
735
+ */
736
+ function createFunctionVariable(variable) {
737
+ const {
738
+ name,
739
+ info,
740
+ detail,
741
+ params = []
742
+ } = variable;
743
+
744
+ const paramsWithNames = params.map(({ name, type }, index) => ({
745
+ name: name || `param ${index + 1}`,
746
+ type
747
+ }));
748
+
749
+ const template = `${name}(${paramsWithNames.map(p => '${' + p.name + '}').join(', ')})`;
750
+
751
+ const paramsSignature = paramsWithNames.map(({ name, type }) => (
752
+ type ? `${name}: ${type}` : name
753
+ )).join(', ');
754
+ const label = `${name}(${paramsSignature})`;
755
+
756
+ return snippetCompletion(template, {
757
+ label,
758
+ type: 'function',
759
+ info,
760
+ detail
761
+ });
762
+ }
763
+
720
764
  function autocompletion() {
721
765
  return [
722
766
  autocompletion$1({
@@ -813,15 +857,18 @@ var theme = [ baseTheme, highlightTheme, syntaxClasses ];
813
857
 
814
858
  /**
815
859
  * @typedef {object} Variable
816
- * @typedef {import('@codemirror/state').Extension} Extension
817
860
  * @property {string} name name or key of the variable
818
861
  * @property {string} [info] short information about the variable, e.g. type
819
862
  * @property {string} [detail] longer description of the variable content
820
863
  * @property {boolean} [isList] whether the variable is a list
821
- * @property {array<Variable>} [schema] array of child variables if the variable is a context or list
864
+ * @property {Array<Variable>} [schema] array of child variables if the variable is a context or list
865
+ * @property {'function'|'variable'} [type] type of the variable
866
+ * @property {Array<{name: string, type: string}>} [params] function parameters
822
867
  */
823
868
 
824
869
  const autocompletionConf = new Compartment();
870
+ const placeholderConf = new Compartment();
871
+
825
872
 
826
873
  /**
827
874
  * Creates a FEEL editor in the supplied container
@@ -847,6 +894,7 @@ function FeelEditor({
847
894
  onChange = () => {},
848
895
  onKeyDown = () => {},
849
896
  onLint = () => {},
897
+ placeholder: placeholder$1 = '',
850
898
  readOnly = false,
851
899
  value = '',
852
900
  variables = []
@@ -903,6 +951,7 @@ function FeelEditor({
903
951
  language(),
904
952
  linter,
905
953
  lintHandler,
954
+ placeholderConf.of(placeholder(placeholder$1)),
906
955
  tooltipLayout,
907
956
  theme,
908
957
  ...editorExtensions
@@ -977,4 +1026,15 @@ FeelEditor.prototype.setVariables = function(variables) {
977
1026
  });
978
1027
  };
979
1028
 
1029
+ /**
1030
+ * Update placeholder text.
1031
+ * @param {string} placeholder
1032
+ * @returns {void}
1033
+ */
1034
+ FeelEditor.prototype.setPlaceholder = function(placeholder$1) {
1035
+ this._cmEditor.dispatch({
1036
+ effects: placeholderConf.reconfigure(placeholder(placeholder$1))
1037
+ });
1038
+ };
1039
+
980
1040
  export { FeelEditor as default };
package/dist/index.js CHANGED
@@ -681,12 +681,7 @@ var variables = context => {
681
681
 
682
682
  const variables = context.state.facet(variablesFacet)[0];
683
683
 
684
- const options = variables.map(v => ({
685
- label: v.name,
686
- type: 'variable',
687
- info: v.info,
688
- detail: v.detail
689
- }));
684
+ const options = variables.map(v => createVariableSuggestion(v));
690
685
 
691
686
  // In most cases, use what is typed before the cursor
692
687
  let nodeBefore = language$1.syntaxTree(context.state).resolve(context.pos, -1);
@@ -719,6 +714,55 @@ var variables = context => {
719
714
  return result;
720
715
  };
721
716
 
717
+ /**
718
+ * @param {import('..').Variable} variable
719
+ * @returns {import('@codemirror/autocomplete').Completion}
720
+ */
721
+ function createVariableSuggestion(variable) {
722
+ if (variable.type === 'function') {
723
+ return createFunctionVariable(variable);
724
+ }
725
+
726
+ return {
727
+ label: variable.name,
728
+ type: 'variable',
729
+ info: variable.info,
730
+ detail: variable.detail
731
+ };
732
+ }
733
+
734
+ /**
735
+ * @param {import('..').Variable} variable
736
+ * @returns {import('@codemirror/autocomplete').Completion}
737
+ */
738
+ function createFunctionVariable(variable) {
739
+ const {
740
+ name,
741
+ info,
742
+ detail,
743
+ params = []
744
+ } = variable;
745
+
746
+ const paramsWithNames = params.map(({ name, type }, index) => ({
747
+ name: name || `param ${index + 1}`,
748
+ type
749
+ }));
750
+
751
+ const template = `${name}(${paramsWithNames.map(p => '${' + p.name + '}').join(', ')})`;
752
+
753
+ const paramsSignature = paramsWithNames.map(({ name, type }) => (
754
+ type ? `${name}: ${type}` : name
755
+ )).join(', ');
756
+ const label = `${name}(${paramsSignature})`;
757
+
758
+ return autocomplete.snippetCompletion(template, {
759
+ label,
760
+ type: 'function',
761
+ info,
762
+ detail
763
+ });
764
+ }
765
+
722
766
  function autocompletion() {
723
767
  return [
724
768
  autocomplete.autocompletion({
@@ -815,15 +859,18 @@ var theme = [ baseTheme, highlightTheme, syntaxClasses ];
815
859
 
816
860
  /**
817
861
  * @typedef {object} Variable
818
- * @typedef {import('@codemirror/state').Extension} Extension
819
862
  * @property {string} name name or key of the variable
820
863
  * @property {string} [info] short information about the variable, e.g. type
821
864
  * @property {string} [detail] longer description of the variable content
822
865
  * @property {boolean} [isList] whether the variable is a list
823
- * @property {array<Variable>} [schema] array of child variables if the variable is a context or list
866
+ * @property {Array<Variable>} [schema] array of child variables if the variable is a context or list
867
+ * @property {'function'|'variable'} [type] type of the variable
868
+ * @property {Array<{name: string, type: string}>} [params] function parameters
824
869
  */
825
870
 
826
871
  const autocompletionConf = new state.Compartment();
872
+ const placeholderConf = new state.Compartment();
873
+
827
874
 
828
875
  /**
829
876
  * Creates a FEEL editor in the supplied container
@@ -849,6 +896,7 @@ function FeelEditor({
849
896
  onChange = () => {},
850
897
  onKeyDown = () => {},
851
898
  onLint = () => {},
899
+ placeholder = '',
852
900
  readOnly = false,
853
901
  value = '',
854
902
  variables = []
@@ -905,6 +953,7 @@ function FeelEditor({
905
953
  language(),
906
954
  linter,
907
955
  lintHandler,
956
+ placeholderConf.of(view.placeholder(placeholder)),
908
957
  tooltipLayout,
909
958
  theme,
910
959
  ...editorExtensions
@@ -979,4 +1028,15 @@ FeelEditor.prototype.setVariables = function(variables) {
979
1028
  });
980
1029
  };
981
1030
 
1031
+ /**
1032
+ * Update placeholder text.
1033
+ * @param {string} placeholder
1034
+ * @returns {void}
1035
+ */
1036
+ FeelEditor.prototype.setPlaceholder = function(placeholder) {
1037
+ this._cmEditor.dispatch({
1038
+ effects: placeholderConf.reconfigure(view.placeholder(placeholder))
1039
+ });
1040
+ };
1041
+
982
1042
  module.exports = FeelEditor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bpmn-io/feel-editor",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "Editor for FEEL expressions.",
5
5
  "files": [
6
6
  "dist"