Skip to content

Commit 75f835d

Browse files
authored
Merge pull request #152 from WebCoder49/popup-update-onselectionchange
Make selectionchange work with popup; Add selectionStart parameter to popup callback
2 parents 9467517 + 21dc4a4 commit 75f835d

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

code-input.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ export namespace plugins {
9696
class Autocomplete extends Plugin {
9797
/**
9898
* Pass in a function to create a plugin that displays the popup that takes in (popup element, textarea, textarea.selectionEnd).
99-
* @param {(popupElement: HTMLElement, textarea: HTMLTextAreaElement, selectionEnd: number) => void} updatePopupCallback a function to display the popup that takes in (popup element, textarea, textarea.selectionEnd).
99+
* @param {(popupElement: HTMLElement, textarea: HTMLTextAreaElement, selectionEnd: number, selectionStart?: number) => void} updatePopupCallback a function to display the popup that takes in (popup element, textarea, textarea.selectionEnd).
100100
*/
101-
constructor(updatePopupCallback: (popupElem: HTMLElement, textarea: HTMLTextAreaElement, selectionEnd: number) => void);
101+
constructor(updatePopupCallback: (popupElem: HTMLElement, textarea: HTMLTextAreaElement, selectionEnd: number, selectionStart?: number) => void);
102102
}
103103

104104
/**

plugins/autocomplete.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ codeInput.plugins.Autocomplete = class extends codeInput.Plugin {
2020
popupElem.style.left = caretCoords.left + "px";
2121

2222
if(!onlyScrolled) {
23-
this.updatePopupCallback(popupElem, textarea, textarea.selectionEnd);
23+
this.updatePopupCallback(popupElem, textarea, textarea.selectionEnd, textarea.selectionStart);
2424
}
2525
}
2626
/* Create the popup element */
@@ -45,7 +45,7 @@ codeInput.plugins.Autocomplete = class extends codeInput.Plugin {
4545

4646
let textarea = codeInput.textareaElement;
4747
textarea.addEventListener("input", () => { this.updatePopup(codeInput, false)});
48-
textarea.addEventListener("click", () => { this.updatePopup(codeInput, false)});
48+
textarea.addEventListener("selectionchange", () => { this.updatePopup(codeInput, false)});
4949
}
5050
/**
5151
* Return the coordinates of the caret in a code-input
@@ -85,4 +85,4 @@ codeInput.plugins.Autocomplete = class extends codeInput.Plugin {
8585
return {"top": afterSpan.offsetTop - textarea.scrollTop, "left": afterSpan.offsetLeft - textarea.scrollLeft};
8686
}
8787
updatePopupCallback = function() {};
88-
}
88+
}

tests/tester.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ function beginTest(isHLJS) {
110110
if(isHLJS) {
111111
codeInput.registerTemplate("code-editor", new codeInput.templates.Hljs(hljs, [
112112
new codeInput.plugins.AutoCloseBrackets(),
113-
new codeInput.plugins.Autocomplete(function(popupElem, textarea, selectionEnd) {
114-
if(textarea.value.substring(selectionEnd-5, selectionEnd) == "popup") {
113+
new codeInput.plugins.Autocomplete(function(popupElem, textarea, selectionEnd, selectionStart) {
114+
if(selectionStart == selectionEnd && textarea.value.substring(selectionEnd-5, selectionEnd) == "popup") {
115115
// Show popup
116116
popupElem.style.display = "block";
117117
popupElem.innerHTML = "Here's your popup!";
@@ -129,8 +129,8 @@ function beginTest(isHLJS) {
129129
} else {
130130
codeInput.registerTemplate("code-editor", new codeInput.templates.Prism(Prism, [
131131
new codeInput.plugins.AutoCloseBrackets(),
132-
new codeInput.plugins.Autocomplete(function(popupElem, textarea, selectionEnd) {
133-
if(textarea.value.substring(selectionEnd-5, selectionEnd) == "popup") {
132+
new codeInput.plugins.Autocomplete(function(popupElem, textarea, selectionEnd, selectionStart) {
133+
if(selectionStart == selectionEnd && textarea.value.substring(selectionEnd-5, selectionEnd) == "popup") {
134134
// Show popup
135135
popupElem.style.display = "block";
136136
popupElem.innerHTML = "Here's your popup!";
@@ -343,12 +343,23 @@ console.log("I've got another line!", 2 < 3, "should be true.");
343343

344344
await waitAsync(50); // Wait for popup to be rendered
345345

346-
testAssertion("Autocomplete", "Popup Shows", confirm("Does the autocomplete popup display correctly? (OK=Yes)"), "user-judged");
347-
backspace(textarea);
346+
testAssertion("Autocomplete", "Popup Shows on input", confirm("Does the autocomplete popup display correctly? (OK=Yes)"), "user-judged");
347+
move(textarea, -1);
348348

349349
await waitAsync(50); // Wait for popup disappearance to be rendered
350350

351-
testAssertion("Autocomplete", "Popup Disappears", confirm("Has the popup disappeared? (OK=Yes)"), "user-judged");
351+
testAssertion("Autocomplete", "Popup Disappears on arrow key", confirm("Has the popup disappeared? (OK=Yes)"), "user-judged");
352+
move(textarea, 1);
353+
354+
await waitAsync(50); // Wait for popup to be rendered
355+
356+
testAssertion("Autocomplete", "Popup Shows on arrow key", confirm("Does the autocomplete popup display correctly? (OK=Yes)"), "user-judged");
357+
backspace(textarea);
358+
359+
await waitAsync(50); // Wait for popup disappearance to be rendered
360+
361+
testAssertion("Autocomplete", "Popup Disappears on backspace", confirm("Has the popup disappeared? (OK=Yes)"), "user-judged");
362+
move(textarea, 1);
352363
backspace(textarea);
353364
backspace(textarea);
354365
backspace(textarea);

0 commit comments

Comments
 (0)