This is an automated email from the ASF dual-hosted git repository.

marat pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-karavan.git


The following commit(s) were added to refs/heads/main by this push:
     new bedabf6  Support Code snippets in VSCode #514
bedabf6 is described below

commit bedabf6810e84ec480540702496e04115bfbf3ce
Author: Marat Gubaidullin <[email protected]>
AuthorDate: Thu Dec 1 17:37:39 2022 -0500

    Support Code snippets in VSCode #514
---
 karavan-core/src/core/api/TemplateApi.ts            | 16 +++++++++++++++-
 .../designer/route/property/DslPropertyField.tsx    |  6 ++++--
 .../src/designer/route/property/ModalEditor.tsx     | 21 +++++++++++----------
 karavan-vscode/src/designerView.ts                  |  9 +++++++--
 karavan-vscode/src/utils.ts                         | 21 +++++++++++++++++++++
 karavan-vscode/webview/App.tsx                      | 11 ++++++++---
 karavan-vscode/webview/index.css                    |  1 +
 7 files changed, 67 insertions(+), 18 deletions(-)

diff --git a/karavan-core/src/core/api/TemplateApi.ts 
b/karavan-core/src/core/api/TemplateApi.ts
index 597d537..dee8094 100644
--- a/karavan-core/src/core/api/TemplateApi.ts
+++ b/karavan-core/src/core/api/TemplateApi.ts
@@ -15,7 +15,8 @@
  * limitations under the License.
  */
 
-export const Templates: Map<string, string> = new Map<string, string>();
+const Templates: Map<string, string> = new Map<string, string>();
+const JavaCode: Map<string, string> = new Map<string, string>();
 
 export const TemplateApi = {
 
@@ -36,4 +37,17 @@ export const TemplateApi = {
         return Templates.get(name)?.replaceAll("NAME", beanName);
     },
 
+
+    saveJavaCodes: (javaCode: Map<string, string>, clean: boolean = false) => {
+        if (clean) JavaCode.clear();
+        javaCode.forEach((value, key) => JavaCode.set(key, value));
+    },
+
+    saveJavaCode: (name: string, code: string) => {
+        JavaCode.set(name, code);
+    },
+
+    getJavaCode: (name: string): string | undefined => {
+        return JavaCode.get(name);
+    },
 }
\ No newline at end of file
diff --git a/karavan-designer/src/designer/route/property/DslPropertyField.tsx 
b/karavan-designer/src/designer/route/property/DslPropertyField.tsx
index c958bb6..f553cfe 100644
--- a/karavan-designer/src/designer/route/property/DslPropertyField.tsx
+++ b/karavan-designer/src/designer/route/property/DslPropertyField.tsx
@@ -274,11 +274,12 @@ export class DslPropertyField extends 
React.Component<Props, State> {
                 </Button>
             </Tooltip>
             <ModalEditor property={property}
-                         value={customCode}
+                         customCode={customCode}
                          showEditor={showEditor}
                          dark={dark}
                          dslLanguage={dslLanguage}
                          title="Java Class"
+                         onClose={() => this.setState({showEditor: false})}
                          onSave={(fieldId, value1) => {
                              this.propertyChanged(fieldId, value);
                              
KaravanInstance.getProps().onSaveCustomCode?.call(this, value, value1);
@@ -306,11 +307,12 @@ export class DslPropertyField extends 
React.Component<Props, State> {
                     </Button>
                 </Tooltip>
                 <ModalEditor property={property}
-                             value={value}
+                             customCode={value}
                              showEditor={showEditor}
                              dark={dark}
                              dslLanguage={dslLanguage}
                              title={`Expression (${dslLanguage?.[0]})`}
+                             onClose={() => this.setState({showEditor: false})}
                              onSave={(fieldId, value1) => {
                                  this.propertyChanged(fieldId, value1);
                                  this.setState({showEditor: false});
diff --git a/karavan-designer/src/designer/route/property/ModalEditor.tsx 
b/karavan-designer/src/designer/route/property/ModalEditor.tsx
index a12242a..0d5568e 100644
--- a/karavan-designer/src/designer/route/property/ModalEditor.tsx
+++ b/karavan-designer/src/designer/route/property/ModalEditor.tsx
@@ -27,8 +27,9 @@ import Editor from "@monaco-editor/react";
 
 interface Props {
     property: PropertyMeta,
-    value: any,
-    onSave?: (fieldId: string, value: string | number | boolean | any) => void,
+    customCode: any,
+    onSave: (fieldId: string, value: string | number | boolean | any) => void,
+    onClose: () => void,
     title: string,
     dslLanguage?: [string, string, string],
     dark: boolean
@@ -36,32 +37,32 @@ interface Props {
 }
 
 interface State {
-    value: any,
+    customCode: any,
 }
 
 export class ModalEditor extends React.Component<Props, State> {
 
     public state: State = {
-        value: this.props.value,
+        customCode: this.props.customCode,
     }
 
     componentDidUpdate = (prevProps: Readonly<Props>, prevState: 
Readonly<State>, snapshot?: any) => {
         if (prevProps.showEditor !== this.props.showEditor) {
-            this.setState({value: this.props.value})
+            this.setState({customCode: this.props.customCode})
         }
     }
 
     close(){
-        this.props.onSave?.call(this, this.props.property.name, 
this.props.value);
+        this.props.onClose?.call(this);
     }
 
     closeAndSave(){
-        this.props.onSave?.call(this, this.props.property.name, 
this.state.value);
+        this.props.onSave?.call(this, this.props.property.name, 
this.state.customCode);
     }
 
     render() {
         const {dark, dslLanguage, title, showEditor} = this.props;
-        const {value} = this.state;
+        const {customCode} = this.state;
         return (
             <Modal
                 aria-label={"expression"}
@@ -88,9 +89,9 @@ export class ModalEditor extends React.Component<Props, 
State> {
                     language={'java'}
                     theme={dark ? 'vs-dark' : 'light'}
                     options={{lineNumbers: "off", folding: false, 
lineNumbersMinChars: 10, showUnused: false, fontSize: 12, minimap: {enabled: 
false}}}
-                    value={value?.toString()}
+                    value={customCode?.toString()}
                     className={'code-editor'}
-                    onChange={(value: any, ev: any) => this.setState({value: 
value})}
+                    onChange={(value: any, ev: any) => 
this.setState({customCode: value})}
                 />
             </Modal>
         )
diff --git a/karavan-vscode/src/designerView.ts 
b/karavan-vscode/src/designerView.ts
index 61ccd86..8a4aa73 100644
--- a/karavan-vscode/src/designerView.ts
+++ b/karavan-vscode/src/designerView.ts
@@ -131,6 +131,7 @@ export class DesignerView {
                             utils.save(message.relativePath, message.code);
                             break;
                         case 'saveCode':
+                            console.log("saveCode")
                             utils.saveCode(message.name, message.yamlFullPath, 
message.yamFileName, message.code);
                             break;
                         case 'getData':
@@ -171,7 +172,9 @@ export class DesignerView {
             // Read components
             utils.readComponents(this.context),
             // Read templates
-            utils.readTemplates(this.context)
+            utils.readTemplates(this.context),
+            // Read java classes
+            utils.readJavaCode(fullPath)
         ]).then(results => {
             // Send Kamelets
             panel.webview.postMessage({ command: 'kamelets', kamelets: 
results[0] });
@@ -179,9 +182,11 @@ export class DesignerView {
             panel.webview.postMessage({ command: 'components', components: 
results[1] });
             // Send templates
             panel.webview.postMessage({ command: 'templates', templates: 
Object.fromEntries(results[2]) });
+            // Send java code
+            panel.webview.postMessage({ command: 'javaCode', templates: 
Object.fromEntries(results[3]) });
             // Send integration
             this.sendIntegrationData(panel, filename, relativePath, fullPath, 
reread, yaml, tab);
-        })
+        }).catch(err => console.log(err));
     }
 
     sendIntegrationData(panel: WebviewPanel, filename: string, relativePath: 
string, fullPath: string, reread: boolean, yaml?: string, tab?: string) {
diff --git a/karavan-vscode/src/utils.ts b/karavan-vscode/src/utils.ts
index c226a36..dd5e22e 100644
--- a/karavan-vscode/src/utils.ts
+++ b/karavan-vscode/src/utils.ts
@@ -109,6 +109,19 @@ export async function readTemplates(context: 
ExtensionContext) {
     return result;
 }
 
+export async function readJavaCode(fullPath: string) {
+    const result = new Map<string, string>();
+    const codePath = path.dirname(fullPath);
+    const javaFiles = await getJavaFiles(codePath); 
+    for (let x in javaFiles){
+        const fname = javaFiles[x];
+        const readData = await readFile(fname);
+        const code = Buffer.from(readData).toString('utf8');
+        result.set(path.basename(fname, ".java"), code);
+    }
+    return result;
+}
+
 export function parceYaml(filename: string, yaml: string): [boolean, string?] {
     const i = CamelDefinitionYaml.yamlToIntegration(filename, yaml);
     if (i.kind === 'Integration' && i.metadata.name) {
@@ -165,6 +178,14 @@ export async function getPropertyFiles(baseDir: string) {
     return result;
 }
 
+export async function getJavaFiles(baseDir: string) {
+    const result: string[] = [];
+    (await getAllFiles(baseDir, [])).filter(f => 
f.endsWith(".java")).forEach(f => {
+        result.push(f);
+    })
+    return result;
+}
+
 export async function getJsonFiles(baseDir: string) {
     const result: string[] = [];
     (await getAllFiles(baseDir, [])).filter(f => 
f.endsWith(".json")).forEach(f => {
diff --git a/karavan-vscode/webview/App.tsx b/karavan-vscode/webview/App.tsx
index 818878b..f6f1925 100644
--- a/karavan-vscode/webview/App.tsx
+++ b/karavan-vscode/webview/App.tsx
@@ -98,6 +98,11 @@ class App extends React.Component<Props, State> {
         const map = new Map( Object.keys(templates).map(key => [key, 
templates[key]]));
         TemplateApi.saveTemplates(map, true);
         break;  
+      case 'javaCode':
+        const javaCode = message.templates;
+        const javaCodeMap = new Map( Object.keys(javaCode).map(key => [key, 
javaCode[key]]));
+        TemplateApi.saveJavaCodes(javaCodeMap, true);
+        break;  
       case 'open':
         if (this.state.filename === '' && this.state.key === '') {
           if (message.page !== "designer" && this.state.interval) 
clearInterval(this.state.interval);
@@ -142,10 +147,10 @@ class App extends React.Component<Props, State> {
   }
 
   saveJavCode(name: string, code: string) {
+    TemplateApi.saveJavaCode(name, code);
     vscode.postMessage({ command: 'saveCode', name: name, yamlFullPath: 
this.state.fullPath, yamFileName: this.state.filename, code: code });
   }
 
-
   public render() {
     return (
       <Page className="karavan">
@@ -164,8 +169,8 @@ class App extends React.Component<Props, State> {
             dark={this.props.dark} 
             onSaveCustomCode={(name, code) => this.saveJavCode(name, code)}
             onGetCustomCode={(name, javaType) => {
-                // return new Promise<string | undefined>(resolve => 
resolve(files.filter(f => f.name === name + ".java")?.at(0)?.code))
-                return new Promise<string | undefined>(resolve => 
resolve(undefined))
+                const code = TemplateApi.getJavaCode(name);
+                return new Promise<string | undefined>(resolve => 
resolve(code))
             }}
             />
         }
diff --git a/karavan-vscode/webview/index.css b/karavan-vscode/webview/index.css
index f27c7a4..ed6bf77 100644
--- a/karavan-vscode/webview/index.css
+++ b/karavan-vscode/webview/index.css
@@ -301,6 +301,7 @@ body, :root, #root, .karavan {
   background-color:  var(--vscode-editor-background);
   border-color:  var(--vscode-input-foreground);
   color:  var(--vscode-input-foreground);
+  padding-left: 8px;
 }
 
 /* Help */

Reply via email to