-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add parameterization rule custom code UI #344
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1cd8cdb
WIP
e-fisher f02a7fd
Merge `main` into `feat/parameterization-code-ui`
e-fisher da9f266
constrained editor poc
e-fisher bd39725
Merge `main` into `feat/parameterization-code-ui`
e-fisher bbfd30a
Merge `main` into `feat/parameterization-code-ui`
e-fisher 889f029
feat: add constrained editor
e-fisher 303aac2
cleanup
e-fisher b28afec
cleanup
e-fisher c8b3b58
fix: initialize constrained editor only once to fix performance issues
e-fisher b6894bc
Merge `main` into `feat/parameterization-code-ui`
e-fisher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,74 @@ | ||
import * as monacoTypes from 'monaco-editor' | ||
import { ReactMonacoEditor } from './ReactMonacoEditor' | ||
// Locally added types | ||
// eslint-disable-next-line import/no-named-as-default | ||
import constrainedEditor, { | ||
ConstrainedEditorInstance, | ||
RestrictionObject, | ||
} from 'constrained-editor-plugin' | ||
import { useEffect, useState } from 'react' | ||
import { EditorProps } from '@monaco-editor/react' | ||
|
||
interface CodeEditorProps { | ||
value: string | ||
onChange?: (value: string) => void | ||
editableRange: RestrictionObject['range'] | ||
options: EditorProps['options'] | ||
} | ||
|
||
export function ConstrainedCodeEditor({ | ||
value, | ||
onChange, | ||
editableRange, | ||
options, | ||
}: CodeEditorProps) { | ||
const [model, setModel] = useState<monacoTypes.editor.ITextModel | null>() | ||
const [constrainedInstance, setConstrainedInstance] = | ||
useState<ConstrainedEditorInstance>() | ||
|
||
useEffect(() => { | ||
if (!model || !constrainedInstance) { | ||
return | ||
} | ||
|
||
// Add editable range to editor | ||
const constrainedModel = constrainedInstance.addRestrictionsTo(model, [ | ||
{ | ||
range: editableRange, | ||
label: 'editableRange', // Used for reading value onDidChangeContentInEditableRange | ||
allowMultiline: true, | ||
}, | ||
]) | ||
|
||
// Listen to changes in the editable range | ||
constrainedModel.onDidChangeContentInEditableRange( | ||
(currentlyChangedContent) => | ||
onChange?.(currentlyChangedContent.editableRange ?? '') | ||
) | ||
|
||
// Cleanup | ||
return constrainedModel.disposeRestrictions | ||
}, [model, constrainedInstance, editableRange, onChange]) | ||
|
||
const handleEditorMount = ( | ||
editor: monacoTypes.editor.IStandaloneCodeEditor, | ||
monaco: typeof monacoTypes | ||
) => { | ||
const instance = constrainedEditor(monaco) | ||
instance.initializeIn(editor) | ||
|
||
const model = editor.getModel() | ||
setModel(model) | ||
|
||
setConstrainedInstance(instance) | ||
} | ||
|
||
return ( | ||
<ReactMonacoEditor | ||
defaultLanguage="javascript" | ||
options={options} | ||
value={value} | ||
onMount={handleEditorMount} | ||
/> | ||
) | ||
} |
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,59 @@ | ||
declare module 'constrained-editor-plugin' { | ||
import * as monacoTypes from 'monaco-editor' | ||
|
||
type Range = [number, number, number, number] | number[] | ||
type EditableRangesMap = Record<string, RestrictionObject> | ||
type ValueMap = Record<string, string> | ||
|
||
type ValueInEditableRanges = Record<string, string> | ||
|
||
type EditableRangeObject = Record< | ||
string, | ||
{ allowMultiline: boolean; range: Range; originalRange: Range } | ||
> | ||
|
||
export interface RestrictionObject { | ||
label: string | ||
range: Range | ||
allowMultiline?: boolean | ||
validate?: (currentValue: string, currentRange: Range) => boolean | ||
} | ||
|
||
export interface ConstrainedModel extends monacoTypes.editor.ITextModel { | ||
editInRestrictedArea: boolean | ||
getCurrentEditableRanges: () => EditableRangesMap | ||
getValueInEditableRanges: () => ValueMap | ||
disposeRestrictions: () => void | ||
onDidChangeContentInEditableRange: ( | ||
callback: ( | ||
currentlyChangedContent: ValueInEditableRanges, | ||
allValuesInEditableRanges: ValueInEditableRanges, | ||
currentEditableRangeObject: EditableRangeObject | ||
) => void | ||
) => void | ||
updateRestrictions: (ranges: RestrictionObject[]) => void | ||
updateValueInEditableRanges: ( | ||
object: ValueMap, | ||
forceMoveMarkers?: boolean | ||
) => void | ||
toggleHighlightOfEditableAreas: () => void | ||
} | ||
|
||
export interface ConstrainedEditorInstance { | ||
initializeIn: ( | ||
editor: monacoTypes.editor.IStandaloneCodeEditor | ||
) => boolean | never | ||
addRestrictionsTo: ( | ||
model: monacoTypes.editor.ITextModel, | ||
ranges: RestrictionObject[] | ||
) => ConstrainedModel | ||
removeRestrictionsIn: () => boolean | never | ||
disposeConstrainer: () => boolean | ||
toggleDevMode: () => void | ||
} | ||
|
||
export declare function constrainedEditor( | ||
monaco: typeof monacoTypes | ||
): ConstrainedEditorInstance | ||
export default constrainedEditor | ||
} |
53 changes: 53 additions & 0 deletions
53
src/views/Generator/RuleEditor/ParameterizationEditor/CustomCode.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,53 @@ | ||
import { FieldGroup } from '@/components/Form' | ||
import { ConstrainedCodeEditor } from '@/components/Monaco/ConstrainerCodeEditor' | ||
import { getCustomCodeSnippet } from '@/rules/parameterization' | ||
import { selectSelectedRuleIndex, useGeneratorStore } from '@/store/generator' | ||
import { ParameterizationRule } from '@/types/rules' | ||
import { Controller, useFormContext } from 'react-hook-form' | ||
|
||
export function CustomCode() { | ||
const { | ||
control, | ||
formState: { errors }, | ||
} = useFormContext<ParameterizationRule>() | ||
const ruleIndex = useGeneratorStore(selectSelectedRuleIndex) | ||
|
||
return ( | ||
<FieldGroup name="value.code" errors={errors} label="Snippet"> | ||
<Controller | ||
name="value.code" | ||
control={control} | ||
render={({ field }) => ( | ||
<div css={{ height: 200 }}> | ||
<ConstrainedCodeEditor | ||
value={getCustomCodeSnippet( | ||
valueWithFallback(field.value), | ||
ruleIndex | ||
)} | ||
onChange={field.onChange} | ||
editableRange={getEditableRanges(valueWithFallback(field.value))} | ||
options={{ wordWrap: 'on' }} | ||
/> | ||
</div> | ||
)} | ||
/> | ||
</FieldGroup> | ||
) | ||
} | ||
|
||
function valueWithFallback(value?: string) { | ||
return ( | ||
value ?? ' // Enter your code here, make sure to add a return statement' | ||
) | ||
} | ||
|
||
function getEditableRanges(value?: string) { | ||
const lines = (value ?? '').split('\n') | ||
const lastLine = lines[lines.length - 1] ?? '1' | ||
const startLine = 2 // Skip the first line with the function declaration | ||
const startColumn = 1 | ||
const endLine = lines.length + 1 | ||
const endColumn = lastLine.length + 1 | ||
|
||
return [startLine, startColumn, endLine, endColumn] | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😮 Are they accepting contributions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The repository doesn't look active lately 😞 But we have already been using this in GCk6 and had types defined there