-
Notifications
You must be signed in to change notification settings - Fork 29
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(CodeEditor): Changing schemas based on the content #172
Conversation
666a5ae
to
22d68a7
Compare
I updated the PR, incorporated your suggestion and:
If the names are off .. I can change them :) |
@@ -14,25 +16,30 @@ interface SourceCodeProps { | |||
|
|||
export const SourceCode: FunctionComponent<SourceCodeProps> = (props) => { | |||
const editorRef = useRef<Parameters<EditorDidMount>[0] | null>(null); | |||
const camelYamlDslSchema = useSchemasStore((state) => state.schemas.camelYamlDsl); | |||
//const schemas = useSchemasStore((state) => state.schemas); |
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.
//const schemas = useSchemasStore((state) => state.schemas); |
packages/ui/src/hooks/entities.ts
Outdated
import { isKamelet } from '../camel-utils/is-kamelet'; | ||
import { isIntegration } from '../camel-utils/is-integration'; |
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.
[minor] could we add these tokens to the camel-utils/index.ts
barrel file so we could have a single line bringing all the isXXX
functions?
packages/ui/src/hooks/entities.ts
Outdated
import { BaseCamelEntity } from '../models/camel-entities'; | ||
import { BaseVisualCamelEntity } from '../models/visualization/base-visual-entity'; | ||
import { CamelRoute, KameletBinding, Pipe } from '../models/visualization/flows'; | ||
import { Beans } from '../models/visualization/metadata'; | ||
import { EventNotifier, isDefined } from '../utils'; | ||
import { isKamelet } from '../camel-utils/is-kamelet'; | ||
import { isIntegration } from '../camel-utils/is-integration'; | ||
import { SourceSchemaType } from '../models/camel-entities/SourceSchemaType'; |
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.
Probably this could come from camel-entities
directly as well
packages/ui/src/hooks/entities.ts
Outdated
const [currentSchemaType, setCurrentSchemaType] = useState<SourceSchemaType>(SourceSchemaType.Route); | ||
/** Set the Source Code and updates the Entities */ | ||
|
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.
const [currentSchemaType, setCurrentSchemaType] = useState<SourceSchemaType>(SourceSchemaType.Route); | |
/** Set the Source Code and updates the Entities */ | |
const [currentSchemaType, setCurrentSchemaType] = useState<SourceSchemaType>(SourceSchemaType.Route); | |
/** Set the Source Code and updates the Entities */ |
packages/ui/src/hooks/entities.ts
Outdated
const entitiesHolder = rawEntities.reduce( | ||
(acc, rawEntity) => { | ||
const entity = getEntity(rawEntity); | ||
setCurrentSchemaType(getEntityType(rawEntity)); |
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.
[important] I think we don't need to set the schema inside of this iteration, as it leads to setting many times the same value, potentially firing subsequent updates.
I guess that it will be enough to inspect only the first element of the rawEntities
array to determine which schema to apply just once.
setSchema(name: string, schema: Schema) { | ||
const type: SourceSchemaType = SourceSchemaType[name]; | ||
if (name === 'camelYamlDsl') { | ||
this.config[SourceSchemaType.Route].schema = schema; | ||
} | ||
if (type) { | ||
this.config[type].schema = schema; | ||
} | ||
} |
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.
I saw the TS
issue that you mentioned and I made a few changes to the method:
setSchema(name: string | SourceSchemaType, schema: Schema) {
if (name === 'camelYamlDsl') {
this.config[SourceSchemaType.Route].schema = schema;
return;
}
if (isEnumType(name, SourceSchemaType)) {
const type: SourceSchemaType = SourceSchemaType[name];
this.config[type].schema = schema;
}
}
The isEnumType
function is a special type of function, a Typeguard function, now called Type predicate
It's defined like the following:
function isEnumType<T extends object>(type: unknown, enumObject: T): type is keyof T {
return typeof type === 'string' && (type as keyof typeof enumObject) in enumObject;
}
You could use that function and place it in a utils folder, maybe alongside of packages/ui/src/utils/is-enum-type.ts
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.
Many thanks!
Define CurrentEntity in the EntitiesContextResult
introduce the SourceSchemaType enum
…w the naming conventions
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.
It looks great @mmelko , thanks for putting this together, I really like updating the schema upon writing, very cool.
This PR introduces EntitySchemaConfig which is meant to be to replace SchemasStore. Schemas should be possible to configure within the entities-context. Also this PR introduces
currentEntity
field which is used for holding the currentEntityType
. After change of thecurrentEntity
proper schema should be also reloaded in the CodeEditor.related to #136 #135