Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#64348] allow pattern input to insert selection as text

Tobias Dillmann31b7eba0d688
diff --git a/app/forms/custom_fields/calculated_values/details_form.rb b/app/forms/custom_fields/calculated_values/details_form.rb
index 83751de7595..0b4c886e92a 100644
--- a/app/forms/custom_fields/calculated_values/details_form.rb
+++ b/app/forms/custom_fields/calculated_values/details_form.rb
@@ -80,9 +80,14 @@ module CustomFields
private
def formula_suggestions
- operators = CustomField::CalculatedValue::OPERATORS.map { |op| { key: op, label: op } }
- custom_fields = ProjectCustomField.where(field_format: %w[int float calculated_value]).map do |cf|
- { key: "cf_#{cf.id}", label: "#{cf.name} (#{helpers.label_for_custom_field_format(cf.field_format)})" }
+ # Insert operators as plain text nodes instead of tokens, since displaying them as tokens would result
+ # in too much visual clutter. We still want to offer autocompletion for them.
+ operators = CustomField::CalculatedValue::OPERATORS.map { |op| { key: op, label: op, insert_as_text: true } }
+
+ custom_fields = ProjectCustomField
+ .where(field_format: CustomField::CalculatedValue::FIELD_FORMATS_FOR_FORMULA)
+ .map do |cf|
+ { key: "cf_#{cf.id}", label: cf.name }
end
{
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rb
index 36835bb4efb..b2480a17834 100644
--- a/app/models/custom_field/calculated_value.rb
+++ b/app/models/custom_field/calculated_value.rb
@@ -33,8 +33,12 @@
module CustomField::CalculatedValue
extend ActiveSupport::Concern
+ # Mathematical operators that are allowed in the formula.
OPERATORS = %w[+ - * / ( )].freeze
+ # Field formats that can be used within a formula.
+ FIELD_FORMATS_FOR_FORMULA = %w[int float calculated_value].freeze
+
included do
validate :validate_formula
diff --git a/frontend/src/stimulus/controllers/pattern-input.controller.ts b/frontend/src/stimulus/controllers/pattern-input.controller.ts
index c7dcdad298e..f226de6f83c 100644
--- a/frontend/src/stimulus/controllers/pattern-input.controller.ts
+++ b/frontend/src/stimulus/controllers/pattern-input.controller.ts
@@ -39,7 +39,7 @@ type FilteredSuggestions = Array<{
type TokenElement = HTMLElement&{ dataset:{ role:'token', prop:string } };
type ListElement = HTMLElement&{ dataset:{ role:'list_item', prop:string } };
-type AttributeToken = { key:string, label:string, label_with_context:string };
+type AttributeToken = { key:string, label:string, label_with_context:string, insert_as_text?:boolean };
const COMPLETION_CHARACTER = '/';
const TOKEN_REGEX = /{{([0-9A-Za-z_]+)}}/g;
@@ -179,19 +179,16 @@ export default class PatternInputController extends Controller {
// Autocomplete events
suggestions_select(event:PointerEvent):void {
const target = event.currentTarget as ListElement;
- const token = this.createToken(target.dataset.prop);
+ const selection = target.dataset.prop;
- if (!this.currentRange) {
- this.contentTarget.appendChild(token);
- this.clearSuggestionsFilter();
- return;
- }
-
- const parentNode = this.currentRange.startContainer.parentNode;
- if (this.isToken(parentNode)) {
- this.replaceToken(token, parentNode);
+ // Some suggestions are configured to be inserted as text to allow for better readability.
+ // As an example, take the mathematical operators of a calculated values formula.
+ if (this.shouldInsertAsText(selection)) {
+ const textNode = document.createTextNode(selection);
+ this.insertNode(textNode);
} else {
- this.insertNodeAtCurrentRange(token);
+ const token = this.createToken(selection);
+ this.insertNode(token);
}
this.clearSuggestionsFilter();
@@ -201,13 +198,8 @@ export default class PatternInputController extends Controller {
if (!this.currentRange) { return; }
const target = event.currentTarget as ListElement;
- const parentNode = this.currentRange.startContainer.parentNode;
const text = document.createTextNode(target.dataset.prop);
- if (this.isToken(parentNode)) {
- this.replaceToken(text, parentNode);
- } else {
- this.insertNodeAtCurrentRange(text);
- }
+ this.insertNodeAtAppropriatePosition(text);
this.clearSuggestionsFilter();
}
@@ -603,4 +595,31 @@ export default class PatternInputController extends Controller {
return new RegExp(`[${CONTROL_SPACE}\\s]`).test(value);
}
+
+ private insertNode(node:Node) {
+ if (!this.currentRange) {
+ this.contentTarget.appendChild(node);
+ return;
+ }
+
+ this.insertNodeAtAppropriatePosition(node);
+ }
+
+ private insertNodeAtAppropriatePosition(node:Node) {
+ if (!this.currentRange) { return; }
+
+ const parentNode = this.currentRange.startContainer.parentNode;
+ if (this.isToken(parentNode)) {
+ this.replaceToken(node, parentNode);
+ } else {
+ this.insertNodeAtCurrentRange(node);
+ }
+ }
+
+ private shouldInsertAsText(tokenKey:string):boolean {
+ return Object.values(this.suggestionsInitialValue).some(
+ (group) =>
+ group.tokens.some((token) => token.key === tokenKey && token.insert_as_text),
+ );
+ }
}