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

[#61935] added first element to pattern input suggestions

Eric Schubert6360cca97db7
diff --git a/app/components/work_packages/types/pattern_input.html.erb b/app/components/work_packages/types/pattern_input.html.erb
index 3cbe1035a35..32e5bcc2a7f 100644
--- a/app/components/work_packages/types/pattern_input.html.erb
+++ b/app/components/work_packages/types/pattern_input.html.erb
@@ -34,6 +34,7 @@ See COPYRIGHT and LICENSE files for more details.
classes: "pattern-input",
"data-controller": "pattern-input",
"data-pattern-input-pattern-initial-value": @value,
+ "data-pattern-input-insert-as-text-template-value": I18n.t("types.edit.subject_configuration.pattern.insert_as_text"),
"data-pattern-input-suggestions-initial-value": suggestions_for_stimulus
)
) do
@@ -69,6 +70,22 @@ See COPYRIGHT and LICENSE files for more details.
%>
</template>
+ <template data-pattern-input-target="insertAsTextTemplate">
+ <%=
+ render(
+ Primer::Alpha::ActionList::Item.new(
+ list: suggestions_list_component,
+ label: "__placeholder__",
+ role: "menuitem",
+ label_classes: "fgColor-muted text-italic",
+ data: {
+ action: "click->pattern-input#close_suggestions"
+ }
+ )
+ )
+ %>
+ </template>
+
<%=
render(
Primer::BaseComponent.new(
diff --git a/config/locales/en.yml b/config/locales/en.yml
index a3932d625f5..e9a47a8ed05 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -677,6 +677,7 @@ en:
pattern:
label: "Subject pattern"
caption: "Search for an attribute or add text. To create a space between elements add it as a text."
+ insert_as_text: "Add as text: \"%{word}\""
export_configuration:
tab: "Generate PDF"
intro: "Select which templates from those that are available you would like to enable for this type. The template determines the design and attributes visible in the exported PDF of a work package using this type. The first template on the list is selected by default."
diff --git a/frontend/src/stimulus/controllers/pattern-input.controller.ts b/frontend/src/stimulus/controllers/pattern-input.controller.ts
index c266dcc4687..0acd8713ab8 100644
--- a/frontend/src/stimulus/controllers/pattern-input.controller.ts
+++ b/frontend/src/stimulus/controllers/pattern-input.controller.ts
@@ -46,6 +46,8 @@ export default class PatternInputController extends Controller {
'suggestionsHeadingTemplate',
'suggestionsDividerTemplate',
'suggestionsItemTemplate',
+
+ 'insertAsTextTemplate',
];
declare readonly tokenTemplateTarget:HTMLTemplateElement;
@@ -57,20 +59,27 @@ export default class PatternInputController extends Controller {
declare readonly suggestionsDividerTemplateTarget:HTMLTemplateElement;
declare readonly suggestionsItemTemplateTarget:HTMLTemplateElement;
+ declare readonly insertAsTextTemplateTarget:HTMLTemplateElement;
+
static values = {
patternInitial: String,
suggestionsInitial: Object,
+ insertAsTextTemplate: String,
};
- declare patternInitialValue:string;
- declare suggestionsInitialValue:Record<string, Record<string, string>>;
+ declare readonly patternInitialValue:string;
+ declare readonly suggestionsInitialValue:Record<string, Record<string, string>>;
+ declare readonly insertAsTextTemplateValue:string;
validTokens:string[];
currentRange:Range|undefined = undefined;
connect() {
this.contentTarget.innerHTML = this.toHtml(this.patternInitialValue) || ' ';
- this.extractValidTokens();
+ this.validTokens = Object.values(this.suggestionsInitialValue)
+ .map((group) => (Object.keys(group)))
+ .reduce((acc, val) => acc.concat(val), []);
+
this.tagInvalidTokens();
this.clearSuggestionsFilter();
}
@@ -98,7 +107,7 @@ export default class PatternInputController extends Controller {
}
// close the suggestions
- if (event.key === 'Escape' || event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
+ if (['Escape', 'ArrowLeft', 'ArrowRight', 'End', 'Home'].includes(event.key)) {
this.clearSuggestionsFilter();
}
@@ -165,10 +174,8 @@ export default class PatternInputController extends Controller {
}
}
- // internal methods
- extractValidTokens() {
- const res = Object.values(this.suggestionsInitialValue).map((group) => (Object.keys(group)));
- this.validTokens = ([] as string[]).concat(...res);
+ close_suggestions() {
+ this.clearSuggestionsFilter();
}
private updateFormInputValue():void {
@@ -188,7 +195,7 @@ export default class PatternInputController extends Controller {
}
}
- insertSpaceIfFirstCharacter() {
+ private insertSpaceIfFirstCharacter() {
const selection = document.getSelection();
if (selection && selection.rangeCount) {
const range = selection.getRangeAt(0);
@@ -211,7 +218,7 @@ export default class PatternInputController extends Controller {
}
}
- insertSpaceIfLastCharacter():void {
+ private insertSpaceIfLastCharacter():void {
const selection = document.getSelection();
if (selection && selection.rangeCount) {
const range = selection.getRangeAt(0);
@@ -233,7 +240,7 @@ export default class PatternInputController extends Controller {
}
}
- setRealCaretPositionAtNode(target:Node, position:'before'|'after' = 'after'):void {
+ private setRealCaretPositionAtNode(target:Node, position:'before'|'after' = 'after'):void {
const selection = document.getSelection();
if (selection === null) { return; }
@@ -289,8 +296,7 @@ export default class PatternInputController extends Controller {
if (selection) {
return (selection.anchorNode?.textContent?.slice(0, selection.anchorOffset)
.split(' ')
- .pop() as string)
- .toLowerCase();
+ .pop() as string);
}
return null;
@@ -305,7 +311,9 @@ export default class PatternInputController extends Controller {
this.clearSuggestionsFilter();
this.suggestionsTarget.classList.remove('d-none');
- const filtered = this.getFilteredSuggestionsData(word);
+ this.appendInsertAsTextElement(word);
+
+ const filtered = this.getFilteredSuggestionsData(word.toLowerCase());
// insert the HTML
filtered.forEach((group, idx) => {
@@ -336,7 +344,19 @@ export default class PatternInputController extends Controller {
});
}
- setSuggestionText(suggestionItem:HTMLElement, value:string) {
+ private appendInsertAsTextElement(word:string):void {
+ const template = this.insertAsTextTemplateTarget.content.cloneNode(true) as DocumentFragment;
+ const item = template.firstElementChild;
+ if (item === null) { return; }
+
+ const textElement = item.querySelector('span');
+ if (textElement === null) { return; }
+
+ textElement.innerText = this.insertAsTextTemplateValue.replace('%{word}', word);
+ this.suggestionsTarget.appendChild(item);
+ }
+
+ private setSuggestionText(suggestionItem:HTMLElement, value:string) {
const textContainer = suggestionItem.querySelector('span');
if (textContainer) {
textContainer.innerText = value;