-
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(ui): Update flux functions insertion from toolbar to add to curr…
…ent line or below
- Loading branch information
Showing
5 changed files
with
151 additions
and
20 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import {Position} from 'codemirror' | ||
|
||
// Constants | ||
import {FROM, UNION} from 'src/shared/constants/fluxFunctions' | ||
|
||
const rejoinScript = (scriptLines: string[]): string => { | ||
return scriptLines.join('\n') | ||
} | ||
|
||
const insertAtLine = ( | ||
lineNumber: number, | ||
scriptLines: string[], | ||
textToInsert: string, | ||
insertOnSameLine?: boolean | ||
): string => { | ||
const front = scriptLines.slice(0, lineNumber) | ||
|
||
const backStartIndex = insertOnSameLine ? lineNumber + 1 : lineNumber | ||
const back = scriptLines.slice(backStartIndex) | ||
|
||
const updated = [...front, textToInsert, ...back] | ||
|
||
return rejoinScript(updated) | ||
} | ||
|
||
const getInsertLineNumber = ( | ||
currentLineNumber: number, | ||
scriptLines: string[] | ||
): number => { | ||
const currentLine = scriptLines[currentLineNumber] | ||
|
||
// Insert on the current line if its an empty line | ||
if (!currentLine.trim()) { | ||
return currentLineNumber | ||
} | ||
|
||
return currentLineNumber + 1 | ||
} | ||
|
||
const functionRequiresNewLine = (funcName: string): boolean => { | ||
switch (funcName) { | ||
case FROM.name: | ||
case UNION.name: { | ||
return true | ||
} | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
const formatFunctionForInsert = (funcName: string, fluxFunction: string) => { | ||
if (functionRequiresNewLine(funcName)) { | ||
return `\n${fluxFunction}` | ||
} | ||
|
||
return ` |> ${fluxFunction}` | ||
} | ||
|
||
const getCursorPosition = ( | ||
insertLineNumber, | ||
formattedFunction, | ||
funcName | ||
): Position => { | ||
const ch = formattedFunction.length - 1 | ||
const line = functionRequiresNewLine(funcName) | ||
? insertLineNumber + 1 | ||
: insertLineNumber | ||
|
||
return {line, ch} | ||
} | ||
|
||
export const insertFluxFunction = ( | ||
currentLineNumber: number, | ||
currentScript: string, | ||
functionName: string, | ||
fluxFunction: string | ||
): {updatedScript: string; cursorPosition: Position} => { | ||
const scriptLines = currentScript.split('\n') | ||
const insertLineNumber = getInsertLineNumber(currentLineNumber, scriptLines) | ||
const insertOnSameLine = currentLineNumber === insertLineNumber | ||
|
||
const formattedFunction = formatFunctionForInsert(functionName, fluxFunction) | ||
|
||
const updatedScript = insertAtLine( | ||
insertLineNumber, | ||
scriptLines, | ||
formattedFunction, | ||
insertOnSameLine | ||
) | ||
|
||
const updatedCursorPosition = getCursorPosition( | ||
insertLineNumber, | ||
formattedFunction, | ||
functionName | ||
) | ||
|
||
return {updatedScript, cursorPosition: updatedCursorPosition} | ||
} |