-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(variables): add ability to add a variable to script from side menu
- Loading branch information
1 parent
394de87
commit e8ffda2
Showing
7 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
ui/src/timeMachine/components/variableToolbar/VariableLabel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React, {FunctionComponent} from 'react' | ||
|
||
interface Props { | ||
name: string | ||
onClickVariable: (name: string) => void | ||
} | ||
|
||
const VariableLabel: FunctionComponent<Props> = ({onClickVariable, name}) => { | ||
return ( | ||
<div | ||
className="variables-toolbar--label" | ||
onClick={() => { | ||
onClickVariable(name) | ||
}} | ||
> | ||
{name} | ||
</div> | ||
) | ||
} | ||
|
||
export default VariableLabel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import {Position} from 'codemirror' | ||
|
||
const rejoinScript = (scriptLines: string[]): string => { | ||
return scriptLines.join('\n') | ||
} | ||
|
||
const getCursorPosition = ( | ||
currentLineNumber: number, | ||
currentCharacterNumber: number, | ||
variableName: string | ||
) => { | ||
return { | ||
line: currentLineNumber, | ||
ch: currentCharacterNumber + formatVariable(variableName).length, | ||
} | ||
} | ||
|
||
const insertAtCharacter = ( | ||
lineNumber: number, | ||
characterNumber: number, | ||
scriptLines: string[], | ||
variableName: string | ||
): string => { | ||
const lineToEdit = scriptLines[lineNumber] | ||
const front = lineToEdit.slice(0, characterNumber) | ||
const back = lineToEdit.slice(characterNumber, lineToEdit.length) | ||
|
||
const updatedLine = `${front}${formatVariable(variableName)}${back}` | ||
scriptLines[lineNumber] = updatedLine | ||
|
||
return rejoinScript(scriptLines) | ||
} | ||
|
||
const formatVariable = (variableName: string): string => { | ||
return `v.${variableName}` | ||
} | ||
|
||
export const insertVariable = ( | ||
currentLineNumber: number, | ||
currentCharacterNumber: number, | ||
currentScript: string, | ||
variableName: string | ||
): {updatedScript: string; cursorPosition: Position} => { | ||
const scriptLines = currentScript.split('\n') | ||
|
||
const updatedScript = insertAtCharacter( | ||
currentLineNumber, | ||
currentCharacterNumber, | ||
scriptLines, | ||
variableName | ||
) | ||
|
||
const updatedCursorPosition = getCursorPosition( | ||
currentLineNumber, | ||
currentCharacterNumber, | ||
variableName | ||
) | ||
|
||
return {updatedScript, cursorPosition: updatedCursorPosition} | ||
} |