Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
30c75e85
Unverified
Commit
30c75e85
authored
3 years ago
by
Gustavo Saiani
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Organize expression editor code (#19298)
parent
beecceb4
Loading
Loading
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/src/metabase/query_builder/components/expressions/ExpressionEditorTextfield.jsx
+42
-101
42 additions, 101 deletions
...lder/components/expressions/ExpressionEditorTextfield.jsx
with
42 additions
and
101 deletions
frontend/src/metabase/query_builder/components/expressions/ExpressionEditorTextfield.jsx
+
42
−
101
View file @
30c75e85
...
...
@@ -15,16 +15,6 @@ import { tokenize } from "metabase/lib/expressions/tokenizer";
import
MetabaseSettings
from
"
metabase/lib/settings
"
;
import
colors
from
"
metabase/lib/colors
"
;
import
{
KEYCODE_TAB
,
KEYCODE_ENTER
,
KEYCODE_ESCAPE
,
KEYCODE_LEFT
,
KEYCODE_UP
,
KEYCODE_RIGHT
,
KEYCODE_DOWN
,
}
from
"
metabase/lib/keyboard
"
;
import
ExternalLink
from
"
metabase/components/ExternalLink
"
;
import
Icon
from
"
metabase/components/Icon
"
;
import
Popover
from
"
metabase/components/Popover
"
;
...
...
@@ -142,12 +132,12 @@ export default class ExpressionEditorTextfield extends React.Component {
fontSize
:
"
12px
"
,
});
this
.
_
setCaretPosition
(
this
.
setCaretPosition
(
this
.
state
.
source
.
length
,
this
.
state
.
source
.
length
===
0
,
);
this
.
_
triggerAutosuggest
();
this
.
triggerAutosuggest
();
}
onSuggestionSelected
=
index
=>
{
...
...
@@ -175,7 +165,7 @@ export default class ExpressionEditorTextfield extends React.Component {
const
replacement
=
suggested
.
slice
(
0
,
suggested
.
length
-
extraTrim
);
const
updatedExpression
=
prefix
+
replacement
.
trim
()
+
postfix
;
this
.
on
ExpressionChange
(
updatedExpression
);
this
.
handle
ExpressionChange
(
updatedExpression
);
const
caretPos
=
updatedExpression
.
length
-
postfix
.
length
;
// setTimeout solves a race condition that happens only
...
...
@@ -184,7 +174,7 @@ export default class ExpressionEditorTextfield extends React.Component {
setTimeout
(()
=>
editor
.
moveCursorTo
(
row
,
caretPos
));
}
else
{
const
newExpression
=
source
+
suggestion
.
text
;
this
.
on
ExpressionChange
(
newExpression
);
this
.
handle
ExpressionChange
(
newExpression
);
editor
.
moveCursorTo
(
row
,
newExpression
.
length
);
}
}
...
...
@@ -239,85 +229,13 @@ export default class ExpressionEditorTextfield extends React.Component {
}
};
onInputKeyDown
=
e
=>
{
const
{
suggestions
,
highlightedSuggestionIndex
}
=
this
.
state
;
if
(
e
.
keyCode
===
KEYCODE_LEFT
||
e
.
keyCode
===
KEYCODE_RIGHT
)
{
setTimeout
(()
=>
this
.
_triggerAutosuggest
());
return
;
}
if
(
e
.
keyCode
===
KEYCODE_ESCAPE
)
{
e
.
stopPropagation
();
e
.
preventDefault
();
this
.
clearSuggestions
();
return
;
}
if
(
!
suggestions
.
length
)
{
if
(
e
.
keyCode
===
KEYCODE_ENTER
&&
this
.
props
.
onCommit
)
{
const
expression
=
this
.
_compileExpression
();
if
(
isExpression
(
expression
))
{
this
.
props
.
onCommit
(
expression
);
}
}
return
;
}
if
(
e
.
keyCode
===
KEYCODE_ENTER
||
e
.
keyCode
===
KEYCODE_TAB
)
{
this
.
onSuggestionSelected
(
highlightedSuggestionIndex
);
e
.
preventDefault
();
}
else
if
(
e
.
keyCode
===
KEYCODE_UP
)
{
this
.
setState
({
highlightedSuggestionIndex
:
(
highlightedSuggestionIndex
+
suggestions
.
length
-
1
)
%
suggestions
.
length
,
});
e
.
preventDefault
();
}
else
if
(
e
.
keyCode
===
KEYCODE_DOWN
)
{
this
.
setState
({
highlightedSuggestionIndex
:
(
highlightedSuggestionIndex
+
suggestions
.
length
+
1
)
%
suggestions
.
length
,
});
e
.
preventDefault
();
}
};
clearSuggestions
()
{
this
.
setState
({
suggestions
:
[],
highlightedSuggestionIndex
:
0
,
helpText
:
null
,
});
}
_compileExpression
()
{
const
{
source
}
=
this
.
state
;
if
(
!
source
||
source
.
length
===
0
)
{
return
null
;
}
const
{
query
,
startRule
}
=
this
.
props
;
const
{
expression
}
=
processSource
({
source
,
query
,
startRule
});
return
expression
;
}
commitExpression
()
{
const
expression
=
this
.
_compileExpression
();
if
(
isExpression
(
expression
))
{
this
.
props
.
onCommit
(
expression
);
}
}
handleFocus
=
()
=>
{
this
.
setState
({
isFocused
:
true
});
const
{
editor
}
=
this
.
input
.
current
;
this
.
on
CursorChange
(
editor
.
selection
);
this
.
handle
CursorChange
(
editor
.
selection
);
};
on
InputBlur
=
e
=>
{
handle
InputBlur
=
e
=>
{
this
.
setState
({
isFocused
:
false
});
// Switching to another window also triggers the blur event.
...
...
@@ -337,7 +255,7 @@ export default class ExpressionEditorTextfield extends React.Component {
this
.
setState
({
errorMessage
});
// whenever our input blurs we push the updated expression to our parent if valid
const
expression
=
this
.
_
compileExpression
();
const
expression
=
this
.
compileExpression
();
if
(
expression
)
{
if
(
!
isExpression
(
expression
))
{
console
.
warn
(
"
isExpression=false
"
,
expression
);
...
...
@@ -350,29 +268,52 @@ export default class ExpressionEditorTextfield extends React.Component {
}
};
onInputClick
=
()
=>
{
this
.
_triggerAutosuggest
();
};
clearSuggestions
()
{
this
.
setState
({
suggestions
:
[],
highlightedSuggestionIndex
:
0
,
helpText
:
null
,
});
}
compileExpression
()
{
const
{
source
}
=
this
.
state
;
if
(
!
source
||
source
.
length
===
0
)
{
return
null
;
}
const
{
query
,
startRule
}
=
this
.
props
;
const
{
expression
}
=
processSource
({
source
,
query
,
startRule
});
return
expression
;
}
commitExpression
()
{
const
expression
=
this
.
compileExpression
();
if
(
isExpression
(
expression
))
{
this
.
props
.
onCommit
(
expression
);
}
}
_
triggerAutosuggest
=
()
=>
{
this
.
on
ExpressionChange
(
this
.
state
.
source
);
triggerAutosuggest
=
()
=>
{
this
.
handle
ExpressionChange
(
this
.
state
.
source
);
};
_
setCaretPosition
=
(
position
,
autosuggest
)
=>
{
setCaretPosition
=
(
position
,
autosuggest
)
=>
{
// FIXME setCaretPosition(this.input.current, position);
if
(
autosuggest
)
{
setTimeout
(()
=>
this
.
_
triggerAutosuggest
());
setTimeout
(()
=>
this
.
triggerAutosuggest
());
}
};
on
ExpressionChange
(
source
)
{
handle
ExpressionChange
(
source
)
{
this
.
setState
({
source
});
if
(
this
.
props
.
onBlankChange
)
{
this
.
props
.
onBlankChange
(
source
.
length
===
0
);
}
}
on
CursorChange
(
selection
)
{
handle
CursorChange
(
selection
)
{
const
cursor
=
selection
.
getCursor
();
const
{
query
,
startRule
}
=
this
.
props
;
...
...
@@ -436,7 +377,7 @@ export default class ExpressionEditorTextfield extends React.Component {
highlightActiveLine
=
{
false
}
wrapEnabled
=
{
true
}
fontSize
=
{
12
}
onBlur
=
{
this
.
on
InputBlur
}
onBlur
=
{
this
.
handle
InputBlur
}
onFocus
=
{
this
.
handleFocus
}
role
=
"ace-editor"
setOptions
=
{
{
...
...
@@ -449,8 +390,8 @@ export default class ExpressionEditorTextfield extends React.Component {
showFoldWidgets
:
false
,
showPrintMargin
:
false
,
}
}
onChange
=
{
source
=>
this
.
on
ExpressionChange
(
source
)
}
onCursorChange
=
{
selection
=>
this
.
on
CursorChange
(
selection
)
}
onChange
=
{
source
=>
this
.
handle
ExpressionChange
(
source
)
}
onCursorChange
=
{
selection
=>
this
.
handle
CursorChange
(
selection
)
}
width
=
"100%"
/>
<
ExpressionEditorSuggestions
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment