From c7a45fcc6d896706576c3973f7fc105ac9090323 Mon Sep 17 00:00:00 2001 From: Philipp Fromme Date: Thu, 9 Jan 2025 17:31:14 +0100 Subject: [PATCH] feat: add autocompletions --- client/src/app/App.js | 2 + .../process-applications/AutoCompletions.js | 59 +++++++ .../__tests__/AutoCompletionsSpec.js | 145 ++++++++++++++++++ .../src/app/process-applications/types.d.ts | 5 + 4 files changed, 211 insertions(+) create mode 100644 client/src/app/process-applications/AutoCompletions.js create mode 100644 client/src/app/process-applications/__tests__/AutoCompletionsSpec.js diff --git a/client/src/app/App.js b/client/src/app/App.js index 3a80538c3..8ce09ab0e 100644 --- a/client/src/app/App.js +++ b/client/src/app/App.js @@ -69,6 +69,7 @@ import ProcessApplications from './process-applications'; import { ProcessApplicationsStatusBar } from './process-applications/components'; import References from './process-applications/References'; +import AutoCompletions from './process-applications/AutoCompletions'; import { PluginsRoot } from './plugins'; @@ -161,6 +162,7 @@ export class App extends PureComponent { }); this.references = new References(processApplications); + this.autoCompletions = new AutoCompletions(processApplications); this.tabRef = React.createRef(); diff --git a/client/src/app/process-applications/AutoCompletions.js b/client/src/app/process-applications/AutoCompletions.js new file mode 100644 index 000000000..087e617ad --- /dev/null +++ b/client/src/app/process-applications/AutoCompletions.js @@ -0,0 +1,59 @@ +/** + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright + * ownership. + * + * Camunda licenses this file to you under the MIT; you may not use this file + * except in compliance with the MIT License. + */ + +/** + * @typedef { import('./types').IndexItem } IndexItem + * @typedef { import('./types').AutoCompletion } AutoCompletion + */ + +export default class AutoCompletions { + constructor(processApplications) { + this._processApplications = processApplications; + } + + /** + * + * @param {string} value + * @param {string} type + * + * @returns {Array} + */ + get(value, type) { + const items = this._processApplications.getItems().filter(item => item.type !== 'processApplication'); + + return items + .filter(item => item.metadata.type === type) + .reduce((autoCompletions, item) => { + const { metadata } = item; + + const { ids } = metadata; + + return [ + ...autoCompletions, + ...ids + .filter(id => id.includes(value)) + .map(id => createAutoCompletion(item, id)) + ]; + }, []); + } +}; + +/** + * @param {IndexItem} item + * @param {string} value + * + * @returns {AutoCompletion} + */ +function createAutoCompletion(item, value) { + return { + uri: item.file.uri, + value + }; +} \ No newline at end of file diff --git a/client/src/app/process-applications/__tests__/AutoCompletionsSpec.js b/client/src/app/process-applications/__tests__/AutoCompletionsSpec.js new file mode 100644 index 000000000..fedaff89e --- /dev/null +++ b/client/src/app/process-applications/__tests__/AutoCompletionsSpec.js @@ -0,0 +1,145 @@ +/** + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright + * ownership. + * + * Camunda licenses this file to you under the MIT; you may not use this file + * except in compliance with the MIT License. + */ + +import ProcessApplications from '../ProcessApplications'; +import AutoCompletions from '../AutoCompletions'; + +describe('AutoCompletions', function() { + + let processApplications, autoCompletions; + + beforeEach(function() { + processApplications = new ProcessApplications(); + + processApplications.emit('activeTab-changed', DEFAULT_ACTIVE_TAB); + + processApplications.emit('items-changed', DEFAULT_ITEMS_PROCESS_APPLICATION); + + autoCompletions = new AutoCompletions(processApplications); + }); + + + describe('#get', function() { + + it('should get autocompletion', function() { + + // given + const value = 'BarDec'; + + // when + const autocompletions = autoCompletions.get(value, 'dmn'); + + // then + console.log(autocompletions); + expect(autocompletions).to.have.length(1); + expect(autocompletions).to.eql([ + { + uri: 'file:///C:/process-application/bar/bar.dmn', + value: 'BarDecision' + } + ]); + }); + + }); + +}); + +const DEFAULT_ITEMS = [ + { + file: { + uri: 'file:///C:/process-application/foo.bpmn', + path: 'C://process-application/foo.bpmn', + dirname: 'C://process-application', + contents: '' + }, + metadata: { + type: 'bpmn', + ids: [ 'FooProcess' ], + linkedIds: [ + { linkedId: 'BarProcess', elementId: 'CallActivity_1', type: 'bpmn' }, + { linkedId: 'BarDecision', elementId: 'BusinessRuleTask_1', type: 'dmn' }, + { linkedId: 'BarForm', elementId: 'UserTask_1', type: 'form' } + ] + } + }, + { + file: { + uri: 'file:///C:/process-application/bar/bar.bpmn', + path: 'C://process-application/bar/bar.bpmn', + dirname: 'C://process-application/bar', + contents: '' + }, + metadata: { + type: 'bpmn', + ids: [ 'BarProcess' ], + linkedIds: [ + { linkedId: 'BazDecision', elementId: 'BusinessRuleTask_1', type: 'dmn' } + ] + } + }, + { + file: { + uri: 'file:///C:/process-application/bar/bar.dmn', + path: 'C://process-application/bar/bar.dmn', + dirname: 'C://process-application/bar', + contents: '' + }, + metadata: { + type: 'dmn', + ids: [ 'BarDecision', 'BazDecision' ], + linkedIds: [] + } + }, + { + file: { + uri: 'file:///C:/process-application/bar/bar.form', + path: 'C://process-application/bar/bar.form', + dirname: 'C://process-application/bar', + contents: '{}' + }, + metadata: { + type: 'form', + ids: [ 'BarForm' ], + linkedIds: [] + } + }, + { + file: { + uri: 'file:///C:/bar.bpmn', + path: 'C://bar.bpmn', + dirname: 'C://', + contents: '' + }, + metadata: { + type: 'bpmn' + } + } +]; + +const DEFAULT_ITEMS_PROCESS_APPLICATION = [ + { + file: { + uri: 'file:///C:/process-application/.process-application', + path: 'C://process-application/.process-application', + dirname: 'C://process-application', + contents: '{}' + }, + metadata: { + type: 'processApplication' + } + }, + ...DEFAULT_ITEMS +]; + +const DEFAULT_ACTIVE_TAB = { + file: { + ...DEFAULT_ITEMS[0].file + } +}; \ No newline at end of file diff --git a/client/src/app/process-applications/types.d.ts b/client/src/app/process-applications/types.d.ts index 75e0cd106..09bb9acaf 100644 --- a/client/src/app/process-applications/types.d.ts +++ b/client/src/app/process-applications/types.d.ts @@ -17,6 +17,11 @@ export type Reference = { } }; +export type AutoCompletion = { + value: string, + uri: string +}; + export type File = { contents: string, dirname: string,