Skip to content

Commit ee539d1

Browse files
authored
Merge pull request opf#18151 from opf/bug/61573-cant-click-outside-of-an-added-attribute-on-google-chrome
[#61573] allow more mouse control on pattern input
2 parents e6b2674 + 14f4969 commit ee539d1

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

frontend/src/stimulus/controllers/pattern-input.controller.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ export default class PatternInputController extends Controller {
8787
event.preventDefault();
8888
}
8989
if (event.key === 'ArrowLeft') {
90-
if (this.contentTarget.innerHTML.startsWith('<')) {
90+
if (this.startsWithToken()) {
9191
this.insertSpaceIfFirstCharacter();
9292
}
9393
}
9494
if (event.key === 'ArrowRight') {
95-
if (this.contentTarget.innerHTML.endsWith('>')) {
95+
if (this.endsWithToken()) {
9696
this.insertSpaceIfLastCharacter();
9797
}
9898
}
@@ -135,6 +135,15 @@ export default class PatternInputController extends Controller {
135135
}
136136

137137
input_mouseup() {
138+
const selection = document.getSelection();
139+
if (selection?.type === 'Caret' && selection?.anchorOffset === 0 && this.startsWithToken()) {
140+
this.insertSpaceIfFirstCharacter();
141+
}
142+
143+
if (selection?.type === 'Caret' && this.endsWithToken()) {
144+
this.insertSpaceIfLastCharacter();
145+
}
146+
138147
this.setRange();
139148
}
140149

@@ -190,7 +199,7 @@ export default class PatternInputController extends Controller {
190199
testRange.selectNodeContents(this.contentTarget);
191200
testRange.setEnd(range.startContainer, range.startOffset);
192201

193-
// if the resulting range is empty it is at the end of the input
202+
// if the resulting range is empty it is at the start of the input
194203
if (testRange.toString() === '') {
195204
// add a space
196205
const beforeToken = document.createTextNode(' ');
@@ -226,16 +235,24 @@ export default class PatternInputController extends Controller {
226235

227236
setRealCaretPositionAtNode(target:Node, position:'before'|'after' = 'after'):void {
228237
const selection = document.getSelection();
229-
if (selection) {
230-
const postRange = document.createRange();
231-
if (position === 'after') {
232-
postRange.setStartAfter(target);
233-
} else {
234-
postRange.setStartBefore(target);
235-
}
236-
selection?.removeAllRanges();
237-
selection?.addRange(postRange);
238+
if (selection === null) { return; }
239+
240+
const postRange = document.createRange();
241+
if (position === 'after') {
242+
postRange.setStartAfter(target);
243+
} else {
244+
postRange.setStartBefore(target);
238245
}
246+
selection.removeAllRanges();
247+
selection.addRange(postRange);
248+
}
249+
250+
private endsWithToken():boolean {
251+
return this.contentTarget.innerHTML.endsWith('>');
252+
}
253+
254+
private startsWithToken():boolean {
255+
return this.contentTarget.innerHTML.startsWith('<');
239256
}
240257

241258
private insertToken(tokenElement:HTMLElement) {

0 commit comments

Comments
 (0)